Skip to content

Commit

Permalink
IGNITE-21492 SQL Calcite: Fix expand star for system fields - Fixes #…
Browse files Browse the repository at this point in the history
…11393.

Signed-off-by: Aleksey Plekhanov <plehanov.alex@gmail.com>
  • Loading branch information
alex-plekhanov committed Jun 19, 2024
1 parent 8f1d5a2 commit f67f936
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@
import org.apache.calcite.sql.type.SqlTypeFamily;
import org.apache.calcite.sql.type.SqlTypeName;
import org.apache.calcite.sql.validate.SelectScope;
import org.apache.calcite.sql.validate.SqlQualified;
import org.apache.calcite.sql.validate.SqlValidator;
import org.apache.calcite.sql.validate.SqlValidatorImpl;
import org.apache.calcite.sql.validate.SqlValidatorNamespace;
Expand Down Expand Up @@ -368,10 +369,31 @@ private SqlNode rewriteTableToQuery(SqlNode from) {
}

/** {@inheritDoc} */
@Override protected void addToSelectList(List<SqlNode> list, Set<String> aliases,
List<Map.Entry<String, RelDataType>> fieldList, SqlNode exp, SelectScope scope, boolean includeSystemVars) {
if (includeSystemVars || exp.getKind() != SqlKind.IDENTIFIER || !isSystemFieldName(deriveAlias(exp, 0)))
super.addToSelectList(list, aliases, fieldList, exp, scope, includeSystemVars);
@Override protected void addToSelectList(
List<SqlNode> list,
Set<String> aliases,
List<Map.Entry<String, RelDataType>> fieldList,
SqlNode exp,
SelectScope scope,
boolean includeSysVars
) {
if (!includeSysVars && exp.getKind() == SqlKind.IDENTIFIER && isSystemFieldName(deriveAlias(exp, 0))) {
SqlQualified qualified = scope.fullyQualify((SqlIdentifier)exp);

if (qualified.namespace == null)
return;

if (qualified.namespace.getTable() != null) {
// If child is table and has only system fields, expand star to these fields.
// Otherwise, expand star to non-system fields only.
for (RelDataTypeField fld : qualified.namespace.getRowType().getFieldList()) {
if (!isSystemField(fld))
return;
}
}
}

super.addToSelectList(list, aliases, fieldList, exp, scope, includeSysVars);
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@

package org.apache.ignite.internal.processors.query.calcite.integration;

import java.util.Collections;
import org.apache.ignite.IgniteCache;
import org.apache.ignite.cache.QueryEntity;
import org.apache.ignite.configuration.CacheConfiguration;
import org.junit.Test;

import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -101,4 +105,36 @@ public void columnNames() {

assertQuery("select 1, -1, 'some string' from person").columnNames("1", "-1", "'some string'").check();
}

/** Test implicit system fields expand by star. */
@Test
public void testSystemFieldsStarExpand() {
IgniteCache<Integer, Integer> cache = client.createCache(new CacheConfiguration<Integer, Integer>("test")
.setSqlSchema("PUBLIC")
.setQueryEntities(
Collections.singletonList(new QueryEntity()
.setTableName("test")
.setKeyType(Integer.class.getName())
.setValueType(Integer.class.getName())
)
)
);

cache.put(0, 0);

assertQuery("select * from test")
.columnNames("_KEY", "_VAL").returns(0, 0).check();

assertQuery("select * from (select * from test)")
.columnNames("_KEY", "_VAL").returns(0, 0).check();

assertQuery("select _KEY, _VAL from (select * from test) as t")
.columnNames("_KEY", "_VAL").returns(0, 0).check();

assertQuery("select * from (select _KEY, _VAL from test) as t")
.columnNames("_KEY", "_VAL").returns(0, 0).check();

assertQuery("select * from (select _KEY, _VAL as OTHER from test) as t")
.columnNames("_KEY", "OTHER").returns(0, 0).check();
}
}

0 comments on commit f67f936

Please sign in to comment.