alex-plekhanov commented on code in PR #11425:
URL: https://github.com/apache/ignite/pull/11425#discussion_r1775406672


##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SqlPlanHistoryIntegrationTest.java:
##########
@@ -0,0 +1,748 @@
+/*
+ * 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.integration;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.query.Query;
+import org.apache.ignite.cache.query.ScanQuery;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.cache.query.SqlQuery;
+import org.apache.ignite.cache.query.TextQuery;
+import org.apache.ignite.cache.query.annotations.QuerySqlField;
+import org.apache.ignite.cache.query.annotations.QuerySqlFunction;
+import org.apache.ignite.calcite.CalciteQueryEngineConfiguration;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.SqlConfiguration;
+import org.apache.ignite.indexing.IndexingQueryEngineConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.processors.query.QueryEngineConfigurationEx;
+import org.apache.ignite.internal.processors.query.running.SqlPlan;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+/** Tests for SQL plan history (Calcite engine). */
+@RunWith(Parameterized.class)
+public class SqlPlanHistoryIntegrationTest extends GridCommonAbstractTest {
+    /** SQL plan history size. */
+    private static final int PLAN_HISTORY_SIZE = 10;
+
+    /** SQL plan history size excess. */
+    private static final int PLAN_HISTORY_EXCESS = 2;
+
+    /** Simple SQL query. */
+    private static final String SQL = "SELECT * FROM A.String";
+
+    /** Failed SQL query. */
+    private static final String SQL_FAILED = "select * from A.String where 
A.fail()=1";
+
+    /** Cross-cache SQL query. */
+    private static final String SQL_CROSS_CACHE = "SELECT * FROM B.String";
+
+    /** Failed cross-cache SQL query. */
+    private static final String SQL_CROSS_CACHE_FAILED = "select * from 
B.String where B.fail()=1";
+
+    /** SQL query with reduce phase. */
+    private static final String SQL_WITH_REDUCE_PHASE = "select o.name n1, 
p.name n2 from \"pers\".Person p, " +
+        "\"org\".Organization o where p.orgId=o._key and o._key=101" +
+        " union select o.name n1, p.name n2 from \"pers\".Person p, 
\"org\".Organization o" +
+        " where p.orgId=o._key and o._key=102";
+
+    /** Set of simple DML commands. */
+    private final List<String> dmlCmds = Arrays.asList(
+        "insert into A.String (_key, _val) values(101, '101')",
+        "update A.String set _val='111' where _key=101",
+        "delete from A.String where _key=101"
+    );
+
+    /** Set of DML commands with joins. */
+    private final List<String> dmlCmdsWithJoins = Arrays.asList(
+        "insert into A.String (_key, _val) select o._key, p.name " +
+            "from \"pers\".Person p, \"org\".Organization o where 
p.orgId=o._key",
+        "update A.String set _val = 'updated' where _key in " +
+            "(select o._key from \"pers\".Person p, \"org\".Organization o 
where p.orgId=o._key)",
+        "delete from A.String where _key in (select orgId from 
\"pers\".Person)"
+    );
+
+    /** Successful SqlFieldsQuery. */
+    private final SqlFieldsQuery sqlFieldsQry = new SqlFieldsQuery(SQL);
+
+    /** Failed SqlFieldsQuery. */
+    private final SqlFieldsQuery sqlFieldsQryFailed = new 
SqlFieldsQuery(SQL_FAILED);
+
+    /** Successful cross-cache SqlFieldsQuery. */
+    private final SqlFieldsQuery sqlFieldsQryCrossCache = new 
SqlFieldsQuery(SQL_CROSS_CACHE);
+
+    /** Failed cross-cache SqlFieldsQuery. */
+    private final SqlFieldsQuery sqlFieldsQryCrossCacheFailed = new 
SqlFieldsQuery(SQL_CROSS_CACHE_FAILED);
+
+    /** Successful SqlFieldsQuery with reduce phase. */
+    private final SqlFieldsQuery sqlFieldsQryWithReducePhase = new 
SqlFieldsQuery(SQL_WITH_REDUCE_PHASE)
+        .setDistributedJoins(true);
+
+    /** Failed SqlFieldsQueries with reduce phase. */
+    private final SqlFieldsQuery[] sqlFieldsQryWithReducePhaseFailed = 
F.asArray(
+        new SqlFieldsQuery(SQL_WITH_REDUCE_PHASE.replace("o._key=101", 
"fail()")),
+        new SqlFieldsQuery(SQL_WITH_REDUCE_PHASE.replace("o._key=102", 
"fail()"))
+    );
+
+    /** Successful SqlQuery. */
+    private final SqlQuery sqlQry = new SqlQuery<>("String", "from String");
+
+    /** Failed SqlQuery. */
+    private final SqlQuery sqlQryFailed = new SqlQuery<>("String", "from 
String where fail()=1");
+
+    /** ScanQuery. */
+    private final ScanQuery<Integer, String> scanQry = new ScanQuery<>();
+
+    /** TextQuery. */
+    private final TextQuery<Integer, String> textQry = new 
TextQuery<>("String", "2");
+
+    /** Client mode flag. */
+    private boolean isClient;
+
+    /** SQL engine. */
+    protected String sqlEngine = CalciteQueryEngineConfiguration.ENGINE_NAME;
+
+    /** Local query flag. */
+    @Parameterized.Parameter
+    public boolean loc;
+
+    /** Fully-fetched query flag. */
+    @Parameterized.Parameter(1)
+    public boolean isFullyFetched;
+
+    /** */
+    @Parameterized.Parameters(name = "loc={0}, fullyFetched={1}")
+    public static Collection<Object[]> params() {
+        return Arrays.asList(new Object[][] {
+            {true, true}, {true, false}, {false, true}, {false, false}
+        });
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String 
igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        QueryEngineConfigurationEx engCfg = configureSqlEngine();
+
+        cfg.setSqlConfiguration(new SqlConfiguration()
+            .setSqlPlanHistorySize(PLAN_HISTORY_SIZE)
+            .setQueryEnginesConfiguration(engCfg)
+        );
+
+        return cfg.setCacheConfiguration(
+            configureCahce("A", Integer.class, String.class),
+            configureCahce("B", Integer.class, String.class),
+            configureCahce("pers", Integer.class, Person.class),
+            configureCahce("org", Integer.class, Organization.class)
+        );
+    }
+
+    /** */
+    protected QueryEngineConfigurationEx configureSqlEngine() {
+        return new CalciteQueryEngineConfiguration();
+    }
+
+    /**
+     * @param name Name.
+     * @param idxTypes Index types.
+     * @return Cache configuration.
+     */
+    @SuppressWarnings("unchecked")
+    private CacheConfiguration configureCahce(String name, Class<?>... 
idxTypes) {

Review Comment:
   Typo: Cahce



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SqlPlanHistoryConfigIntegrationTest.java:
##########
@@ -0,0 +1,41 @@
+/*
+ * 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.integration;
+
+import org.apache.ignite.Ignite;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+
+/** Test for Sql plan history configuration. */
+public class SqlPlanHistoryConfigIntegrationTest extends 
GridCommonAbstractTest {

Review Comment:
   Let's move this test to ignite-spring module, instead of adding new 
dependency to ignite-calcite.



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SqlPlanHistoryIntegrationTest.java:
##########
@@ -0,0 +1,748 @@
+/*
+ * 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.integration;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.query.Query;
+import org.apache.ignite.cache.query.ScanQuery;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.cache.query.SqlQuery;
+import org.apache.ignite.cache.query.TextQuery;
+import org.apache.ignite.cache.query.annotations.QuerySqlField;
+import org.apache.ignite.cache.query.annotations.QuerySqlFunction;
+import org.apache.ignite.calcite.CalciteQueryEngineConfiguration;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.SqlConfiguration;
+import org.apache.ignite.indexing.IndexingQueryEngineConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.processors.query.QueryEngineConfigurationEx;
+import org.apache.ignite.internal.processors.query.running.SqlPlan;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+/** Tests for SQL plan history (Calcite engine). */
+@RunWith(Parameterized.class)
+public class SqlPlanHistoryIntegrationTest extends GridCommonAbstractTest {
+    /** SQL plan history size. */
+    private static final int PLAN_HISTORY_SIZE = 10;
+
+    /** SQL plan history size excess. */
+    private static final int PLAN_HISTORY_EXCESS = 2;
+
+    /** Simple SQL query. */
+    private static final String SQL = "SELECT * FROM A.String";
+
+    /** Failed SQL query. */
+    private static final String SQL_FAILED = "select * from A.String where 
A.fail()=1";
+
+    /** Cross-cache SQL query. */
+    private static final String SQL_CROSS_CACHE = "SELECT * FROM B.String";
+
+    /** Failed cross-cache SQL query. */
+    private static final String SQL_CROSS_CACHE_FAILED = "select * from 
B.String where B.fail()=1";
+
+    /** SQL query with reduce phase. */
+    private static final String SQL_WITH_REDUCE_PHASE = "select o.name n1, 
p.name n2 from \"pers\".Person p, " +
+        "\"org\".Organization o where p.orgId=o._key and o._key=101" +
+        " union select o.name n1, p.name n2 from \"pers\".Person p, 
\"org\".Organization o" +
+        " where p.orgId=o._key and o._key=102";
+
+    /** Set of simple DML commands. */
+    private final List<String> dmlCmds = Arrays.asList(
+        "insert into A.String (_key, _val) values(101, '101')",
+        "update A.String set _val='111' where _key=101",
+        "delete from A.String where _key=101"
+    );
+
+    /** Set of DML commands with joins. */
+    private final List<String> dmlCmdsWithJoins = Arrays.asList(
+        "insert into A.String (_key, _val) select o._key, p.name " +
+            "from \"pers\".Person p, \"org\".Organization o where 
p.orgId=o._key",
+        "update A.String set _val = 'updated' where _key in " +
+            "(select o._key from \"pers\".Person p, \"org\".Organization o 
where p.orgId=o._key)",
+        "delete from A.String where _key in (select orgId from 
\"pers\".Person)"
+    );
+
+    /** Successful SqlFieldsQuery. */
+    private final SqlFieldsQuery sqlFieldsQry = new SqlFieldsQuery(SQL);
+
+    /** Failed SqlFieldsQuery. */
+    private final SqlFieldsQuery sqlFieldsQryFailed = new 
SqlFieldsQuery(SQL_FAILED);
+
+    /** Successful cross-cache SqlFieldsQuery. */
+    private final SqlFieldsQuery sqlFieldsQryCrossCache = new 
SqlFieldsQuery(SQL_CROSS_CACHE);
+
+    /** Failed cross-cache SqlFieldsQuery. */
+    private final SqlFieldsQuery sqlFieldsQryCrossCacheFailed = new 
SqlFieldsQuery(SQL_CROSS_CACHE_FAILED);
+
+    /** Successful SqlFieldsQuery with reduce phase. */
+    private final SqlFieldsQuery sqlFieldsQryWithReducePhase = new 
SqlFieldsQuery(SQL_WITH_REDUCE_PHASE)
+        .setDistributedJoins(true);
+
+    /** Failed SqlFieldsQueries with reduce phase. */
+    private final SqlFieldsQuery[] sqlFieldsQryWithReducePhaseFailed = 
F.asArray(
+        new SqlFieldsQuery(SQL_WITH_REDUCE_PHASE.replace("o._key=101", 
"fail()")),
+        new SqlFieldsQuery(SQL_WITH_REDUCE_PHASE.replace("o._key=102", 
"fail()"))
+    );
+
+    /** Successful SqlQuery. */
+    private final SqlQuery sqlQry = new SqlQuery<>("String", "from String");
+
+    /** Failed SqlQuery. */
+    private final SqlQuery sqlQryFailed = new SqlQuery<>("String", "from 
String where fail()=1");
+
+    /** ScanQuery. */
+    private final ScanQuery<Integer, String> scanQry = new ScanQuery<>();
+
+    /** TextQuery. */
+    private final TextQuery<Integer, String> textQry = new 
TextQuery<>("String", "2");
+
+    /** Client mode flag. */
+    private boolean isClient;
+
+    /** SQL engine. */
+    protected String sqlEngine = CalciteQueryEngineConfiguration.ENGINE_NAME;
+
+    /** Local query flag. */
+    @Parameterized.Parameter
+    public boolean loc;
+
+    /** Fully-fetched query flag. */
+    @Parameterized.Parameter(1)
+    public boolean isFullyFetched;
+
+    /** */
+    @Parameterized.Parameters(name = "loc={0}, fullyFetched={1}")
+    public static Collection<Object[]> params() {
+        return Arrays.asList(new Object[][] {
+            {true, true}, {true, false}, {false, true}, {false, false}
+        });
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String 
igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        QueryEngineConfigurationEx engCfg = configureSqlEngine();
+
+        cfg.setSqlConfiguration(new SqlConfiguration()
+            .setSqlPlanHistorySize(PLAN_HISTORY_SIZE)
+            .setQueryEnginesConfiguration(engCfg)
+        );
+
+        return cfg.setCacheConfiguration(
+            configureCahce("A", Integer.class, String.class),
+            configureCahce("B", Integer.class, String.class),
+            configureCahce("pers", Integer.class, Person.class),
+            configureCahce("org", Integer.class, Organization.class)
+        );
+    }
+
+    /** */
+    protected QueryEngineConfigurationEx configureSqlEngine() {
+        return new CalciteQueryEngineConfiguration();
+    }
+
+    /**
+     * @param name Name.
+     * @param idxTypes Index types.
+     * @return Cache configuration.
+     */
+    @SuppressWarnings("unchecked")
+    private CacheConfiguration configureCahce(String name, Class<?>... 
idxTypes) {
+        return new CacheConfiguration()
+            .setName(name)
+            .setIndexedTypes(idxTypes)
+            .setSqlFunctionClasses(Functions.class);
+    }
+
+    /**
+     * @return Ignite node where queries are executed.
+     */
+    protected IgniteEx queryNode() {
+        IgniteEx node = grid(0);
+
+        assertFalse(node.context().clientNode());
+
+        return node;
+    }
+
+    /**
+     * @return Ignite map node.
+     */
+    protected IgniteEx mapNode() {
+        IgniteEx node = grid(1);
+
+        assertFalse(node.context().clientNode());
+
+        return node;
+    }
+
+    /**
+     * Starts Ignite instance.
+     *
+     * @throws Exception In case of failure.
+     */
+    protected void startTestGrid() throws Exception {
+        startGrids(2);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        super.beforeTestsStarted();
+
+        startTestGrid();
+
+        IgniteCache<Integer, String> cacheA = queryNode().cache("A");
+        IgniteCache<Integer, String> cacheB = queryNode().cache("B");
+
+        for (int i = 0; i < 100; i++) {
+            cacheA.put(i, String.valueOf(i));
+            cacheB.put(i, String.valueOf(i));
+        }
+
+        IgniteCache<Integer, Person> cachePers = queryNode().cache("pers");
+        IgniteCache<Integer, Organization> cacheOrg = queryNode().cache("org");
+
+        cacheOrg.put(101, new Organization("o1"));
+        cacheOrg.put(102, new Organization("o2"));
+        cachePers.put(103, new Person(101, "p1"));
+        cachePers.put(104, new Person(102, "p2"));
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTestsStopped() throws Exception {
+        super.afterTestsStopped();
+
+        stopAllGrids();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        resetPlanHistory();
+    }
+
+    /**
+     * @param sqlEngine Sql engine.
+     */
+    protected void setSqlEngine(String sqlEngine) {
+        this.sqlEngine = sqlEngine;
+    }
+
+    /**
+     * @param isClient Client more flag.
+     */
+    protected void setClientMode(boolean isClient) {
+        this.isClient = isClient;
+    }
+
+    /**
+     * Clears current SQL plan history.
+     */
+    public void resetPlanHistory() {
+        
queryNode().context().query().runningQueryManager().resetPlanHistoryMetrics();
+
+        
mapNode().context().query().runningQueryManager().resetPlanHistoryMetrics();

Review Comment:
   ```
   for (IgniteEx ignite: G.allGrids())
       ignite.context().query().runningQueryManager().resetPlanHistoryMetrics();
   ```



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SqlPlanHistoryIntegrationTest.java:
##########
@@ -0,0 +1,748 @@
+/*
+ * 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.integration;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.query.Query;
+import org.apache.ignite.cache.query.ScanQuery;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.cache.query.SqlQuery;
+import org.apache.ignite.cache.query.TextQuery;
+import org.apache.ignite.cache.query.annotations.QuerySqlField;
+import org.apache.ignite.cache.query.annotations.QuerySqlFunction;
+import org.apache.ignite.calcite.CalciteQueryEngineConfiguration;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.SqlConfiguration;
+import org.apache.ignite.indexing.IndexingQueryEngineConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.processors.query.QueryEngineConfigurationEx;
+import org.apache.ignite.internal.processors.query.running.SqlPlan;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+/** Tests for SQL plan history (Calcite engine). */
+@RunWith(Parameterized.class)
+public class SqlPlanHistoryIntegrationTest extends GridCommonAbstractTest {
+    /** SQL plan history size. */
+    private static final int PLAN_HISTORY_SIZE = 10;
+
+    /** SQL plan history size excess. */
+    private static final int PLAN_HISTORY_EXCESS = 2;
+
+    /** Simple SQL query. */
+    private static final String SQL = "SELECT * FROM A.String";
+
+    /** Failed SQL query. */
+    private static final String SQL_FAILED = "select * from A.String where 
A.fail()=1";
+
+    /** Cross-cache SQL query. */
+    private static final String SQL_CROSS_CACHE = "SELECT * FROM B.String";
+
+    /** Failed cross-cache SQL query. */
+    private static final String SQL_CROSS_CACHE_FAILED = "select * from 
B.String where B.fail()=1";
+
+    /** SQL query with reduce phase. */
+    private static final String SQL_WITH_REDUCE_PHASE = "select o.name n1, 
p.name n2 from \"pers\".Person p, " +
+        "\"org\".Organization o where p.orgId=o._key and o._key=101" +
+        " union select o.name n1, p.name n2 from \"pers\".Person p, 
\"org\".Organization o" +
+        " where p.orgId=o._key and o._key=102";
+
+    /** Set of simple DML commands. */
+    private final List<String> dmlCmds = Arrays.asList(
+        "insert into A.String (_key, _val) values(101, '101')",
+        "update A.String set _val='111' where _key=101",
+        "delete from A.String where _key=101"
+    );
+
+    /** Set of DML commands with joins. */
+    private final List<String> dmlCmdsWithJoins = Arrays.asList(
+        "insert into A.String (_key, _val) select o._key, p.name " +
+            "from \"pers\".Person p, \"org\".Organization o where 
p.orgId=o._key",
+        "update A.String set _val = 'updated' where _key in " +
+            "(select o._key from \"pers\".Person p, \"org\".Organization o 
where p.orgId=o._key)",
+        "delete from A.String where _key in (select orgId from 
\"pers\".Person)"
+    );
+
+    /** Successful SqlFieldsQuery. */
+    private final SqlFieldsQuery sqlFieldsQry = new SqlFieldsQuery(SQL);
+
+    /** Failed SqlFieldsQuery. */
+    private final SqlFieldsQuery sqlFieldsQryFailed = new 
SqlFieldsQuery(SQL_FAILED);
+
+    /** Successful cross-cache SqlFieldsQuery. */
+    private final SqlFieldsQuery sqlFieldsQryCrossCache = new 
SqlFieldsQuery(SQL_CROSS_CACHE);
+
+    /** Failed cross-cache SqlFieldsQuery. */
+    private final SqlFieldsQuery sqlFieldsQryCrossCacheFailed = new 
SqlFieldsQuery(SQL_CROSS_CACHE_FAILED);
+
+    /** Successful SqlFieldsQuery with reduce phase. */
+    private final SqlFieldsQuery sqlFieldsQryWithReducePhase = new 
SqlFieldsQuery(SQL_WITH_REDUCE_PHASE)
+        .setDistributedJoins(true);
+
+    /** Failed SqlFieldsQueries with reduce phase. */
+    private final SqlFieldsQuery[] sqlFieldsQryWithReducePhaseFailed = 
F.asArray(
+        new SqlFieldsQuery(SQL_WITH_REDUCE_PHASE.replace("o._key=101", 
"fail()")),
+        new SqlFieldsQuery(SQL_WITH_REDUCE_PHASE.replace("o._key=102", 
"fail()"))
+    );
+
+    /** Successful SqlQuery. */
+    private final SqlQuery sqlQry = new SqlQuery<>("String", "from String");
+
+    /** Failed SqlQuery. */
+    private final SqlQuery sqlQryFailed = new SqlQuery<>("String", "from 
String where fail()=1");
+
+    /** ScanQuery. */
+    private final ScanQuery<Integer, String> scanQry = new ScanQuery<>();
+
+    /** TextQuery. */
+    private final TextQuery<Integer, String> textQry = new 
TextQuery<>("String", "2");
+
+    /** Client mode flag. */
+    private boolean isClient;
+
+    /** SQL engine. */
+    protected String sqlEngine = CalciteQueryEngineConfiguration.ENGINE_NAME;
+
+    /** Local query flag. */
+    @Parameterized.Parameter
+    public boolean loc;
+
+    /** Fully-fetched query flag. */
+    @Parameterized.Parameter(1)
+    public boolean isFullyFetched;
+
+    /** */
+    @Parameterized.Parameters(name = "loc={0}, fullyFetched={1}")
+    public static Collection<Object[]> params() {
+        return Arrays.asList(new Object[][] {
+            {true, true}, {true, false}, {false, true}, {false, false}
+        });
+    }
+
+    /** {@inheritDoc} */
+    @Override protected IgniteConfiguration getConfiguration(String 
igniteInstanceName) throws Exception {
+        IgniteConfiguration cfg = super.getConfiguration(igniteInstanceName);
+
+        QueryEngineConfigurationEx engCfg = configureSqlEngine();
+
+        cfg.setSqlConfiguration(new SqlConfiguration()
+            .setSqlPlanHistorySize(PLAN_HISTORY_SIZE)
+            .setQueryEnginesConfiguration(engCfg)
+        );
+
+        return cfg.setCacheConfiguration(
+            configureCahce("A", Integer.class, String.class),
+            configureCahce("B", Integer.class, String.class),
+            configureCahce("pers", Integer.class, Person.class),
+            configureCahce("org", Integer.class, Organization.class)
+        );
+    }
+
+    /** */
+    protected QueryEngineConfigurationEx configureSqlEngine() {
+        return new CalciteQueryEngineConfiguration();
+    }
+
+    /**
+     * @param name Name.
+     * @param idxTypes Index types.
+     * @return Cache configuration.
+     */
+    @SuppressWarnings("unchecked")
+    private CacheConfiguration configureCahce(String name, Class<?>... 
idxTypes) {
+        return new CacheConfiguration()
+            .setName(name)
+            .setIndexedTypes(idxTypes)
+            .setSqlFunctionClasses(Functions.class);
+    }
+
+    /**
+     * @return Ignite node where queries are executed.
+     */
+    protected IgniteEx queryNode() {
+        IgniteEx node = grid(0);
+
+        assertFalse(node.context().clientNode());
+
+        return node;
+    }
+
+    /**
+     * @return Ignite map node.
+     */
+    protected IgniteEx mapNode() {
+        IgniteEx node = grid(1);
+
+        assertFalse(node.context().clientNode());
+
+        return node;
+    }
+
+    /**
+     * Starts Ignite instance.
+     *
+     * @throws Exception In case of failure.
+     */
+    protected void startTestGrid() throws Exception {
+        startGrids(2);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTestsStarted() throws Exception {
+        super.beforeTestsStarted();
+
+        startTestGrid();
+
+        IgniteCache<Integer, String> cacheA = queryNode().cache("A");
+        IgniteCache<Integer, String> cacheB = queryNode().cache("B");
+
+        for (int i = 0; i < 100; i++) {
+            cacheA.put(i, String.valueOf(i));
+            cacheB.put(i, String.valueOf(i));
+        }
+
+        IgniteCache<Integer, Person> cachePers = queryNode().cache("pers");
+        IgniteCache<Integer, Organization> cacheOrg = queryNode().cache("org");
+
+        cacheOrg.put(101, new Organization("o1"));
+        cacheOrg.put(102, new Organization("o2"));
+        cachePers.put(103, new Person(101, "p1"));
+        cachePers.put(104, new Person(102, "p2"));
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void afterTestsStopped() throws Exception {
+        super.afterTestsStopped();
+
+        stopAllGrids();
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        resetPlanHistory();
+    }
+
+    /**
+     * @param sqlEngine Sql engine.
+     */
+    protected void setSqlEngine(String sqlEngine) {
+        this.sqlEngine = sqlEngine;
+    }
+
+    /**
+     * @param isClient Client more flag.
+     */
+    protected void setClientMode(boolean isClient) {
+        this.isClient = isClient;
+    }
+
+    /**
+     * Clears current SQL plan history.
+     */
+    public void resetPlanHistory() {
+        
queryNode().context().query().runningQueryManager().resetPlanHistoryMetrics();
+
+        
mapNode().context().query().runningQueryManager().resetPlanHistoryMetrics();
+    }
+
+    /** Checks successful JDBC queries. */
+    @Test
+    public void testJdbcQuery() throws SQLException {
+        if (loc)

Review Comment:
   To ignore test it's better to use assumeTrue/assumeFalse statements. In your 
case `assumeFalse(loc)`.
   Tests will be marked as ignored instead of passed (and we can also provide a 
reason, why they are ignored). 



##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SqlPlanHistoryIntegrationTest.java:
##########
@@ -0,0 +1,748 @@
+/*
+ * 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.integration;
+
+import java.sql.Connection;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+import java.util.ArrayList;
+import java.util.Arrays;
+import java.util.Collection;
+import java.util.List;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.stream.Collectors;
+import org.apache.ignite.IgniteCache;
+import org.apache.ignite.cache.query.Query;
+import org.apache.ignite.cache.query.ScanQuery;
+import org.apache.ignite.cache.query.SqlFieldsQuery;
+import org.apache.ignite.cache.query.SqlQuery;
+import org.apache.ignite.cache.query.TextQuery;
+import org.apache.ignite.cache.query.annotations.QuerySqlField;
+import org.apache.ignite.cache.query.annotations.QuerySqlFunction;
+import org.apache.ignite.calcite.CalciteQueryEngineConfiguration;
+import org.apache.ignite.configuration.CacheConfiguration;
+import org.apache.ignite.configuration.IgniteConfiguration;
+import org.apache.ignite.configuration.SqlConfiguration;
+import org.apache.ignite.indexing.IndexingQueryEngineConfiguration;
+import org.apache.ignite.internal.IgniteEx;
+import org.apache.ignite.internal.IgniteInterruptedCheckedException;
+import org.apache.ignite.internal.processors.query.IgniteSQLException;
+import org.apache.ignite.internal.processors.query.QueryEngineConfigurationEx;
+import org.apache.ignite.internal.processors.query.running.SqlPlan;
+import org.apache.ignite.internal.util.typedef.F;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+/** Tests for SQL plan history (Calcite engine). */
+@RunWith(Parameterized.class)
+public class SqlPlanHistoryIntegrationTest extends GridCommonAbstractTest {

Review Comment:
   Test already Parameterized. Perhaps it's worth to add parameters queryEngine 
and clientMode instead of creating child classes?



##########
modules/core/src/main/java/org/apache/ignite/internal/processors/query/running/SqlPlanHistoryTracker.java:
##########
@@ -0,0 +1,74 @@
+/*
+ * 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.running;
+
+import java.util.Collections;
+import java.util.Set;
+import org.apache.ignite.internal.util.GridBoundedConcurrentLinkedHashSet;
+
+/** Class that manages recording and storing SQL plans. */
+public class SqlPlanHistoryTracker {
+    /** SQL plan history. */
+    private final GridBoundedConcurrentLinkedHashSet<SqlPlan> sqlPlanHistory;
+
+    /** SQL plan history size. */
+    private int historySize;
+
+    /**
+     * @param historySize SQL plan history size.
+     */
+    public SqlPlanHistoryTracker(int historySize) {
+        this.historySize = historySize;
+
+        sqlPlanHistory = (historySize > 0) ? new 
GridBoundedConcurrentLinkedHashSet<>(historySize) : null;
+    }
+
+    /**
+     * @param plan SQL plan.
+     * @param qry Query.
+     * @param schema Schema name.
+     * @param loc Local query flag.
+     * @param engine SQL engine.
+     */
+    public void addPlan(String plan, String qry, String schema, boolean loc, 
String engine) {
+        if (historySize <= 0)
+            return;
+
+        SqlPlan sqlPlan = new SqlPlan(plan, qry, schema, loc, engine);
+
+        if (sqlPlanHistory.contains(sqlPlan))
+            sqlPlanHistory.remove(sqlPlan);
+
+        sqlPlanHistory.add(sqlPlan);
+    }
+
+    /** */
+    public Set<SqlPlan> sqlPlanHistory() {
+        if (historySize <= 0)
+            return Collections.emptySet();
+
+        return Collections.unmodifiableSet(sqlPlanHistory);
+    }
+
+    /**
+     * @param historySize History size.
+     */
+    public void setHistorySize(int historySize) {
+        this.historySize = historySize;

Review Comment:
   Let's recreate sqlPlanHistory here. Now this method used only once in test 
with value 0, but if someone will use this method (perhaps in test too) with 
another value, result can be unpredicted.



-- 
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: notifications-unsubscr...@ignite.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Reply via email to