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

Aleksey Plekhanov commented on CALCITE-4923:
--------------------------------------------

[~julianhyde], this bug is not reproduced in pure Calcite. As I said before - 
it's reproduced when we override {{SqlValidatorImpl.addToSelectList()}} to 
filter out some system columns.
For example, if there is "ROWID" system column in each our table and we wish to 
hide it from the user (until the user requests it explicitly), we can override 
the {{addToSelectList}} method to something like this:
{code:java}
    @Override protected void addToSelectList(List<SqlNode> list, Set<String> 
aliases,
        List<Map.Entry<String, RelDataType>> fieldList,
        SqlNode exp, SelectScope scope, boolean includeSystemVars) {
      if (!"ROWID".equals(SqlValidatorUtil.getAlias(exp, -1)))
        super.addToSelectList(list, aliases, fieldList, exp, scope, 
includeSystemVars);
    }
{code}
In this case, using "star" with NATURAL JOIN or JOIN with USING containing 
tables with "ROWID" columns will fail, for example:
{noformat}
CREATE TABLE t1 (rowid INT, a INT, b VARCHAR);
CREATE TABLE t2 (rowid INT, a INT, c VARCHAR);
SELECT * FROM t1 JOIN t2 USING (a);
{noformat}
Fails with:
{noformat}
Caused by: java.lang.ArrayIndexOutOfBoundsException: Index 4 out of bounds for 
length 4
        at 
com.google.common.collect.RegularImmutableList.get(RegularImmutableList.java:75)
        at 
org.apache.calcite.sql.validate.SqlValidatorImpl$Permute.permute(SqlValidatorImpl.java:7109)
        at 
org.apache.calcite.sql.validate.SqlValidatorImpl.expandStar(SqlValidatorImpl.java:664)
{noformat}
But if we don't use "star", for example:
{noformat}
SELECT b, c FROM t1 JOIN t2 USING (a);
{noformat}
The query runs successfully. All other types of queries with "star", except 
NATURAL JOINs and JOINs with USING also work fine.

I see no other ways to exclude system columns except overriding 
{{{}addToSelectList{}}}. Perhaps it also can be done after the 
{{SqlValidatorImpl.expandStar()}} method execution (filter out 
{{{}selectItems{}}}, {{aliases,}} and {{fields}} by overriding this method) but 
{{SqlValidatorImpl.expandStar()}} method is private now. Can we make it 
protected?

> Expand "star" for NATURAL JOIN fails if some columns are filtered out by 
> addToSelectList
> ----------------------------------------------------------------------------------------
>
>                 Key: CALCITE-4923
>                 URL: https://issues.apache.org/jira/browse/CALCITE-4923
>             Project: Calcite
>          Issue Type: Bug
>            Reporter: Aleksey Plekhanov
>            Priority: Major
>
> If all columns ("*") are requested and some table columns are filtered out by 
> overridden {{SqlValidatorImpl.addToSelectList()}} method (for example, some 
> system columns, which we don't want to show to user until it explicitly 
> requests it) query with NATURAL JOIN and JOIN with USING fail on 
> {{SqlValidatorImpl.expandStar()}} with error:
> {noformat}
> java.lang.ArrayIndexOutOfBoundsException: 7
>       at 
> com.google.common.collect.RegularImmutableList.get(RegularImmutableList.java:60)
>       at 
> org.apache.calcite.sql.validate.SqlValidatorImpl$Permute.permute(SqlValidatorImpl.java:7109)
>       at 
> org.apache.calcite.sql.validate.SqlValidatorImpl.expandStar(SqlValidatorImpl.java:664)
>       at 
> org.apache.calcite.sql.validate.SqlValidatorImpl.expandSelectItem(SqlValidatorImpl.java:426)
>       at 
> org.apache.calcite.sql.validate.SqlValidatorImpl.validateSelectList(SqlValidatorImpl.java:4409)
>       at 
> org.apache.calcite.sql.validate.SqlValidatorImpl.validateSelect(SqlValidatorImpl.java:3652)
> {noformat}
> Because {{Permute.permute()}} rely on columns count equality in 
> {{selectItems}} and JOIN row type.



--
This message was sent by Atlassian Jira
(v8.20.1#820001)

Reply via email to