oleg-vlsk commented on code in PR #11425: URL: https://github.com/apache/ignite/pull/11425#discussion_r1855356323
########## 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); + }); + } + + /** Checks failed SqlFieldsQuery with reduce phase. */ + @Test + public void testSqlFieldsQueryWithReducePhaseFailed() { + runQueryWithReducePhase(() -> { + try { + cacheQuery(sqlFieldsQryWithReducePhaseFailed.setLocal(loc), "pers"); + } + catch (Exception ignore) { + //No-Op + } + + checkSqlPlanHistory((!isClient && sqlEngine == IndexingQueryEngineConfiguration.ENGINE_NAME) ? 1 : 0); + }); + } + + /** Checks successful SqlQuery. */ + @Test + public void testSqlQuery() { + runSuccessfulQuery(sqlQry); + } + + /** Checks failed SqlQuery. */ + @Test + public void testSqlQueryFailed() { + runFailedQuery(sqlQryFailed); + } + + /** Checks ScanQuery. */ + @Test + public void testScanQuery() { + runQueryWithoutPlan(scanQry); + } + + /** Checks TextQuery. */ + @Test + public void testTextQuery() { + runQueryWithoutPlan(textQry); + } + + /** Checks DML commands executed via JDBC. */ + @Test + public void testJdbcDml() { + runJdbcDml(dmlCmds); + } + + /** Checks DML commands with joins executed via JDBC. */ + @Test + public void testJdbcDmlWithJoins() { + runJdbcDml(dmlCmdsWithJoins); + } + + /** Checks DML commands executed via SqlFieldsQuery. */ + @Test + public void testSqlFieldsDml() { + runSqlFieldsDml(dmlCmds); + } + + /** Checks DML commands with joins executed via SqlFieldsQuery. */ + @Test + public void testSqlFieldsDmlWithJoins() { + runSqlFieldsDml(dmlCmdsWithJoins); + } + + /** Checks that older plan entries are evicted when maximum history size is reached. */ + @Test + public void testPlanHistoryEviction() throws Exception { + assumeFalse("No SQL plans are written on the client node when using the H2 engine", + isClient && sqlEngine == IndexingQueryEngineConfiguration.ENGINE_NAME); + + for (int i = 1; i <= (PLAN_HISTORY_SIZE + PLAN_HISTORY_EXCESS); i++) { + try { + SqlFieldsQuery qry = new SqlFieldsQuery(SQL + " where A.fail()=" + i); + + cacheQuery(qry.setLocal(loc), "A"); + } + catch (Exception ignore) { + //No-op + } + } + + GridTestUtils.waitForCondition(() -> getSqlPlanHistory().size() == PLAN_HISTORY_SIZE, 1000); + + checkSqlPlanHistory(PLAN_HISTORY_SIZE); Review Comment: Check has been removed. -- 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