[ 
https://issues.apache.org/jira/browse/CALCITE-6939?page=com.atlassian.jira.plugin.system.issuetabpanels:comment-tabpanel&focusedCommentId=17941211#comment-17941211
 ] 

Julian Hyde edited comment on CALCITE-6939 at 4/5/25 8:11 AM:
--------------------------------------------------------------

I don’t think you should resolve left-to-right. This would make some 
refactorings tricky. Allow backward and forward references, but check for 
cycles. I proposed something similar in CALCITE-4496. 



I also don’t agree with the “only if all the other name lookup methods have 
failed” strategy. An unqualified column reference x should prefer a select 
expression to a column in one of the tables in the FROM clause. Otherwise the 
behavior of the query would change if someone added a column x to one of the 
tables. If this is to be a proper feature, it should use a sensible scoping 
order. 

I think you can leave it unspecified whether expressions are repeatable. If 
people want deterministic behavior they can use a CTE. 


was (Author: julianhyde):
I don’t think you should resolve left-to-right. This would make some 
refactorings tricky. Allow backward and forward references, but check for 
cycles. I proposed something similar in CALCITE-4496. 

I think you can leave it unspecified whether expressions are repeatable. If 
people want deterministic behavior they can use a CTE. 

> Add support for Lateral Column Alias
> ------------------------------------
>
>                 Key: CALCITE-6939
>                 URL: https://issues.apache.org/jira/browse/CALCITE-6939
>             Project: Calcite
>          Issue Type: Wish
>          Components: core
>    Affects Versions: 1.39.0
>            Reporter: Mihai Budiu
>            Priority: Minor
>              Labels: pull-request-available
>
> Several SQL dialects allow a SELECT to refer to aliases introduced previously 
> in the same select.
> Examples are: Spark, BigQuery, Snowflake. Other names for this feature are 
> "column aliasing with immediate reference", or "column alias visibility in 
> the same SELECT statement".
> This feature was already proposed and implemented by [CALCITE-5248], but that 
> issue and PR contains multiple other unrelated features. 
> I am also proposing adding a conformance flag `isSelectAlias` which enables 
> this feature. This is strictly a validator feature.
> The semantics has to be clearly defined; we do not want to change the 
> semantics of any existing query. My proposal is to allow a name to be 
> resolved in the current SELECT statement only if all the other name lookup 
> methods have failed. 
> I also think it's a good idea to introduce new aliases from left to right, so 
> a expression can only refer to aliases introduced to the left of it. This 
> removes a few unpleasant corner cases too.
> Another difficult problem is the handling of nondeterministic expressions. 
> When you refer to a prior expression by an alias, can/is the expression 
> re-evaluated? Unfortunately I believe that the current Calcite IR makes it 
> difficult to represent the program in such a way that re-evaluation is 
> avoided. (But this is a problem with many other Calcite rewrite rules as 
> well.) This feature is still useful when applied to deterministic expressions.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

Reply via email to