Skip to content
Open
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
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,19 @@ function quoteIdentForDialect(identifier: string, dialect: string): string {
case "mysql":
case "mariadb":
case "clickhouse":
// A double-quoted token is a string literal in these engines, not a name.
case "bigquery":

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 BigQuery boolean partitions fail comparison

quoteIdentForDialect now lets discovery read a BigQuery boolean partition column. buildPartitionWhereClause compares it with a string literal, which BigQuery rejects.

Learn more

The data diff tool advertises boolean columns for categorical partitioning. The BigQuery driver returns row values as supplied by the client, but executeQuery converts them to strings. buildPartitionWhereClause then wraps each value in single quotes. GoogleSQL does not coerce STRING literals to BOOL for equality comparisons.

Example: Partitioning two BigQuery tables by their is_active boolean column discovers true and false, then generates predicates such as is_active = 'true'. BigQuery rejects the predicate instead of comparing active rows.

Recommended fix: Retain the discovered value type for predicate construction, or bind a typed parameter. Emit TRUE and FALSE for boolean columns while preserving 'true' and 'false' for string columns.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

case "spark":
case "databricks":
Comment on lines +48 to +50

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🔴 Uncapped warehouse queries per partition

For categorical diffs on BigQuery and default Databricks, quoteIdentForDialect lets discovery return one partition per distinct value. runPartitionedDiff starts a separate warehouse diff for every value without a cap or cost confirmation.

Learn more

The data diff tool lets an agent choose categorical partitioning, and its read permission prompt does not describe the job count or cost. Before this change, the double-quoted categorical column on BigQuery and default Databricks selected one constant from each nonempty table. Correct identifier quoting now makes partition discovery retrieve every distinct value across both tables. The partition loop starts a new engine session for every value. The step limit resets in each session, so it does not limit the total work.

Example: Comparing two large BigQuery event tables by a string-valued customer ID with 10,000 distinct customers starts 10,000 separate diffs. The initial approval to read both tables does not disclose that query fan-out.

Recommended fix: Bound discovery and the overall job budget rather than only the steps per partition. Before running above that budget, show a warehouse cost estimate and require explicit confirmation, as required by the cost review rule.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

Comment on lines +48 to +50

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Apostrophes break categorical partition comparisons

quoteIdentForDialect now lets discovery return actual category values, including names with apostrophes. buildPartitionWhereClause doubles the apostrophe, which BigQuery rejects and Databricks drops. The partition either fails or compares the wrong rows.

Learn more

Partition discovery reads distinct category values from the warehouse once the column is quoted correctly. The partition loop builds a separate filter from each returned value. Both filters use buildPartitionWhereClause, which always doubles apostrophes. The dialect-aware batch SQL tests record that BigQuery requires a backslash escape and Databricks interprets doubled apostrophes as string concatenation. The diff therefore does not check the discovered rows in either warehouse for these values.

Example: A partition value of O'Brien produces 'O''Brien'. BigQuery rejects the adjacent literals; Databricks reads them as OBrien and can report an empty, identical partition even if the O'Brien rows differ.

Recommended fix: Bind partition values as parameters where the execution path supports them. Otherwise use a dialect-aware value encoder that preserves apostrophes and backslashes, and add coverage that checks what each warehouse reads rather than only comparing generated SQL strings.

Devin Review


Was this helpful? React with 👍 or 👎 to provide feedback.

case "hive":
return `\`${identifier.replace(/`/g, "``")}\``
case "tsql":
case "fabric":
case "sqlserver":
case "mssql":
return `[${identifier.replace(/\]/g, "]]")}]`
default:
// ANSI SQL: Postgres, Snowflake, BigQuery, DuckDB, Oracle, Redshift, etc.
// ANSI SQL: Postgres, Snowflake, DuckDB, Oracle, Redshift, etc.
return `"${identifier.replace(/"/g, '""')}"`
}
}
Expand Down
19 changes: 17 additions & 2 deletions packages/opencode/test/altimate/data-diff-cross-dialect.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -91,11 +91,26 @@ describe("buildPartitionWhereClause — cross-dialect correctness (the CRITICAL

test("bigquery: DATE_TRUNC uppercase + bare literal", () => {
const sql = buildPartitionWhereClause(col, value, "month", undefined, "bigquery")
// `quoteIdentForDialect` falls through to ANSI double-quotes for bigquery
expect(sql).toContain(`DATE_TRUNC("order_date", MONTH)`)
expect(sql).toContain("DATE_TRUNC(`order_date`, MONTH)")
expect(sql).toContain(`'2026-04-01'`)
})

// A double-quoted token is a string literal on these engines, so `"status" = 'open'`
// compares two constants instead of reading the column.
for (const dialect of ["bigquery", "spark", "databricks", "hive"]) {
test(`${dialect}: the partition column is a backticked name, never a double-quoted string`, () => {
const categorical = buildPartitionWhereClause("status", "open", undefined, undefined, dialect)
expect(categorical).toBe("`status` = 'open'")
const numeric = buildPartitionWhereClause("id", "100", undefined, 50, dialect)
expect(numeric).toBe("`id` >= 100 AND `id` < 150")
expect(buildPartitionWhereClause("order_date", value, "month", undefined, dialect)).not.toContain('"order_date"')
})
}

test("bigquery: a backtick inside the column name is doubled", () => {
expect(buildPartitionWhereClause("we`ird", "x", undefined, undefined, "bigquery")).toBe("`we``ird` = 'x'")
})

// The regression this guards against: before the fix, the orchestrator built
// ONE partition WHERE using `sourceDialect` and passed it to both sides.
// A cross-dialect MSSQL → Postgres diff would send `DATETRUNC`/`CONVERT` to
Expand Down
9 changes: 9 additions & 0 deletions packages/opencode/test/altimate/data-diff-cte.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,15 @@ describe("resolveTableSources", () => {
expect(mysql.targetCtePrefix).toContain("`db`.`orders`")
})

test("dialect-aware quoting: bigquery and databricks use backticks, not string-literal double quotes", () => {
const bigquery = resolveTableSources("proj.analytics.orders", "SELECT 1 AS id", "bigquery", "bigquery")
expect(bigquery.sourceCtePrefix).toContain("SELECT * FROM `proj`.`analytics`.`orders`")
expect(bigquery.sourceCtePrefix).not.toContain('"orders"')

const databricks = resolveTableSources("SELECT 1 AS id", "main.sales.orders", "databricks", "databricks")
expect(databricks.targetCtePrefix).toContain("SELECT * FROM `main`.`sales`.`orders`")
})

test("query detection requires both keyword AND whitespace", () => {
// A table literally named "select" should NOT be treated as a query
const r = resolveTableSources("select", "with")
Expand Down
Loading