Skip to content
Merged
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
11 changes: 8 additions & 3 deletions internal-packages/tsql/src/query/printer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1559,9 +1559,14 @@ export class ClickHousePrinter {
joinStrings.push(`AS ${this.printIdentifier(node.alias)}`);
}
Comment on lines 1559 to 1560

Choose a reason for hiding this comment

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

🚩 Pre-existing double-alias bug for direct table references with explicit aliases

When a direct table reference has an explicit alias (e.g., FROM runs AS r), the code at internal-packages/tsql/src/query/printer.ts:1494-1497 already embeds the alias into the table string: clickhouseName AS r. Then at internal-packages/tsql/src/query/printer.ts:1557-1559, if node.alias is set, it pushes AS r again. This would produce clickhouseName AS r AS r FINAL, which is invalid ClickHouse syntax.

This is pre-existing (not introduced by this PR), and no tests exercise explicit table aliases in FROM clauses. The alias block at line 1557-1559 appears intended for subqueries/placeholders (which don't embed their own alias), but it fires unconditionally for all expression types including direct table references that already include the alias.

With this PR always adding FINAL, the invalid output becomes more likely to surface if table aliases are ever used in TSQL queries.

(Refers to lines 1557-1560)

Open in Devin Review

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


// Add FINAL
if (node.table_final) {
joinStrings.push("FINAL");
// Always add FINAL for direct table references to ensure deduplicated results
// from ReplacingMergeTree tables in ClickHouse
if (node.table) {
const tableExpr = node.table;
const isDirectTable = (tableExpr as Field).expression_type === "field";
if (isDirectTable) {
joinStrings.push("FINAL");
}
}

// Add SAMPLE
Expand Down