-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #15 from dataform-co/remove-sql-package
Remove sql package usage
- Loading branch information
Showing
9 changed files
with
270 additions
and
24 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,4 +3,4 @@ | |
"assertionSchema": "segment_dataform_package", | ||
"warehouse": "bigquery", | ||
"gcloudProjectId": "dataform-corp" | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,129 @@ | ||
const getDialect = () => { | ||
const dataformWarehouse = global.dataform.projectConfig.warehouse; | ||
if (!dataformWarehouse) { | ||
return "standard"; | ||
} | ||
return { | ||
bigquery: "standard", | ||
redshift: "redshift", | ||
postgres: "postgres", | ||
snowflake: "snowflake", | ||
sqldatawarehouse: "mssql", | ||
}[dataformWarehouse]; | ||
}; | ||
|
||
const timestampDiff = (datePart, start, end) => { | ||
const dialect = getDialect(); | ||
if (dialect === "snowflake" || dialect === "redshift") { | ||
return `datediff(${datePart}, ${start}, ${end})`; | ||
} | ||
return `timestamp_diff(${end}, ${start}, ${datePart})`; | ||
}; | ||
|
||
const windowFunction = ( | ||
name, | ||
value, | ||
ignoreNulls = false, | ||
windowSpecification | ||
) => { | ||
const dialect = getDialect(); | ||
const partitionFieldsAsString = windowSpecification.partitionFields | ||
? [...windowSpecification.partitionFields].join(`, `) | ||
: ""; | ||
const orderFieldsAsString = windowSpecification.orderFields | ||
? [...windowSpecification.orderFields].join(`, `) | ||
: ""; | ||
|
||
if ( | ||
dialect === "standard" || | ||
dialect === "mssql" || | ||
dialect === "snowflake" | ||
) { | ||
return `${name}(${value} ${ignoreNulls ? `ignore nulls` : ``}) over (${ | ||
windowSpecification.partitionFields | ||
? `partition by ${partitionFieldsAsString}` | ||
: `` | ||
} ${ | ||
windowSpecification.orderFields ? `order by ${orderFieldsAsString}` : `` | ||
} ${ | ||
windowSpecification.frameClause ? windowSpecification.frameClause : `` | ||
})`; | ||
} | ||
|
||
// For some window functions in Redshift, a frame clause is always required | ||
const requiresFrame = [ | ||
"avg", | ||
"count", | ||
"first_value", | ||
"last_value", | ||
"max", | ||
"min", | ||
"nth_value", | ||
"stddev_samp", | ||
"stddev_pop", | ||
"stddev", | ||
"sum", | ||
"variance", | ||
"var_samp", | ||
"var_pop", | ||
].includes(name.toLowerCase()); | ||
|
||
if (dialect === "redshift") { | ||
return `${name}(${value} ${ignoreNulls ? `ignore nulls` : ``}) over (${ | ||
windowSpecification.partitionFields | ||
? `partition by ${partitionFieldsAsString}` | ||
: `` | ||
} ${ | ||
windowSpecification.orderFields ? `order by ${orderFieldsAsString}` : `` | ||
} ${ | ||
windowSpecification.orderFields | ||
? windowSpecification.frameClause | ||
? windowSpecification.frameClause | ||
: requiresFrame | ||
? `rows between unbounded preceding and unbounded following` | ||
: `` | ||
: `` | ||
})`; | ||
} | ||
|
||
if (dialect === "postgres") { | ||
return `${name}(${value}) over (${ | ||
windowSpecification.partitionFields | ||
? `partition by ${partitionFieldsAsString}` | ||
: `` | ||
} ${windowSpecification.orderFields || ignoreNulls ? `order by` : ``} ${ | ||
ignoreNulls ? `case when ${value} is not null then 0 else 1 end asc` : `` | ||
} ${orderFieldsAsString && ignoreNulls ? `,` : ``} ${orderFieldsAsString} ${ | ||
windowSpecification.orderFields | ||
? windowSpecification.frameClause | ||
? windowSpecification.frameClause | ||
: requiresFrame | ||
? `rows between unbounded preceding and unbounded following` | ||
: `` | ||
: `` | ||
})`; | ||
} | ||
}; | ||
|
||
const asString = (castableToString) => { | ||
const dialect = getDialect(); | ||
if (dialect === "postgres" || dialect === "redshift") { | ||
return `cast(${castableToString} as varchar)`; | ||
} | ||
return `cast(${castableToString} as string)`; | ||
}; | ||
|
||
const surrogateKey = (columnNames) => { | ||
const dialect = getDialect(); | ||
const columnsAsStrings = columnNames.map((id) => asString(id)).join(`,`); | ||
if (dialect === "standard") { | ||
return asString(`farm_fingerprint(concat(${columnsAsStrings}))`); | ||
} | ||
return asString(`md5(concat(${columnsAsStrings}))`); | ||
}; | ||
|
||
module.exports = { | ||
timestampDiff, | ||
windowFunction, | ||
surrogateKey, | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
const sql = require("@dataform/sql")(); | ||
const sql = require("./sql") | ||
|
||
let USER = `coalesce( | ||
identifies.user_id, | ||
|
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,6 @@ | ||
{ | ||
"name": "dataform-segment", | ||
"dependencies": { | ||
"@dataform/core": "1.15.5", | ||
"@dataform/sql": "0.2.0" | ||
"@dataform/core": "1.15.5" | ||
} | ||
} |