|
| 1 | +/* |
| 2 | + * Copyright (2026) The Delta Lake Project Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.delta.spark.internal.v2.adapters; |
| 17 | + |
| 18 | +import static org.junit.jupiter.api.Assertions.*; |
| 19 | + |
| 20 | +import io.delta.kernel.data.ArrayValue; |
| 21 | +import io.delta.kernel.internal.actions.Format; |
| 22 | +import io.delta.kernel.internal.actions.Metadata; |
| 23 | +import io.delta.kernel.internal.actions.Protocol; |
| 24 | +import io.delta.kernel.internal.util.VectorUtils; |
| 25 | +import io.delta.kernel.types.IntegerType; |
| 26 | +import io.delta.kernel.types.StringType; |
| 27 | +import io.delta.kernel.types.StructType; |
| 28 | +import java.util.*; |
| 29 | +import org.apache.spark.sql.delta.NameMapping$; |
| 30 | +import org.apache.spark.sql.delta.NoMapping$; |
| 31 | +import org.junit.jupiter.api.Test; |
| 32 | +import scala.jdk.javaapi.CollectionConverters; |
| 33 | + |
| 34 | +/** Unit tests for {@link KernelMetadataAdapter} and {@link KernelProtocolAdapter}. */ |
| 35 | +public class ActionAdaptersTest { |
| 36 | + |
| 37 | + // ===== KernelProtocolAdapter ===== |
| 38 | + |
| 39 | + @Test |
| 40 | + public void testProtocolAdapterWithTableFeatures() { |
| 41 | + // Reader features: supported but empty (version >= 3 means features are supported, even with |
| 42 | + // an empty set). Writer features: supported and populated. |
| 43 | + Set<String> readerFeatures = Collections.emptySet(); |
| 44 | + Set<String> writerFeatures = new HashSet<>(Arrays.asList("v2Checkpoint", "rowTracking")); |
| 45 | + Protocol kernelProtocol = new Protocol(3, 7, readerFeatures, writerFeatures); |
| 46 | + |
| 47 | + KernelProtocolAdapter adapter = new KernelProtocolAdapter(kernelProtocol); |
| 48 | + |
| 49 | + assertEquals(3, adapter.minReaderVersion()); |
| 50 | + assertEquals(7, adapter.minWriterVersion()); |
| 51 | + assertTrue(adapter.readerFeatures().isDefined()); |
| 52 | + assertTrue(CollectionConverters.asJava(adapter.readerFeatures().get()).isEmpty()); |
| 53 | + assertTrue(adapter.writerFeatures().isDefined()); |
| 54 | + assertEquals( |
| 55 | + new HashSet<>(Arrays.asList("v2Checkpoint", "rowTracking")), |
| 56 | + CollectionConverters.asJava(adapter.writerFeatures().get())); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void testProtocolAdapterLegacyProtocol() { |
| 61 | + Protocol kernelProtocol = new Protocol(1, 2); |
| 62 | + |
| 63 | + KernelProtocolAdapter adapter = new KernelProtocolAdapter(kernelProtocol); |
| 64 | + |
| 65 | + assertEquals(1, adapter.minReaderVersion()); |
| 66 | + assertEquals(2, adapter.minWriterVersion()); |
| 67 | + assertTrue(adapter.readerFeatures().isEmpty()); |
| 68 | + assertTrue(adapter.writerFeatures().isEmpty()); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testProtocolAdapterNullThrows() { |
| 73 | + assertThrows(NullPointerException.class, () -> new KernelProtocolAdapter(null)); |
| 74 | + } |
| 75 | + |
| 76 | + // ===== KernelMetadataAdapter ===== |
| 77 | + |
| 78 | + @Test |
| 79 | + public void testMetadataAdapter() { |
| 80 | + ArrayValue partCols = |
| 81 | + VectorUtils.buildArrayValue(Arrays.asList("part1", "part2"), StringType.STRING); |
| 82 | + Map<String, String> formatOptions = Collections.singletonMap("foo", "bar"); |
| 83 | + Format format = new Format("parquet", formatOptions); |
| 84 | + Map<String, String> configuration = new HashMap<>(); |
| 85 | + configuration.put("zip", "zap"); |
| 86 | + configuration.put("delta.columnMapping.mode", "name"); |
| 87 | + |
| 88 | + Metadata kernelMetadata = |
| 89 | + new Metadata( |
| 90 | + "id", |
| 91 | + Optional.of("name"), |
| 92 | + Optional.of("description"), |
| 93 | + format, |
| 94 | + "{\"type\":\"struct\",\"fields\":" |
| 95 | + + "[{\"name\":\"part1\",\"type\":\"integer\",\"nullable\":true,\"metadata\":{}}," |
| 96 | + + "{\"name\":\"part2\",\"type\":\"string\",\"nullable\":false,\"metadata\":{}}," |
| 97 | + + "{\"name\":\"col1\",\"type\":\"string\",\"nullable\":false,\"metadata\":{}}]}", |
| 98 | + new StructType() |
| 99 | + .add("part1", IntegerType.INTEGER) |
| 100 | + .add("part2", StringType.STRING, false /* nullable */) |
| 101 | + .add("col1", StringType.STRING, false /* nullable */), |
| 102 | + partCols, |
| 103 | + Optional.of(42L), |
| 104 | + VectorUtils.stringStringMapValue(configuration)); |
| 105 | + |
| 106 | + KernelMetadataAdapter adapter = new KernelMetadataAdapter(kernelMetadata); |
| 107 | + |
| 108 | + assertEquals("id", adapter.id()); |
| 109 | + assertEquals("name", adapter.name()); |
| 110 | + assertEquals("description", adapter.description()); |
| 111 | + assertEquals(3, adapter.schema().fields().length); |
| 112 | + assertEquals("integer", adapter.schema().apply("part1").dataType().typeName()); |
| 113 | + assertTrue(adapter.schema().apply("part1").nullable()); |
| 114 | + assertEquals("string", adapter.schema().apply("part2").dataType().typeName()); |
| 115 | + assertFalse(adapter.schema().apply("part2").nullable()); |
| 116 | + assertEquals("string", adapter.schema().apply("col1").dataType().typeName()); |
| 117 | + assertFalse(adapter.schema().apply("col1").nullable()); |
| 118 | + assertEquals( |
| 119 | + Arrays.asList("part1", "part2"), CollectionConverters.asJava(adapter.partitionColumns())); |
| 120 | + org.apache.spark.sql.types.StructType partSchema = adapter.partitionSchema(); |
| 121 | + assertEquals(2, partSchema.fields().length); |
| 122 | + assertEquals("part1", partSchema.fields()[0].name()); |
| 123 | + assertEquals("integer", partSchema.fields()[0].dataType().typeName()); |
| 124 | + assertTrue(partSchema.fields()[0].nullable()); |
| 125 | + assertEquals("part2", partSchema.fields()[1].name()); |
| 126 | + assertEquals("string", partSchema.fields()[1].dataType().typeName()); |
| 127 | + assertFalse(partSchema.fields()[1].nullable()); |
| 128 | + assertEquals(configuration, CollectionConverters.asJava(adapter.configuration())); |
| 129 | + assertEquals(NameMapping$.MODULE$, adapter.columnMappingMode()); |
| 130 | + } |
| 131 | + |
| 132 | + @Test |
| 133 | + public void testMetadataAdapterWithNullOptionalFields() { |
| 134 | + ArrayValue emptyPartCols = |
| 135 | + VectorUtils.buildArrayValue(Collections.emptyList(), StringType.STRING); |
| 136 | + Format format = new Format("parquet", Collections.emptyMap()); |
| 137 | + |
| 138 | + Metadata kernelMetadata = |
| 139 | + new Metadata( |
| 140 | + "id2", |
| 141 | + Optional.empty(), |
| 142 | + Optional.empty(), |
| 143 | + format, |
| 144 | + "{\"type\":\"struct\",\"fields\":[]}", |
| 145 | + new StructType(), |
| 146 | + emptyPartCols, |
| 147 | + Optional.empty(), |
| 148 | + VectorUtils.stringStringMapValue(Collections.emptyMap())); |
| 149 | + |
| 150 | + KernelMetadataAdapter adapter = new KernelMetadataAdapter(kernelMetadata); |
| 151 | + |
| 152 | + assertEquals("id2", adapter.id()); |
| 153 | + assertNull(adapter.name()); |
| 154 | + assertNull(adapter.description()); |
| 155 | + assertEquals(0, adapter.schema().fields().length); |
| 156 | + assertTrue(CollectionConverters.asJava(adapter.partitionColumns()).isEmpty()); |
| 157 | + assertEquals(0, adapter.partitionSchema().fields().length); |
| 158 | + assertTrue(CollectionConverters.asJava(adapter.configuration()).isEmpty()); |
| 159 | + assertEquals(NoMapping$.MODULE$, adapter.columnMappingMode()); |
| 160 | + } |
| 161 | + |
| 162 | + @Test |
| 163 | + public void testMetadataAdapterNullThrows() { |
| 164 | + assertThrows(NullPointerException.class, () -> new KernelMetadataAdapter(null)); |
| 165 | + } |
| 166 | +} |
0 commit comments