korlov42 commented on code in PR #7303:
URL: https://github.com/apache/ignite-3/pull/7303#discussion_r2650899006


##########
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:
   introduced static private class instead



-- 
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]

Reply via email to