|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.ignite.internal.sql.engine.planner.hints; |
| 19 | + |
| 20 | +import java.util.function.UnaryOperator; |
| 21 | +import org.apache.ignite.internal.sql.engine.framework.TestBuilders.TableBuilder; |
| 22 | +import org.apache.ignite.internal.sql.engine.planner.AbstractPlannerTest; |
| 23 | +import org.apache.ignite.internal.sql.engine.rel.AbstractIgniteJoin; |
| 24 | +import org.apache.ignite.internal.sql.engine.rel.IgniteTableScan; |
| 25 | +import org.apache.ignite.internal.sql.engine.schema.IgniteSchema; |
| 26 | +import org.apache.ignite.internal.sql.engine.trait.IgniteDistributions; |
| 27 | +import org.apache.ignite.internal.type.NativeTypes; |
| 28 | +import org.junit.jupiter.api.BeforeAll; |
| 29 | +import org.junit.jupiter.api.Test; |
| 30 | + |
| 31 | +/** |
| 32 | + * Planner tests for index/no_index hints propagation. |
| 33 | + */ |
| 34 | +public class HintPropagationPlannerTest extends AbstractPlannerTest { |
| 35 | + private static IgniteSchema SCHEMA; |
| 36 | + |
| 37 | + private static final String TBL1 = "TBL1"; |
| 38 | + |
| 39 | + private static final String TBL2 = "TBL2"; |
| 40 | + |
| 41 | + @BeforeAll |
| 42 | + public static void setup() { |
| 43 | + SCHEMA = createSchemaFrom( |
| 44 | + createSimpleTable(TBL1, 100) |
| 45 | + .andThen(addHashIndex("ID")) |
| 46 | + .andThen(addSortIndex("VAL1")) |
| 47 | + .andThen(addSortIndex("VAL2", "VAL3")) |
| 48 | + .andThen(addSortIndex("VAL3")), |
| 49 | + createSimpleTable(TBL2, 100_000) |
| 50 | + .andThen(addHashIndex("ID")) |
| 51 | + .andThen(addSortIndex("VAL1")) |
| 52 | + .andThen(addSortIndex("VAL2")) |
| 53 | + .andThen(addSortIndex("VAL3")) |
| 54 | + ); |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + public void usingDifferentIndexesForSameTable() throws Exception { |
| 59 | + var sql = "SELECT * FROM" |
| 60 | + + " (SELECT t1.val1, t1.val2, t1.val3 FROM tbl1 /*+ NO_INDEX */ as t1" |
| 61 | + + " LEFT JOIN tbl2 /*+ FORCE_INDEX(idx_val3) */ as t2 ON (t1.val2 = t2.val2 AND t2.val2 > 'x')) as t" |
| 62 | + + " LEFT JOIN tbl2 /*+ FORCE_INDEX(idx_val2) */ as t3 ON (t.val3 = t3.val3 AND t3.val3 < 'a')"; |
| 63 | + |
| 64 | + assertPlan(sql, SCHEMA, isInstanceOf(AbstractIgniteJoin.class) |
| 65 | + .and(input(0, nodeOrAnyChild(isInstanceOf(AbstractIgniteJoin.class) |
| 66 | + .and(input(0, nodeOrAnyChild(isInstanceOf(IgniteTableScan.class)))) |
| 67 | + .and(input(1, nodeOrAnyChild(isIndexScan(TBL2, "IDX_VAL3") |
| 68 | + .and(scan -> scan.searchBounds() == null) |
| 69 | + .and(scan -> ">($t0, _UTF-8'x')".equals(scan.condition().toString())) |
| 70 | + ))) |
| 71 | + ))) |
| 72 | + .and(input(1, nodeOrAnyChild(isIndexScan(TBL2, "IDX_VAL2") |
| 73 | + .and(scan -> scan.searchBounds() == null) |
| 74 | + .and(scan -> "<($t3, _UTF-8'a')".equals(scan.condition().toString())) |
| 75 | + ))) |
| 76 | + ); |
| 77 | + |
| 78 | + sql = "SELECT * FROM" |
| 79 | + + " (SELECT /*+ NO_INDEX */ t1.val1, t1.val2, t1.val3 FROM tbl1 as t1 LEFT JOIN tbl2 as t2 ON (t1.val2 = t2.val2)) as t" |
| 80 | + + " LEFT JOIN tbl2 /*+ FORCE_INDEX(idx_val2) */ as t3 ON (t.val3 = t3.val3)"; |
| 81 | + |
| 82 | + assertPlan(sql, SCHEMA, isInstanceOf(AbstractIgniteJoin.class) |
| 83 | + .and(input(0, nodeOrAnyChild(isInstanceOf(AbstractIgniteJoin.class) |
| 84 | + .and(input(0, nodeOrAnyChild(isInstanceOf(IgniteTableScan.class)))) |
| 85 | + .and(input(1, nodeOrAnyChild(isInstanceOf(IgniteTableScan.class)))) |
| 86 | + ))) |
| 87 | + .and(input(1, nodeOrAnyChild(isIndexScan(TBL2, "IDX_VAL2")))) |
| 88 | + ); |
| 89 | + } |
| 90 | + |
| 91 | + @Test |
| 92 | + public void testHintOverriding() throws Exception { |
| 93 | + var sql = "SELECT /*+ NO_INDEX */ * FROM" |
| 94 | + + " (SELECT t1.val1, t1.val2, t1.val3 FROM tbl1 as t1 LEFT JOIN tbl2 as t2 ON (t1.val2 = t2.val2)) as t" |
| 95 | + + " LEFT JOIN tbl2 /*+ FORCE_INDEX(idx_val2_val3) */ as t3 ON (t.val3 = t3.val3)"; |
| 96 | + |
| 97 | + assertPlan(sql, SCHEMA, isInstanceOf(AbstractIgniteJoin.class) |
| 98 | + .and(input(0, nodeOrAnyChild(isInstanceOf(AbstractIgniteJoin.class) |
| 99 | + .and(input(0, nodeOrAnyChild(isInstanceOf(IgniteTableScan.class)))) |
| 100 | + .and(input(1, nodeOrAnyChild(isInstanceOf(IgniteTableScan.class)))) |
| 101 | + ))) |
| 102 | + .and(input(1, nodeOrAnyChild(isIndexScan(TBL2, "idx_val3")))) |
| 103 | + ); |
| 104 | + |
| 105 | + sql = "SELECT /*+ FORCE_INDEX(idx_val3) */ * FROM" |
| 106 | + + " (SELECT t1.val1, t1.val2, t1.val3 FROM tbl1 /*+ NO_INDEX */ as t1 " |
| 107 | + + " LEFT JOIN tbl2 as t2 ON (t1.val2 = t2.val2)) as t" |
| 108 | + + " LEFT JOIN tbl2 /*+ FORCE_INDEX(idx_val2) */as t3 ON (t.val3 = t3.val3)"; |
| 109 | + |
| 110 | + assertPlan(sql, SCHEMA, isInstanceOf(AbstractIgniteJoin.class) |
| 111 | + .and(input(0, nodeOrAnyChild(isInstanceOf(AbstractIgniteJoin.class) |
| 112 | + .and(input(0, nodeOrAnyChild(isInstanceOf(IgniteTableScan.class)))) |
| 113 | + .and(input(1, nodeOrAnyChild(isIndexScan(TBL2, "IDX_VAL3")))) |
| 114 | + ))) |
| 115 | + .and(input(1, nodeOrAnyChild(isIndexScan(TBL2, "IDX_VAL2")))) |
| 116 | + ); |
| 117 | + } |
| 118 | + |
| 119 | + private static UnaryOperator<TableBuilder> createSimpleTable(String name, int sz) { |
| 120 | + return t -> t.name(name) |
| 121 | + .size(sz) |
| 122 | + .distribution(IgniteDistributions.single()) |
| 123 | + .addKeyColumn("ID", NativeTypes.INT32) |
| 124 | + .addColumn("VAL1", NativeTypes.INT32) |
| 125 | + .addColumn("VAL2", NativeTypes.STRING) |
| 126 | + .addColumn("VAL3", NativeTypes.STRING); |
| 127 | + } |
| 128 | +} |
0 commit comments