tledkov-gridgain commented on a change in pull request #8255:
URL: https://github.com/apache/ignite/pull/8255#discussion_r492722087



##########
File path: 
modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/GridQueryOptimizer.java
##########
@@ -0,0 +1,865 @@
+/*
+ * 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.processors.query.h2;
+
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+import java.util.function.BiPredicate;
+import java.util.function.Predicate;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteSystemProperties;
+import org.apache.ignite.internal.processors.query.h2.opt.GridH2ProxyIndex;
+import 
org.apache.ignite.internal.processors.query.h2.sql.GridSqlAggregateFunction;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlAlias;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlArray;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlAst;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlColumn;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlElement;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlFunction;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlJoin;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperation;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlQuery;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlSelect;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlSubquery;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlTable;
+import org.apache.ignite.internal.processors.query.h2.sql.GridSqlUnion;
+import org.apache.ignite.internal.util.typedef.F;
+import org.h2.index.Index;
+import org.h2.table.Column;
+import org.jetbrains.annotations.Nullable;
+
+import static 
org.apache.ignite.internal.processors.query.h2.sql.GridSqlConst.TRUE;
+import static 
org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.AND;
+import static 
org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.EQUAL;
+import static 
org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.EQUAL_NULL_SAFE;
+import static 
org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.EXISTS;
+import static 
org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.IN;
+import static 
org.apache.ignite.internal.processors.query.h2.sql.GridSqlOperationType.NEGATE;
+
+/** */
+public class GridQueryOptimizer {
+    /** Predicat returns {@code true} if current element has subquery in its 
children. */
+    private static final BiPredicate<GridSqlAst, GridSqlAst> 
ELEMENT_WITH_SUBQUERY
+        = (parent, child) -> child instanceof GridSqlSubquery;
+
+    /**
+     * Predicate returns {@code true} in case there is a child
+     * which is an IN-expression and has a subquery in its children.
+     */
+    private static final BiPredicate<GridSqlAst, GridSqlAst> 
ELEMENT_WITH_SUBQUERY_WITHIN_IN_EXPRESSION
+        = (parent, child) -> child instanceof GridSqlOperation && 
((GridSqlOperation)child).operationType() == IN
+        && child.child(1) instanceof GridSqlSubquery;
+
+    /**
+     * Predicate returns {@code true} in case there is a child
+     * which is an EXISTS-expression and has a subquery in its children.
+     */
+    private static final BiPredicate<GridSqlAst, GridSqlAst> 
ELEMENT_WITH_SUBQUERY_WITHIN_EXISTS_EXPRESSION
+        = (parent, child) -> child instanceof GridSqlOperation && 
((GridSqlOperation)child).operationType() == EXISTS
+        && child.child() instanceof GridSqlSubquery;
+
+    /** Predicat returns {@code true} if current element is an AND operation. 
*/
+    private static final Predicate<GridSqlAst> ELEMENT_IS_AND_OPERATION
+        = elem -> elem instanceof GridSqlOperation && 
((GridSqlOperation)elem).operationType() == AND;
+
+    /**
+     * Predicate returns {@code true} in case there is a child which is an 
alias for a subquery.
+     */
+    private static final BiPredicate<GridSqlAst, GridSqlAst> 
ELEMENT_WITH_ALIAS_WITH_SUBQUERY
+        = (parent, child) -> child instanceof GridSqlAlias && child.child() 
instanceof GridSqlSubquery;
+
+    /** Predicat returns {@code true} if current element is an INNER JOIN. */
+    private static final Predicate<GridSqlAst> ELEMENT_IS_INNER_JOIN
+        = elem -> elem instanceof GridSqlJoin && 
!((GridSqlJoin)elem).isLeftOuter();
+
+    /** Predicat returns {@code true} if current element is an EQUAL or 
EQUAL_NULL_SAFE operation. */
+    private static final Predicate<GridSqlAst> ELEMENT_IS_EQ
+        = elem -> elem instanceof GridSqlOperation && (EQUAL == 
((GridSqlOperation)elem).operationType()
+        || EQUAL_NULL_SAFE == ((GridSqlOperation)elem).operationType());
+
+    /**
+     * Whether to apply optimization or not.
+     */
+    private static volatile Boolean optimizationEnabled;
+
+    /**
+     * @return {@code true} if optimization should be applied.
+     */
+    @SuppressWarnings("NonThreadSafeLazyInitialization")
+    private static boolean optimizationEnabled() {
+        if (optimizationEnabled == null) { // it's OK if this will be 
initialized several times in case of races
+            optimizationEnabled = Boolean.parseBoolean(
+                
System.getProperty(IgniteSystemProperties.IGNITE_ENABLE_SUBQUERY_REWRITE_OPTIMIZATION,
 "true")
+            );
+        }
+
+        return optimizationEnabled;
+    }
+
+    /**
+     * @param parent Parent.
+     */
+    public static boolean pullOutSubQueries(GridSqlQuery parent) {
+        boolean wasPulledOut = false;
+
+        if (!optimizationEnabled())
+            return wasPulledOut;
+
+        if (parent instanceof GridSqlUnion) {
+            GridSqlUnion union = (GridSqlUnion)parent;
+
+            wasPulledOut |= pullOutSubQueries(union.left());
+            wasPulledOut |= pullOutSubQueries(union.right());
+
+            return wasPulledOut;
+        }
+
+        // pull out select expressions from SELECT clause
+        GridSqlSelect select = (GridSqlSelect)parent;
+        boolean locPulledOut;
+
+        for (int i = 0; i < select.allColumns(); i++) {
+            locPulledOut = false;
+            GridSqlAst col = select.columns(false).get(i);
+
+            if (col instanceof GridSqlSubquery)
+                locPulledOut = pullOutSubQryFromSelectExpr(select, null, i);
+            else {
+                ASTNodeFinder finder = new ASTNodeFinder(
+                    col,
+                    ELEMENT_WITH_SUBQUERY
+                );
+
+                ASTNodeFinder.Result res;
+                while ((res = finder.findNext()) != null)
+                    locPulledOut |= pullOutSubQryFromSelectExpr(select, 
res.getEl(), res.getIdx());
+            }
+
+            if (locPulledOut) // we have to analize just pulled out element as 
well
+                i--;
+
+            wasPulledOut |= locPulledOut;
+        }
+
+        // pull out sub-queries from table list
+        do {
+            locPulledOut = false;
+
+            if (ELEMENT_WITH_ALIAS_WITH_SUBQUERY.test(null, select.from()))

Review comment:
       Does it make sense to check this condition at each iteration (and the 
same condition at the loops below)?
   Please clarify. It's not clear to me.




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

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to