mihaibudiu commented on code in PR #4662: URL: https://github.com/apache/calcite/pull/4662#discussion_r2590747075
########## core/src/main/java/org/apache/calcite/sql/SqlStarExclude.java: ########## @@ -0,0 +1,85 @@ +/* + * 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.calcite.sql; + +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.calcite.sql.validate.SqlValidator; +import org.apache.calcite.sql.validate.SqlValidatorScope; +import org.apache.calcite.util.ImmutableNullableList; + +import java.util.List; + +import static java.util.Objects.requireNonNull; + +/** + * Represents {@code SELECT * EXCLUDE(...)} when parsed by the Babel parser. + */ +public class SqlStarExclude extends SqlCall { + public static final SqlSpecialOperator OPERATOR = + new SqlSpecialOperator("SELECT_STAR_EXCLUDE", SqlKind.OTHER); + + private final SqlIdentifier starIdentifier; + private final SqlNodeList excludeList; + + public SqlStarExclude(SqlIdentifier starIdentifier, SqlNodeList excludeList) { + super(requireNonNull(starIdentifier, "starIdentifier").getParserPosition()); + this.starIdentifier = starIdentifier; + this.excludeList = requireNonNull(excludeList, "excludeList"); + } + + public SqlIdentifier getStarIdentifier() { + return starIdentifier; + } + + public SqlNodeList getExcludeList() { + return excludeList; + } + + @Override public SqlOperator getOperator() { + return OPERATOR; + } + + @Override public SqlKind getKind() { + return OPERATOR.getKind(); + } + + @Override public List<SqlNode> getOperandList() { + return ImmutableNullableList.of(starIdentifier, excludeList); + } + + @Override public SqlNode clone(SqlParserPos pos) { + return new SqlStarExclude((SqlIdentifier) starIdentifier.clone(pos), + excludeList.clone(pos)); + } + + @Override public void unparse(SqlWriter writer, int leftPrec, int rightPrec) { + starIdentifier.unparse(writer, leftPrec, rightPrec); + writer.sep("EXCLUDE"); + writer.print("("); + for (int i = 0; i < excludeList.size(); i++) { + if (i > 0) { + writer.sep(","); + } + excludeList.get(i).unparse(writer, 0, 0); + } + writer.print(")"); + } + + @Override public void validate(SqlValidator validator, SqlValidatorScope scope) { + // Babel already validates the exclude list Review Comment: This seems odd, what if someone wants to use this outside of Babel? ########## core/src/main/java/org/apache/calcite/sql/SqlStarExclude.java: ########## @@ -0,0 +1,85 @@ +/* + * 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.calcite.sql; + +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.calcite.sql.validate.SqlValidator; +import org.apache.calcite.sql.validate.SqlValidatorScope; +import org.apache.calcite.util.ImmutableNullableList; + +import java.util.List; + +import static java.util.Objects.requireNonNull; + +/** + * Represents {@code SELECT * EXCLUDE(...)} when parsed by the Babel parser. Review Comment: The fact that the Babel parser (only) uses this class should probably not be part of the Javadoc of this class. ########## core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java: ########## @@ -750,18 +769,26 @@ private boolean expandStar(List<SqlNode> selectItems, Set<String> aliases, String columnName = field.getName(); // TODO: do real implicit collation here + final SqlIdentifier columnId = Review Comment: does the comment still apply? if you don't know, maybe you should not separate it from the statement it may apply to ########## site/_docs/reference.md: ########## @@ -3080,6 +3080,10 @@ Note: * A scalar value has length 1; * The length of array or object is the number of elements is contains. +### Babel parser extensions Review Comment: could you instead add the new rule to the grammar and make this a note after the grammar rule is described? ########## core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java: ########## @@ -778,6 +805,87 @@ private static int calculatePermuteOffset(List<SqlNode> selectItems) { return 0; } + private static boolean matchesExcludeNames(List<String> columnNames, + List<String> excludeNames, SqlNameMatcher nameMatcher) { + if (excludeNames.size() > columnNames.size()) { Review Comment: is this right even if there are duplicates? or have duplicates been checked elsewhere? ########## core/src/main/java/org/apache/calcite/sql/validate/SqlValidatorImpl.java: ########## @@ -778,6 +805,87 @@ private static int calculatePermuteOffset(List<SqlNode> selectItems) { return 0; } + private static boolean matchesExcludeNames(List<String> columnNames, + List<String> excludeNames, SqlNameMatcher nameMatcher) { + if (excludeNames.size() > columnNames.size()) { + return false; + } + final int offset = columnNames.size() - excludeNames.size(); + for (int i = 0; i < excludeNames.size(); i++) { + if (!nameMatcher.matches(columnNames.get(offset + i), excludeNames.get(i))) { + return false; + } + } + return true; + } + + private static boolean shouldExcludeField(@Nullable SqlNodeList excludeList, + SqlIdentifier columnId, SqlNameMatcher nameMatcher) { + if (excludeList == null) { + return false; + } + for (SqlNode node : excludeList) { + if (!(node instanceof SqlIdentifier)) { Review Comment: I suspect this can never happen, so this can perhaps be an assertion? -- 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: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
