[ 
https://issues.apache.org/jira/browse/CALCITE-4541?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel
 ]

yanjing.wang updated CALCITE-4541:
----------------------------------
    Description: 
{code:java}
String sql = "select 20210101 as ds , count(distinct id) as c from users where 
ds = 20210101 group by ds";
{code}
 

'group by ds' expression will be expanded to 'group by 20210101' at line 
'expandGroupByOrHavingExpr' of 'validateGroupClause(SqlSelect select)' method.

 

but when GroupByScope 'validateExpr(SqlNode expr)', it will check if 'group by 
20210101' is a group by ordinal literal such as 'group by 1, 2' etc. . and in 
'visit(SqlLiteral literal)' method of 'ExtendedExpander' class does this. 

 

the problem raises when check if it is a 'group by ordinal', it only has a 
condition 

 
{code:java}
boolean isOrdinalLiteral = literal == root;  
{code}
this is always true in this scenario.

 

it raise exception 'SqlValidatorException: Ordinal out of range when executing 
following code.
{code:java}
if (intValue < 1 || intValue > select.getSelectList().size()) { throw 
validator.newValidationError(literal, RESOURCE.orderByOrdinalOutOfRange()); 
}{code}
 

I think whether we need check 'originalExprs' instance variable of 
sqlValidatorImpl class contains the literal. if so, it can be verified as not 
an ordinal. but I'm not sure whether this approach is correct.

 

 

 

  was:
{code:java}
String sql = "select 20210101 as ds , count(distinct id) as c from users where 
ds = 20210101 group by ds";
{code}
 

'group by ds' expression will be expanded to 'group by 20210101' at line 
'expandGroupByOrHavingExpr' of 'validateGroupClause(SqlSelect select)' method.

 

but when GroupByScope 'validateExpr(SqlNode expr)', it will check if 'group by 
20210101' is a group by ordinal literal such as 'group by 1, 2' etc. . and in 
'visit(SqlLiteral literal)' method of 'ExtendedExpander' class does this. 

 

the problem raises when check if it is a 'group by ordinal', it only has a 
condition 

 
{code:java}
boolean isOrdinalLiteral = literal == root;  
{code}
this is always true in this scenario.

 

it raise exception 'SqlValidatorException: Ordinal out of range when executing 
following code.
{code:java}
if (intValue < 1 || intValue > select.getSelectList().size()) { throw 
validator.newValidationError(literal, RESOURCE.orderByOrdinalOutOfRange()); 
}{code}
 

I think if we need check 'originalExprs' instance variable of sqlValidatorImpl 
class contains the literal. if so, it can be verified as not an ordinal. but 
I'm not sure whether this approach is correct.

 

 

 


> group by numerical literal will collide with group by ordinal in default sql 
> validator
> --------------------------------------------------------------------------------------
>
>                 Key: CALCITE-4541
>                 URL: https://issues.apache.org/jira/browse/CALCITE-4541
>             Project: Calcite
>          Issue Type: Bug
>          Components: core
>    Affects Versions: 1.26.0
>         Environment: open-jdk8
>            Reporter: yanjing.wang
>            Priority: Major
>             Fix For: 1.27.0
>
>
> {code:java}
> String sql = "select 20210101 as ds , count(distinct id) as c from users 
> where ds = 20210101 group by ds";
> {code}
>  
> 'group by ds' expression will be expanded to 'group by 20210101' at line 
> 'expandGroupByOrHavingExpr' of 'validateGroupClause(SqlSelect select)' method.
>  
> but when GroupByScope 'validateExpr(SqlNode expr)', it will check if 'group 
> by 20210101' is a group by ordinal literal such as 'group by 1, 2' etc. . and 
> in 'visit(SqlLiteral literal)' method of 'ExtendedExpander' class does this. 
>  
> the problem raises when check if it is a 'group by ordinal', it only has a 
> condition 
>  
> {code:java}
> boolean isOrdinalLiteral = literal == root;  
> {code}
> this is always true in this scenario.
>  
> it raise exception 'SqlValidatorException: Ordinal out of range when 
> executing following code.
> {code:java}
> if (intValue < 1 || intValue > select.getSelectList().size()) { throw 
> validator.newValidationError(literal, RESOURCE.orderByOrdinalOutOfRange()); 
> }{code}
>  
> I think whether we need check 'originalExprs' instance variable of 
> sqlValidatorImpl class contains the literal. if so, it can be verified as not 
> an ordinal. but I'm not sure whether this approach is correct.
>  
>  
>  



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to