|
| 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.iceberg; |
| 19 | + |
| 20 | +import org.apache.doris.connector.iceberg.IcebergCatalogOps.CatalogBackedIcebergCatalogOps; |
| 21 | +import org.apache.doris.connector.spi.ConnectorColumn; |
| 22 | +import org.apache.doris.connector.spi.ConnectorType; |
| 23 | +import org.apache.doris.connector.spi.DorisConnectorException; |
| 24 | +import org.apache.doris.connector.spi.ddl.ConnectorColumnPath; |
| 25 | +import org.apache.doris.connector.spi.ddl.ConnectorColumnPosition; |
| 26 | + |
| 27 | +import org.apache.iceberg.PartitionSpec; |
| 28 | +import org.apache.iceberg.Schema; |
| 29 | +import org.apache.iceberg.inmemory.InMemoryCatalog; |
| 30 | +import org.apache.iceberg.types.Types; |
| 31 | +import org.junit.jupiter.api.AfterEach; |
| 32 | +import org.junit.jupiter.api.Assertions; |
| 33 | +import org.junit.jupiter.api.BeforeEach; |
| 34 | +import org.junit.jupiter.api.Test; |
| 35 | + |
| 36 | +import java.util.Arrays; |
| 37 | +import java.util.Collections; |
| 38 | +import java.util.HashMap; |
| 39 | +import java.util.Map; |
| 40 | + |
| 41 | +/** |
| 42 | + * {@code MODIFY COLUMN} of iceberg BINARY / FIXED columns and nested fields, driven through |
| 43 | + * {@link IcebergConnectorMetadata} into a REAL {@link InMemoryCatalog}. Doris reads BINARY as {@code VARBINARY} and |
| 44 | + * FIXED(n) as {@code VARBINARY(n)}, and iceberg has no type to build for VARBINARY, so a modify that restates how |
| 45 | + * such a column or field is read (to change its nullability, comment or position) must keep the current iceberg |
| 46 | + * type rather than fail with "Unsupported type for Iceberg: VARBINARY". A VARBINARY that restates nothing still |
| 47 | + * fails. |
| 48 | + */ |
| 49 | +public class IcebergConnectorMetadataBinaryModifyTest { |
| 50 | + |
| 51 | + private static final IcebergTableHandle HANDLE = new IcebergTableHandle("db1", "t1"); |
| 52 | + private static final String UNSUPPORTED = "Unsupported type for Iceberg: VARBINARY"; |
| 53 | + |
| 54 | + private InMemoryCatalog catalog; |
| 55 | + private CatalogBackedIcebergCatalogOps ops; |
| 56 | + private IcebergConnectorMetadata metadata; |
| 57 | + |
| 58 | + @BeforeEach |
| 59 | + public void setUp() { |
| 60 | + catalog = new InMemoryCatalog(); |
| 61 | + catalog.initialize("test", Collections.emptyMap()); |
| 62 | + ops = new CatalogBackedIcebergCatalogOps(catalog); |
| 63 | + ops.createDatabase("db1", Collections.emptyMap()); |
| 64 | + Schema schema = new Schema( |
| 65 | + Types.NestedField.optional(1, "id", Types.LongType.get()), |
| 66 | + Types.NestedField.optional(2, "payload", Types.BinaryType.get()), |
| 67 | + Types.NestedField.required(3, "digest", Types.FixedType.ofLength(16), "sha"), |
| 68 | + Types.NestedField.optional(4, "s", Types.StructType.of( |
| 69 | + Types.NestedField.optional(5, "a", Types.BinaryType.get(), "raw"), |
| 70 | + Types.NestedField.optional(6, "b", Types.IntegerType.get()))), |
| 71 | + Types.NestedField.optional(7, "arr", Types.ListType.ofRequired(8, Types.FixedType.ofLength(4))), |
| 72 | + Types.NestedField.optional(9, "m", Types.MapType.ofRequired(10, 11, |
| 73 | + Types.StringType.get(), Types.BinaryType.get()))); |
| 74 | + ops.createTable("db1", "t1", schema, PartitionSpec.unpartitioned(), null, |
| 75 | + IcebergSchemaBuilder.buildTableProperties(Collections.emptyMap())); |
| 76 | + Map<String, String> props = new HashMap<>(); |
| 77 | + props.put(IcebergCatalogProperties.ICEBERG_CATALOG_TYPE, IcebergCatalogProperties.TYPE_REST); |
| 78 | + metadata = new IcebergConnectorMetadata(ops, IcebergCatalogProperties.of(props), |
| 79 | + new RecordingConnectorContext()); |
| 80 | + } |
| 81 | + |
| 82 | + @AfterEach |
| 83 | + public void tearDown() throws Exception { |
| 84 | + catalog.close(); |
| 85 | + } |
| 86 | + |
| 87 | + private Schema reload() { |
| 88 | + return ops.loadTable("db1", "t1").schema(); |
| 89 | + } |
| 90 | + |
| 91 | + /** The neutral type fe-core sends for a Doris {@code VARBINARY}, which is how iceberg BINARY is read. */ |
| 92 | + private static ConnectorType varbinary() { |
| 93 | + return ConnectorType.of("VARBINARY"); |
| 94 | + } |
| 95 | + |
| 96 | + /** The neutral type fe-core sends for a Doris {@code VARBINARY(n)}, which is how iceberg FIXED(n) is read. */ |
| 97 | + private static ConnectorType varbinary(int length) { |
| 98 | + return ConnectorType.of("VARBINARY", length, 0); |
| 99 | + } |
| 100 | + |
| 101 | + /** A nullable column with its nullability stated and its comment omitted. */ |
| 102 | + private static ConnectorColumn column(String name, ConnectorType type) { |
| 103 | + return new ConnectorColumn(name, type, "", true, null, false).withSpecified(true, false); |
| 104 | + } |
| 105 | + |
| 106 | + private static ConnectorColumnPath path(String... parts) { |
| 107 | + return ConnectorColumnPath.of(Arrays.asList(parts)); |
| 108 | + } |
| 109 | + |
| 110 | + @Test |
| 111 | + public void testRestatingBinaryColumnKeepsBinaryAndMovesIt() { |
| 112 | + // MODIFY COLUMN payload VARBINARY NULL FIRST |
| 113 | + metadata.modifyColumn(null, HANDLE, column("payload", varbinary()), ConnectorColumnPosition.FIRST); |
| 114 | + |
| 115 | + Schema schema = reload(); |
| 116 | + Assertions.assertEquals(Types.BinaryType.get(), schema.findField("payload").type()); |
| 117 | + Assertions.assertEquals("payload", schema.columns().get(0).name()); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void testRestatingFixedColumnKeepsFixedAndWidensNullability() { |
| 122 | + // MODIFY COLUMN digest VARBINARY(16) NULL COMMENT 'digest' |
| 123 | + ConnectorColumn digest = new ConnectorColumn("digest", varbinary(16), "digest", true, null, false) |
| 124 | + .withSpecified(true, true); |
| 125 | + metadata.modifyColumn(null, HANDLE, digest, null); |
| 126 | + |
| 127 | + Types.NestedField field = reload().findField("digest"); |
| 128 | + Assertions.assertEquals(Types.FixedType.ofLength(16), field.type()); |
| 129 | + Assertions.assertTrue(field.isOptional()); |
| 130 | + Assertions.assertEquals("digest", field.doc()); |
| 131 | + } |
| 132 | + |
| 133 | + @Test |
| 134 | + public void testRestatingBinaryStructFieldChangesItsComment() { |
| 135 | + // MODIFY COLUMN s.a VARBINARY COMMENT 'bytes' |
| 136 | + ConnectorColumn a = new ConnectorColumn("a", varbinary(), "bytes", true, null, false) |
| 137 | + .withSpecified(false, true); |
| 138 | + metadata.modifyNestedColumn(null, HANDLE, path("s", "a"), a, null); |
| 139 | + |
| 140 | + Types.NestedField field = reload().findField("s.a"); |
| 141 | + Assertions.assertEquals(Types.BinaryType.get(), field.type()); |
| 142 | + Assertions.assertEquals("bytes", field.doc()); |
| 143 | + } |
| 144 | + |
| 145 | + @Test |
| 146 | + public void testRestatingBinaryElementAndValueWidensThem() { |
| 147 | + // MODIFY COLUMN arr.element VARBINARY(4) NULL; MODIFY COLUMN m.value VARBINARY NULL |
| 148 | + metadata.modifyNestedColumn(null, HANDLE, path("arr", "element"), column("element", varbinary(4)), null); |
| 149 | + metadata.modifyNestedColumn(null, HANDLE, path("m", "value"), column("value", varbinary()), null); |
| 150 | + |
| 151 | + Schema schema = reload(); |
| 152 | + Types.ListType arr = schema.findField("arr").type().asListType(); |
| 153 | + Assertions.assertEquals(Types.FixedType.ofLength(4), arr.elementType()); |
| 154 | + Assertions.assertTrue(arr.isElementOptional()); |
| 155 | + Types.MapType m = schema.findField("m").type().asMapType(); |
| 156 | + Assertions.assertEquals(Types.BinaryType.get(), m.valueType()); |
| 157 | + Assertions.assertTrue(m.isValueOptional()); |
| 158 | + } |
| 159 | + |
| 160 | + @Test |
| 161 | + public void testVarbinaryThatRestatesNothingStillFails() { |
| 162 | + Schema before = reload(); |
| 163 | + // A VARBINARY is not how the current column or field is read: a length other than FIXED's, a length for an |
| 164 | + // unbounded BINARY, a column or field of another type, or no such column. |
| 165 | + assertFails(() -> metadata.modifyColumn(null, HANDLE, column("payload", varbinary(16)), null)); |
| 166 | + assertFails(() -> metadata.modifyColumn(null, HANDLE, column("digest", varbinary(8)), null)); |
| 167 | + assertFails(() -> metadata.modifyColumn(null, HANDLE, column("id", varbinary()), null)); |
| 168 | + assertFails(() -> metadata.modifyColumn(null, HANDLE, column("s", varbinary()), null)); |
| 169 | + assertFails(() -> metadata.modifyColumn(null, HANDLE, column("missing", varbinary()), null)); |
| 170 | + assertFails(() -> metadata.modifyNestedColumn(null, HANDLE, path("s", "a"), column("a", varbinary(3)), null)); |
| 171 | + assertFails(() -> metadata.modifyNestedColumn(null, HANDLE, path("s", "b"), column("b", varbinary()), null)); |
| 172 | + Assertions.assertTrue(before.sameSchema(reload()), "a failed modify must not change the schema"); |
| 173 | + } |
| 174 | + |
| 175 | + private static void assertFails(org.junit.jupiter.api.function.Executable modify) { |
| 176 | + DorisConnectorException ex = Assertions.assertThrows(DorisConnectorException.class, modify); |
| 177 | + Assertions.assertEquals(UNSUPPORTED, ex.getMessage()); |
| 178 | + } |
| 179 | +} |
0 commit comments