You are viewing a plain text version of this content. The canonical link for it is here.
Posted to reviews@iotdb.apache.org by "DaZuiZui (via GitHub)" <gi...@apache.org> on 2026/06/16 11:41:22 UTC

[PR] [IOTDB-17797] Support lateral column aliases in table SELECT list [iotdb]

DaZuiZui opened a new pull request, #17960:
URL: https://github.com/apache/iotdb/pull/17960

   ## Description
   
   ### Support lateral column aliases in table SELECT lists
   
   This PR implements Part 2 of #17797 for the table-model analyzer: later `SingleColumn` SELECT items can reference aliases explicitly defined by earlier `SingleColumn` items in the same SELECT list.
   
   Examples now supported:
   
   ```sql
   SELECT s1 AS x, x + 1 AS y FROM table1;
   SELECT s1 AS x, x + 1 AS y FROM table1 ORDER BY y;
   SELECT s1 AS x, row_number() OVER (ORDER BY x) AS rn FROM table1;
   SELECT s1 AS x, avg(s2) OVER (PARTITION BY x) AS a FROM table1;
   ```
   
   ### Name-resolution behavior
   
   The LCA rewrite is left-to-right and only applies to unqualified identifiers in later SELECT items. It does not rewrite qualified references such as `t.x`, dereference expressions such as `x.y`, or identifiers inside subqueries.
   
   Resolution order is:
   
   1. local source column
   2. visible previous SELECT aliases
   3. existing analyzer resolution
   
   If multiple previous aliases with the same canonical name are visible and no local source column wins, the analyzer raises a clear ambiguity error. `AllColumns` and `COLUMNS(...)` do not register reusable LCA aliases.
   
   `GROUP BY` alias reuse uses the LCA-rewritten SELECT expression, while `ORDER BY` keeps the existing output-alias precedence. `WHERE` and `HAVING` still do not see SELECT aliases.
   
   For `CAST(value AS type)`, only `value` participates in LCA rewriting. Type names are not treated as alias references.
   
   ### Implementation notes
   
   The original AST is kept unchanged. The analyzer records each `SingleColumn`'s semantic expression after LCA rewriting and uses it for type analysis, source-column tracking, output expression analysis, and GROUP BY alias reuse. Output field names without explicit aliases are still derived from the original SELECT item text rather than from the rewritten expression.
   
   Inline window specifications in later SELECT items are rewritten and registered against the rewritten `FunctionCall` nodes before expression analysis. Referencing an alias whose expression contains a window function is rejected explicitly.
   
   Fixes #17797
   
   <hr>
   
   This PR has:
   - [x] been self-reviewed.
   - [x] added documentation for new or modified features or behaviors.
   - [x] added comments explaining the "why" and the intent of the code wherever would not be obvious for an unfamiliar reader.
   - [x] added unit tests or modified existing tests to cover new code paths, ensuring the threshold for code coverage.
   
   <hr>
   
   ##### Key changed/added classes (or packages if there are too many classes) in this PR
   
   - `StatementAnalyzer`: SELECT-list LCA rewrite, rewritten SELECT expression tracking, output-scope name handling, and window metadata registration for rewritten expressions.
   - `ExpressionRewriter` / `ExpressionTreeRewriter`: support for leaf expression rewrite hooks used by LCA expression copying.
   - `SelectAliasReuseTest`: coverage for LCA chaining, collisions, ambiguity, aggregates, GROUP BY/ORDER BY reuse, window specs, CAST type-name collisions, WHERE/HAVING isolation, and output field names.
   - `relational/analyzer/README.md`: documented table-model SELECT alias resolution rules.
   
   ##### Tests
   
   ```bash
   ./mvnw spotless:apply -pl iotdb-core/datanode
   ./mvnw test -pl iotdb-core/datanode -Dtest=SelectAliasReuseTest
   ```
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [IOTDB-17797] Support lateral column aliases in table SELECT list [iotdb]

Posted by "DaZuiZui (via GitHub)" <gi...@apache.org>.
DaZuiZui commented on PR #17960:
URL: https://github.com/apache/iotdb/pull/17960#issuecomment-4861941714

   Thanks for the detailed review and the measurements.
   
   I’ve addressed the performance/robustness issue you pointed out in the latest commit. The LCA rewrite no longer deep-copies/inlines the already-expanded expression from the previous alias. Instead, the planner reuses previous LCA projections through symbol mappings and appends SELECT projections in batches, so deep chained aliases should no longer cause the O(N^2) plan-expression / EXPLAIN JSON blowup.
   
   I also added regression tests for this case, including:
   - reusing a previous `CAST(...) AS x` projection without duplicating the Cast expression
   - a 200-column chained LCA case that keeps plan expressions shallow and the plan JSON bounded
   
   Verified with:
   
   ./mvnw test -pl iotdb-core/datanode -Dtest=SelectAliasReuseTest
   
   All 40 tests passed. Please take another look when you have time.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [IOTDB-17797] Support lateral column aliases in table SELECT list [iotdb]

Posted by "DaZuiZui (via GitHub)" <gi...@apache.org>.
DaZuiZui commented on PR #17960:
URL: https://github.com/apache/iotdb/pull/17960#issuecomment-4738910097

   Hi @JackieTien97 ,
   Thanks for the careful review and helpful suggestions.
   
   I have addressed the latest review comments in PR #17960. The SELECT-list LCA alias lookup now avoids rebuilding immutable snapshots for each SingleColumn, and I added regression coverage for the wide SELECT-list LCA chain, delimited alias case sensitivity, named WINDOW definitions, DISTINCT without ORDER BY, and LCA references in window frame bounds.
   
   I verified the changes with:
   
   ./mvnw spotless:apply -pl iotdb-core/datanode
   ./mvnw -nsu test -pl iotdb-core/datanode -Dtest=SelectAliasReuseTest
   Thanks again for the review.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [IOTDB-17797] Support lateral column aliases in table SELECT list [iotdb]

Posted by "DaZuiZui (via GitHub)" <gi...@apache.org>.
DaZuiZui commented on code in PR #17960:
URL: https://github.com/apache/iotdb/pull/17960#discussion_r3432903471


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java:
##########
@@ -1670,11 +2185,25 @@ private SelectAnalysis analyzeSelect(QuerySpecification node, Scope scope) {
               outputPosition++;
             }
           } else {
+            SelectAliasLookup visibleAliases = visibleAliasBuilder.build();

Review Comment:
   Thanks for the suggestion. Addressed in 3bd3a8d445: SELECT-list LCA rewriting now reads from a builder-backed SelectAliasResolver while walking the SELECT list, so it avoids rebuilding immutable snapshots for each SingleColumn and only snapshots once for later clauses. I also added regression coverage for a wide SELECT-list LCA chain, delimited alias case sensitivity, named WINDOW definitions not seeing SELECT aliases, DISTINCT with LCA and no ORDER BY, and LCA references in window frame bounds.
   
   Verified with:
   
   ```
   ./mvnw spotless:apply -pl iotdb-core/datanode
   ./mvnw -nsu test -pl iotdb-core/datanode -Dtest=SelectAliasReuseTest
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [IOTDB-17797] Support lateral column aliases in table SELECT list [iotdb]

Posted by "JackieTien97 (via GitHub)" <gi...@apache.org>.
JackieTien97 commented on code in PR #17960:
URL: https://github.com/apache/iotdb/pull/17960#discussion_r3432746539


##########
iotdb-core/datanode/src/main/java/org/apache/iotdb/db/queryengine/plan/relational/analyzer/StatementAnalyzer.java:
##########
@@ -1670,11 +2185,25 @@ private SelectAnalysis analyzeSelect(QuerySpecification node, Scope scope) {
               outputPosition++;
             }
           } else {
+            SelectAliasLookup visibleAliases = visibleAliasBuilder.build();

Review Comment:
   Nit / follow-up suggestion: this builds a fresh immutable `SelectAliasLookup` for every `SingleColumn`. Since the `SelectAliasLookup` constructor copies the whole current alias map and each alias list, analyzing a very wide SELECT list with many reusable aliases ends up doing repeated prefix copies, i.e. roughly O(N^2) alias-map copying.
   
   This is probably fine for normal SELECT lists, but it may be worth avoiding the repeated snapshots by using an incremental/shared immutable lookup structure, or by exposing a read-only lookup view over the builder whose visible state grows left-to-right.
   
   Could you also add IT/regression coverage for this area? In particular, it would be useful to cover a wide SELECT-list LCA chain/reuse case, plus the edge cases around delimited/quoted alias case sensitivity, named `WINDOW` definitions not seeing SELECT aliases, `DISTINCT` with LCA and no `ORDER BY`, and LCA references in window frame bounds.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [IOTDB-17797] Support lateral column aliases in table SELECT list [iotdb]

Posted by "DaZuiZui (via GitHub)" <gi...@apache.org>.
DaZuiZui commented on PR #17960:
URL: https://github.com/apache/iotdb/pull/17960#issuecomment-4726909808

   I have completed the implementation for Issue #17797 Part 2: supporting lateral column aliases in the table-model SELECT list.
   
   The PR is available here:
   
   https://github.com/apache/iotdb/pull/17960
   
   This PR adds left-to-right SELECT-list alias resolution, keeps local input columns at higher priority than SELECT aliases, avoids rewriting qualified expressions and subqueries, preserves WHERE/HAVING semantics, and keeps the existing ORDER BY alias behavior. It also includes analyzer tests and a plan-level test to ensure reused aliases do not cause duplicated projection computation.
   
   I have verified the changes with:
   
   ./mvnw spotless:apply -pl iotdb-core/datanode
   ./mvnw -nsu test -pl iotdb-core/datanode -Dtest=SelectAliasReuseTest
   The PR is ready for review. It will close #17797 after being merged.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [IOTDB-17797] Support lateral column aliases in table SELECT list [iotdb]

Posted by "DaZuiZui (via GitHub)" <gi...@apache.org>.
DaZuiZui commented on PR #17960:
URL: https://github.com/apache/iotdb/pull/17960#issuecomment-4861924692

   I reviewed the latest commit 01af2d3fee and checked how it addresses the previous LCA plan blowup issue.
   
   Specifically, I verified that the analyzer no longer deep-copies the expanded alias expression, and that the planner now reuses previous LCA projections through symbol mappings with batched projections. I also checked the new regression tests for the Cast reuse case and the deep chained-alias case.
   
   I ran:
   
   ./mvnw test -pl iotdb-core/datanode -Dtest=SelectAliasReuseTest
   
   All 40 tests passed. The performance/robustness issue I raised before looks resolved to me.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] [IOTDB-17797] Support lateral column aliases in table SELECT list [iotdb]

Posted by "JackieTien97 (via GitHub)" <gi...@apache.org>.
JackieTien97 merged PR #17960:
URL: https://github.com/apache/iotdb/pull/17960


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: reviews-unsubscribe@iotdb.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org