lowka commented on code in PR #4221: URL: https://github.com/apache/ignite-3/pull/4221#discussion_r1721269288
########## modules/sql-engine/src/integrationTest/java/org/apache/ignite/internal/sql/engine/ItSqlUsesSelectCountOptimizedTest.java: ########## @@ -0,0 +1,151 @@ +/* + * 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.sql.engine; + +import org.apache.ignite.internal.sql.BaseSqlIntegrationTest; +import org.apache.ignite.internal.sql.engine.util.QueryChecker; +import org.apache.ignite.internal.testframework.SystemPropertiesExtension; +import org.apache.ignite.internal.testframework.WithSystemProperty; +import org.apache.ignite.internal.tx.InternalTransaction; +import org.apache.ignite.tx.Transaction; +import org.apache.ignite.tx.TransactionOptions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** + * Tests for SELECT COUNT(*) optimization. + */ +@ExtendWith(SystemPropertiesExtension.class) +public class ItSqlUsesSelectCountOptimizedTest extends BaseSqlIntegrationTest { + + @BeforeAll + @SuppressWarnings("ConcatenationWithEmptyString") + static void initSchema() { + CLUSTER.aliveNode().sql().executeScript("" + + "CREATE TABLE test (id INT PRIMARY KEY, val INT);" + + "INSERT INTO test SELECT x, x FROM TABLE(system_range(1, 10));"); + } + + @Test + public void countOpt() { + assertQuery("SELECT COUNT(*) FROM test") + .matches(QueryChecker.containsSubPlan("SelectCount")) + .returns(10L) + .check(); + + assertQuery("SELECT 1, COUNT(*) FROM test") + .matches(QueryChecker.containsSubPlan("SelectCount")) + .returns(1, 10L) + .check(); + + assertQuery("SELECT ?, COUNT(*) FROM test") + .withParam(1) + .matches(QueryChecker.containsSubPlan("SelectCount")) + .returns(1, 10L) + .check(); + + assertQuery("SELECT COUNT(1) FROM test") + .matches(QueryChecker.containsSubPlan("SelectCount")) + .returns(10L) + .check(); + } + + @Test + public void countOptNoTable() { + assertQuery("SELECT COUNT(*)") + .matches(QueryChecker.containsSubPlan("SelectCount")) + .returns(1L) Review Comment: This query always produces a single row, thus count(1) returns 1. But this code is removed. ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/prepare/PlannerHelper.java: ########## @@ -343,4 +360,171 @@ static boolean hasSubQuery(SqlNode node) { return super.visit(call); } } + + + /** + * Tries to optimize a query that looks like {@code SELECT count(*)}. + * + * @param planner Planner. + * @param txContext Transactional context. + * @param node Query node. + * @return Plan node with list of aliases, if the optimization is applicable. + */ + public static @Nullable Pair<IgniteRel, List<String>> tryOptimizeSelectCount( + IgnitePlanner planner, + @Nullable QueryTransactionContext txContext, + SqlNode node + ) { + if (txContext != null && txContext.explicitTx() != null) { + return null; + } + + if (!(node instanceof SqlSelect)) { + return null; + } + + SqlSelect select = (SqlSelect) node; + + if (select.getGroup() != null + || select.getWhere() != null + || select.getHaving() != null + || select.getQualify() != null + || !select.getWindowList().isEmpty() + || select.getOffset() != null + || select.getFetch() != null) { + return null; + } + + IgniteSqlToRelConvertor converter = planner.sqlToRelConverter(); + + RelOptTable targetTable; + + if (select.getFrom() != null) { + SqlNode from = SqlUtil.stripAs(select.getFrom()); + // Skip non-references such as VALUES .. + if (from.getKind() != SqlKind.IDENTIFIER) { + return null; + } + + targetTable = converter.getTargetTable(select.getFrom()); + IgniteDataSource dataSource = targetTable.unwrap(IgniteDataSource.class); + if (!(dataSource instanceof IgniteTable)) { + return null; + } + } else { + // Try to optimize SELECT count(*) as well + targetTable = null; + } + + IgniteTypeFactory typeFactory = planner.getTypeFactory(); + + // SELECT COUNT(*) ... row type + RelDataType countResultType = typeFactory.createSqlType(SqlTypeName.BIGINT); + + RelDataTypeFactory.Builder inputRowBuilder = new Builder(typeFactory); + inputRowBuilder.add("ROWCOUNT", countResultType); + RelDataType inputRowType = inputRowBuilder.build(); Review Comment: Thanks fixed. -- 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]
