oleg-vlsk commented on code in PR #11425:
URL: https://github.com/apache/ignite/pull/11425#discussion_r1855379142


##########
modules/calcite/src/test/java/org/apache/ignite/internal/processors/query/calcite/integration/SqlPlanHistoryIntegrationTest.java:
##########
@@ -0,0 +1,718 @@
+/*
+ * 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.Comparator;
+import java.util.List;
+import java.util.Map;
+import java.util.Set;
+import java.util.function.Consumer;
+import java.util.function.Function;
+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.internal.U;
+import org.apache.ignite.lang.IgniteBiTuple;
+import org.apache.ignite.testframework.GridTestUtils;
+import org.apache.ignite.testframework.junits.common.GridCommonAbstractTest;
+import org.junit.AssumptionViolatedException;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.junit.runners.Parameterized;
+
+import static org.junit.Assume.assumeFalse;
+
+/** Tests for SQL plan history. */
+@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";
+
+    /** List of simple DML commands and the simple queries flag. */
+    private final IgniteBiTuple<List<String>, Boolean> dmlCmds = new 
IgniteBiTuple<>(
+        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"
+        ), true
+    );
+
+    /** List of DML commands with joins and the simple queries flag. */
+    private final IgniteBiTuple<List<String>, Boolean> dmlCmdsWithJoins = new 
IgniteBiTuple<>(
+        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)"
+        ), false
+    );
+
+    /** 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 SqlFieldsQuery with reduce phase. */
+    private final SqlFieldsQuery sqlFieldsQryWithReducePhaseFailed =
+        new SqlFieldsQuery(SQL_WITH_REDUCE_PHASE.replace("o._key=101", 
"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");
+
+    /** Flag for queries with map-reduce phases. */
+    private boolean isReducePhase;
+
+    /** SQL engine. */
+    @Parameterized.Parameter
+    public String sqlEngine;
+
+    /** Client mode flag. */
+    @Parameterized.Parameter(1)
+    public boolean isClient;
+
+    /** Local query flag. */
+    @Parameterized.Parameter(2)
+    public boolean loc;
+
+    /** Fully-fetched query flag. */
+    @Parameterized.Parameter(3)
+    public boolean isFullyFetched;
+
+    /** */
+    @Parameterized.Parameters(name = "sqlEngine={0}, isClient={1} loc={2}, 
isFullyFetched={3}")
+    public static Collection<Object[]> params() {
+        return Arrays.stream(new Object[][]{
+            {CalciteQueryEngineConfiguration.ENGINE_NAME},
+            {IndexingQueryEngineConfiguration.ENGINE_NAME}
+        }).flatMap(sqlEngine -> Arrays.stream(new Boolean[]{true, false})
+                .flatMap(isClient -> Arrays.stream(new Boolean[]{true, false})
+                    .flatMap(loc -> Arrays.stream(new Boolean[]{true, false})
+                        .map(isFullyFetched -> new Object[]{sqlEngine[0], 
isClient, loc, isFullyFetched})))
+        ).collect(Collectors.toList());
+    }
+
+    /** {@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(
+            configureCache("A", Integer.class, String.class),
+            configureCache("B", Integer.class, String.class),
+            configureCache("pers", Integer.class, Person.class),
+            configureCache("org", Integer.class, Organization.class)
+        );
+    }
+
+    /** */
+    protected QueryEngineConfigurationEx configureSqlEngine() {
+        if (sqlEngine.equals(CalciteQueryEngineConfiguration.ENGINE_NAME))
+            return new CalciteQueryEngineConfiguration();
+        else
+            return new IndexingQueryEngineConfiguration();
+    }
+
+    /**
+     * @param name Name.
+     * @param idxTypes Index types.
+     * @return Cache configuration.
+     */
+    @SuppressWarnings("unchecked")
+    private CacheConfiguration configureCache(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 = isClient ? grid(1) : grid(0);
+
+        if (isClient)
+            assertTrue(node.context().clientNode());
+        else
+            assertFalse(node.context().clientNode());
+
+        return node;
+    }
+
+    /**
+     * Starts Ignite instance.
+     *
+     * @throws Exception In case of failure.
+     */
+    protected void startTestGrid() throws Exception {
+        startGrid(0);
+
+        if (isClient)
+            startClientGrid(1);
+    }
+
+    /** {@inheritDoc} */
+    @Override protected void beforeTest() throws Exception {
+        assumeFalse("Local queries can't be executed on client nodes", 
isClient && loc);
+
+        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 afterTest() throws Exception {
+        stopAllGrids();
+    }
+
+    /** Checks successful JDBC queries. */
+    @Test
+    public void testJdbcQuery() throws Exception {
+        for (int i = 0; i < 2; i++) {
+            jdbcQuery(SQL);
+
+            checkSqlPlanHistory(1);
+        }
+    }
+
+    /** Checks failed JDBC queries. */
+    @Test
+    public void testJdbcQueryFailed() throws Exception {
+        try {
+            jdbcQuery(SQL_FAILED);
+        }
+        catch (Exception e) {
+            if (e instanceof AssumptionViolatedException)
+                throw e;
+        }
+
+        checkSqlPlanHistory(1);
+    }
+
+    /** Checks successful SqlFieldsQuery. */
+    @Test
+    public void testSqlFieldsQuery() {
+        runSuccessfulQuery(sqlFieldsQry);
+    }
+
+    /** Checks failed SqlFieldsQuery. */
+    @Test
+    public void testSqlFieldsQueryFailed() {
+        runFailedQuery(sqlFieldsQryFailed);
+    }
+
+    /** Checks successful cross-cache SqlFieldsQuery. */
+    @Test
+    public void testSqlFieldsCrossCacheQuery() {
+        runSuccessfulQuery(sqlFieldsQryCrossCache);
+    }
+
+    /** Checks failed cross-cache SqlFieldsQuery. */
+    @Test
+    public void testSqlFieldsCrossCacheQueryFailed() {
+        runFailedQuery(sqlFieldsQryCrossCacheFailed);
+    }
+
+    /** Checks successful SqlFieldsQuery with reduce phase. */
+    @Test
+    public void testSqlFieldsQueryWithReducePhase() {
+        runQueryWithReducePhase(() -> {
+            cacheQuery(sqlFieldsQryWithReducePhase.setLocal(loc), "pers");
+
+            checkSqlPlanHistory((!isClient && sqlEngine == 
IndexingQueryEngineConfiguration.ENGINE_NAME) ? 3 : 1);

Review Comment:
   2 entries for each SELECT sub-query (map phase) + 1 entry for the UNION 
sub-query (reduce phase). For example:
   
   SELECT 1
   ```
   SELECT
       O__Z1.NAME AS __C0_0,
       P__Z0.NAME AS __C0_1
   FROM "org".ORGANIZATION O__Z1
       /* "org"."_key_PK": _KEY = 101 */
       /* WHERE O__Z1._KEY = 101
       */
   INNER JOIN "pers".PERSON P__Z0
       /* batched:broadcast "pers".PERSON_ORGID_IDX: ORGID = O__Z1._KEY */
       ON 1=1
   WHERE (O__Z1._KEY = 101)
       AND (P__Z0.ORGID = O__Z1._KEY)
   ```
   SELECT 2
   ```
   SELECT
       O__Z3.NAME AS __C1_0,
       P__Z2.NAME AS __C1_1
   FROM "org".ORGANIZATION O__Z3
       /* "org"."_key_PK": _KEY = 102 */
       /* WHERE O__Z3._KEY = 102
       */
   INNER JOIN "pers".PERSON P__Z2
       /* batched:broadcast "pers".PERSON_ORGID_IDX: ORGID = O__Z3._KEY */
       ON 1=1
   WHERE (O__Z3._KEY = 102)
       AND (P__Z2.ORGID = O__Z3._KEY)
   ```
   UNION
   ```
   (SELECT
       __C0_0 AS N1,
       __C0_1 AS N2
   FROM PUBLIC.__T0
       /* "pers"."merge_scan" */)
   UNION
   (SELECT
       __C1_0 AS N1,
       __C1_1 AS N2
   FROM PUBLIC.__T1
       /* "pers"."merge_scan" */)
   ```



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