|
| 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 org.lance.spark.utils; |
| 15 | + |
| 16 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 17 | +import org.apache.arrow.vector.types.pojo.Field; |
| 18 | +import org.apache.arrow.vector.types.pojo.FieldType; |
| 19 | +import org.apache.spark.sql.types.DataTypes; |
| 20 | +import org.apache.spark.sql.types.Metadata; |
| 21 | +import org.apache.spark.sql.types.MetadataBuilder; |
| 22 | +import org.apache.spark.sql.types.StructField; |
| 23 | +import org.apache.spark.sql.types.StructType; |
| 24 | +import org.junit.jupiter.api.Test; |
| 25 | + |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.Collections; |
| 28 | + |
| 29 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 30 | +import static org.junit.jupiter.api.Assertions.assertFalse; |
| 31 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 32 | + |
| 33 | +public class BlobUtilsTest { |
| 34 | + |
| 35 | + @Test |
| 36 | + public void testBlobV2FieldWithArrowExtensionName() { |
| 37 | + assertTrue(BlobUtils.isBlobV2SparkField(blobV2Field())); |
| 38 | + } |
| 39 | + |
| 40 | + @Test |
| 41 | + public void testBlobV2FieldNullSafety() { |
| 42 | + assertFalse(BlobUtils.isBlobV2SparkField(null)); |
| 43 | + } |
| 44 | + |
| 45 | + @Test |
| 46 | + public void testV1Field() { |
| 47 | + assertTrue(BlobUtils.isBlobSparkField(blobV1Field())); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void testBlobV2ArrowFieldRejectsUnrelated() { |
| 52 | + Field f = |
| 53 | + new Field( |
| 54 | + "payload", |
| 55 | + new FieldType(true, ArrowType.Binary.INSTANCE, null, Collections.emptyMap()), |
| 56 | + null); |
| 57 | + assertFalse(BlobUtils.isBlobV2ArrowField(f)); |
| 58 | + assertFalse(BlobUtils.isBlobV2ArrowField(null)); |
| 59 | + } |
| 60 | + |
| 61 | + @Test |
| 62 | + public void testHasBlobV2FieldsInSchema() { |
| 63 | + StructType schema = |
| 64 | + new StructType( |
| 65 | + new StructField[] { |
| 66 | + field("id", DataTypes.IntegerType), blobV2Field(), |
| 67 | + }); |
| 68 | + assertTrue(BlobUtils.hasBlobV2Fields(schema)); |
| 69 | + } |
| 70 | + |
| 71 | + @Test |
| 72 | + public void testDescriptorStructShape() { |
| 73 | + StructType s = BlobUtils.BLOB_DESCRIPTOR_STRUCT; |
| 74 | + assertEquals(5, s.fields().length); |
| 75 | + assertEquals(DataTypes.ShortType, s.apply("kind").dataType()); |
| 76 | + assertEquals(DataTypes.LongType, s.apply("position").dataType()); |
| 77 | + assertEquals(DataTypes.LongType, s.apply("size").dataType()); |
| 78 | + assertEquals(DataTypes.LongType, s.apply("blob_id").dataType()); |
| 79 | + assertEquals(DataTypes.StringType, s.apply("blob_uri").dataType()); |
| 80 | + } |
| 81 | + |
| 82 | + @Test |
| 83 | + public void testBlobV2DescriptorSchemaRewrite() { |
| 84 | + StructType schema = |
| 85 | + new StructType( |
| 86 | + new StructField[] { |
| 87 | + field("id", DataTypes.IntegerType), blobV2Field(), |
| 88 | + }); |
| 89 | + StructType rewritten = BlobUtils.applyBlobV2DescriptorSchema(schema); |
| 90 | + assertEquals(DataTypes.IntegerType, rewritten.apply("id").dataType()); |
| 91 | + assertEquals(BlobUtils.BLOB_DESCRIPTOR_STRUCT, rewritten.apply("payload").dataType()); |
| 92 | + } |
| 93 | + |
| 94 | + @Test |
| 95 | + public void testV1FieldsPreservedInRewrite() { |
| 96 | + StructType schema = |
| 97 | + new StructType( |
| 98 | + new StructField[] { |
| 99 | + field("id", DataTypes.IntegerType), blobV1Field(), |
| 100 | + }); |
| 101 | + StructType rewritten = BlobUtils.applyBlobV2DescriptorSchema(schema); |
| 102 | + assertEquals(DataTypes.BinaryType, rewritten.apply("payload").dataType()); |
| 103 | + } |
| 104 | + |
| 105 | + @Test |
| 106 | + public void testUnloadedDescriptorStructRecognizedAsBlobV2() { |
| 107 | + Field f = |
| 108 | + new Field( |
| 109 | + "payload", |
| 110 | + new FieldType(true, ArrowType.Struct.INSTANCE, null, Collections.emptyMap()), |
| 111 | + Arrays.asList( |
| 112 | + intChild("kind"), |
| 113 | + intChild("position"), |
| 114 | + intChild("size"), |
| 115 | + intChild("blob_id"), |
| 116 | + utf8Child("blob_uri"))); |
| 117 | + assertTrue(BlobUtils.isBlobV2ArrowField(f)); |
| 118 | + assertFalse(BlobUtils.isBlobArrowField(f)); |
| 119 | + } |
| 120 | + |
| 121 | + private static Field intChild(String name) { |
| 122 | + return new Field( |
| 123 | + name, |
| 124 | + new FieldType(true, new ArrowType.Int(64, false), null, Collections.emptyMap()), |
| 125 | + null); |
| 126 | + } |
| 127 | + |
| 128 | + private static Field utf8Child(String name) { |
| 129 | + return new Field( |
| 130 | + name, new FieldType(true, ArrowType.Utf8.INSTANCE, null, Collections.emptyMap()), null); |
| 131 | + } |
| 132 | + |
| 133 | + private static StructField field(String name, org.apache.spark.sql.types.DataType dt) { |
| 134 | + return new StructField(name, dt, true, Metadata.empty()); |
| 135 | + } |
| 136 | + |
| 137 | + private static StructField blobV2Field() { |
| 138 | + Metadata md = |
| 139 | + new MetadataBuilder() |
| 140 | + .putString(BlobUtils.ARROW_EXTENSION_NAME_KEY, BlobUtils.ARROW_EXTENSION_BLOB_V2) |
| 141 | + .build(); |
| 142 | + return new StructField("payload", DataTypes.BinaryType, true, md); |
| 143 | + } |
| 144 | + |
| 145 | + private static StructField blobV1Field() { |
| 146 | + Metadata md = |
| 147 | + new MetadataBuilder() |
| 148 | + .putString(BlobUtils.LANCE_ENCODING_BLOB_KEY, BlobUtils.LANCE_ENCODING_BLOB_VALUE) |
| 149 | + .build(); |
| 150 | + return new StructField("payload", DataTypes.BinaryType, true, md); |
| 151 | + } |
| 152 | +} |
0 commit comments