mxm commented on code in PR #15265:
URL: https://github.com/apache/iceberg/pull/15265#discussion_r2798998886


##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkParquetReaders.java:
##########
@@ -853,4 +868,29 @@ public double[] toDoubleArray() {
       return ArrayUtil.toPrimitive((Double[]) values);
     }
   }
+
+  /** Variant reader to convert from Iceberg Variant to Flink Variant */
+  private static class VariantReader
+      extends DelegatingValueReader<org.apache.iceberg.variants.Variant, 
Variant> {
+    @SuppressWarnings("unchecked")
+    private VariantReader(ParquetValueReader<?> reader) {
+      super((ParquetValueReader<org.apache.iceberg.variants.Variant>) reader);
+    }
+
+    @Override
+    public Variant read(Variant reuse) {
+      org.apache.iceberg.variants.Variant icebergVariant = 
super.readFromDelegate(null);
+
+      byte[] metadataBytes = new byte[icebergVariant.metadata().sizeInBytes()];
+      ByteBuffer metadataBuffer =
+          
ByteBuffer.wrap(metadataBytes).order(java.nio.ByteOrder.LITTLE_ENDIAN);
+      icebergVariant.metadata().writeTo(metadataBuffer, 0);
+
+      byte[] valueBytes = new byte[icebergVariant.value().sizeInBytes()];
+      ByteBuffer valueBuffer = 
ByteBuffer.wrap(valueBytes).order(java.nio.ByteOrder.LITTLE_ENDIAN);

Review Comment:
   Good catch on the encoding.



##########
core/src/test/java/org/apache/iceberg/variants/VariantTestHelper.java:
##########
@@ -0,0 +1,142 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.iceberg.variants;
+
+import java.math.BigDecimal;
+import java.nio.ByteBuffer;
+
+public final class VariantTestHelper {
+
+  private VariantTestHelper() {}
+
+  public static final VariantPrimitive<?>[] PRIMITIVES =
+      new VariantPrimitive[] {
+        Variants.ofNull(),
+        Variants.of(true),
+        Variants.of(false),
+        Variants.of((byte) 34),
+        Variants.of((byte) -34),
+        Variants.of((short) 1234),
+        Variants.of((short) -1234),
+        Variants.of(12345),
+        Variants.of(-12345),
+        Variants.of(9876543210L),
+        Variants.of(-9876543210L),
+        Variants.of(10.11F),
+        Variants.of(-10.11F),
+        Variants.of(14.3D),
+        Variants.of(-14.3D),
+        Variants.ofIsoDate("2024-11-07"),
+        Variants.ofIsoDate("1957-11-07"),
+        Variants.ofIsoTimestamptz("2024-11-07T12:33:54.123456+00:00"),
+        Variants.ofIsoTimestamptz("1957-11-07T12:33:54.123456+00:00"),
+        Variants.ofIsoTimestampntz("2024-11-07T12:33:54.123456"),
+        Variants.ofIsoTimestampntz("1957-11-07T12:33:54.123456"),
+        Variants.of(new BigDecimal("12345.6789")), // decimal4
+        Variants.of(new BigDecimal("-12345.6789")), // decimal4
+        Variants.of(new BigDecimal("123456789.987654321")), // decimal8
+        Variants.of(new BigDecimal("-123456789.987654321")), // decimal8
+        Variants.of(new BigDecimal("9876543210.123456789")), // decimal16
+        Variants.of(new BigDecimal("-9876543210.123456789")), // decimal16
+        Variants.of(ByteBuffer.wrap(new byte[] {0x0a, 0x0b, 0x0c, 0x0d})),
+        Variants.of("iceberg"),
+        Variants.ofUUID("f24f9b64-81fa-49d1-b74e-8c09a6e31c56"),
+      };
+
+  public static final VariantPrimitive<?>[] UNSUPPORTED_PRIMITIVES =
+      new VariantPrimitive[] {
+        Variants.ofIsoTime("12:33:54.123456"),
+        Variants.ofIsoTimestamptzNanos("2024-11-07T12:33:54.123456789+00:00"),
+        Variants.ofIsoTimestampntzNanos("2024-11-07T12:33:54.123456789"),
+      };

Review Comment:
   In which sense are those unsupported? Only in Spark?



##########
flink/v2.1/flink/src/main/java/org/apache/iceberg/flink/data/FlinkParquetWriters.java:
##########
@@ -588,6 +610,48 @@ public Map.Entry<K, V> next() {
     }
   }
 
+  /** Variant writer converts from VariantVal to Variant */
+  private static class VariantWriter implements ParquetValueWriter<Variant> {
+    private final ParquetValueWriter<org.apache.iceberg.variants.Variant> 
writer;
+
+    @SuppressWarnings("unchecked")
+    private VariantWriter(ParquetValueWriter<?> writer) {
+      this.writer = (ParquetValueWriter<org.apache.iceberg.variants.Variant>) 
writer;
+    }
+
+    @Override
+    public void write(int repetitionLevel, Variant variant) {
+      Preconditions.checkArgument(
+          variant instanceof BinaryVariant,
+          "Expected BinaryVariant but got: " + 
variant.getClass().getSimpleName());

Review Comment:
   NIT
   ```suggestion
             "Expected BinaryVariant but got: %s", 
variant.getClass().getSimpleName());
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to