You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@ignite.apache.org by "korlov42 (via GitHub)" <gi...@apache.org> on 2024/08/12 11:44:08 UTC

[PR] IGNITE-22772 Sql. Fix row type derivation for renaming in FROM clause [ignite-3]

korlov42 opened a new pull request, #4219:
URL: https://github.com/apache/ignite-3/pull/4219

   https://issues.apache.org/jira/browse/IGNITE-22772
   
   This patch introduces proper handling of system/hidden columns during column renaming in `FROM` clause (`SELECT * FROM my_table AS new_name(new_c1, new_c2, ...)`). Provided solution has one limitation: it's impossible to rename system columns directly. But this limitation has workaround: table may be replaced with sub-select explicitly mentioning system columns in SELECT clause (see last test case in `rename_columns_in_from.test`). 
   
   -------------
   Thank you for submitting the pull request.
   
   To streamline the review process of the patch and ensure better code quality
   we ask both an author and a reviewer to verify the following:
   
   ### The Review Checklist
   - [ ] **Formal criteria:** TC status, codestyle, mandatory documentation. Also make sure to complete the following:  
   \- There is a single JIRA ticket related to the pull request.  
   \- The web-link to the pull request is attached to the JIRA ticket.  
   \- The JIRA ticket has the Patch Available state.  
   \- The description of the JIRA ticket explains WHAT was made, WHY and HOW.  
   \- The pull request title is treated as the final commit message. The following pattern must be used: IGNITE-XXXX Change summary where XXXX - number of JIRA issue.
   - [ ] **Design:** new code conforms with the design principles of the components it is added to.
   - [ ] **Patch quality:** patch cannot be split into smaller pieces, its size must be reasonable.
   - [ ] **Code quality:** code is clean and readable, necessary developer documentation is added if needed.
   - [ ] **Tests code quality:** test set covers positive/negative scenarios, happy/edge cases. Tests are effective in terms of execution time and resources.
   
   ### Notes
   - [Apache Ignite Coding Guidelines](https://cwiki.apache.org/confluence/display/IGNITE/Java+Code+Style+Guide)


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-22772 Sql. Fix row type derivation for renaming in FROM clause [ignite-3]

Posted by "xtern (via GitHub)" <gi...@apache.org>.
xtern commented on code in PR #4219:
URL: https://github.com/apache/ignite-3/pull/4219#discussion_r1715333963


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/IgniteAliasNamespace.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.prepare;
+
+import static org.apache.calcite.util.Static.RESOURCE;
+
+import java.util.List;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.validate.AliasNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorImpl;
+import org.apache.calcite.sql.validate.SqlValidatorNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorTable;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Util;
+import org.apache.calcite.util.mapping.Mapping;
+import org.apache.calcite.util.mapping.MappingType;
+import org.apache.calcite.util.mapping.Mappings;
+import org.apache.ignite.internal.sql.engine.schema.ColumnDescriptor;
+import org.apache.ignite.internal.sql.engine.schema.TableDescriptor;
+
+/**
+ * Alias namespace that properly handles `system` (aka {@link ColumnDescriptor#hidden() hidden}) columns during renaming.
+ */
+public class IgniteAliasNamespace extends AliasNamespace {
+    IgniteAliasNamespace(
+            SqlValidatorImpl validator, SqlCall call, SqlNode enclosingNode
+    ) {
+        super(validator, call, enclosingNode);
+    }
+
+    @Override
+    protected RelDataType validateImpl(RelDataType targetRowType) {
+        List<SqlNode> operands = call.getOperandList();
+
+        if (operands.size() == 2) {
+            return super.validateImpl(targetRowType);
+        }
+
+        // Alias is 'AS t (c0, ..., cN)'
+        SqlValidatorNamespace childNs = validator.getNamespace(operands.get(0));
+
+        assert childNs != null : "Alias in FROM list must have a namespace";
+
+        SqlValidatorTable table = childNs.getTable();
+
+        if (table == null) {
+            // Not all namespaces derived from a table (for example, the one derived from sub-query).
+            // In this case let's fallback to original validation, because logic below is meant to
+            // handle `system` columns which belongs solely to the IgniteTable.
+            return super.validateImpl(targetRowType);
+        }
+
+        List<SqlNode> columnNames = Util.skip(operands, 2);
+        List<String> nameList = SqlIdentifier.simpleNames(columnNames);
+
+        int i = Util.firstDuplicate(nameList);
+        if (i >= 0) {
+            SqlIdentifier id = (SqlIdentifier) columnNames.get(i);
+            throw validator.newValidationError(id,
+                    RESOURCE.aliasListDuplicate(id.getSimple()));
+        }
+
+        TableDescriptor descriptor = table.unwrap(TableDescriptor.class);
+
+        assert descriptor != null;
+
+        RelDataType rowTypeSansHidden = descriptor.rowTypeSansHidden();
+        // At the moment, only system columns are hidden, and user must provide
+        // aliases for all non-system columns.
+        if (columnNames.size() != rowTypeSansHidden.getFieldList().size()) {
+            // Position error over all column names
+            SqlNode node = operands.size() == 3
+                    ? operands.get(2)
+                    : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
+
+            throw validator.newValidationError(node,
+                    RESOURCE.aliasListDegree(rowTypeSansHidden.getFieldCount(),
+                            getString(rowTypeSansHidden), columnNames.size()));
+        }
+
+        // Although this may look like proper way to address `system` columns, in fact it's not:
+        //     1) Calcite has different notion of `system` columns. First of all, all implementations
+        //     of namespace just propagate call to getRowType() method. Second, system columns are
+        //     registered only when converter meets LogicalJoin.
+        //
+        //     2) Row type returned from this method will be used to resolve columns. Thus, if we skip
+        //     `system` columns validator won't be able to resolve any reference of such columns (given
+        //     that they are registered only in case of joins)
+        RelDataType rowType = childNs.getRowTypeSansSystemColumns();
+
+        assert rowType != null;
+
+        int fieldsCount = rowType.getFieldCount();
+        Mapping mapping = Mappings.create(MappingType.PARTIAL_FUNCTION, fieldsCount, nameList.size());
+        int idx = 0;
+        for (ColumnDescriptor column : descriptor) {
+            if (!column.hidden()) {
+                mapping.set(column.logicalIndex(), idx++);
+            }
+        }
+
+        RelDataType aliasedType = validator.getTypeFactory().builder()
+                .addAll(
+                        Util.transform(
+                                rowType.getFieldList(),
+                                f -> {
+                                    int index = mapping.getTargetOpt(f.getIndex());
+                                    String name = index == -1 ? f.getName() : nameList.get(index);
+
+                                    return Pair.of(name, f.getType());
+                                }
+                        )
+                )
+                .kind(rowType.getStructKind())
+                .build();
+
+        return validator.getTypeFactory()
+                .createTypeWithNullability(aliasedType, rowType.isNullable());

Review Comment:
   I understand that this is a copy-paste from the calcite, but just curious what is the difference between
   ```
   return createTypeWithNullability(aliasedType, rowType.isNullable());
   ```
   and
   ```
   return aliasedTypeBuilder...nullable(rowType.isNullable()).build();
   ```



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/IgniteAliasNamespace.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.prepare;
+
+import static org.apache.calcite.util.Static.RESOURCE;
+
+import java.util.List;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.validate.AliasNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorImpl;
+import org.apache.calcite.sql.validate.SqlValidatorNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorTable;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Util;
+import org.apache.calcite.util.mapping.Mapping;
+import org.apache.calcite.util.mapping.MappingType;
+import org.apache.calcite.util.mapping.Mappings;
+import org.apache.ignite.internal.sql.engine.schema.ColumnDescriptor;
+import org.apache.ignite.internal.sql.engine.schema.TableDescriptor;
+
+/**
+ * Alias namespace that properly handles `system` (aka {@link ColumnDescriptor#hidden() hidden}) columns during renaming.
+ */
+public class IgniteAliasNamespace extends AliasNamespace {
+    IgniteAliasNamespace(
+            SqlValidatorImpl validator, SqlCall call, SqlNode enclosingNode
+    ) {
+        super(validator, call, enclosingNode);
+    }
+
+    @Override
+    protected RelDataType validateImpl(RelDataType targetRowType) {
+        List<SqlNode> operands = call.getOperandList();
+
+        if (operands.size() == 2) {
+            return super.validateImpl(targetRowType);
+        }
+
+        // Alias is 'AS t (c0, ..., cN)'
+        SqlValidatorNamespace childNs = validator.getNamespace(operands.get(0));
+
+        assert childNs != null : "Alias in FROM list must have a namespace";
+
+        SqlValidatorTable table = childNs.getTable();
+
+        if (table == null) {
+            // Not all namespaces derived from a table (for example, the one derived from sub-query).
+            // In this case let's fallback to original validation, because logic below is meant to
+            // handle `system` columns which belongs solely to the IgniteTable.
+            return super.validateImpl(targetRowType);
+        }
+
+        List<SqlNode> columnNames = Util.skip(operands, 2);
+        List<String> nameList = SqlIdentifier.simpleNames(columnNames);
+
+        int i = Util.firstDuplicate(nameList);
+        if (i >= 0) {
+            SqlIdentifier id = (SqlIdentifier) columnNames.get(i);
+            throw validator.newValidationError(id,
+                    RESOURCE.aliasListDuplicate(id.getSimple()));
+        }
+
+        TableDescriptor descriptor = table.unwrap(TableDescriptor.class);
+
+        assert descriptor != null;
+
+        RelDataType rowTypeSansHidden = descriptor.rowTypeSansHidden();
+        // At the moment, only system columns are hidden, and user must provide
+        // aliases for all non-system columns.
+        if (columnNames.size() != rowTypeSansHidden.getFieldList().size()) {
+            // Position error over all column names
+            SqlNode node = operands.size() == 3
+                    ? operands.get(2)
+                    : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
+
+            throw validator.newValidationError(node,
+                    RESOURCE.aliasListDegree(rowTypeSansHidden.getFieldCount(),
+                            getString(rowTypeSansHidden), columnNames.size()));
+        }
+
+        // Although this may look like proper way to address `system` columns, in fact it's not:
+        //     1) Calcite has different notion of `system` columns. First of all, all implementations
+        //     of namespace just propagate call to getRowType() method. Second, system columns are
+        //     registered only when converter meets LogicalJoin.
+        //
+        //     2) Row type returned from this method will be used to resolve columns. Thus, if we skip
+        //     `system` columns validator won't be able to resolve any reference of such columns (given

Review Comment:
   ```suggestion
           //     `system` columns, validator won't be able to resolve any reference of such columns (given
   ```



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/IgniteAliasNamespace.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.prepare;
+
+import static org.apache.calcite.util.Static.RESOURCE;
+
+import java.util.List;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.validate.AliasNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorImpl;
+import org.apache.calcite.sql.validate.SqlValidatorNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorTable;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Util;
+import org.apache.calcite.util.mapping.Mapping;
+import org.apache.calcite.util.mapping.MappingType;
+import org.apache.calcite.util.mapping.Mappings;
+import org.apache.ignite.internal.sql.engine.schema.ColumnDescriptor;
+import org.apache.ignite.internal.sql.engine.schema.TableDescriptor;
+
+/**
+ * Alias namespace that properly handles `system` (aka {@link ColumnDescriptor#hidden() hidden}) columns during renaming.
+ */
+public class IgniteAliasNamespace extends AliasNamespace {
+    IgniteAliasNamespace(
+            SqlValidatorImpl validator, SqlCall call, SqlNode enclosingNode
+    ) {
+        super(validator, call, enclosingNode);
+    }
+
+    @Override
+    protected RelDataType validateImpl(RelDataType targetRowType) {
+        List<SqlNode> operands = call.getOperandList();
+
+        if (operands.size() == 2) {
+            return super.validateImpl(targetRowType);
+        }
+
+        // Alias is 'AS t (c0, ..., cN)'
+        SqlValidatorNamespace childNs = validator.getNamespace(operands.get(0));
+
+        assert childNs != null : "Alias in FROM list must have a namespace";
+
+        SqlValidatorTable table = childNs.getTable();
+
+        if (table == null) {
+            // Not all namespaces derived from a table (for example, the one derived from sub-query).
+            // In this case let's fallback to original validation, because logic below is meant to
+            // handle `system` columns which belongs solely to the IgniteTable.
+            return super.validateImpl(targetRowType);
+        }
+
+        List<SqlNode> columnNames = Util.skip(operands, 2);
+        List<String> nameList = SqlIdentifier.simpleNames(columnNames);
+
+        int i = Util.firstDuplicate(nameList);
+        if (i >= 0) {
+            SqlIdentifier id = (SqlIdentifier) columnNames.get(i);
+            throw validator.newValidationError(id,
+                    RESOURCE.aliasListDuplicate(id.getSimple()));
+        }
+
+        TableDescriptor descriptor = table.unwrap(TableDescriptor.class);
+
+        assert descriptor != null;
+
+        RelDataType rowTypeSansHidden = descriptor.rowTypeSansHidden();
+        // At the moment, only system columns are hidden, and user must provide
+        // aliases for all non-system columns.
+        if (columnNames.size() != rowTypeSansHidden.getFieldList().size()) {
+            // Position error over all column names
+            SqlNode node = operands.size() == 3
+                    ? operands.get(2)
+                    : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
+
+            throw validator.newValidationError(node,
+                    RESOURCE.aliasListDegree(rowTypeSansHidden.getFieldCount(),
+                            getString(rowTypeSansHidden), columnNames.size()));
+        }
+
+        // Although this may look like proper way to address `system` columns, in fact it's not:
+        //     1) Calcite has different notion of `system` columns. First of all, all implementations
+        //     of namespace just propagate call to getRowType() method. Second, system columns are
+        //     registered only when converter meets LogicalJoin.
+        //
+        //     2) Row type returned from this method will be used to resolve columns. Thus, if we skip
+        //     `system` columns validator won't be able to resolve any reference of such columns (given
+        //     that they are registered only in case of joins)

Review Comment:
   It looks like the period in the first point needs to be removed, or added here as well.



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-22772 Sql. Fix row type derivation for renaming in FROM clause [ignite-3]

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #4219:
URL: https://github.com/apache/ignite-3/pull/4219#discussion_r1714920133


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/TableDescriptor.java:
##########
@@ -41,6 +41,9 @@ public interface TableDescriptor extends InitializerExpressionFactory, Iterable<
      */
     RelDataType rowType(IgniteTypeFactory factory, @Nullable ImmutableBitSet usedColumns);
 
+    /** Returns type of the row excluding all {@link ColumnDescriptor#hidden() hidden} columns. */
+    RelDataType rowTypeSansHidden();

Review Comment:
   this means "without" or "ignoring" ([link](https://dictionary.cambridge.org/dictionary/english/sans) to dictionary). This term is used in core calcite in a few places:
   * `SqlTypeUtil#equalSansNullability(org.apache.calcite.rel.type.RelDataType, org.apache.calcite.rel.type.RelDataType)`
   * `RelTraitSet#isDefaultSansConvention`
   * `RelDataType#equalsSansFieldNames`
   * `SqlValidatorNamespace#getRowTypeSansSystemColumns`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-22772 Sql. Fix row type derivation for renaming in FROM clause [ignite-3]

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #4219:
URL: https://github.com/apache/ignite-3/pull/4219#discussion_r1716497953


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/IgniteAliasNamespace.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.prepare;
+
+import static org.apache.calcite.util.Static.RESOURCE;
+
+import java.util.List;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.validate.AliasNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorImpl;
+import org.apache.calcite.sql.validate.SqlValidatorNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorTable;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Util;
+import org.apache.calcite.util.mapping.Mapping;
+import org.apache.calcite.util.mapping.MappingType;
+import org.apache.calcite.util.mapping.Mappings;
+import org.apache.ignite.internal.sql.engine.schema.ColumnDescriptor;
+import org.apache.ignite.internal.sql.engine.schema.TableDescriptor;
+
+/**
+ * Alias namespace that properly handles `system` (aka {@link ColumnDescriptor#hidden() hidden}) columns during renaming.
+ */
+public class IgniteAliasNamespace extends AliasNamespace {
+    IgniteAliasNamespace(
+            SqlValidatorImpl validator, SqlCall call, SqlNode enclosingNode
+    ) {
+        super(validator, call, enclosingNode);
+    }
+
+    @Override
+    protected RelDataType validateImpl(RelDataType targetRowType) {
+        List<SqlNode> operands = call.getOperandList();
+
+        if (operands.size() == 2) {
+            return super.validateImpl(targetRowType);
+        }
+
+        // Alias is 'AS t (c0, ..., cN)'
+        SqlValidatorNamespace childNs = validator.getNamespace(operands.get(0));
+
+        assert childNs != null : "Alias in FROM list must have a namespace";
+
+        SqlValidatorTable table = childNs.getTable();
+
+        if (table == null) {
+            // Not all namespaces derived from a table (for example, the one derived from sub-query).
+            // In this case let's fallback to original validation, because logic below is meant to
+            // handle `system` columns which belongs solely to the IgniteTable.
+            return super.validateImpl(targetRowType);
+        }
+
+        List<SqlNode> columnNames = Util.skip(operands, 2);
+        List<String> nameList = SqlIdentifier.simpleNames(columnNames);
+
+        int i = Util.firstDuplicate(nameList);
+        if (i >= 0) {
+            SqlIdentifier id = (SqlIdentifier) columnNames.get(i);
+            throw validator.newValidationError(id,
+                    RESOURCE.aliasListDuplicate(id.getSimple()));
+        }
+
+        TableDescriptor descriptor = table.unwrap(TableDescriptor.class);
+
+        assert descriptor != null;
+
+        RelDataType rowTypeSansHidden = descriptor.rowTypeSansHidden();
+        // At the moment, only system columns are hidden, and user must provide
+        // aliases for all non-system columns.
+        if (columnNames.size() != rowTypeSansHidden.getFieldList().size()) {
+            // Position error over all column names
+            SqlNode node = operands.size() == 3
+                    ? operands.get(2)
+                    : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
+
+            throw validator.newValidationError(node,
+                    RESOURCE.aliasListDegree(rowTypeSansHidden.getFieldCount(),
+                            getString(rowTypeSansHidden), columnNames.size()));
+        }
+
+        // Although this may look like proper way to address `system` columns, in fact it's not:
+        //     1) Calcite has different notion of `system` columns. First of all, all implementations
+        //     of namespace just propagate call to getRowType() method. Second, system columns are
+        //     registered only when converter meets LogicalJoin.
+        //
+        //     2) Row type returned from this method will be used to resolve columns. Thus, if we skip
+        //     `system` columns validator won't be able to resolve any reference of such columns (given
+        //     that they are registered only in case of joins)
+        RelDataType rowType = childNs.getRowTypeSansSystemColumns();
+
+        assert rowType != null;
+
+        int fieldsCount = rowType.getFieldCount();
+        Mapping mapping = Mappings.create(MappingType.PARTIAL_FUNCTION, fieldsCount, nameList.size());
+        int idx = 0;
+        for (ColumnDescriptor column : descriptor) {
+            if (!column.hidden()) {
+                mapping.set(column.logicalIndex(), idx++);
+            }
+        }
+
+        RelDataType aliasedType = validator.getTypeFactory().builder()
+                .addAll(
+                        Util.transform(
+                                rowType.getFieldList(),
+                                f -> {
+                                    int index = mapping.getTargetOpt(f.getIndex());
+                                    String name = index == -1 ? f.getName() : nameList.get(index);
+
+                                    return Pair.of(name, f.getType());
+                                }
+                        )
+                )
+                .kind(rowType.getStructKind())
+                .build();
+
+        return validator.getTypeFactory()
+                .createTypeWithNullability(aliasedType, rowType.isNullable());

Review Comment:
   ```
       /**
        * Changes the nullability of the last field added.
        *
        * @throws java.lang.IndexOutOfBoundsException if no fields have been
        *                                             added
        */
       public Builder nullable(boolean nullable) {
         RelDataType lastType = types.get(types.size() - 1);
         if (lastType.isNullable() != nullable) {
           final RelDataType type =
               typeFactory.createTypeWithNullability(lastType, nullable);
           types.set(types.size() - 1, type);
         }
         return this;
       }
   ```
   
   it changes nullability for the last column, but we need to make an entire row nullable
   



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-22772 Sql. Fix row type derivation for renaming in FROM clause [ignite-3]

Posted by "xtern (via GitHub)" <gi...@apache.org>.
xtern commented on code in PR #4219:
URL: https://github.com/apache/ignite-3/pull/4219#discussion_r1716503282


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/IgniteAliasNamespace.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.prepare;
+
+import static org.apache.calcite.util.Static.RESOURCE;
+
+import java.util.List;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.validate.AliasNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorImpl;
+import org.apache.calcite.sql.validate.SqlValidatorNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorTable;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Util;
+import org.apache.calcite.util.mapping.Mapping;
+import org.apache.calcite.util.mapping.MappingType;
+import org.apache.calcite.util.mapping.Mappings;
+import org.apache.ignite.internal.sql.engine.schema.ColumnDescriptor;
+import org.apache.ignite.internal.sql.engine.schema.TableDescriptor;
+
+/**
+ * Alias namespace that properly handles `system` (aka {@link ColumnDescriptor#hidden() hidden}) columns during renaming.
+ */
+public class IgniteAliasNamespace extends AliasNamespace {
+    IgniteAliasNamespace(
+            SqlValidatorImpl validator, SqlCall call, SqlNode enclosingNode
+    ) {
+        super(validator, call, enclosingNode);
+    }
+
+    @Override
+    protected RelDataType validateImpl(RelDataType targetRowType) {
+        List<SqlNode> operands = call.getOperandList();
+
+        if (operands.size() == 2) {
+            return super.validateImpl(targetRowType);
+        }
+
+        // Alias is 'AS t (c0, ..., cN)'
+        SqlValidatorNamespace childNs = validator.getNamespace(operands.get(0));
+
+        assert childNs != null : "Alias in FROM list must have a namespace";
+
+        SqlValidatorTable table = childNs.getTable();
+
+        if (table == null) {
+            // Not all namespaces derived from a table (for example, the one derived from sub-query).
+            // In this case let's fallback to original validation, because logic below is meant to
+            // handle `system` columns which belongs solely to the IgniteTable.
+            return super.validateImpl(targetRowType);
+        }
+
+        List<SqlNode> columnNames = Util.skip(operands, 2);
+        List<String> nameList = SqlIdentifier.simpleNames(columnNames);
+
+        int i = Util.firstDuplicate(nameList);
+        if (i >= 0) {
+            SqlIdentifier id = (SqlIdentifier) columnNames.get(i);
+            throw validator.newValidationError(id,
+                    RESOURCE.aliasListDuplicate(id.getSimple()));
+        }
+
+        TableDescriptor descriptor = table.unwrap(TableDescriptor.class);
+
+        assert descriptor != null;
+
+        RelDataType rowTypeSansHidden = descriptor.rowTypeSansHidden();
+        // At the moment, only system columns are hidden, and user must provide
+        // aliases for all non-system columns.
+        if (columnNames.size() != rowTypeSansHidden.getFieldList().size()) {
+            // Position error over all column names
+            SqlNode node = operands.size() == 3
+                    ? operands.get(2)
+                    : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
+
+            throw validator.newValidationError(node,
+                    RESOURCE.aliasListDegree(rowTypeSansHidden.getFieldCount(),
+                            getString(rowTypeSansHidden), columnNames.size()));
+        }
+
+        // Although this may look like proper way to address `system` columns, in fact it's not:
+        //     1) Calcite has different notion of `system` columns. First of all, all implementations
+        //     of namespace just propagate call to getRowType() method. Second, system columns are
+        //     registered only when converter meets LogicalJoin.
+        //
+        //     2) Row type returned from this method will be used to resolve columns. Thus, if we skip
+        //     `system` columns validator won't be able to resolve any reference of such columns (given
+        //     that they are registered only in case of joins)
+        RelDataType rowType = childNs.getRowTypeSansSystemColumns();
+
+        assert rowType != null;
+
+        int fieldsCount = rowType.getFieldCount();
+        Mapping mapping = Mappings.create(MappingType.PARTIAL_FUNCTION, fieldsCount, nameList.size());
+        int idx = 0;
+        for (ColumnDescriptor column : descriptor) {
+            if (!column.hidden()) {
+                mapping.set(column.logicalIndex(), idx++);
+            }
+        }
+
+        RelDataType aliasedType = validator.getTypeFactory().builder()
+                .addAll(
+                        Util.transform(
+                                rowType.getFieldList(),
+                                f -> {
+                                    int index = mapping.getTargetOpt(f.getIndex());
+                                    String name = index == -1 ? f.getName() : nameList.get(index);
+
+                                    return Pair.of(name, f.getType());
+                                }
+                        )
+                )
+                .kind(rowType.getStructKind())
+                .build();
+
+        return validator.getTypeFactory()
+                .createTypeWithNullability(aliasedType, rowType.isNullable());

Review Comment:
   :woman_facepalming:  thanks



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-22772 Sql. Fix row type derivation for renaming in FROM clause [ignite-3]

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 commented on code in PR #4219:
URL: https://github.com/apache/ignite-3/pull/4219#discussion_r1716599131


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/IgniteAliasNamespace.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.prepare;
+
+import static org.apache.calcite.util.Static.RESOURCE;
+
+import java.util.List;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.validate.AliasNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorImpl;
+import org.apache.calcite.sql.validate.SqlValidatorNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorTable;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Util;
+import org.apache.calcite.util.mapping.Mapping;
+import org.apache.calcite.util.mapping.MappingType;
+import org.apache.calcite.util.mapping.Mappings;
+import org.apache.ignite.internal.sql.engine.schema.ColumnDescriptor;
+import org.apache.ignite.internal.sql.engine.schema.TableDescriptor;
+
+/**
+ * Alias namespace that properly handles `system` (aka {@link ColumnDescriptor#hidden() hidden}) columns during renaming.
+ */
+public class IgniteAliasNamespace extends AliasNamespace {
+    IgniteAliasNamespace(
+            SqlValidatorImpl validator, SqlCall call, SqlNode enclosingNode
+    ) {
+        super(validator, call, enclosingNode);
+    }
+
+    @Override
+    protected RelDataType validateImpl(RelDataType targetRowType) {
+        List<SqlNode> operands = call.getOperandList();
+
+        if (operands.size() == 2) {
+            return super.validateImpl(targetRowType);
+        }
+
+        // Alias is 'AS t (c0, ..., cN)'
+        SqlValidatorNamespace childNs = validator.getNamespace(operands.get(0));
+
+        assert childNs != null : "Alias in FROM list must have a namespace";
+
+        SqlValidatorTable table = childNs.getTable();
+
+        if (table == null) {
+            // Not all namespaces derived from a table (for example, the one derived from sub-query).
+            // In this case let's fallback to original validation, because logic below is meant to
+            // handle `system` columns which belongs solely to the IgniteTable.
+            return super.validateImpl(targetRowType);
+        }
+
+        List<SqlNode> columnNames = Util.skip(operands, 2);
+        List<String> nameList = SqlIdentifier.simpleNames(columnNames);
+
+        int i = Util.firstDuplicate(nameList);
+        if (i >= 0) {
+            SqlIdentifier id = (SqlIdentifier) columnNames.get(i);
+            throw validator.newValidationError(id,
+                    RESOURCE.aliasListDuplicate(id.getSimple()));
+        }
+
+        TableDescriptor descriptor = table.unwrap(TableDescriptor.class);
+
+        assert descriptor != null;
+
+        RelDataType rowTypeSansHidden = descriptor.rowTypeSansHidden();
+        // At the moment, only system columns are hidden, and user must provide
+        // aliases for all non-system columns.
+        if (columnNames.size() != rowTypeSansHidden.getFieldList().size()) {
+            // Position error over all column names
+            SqlNode node = operands.size() == 3
+                    ? operands.get(2)
+                    : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
+
+            throw validator.newValidationError(node,
+                    RESOURCE.aliasListDegree(rowTypeSansHidden.getFieldCount(),
+                            getString(rowTypeSansHidden), columnNames.size()));
+        }
+
+        // Although this may look like proper way to address `system` columns, in fact it's not:
+        //     1) Calcite has different notion of `system` columns. First of all, all implementations
+        //     of namespace just propagate call to getRowType() method. Second, system columns are
+        //     registered only when converter meets LogicalJoin.
+        //
+        //     2) Row type returned from this method will be used to resolve columns. Thus, if we skip
+        //     `system` columns validator won't be able to resolve any reference of such columns (given
+        //     that they are registered only in case of joins)

Review Comment:
   fixed, thanks



##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/IgniteAliasNamespace.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.prepare;
+
+import static org.apache.calcite.util.Static.RESOURCE;
+
+import java.util.List;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.validate.AliasNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorImpl;
+import org.apache.calcite.sql.validate.SqlValidatorNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorTable;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Util;
+import org.apache.calcite.util.mapping.Mapping;
+import org.apache.calcite.util.mapping.MappingType;
+import org.apache.calcite.util.mapping.Mappings;
+import org.apache.ignite.internal.sql.engine.schema.ColumnDescriptor;
+import org.apache.ignite.internal.sql.engine.schema.TableDescriptor;
+
+/**
+ * Alias namespace that properly handles `system` (aka {@link ColumnDescriptor#hidden() hidden}) columns during renaming.
+ */
+public class IgniteAliasNamespace extends AliasNamespace {
+    IgniteAliasNamespace(
+            SqlValidatorImpl validator, SqlCall call, SqlNode enclosingNode
+    ) {
+        super(validator, call, enclosingNode);
+    }
+
+    @Override
+    protected RelDataType validateImpl(RelDataType targetRowType) {
+        List<SqlNode> operands = call.getOperandList();
+
+        if (operands.size() == 2) {
+            return super.validateImpl(targetRowType);
+        }
+
+        // Alias is 'AS t (c0, ..., cN)'
+        SqlValidatorNamespace childNs = validator.getNamespace(operands.get(0));
+
+        assert childNs != null : "Alias in FROM list must have a namespace";
+
+        SqlValidatorTable table = childNs.getTable();
+
+        if (table == null) {
+            // Not all namespaces derived from a table (for example, the one derived from sub-query).
+            // In this case let's fallback to original validation, because logic below is meant to
+            // handle `system` columns which belongs solely to the IgniteTable.
+            return super.validateImpl(targetRowType);
+        }
+
+        List<SqlNode> columnNames = Util.skip(operands, 2);
+        List<String> nameList = SqlIdentifier.simpleNames(columnNames);
+
+        int i = Util.firstDuplicate(nameList);
+        if (i >= 0) {
+            SqlIdentifier id = (SqlIdentifier) columnNames.get(i);
+            throw validator.newValidationError(id,
+                    RESOURCE.aliasListDuplicate(id.getSimple()));
+        }
+
+        TableDescriptor descriptor = table.unwrap(TableDescriptor.class);
+
+        assert descriptor != null;
+
+        RelDataType rowTypeSansHidden = descriptor.rowTypeSansHidden();
+        // At the moment, only system columns are hidden, and user must provide
+        // aliases for all non-system columns.
+        if (columnNames.size() != rowTypeSansHidden.getFieldList().size()) {
+            // Position error over all column names
+            SqlNode node = operands.size() == 3
+                    ? operands.get(2)
+                    : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
+
+            throw validator.newValidationError(node,
+                    RESOURCE.aliasListDegree(rowTypeSansHidden.getFieldCount(),
+                            getString(rowTypeSansHidden), columnNames.size()));
+        }
+
+        // Although this may look like proper way to address `system` columns, in fact it's not:
+        //     1) Calcite has different notion of `system` columns. First of all, all implementations
+        //     of namespace just propagate call to getRowType() method. Second, system columns are
+        //     registered only when converter meets LogicalJoin.
+        //
+        //     2) Row type returned from this method will be used to resolve columns. Thus, if we skip
+        //     `system` columns validator won't be able to resolve any reference of such columns (given

Review Comment:
   fixed, thanks



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-22772 Sql. Fix row type derivation for renaming in FROM clause [ignite-3]

Posted by "ygerzhedovich (via GitHub)" <gi...@apache.org>.
ygerzhedovich commented on code in PR #4219:
URL: https://github.com/apache/ignite-3/pull/4219#discussion_r1713728362


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/schema/TableDescriptor.java:
##########
@@ -41,6 +41,9 @@ public interface TableDescriptor extends InitializerExpressionFactory, Iterable<
      */
     RelDataType rowType(IgniteTypeFactory factory, @Nullable ImmutableBitSet usedColumns);
 
+    /** Returns type of the row excluding all {@link ColumnDescriptor#hidden() hidden} columns. */
+    RelDataType rowTypeSansHidden();

Review Comment:
   Why `Sans`? What does it mean?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-22772 Sql. Fix row type derivation for renaming in FROM clause [ignite-3]

Posted by "korlov42 (via GitHub)" <gi...@apache.org>.
korlov42 merged PR #4219:
URL: https://github.com/apache/ignite-3/pull/4219


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


Re: [PR] IGNITE-22772 Sql. Fix row type derivation for renaming in FROM clause [ignite-3]

Posted by "xtern (via GitHub)" <gi...@apache.org>.
xtern commented on code in PR #4219:
URL: https://github.com/apache/ignite-3/pull/4219#discussion_r1715228597


##########
modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/IgniteAliasNamespace.java:
##########
@@ -0,0 +1,157 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.sql.engine.prepare;
+
+import static org.apache.calcite.util.Static.RESOURCE;
+
+import java.util.List;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rel.type.RelDataTypeField;
+import org.apache.calcite.sql.SqlCall;
+import org.apache.calcite.sql.SqlIdentifier;
+import org.apache.calcite.sql.SqlNode;
+import org.apache.calcite.sql.SqlNodeList;
+import org.apache.calcite.sql.parser.SqlParserPos;
+import org.apache.calcite.sql.validate.AliasNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorImpl;
+import org.apache.calcite.sql.validate.SqlValidatorNamespace;
+import org.apache.calcite.sql.validate.SqlValidatorTable;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.Util;
+import org.apache.calcite.util.mapping.Mapping;
+import org.apache.calcite.util.mapping.MappingType;
+import org.apache.calcite.util.mapping.Mappings;
+import org.apache.ignite.internal.sql.engine.schema.ColumnDescriptor;
+import org.apache.ignite.internal.sql.engine.schema.TableDescriptor;
+
+/**
+ * Alias namespace that properly handles `system` (aka {@link ColumnDescriptor#hidden() hidden}) columns during renaming.
+ */
+public class IgniteAliasNamespace extends AliasNamespace {
+    IgniteAliasNamespace(
+            SqlValidatorImpl validator, SqlCall call, SqlNode enclosingNode
+    ) {
+        super(validator, call, enclosingNode);
+    }
+
+    @Override
+    protected RelDataType validateImpl(RelDataType targetRowType) {
+        List<SqlNode> operands = call.getOperandList();
+
+        if (operands.size() == 2) {
+            return super.validateImpl(targetRowType);
+        }
+
+        // Alias is 'AS t (c0, ..., cN)'
+        SqlValidatorNamespace childNs = validator.getNamespace(operands.get(0));
+
+        assert childNs != null : "Alias in FROM list must have a namespace";
+
+        SqlValidatorTable table = childNs.getTable();
+
+        if (table == null) {
+            // Not all namespaces derived from a table (for example, the one derived from sub-query).
+            // In this case let's fallback to original validation, because logic below is meant to
+            // handle `system` columns which belongs solely to the IgniteTable.
+            return super.validateImpl(targetRowType);
+        }
+
+        List<SqlNode> columnNames = Util.skip(operands, 2);
+        List<String> nameList = SqlIdentifier.simpleNames(columnNames);
+
+        int i = Util.firstDuplicate(nameList);
+        if (i >= 0) {
+            SqlIdentifier id = (SqlIdentifier) columnNames.get(i);
+            throw validator.newValidationError(id,
+                    RESOURCE.aliasListDuplicate(id.getSimple()));
+        }
+
+        TableDescriptor descriptor = table.unwrap(TableDescriptor.class);
+
+        assert descriptor != null;
+
+        RelDataType rowTypeSansHidden = descriptor.rowTypeSansHidden();
+        // At the moment, only system columns are hidden, and user must provide
+        // aliases for all non-system columns.
+        if (columnNames.size() != rowTypeSansHidden.getFieldList().size()) {
+            // Position error over all column names
+            SqlNode node = operands.size() == 3
+                    ? operands.get(2)
+                    : new SqlNodeList(columnNames, SqlParserPos.sum(columnNames));
+
+            throw validator.newValidationError(node,
+                    RESOURCE.aliasListDegree(rowTypeSansHidden.getFieldCount(),
+                            getString(rowTypeSansHidden), columnNames.size()));
+        }
+
+        // Although this may look like proper way to address `system` columns, in fact it's not:
+        //     1) Calcite has different notion of `system` columns. First of all, all implementations
+        //     of namespace just propagate call to getRowType() method. Second, system columns are
+        //     registered only when converter meets LogicalJoin.
+        //
+        //     2) Row type returned from this method will be used to resolve columns. Thus, if we skip
+        //     `system` columns validator won't be able to resolve any reference of such columns (given
+        //     that they are registered only in case of joins)

Review Comment:
   It looks like the period in the first point needs to be removed, or added here as well :sunglasses: 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: notifications-unsubscribe@ignite.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org