Skip to content
This repository was archived by the owner on Jun 13, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion dataform.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@
"assertionSchema": "segment_dataform_package",
"warehouse": "bigquery",
"gcloudProjectId": "dataform-corp"
}
}
9 changes: 0 additions & 9 deletions environments.json

This file was deleted.

4 changes: 2 additions & 2 deletions includes/sessionized_events.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const sql = require("@dataform/sql")();
const sql = require("./sql")
const segmentCommon = require("./common");

module.exports = (params) => {
Expand Down Expand Up @@ -42,7 +42,7 @@ select
*,
coalesce(
(
${sql.timestamps.diff(`millisecond`,
${sql.timestampDiff(`millisecond`,
sql.windowFunction(
"lag",
"timestamp",
Expand Down
4 changes: 2 additions & 2 deletions includes/sessions.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const segmentCommon = require("./common");
const sql = require("@dataform/sql")();
const sql = require("./sql")

module.exports = (params) => {

Expand Down Expand Up @@ -92,7 +92,7 @@ select
${ctx.when(global.dataform.projectConfig.warehouse == "bigquery", `struct(\n `)}
${segmentCommon.enabledEvents(params).map((event) =>
`count(segment_sessionized_events.${event}_id) as total_${event}s`).join(`,\n `)},
${sql.timestamps.diff("millisecond", "min(segment_sessionized_events.timestamp)", "max(segment_sessionized_events.timestamp)")} as duration_millis
${sql.timestampDiff("millisecond", "min(segment_sessionized_events.timestamp)", "max(segment_sessionized_events.timestamp)")} as duration_millis
${ctx.when(global.dataform.projectConfig.warehouse == "bigquery", `) as stats`)}

-- first values in the session for page fields
Expand Down
129 changes: 129 additions & 0 deletions includes/sql.js
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,
};
2 changes: 1 addition & 1 deletion includes/user_map.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
const sql = require("@dataform/sql")();
const sql = require("./sql")
const segmentCommon = require("./common");

module.exports = (params) => {
Expand Down
2 changes: 1 addition & 1 deletion includes/users.js
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,
Expand Down
139 changes: 133 additions & 6 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 1 addition & 2 deletions package.json
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"
}
}