xtern commented on code in PR #7303: URL: https://github.com/apache/ignite-3/pull/7303#discussion_r2646992785
########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/expressions/SqlExpressionFactoryAdapter.java: ########## @@ -0,0 +1,318 @@ +/* + * 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.expressions; + +import static java.util.Objects.requireNonNull; +import static org.apache.ignite.internal.sql.engine.sql.IgniteSqlParser.PARSER_CONFIG; + +import java.util.Set; +import java.util.function.LongSupplier; +import org.apache.calcite.adapter.java.JavaTypeFactory; +import org.apache.calcite.linq4j.QueryProvider; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.runtime.CalciteContextException; +import org.apache.calcite.schema.SchemaPlus; +import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.parser.SqlParseException; +import org.apache.calcite.sql.parser.SqlParser; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.calcite.sql.type.SqlTypeUtil; +import org.apache.calcite.sql.util.SqlShuttle; +import org.apache.calcite.sql.validate.SqlValidator; +import org.apache.calcite.util.SourceStringReader; +import org.apache.calcite.util.Static; +import org.apache.calcite.util.Util.FoundOne; +import org.apache.ignite.internal.sql.engine.api.expressions.ContextBuilder; +import org.apache.ignite.internal.sql.engine.api.expressions.EvaluationContext; +import org.apache.ignite.internal.sql.engine.api.expressions.ExpressionEvaluationException; +import org.apache.ignite.internal.sql.engine.api.expressions.ExpressionFactory; +import org.apache.ignite.internal.sql.engine.api.expressions.ExpressionParsingException; +import org.apache.ignite.internal.sql.engine.api.expressions.ExpressionValidationException; +import org.apache.ignite.internal.sql.engine.api.expressions.IgnitePredicate; +import org.apache.ignite.internal.sql.engine.api.expressions.RowAccessor; +import org.apache.ignite.internal.sql.engine.api.expressions.RowFactoryFactory; +import org.apache.ignite.internal.sql.engine.exec.SqlEvaluationContext; +import org.apache.ignite.internal.sql.engine.exec.exp.SqlExpressionFactory; +import org.apache.ignite.internal.sql.engine.exec.exp.SqlPredicate; +import org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner; +import org.apache.ignite.internal.sql.engine.prepare.PlanningContext; +import org.apache.ignite.internal.sql.engine.sql.IgniteSqlParser; +import org.apache.ignite.internal.sql.engine.util.Commons; +import org.apache.ignite.internal.sql.engine.util.TypeUtils; +import org.apache.ignite.internal.type.StructNativeType; +import org.apache.ignite.internal.util.ExceptionUtils; +import org.jetbrains.annotations.Nullable; + +/** + * Implementation of {@link ExpressionFactory} interface which parses and validates the given expression but delegates actual implementation + * to the instance of {@link SqlExpressionFactory}. + */ +public class SqlExpressionFactoryAdapter implements ExpressionFactory { + private static final String SINGLE_INPUT_ROW_NAMESPACE_NAME = "INPUT"; + + private final SqlExpressionFactory factory; + + /** Constructs tye adapter. */ Review Comment: I believe this is a typo. ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/IgniteSqlToRelConvertor.java: ########## @@ -139,6 +140,31 @@ public SqlNode validateExpression(RelDataType rowType, SqlNode expr) { throw new UnsupportedOperationException("Not implemented yet"); } + /** + * Converts given AST representing expression to a {@link RexNode relational expression tree}. + * + * @param node The AST root to convert. + * @param scope The scope to use to resolve column reference, if any. + * @param inputRowType The input row type. Should be provided if expression contains any column references. + * @return A converted expression tree. + */ + public RexNode convertExpressionExt(SqlNode node, SqlValidatorScope scope, @Nullable RelDataType inputRowType) { + // Review Comment: you should probably write something or drop it ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/exec/ExchangeServiceImpl.java: ########## @@ -177,13 +188,21 @@ private void onMessage(InternalClusterNode node, QueryBatchMessage msg) { try { inbox.onBatchReceived(node.name(), msg.batchId(), msg.last(), msg.rows()); } catch (Throwable e) { - inbox.onError(e); + Throwable toUse; + //noinspection InstanceofCatchParameter + if (e instanceof ExpressionEvaluationException) { Review Comment: Let's extract duplicated code (error handling) to a separate method ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/expressions/SqlExpressionFactoryAdapter.java: ########## @@ -0,0 +1,318 @@ +/* + * 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.expressions; + +import static java.util.Objects.requireNonNull; +import static org.apache.ignite.internal.sql.engine.sql.IgniteSqlParser.PARSER_CONFIG; + +import java.util.Set; +import java.util.function.LongSupplier; +import org.apache.calcite.adapter.java.JavaTypeFactory; +import org.apache.calcite.linq4j.QueryProvider; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.runtime.CalciteContextException; +import org.apache.calcite.schema.SchemaPlus; +import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.parser.SqlParseException; +import org.apache.calcite.sql.parser.SqlParser; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.calcite.sql.type.SqlTypeUtil; +import org.apache.calcite.sql.util.SqlShuttle; +import org.apache.calcite.sql.validate.SqlValidator; +import org.apache.calcite.util.SourceStringReader; +import org.apache.calcite.util.Static; +import org.apache.calcite.util.Util.FoundOne; +import org.apache.ignite.internal.sql.engine.api.expressions.ContextBuilder; +import org.apache.ignite.internal.sql.engine.api.expressions.EvaluationContext; +import org.apache.ignite.internal.sql.engine.api.expressions.ExpressionEvaluationException; +import org.apache.ignite.internal.sql.engine.api.expressions.ExpressionFactory; +import org.apache.ignite.internal.sql.engine.api.expressions.ExpressionParsingException; +import org.apache.ignite.internal.sql.engine.api.expressions.ExpressionValidationException; +import org.apache.ignite.internal.sql.engine.api.expressions.IgnitePredicate; +import org.apache.ignite.internal.sql.engine.api.expressions.RowAccessor; +import org.apache.ignite.internal.sql.engine.api.expressions.RowFactoryFactory; +import org.apache.ignite.internal.sql.engine.exec.SqlEvaluationContext; +import org.apache.ignite.internal.sql.engine.exec.exp.SqlExpressionFactory; +import org.apache.ignite.internal.sql.engine.exec.exp.SqlPredicate; +import org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner; +import org.apache.ignite.internal.sql.engine.prepare.PlanningContext; +import org.apache.ignite.internal.sql.engine.sql.IgniteSqlParser; +import org.apache.ignite.internal.sql.engine.util.Commons; +import org.apache.ignite.internal.sql.engine.util.TypeUtils; +import org.apache.ignite.internal.type.StructNativeType; +import org.apache.ignite.internal.util.ExceptionUtils; +import org.jetbrains.annotations.Nullable; + +/** + * Implementation of {@link ExpressionFactory} interface which parses and validates the given expression but delegates actual implementation + * to the instance of {@link SqlExpressionFactory}. + */ +public class SqlExpressionFactoryAdapter implements ExpressionFactory { + private static final String SINGLE_INPUT_ROW_NAMESPACE_NAME = "INPUT"; + + private final SqlExpressionFactory factory; + + /** Constructs tye adapter. */ + public SqlExpressionFactoryAdapter( + SqlExpressionFactory factory + ) { + this.factory = factory; + } + + @Override + public <RowT> ContextBuilder<RowT> contextBuilder() { + return new ContextBuilderImpl<>(); + } + + @Override + public IgnitePredicate predicate( + String expression, + StructNativeType inputRowType + ) throws ExpressionParsingException, ExpressionValidationException { + SqlNode expressionAst = parse(expression); + + try { + // Reject subqueries earlier so we can implement lightweight validation + // without necessity to register all the namespaces. + expressionAst.accept(RejectSubQueriesValidator.INSTANCE); + + // Usage of system context-dependent functions can be validated here as well. + expressionAst.accept(RejectContextDependentFunctionValidator.INSTANCE); + } catch (FoundOne one) { + String message = (String) one.getNode(); + + assert message != null; + + throw new ExpressionValidationException(message); + } + + try (IgnitePlanner planner = createPlanner()) { + SqlValidator validator = planner.validator(); + + RowBasedScope scope = new RowBasedScope(validator.getEmptyScope()); + + RelDataType relDataType = TypeUtils.native2relationalType(planner.getTypeFactory(), inputRowType); + scope.addChild(new RowNamespace(validator, relDataType), SINGLE_INPUT_ROW_NAMESPACE_NAME, false); + + try { + expressionAst.validateExpr(validator, scope); + + try { Review Comment: This code with 3 try-statements and duplicate try/catch-statements doesn't look very nice. We might be able to separate some parts into separate methods 🤔 (for example, validation could be separated into a separate method and duplicated code in the catch block). ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/expressions/SqlExpressionFactoryAdapter.java: ########## @@ -0,0 +1,318 @@ +/* + * 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.expressions; + +import static java.util.Objects.requireNonNull; +import static org.apache.ignite.internal.sql.engine.sql.IgniteSqlParser.PARSER_CONFIG; + +import java.util.Set; +import java.util.function.LongSupplier; +import org.apache.calcite.adapter.java.JavaTypeFactory; +import org.apache.calcite.linq4j.QueryProvider; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.runtime.CalciteContextException; +import org.apache.calcite.schema.SchemaPlus; +import org.apache.calcite.sql.SqlCall; +import org.apache.calcite.sql.SqlIdentifier; +import org.apache.calcite.sql.SqlKind; +import org.apache.calcite.sql.SqlNode; +import org.apache.calcite.sql.parser.SqlParseException; +import org.apache.calcite.sql.parser.SqlParser; +import org.apache.calcite.sql.parser.SqlParserPos; +import org.apache.calcite.sql.type.SqlTypeUtil; +import org.apache.calcite.sql.util.SqlShuttle; +import org.apache.calcite.sql.validate.SqlValidator; +import org.apache.calcite.util.SourceStringReader; +import org.apache.calcite.util.Static; +import org.apache.calcite.util.Util.FoundOne; +import org.apache.ignite.internal.sql.engine.api.expressions.ContextBuilder; +import org.apache.ignite.internal.sql.engine.api.expressions.EvaluationContext; +import org.apache.ignite.internal.sql.engine.api.expressions.ExpressionEvaluationException; +import org.apache.ignite.internal.sql.engine.api.expressions.ExpressionFactory; +import org.apache.ignite.internal.sql.engine.api.expressions.ExpressionParsingException; +import org.apache.ignite.internal.sql.engine.api.expressions.ExpressionValidationException; +import org.apache.ignite.internal.sql.engine.api.expressions.IgnitePredicate; +import org.apache.ignite.internal.sql.engine.api.expressions.RowAccessor; +import org.apache.ignite.internal.sql.engine.api.expressions.RowFactoryFactory; +import org.apache.ignite.internal.sql.engine.exec.SqlEvaluationContext; +import org.apache.ignite.internal.sql.engine.exec.exp.SqlExpressionFactory; +import org.apache.ignite.internal.sql.engine.exec.exp.SqlPredicate; +import org.apache.ignite.internal.sql.engine.prepare.IgnitePlanner; +import org.apache.ignite.internal.sql.engine.prepare.PlanningContext; +import org.apache.ignite.internal.sql.engine.sql.IgniteSqlParser; +import org.apache.ignite.internal.sql.engine.util.Commons; +import org.apache.ignite.internal.sql.engine.util.TypeUtils; +import org.apache.ignite.internal.type.StructNativeType; +import org.apache.ignite.internal.util.ExceptionUtils; +import org.jetbrains.annotations.Nullable; + +/** + * Implementation of {@link ExpressionFactory} interface which parses and validates the given expression but delegates actual implementation + * to the instance of {@link SqlExpressionFactory}. + */ +public class SqlExpressionFactoryAdapter implements ExpressionFactory { + private static final String SINGLE_INPUT_ROW_NAMESPACE_NAME = "INPUT"; + + private final SqlExpressionFactory factory; + + /** Constructs tye adapter. */ + public SqlExpressionFactoryAdapter( + SqlExpressionFactory factory + ) { + this.factory = factory; + } + + @Override + public <RowT> ContextBuilder<RowT> contextBuilder() { + return new ContextBuilderImpl<>(); + } + + @Override + public IgnitePredicate predicate( + String expression, + StructNativeType inputRowType + ) throws ExpressionParsingException, ExpressionValidationException { + SqlNode expressionAst = parse(expression); + + try { + // Reject subqueries earlier so we can implement lightweight validation + // without necessity to register all the namespaces. + expressionAst.accept(RejectSubQueriesValidator.INSTANCE); + + // Usage of system context-dependent functions can be validated here as well. + expressionAst.accept(RejectContextDependentFunctionValidator.INSTANCE); + } catch (FoundOne one) { + String message = (String) one.getNode(); + + assert message != null; + + throw new ExpressionValidationException(message); + } + + try (IgnitePlanner planner = createPlanner()) { + SqlValidator validator = planner.validator(); + + RowBasedScope scope = new RowBasedScope(validator.getEmptyScope()); + + RelDataType relDataType = TypeUtils.native2relationalType(planner.getTypeFactory(), inputRowType); + scope.addChild(new RowNamespace(validator, relDataType), SINGLE_INPUT_ROW_NAMESPACE_NAME, false); + + try { + expressionAst.validateExpr(validator, scope); + + try { + // Aggregate functions are not resolved until validation, hence we cannot reject + // such expressions until syntax tree is validated. + expressionAst.accept(RejectAggregatesValidator.INSTANCE); + } catch (FoundOne one) { + String message = (String) one.getNode(); + + assert message != null; + + throw new ExpressionValidationException(message); + } + + RelDataType resultType = validator.deriveType(scope, expressionAst); + + if (!SqlTypeUtil.isBoolean(resultType)) { + throw new ExpressionValidationException("Expected BOOLEAN expression but " + resultType + " was provided."); + } + } catch (CalciteContextException ex) { + String message = ex.getMessage(); + if (message == null) { + message = "Unable to validate expression."; + } + + throw new ExpressionValidationException(message); + } + + RexNode rexNode = planner.sqlToRelConverter().convertExpressionExt(expressionAst, scope, relDataType); + + SqlPredicate predicate = factory.predicate( + rexNode, + relDataType + ); + + return new IgnitePredicate() { Review Comment: It might be better to use a static wrapper-class instead of an anonymous that might hold the reference to the factory adapter instance. -- 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]
