|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one |
| 3 | + * or more contributor license agreements. See the NOTICE file |
| 4 | + * distributed with this work for additional information |
| 5 | + * regarding copyright ownership. The ASF licenses this file |
| 6 | + * to you under the Apache License, Version 2.0 (the |
| 7 | + * "License"); you may not use this file except in compliance |
| 8 | + * with the License. You may obtain a copy of the License at |
| 9 | + * |
| 10 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | + * |
| 12 | + * Unless required by applicable law or agreed to in writing, |
| 13 | + * software distributed under the License is distributed on an |
| 14 | + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 15 | + * KIND, either express or implied. See the License for the |
| 16 | + * specific language governing permissions and limitations |
| 17 | + * under the License. |
| 18 | + */ |
| 19 | + |
| 20 | +package org.apache.datafusion.proto; |
| 21 | + |
| 22 | +import static org.junit.jupiter.api.Assertions.assertEquals; |
| 23 | +import static org.junit.jupiter.api.Assertions.assertThrows; |
| 24 | +import static org.junit.jupiter.api.Assertions.assertTrue; |
| 25 | + |
| 26 | +import java.util.Arrays; |
| 27 | +import java.util.HashMap; |
| 28 | +import java.util.List; |
| 29 | +import java.util.Map; |
| 30 | + |
| 31 | +import org.apache.arrow.vector.types.DateUnit; |
| 32 | +import org.apache.arrow.vector.types.FloatingPointPrecision; |
| 33 | +import org.apache.arrow.vector.types.pojo.ArrowType; |
| 34 | +import org.apache.arrow.vector.types.pojo.Field; |
| 35 | +import org.apache.arrow.vector.types.pojo.FieldType; |
| 36 | +import org.apache.arrow.vector.types.pojo.Schema; |
| 37 | +import org.junit.jupiter.api.Test; |
| 38 | + |
| 39 | +import datafusion_common.DatafusionCommon; |
| 40 | + |
| 41 | +class SchemaConverterTest { |
| 42 | + |
| 43 | + @Test |
| 44 | + void roundTripsPrimitivesAndMetadata() { |
| 45 | + Map<String, String> schemaMeta = new HashMap<>(); |
| 46 | + schemaMeta.put("origin", "test"); |
| 47 | + Map<String, String> fieldMeta = new HashMap<>(); |
| 48 | + fieldMeta.put("ns", "demo"); |
| 49 | + |
| 50 | + List<Field> fields = |
| 51 | + Arrays.asList( |
| 52 | + new Field("a_bool", FieldType.nullable(ArrowType.Bool.INSTANCE), null), |
| 53 | + new Field("a_i32", FieldType.notNullable(new ArrowType.Int(32, true)), null), |
| 54 | + new Field("a_i64", FieldType.nullable(new ArrowType.Int(64, true)), null), |
| 55 | + new Field("a_u32", FieldType.nullable(new ArrowType.Int(32, false)), null), |
| 56 | + new Field( |
| 57 | + "a_f64", |
| 58 | + FieldType.nullable(new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE)), |
| 59 | + null), |
| 60 | + new Field("a_str", FieldType.nullable(ArrowType.Utf8.INSTANCE), null), |
| 61 | + new Field("a_date", FieldType.nullable(new ArrowType.Date(DateUnit.DAY)), null), |
| 62 | + new Field( |
| 63 | + "a_with_meta", |
| 64 | + new FieldType(true, new ArrowType.Int(8, true), null, fieldMeta), |
| 65 | + null)); |
| 66 | + |
| 67 | + Schema original = new Schema(fields, schemaMeta); |
| 68 | + |
| 69 | + DatafusionCommon.Schema proto = SchemaConverter.toProto(original); |
| 70 | + Schema roundTripped = SchemaConverter.fromProto(proto); |
| 71 | + |
| 72 | + assertEquals(original, roundTripped); |
| 73 | + } |
| 74 | + |
| 75 | + @Test |
| 76 | + void decimalPreservesPrecisionAndScale() { |
| 77 | + Schema original = |
| 78 | + new Schema( |
| 79 | + List.of( |
| 80 | + new Field( |
| 81 | + "amount", |
| 82 | + FieldType.nullable(new ArrowType.Decimal(18, 5, 128)), |
| 83 | + null))); |
| 84 | + |
| 85 | + DatafusionCommon.Schema proto = SchemaConverter.toProto(original); |
| 86 | + Schema roundTripped = SchemaConverter.fromProto(proto); |
| 87 | + |
| 88 | + assertEquals(original, roundTripped); |
| 89 | + ArrowType.Decimal d = (ArrowType.Decimal) roundTripped.getFields().get(0).getType(); |
| 90 | + assertEquals(18, d.getPrecision()); |
| 91 | + assertEquals(5, d.getScale()); |
| 92 | + assertEquals(128, d.getBitWidth()); |
| 93 | + } |
| 94 | + |
| 95 | + @Test |
| 96 | + void unsupportedTypeRaisesUnsupportedOperationException() { |
| 97 | + Field listField = |
| 98 | + new Field( |
| 99 | + "nested", |
| 100 | + FieldType.nullable(new ArrowType.List()), |
| 101 | + List.of(new Field("item", FieldType.nullable(new ArrowType.Int(32, true)), null))); |
| 102 | + Schema original = new Schema(List.of(listField)); |
| 103 | + |
| 104 | + UnsupportedOperationException ex = |
| 105 | + assertThrows(UnsupportedOperationException.class, () -> SchemaConverter.toProto(original)); |
| 106 | + assertTrue( |
| 107 | + ex.getMessage().contains("List"), |
| 108 | + "exception message should name the unsupported type, was: " + ex.getMessage()); |
| 109 | + } |
| 110 | +} |
0 commit comments