|
| 1 | +// Licensed to the Apache Software Foundation (ASF) under one |
| 2 | +// or more contributor license agreements. See the NOTICE file |
| 3 | +// distributed with this work for additional information |
| 4 | +// regarding copyright ownership. The ASF licenses this file |
| 5 | +// to you under the Apache License, Version 2.0 (the |
| 6 | +// "License"); you may not use this file except in compliance |
| 7 | +// with 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, |
| 12 | +// software distributed under the License is distributed on an |
| 13 | +// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 14 | +// KIND, either express or implied. See the License for the |
| 15 | +// specific language governing permissions and limitations |
| 16 | +// under the License. |
| 17 | + |
| 18 | +package org.apache.doris.nereids.trees.plans.physical; |
| 19 | + |
| 20 | +import org.apache.doris.catalog.Column; |
| 21 | +import org.apache.doris.catalog.PrimitiveType; |
| 22 | +import org.apache.doris.datasource.paimon.PaimonExternalDatabase; |
| 23 | +import org.apache.doris.datasource.paimon.PaimonExternalTable; |
| 24 | +import org.apache.doris.nereids.properties.DataTrait; |
| 25 | +import org.apache.doris.nereids.properties.DistributionSpecHiveTableSinkHashPartitioned; |
| 26 | +import org.apache.doris.nereids.properties.LogicalProperties; |
| 27 | +import org.apache.doris.nereids.properties.PhysicalProperties; |
| 28 | +import org.apache.doris.nereids.trees.expressions.NamedExpression; |
| 29 | +import org.apache.doris.nereids.trees.expressions.Slot; |
| 30 | +import org.apache.doris.nereids.trees.expressions.SlotReference; |
| 31 | +import org.apache.doris.nereids.trees.plans.Plan; |
| 32 | +import org.apache.doris.nereids.types.IntegerType; |
| 33 | +import org.apache.doris.nereids.types.StringType; |
| 34 | + |
| 35 | +import org.apache.paimon.schema.TableSchema; |
| 36 | +import org.apache.paimon.table.BucketMode; |
| 37 | +import org.apache.paimon.table.FileStoreTable; |
| 38 | +import org.junit.Assert; |
| 39 | +import org.junit.Test; |
| 40 | +import org.mockito.Mockito; |
| 41 | + |
| 42 | +import java.util.Arrays; |
| 43 | +import java.util.BitSet; |
| 44 | +import java.util.Collections; |
| 45 | +import java.util.List; |
| 46 | +import java.util.Optional; |
| 47 | +import java.util.stream.Collectors; |
| 48 | + |
| 49 | +public class PhysicalPaimonTableSinkTest { |
| 50 | + |
| 51 | + @Test |
| 52 | + public void testBucketUnawareWithoutDistributionColumnsUsesRandomPartitioned() { |
| 53 | + SlotReference valueSlot = new SlotReference("v", StringType.INSTANCE); |
| 54 | + PhysicalPaimonTableSink<Plan> sink = newSink( |
| 55 | + BucketMode.BUCKET_UNAWARE, |
| 56 | + Collections.emptyList(), |
| 57 | + Collections.emptyList(), |
| 58 | + Collections.singletonList(new Column("v", PrimitiveType.STRING)), |
| 59 | + Collections.singletonList(valueSlot)); |
| 60 | + |
| 61 | + Assert.assertSame(PhysicalProperties.SINK_RANDOM_PARTITIONED, sink.getRequirePhysicalProperties()); |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + public void testFixedBucketUsesPartitionAndBucketKeyExprIds() { |
| 66 | + SlotReference partitionSlot = new SlotReference("pt", IntegerType.INSTANCE); |
| 67 | + SlotReference bucketSlot = new SlotReference("bucket_key", IntegerType.INSTANCE); |
| 68 | + SlotReference valueSlot = new SlotReference("v", StringType.INSTANCE); |
| 69 | + PhysicalPaimonTableSink<Plan> sink = newSink( |
| 70 | + BucketMode.HASH_FIXED, |
| 71 | + Collections.singletonList("bucket_key"), |
| 72 | + Collections.singletonList(new Column("pt", PrimitiveType.INT)), |
| 73 | + Arrays.asList( |
| 74 | + new Column("pt", PrimitiveType.INT), |
| 75 | + new Column("bucket_key", PrimitiveType.INT), |
| 76 | + new Column("v", PrimitiveType.STRING)), |
| 77 | + Arrays.asList(partitionSlot, bucketSlot, valueSlot)); |
| 78 | + |
| 79 | + PhysicalProperties properties = sink.getRequirePhysicalProperties(); |
| 80 | + |
| 81 | + Assert.assertTrue(properties.getDistributionSpec() |
| 82 | + instanceof DistributionSpecHiveTableSinkHashPartitioned); |
| 83 | + DistributionSpecHiveTableSinkHashPartitioned distribution = |
| 84 | + (DistributionSpecHiveTableSinkHashPartitioned) properties.getDistributionSpec(); |
| 85 | + Assert.assertEquals(Arrays.asList(partitionSlot.getExprId(), bucketSlot.getExprId()), |
| 86 | + distribution.getOutputColExprIds()); |
| 87 | + } |
| 88 | + |
| 89 | + @Test |
| 90 | + public void testFixedBucketRequiresBucketKeyInSinkOutput() { |
| 91 | + SlotReference partitionSlot = new SlotReference("pt", IntegerType.INSTANCE); |
| 92 | + SlotReference valueSlot = new SlotReference("v", StringType.INSTANCE); |
| 93 | + PhysicalPaimonTableSink<Plan> sink = newSink( |
| 94 | + BucketMode.HASH_FIXED, |
| 95 | + Collections.singletonList("bucket_key"), |
| 96 | + Collections.singletonList(new Column("pt", PrimitiveType.INT)), |
| 97 | + Arrays.asList(new Column("pt", PrimitiveType.INT), new Column("v", PrimitiveType.STRING)), |
| 98 | + Arrays.asList(partitionSlot, valueSlot)); |
| 99 | + |
| 100 | + UnsupportedOperationException exception = Assert.assertThrows(UnsupportedOperationException.class, |
| 101 | + sink::getRequirePhysicalProperties); |
| 102 | + |
| 103 | + Assert.assertTrue(exception.getMessage().contains("requires distribution column")); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void testUnsupportedBucketModeFailsFast() { |
| 108 | + SlotReference valueSlot = new SlotReference("v", StringType.INSTANCE); |
| 109 | + PhysicalPaimonTableSink<Plan> sink = newSink( |
| 110 | + BucketMode.HASH_DYNAMIC, |
| 111 | + Collections.emptyList(), |
| 112 | + Collections.emptyList(), |
| 113 | + Collections.singletonList(new Column("v", PrimitiveType.STRING)), |
| 114 | + Collections.singletonList(valueSlot)); |
| 115 | + |
| 116 | + UnsupportedOperationException exception = Assert.assertThrows(UnsupportedOperationException.class, |
| 117 | + sink::getRequirePhysicalProperties); |
| 118 | + |
| 119 | + Assert.assertTrue(exception.getMessage().contains("Unsupported Paimon bucket mode")); |
| 120 | + } |
| 121 | + |
| 122 | + private static PhysicalPaimonTableSink<Plan> newSink(BucketMode bucketMode, List<String> bucketKeys, |
| 123 | + List<Column> partitionColumns, List<Column> columns, List<SlotReference> outputSlots) { |
| 124 | + PaimonExternalTable table = Mockito.mock(PaimonExternalTable.class); |
| 125 | + FileStoreTable fileStoreTable = Mockito.mock(FileStoreTable.class); |
| 126 | + TableSchema schema = Mockito.mock(TableSchema.class); |
| 127 | + Mockito.when(table.getPaimonTable(Mockito.any(Optional.class))).thenReturn(fileStoreTable); |
| 128 | + Mockito.when(table.getPartitionColumns(Mockito.any(Optional.class))).thenReturn(partitionColumns); |
| 129 | + Mockito.when(fileStoreTable.bucketMode()).thenReturn(bucketMode); |
| 130 | + Mockito.when(fileStoreTable.schema()).thenReturn(schema); |
| 131 | + Mockito.when(schema.bucketKeys()).thenReturn(bucketKeys); |
| 132 | + |
| 133 | + Plan child = Mockito.mock(Plan.class); |
| 134 | + Mockito.when(child.getAllChildrenTypes()).thenReturn(new BitSet()); |
| 135 | + Mockito.when(child.depth()).thenReturn(1); |
| 136 | + Mockito.when(child.getOutput()).thenReturn(outputSlots.stream() |
| 137 | + .map(Slot.class::cast) |
| 138 | + .collect(Collectors.toList())); |
| 139 | + List<Slot> output = outputSlots.stream() |
| 140 | + .map(Slot.class::cast) |
| 141 | + .collect(Collectors.toList()); |
| 142 | + List<NamedExpression> outputExprs = outputSlots.stream() |
| 143 | + .map(NamedExpression.class::cast) |
| 144 | + .collect(Collectors.toList()); |
| 145 | + |
| 146 | + return new PhysicalPaimonTableSink<>( |
| 147 | + Mockito.mock(PaimonExternalDatabase.class), |
| 148 | + table, |
| 149 | + columns, |
| 150 | + outputExprs, |
| 151 | + Optional.empty(), |
| 152 | + new LogicalProperties(() -> output, () -> DataTrait.EMPTY_TRAIT), |
| 153 | + child); |
| 154 | + } |
| 155 | +} |
0 commit comments