Skip to content

Commit a3d3b17

Browse files
authored
TRQL: always add FINAL keyword (#3051)
For now we’re going to always add FINAL to TRQL queries for data correctness. In the future we will implement an automated optimization where we use `SELECT argMax(column, _version)` and `WHERE _is_deleted = 0`. But this is a more complex change and needs more investigation of downsides.
1 parent 35e11e0 commit a3d3b17

File tree

1 file changed

+8
-3
lines changed

1 file changed

+8
-3
lines changed

internal-packages/tsql/src/query/printer.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1559,9 +1559,14 @@ export class ClickHousePrinter {
15591559
joinStrings.push(`AS ${this.printIdentifier(node.alias)}`);
15601560
}
15611561

1562-
// Add FINAL
1563-
if (node.table_final) {
1564-
joinStrings.push("FINAL");
1562+
// Always add FINAL for direct table references to ensure deduplicated results
1563+
// from ReplacingMergeTree tables in ClickHouse
1564+
if (node.table) {
1565+
const tableExpr = node.table;
1566+
const isDirectTable = (tableExpr as Field).expression_type === "field";
1567+
if (isDirectTable) {
1568+
joinStrings.push("FINAL");
1569+
}
15651570
}
15661571

15671572
// Add SAMPLE

0 commit comments

Comments
 (0)