Fokko commented on code in PR #11786:
URL: https://github.com/apache/iceberg/pull/11786#discussion_r1887585686


##########
core/src/main/java/org/apache/iceberg/data/avro/PlannedDataReader.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * 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.data.avro;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Supplier;
+import org.apache.avro.LogicalType;
+import org.apache.avro.LogicalTypes;
+import org.apache.avro.Schema;
+import org.apache.avro.io.DatumReader;
+import org.apache.avro.io.Decoder;
+import org.apache.iceberg.avro.AvroSchemaUtil;
+import org.apache.iceberg.avro.AvroWithPartnerVisitor;
+import org.apache.iceberg.avro.SupportsRowPosition;
+import org.apache.iceberg.avro.ValueReader;
+import org.apache.iceberg.avro.ValueReaders;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.Pair;
+
+public class PlannedDataReader<T> implements DatumReader<T>, 
SupportsRowPosition {
+
+  public static <D> PlannedDataReader<D> create(org.apache.iceberg.Schema 
expectedSchema) {
+    return create(expectedSchema, ImmutableMap.of());
+  }
+
+  public static <D> PlannedDataReader<D> create(
+      org.apache.iceberg.Schema expectedSchema, Map<Integer, ?> idToConstant) {
+    return new PlannedDataReader<>(expectedSchema, idToConstant);
+  }
+
+  private final org.apache.iceberg.Schema expectedSchema;
+  private final Map<Integer, ?> idToConstant;
+  private ValueReader<T> reader;
+
+  protected PlannedDataReader(
+      org.apache.iceberg.Schema expectedSchema, Map<Integer, ?> idToConstant) {
+    this.expectedSchema = expectedSchema;
+    this.idToConstant = idToConstant;
+  }
+
+  @Override
+  @SuppressWarnings("unchecked")
+  public void setSchema(Schema fileSchema) {
+    this.reader =
+        (ValueReader<T>)
+            AvroWithPartnerVisitor.visit(
+                expectedSchema.asStruct(),
+                fileSchema,
+                new ReadBuilder(idToConstant),
+                AvroWithPartnerVisitor.FieldIDAccessors.get());
+  }
+
+  @Override
+  public T read(T reuse, Decoder decoder) throws IOException {
+    return reader.read(decoder, reuse);
+  }
+
+  @Override
+  public void setRowPositionSupplier(Supplier<Long> posSupplier) {
+    if (reader instanceof SupportsRowPosition) {
+      ((SupportsRowPosition) reader).setRowPositionSupplier(posSupplier);
+    }
+  }
+
+  private static class ReadBuilder extends AvroWithPartnerVisitor<Type, 
ValueReader<?>> {
+    private final Map<Integer, ?> idToConstant;
+
+    private ReadBuilder(Map<Integer, ?> idToConstant) {
+      this.idToConstant = idToConstant;
+    }
+
+    @Override
+    public ValueReader<?> record(Type partner, Schema record, 
List<ValueReader<?>> fieldReaders) {
+      if (partner == null) {
+        return ValueReaders.skipStruct(fieldReaders);
+      }
+
+      Types.StructType expected = partner.asStructType();
+      List<Pair<Integer, ValueReader<?>>> readPlan =
+          ValueReaders.buildReadPlan(expected, record, fieldReaders, 
idToConstant);
+
+      return GenericReaders.struct(readPlan, expected);
+    }
+
+    @Override
+    public ValueReader<?> union(Type partner, Schema union, 
List<ValueReader<?>> options) {
+      return ValueReaders.union(options);
+    }
+
+    @Override
+    public ValueReader<?> array(Type ignored, Schema array, ValueReader<?> 
elementReader) {
+      return ValueReaders.array(elementReader);
+    }
+
+    @Override
+    public ValueReader<?> arrayMap(
+        Type ignored, Schema map, ValueReader<?> keyReader, ValueReader<?> 
valueReader) {
+      return ValueReaders.arrayMap(keyReader, valueReader);
+    }
+
+    @Override
+    public ValueReader<?> map(Type ignored, Schema map, ValueReader<?> 
valueReader) {
+      return ValueReaders.map(ValueReaders.strings(), valueReader);
+    }
+
+    @Override
+    public ValueReader<?> primitive(Type ignored, Schema primitive) {
+      LogicalType logicalType = primitive.getLogicalType();
+      if (logicalType != null) {
+        switch (logicalType.getName()) {
+          case "date":
+            return GenericReaders.dates();
+
+          case "time-micros":
+            return GenericReaders.times();
+
+          case "timestamp-micros":

Review Comment:
   I noticed that we have this string all over the place, probably a good idea 
to make a constant for it: 
https://github.com/search?q=repo%3Aapache%2Ficeberg+timestamp-micros+language%3AJava&type=code&l=Java
   
   We can also do that in a separate PR.



##########
core/src/main/java/org/apache/iceberg/data/avro/DataReader.java:
##########
@@ -36,6 +36,10 @@
 import org.apache.iceberg.types.Type;
 import org.apache.iceberg.types.Types;
 
+/**
+ * @deprecated will be removed in 2.0.0; use {@link PlannedDataReader} instead.
+ */
+@Deprecated

Review Comment:
   Checked, and didn't find any usage anymore 👍 



##########
core/src/main/java/org/apache/iceberg/data/avro/RawDecoder.java:
##########
@@ -33,6 +33,27 @@
 public class RawDecoder<D> extends MessageDecoder.BaseDecoder<D> {
   private static final ThreadLocal<BinaryDecoder> DECODER = new 
ThreadLocal<>();
 
+  /**
+   * Creates a new {@link MessageDecoder} that constructs datum instances 
described by the {@link
+   * Schema readSchema}.
+   *
+   * <p>The {@code readSchema} is used for the expected schema and the {@code 
writeSchema} is the
+   * schema used to decode buffers. The {@code writeSchema} must be the schema 
that was used to
+   * encode all buffers decoded by this class.
+   *
+   * @param readSchema an Iceberg schema to produce when reading
+   * @param readerFunction a function that produces a DatumReader from the 
read schema
+   * @param writeSchema an Avro schema that describes serialized data to be 
read
+   */
+  public static <D> RawDecoder<D> create(
+      org.apache.iceberg.Schema readSchema,
+      Function<org.apache.iceberg.Schema, DatumReader<D>> readerFunction,

Review Comment:
   Nice!



##########
core/src/main/java/org/apache/iceberg/data/avro/PlannedDataReader.java:
##########
@@ -0,0 +1,187 @@
+/*
+ * 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.data.avro;
+
+import java.io.IOException;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Supplier;
+import org.apache.avro.LogicalType;
+import org.apache.avro.LogicalTypes;
+import org.apache.avro.Schema;
+import org.apache.avro.io.DatumReader;
+import org.apache.avro.io.Decoder;
+import org.apache.iceberg.avro.AvroSchemaUtil;
+import org.apache.iceberg.avro.AvroWithPartnerVisitor;
+import org.apache.iceberg.avro.SupportsRowPosition;
+import org.apache.iceberg.avro.ValueReader;
+import org.apache.iceberg.avro.ValueReaders;
+import org.apache.iceberg.relocated.com.google.common.collect.ImmutableMap;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+import org.apache.iceberg.util.Pair;
+
+public class PlannedDataReader<T> implements DatumReader<T>, 
SupportsRowPosition {
+
+  public static <D> PlannedDataReader<D> create(org.apache.iceberg.Schema 
expectedSchema) {
+    return create(expectedSchema, ImmutableMap.of());
+  }
+
+  public static <D> PlannedDataReader<D> create(
+      org.apache.iceberg.Schema expectedSchema, Map<Integer, ?> idToConstant) {
+    return new PlannedDataReader<>(expectedSchema, idToConstant);
+  }
+
+  private final org.apache.iceberg.Schema expectedSchema;
+  private final Map<Integer, ?> idToConstant;
+  private ValueReader<T> reader;
+
+  protected PlannedDataReader(
+      org.apache.iceberg.Schema expectedSchema, Map<Integer, ?> idToConstant) {
+    this.expectedSchema = expectedSchema;
+    this.idToConstant = idToConstant;
+  }
+
+  @Override
+  @SuppressWarnings("unchecked")
+  public void setSchema(Schema fileSchema) {
+    this.reader =
+        (ValueReader<T>)
+            AvroWithPartnerVisitor.visit(
+                expectedSchema.asStruct(),
+                fileSchema,
+                new ReadBuilder(idToConstant),
+                AvroWithPartnerVisitor.FieldIDAccessors.get());
+  }
+
+  @Override
+  public T read(T reuse, Decoder decoder) throws IOException {
+    return reader.read(decoder, reuse);
+  }
+
+  @Override
+  public void setRowPositionSupplier(Supplier<Long> posSupplier) {
+    if (reader instanceof SupportsRowPosition) {
+      ((SupportsRowPosition) reader).setRowPositionSupplier(posSupplier);
+    }
+  }
+
+  private static class ReadBuilder extends AvroWithPartnerVisitor<Type, 
ValueReader<?>> {
+    private final Map<Integer, ?> idToConstant;
+
+    private ReadBuilder(Map<Integer, ?> idToConstant) {
+      this.idToConstant = idToConstant;
+    }
+
+    @Override
+    public ValueReader<?> record(Type partner, Schema record, 
List<ValueReader<?>> fieldReaders) {
+      if (partner == null) {
+        return ValueReaders.skipStruct(fieldReaders);
+      }
+
+      Types.StructType expected = partner.asStructType();
+      List<Pair<Integer, ValueReader<?>>> readPlan =
+          ValueReaders.buildReadPlan(expected, record, fieldReaders, 
idToConstant);
+
+      return GenericReaders.struct(readPlan, expected);
+    }
+
+    @Override
+    public ValueReader<?> union(Type partner, Schema union, 
List<ValueReader<?>> options) {
+      return ValueReaders.union(options);
+    }
+
+    @Override
+    public ValueReader<?> array(Type ignored, Schema array, ValueReader<?> 
elementReader) {
+      return ValueReaders.array(elementReader);
+    }
+
+    @Override
+    public ValueReader<?> arrayMap(
+        Type ignored, Schema map, ValueReader<?> keyReader, ValueReader<?> 
valueReader) {
+      return ValueReaders.arrayMap(keyReader, valueReader);
+    }
+
+    @Override
+    public ValueReader<?> map(Type ignored, Schema map, ValueReader<?> 
valueReader) {
+      return ValueReaders.map(ValueReaders.strings(), valueReader);
+    }
+
+    @Override
+    public ValueReader<?> primitive(Type partner, Schema primitive) {
+      LogicalType logicalType = primitive.getLogicalType();
+      if (logicalType != null) {
+        switch (logicalType.getName()) {
+          case "date":
+            return GenericReaders.dates();
+
+          case "time-micros":
+            return GenericReaders.times();
+
+          case "timestamp-micros":
+            if (AvroSchemaUtil.isTimestamptz(primitive)) {
+              return GenericReaders.timestamptz();
+            }
+            return GenericReaders.timestamps();
+
+          case "decimal":
+            return ValueReaders.decimal(
+                ValueReaders.decimalBytesReader(primitive),
+                ((LogicalTypes.Decimal) logicalType).getScale());
+
+          case "uuid":
+            return ValueReaders.uuids();

Review Comment:
   Separate PR, but I think we also need to take the physical type into 
account: 
https://github.com/apache/avro/blob/main/doc/content/en/docs/%2B%2Bversion%2B%2B/Specification/_index.md#uuid
   
   First Avro only supported UUIDs stored as strings, but now also as 
`fixed[16]`.



-- 
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: issues-unsubscr...@iceberg.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org
For additional commands, e-mail: issues-h...@iceberg.apache.org

Reply via email to