AMashenkov commented on code in PR #1721: URL: https://github.com/apache/ignite-3/pull/1721#discussion_r1121720617
########## modules/sql-engine/src/test/java/org/apache/ignite/internal/sql/engine/planner/IndexSearchBoundsPlannerTest.java: ########## @@ -0,0 +1,510 @@ +/* + * 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 java.util.ArrayList; +import java.util.List; +import java.util.Objects; +import java.util.UUID; +import java.util.function.Predicate; +import java.util.stream.IntStream; +import org.apache.calcite.rel.RelCollations; +import org.apache.calcite.rel.type.RelDataTypeFactory; +import org.apache.calcite.rex.RexLiteral; +import org.apache.calcite.rex.RexNode; +import org.apache.calcite.rex.RexUtil; +import org.apache.calcite.sql.type.SqlTypeName; +import org.apache.ignite.internal.index.ColumnCollation; +import org.apache.ignite.internal.sql.engine.metadata.ColocationGroup; +import org.apache.ignite.internal.sql.engine.prepare.MappingQueryContext; +import org.apache.ignite.internal.sql.engine.prepare.bounds.ExactBounds; +import org.apache.ignite.internal.sql.engine.prepare.bounds.MultiBounds; +import org.apache.ignite.internal.sql.engine.prepare.bounds.RangeBounds; +import org.apache.ignite.internal.sql.engine.prepare.bounds.SearchBounds; +import org.apache.ignite.internal.sql.engine.rel.IgniteIndexScan; +import org.apache.ignite.internal.sql.engine.rel.IgniteUnionAll; +import org.apache.ignite.internal.sql.engine.schema.IgniteSchema; +import org.apache.ignite.internal.sql.engine.trait.IgniteDistribution; +import org.apache.ignite.internal.sql.engine.trait.IgniteDistributions; +import org.apache.ignite.internal.sql.engine.trait.TraitUtils; +import org.apache.ignite.internal.sql.engine.type.IgniteTypeFactory; +import org.apache.ignite.internal.sql.engine.type.IgniteTypeSystem; +import org.apache.ignite.internal.sql.engine.util.RexUtils; +import org.jetbrains.annotations.Nullable; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +/** + * Index bounds check tests. + */ +public class IndexSearchBoundsPlannerTest extends AbstractPlannerTest { + private static List<String> NODES = new ArrayList<>(4); + + private IgniteSchema publicSchema; + + private TestTable tbl; + + @BeforeAll + public static void init() { + IntStream.rangeClosed(0, 3).forEach(i -> NODES.add(UUID.randomUUID().toString())); + } + + @BeforeEach + public void beforeEach() { + IgniteTypeFactory typeFactory = new IgniteTypeFactory(IgniteTypeSystem.INSTANCE); + + tbl = new TestTable( + new RelDataTypeFactory.Builder(typeFactory) + .add("C1", typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER), true)) + .add("C2", typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.VARCHAR), true)) + .add("C3", typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER), true)) + .add("C4", typeFactory.createTypeWithNullability(typeFactory.createSqlType(SqlTypeName.INTEGER), true)) + .build(), "TEST") { + @Override + public ColocationGroup colocationGroup(MappingQueryContext ctx) { + return ColocationGroup.forNodes(select(NODES, 0)); + } + + @Override + public IgniteDistribution distribution() { + return IgniteDistributions.single(); + } + }; + + tbl.addIndex("C1C2C3", 0, 1, 2); + + publicSchema = new IgniteSchema("PUBLIC"); + + publicSchema.addTable(tbl); + } + + /** Simple case on one field, without multi tuple SEARCH/SARG. */ + @Test + public void testBoundsOneFieldSingleTuple() throws Exception { + assertBounds("SELECT * FROM TEST WHERE C1 = 1", exact(1)); + + assertBounds("SELECT * FROM TEST WHERE C1 = 1 AND C3 = 1", exact(1), empty(), empty()); + + assertBounds("SELECT * FROM TEST WHERE C1 > 1 AND C1 <= 3", + range(1, 3, false, true)); + + assertBounds("SELECT * FROM TEST WHERE C1 < 3 AND C1 IS NOT NULL", + range(null, 3, true, false)); + + // Redundant "IS NOT NULL condition". + assertBounds("SELECT * FROM TEST WHERE C1 > 3 AND C1 IS NOT NULL", + range(3, "$NULL_BOUND()", false, false)); + + // C4 field not in collation. + assertBounds("SELECT * FROM TEST WHERE C1 > 1 AND C1 <= 3 AND C4 = 1", + range(1, 3, false, true), + empty(), + empty() + ); + + // Cannot proceed to C3 without C2. + assertBounds("SELECT * FROM TEST WHERE C1 = 1 AND C3 = 1", + exact(1), + empty(), + empty() + ); + + assertBounds("SELECT * FROM TEST WHERE C1 > 1 AND C1 <= 3 AND C3 = 1", + range(1, 3, false, true), + empty(), + empty() + ); + } + + /** Simple SEARCH/SARG. */ + @Test + public void testBoundsOneFieldSearch() throws Exception { + assertBounds("SELECT * FROM TEST WHERE C1 IN (1, 2, 3)", + multi(exact(1), exact(2), exact(3))); + + assertBounds("SELECT * FROM TEST WHERE C1 IN (1, 2, 3) AND C1 IS NOT NULL", Review Comment: Will be IN condition merged with inequality? C1 IN (1, 2, 3) AND C1 > 1 C1 IN (1, 2, 3) OR C1 > 1 -- 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]
