alex-plekhanov commented on a change in pull request #9821:
URL: https://github.com/apache/ignite/pull/9821#discussion_r809731537



##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessor.java
##########
@@ -88,8 +89,23 @@
 import org.apache.ignite.internal.processors.query.calcite.util.Service;
 import org.jetbrains.annotations.Nullable;
 
+import static org.apache.ignite.IgniteSystemProperties.getLong;
+
 /** */
 public class CalciteQueryProcessor extends GridProcessorAdapter implements 
QueryEngine {
+    /**
+     * Default planner timeout, in ms.
+     */
+    private static final long DFLT_IGNITE_CALCITE_PLANNER_TIMEOUT = 15000;
+
+    /**
+     * Determines whether to use the experimental sql, calcite based, engine.
+     */
+    @SystemProperty(value = "Timeout of experimental, calcite based, sql 
engine planner timeout, in ms",

Review comment:
       "Timeout" word - twice. Let's also remove "experimental" word.

##########
File path: 
modules/calcite/src/main/java/org/apache/ignite/internal/processors/query/calcite/CalciteQueryProcessor.java
##########
@@ -88,8 +89,23 @@
 import org.apache.ignite.internal.processors.query.calcite.util.Service;
 import org.jetbrains.annotations.Nullable;
 
+import static org.apache.ignite.IgniteSystemProperties.getLong;
+
 /** */
 public class CalciteQueryProcessor extends GridProcessorAdapter implements 
QueryEngine {
+    /**
+     * Default planner timeout, in ms.
+     */
+    private static final long DFLT_IGNITE_CALCITE_PLANNER_TIMEOUT = 15000;
+
+    /**
+     * Determines whether to use the experimental sql, calcite based, engine.

Review comment:
       Comment not related to the property

##########
File path: 
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/RunningQueriesIntegrationTest.java
##########
@@ -234,4 +254,95 @@ public void testCancelByRemoteFragment() throws 
IgniteCheckedException {
 
         GridTestUtils.assertThrowsAnyCause(log, () -> fut.get(100), 
IgniteSQLException.class, "The query was cancelled while executing.");
     }
+
+    /** */
+    @Test
+    public void testLongPlanningTimeout() throws Exception {
+        Stream.of("T1", "T2").forEach(tblName -> {
+            sql(String.format("CREATE TABLE %s(A INT, B INT)", tblName));
+
+            IgniteTable tbl = 
(IgniteTable)queryProcessor(client).schemaHolder().schema("PUBLIC").getTable(tblName);
+            IgniteIndex oldPk = tbl.getIndex("_key_PK");

Review comment:
       "_key_PK" -> `QueryUtils.PRIMARY_KEY_INDEX`

##########
File path: 
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/PlannerTimeoutTest.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.processors.query.calcite.planner;
+
+import java.util.Collections;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import com.google.common.util.concurrent.Uninterruptibles;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.volcano.VolcanoPlanner;
+import org.apache.calcite.plan.volcano.VolcanoTimeoutException;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelVisitor;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.util.Pair;
+import 
org.apache.ignite.internal.processors.query.calcite.metadata.cost.IgniteCostFactory;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.BaseQueryContext;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.IgnitePlanner;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext;
+import 
org.apache.ignite.internal.processors.query.calcite.rel.IgniteConvention;
+import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel;
+import 
org.apache.ignite.internal.processors.query.calcite.schema.CacheIndexImpl;
+import org.apache.ignite.internal.processors.query.calcite.schema.IgniteSchema;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistribution;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions;
+import org.apache.ignite.internal.processors.query.calcite.trait.TraitUtils;
+import 
org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
+import 
org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeSystem;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.junit.Test;
+
+import static org.apache.calcite.tools.Frameworks.createRootSchema;
+import static org.apache.calcite.tools.Frameworks.newConfigBuilder;
+import static 
org.apache.ignite.internal.processors.query.calcite.CalciteQueryProcessor.FRAMEWORK_CONFIG;
+
+/**
+ * Test planner timeout.
+ */
+public class PlannerTimeoutTest extends AbstractPlannerTest {
+    /** */
+    private static final long PLANNER_TIMEOUT = 1_000;
+
+    /** */
+    private static final IgniteTypeFactory typeFactory = new 
IgniteTypeFactory(IgniteTypeSystem.INSTANCE);
+
+    /** */
+    @Test
+    public void testLongPlanningTimeout() throws Exception {
+        SchemaPlus schema = createSchema("PUBLIC",
+            Pair.of("T1", createTestTable(Pair.of("A", Integer.class), 
Pair.of("B", Integer.class))),
+            Pair.of("T2", createTestTable(Pair.of("A", Integer.class), 
Pair.of("C", Integer.class)))
+        );
+
+        String sql = "SELECT * FROM T1 JOIN T2 ON T1.A = T2.A";
+
+        PlanningContext ctx = PlanningContext.builder()
+            .parentContext(BaseQueryContext.builder()
+                .frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG)
+                    .defaultSchema(schema)
+                    .costFactory(new IgniteCostFactory(1, 100, 1, 1))
+                    .build())
+                .logger(log)
+                .build()
+            )
+            .plannerTimeout(PLANNER_TIMEOUT)
+            .query(sql)
+            .build();
+
+        AtomicReference<IgniteRel> plan = new AtomicReference<>();
+        AtomicReference<RelOptPlanner.CannotPlanException> plannerError = new 
AtomicReference<>();
+
+        GridTestUtils.assertTimeout(3 * PLANNER_TIMEOUT, 
TimeUnit.MILLISECONDS, () -> {
+            try (IgnitePlanner planner = ctx.planner()) {
+                plan.set(physicalPlan(planner, ctx.query()));
+
+                VolcanoPlanner volcanoPlanner = 
GridTestUtils.getFieldValue(planner, "planner");

Review comment:
       `ctx.cluster().planner()`?

##########
File path: 
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/RunningQueriesIntegrationTest.java
##########
@@ -234,4 +254,95 @@ public void testCancelByRemoteFragment() throws 
IgniteCheckedException {
 
         GridTestUtils.assertThrowsAnyCause(log, () -> fut.get(100), 
IgniteSQLException.class, "The query was cancelled while executing.");
     }
+
+    /** */
+    @Test
+    public void testLongPlanningTimeout() throws Exception {
+        Stream.of("T1", "T2").forEach(tblName -> {
+            sql(String.format("CREATE TABLE %s(A INT, B INT)", tblName));
+
+            IgniteTable tbl = 
(IgniteTable)queryProcessor(client).schemaHolder().schema("PUBLIC").getTable(tblName);
+            IgniteIndex oldPk = tbl.getIndex("_key_PK");
+            tbl.addIndex(new SlowCollationIndex(oldPk));
+
+            sql(String.format("INSERT INTO %s(A, B) VALUES (1, 1)", tblName));
+        });
+
+        String longJoinQry = "SELECT * FROM T1 JOIN T2 ON T1.A = T2.A";
+
+        try {
+            AtomicReference<List<List<?>>> res = new AtomicReference<>();
+            GridTestUtils.assertTimeout(3 * PLANNER_TIMEOUT, 
TimeUnit.MILLISECONDS, () -> {
+                res.set(sql(longJoinQry));
+            });
+
+            assertNotNull(res.get());
+            assertFalse(res.get().isEmpty());
+        }
+        catch (Exception e) {
+            assertTrue("Unexpected exception: " + e, e instanceof 
RelOptPlanner.CannotPlanException);
+        }
+    }
+
+    /** */
+    private static class SlowCollationIndex implements IgniteIndex {

Review comment:
       Perhaps it's worth to create `DelegatingIgniteIndex` class in 
`AbstractBasicIntegrationTest` and reuse it in this test and 
`IndexScanlIntegrationTest` to remove boilerplate code. WDYT?

##########
File path: 
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/PlannerTimeoutTest.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.processors.query.calcite.planner;
+
+import java.util.Collections;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import com.google.common.util.concurrent.Uninterruptibles;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.volcano.VolcanoPlanner;
+import org.apache.calcite.plan.volcano.VolcanoTimeoutException;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelVisitor;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.util.Pair;
+import 
org.apache.ignite.internal.processors.query.calcite.metadata.cost.IgniteCostFactory;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.BaseQueryContext;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.IgnitePlanner;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext;
+import 
org.apache.ignite.internal.processors.query.calcite.rel.IgniteConvention;
+import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel;
+import 
org.apache.ignite.internal.processors.query.calcite.schema.CacheIndexImpl;
+import org.apache.ignite.internal.processors.query.calcite.schema.IgniteSchema;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistribution;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions;
+import org.apache.ignite.internal.processors.query.calcite.trait.TraitUtils;
+import 
org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
+import 
org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeSystem;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.junit.Test;
+
+import static org.apache.calcite.tools.Frameworks.createRootSchema;
+import static org.apache.calcite.tools.Frameworks.newConfigBuilder;
+import static 
org.apache.ignite.internal.processors.query.calcite.CalciteQueryProcessor.FRAMEWORK_CONFIG;
+
+/**
+ * Test planner timeout.
+ */
+public class PlannerTimeoutTest extends AbstractPlannerTest {
+    /** */
+    private static final long PLANNER_TIMEOUT = 1_000;
+
+    /** */
+    private static final IgniteTypeFactory typeFactory = new 
IgniteTypeFactory(IgniteTypeSystem.INSTANCE);
+
+    /** */
+    @Test
+    public void testLongPlanningTimeout() throws Exception {
+        SchemaPlus schema = createSchema("PUBLIC",
+            Pair.of("T1", createTestTable(Pair.of("A", Integer.class), 
Pair.of("B", Integer.class))),
+            Pair.of("T2", createTestTable(Pair.of("A", Integer.class), 
Pair.of("C", Integer.class)))
+        );
+
+        String sql = "SELECT * FROM T1 JOIN T2 ON T1.A = T2.A";
+
+        PlanningContext ctx = PlanningContext.builder()
+            .parentContext(BaseQueryContext.builder()
+                .frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG)
+                    .defaultSchema(schema)
+                    .costFactory(new IgniteCostFactory(1, 100, 1, 1))
+                    .build())
+                .logger(log)
+                .build()
+            )
+            .plannerTimeout(PLANNER_TIMEOUT)
+            .query(sql)
+            .build();
+
+        AtomicReference<IgniteRel> plan = new AtomicReference<>();
+        AtomicReference<RelOptPlanner.CannotPlanException> plannerError = new 
AtomicReference<>();
+
+        GridTestUtils.assertTimeout(3 * PLANNER_TIMEOUT, 
TimeUnit.MILLISECONDS, () -> {
+            try (IgnitePlanner planner = ctx.planner()) {
+                plan.set(physicalPlan(planner, ctx.query()));
+
+                VolcanoPlanner volcanoPlanner = 
GridTestUtils.getFieldValue(planner, "planner");
+
+                assertNotNull(volcanoPlanner);
+
+                
GridTestUtils.assertThrowsWithCause(volcanoPlanner::checkCancel, 
VolcanoTimeoutException.class);
+            }
+            catch (RelOptPlanner.CannotPlanException e) {
+                plannerError.set(e);
+            }
+            catch (Exception e) {
+                throw new RuntimeException("Planning failed", e);
+            }
+        });
+
+        assertTrue(plan.get() != null || plannerError.get() != null);
+
+        if (plan.get() != null) {
+            new RelVisitor() {
+                @Override public void visit(
+                    RelNode node,
+                    int ordinal,
+                    @Nullable RelNode parent) {

Review comment:
       `{` on the new line, for better reading. 
   Remove `@Nullable`? It depends on checkerframework, which we don't use in 
Ignite.

##########
File path: 
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/PlannerTimeoutTest.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.processors.query.calcite.planner;
+
+import java.util.Collections;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import com.google.common.util.concurrent.Uninterruptibles;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.volcano.VolcanoPlanner;
+import org.apache.calcite.plan.volcano.VolcanoTimeoutException;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelVisitor;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.util.Pair;
+import 
org.apache.ignite.internal.processors.query.calcite.metadata.cost.IgniteCostFactory;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.BaseQueryContext;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.IgnitePlanner;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext;
+import 
org.apache.ignite.internal.processors.query.calcite.rel.IgniteConvention;
+import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel;
+import 
org.apache.ignite.internal.processors.query.calcite.schema.CacheIndexImpl;
+import org.apache.ignite.internal.processors.query.calcite.schema.IgniteSchema;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistribution;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions;
+import org.apache.ignite.internal.processors.query.calcite.trait.TraitUtils;
+import 
org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
+import 
org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeSystem;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.junit.Test;
+
+import static org.apache.calcite.tools.Frameworks.createRootSchema;
+import static org.apache.calcite.tools.Frameworks.newConfigBuilder;
+import static 
org.apache.ignite.internal.processors.query.calcite.CalciteQueryProcessor.FRAMEWORK_CONFIG;
+
+/**
+ * Test planner timeout.
+ */
+public class PlannerTimeoutTest extends AbstractPlannerTest {
+    /** */
+    private static final long PLANNER_TIMEOUT = 1_000;
+
+    /** */
+    private static final IgniteTypeFactory typeFactory = new 
IgniteTypeFactory(IgniteTypeSystem.INSTANCE);
+
+    /** */
+    @Test
+    public void testLongPlanningTimeout() throws Exception {
+        SchemaPlus schema = createSchema("PUBLIC",
+            Pair.of("T1", createTestTable(Pair.of("A", Integer.class), 
Pair.of("B", Integer.class))),
+            Pair.of("T2", createTestTable(Pair.of("A", Integer.class), 
Pair.of("C", Integer.class)))
+        );
+
+        String sql = "SELECT * FROM T1 JOIN T2 ON T1.A = T2.A";
+
+        PlanningContext ctx = PlanningContext.builder()
+            .parentContext(BaseQueryContext.builder()

Review comment:
       Let's reuse `baseQueryContext()` method from `AbstractPlannerTest`

##########
File path: 
modules/core/src/main/java/org/apache/ignite/IgniteSystemProperties.java
##########
@@ -2010,7 +2010,7 @@
     /**
      * Determines whether to use the experimental sql, calcite based, engine.
      */
-    @SystemProperty(value = "Determines whether to use the experimental sql, 
calcite based, engine.",
+    @SystemProperty(value = "Determines whether to use the experimental sql, 
calcite based, engine",

Review comment:
       Please revert this (this property will be removed by another patch, to 
avoid merge conflicts)

##########
File path: 
modules/calcite/src/test/java/org/apache/ignite/testsuites/PlannerTestSuite.java
##########
@@ -69,6 +70,7 @@
     JoinWithUsingPlannerTest.class,
     ProjectFilterScanMergePlannerTest.class,
     IndexRebuildPlannerTest.class,
+    PlannerTimeoutTest.class

Review comment:
       Please add `,` to the end of line (it helps to merge branches without 
conflicts)

##########
File path: 
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/RunningQueriesIntegrationTest.java
##########
@@ -234,4 +254,95 @@ public void testCancelByRemoteFragment() throws 
IgniteCheckedException {
 
         GridTestUtils.assertThrowsAnyCause(log, () -> fut.get(100), 
IgniteSQLException.class, "The query was cancelled while executing.");
     }
+
+    /** */
+    @Test
+    public void testLongPlanningTimeout() throws Exception {
+        Stream.of("T1", "T2").forEach(tblName -> {
+            sql(String.format("CREATE TABLE %s(A INT, B INT)", tblName));
+
+            IgniteTable tbl = 
(IgniteTable)queryProcessor(client).schemaHolder().schema("PUBLIC").getTable(tblName);
+            IgniteIndex oldPk = tbl.getIndex("_key_PK");
+            tbl.addIndex(new SlowCollationIndex(oldPk));
+
+            sql(String.format("INSERT INTO %s(A, B) VALUES (1, 1)", tblName));
+        });
+
+        String longJoinQry = "SELECT * FROM T1 JOIN T2 ON T1.A = T2.A";
+
+        try {
+            AtomicReference<List<List<?>>> res = new AtomicReference<>();
+            GridTestUtils.assertTimeout(3 * PLANNER_TIMEOUT, 
TimeUnit.MILLISECONDS, () -> {
+                res.set(sql(longJoinQry));
+            });
+
+            assertNotNull(res.get());
+            assertFalse(res.get().isEmpty());
+        }
+        catch (Exception e) {
+            assertTrue("Unexpected exception: " + e, e instanceof 
RelOptPlanner.CannotPlanException);
+        }
+    }
+
+    /** */
+    private static class SlowCollationIndex implements IgniteIndex {
+        /** */
+        private final IgniteIndex delegate;
+
+        /** */
+        public SlowCollationIndex(IgniteIndex delegate) {
+            this.delegate = delegate;
+        }
+
+        /** {@inheritDoc} */
+        @Override public RelCollation collation() {
+            Uninterruptibles.sleepUninterruptibly(200, TimeUnit.MILLISECONDS);

Review comment:
       `doSleep(200L)`

##########
File path: 
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/PlannerTimeoutTest.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.processors.query.calcite.planner;
+
+import java.util.Collections;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import com.google.common.util.concurrent.Uninterruptibles;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.volcano.VolcanoPlanner;
+import org.apache.calcite.plan.volcano.VolcanoTimeoutException;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelVisitor;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.util.Pair;
+import 
org.apache.ignite.internal.processors.query.calcite.metadata.cost.IgniteCostFactory;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.BaseQueryContext;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.IgnitePlanner;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext;
+import 
org.apache.ignite.internal.processors.query.calcite.rel.IgniteConvention;
+import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel;
+import 
org.apache.ignite.internal.processors.query.calcite.schema.CacheIndexImpl;
+import org.apache.ignite.internal.processors.query.calcite.schema.IgniteSchema;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistribution;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions;
+import org.apache.ignite.internal.processors.query.calcite.trait.TraitUtils;
+import 
org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
+import 
org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeSystem;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.junit.Test;
+
+import static org.apache.calcite.tools.Frameworks.createRootSchema;
+import static org.apache.calcite.tools.Frameworks.newConfigBuilder;
+import static 
org.apache.ignite.internal.processors.query.calcite.CalciteQueryProcessor.FRAMEWORK_CONFIG;
+
+/**
+ * Test planner timeout.
+ */
+public class PlannerTimeoutTest extends AbstractPlannerTest {
+    /** */
+    private static final long PLANNER_TIMEOUT = 1_000;
+
+    /** */
+    private static final IgniteTypeFactory typeFactory = new 
IgniteTypeFactory(IgniteTypeSystem.INSTANCE);
+
+    /** */
+    @Test
+    public void testLongPlanningTimeout() throws Exception {
+        SchemaPlus schema = createSchema("PUBLIC",
+            Pair.of("T1", createTestTable(Pair.of("A", Integer.class), 
Pair.of("B", Integer.class))),
+            Pair.of("T2", createTestTable(Pair.of("A", Integer.class), 
Pair.of("C", Integer.class)))
+        );
+
+        String sql = "SELECT * FROM T1 JOIN T2 ON T1.A = T2.A";
+
+        PlanningContext ctx = PlanningContext.builder()
+            .parentContext(BaseQueryContext.builder()
+                .frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG)
+                    .defaultSchema(schema)
+                    .costFactory(new IgniteCostFactory(1, 100, 1, 1))
+                    .build())
+                .logger(log)
+                .build()
+            )
+            .plannerTimeout(PLANNER_TIMEOUT)
+            .query(sql)
+            .build();
+
+        AtomicReference<IgniteRel> plan = new AtomicReference<>();
+        AtomicReference<RelOptPlanner.CannotPlanException> plannerError = new 
AtomicReference<>();
+
+        GridTestUtils.assertTimeout(3 * PLANNER_TIMEOUT, 
TimeUnit.MILLISECONDS, () -> {
+            try (IgnitePlanner planner = ctx.planner()) {
+                plan.set(physicalPlan(planner, ctx.query()));
+
+                VolcanoPlanner volcanoPlanner = 
GridTestUtils.getFieldValue(planner, "planner");
+
+                assertNotNull(volcanoPlanner);
+
+                
GridTestUtils.assertThrowsWithCause(volcanoPlanner::checkCancel, 
VolcanoTimeoutException.class);
+            }
+            catch (RelOptPlanner.CannotPlanException e) {
+                plannerError.set(e);
+            }
+            catch (Exception e) {
+                throw new RuntimeException("Planning failed", e);
+            }
+        });
+
+        assertTrue(plan.get() != null || plannerError.get() != null);
+
+        if (plan.get() != null) {
+            new RelVisitor() {
+                @Override public void visit(
+                    RelNode node,
+                    int ordinal,
+                    @Nullable RelNode parent) {
+                    
assertNotNull(node.getTraitSet().getTrait(IgniteConvention.INSTANCE.getTraitDef()));
+                    super.visit(node, ordinal, parent);
+                }
+            }.go(plan.get());
+        }
+    }
+
+    /** */
+    private static SchemaPlus createSchema(String name, Pair<String, 
TestTable>... tables) {
+        IgniteSchema schema = new IgniteSchema(name);
+
+        for (Pair<String, TestTable> tbl: tables)
+            schema.addTable(tbl.left, tbl.right);
+
+        return createRootSchema(false).add(name, schema);
+    }
+
+    /** */
+    private static TestTable createTestTable(Pair<String, Class<?>>... cols) {

Review comment:
       There are already `createSchema` and `createTable` (with distribution 
and columns) methods exists in `AbstractPlannerTest`, you should reuse these 
methods.

##########
File path: 
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/planner/PlannerTimeoutTest.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.processors.query.calcite.planner;
+
+import java.util.Collections;
+import java.util.concurrent.TimeUnit;
+import java.util.concurrent.atomic.AtomicReference;
+import com.google.common.util.concurrent.Uninterruptibles;
+import org.apache.calcite.plan.RelOptPlanner;
+import org.apache.calcite.plan.volcano.VolcanoPlanner;
+import org.apache.calcite.plan.volcano.VolcanoTimeoutException;
+import org.apache.calcite.rel.RelCollation;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelVisitor;
+import org.apache.calcite.rel.type.RelDataTypeFactory;
+import org.apache.calcite.schema.SchemaPlus;
+import org.apache.calcite.util.Pair;
+import 
org.apache.ignite.internal.processors.query.calcite.metadata.cost.IgniteCostFactory;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.BaseQueryContext;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.IgnitePlanner;
+import 
org.apache.ignite.internal.processors.query.calcite.prepare.PlanningContext;
+import 
org.apache.ignite.internal.processors.query.calcite.rel.IgniteConvention;
+import org.apache.ignite.internal.processors.query.calcite.rel.IgniteRel;
+import 
org.apache.ignite.internal.processors.query.calcite.schema.CacheIndexImpl;
+import org.apache.ignite.internal.processors.query.calcite.schema.IgniteSchema;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistribution;
+import 
org.apache.ignite.internal.processors.query.calcite.trait.IgniteDistributions;
+import org.apache.ignite.internal.processors.query.calcite.trait.TraitUtils;
+import 
org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeFactory;
+import 
org.apache.ignite.internal.processors.query.calcite.type.IgniteTypeSystem;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.checkerframework.checker.nullness.qual.Nullable;
+import org.junit.Test;
+
+import static org.apache.calcite.tools.Frameworks.createRootSchema;
+import static org.apache.calcite.tools.Frameworks.newConfigBuilder;
+import static 
org.apache.ignite.internal.processors.query.calcite.CalciteQueryProcessor.FRAMEWORK_CONFIG;
+
+/**
+ * Test planner timeout.
+ */
+public class PlannerTimeoutTest extends AbstractPlannerTest {
+    /** */
+    private static final long PLANNER_TIMEOUT = 1_000;
+
+    /** */
+    private static final IgniteTypeFactory typeFactory = new 
IgniteTypeFactory(IgniteTypeSystem.INSTANCE);
+
+    /** */
+    @Test
+    public void testLongPlanningTimeout() throws Exception {
+        SchemaPlus schema = createSchema("PUBLIC",
+            Pair.of("T1", createTestTable(Pair.of("A", Integer.class), 
Pair.of("B", Integer.class))),
+            Pair.of("T2", createTestTable(Pair.of("A", Integer.class), 
Pair.of("C", Integer.class)))
+        );
+
+        String sql = "SELECT * FROM T1 JOIN T2 ON T1.A = T2.A";
+
+        PlanningContext ctx = PlanningContext.builder()
+            .parentContext(BaseQueryContext.builder()
+                .frameworkConfig(newConfigBuilder(FRAMEWORK_CONFIG)
+                    .defaultSchema(schema)
+                    .costFactory(new IgniteCostFactory(1, 100, 1, 1))
+                    .build())
+                .logger(log)
+                .build()
+            )
+            .plannerTimeout(PLANNER_TIMEOUT)
+            .query(sql)
+            .build();
+
+        AtomicReference<IgniteRel> plan = new AtomicReference<>();
+        AtomicReference<RelOptPlanner.CannotPlanException> plannerError = new 
AtomicReference<>();
+
+        GridTestUtils.assertTimeout(3 * PLANNER_TIMEOUT, 
TimeUnit.MILLISECONDS, () -> {
+            try (IgnitePlanner planner = ctx.planner()) {
+                plan.set(physicalPlan(planner, ctx.query()));
+
+                VolcanoPlanner volcanoPlanner = 
GridTestUtils.getFieldValue(planner, "planner");
+
+                assertNotNull(volcanoPlanner);
+
+                
GridTestUtils.assertThrowsWithCause(volcanoPlanner::checkCancel, 
VolcanoTimeoutException.class);
+            }
+            catch (RelOptPlanner.CannotPlanException e) {
+                plannerError.set(e);
+            }
+            catch (Exception e) {
+                throw new RuntimeException("Planning failed", e);
+            }
+        });
+
+        assertTrue(plan.get() != null || plannerError.get() != null);
+
+        if (plan.get() != null) {
+            new RelVisitor() {
+                @Override public void visit(
+                    RelNode node,
+                    int ordinal,
+                    @Nullable RelNode parent) {
+                    
assertNotNull(node.getTraitSet().getTrait(IgniteConvention.INSTANCE.getTraitDef()));
+                    super.visit(node, ordinal, parent);
+                }
+            }.go(plan.get());
+        }
+    }
+
+    /** */
+    private static SchemaPlus createSchema(String name, Pair<String, 
TestTable>... tables) {
+        IgniteSchema schema = new IgniteSchema(name);
+
+        for (Pair<String, TestTable> tbl: tables)
+            schema.addTable(tbl.left, tbl.right);
+
+        return createRootSchema(false).add(name, schema);
+    }
+
+    /** */
+    private static TestTable createTestTable(Pair<String, Class<?>>... cols) {
+        RelDataTypeFactory.Builder relDataTypeFactoryBuilder = new 
RelDataTypeFactory.Builder(typeFactory);
+
+        for (Pair<String, Class<?>> col: cols) {
+            relDataTypeFactoryBuilder.add(col.left, 
typeFactory.createType(col.right));
+        }
+
+        TestTable table = new TestTable(relDataTypeFactoryBuilder.build()) {
+            @Override public IgniteDistribution distribution() {
+                return IgniteDistributions.broadcast();
+            }
+        };
+
+        RelCollation pkColl = 
TraitUtils.createCollation(Collections.singletonList(0));
+        table.addIndex(new CacheIndexImpl(pkColl, "pk", null, table) {
+            @Override public RelCollation collation() {
+                Uninterruptibles.sleepUninterruptibly(200, 
TimeUnit.MILLISECONDS);

Review comment:
       `doSleep(200L)`




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