|
| 1 | +/* |
| 2 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 3 | + * you may not use this file except in compliance with the License. |
| 4 | + * You may obtain a copy of the License at |
| 5 | + * |
| 6 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 7 | + * |
| 8 | + * Unless required by applicable law or agreed to in writing, software |
| 9 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 10 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 11 | + * See the License for the specific language governing permissions and |
| 12 | + * limitations under the License. |
| 13 | + */ |
| 14 | +package io.trino.sql.planner.sanity; |
| 15 | + |
| 16 | +import com.google.common.collect.ImmutableList; |
| 17 | +import com.google.common.collect.ImmutableMap; |
| 18 | +import io.trino.connector.CatalogHandle; |
| 19 | +import io.trino.execution.warnings.WarningCollector; |
| 20 | +import io.trino.metadata.TableExecuteHandle; |
| 21 | +import io.trino.metadata.TableHandle; |
| 22 | +import io.trino.plugin.tpch.TpchTableHandle; |
| 23 | +import io.trino.plugin.tpch.TpchTransactionHandle; |
| 24 | +import io.trino.spi.connector.ColumnHandle; |
| 25 | +import io.trino.spi.connector.SchemaTableName; |
| 26 | +import io.trino.spi.connector.WriterScalingOptions; |
| 27 | +import io.trino.spi.predicate.TupleDomain; |
| 28 | +import io.trino.sql.ir.Constant; |
| 29 | +import io.trino.sql.ir.Expression; |
| 30 | +import io.trino.sql.planner.OrderingScheme; |
| 31 | +import io.trino.sql.planner.Partitioning; |
| 32 | +import io.trino.sql.planner.PartitioningScheme; |
| 33 | +import io.trino.sql.planner.PlanNodeIdAllocator; |
| 34 | +import io.trino.sql.planner.Symbol; |
| 35 | +import io.trino.sql.planner.assertions.BasePlanTest; |
| 36 | +import io.trino.sql.planner.plan.Assignments; |
| 37 | +import io.trino.sql.planner.plan.ExchangeNode; |
| 38 | +import io.trino.sql.planner.plan.ExplainAnalyzeNode; |
| 39 | +import io.trino.sql.planner.plan.FilterNode; |
| 40 | +import io.trino.sql.planner.plan.OutputNode; |
| 41 | +import io.trino.sql.planner.plan.PlanNode; |
| 42 | +import io.trino.sql.planner.plan.ProjectNode; |
| 43 | +import io.trino.sql.planner.plan.StatisticAggregations; |
| 44 | +import io.trino.sql.planner.plan.StatisticAggregationsDescriptor; |
| 45 | +import io.trino.sql.planner.plan.TableExecuteNode; |
| 46 | +import io.trino.sql.planner.plan.TableFinishNode; |
| 47 | +import io.trino.sql.planner.plan.TableScanNode; |
| 48 | +import io.trino.sql.planner.plan.TableWriterNode; |
| 49 | +import io.trino.testing.TestingTableExecuteHandle; |
| 50 | +import io.trino.testing.TestingTransactionHandle; |
| 51 | +import org.junit.jupiter.api.BeforeAll; |
| 52 | +import org.junit.jupiter.api.Test; |
| 53 | + |
| 54 | +import java.util.List; |
| 55 | +import java.util.Map; |
| 56 | +import java.util.Optional; |
| 57 | + |
| 58 | +import static io.trino.spi.type.BigintType.BIGINT; |
| 59 | +import static io.trino.sql.planner.SystemPartitioningHandle.SINGLE_DISTRIBUTION; |
| 60 | +import static io.trino.sql.planner.TestingPlannerContext.PLANNER_CONTEXT; |
| 61 | +import static io.trino.sql.planner.plan.ExchangeNode.Scope.LOCAL; |
| 62 | +import static io.trino.sql.planner.plan.ExchangeNode.Type.REPARTITION; |
| 63 | +import static io.trino.testing.TestingHandles.TEST_CATALOG_HANDLE; |
| 64 | +import static org.assertj.core.api.Assertions.assertThatThrownBy; |
| 65 | + |
| 66 | +final class TestTableExecuteStructureValidator |
| 67 | + extends BasePlanTest |
| 68 | +{ |
| 69 | + private final PlanNodeIdAllocator idAllocator = new PlanNodeIdAllocator(); |
| 70 | + private final TableExecuteStructureValidator validator = new TableExecuteStructureValidator(); |
| 71 | + private final Expression predicate = new Constant(BIGINT, 1L); |
| 72 | + private final Symbol symbol = new Symbol(BIGINT, "bigint"); |
| 73 | + private final List<Symbol> symbols = ImmutableList.of(symbol); |
| 74 | + private final List<String> columnNames = ImmutableList.of("bigint"); |
| 75 | + private final Assignments assignments = Assignments.of(symbol, predicate); |
| 76 | + private final Map<Symbol, ColumnHandle> assignmentsMap = ImmutableMap.of(symbol, new ColumnHandle() {}); |
| 77 | + private final Optional<OrderingScheme> orderingSchema = Optional.empty(); |
| 78 | + private final Optional<PartitioningScheme> partitioningSchema = Optional.empty(); |
| 79 | + private final Optional<StatisticAggregations> statisticAggregations = Optional.empty(); |
| 80 | + private final Optional<StatisticAggregationsDescriptor<Symbol>> statisticsAggregationDescriptor = Optional.empty(); |
| 81 | + |
| 82 | + private TableScanNode tableScanNode; |
| 83 | + private TableWriterNode.TableExecuteTarget tableExecuteTarget; |
| 84 | + private PartitioningScheme partitioningScheme; |
| 85 | + |
| 86 | + @BeforeAll |
| 87 | + void setup() |
| 88 | + { |
| 89 | + CatalogHandle catalogHandle = getCurrentCatalogHandle(); |
| 90 | + |
| 91 | + TableHandle nationTableHandle = new TableHandle( |
| 92 | + catalogHandle, |
| 93 | + new TpchTableHandle("sf1", "nation", 1.0), |
| 94 | + TpchTransactionHandle.INSTANCE); |
| 95 | + |
| 96 | + tableScanNode = new TableScanNode( |
| 97 | + idAllocator.getNextId(), |
| 98 | + nationTableHandle, |
| 99 | + symbols, |
| 100 | + assignmentsMap, |
| 101 | + TupleDomain.all(), |
| 102 | + Optional.empty(), |
| 103 | + false, |
| 104 | + Optional.empty()); |
| 105 | + |
| 106 | + tableExecuteTarget = new TableWriterNode.TableExecuteTarget( |
| 107 | + new TableExecuteHandle( |
| 108 | + TEST_CATALOG_HANDLE, |
| 109 | + TestingTransactionHandle.create(), |
| 110 | + new TestingTableExecuteHandle()), |
| 111 | + Optional.empty(), |
| 112 | + new SchemaTableName("schemaName", "tableName"), |
| 113 | + WriterScalingOptions.DISABLED); |
| 114 | + |
| 115 | + partitioningScheme = new PartitioningScheme(Partitioning.create(SINGLE_DISTRIBUTION, symbols), symbols); |
| 116 | + } |
| 117 | + |
| 118 | + @Test |
| 119 | + void testValidateSuccessfulWithExecuteNode() |
| 120 | + { |
| 121 | + PlanNode root = new OutputNode(idAllocator.getNextId(), |
| 122 | + new ExplainAnalyzeNode(idAllocator.getNextId(), |
| 123 | + new TableExecuteNode(idAllocator.getNextId(), |
| 124 | + new ExchangeNode(idAllocator.getNextId(), |
| 125 | + REPARTITION, |
| 126 | + LOCAL, |
| 127 | + partitioningScheme, |
| 128 | + ImmutableList.of(new TableFinishNode(idAllocator.getNextId(), |
| 129 | + new ExchangeNode(idAllocator.getNextId(), |
| 130 | + REPARTITION, |
| 131 | + LOCAL, |
| 132 | + partitioningScheme, |
| 133 | + ImmutableList.of( |
| 134 | + new ProjectNode(idAllocator.getNextId(), |
| 135 | + tableScanNode, |
| 136 | + assignments)), |
| 137 | + ImmutableList.of(symbols), |
| 138 | + orderingSchema), |
| 139 | + tableExecuteTarget, |
| 140 | + symbol, |
| 141 | + statisticAggregations, |
| 142 | + statisticsAggregationDescriptor)), |
| 143 | + ImmutableList.of(symbols), |
| 144 | + orderingSchema), |
| 145 | + tableExecuteTarget, |
| 146 | + symbol, |
| 147 | + symbol, |
| 148 | + symbols, |
| 149 | + columnNames, |
| 150 | + partitioningSchema), |
| 151 | + symbol, |
| 152 | + symbols, |
| 153 | + false), |
| 154 | + columnNames, |
| 155 | + symbols); |
| 156 | + validator.validate(root, null, PLANNER_CONTEXT, WarningCollector.NOOP); |
| 157 | + } |
| 158 | + |
| 159 | + @Test |
| 160 | + void testValidateSuccessfulWithoutExecuteNode() |
| 161 | + { |
| 162 | + PlanNode root = new OutputNode(idAllocator.getNextId(), |
| 163 | + new ExplainAnalyzeNode(idAllocator.getNextId(), |
| 164 | + new ExchangeNode(idAllocator.getNextId(), |
| 165 | + REPARTITION, |
| 166 | + LOCAL, |
| 167 | + partitioningScheme, |
| 168 | + ImmutableList.of(new TableFinishNode(idAllocator.getNextId(), |
| 169 | + new ExchangeNode(idAllocator.getNextId(), |
| 170 | + REPARTITION, |
| 171 | + LOCAL, |
| 172 | + partitioningScheme, |
| 173 | + ImmutableList.of( |
| 174 | + new ProjectNode(idAllocator.getNextId(), |
| 175 | + tableScanNode, |
| 176 | + assignments)), |
| 177 | + ImmutableList.of(symbols), |
| 178 | + orderingSchema), |
| 179 | + tableExecuteTarget, |
| 180 | + symbol, |
| 181 | + statisticAggregations, |
| 182 | + statisticsAggregationDescriptor)), |
| 183 | + ImmutableList.of(symbols), |
| 184 | + orderingSchema), |
| 185 | + symbol, |
| 186 | + symbols, |
| 187 | + false), |
| 188 | + columnNames, |
| 189 | + symbols); |
| 190 | + validator.validate(root, null, PLANNER_CONTEXT, WarningCollector.NOOP); |
| 191 | + } |
| 192 | + |
| 193 | + @Test |
| 194 | + void testValidateFailed() |
| 195 | + { |
| 196 | + PlanNode root = new OutputNode(idAllocator.getNextId(), |
| 197 | + new ExplainAnalyzeNode(idAllocator.getNextId(), |
| 198 | + new TableExecuteNode(idAllocator.getNextId(), |
| 199 | + new ExchangeNode(idAllocator.getNextId(), |
| 200 | + REPARTITION, |
| 201 | + LOCAL, |
| 202 | + partitioningScheme, |
| 203 | + ImmutableList.of(new FilterNode(idAllocator.getNextId(), |
| 204 | + new ExchangeNode(idAllocator.getNextId(), |
| 205 | + REPARTITION, |
| 206 | + LOCAL, |
| 207 | + partitioningScheme, |
| 208 | + ImmutableList.of(new TableFinishNode(idAllocator.getNextId(), |
| 209 | + new ExchangeNode(idAllocator.getNextId(), |
| 210 | + REPARTITION, |
| 211 | + LOCAL, |
| 212 | + partitioningScheme, |
| 213 | + ImmutableList.of( |
| 214 | + new ProjectNode(idAllocator.getNextId(), |
| 215 | + tableScanNode, |
| 216 | + assignments)), |
| 217 | + ImmutableList.of(symbols), |
| 218 | + orderingSchema), |
| 219 | + tableExecuteTarget, |
| 220 | + symbol, |
| 221 | + statisticAggregations, |
| 222 | + statisticsAggregationDescriptor)), |
| 223 | + ImmutableList.of(symbols), |
| 224 | + orderingSchema), |
| 225 | + predicate)), |
| 226 | + ImmutableList.of(symbols), |
| 227 | + orderingSchema), |
| 228 | + tableExecuteTarget, |
| 229 | + symbol, |
| 230 | + symbol, |
| 231 | + symbols, |
| 232 | + columnNames, |
| 233 | + partitioningSchema), |
| 234 | + symbol, |
| 235 | + symbols, |
| 236 | + false), |
| 237 | + columnNames, |
| 238 | + symbols); |
| 239 | + assertThatThrownBy(() -> validator.validate(root, null, PLANNER_CONTEXT, WarningCollector.NOOP)) |
| 240 | + .isInstanceOf(IllegalStateException.class) |
| 241 | + .hasMessage("Unexpected FilterNode found in plan; probably connector was not able to handle provided WHERE expression"); |
| 242 | + } |
| 243 | +} |
0 commit comments