korlov42 commented on code in PR #4221: URL: https://github.com/apache/ignite-3/pull/4221#discussion_r1719497075
########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/rel/IgniteSelectCount.java: ########## @@ -0,0 +1,131 @@ +/* + * 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.rel; + +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; + +import java.util.List; +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.plan.RelOptCost; +import org.apache.calcite.plan.RelOptPlanner; +import org.apache.calcite.plan.RelOptTable; +import org.apache.calcite.plan.RelTraitSet; +import org.apache.calcite.rel.AbstractRelNode; +import org.apache.calcite.rel.RelWriter; +import org.apache.calcite.rel.metadata.RelMetadataQuery; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexNode; +import org.apache.ignite.internal.sql.engine.metadata.cost.IgniteCostFactory; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Relational operator that represents a SELECT COUNT(*) query. Review Comment: Description is too vague. This node represents not an abstract SELECT COUNT(*) queries, but with number of conditions/limitations. Let's improve javadoc. with information about these conditions/limitations. It would be nice to provide a few queries for example. ########## modules/sql-engine/src/main/java/org/apache/ignite/internal/sql/engine/rel/IgniteSelectCount.java: ########## @@ -0,0 +1,131 @@ +/* + * 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.rel; + +import static org.apache.ignite.internal.lang.IgniteStringFormatter.format; + +import java.util.List; +import org.apache.calcite.plan.RelOptCluster; +import org.apache.calcite.plan.RelOptCost; +import org.apache.calcite.plan.RelOptPlanner; +import org.apache.calcite.plan.RelOptTable; +import org.apache.calcite.plan.RelTraitSet; +import org.apache.calcite.rel.AbstractRelNode; +import org.apache.calcite.rel.RelWriter; +import org.apache.calcite.rel.metadata.RelMetadataQuery; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexNode; +import org.apache.ignite.internal.sql.engine.metadata.cost.IgniteCostFactory; +import org.checkerframework.checker.nullness.qual.Nullable; + +/** + * Relational operator that represents a SELECT COUNT(*) query. + */ +public class IgniteSelectCount extends AbstractRelNode implements IgniteRel { + + private static final String REL_TYPE_NAME = "SelectCount"; + + private final RelOptTable table; + private final List<RexNode> expressions; + private final RelDataType rowType; + + /** + * Constructor. + * + * @param cluster A cluster this relation belongs to. + * @param traits A set of traits this node satisfies. + * @param table A target table. + * @param rowType A row type. + * @param expressions Returns a list of expressions returned alongside COUNT(*). + */ + public IgniteSelectCount( + RelOptCluster cluster, + RelTraitSet traits, + @Nullable RelOptTable table, + RelDataType rowType, Review Comment: why do we need to pass it explicitly when we can always derive it from a list of expressions? ########## modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/SelectCountPlannerTest.java: ########## @@ -0,0 +1,366 @@ +/* + * 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.planner; + +import static java.util.stream.Collectors.toList; +import static org.apache.ignite.internal.testframework.IgniteTestUtils.await; +import static org.hamcrest.CoreMatchers.not; +import static org.hamcrest.MatcherAssert.assertThat; +import static org.hamcrest.Matchers.equalTo; +import static org.hamcrest.Matchers.instanceOf; +import static org.junit.jupiter.api.Assertions.assertEquals; + +import java.util.ArrayList; +import java.util.List; +import org.apache.calcite.rel.type.RelDataType; +import org.apache.calcite.rex.RexNode; +import org.apache.ignite.internal.catalog.CatalogCommand; +import org.apache.ignite.internal.catalog.commands.DropTableCommand; +import org.apache.ignite.internal.catalog.descriptors.CatalogTableDescriptor; +import org.apache.ignite.internal.sql.SqlCommon; +import org.apache.ignite.internal.sql.engine.framework.TestBuilders; +import org.apache.ignite.internal.sql.engine.framework.TestCluster; +import org.apache.ignite.internal.sql.engine.framework.TestNode; +import org.apache.ignite.internal.sql.engine.prepare.QueryPlan; +import org.apache.ignite.internal.sql.engine.prepare.SelectCountPlan; +import org.apache.ignite.internal.testframework.SystemPropertiesExtension; +import org.apache.ignite.internal.testframework.WithSystemProperty; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.api.extension.ExtendWith; + +/** + * Tests to verify SELECT COUNT(*) optimized plans. + */ +@ExtendWith(SystemPropertiesExtension.class) +public class SelectCountPlannerTest extends AbstractPlannerTest { + + private static final String NODE_NAME = "N1"; + + private static final TestCluster CLUSTER = TestBuilders.cluster() + .nodes(NODE_NAME) + .build(); + + private final TestNode node = CLUSTER.node(NODE_NAME); + + @BeforeAll + static void start() { + CLUSTER.start(); + } + + @AfterAll + static void stop() throws Exception { + CLUSTER.stop(); + } + + @AfterEach + void clearCatalog() { + int version = CLUSTER.catalogManager().latestCatalogVersion(); + + List<CatalogCommand> commands = new ArrayList<>(); + for (CatalogTableDescriptor table : CLUSTER.catalogManager().tables(version)) { + commands.add( + DropTableCommand.builder() + .schemaName(SqlCommon.DEFAULT_SCHEMA_NAME) + .tableName(table.name()) + .build() + ); + } + + await(CLUSTER.catalogManager().execute(commands)); + } + + @Test + public void optimizeCountStar() { + node.initSchema("CREATE TABLE test (id INT PRIMARY KEY, val INT)"); + + QueryPlan plan = node.prepare("SELECT count(*) FROM test"); + + assertThat(plan, instanceOf(SelectCountPlan.class)); + assertExpressions((SelectCountPlan) plan, "$0"); Review Comment: what does it mean? -- 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]
