|
| 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.connector.api; |
| 19 | + |
| 20 | +import org.junit.jupiter.api.Assertions; |
| 21 | +import org.junit.jupiter.api.Test; |
| 22 | + |
| 23 | +import java.util.Arrays; |
| 24 | +import java.util.Collections; |
| 25 | +import java.util.List; |
| 26 | + |
| 27 | +/** |
| 28 | + * Pins the complex-type shape contract enforced by the {@link ConnectorType} constructor. |
| 29 | + * |
| 30 | + * <p><b>WHY this matters:</b> a complex type is described by PARALLEL lists — one of child types, one |
| 31 | + * of STRUCT field names, plus optional per-child metadata. Nothing downstream can detect that they |
| 32 | + * disagree: fe-core's converter fills a missing STRUCT field name with {@code "col" + index} and turns |
| 33 | + * a childless ARRAY/MAP into {@code ARRAY<NULL>}/{@code MAP<NULL,NULL>}, all silently. The connector |
| 34 | + * that got it wrong compiles, the table loads, DESCRIBE prints something — and the user only finds out |
| 35 | + * when a query that names a real sub-field is rejected as "field name not found", by which point the |
| 36 | + * error site is nowhere near the type mapping that caused it. So the invariant is enforced where the |
| 37 | + * mistake is made: at construction. |
| 38 | + */ |
| 39 | +public class ConnectorTypeTest { |
| 40 | + |
| 41 | + private static final ConnectorType INT = ConnectorType.of("INT"); |
| 42 | + private static final ConnectorType STR = ConnectorType.of("STRING"); |
| 43 | + |
| 44 | + private static ConnectorType struct(List<ConnectorType> children, List<String> names) { |
| 45 | + return new ConnectorType("STRUCT", -1, -1, children, names); |
| 46 | + } |
| 47 | + |
| 48 | + // ---------- STRUCT: field names are mandatory and parallel ---------- |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testStructWithoutFieldNamesRejected() { |
| 52 | + // Exactly what the trino ROW mapping used to build. |
| 53 | + IllegalArgumentException e = Assertions.assertThrows(IllegalArgumentException.class, |
| 54 | + () -> new ConnectorType("STRUCT", -1, -1, Arrays.asList(INT, STR))); |
| 55 | + // Both counts belong in the message: "they disagree" without the numbers does not locate the bug. |
| 56 | + Assertions.assertTrue(e.getMessage().contains("(0)") && e.getMessage().contains("(2)"), e.getMessage()); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testStructWithTooFewFieldNamesRejected() { |
| 61 | + IllegalArgumentException e = Assertions.assertThrows(IllegalArgumentException.class, |
| 62 | + () -> struct(Arrays.asList(INT, STR), Collections.singletonList("a"))); |
| 63 | + Assertions.assertTrue(e.getMessage().contains("(1)") && e.getMessage().contains("(2)"), e.getMessage()); |
| 64 | + } |
| 65 | + |
| 66 | + @Test |
| 67 | + public void testStructWithTooManyFieldNamesRejected() { |
| 68 | + Assertions.assertThrows(IllegalArgumentException.class, |
| 69 | + () -> struct(Collections.singletonList(INT), Arrays.asList("a", "b"))); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void testStructWithNoChildrenRejected() { |
| 74 | + Assertions.assertThrows(IllegalArgumentException.class, |
| 75 | + () -> struct(Collections.emptyList(), Collections.emptyList())); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testStructWithNullFieldNameRejected() { |
| 80 | + // A null name is unresolvable in exactly the same way a missing one is. |
| 81 | + Assertions.assertThrows(IllegalArgumentException.class, |
| 82 | + () -> struct(Arrays.asList(INT, STR), Arrays.asList("a", null))); |
| 83 | + } |
| 84 | + |
| 85 | + @Test |
| 86 | + public void testLowercaseStructTagStillValidated() { |
| 87 | + // The tag is compared case-insensitively so a spelling variant cannot dodge the check. |
| 88 | + Assertions.assertThrows(IllegalArgumentException.class, |
| 89 | + () -> new ConnectorType("struct", -1, -1, Arrays.asList(INT, STR))); |
| 90 | + } |
| 91 | + |
| 92 | + // ---------- ARRAY / MAP: fixed arity ---------- |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testArrayArityEnforced() { |
| 96 | + Assertions.assertThrows(IllegalArgumentException.class, |
| 97 | + () -> new ConnectorType("ARRAY", -1, -1, Collections.emptyList())); |
| 98 | + Assertions.assertThrows(IllegalArgumentException.class, |
| 99 | + () -> new ConnectorType("ARRAY", -1, -1, Arrays.asList(INT, STR))); |
| 100 | + } |
| 101 | + |
| 102 | + @Test |
| 103 | + public void testMapArityEnforced() { |
| 104 | + Assertions.assertThrows(IllegalArgumentException.class, |
| 105 | + () -> new ConnectorType("MAP", -1, -1, Collections.singletonList(INT))); |
| 106 | + Assertions.assertThrows(IllegalArgumentException.class, |
| 107 | + () -> new ConnectorType("MAP", -1, -1, Arrays.asList(INT, STR, INT))); |
| 108 | + } |
| 109 | + |
| 110 | + // ---------- optional per-child lists ---------- |
| 111 | + |
| 112 | + @Test |
| 113 | + public void testOptionalListLongerThanChildrenRejected() { |
| 114 | + // An entry with no child to belong to cannot be interpreted at all. |
| 115 | + Assertions.assertThrows(IllegalArgumentException.class, |
| 116 | + () -> new ConnectorType("STRUCT", -1, -1, Collections.singletonList(INT), |
| 117 | + Collections.singletonList("a"), Arrays.asList(true, false), |
| 118 | + Collections.emptyList(), Collections.emptyList())); |
| 119 | + Assertions.assertThrows(IllegalArgumentException.class, |
| 120 | + () -> new ConnectorType("ARRAY", -1, -1, Collections.singletonList(INT), |
| 121 | + Collections.emptyList(), Collections.emptyList(), Collections.emptyList(), |
| 122 | + Arrays.asList(1, 2))); |
| 123 | + } |
| 124 | + |
| 125 | + @Test |
| 126 | + public void testOptionalListShorterThanChildrenStillLegal() { |
| 127 | + // Deliberately NOT rejected: these four are read index-tolerantly ("not carried for that index" |
| 128 | + // -> default), which is the documented behavior every legacy factory relies on. Tightening this |
| 129 | + // to exact-length would hard-fail a supported state. |
| 130 | + ConnectorType ct = new ConnectorType("STRUCT", -1, -1, Arrays.asList(INT, STR), |
| 131 | + Arrays.asList("a", "b"), Collections.singletonList(false), |
| 132 | + Collections.emptyList(), Collections.emptyList()); |
| 133 | + Assertions.assertFalse(ct.isChildNullable(0)); |
| 134 | + Assertions.assertTrue(ct.isChildNullable(1)); |
| 135 | + Assertions.assertNull(ct.getChildComment(0)); |
| 136 | + Assertions.assertEquals(-1, ct.getChildFieldId(0)); |
| 137 | + } |
| 138 | + |
| 139 | + // ---------- every legal construction path must stay legal ---------- |
| 140 | + |
| 141 | + @Test |
| 142 | + public void testFactoriesRemainValid() { |
| 143 | + Assertions.assertEquals(1, ConnectorType.arrayOf(INT).getChildren().size()); |
| 144 | + Assertions.assertEquals(1, ConnectorType.arrayOf(INT, false).getChildren().size()); |
| 145 | + Assertions.assertEquals(2, ConnectorType.mapOf(STR, INT).getChildren().size()); |
| 146 | + Assertions.assertEquals(2, ConnectorType.mapOf(STR, INT, false).getChildren().size()); |
| 147 | + |
| 148 | + List<String> names = Arrays.asList("a", "b"); |
| 149 | + List<ConnectorType> types = Arrays.asList(INT, STR); |
| 150 | + Assertions.assertEquals(names, ConnectorType.structOf(names, types).getFieldNames()); |
| 151 | + Assertions.assertEquals(names, ConnectorType.structOf(names, types, |
| 152 | + Arrays.asList(true, false), Arrays.asList("c1", "c2")).getFieldNames()); |
| 153 | + Assertions.assertEquals(names, ConnectorType.structOf(names, types, |
| 154 | + Arrays.asList(true, false), Arrays.asList("c1", "c2"), |
| 155 | + Arrays.asList(true, true)).getFieldNames()); |
| 156 | + } |
| 157 | + |
| 158 | + @Test |
| 159 | + public void testWithChildrenFieldIdsRemainsValid() { |
| 160 | + // The iceberg usage: one id per child on ARRAY (1), MAP (2) and STRUCT (N). |
| 161 | + Assertions.assertEquals(7, ConnectorType.arrayOf(INT) |
| 162 | + .withChildrenFieldIds(Collections.singletonList(7)).getChildFieldId(0)); |
| 163 | + Assertions.assertEquals(9, ConnectorType.mapOf(STR, INT) |
| 164 | + .withChildrenFieldIds(Arrays.asList(8, 9)).getChildFieldId(1)); |
| 165 | + Assertions.assertEquals(3, ConnectorType.structOf(Arrays.asList("a", "b"), Arrays.asList(INT, STR)) |
| 166 | + .withChildrenFieldIds(Arrays.asList(2, 3)).getChildFieldId(1)); |
| 167 | + } |
| 168 | + |
| 169 | + @Test |
| 170 | + public void testNonComplexTypesUnaffected() { |
| 171 | + // typeName is a free-form string with no vocabulary, so an unrecognized tag must not be |
| 172 | + // second-guessed - we cannot conclude "then it has no children". |
| 173 | + Assertions.assertEquals("JSONB", ConnectorType.of("JSONB").getTypeName()); |
| 174 | + Assertions.assertEquals(10, ConnectorType.of("DECIMALV3", 10, 2).getPrecision()); |
| 175 | + Assertions.assertEquals("SOMETHING", |
| 176 | + new ConnectorType("SOMETHING", -1, -1, Arrays.asList(INT, STR)).getTypeName()); |
| 177 | + } |
| 178 | +} |
0 commit comments