Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

IGNITE-21492 SQL Calcite: Fix expand star for system fields #11393

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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();
}
}
Loading