You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@iceberg.apache.org by GitBox <gi...@apache.org> on 2020/07/23 04:00:19 UTC

[GitHub] [iceberg] JingsongLi opened a new pull request #1232: Flink: Using RowData to avro reader and writer

JingsongLi opened a new pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232


   Fixes #1231 


----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] rdblue merged pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
rdblue merged pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232


   


----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] JingsongLi commented on a change in pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
JingsongLi commented on a change in pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#discussion_r466108675



##########
File path: flink/src/main/java/org/apache/iceberg/flink/FlinkTypeVisitor.java
##########
@@ -30,7 +30,7 @@
 import org.apache.flink.table.types.logical.YearMonthIntervalType;
 import org.apache.flink.table.types.logical.ZonedTimestampType;
 
-abstract class FlinkTypeVisitor<T> implements LogicalTypeVisitor<T> {
+public abstract class FlinkTypeVisitor<T> implements LogicalTypeVisitor<T> {

Review comment:
       No need, I used to think the reading and writing will rely on `FlinkTypeVisitor`.




----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] rdblue commented on pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
rdblue commented on pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#issuecomment-664048886


   Looks like this one needs to be rebased after the others from #1231 are merged.


----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] openinx commented on a change in pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
openinx commented on a change in pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#discussion_r461997869



##########
File path: flink/src/test/java/org/apache/iceberg/flink/data/TestFlinkAvroReaderWriter.java
##########
@@ -22,52 +22,76 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.Iterator;
-import org.apache.flink.types.Row;
+import java.util.List;
+import org.apache.flink.table.data.RowData;
 import org.apache.iceberg.Files;
 import org.apache.iceberg.Schema;
 import org.apache.iceberg.avro.Avro;
+import org.apache.iceberg.data.DataTest;
+import org.apache.iceberg.data.RandomGenericData;
+import org.apache.iceberg.data.Record;
+import org.apache.iceberg.data.avro.DataWriter;
+import org.apache.iceberg.flink.FlinkSchemaUtil;
 import org.apache.iceberg.io.CloseableIterable;
 import org.apache.iceberg.io.FileAppender;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
 import org.junit.Assert;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
 
-import static org.apache.iceberg.flink.data.RandomData.COMPLEX_SCHEMA;
+public class TestFlinkAvroReaderWriter extends DataTest {
 
-public class TestFlinkAvroReaderWriter {
-  private static final int NUM_RECORDS = 20_000;
+  private static final int NUM_RECORDS = 100;
 
-  @Rule
-  public TemporaryFolder temp = new TemporaryFolder();
+  @Override
+  protected void writeAndValidate(Schema schema) throws IOException {
+    List<RowData> inputs = generateDataFromAvroFile(schema);

Review comment:
       I see you will generate the `List<Record>` firstly, then write to the file appender, and finally read them into `List<RowData>`,   could we just use the `RandomData#generateRowData`  to produce those `RowData` ?   

##########
File path: flink/src/test/java/org/apache/iceberg/flink/data/RandomData.java
##########
@@ -88,6 +93,34 @@ public Row next() {
     };
   }
 
+  private static Iterable<RowData> generateRowData(Schema schema, int numRecords,
+      Supplier<RandomRowGenerator> supplier) {
+    DataStructureConverter<Object, Object> converter =
+        DataStructureConverters.getConverter(TypeConversions.fromLogicalToDataType(FlinkSchemaUtil.convert(schema)));

Review comment:
       Here  we may need to call `converter.open(RandomData.class.getClassLoader())` to initialize the converter ? 




----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] rdblue commented on a change in pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#discussion_r465977824



##########
File path: flink/src/main/java/org/apache/iceberg/flink/FlinkTypeVisitor.java
##########
@@ -30,7 +30,7 @@
 import org.apache.flink.table.types.logical.YearMonthIntervalType;
 import org.apache.flink.table.types.logical.ZonedTimestampType;
 
-abstract class FlinkTypeVisitor<T> implements LogicalTypeVisitor<T> {
+public abstract class FlinkTypeVisitor<T> implements LogicalTypeVisitor<T> {

Review comment:
       Does this need to be public? The only reference to `FlinkTypeVisitor` that I see in this PR is here, so I'm not sure why this is needed.




----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] JingsongLi commented on a change in pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
JingsongLi commented on a change in pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#discussion_r466108100



##########
File path: flink/src/test/java/org/apache/iceberg/flink/data/TestFlinkAvroReaderWriter.java
##########
@@ -22,52 +22,76 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.Iterator;
-import org.apache.flink.types.Row;
+import java.util.List;
+import org.apache.flink.table.data.RowData;
 import org.apache.iceberg.Files;
 import org.apache.iceberg.Schema;
 import org.apache.iceberg.avro.Avro;
+import org.apache.iceberg.data.DataTest;
+import org.apache.iceberg.data.RandomGenericData;
+import org.apache.iceberg.data.Record;
+import org.apache.iceberg.data.avro.DataWriter;
+import org.apache.iceberg.flink.FlinkSchemaUtil;
 import org.apache.iceberg.io.CloseableIterable;
 import org.apache.iceberg.io.FileAppender;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
 import org.junit.Assert;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
 
-import static org.apache.iceberg.flink.data.RandomData.COMPLEX_SCHEMA;
+public class TestFlinkAvroReaderWriter extends DataTest {
 
-public class TestFlinkAvroReaderWriter {
-  private static final int NUM_RECORDS = 20_000;
+  private static final int NUM_RECORDS = 100;
 
-  @Rule
-  public TemporaryFolder temp = new TemporaryFolder();
+  @Override
+  protected void writeAndValidate(Schema schema) throws IOException {
+    List<RowData> inputs = generateDataFromAvroFile(schema);
 
-  private void testCorrectness(Schema schema, int numRecords, Iterable<Row> iterable) throws IOException {
     File testFile = temp.newFile();
     Assert.assertTrue("Delete should succeed", testFile.delete());
 
-    try (FileAppender<Row> writer = Avro.write(Files.localOutput(testFile))
+    try (FileAppender<RowData> writer = Avro.write(Files.localOutput(testFile))
         .schema(schema)
-        .createWriterFunc(FlinkAvroWriter::new)
+        .createWriterFunc(ignore -> new FlinkAvroWriter(FlinkSchemaUtil.convert(schema)))
         .build()) {
-      writer.addAll(iterable);
+      writer.addAll(inputs);
     }
 
-    try (CloseableIterable<Row> reader = Avro.read(Files.localInput(testFile))
+    try (CloseableIterable<RowData> reader = Avro.read(Files.localInput(testFile))
         .project(schema)
         .createReaderFunc(FlinkAvroReader::new)
         .build()) {
-      Iterator<Row> expected = iterable.iterator();
-      Iterator<Row> rows = reader.iterator();
-      for (int i = 0; i < numRecords; i += 1) {
+      Iterator<RowData> expected = inputs.iterator();
+      Iterator<RowData> rows = reader.iterator();
+      for (int i = 0; i < NUM_RECORDS; i += 1) {
         Assert.assertTrue("Should have expected number of rows", rows.hasNext());
         Assert.assertEquals(expected.next(), rows.next());
       }
       Assert.assertFalse("Should not have extra rows", rows.hasNext());
     }
   }
 
-  @Test
-  public void testNormalData() throws IOException {
-    testCorrectness(COMPLEX_SCHEMA, NUM_RECORDS, RandomData.generate(COMPLEX_SCHEMA, NUM_RECORDS, 19982));
+  private List<RowData> generateDataFromAvroFile(Schema schema) throws IOException {

Review comment:
       You are right, we should have a asserter for `RowData` and `Record`.




----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] rdblue commented on a change in pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#discussion_r465992783



##########
File path: flink/src/test/java/org/apache/iceberg/flink/data/TestFlinkAvroReaderWriter.java
##########
@@ -22,52 +22,76 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.Iterator;
-import org.apache.flink.types.Row;
+import java.util.List;
+import org.apache.flink.table.data.RowData;
 import org.apache.iceberg.Files;
 import org.apache.iceberg.Schema;
 import org.apache.iceberg.avro.Avro;
+import org.apache.iceberg.data.DataTest;
+import org.apache.iceberg.data.RandomGenericData;
+import org.apache.iceberg.data.Record;
+import org.apache.iceberg.data.avro.DataWriter;
+import org.apache.iceberg.flink.FlinkSchemaUtil;
 import org.apache.iceberg.io.CloseableIterable;
 import org.apache.iceberg.io.FileAppender;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
 import org.junit.Assert;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
 
-import static org.apache.iceberg.flink.data.RandomData.COMPLEX_SCHEMA;
+public class TestFlinkAvroReaderWriter extends DataTest {
 
-public class TestFlinkAvroReaderWriter {
-  private static final int NUM_RECORDS = 20_000;
+  private static final int NUM_RECORDS = 100;
 
-  @Rule
-  public TemporaryFolder temp = new TemporaryFolder();
+  @Override
+  protected void writeAndValidate(Schema schema) throws IOException {
+    List<RowData> inputs = generateDataFromAvroFile(schema);
 
-  private void testCorrectness(Schema schema, int numRecords, Iterable<Row> iterable) throws IOException {
     File testFile = temp.newFile();
     Assert.assertTrue("Delete should succeed", testFile.delete());
 
-    try (FileAppender<Row> writer = Avro.write(Files.localOutput(testFile))
+    try (FileAppender<RowData> writer = Avro.write(Files.localOutput(testFile))
         .schema(schema)
-        .createWriterFunc(FlinkAvroWriter::new)
+        .createWriterFunc(ignore -> new FlinkAvroWriter(FlinkSchemaUtil.convert(schema)))
         .build()) {
-      writer.addAll(iterable);
+      writer.addAll(inputs);
     }
 
-    try (CloseableIterable<Row> reader = Avro.read(Files.localInput(testFile))
+    try (CloseableIterable<RowData> reader = Avro.read(Files.localInput(testFile))
         .project(schema)
         .createReaderFunc(FlinkAvroReader::new)
         .build()) {
-      Iterator<Row> expected = iterable.iterator();
-      Iterator<Row> rows = reader.iterator();
-      for (int i = 0; i < numRecords; i += 1) {
+      Iterator<RowData> expected = inputs.iterator();
+      Iterator<RowData> rows = reader.iterator();
+      for (int i = 0; i < NUM_RECORDS; i += 1) {
         Assert.assertTrue("Should have expected number of rows", rows.hasNext());
         Assert.assertEquals(expected.next(), rows.next());
       }
       Assert.assertFalse("Should not have extra rows", rows.hasNext());
     }
   }
 
-  @Test
-  public void testNormalData() throws IOException {
-    testCorrectness(COMPLEX_SCHEMA, NUM_RECORDS, RandomData.generate(COMPLEX_SCHEMA, NUM_RECORDS, 19982));
+  private List<RowData> generateDataFromAvroFile(Schema schema) throws IOException {

Review comment:
       I think it would be better to validate Flink `RowData` against generic `Record`. That's what we do in Spark tests, where we first write using generics (or Avro in older tests) and then validate that the records we read using the Spark object model are equivalent. By doing that, you not only test that `RowData` to disk and back to `RowData` works, but that the records are actually equivalent to another read format.




----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] JingsongLi commented on a change in pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
JingsongLi commented on a change in pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#discussion_r462011265



##########
File path: flink/src/main/java/org/apache/iceberg/flink/data/FlinkValueWriters.java
##########
@@ -0,0 +1,274 @@
+/*
+ * 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.flink.data;
+
+import java.io.IOException;
+import java.lang.reflect.Array;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.util.Utf8;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.MapData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.iceberg.avro.ValueWriter;
+import org.apache.iceberg.avro.ValueWriters;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.types.TypeUtil;
+
+public class FlinkValueWriters {
+
+  private FlinkValueWriters() {}
+
+  static ValueWriter<StringData> strings() {
+    return StringWriter.INSTANCE;
+  }
+
+  static ValueWriter<byte[]> uuids() {
+    return ValueWriters.fixed(16);
+  }
+
+  static ValueWriter<Integer> timeMicros() {
+    return TimeMicrosWriter.INSTANCE;
+  }
+
+  static ValueWriter<TimestampData> timestampMicros() {
+    return TimestampMicrosWriter.INSTANCE;
+  }
+
+  static ValueWriter<DecimalData> decimal(int precision, int scale) {
+    return new DecimalWriter(precision, scale);
+  }
+
+  static <T> ValueWriter<ArrayData> array(ValueWriter<T> elementWriter, LogicalType elementType) {
+    return new ArrayWriter<>(elementWriter, elementType);
+  }
+
+  static <K, V> ValueWriter<MapData> arrayMap(
+      ValueWriter<K> keyWriter, LogicalType keyType, ValueWriter<V> valueWriter, LogicalType valueType) {
+    return new ArrayMapWriter<>(keyWriter, keyType, valueWriter, valueType);
+  }
+
+  static <K, V> ValueWriter<MapData> map(
+      ValueWriter<K> keyWriter, LogicalType keyType, ValueWriter<V> valueWriter, LogicalType valueType) {
+    return new MapWriter<>(keyWriter, keyType, valueWriter, valueType);
+  }
+
+  static ValueWriter<RowData> row(List<ValueWriter<?>> writers, List<LogicalType> types) {
+    return new RowWriter(writers, types);
+  }
+
+  private static class StringWriter implements ValueWriter<StringData> {
+    private static final StringWriter INSTANCE = new StringWriter();
+
+    private StringWriter() {
+    }
+
+    @Override
+    public void write(StringData s, Encoder encoder) throws IOException {
+      // toBytes is cheaper than Avro calling toString, which incurs encoding costs
+      encoder.writeString(new Utf8(s.toBytes()));
+    }
+  }
+
+  private static class DecimalWriter implements ValueWriter<DecimalData> {
+    private final int precision;
+    private final int scale;
+    private final int length;
+    private final ThreadLocal<byte[]> bytes;
+
+    private DecimalWriter(int precision, int scale) {
+      this.precision = precision;
+      this.scale = scale;
+      this.length = TypeUtil.decimalRequiredBytes(precision);
+      this.bytes = ThreadLocal.withInitial(() -> new byte[length]);
+    }
+
+    @Override
+    public void write(DecimalData d, Encoder encoder) throws IOException {
+      Preconditions.checkArgument(d.scale() == scale,
+          "Cannot write value as decimal(%s,%s), wrong scale: %s", precision, scale, d);
+      Preconditions.checkArgument(d.precision() <= precision,
+          "Cannot write value as decimal(%s,%s), too large: %s", precision, scale, d);
+
+      BigDecimal decimal = d.toBigDecimal();
+
+      byte fillByte = (byte) (decimal.signum() < 0 ? 0xFF : 0x00);

Review comment:
       Created https://github.com/apache/iceberg/pull/1265 for this.

##########
File path: flink/src/test/java/org/apache/iceberg/flink/data/RandomData.java
##########
@@ -88,6 +93,34 @@ public Row next() {
     };
   }
 
+  private static Iterable<RowData> generateRowData(Schema schema, int numRecords,
+      Supplier<RandomRowGenerator> supplier) {
+    DataStructureConverter<Object, Object> converter =
+        DataStructureConverters.getConverter(TypeConversions.fromLogicalToDataType(FlinkSchemaUtil.convert(schema)));

Review comment:
       Yes, we can, only `StructuredObjectConverter` implements `open`, but now, Flink not support structure type. (It is not RowType).
   I'll revert this method in `RandomData`, it is not be used.

##########
File path: flink/src/test/java/org/apache/iceberg/flink/data/TestFlinkAvroReaderWriter.java
##########
@@ -22,52 +22,76 @@
 import java.io.File;
 import java.io.IOException;
 import java.util.Iterator;
-import org.apache.flink.types.Row;
+import java.util.List;
+import org.apache.flink.table.data.RowData;
 import org.apache.iceberg.Files;
 import org.apache.iceberg.Schema;
 import org.apache.iceberg.avro.Avro;
+import org.apache.iceberg.data.DataTest;
+import org.apache.iceberg.data.RandomGenericData;
+import org.apache.iceberg.data.Record;
+import org.apache.iceberg.data.avro.DataWriter;
+import org.apache.iceberg.flink.FlinkSchemaUtil;
 import org.apache.iceberg.io.CloseableIterable;
 import org.apache.iceberg.io.FileAppender;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
 import org.junit.Assert;
-import org.junit.Rule;
-import org.junit.Test;
-import org.junit.rules.TemporaryFolder;
 
-import static org.apache.iceberg.flink.data.RandomData.COMPLEX_SCHEMA;
+public class TestFlinkAvroReaderWriter extends DataTest {
 
-public class TestFlinkAvroReaderWriter {
-  private static final int NUM_RECORDS = 20_000;
+  private static final int NUM_RECORDS = 100;
 
-  @Rule
-  public TemporaryFolder temp = new TemporaryFolder();
+  @Override
+  protected void writeAndValidate(Schema schema) throws IOException {
+    List<RowData> inputs = generateDataFromAvroFile(schema);

Review comment:
       - First, `RandomData` now is incorrect, like array, like timestamp with zone, and etc.. 
   - Second, using Iceberg avro writer can test format compatible better.




----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] rdblue commented on a change in pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#discussion_r465989515



##########
File path: flink/src/main/java/org/apache/iceberg/flink/data/FlinkValueWriters.java
##########
@@ -0,0 +1,240 @@
+/*
+ * 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.flink.data;
+
+import java.io.IOException;
+import java.lang.reflect.Array;
+import java.util.List;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.util.Utf8;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.MapData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.iceberg.avro.ValueWriter;
+import org.apache.iceberg.types.TypeUtil;
+import org.apache.iceberg.util.DecimalUtil;
+
+public class FlinkValueWriters {
+
+  private FlinkValueWriters() {}
+
+  static ValueWriter<StringData> strings() {
+    return StringWriter.INSTANCE;
+  }
+
+  static ValueWriter<Integer> timeMicros() {
+    return TimeMicrosWriter.INSTANCE;
+  }
+
+  static ValueWriter<TimestampData> timestampMicros() {
+    return TimestampMicrosWriter.INSTANCE;
+  }
+
+  static ValueWriter<DecimalData> decimal(int precision, int scale) {
+    return new DecimalWriter(precision, scale);
+  }
+
+  static <T> ValueWriter<ArrayData> array(ValueWriter<T> elementWriter, LogicalType elementType) {
+    return new ArrayWriter<>(elementWriter, elementType);
+  }
+
+  static <K, V> ValueWriter<MapData> arrayMap(ValueWriter<K> keyWriter, LogicalType keyType,
+                                              ValueWriter<V> valueWriter, LogicalType valueType) {
+    return new ArrayMapWriter<>(keyWriter, keyType, valueWriter, valueType);
+  }
+
+  static <K, V> ValueWriter<MapData> map(ValueWriter<K> keyWriter, LogicalType keyType,
+                                         ValueWriter<V> valueWriter, LogicalType valueType) {
+    return new MapWriter<>(keyWriter, keyType, valueWriter, valueType);
+  }
+
+  static ValueWriter<RowData> row(List<ValueWriter<?>> writers, List<LogicalType> types) {
+    return new RowWriter(writers, types);
+  }
+
+  private static class StringWriter implements ValueWriter<StringData> {
+    private static final StringWriter INSTANCE = new StringWriter();
+
+    private StringWriter() {
+    }
+
+    @Override
+    public void write(StringData s, Encoder encoder) throws IOException {
+      // toBytes is cheaper than Avro calling toString, which incurs encoding costs
+      encoder.writeString(new Utf8(s.toBytes()));
+    }
+  }
+
+  private static class DecimalWriter implements ValueWriter<DecimalData> {
+    private final int precision;
+    private final int scale;
+    private final ThreadLocal<byte[]> bytes;
+
+    private DecimalWriter(int precision, int scale) {
+      this.precision = precision;
+      this.scale = scale;
+      this.bytes = ThreadLocal.withInitial(() -> new byte[TypeUtil.decimalRequiredBytes(precision)]);
+    }
+
+    @Override
+    public void write(DecimalData d, Encoder encoder) throws IOException {
+      encoder.writeFixed(DecimalUtil.toReusedFixLengthBytes(precision, scale, d.toBigDecimal(), bytes.get()));
+    }
+  }
+
+  private static class TimeMicrosWriter implements ValueWriter<Integer> {
+    private static final TimeMicrosWriter INSTANCE = new TimeMicrosWriter();
+
+    @Override
+    public void write(Integer timeMills, Encoder encoder) throws IOException {
+      encoder.writeLong(timeMills * 1000);
+    }
+  }
+
+  private static class TimestampMicrosWriter implements ValueWriter<TimestampData> {
+    private static final TimestampMicrosWriter INSTANCE = new TimestampMicrosWriter();
+
+    @Override
+    public void write(TimestampData timestampData, Encoder encoder) throws IOException {
+      long micros = timestampData.getMillisecond() * 1000 + timestampData.getNanoOfMillisecond() / 1000;
+      encoder.writeLong(micros);
+    }
+  }
+
+  private static class ArrayWriter<T> implements ValueWriter<ArrayData> {

Review comment:
       Eventually, we should refactor this into a base class for array data, so that the encoder parts are shared between Flink and Spark. Not something we should do right now, though.




----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] rdblue commented on a change in pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#discussion_r461977867



##########
File path: core/src/main/java/org/apache/iceberg/avro/AvroWithPartnerByStructureVisitor.java
##########
@@ -0,0 +1,164 @@
+/*
+ * 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.avro;
+
+import java.util.Deque;
+import java.util.List;
+import org.apache.avro.Schema;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.util.Pair;
+
+/**
+ * A abstract avro schema visitor with partner type. The visitor rely on the structure matching exactly and are
+ * guaranteed that because both schemas are derived from the same Iceberg schema.
+ *
+ * @param <P> Partner type.
+ * @param <T> Return T.
+ */
+public abstract class AvroWithPartnerByStructureVisitor<P, T> {

Review comment:
       I think this PR needs to be rebased now that #1235 is in, right?

##########
File path: flink/src/main/java/org/apache/iceberg/flink/data/FlinkValueReaders.java
##########
@@ -0,0 +1,314 @@
+/*
+ * 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.flink.data;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.util.Utf8;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.data.GenericMapData;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.MapData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.iceberg.avro.ValueReader;
+import org.apache.iceberg.avro.ValueReaders;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.types.Types;
+
+public class FlinkValueReaders {
+
+  private FlinkValueReaders() {}
+
+  static ValueReader<StringData> strings() {
+    return StringReader.INSTANCE;
+  }
+
+  static ValueReader<StringData> enums(List<String> symbols) {
+    return new EnumReader(symbols);
+  }
+
+  static ValueReader<byte[]> uuids() {
+    return ValueReaders.fixed(16);
+  }
+
+  static ValueReader<Integer> timeMicros() {
+    return TimeMicrosReader.INSTANCE;
+  }
+
+  static ValueReader<TimestampData> timestampMills() {
+    return TimestampMillsReader.INSTANCE;
+  }
+
+  static ValueReader<TimestampData> timestampMicros() {
+    return TimestampMicrosReader.INSTANCE;
+  }
+
+  static ValueReader<DecimalData> decimal(ValueReader<byte[]> unscaledReader, int precision, int scale) {
+    return new DecimalReader(unscaledReader, precision, scale);
+  }
+
+  static ValueReader<ArrayData> array(ValueReader<?> elementReader) {
+    return new ArrayReader(elementReader);
+  }
+
+  static ValueReader<MapData> arrayMap(ValueReader<?> keyReader,
+                                                 ValueReader<?> valueReader) {

Review comment:
       Nit: indentation is off.

##########
File path: flink/src/main/java/org/apache/iceberg/flink/data/FlinkValueReaders.java
##########
@@ -0,0 +1,314 @@
+/*
+ * 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.flink.data;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.util.Utf8;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.data.GenericMapData;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.MapData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.iceberg.avro.ValueReader;
+import org.apache.iceberg.avro.ValueReaders;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.types.Types;
+
+public class FlinkValueReaders {
+
+  private FlinkValueReaders() {}
+
+  static ValueReader<StringData> strings() {
+    return StringReader.INSTANCE;
+  }
+
+  static ValueReader<StringData> enums(List<String> symbols) {
+    return new EnumReader(symbols);
+  }
+
+  static ValueReader<byte[]> uuids() {
+    return ValueReaders.fixed(16);
+  }
+
+  static ValueReader<Integer> timeMicros() {
+    return TimeMicrosReader.INSTANCE;
+  }
+
+  static ValueReader<TimestampData> timestampMills() {
+    return TimestampMillsReader.INSTANCE;
+  }
+
+  static ValueReader<TimestampData> timestampMicros() {
+    return TimestampMicrosReader.INSTANCE;
+  }
+
+  static ValueReader<DecimalData> decimal(ValueReader<byte[]> unscaledReader, int precision, int scale) {
+    return new DecimalReader(unscaledReader, precision, scale);
+  }
+
+  static ValueReader<ArrayData> array(ValueReader<?> elementReader) {
+    return new ArrayReader(elementReader);
+  }
+
+  static ValueReader<MapData> arrayMap(ValueReader<?> keyReader,
+                                                 ValueReader<?> valueReader) {
+    return new ArrayMapReader(keyReader, valueReader);
+  }
+
+  static ValueReader<MapData> map(ValueReader<?> keyReader, ValueReader<?> valueReader) {
+    return new MapReader(keyReader, valueReader);
+  }
+
+  static ValueReader<RowData> struct(List<ValueReader<?>> readers, Types.StructType struct,
+                                         Map<Integer, ?> idToConstant) {
+    return new StructReader(readers, struct, idToConstant);
+  }
+
+  private static class StringReader implements ValueReader<StringData> {
+    private static final StringReader INSTANCE = new StringReader();
+
+    private StringReader() {
+    }
+
+    @Override
+    public StringData read(Decoder decoder, Object reuse) throws IOException {
+      // use the decoder's readString(Utf8) method because it may be a resolving decoder
+      Utf8 utf8 = null;
+      if (reuse instanceof StringData) {
+        utf8 = new Utf8(((StringData) reuse).toBytes());
+      }
+
+      Utf8 string = decoder.readString(utf8);
+      return StringData.fromBytes(string.getBytes(), 0, string.getByteLength());
+    }
+  }
+
+  private static class EnumReader implements ValueReader<StringData> {
+    private final StringData[] symbols;
+
+    private EnumReader(List<String> symbols) {
+      this.symbols = new StringData[symbols.size()];
+      for (int i = 0; i < this.symbols.length; i += 1) {
+        this.symbols[i] = StringData.fromBytes(symbols.get(i).getBytes(StandardCharsets.UTF_8));
+      }
+    }
+
+    @Override
+    public StringData read(Decoder decoder, Object ignore) throws IOException {
+      int index = decoder.readEnum();
+      return symbols[index];
+    }
+  }
+
+  private static class DecimalReader implements ValueReader<DecimalData> {
+    private final ValueReader<byte[]> bytesReader;
+    private final int precision;
+    private final int scale;
+
+    private DecimalReader(ValueReader<byte[]> bytesReader, int precision, int scale) {
+      this.bytesReader = bytesReader;
+      this.precision = precision;
+      this.scale = scale;
+    }
+
+    @Override
+    public DecimalData read(Decoder decoder, Object reuse) throws IOException {
+      byte[] bytes = bytesReader.read(decoder, null);
+      return DecimalData.fromBigDecimal(new BigDecimal(new BigInteger(bytes), scale), precision, scale);
+    }
+  }
+
+  private static class TimeMicrosReader implements ValueReader<Integer> {
+    private static final TimeMicrosReader INSTANCE = new TimeMicrosReader();
+
+    @Override
+    public Integer read(Decoder decoder, Object reuse) throws IOException {
+      long micros = decoder.readLong();
+      // Flink only support time mills, just erase micros.
+      return (int) (micros / 1000);
+    }
+  }
+
+  private static class TimestampMillsReader implements ValueReader<TimestampData> {
+    private static final TimestampMillsReader INSTANCE = new TimestampMillsReader();
+
+    @Override
+    public TimestampData read(Decoder decoder, Object reuse) throws IOException {
+      // TODO Do we need to consider time zones.
+      return TimestampData.fromEpochMillis(decoder.readLong());
+    }
+  }
+
+  private static class TimestampMicrosReader implements ValueReader<TimestampData> {
+    private static final TimestampMicrosReader INSTANCE = new TimestampMicrosReader();
+
+    @Override
+    public TimestampData read(Decoder decoder, Object reuse) throws IOException {
+      // TODO Do we need to consider time zones.

Review comment:
       Time zones are left to the processing engine. It is up to the engine to convert times to concrete values for storage and from concrete values for display. Iceberg's responsibility is to return the value without modification.

##########
File path: flink/src/main/java/org/apache/iceberg/flink/data/FlinkValueWriters.java
##########
@@ -0,0 +1,274 @@
+/*
+ * 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.flink.data;
+
+import java.io.IOException;
+import java.lang.reflect.Array;
+import java.math.BigDecimal;
+import java.util.List;
+import org.apache.avro.io.Encoder;
+import org.apache.avro.util.Utf8;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.MapData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.iceberg.avro.ValueWriter;
+import org.apache.iceberg.avro.ValueWriters;
+import org.apache.iceberg.relocated.com.google.common.base.Preconditions;
+import org.apache.iceberg.types.TypeUtil;
+
+public class FlinkValueWriters {
+
+  private FlinkValueWriters() {}
+
+  static ValueWriter<StringData> strings() {
+    return StringWriter.INSTANCE;
+  }
+
+  static ValueWriter<byte[]> uuids() {
+    return ValueWriters.fixed(16);
+  }
+
+  static ValueWriter<Integer> timeMicros() {
+    return TimeMicrosWriter.INSTANCE;
+  }
+
+  static ValueWriter<TimestampData> timestampMicros() {
+    return TimestampMicrosWriter.INSTANCE;
+  }
+
+  static ValueWriter<DecimalData> decimal(int precision, int scale) {
+    return new DecimalWriter(precision, scale);
+  }
+
+  static <T> ValueWriter<ArrayData> array(ValueWriter<T> elementWriter, LogicalType elementType) {
+    return new ArrayWriter<>(elementWriter, elementType);
+  }
+
+  static <K, V> ValueWriter<MapData> arrayMap(
+      ValueWriter<K> keyWriter, LogicalType keyType, ValueWriter<V> valueWriter, LogicalType valueType) {
+    return new ArrayMapWriter<>(keyWriter, keyType, valueWriter, valueType);
+  }
+
+  static <K, V> ValueWriter<MapData> map(
+      ValueWriter<K> keyWriter, LogicalType keyType, ValueWriter<V> valueWriter, LogicalType valueType) {
+    return new MapWriter<>(keyWriter, keyType, valueWriter, valueType);
+  }
+
+  static ValueWriter<RowData> row(List<ValueWriter<?>> writers, List<LogicalType> types) {
+    return new RowWriter(writers, types);
+  }
+
+  private static class StringWriter implements ValueWriter<StringData> {
+    private static final StringWriter INSTANCE = new StringWriter();
+
+    private StringWriter() {
+    }
+
+    @Override
+    public void write(StringData s, Encoder encoder) throws IOException {
+      // toBytes is cheaper than Avro calling toString, which incurs encoding costs
+      encoder.writeString(new Utf8(s.toBytes()));
+    }
+  }
+
+  private static class DecimalWriter implements ValueWriter<DecimalData> {
+    private final int precision;
+    private final int scale;
+    private final int length;
+    private final ThreadLocal<byte[]> bytes;
+
+    private DecimalWriter(int precision, int scale) {
+      this.precision = precision;
+      this.scale = scale;
+      this.length = TypeUtil.decimalRequiredBytes(precision);
+      this.bytes = ThreadLocal.withInitial(() -> new byte[length]);
+    }
+
+    @Override
+    public void write(DecimalData d, Encoder encoder) throws IOException {
+      Preconditions.checkArgument(d.scale() == scale,
+          "Cannot write value as decimal(%s,%s), wrong scale: %s", precision, scale, d);
+      Preconditions.checkArgument(d.precision() <= precision,
+          "Cannot write value as decimal(%s,%s), too large: %s", precision, scale, d);
+
+      BigDecimal decimal = d.toBigDecimal();
+
+      byte fillByte = (byte) (decimal.signum() < 0 ? 0xFF : 0x00);

Review comment:
       Can we move this logic into a common `DecimalUtil` method? I think we have quite a few copies of it.




----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] JingsongLi commented on a change in pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
JingsongLi commented on a change in pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#discussion_r465463522



##########
File path: flink/src/main/java/org/apache/iceberg/flink/TaskWriterFactory.java
##########
@@ -103,7 +103,7 @@ protected PartitionKey partition(Row row) {
 
           case AVRO:
             return Avro.write(outputFile)
-                .createWriterFunc(FlinkAvroWriter::new)
+                .createWriterFunc(ignore -> new FlinkAvroWriter(FlinkSchemaUtil.convert(schema)))

Review comment:
       Removed this and add comment:
   `TODO add AVRO once the RowDataWrapper are ready.`




----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] rdblue commented on a change in pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
rdblue commented on a change in pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#discussion_r465982002



##########
File path: flink/src/main/java/org/apache/iceberg/flink/data/FlinkValueReaders.java
##########
@@ -0,0 +1,312 @@
+/*
+ * 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.flink.data;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Map;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.util.Utf8;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.data.GenericMapData;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.MapData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.iceberg.avro.ValueReader;
+import org.apache.iceberg.avro.ValueReaders;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.types.Types;
+
+public class FlinkValueReaders {
+
+  private FlinkValueReaders() {}
+
+  static ValueReader<StringData> strings() {
+    return StringReader.INSTANCE;
+  }
+
+  static ValueReader<StringData> enums(List<String> symbols) {
+    return new EnumReader(symbols);
+  }
+
+  static ValueReader<byte[]> uuids() {
+    return ValueReaders.fixed(16);
+  }
+
+  static ValueReader<Integer> timeMicros() {
+    return TimeMicrosReader.INSTANCE;
+  }
+
+  static ValueReader<TimestampData> timestampMills() {
+    return TimestampMillsReader.INSTANCE;
+  }
+
+  static ValueReader<TimestampData> timestampMicros() {
+    return TimestampMicrosReader.INSTANCE;
+  }
+
+  static ValueReader<DecimalData> decimal(ValueReader<byte[]> unscaledReader, int precision, int scale) {
+    return new DecimalReader(unscaledReader, precision, scale);
+  }
+
+  static ValueReader<ArrayData> array(ValueReader<?> elementReader) {
+    return new ArrayReader(elementReader);
+  }
+
+  static ValueReader<MapData> arrayMap(ValueReader<?> keyReader,
+                                       ValueReader<?> valueReader) {
+    return new ArrayMapReader(keyReader, valueReader);
+  }
+
+  static ValueReader<MapData> map(ValueReader<?> keyReader, ValueReader<?> valueReader) {
+    return new MapReader(keyReader, valueReader);
+  }
+
+  static ValueReader<RowData> struct(List<ValueReader<?>> readers, Types.StructType struct,
+                                     Map<Integer, ?> idToConstant) {
+    return new StructReader(readers, struct, idToConstant);
+  }
+
+  private static class StringReader implements ValueReader<StringData> {
+    private static final StringReader INSTANCE = new StringReader();
+
+    private StringReader() {
+    }
+
+    @Override
+    public StringData read(Decoder decoder, Object reuse) throws IOException {
+      // use the decoder's readString(Utf8) method because it may be a resolving decoder
+      Utf8 utf8 = null;
+      if (reuse instanceof StringData) {
+        utf8 = new Utf8(((StringData) reuse).toBytes());
+      }
+
+      Utf8 string = decoder.readString(utf8);
+      return StringData.fromBytes(string.getBytes(), 0, string.getByteLength());
+    }
+  }
+
+  private static class EnumReader implements ValueReader<StringData> {
+    private final StringData[] symbols;
+
+    private EnumReader(List<String> symbols) {
+      this.symbols = new StringData[symbols.size()];
+      for (int i = 0; i < this.symbols.length; i += 1) {
+        this.symbols[i] = StringData.fromBytes(symbols.get(i).getBytes(StandardCharsets.UTF_8));
+      }
+    }
+
+    @Override
+    public StringData read(Decoder decoder, Object ignore) throws IOException {
+      int index = decoder.readEnum();
+      return symbols[index];
+    }
+  }
+
+  private static class DecimalReader implements ValueReader<DecimalData> {
+    private final ValueReader<byte[]> bytesReader;
+    private final int precision;
+    private final int scale;
+
+    private DecimalReader(ValueReader<byte[]> bytesReader, int precision, int scale) {
+      this.bytesReader = bytesReader;
+      this.precision = precision;
+      this.scale = scale;
+    }
+
+    @Override
+    public DecimalData read(Decoder decoder, Object reuse) throws IOException {
+      byte[] bytes = bytesReader.read(decoder, null);
+      return DecimalData.fromBigDecimal(new BigDecimal(new BigInteger(bytes), scale), precision, scale);
+    }
+  }
+
+  private static class TimeMicrosReader implements ValueReader<Integer> {
+    private static final TimeMicrosReader INSTANCE = new TimeMicrosReader();
+
+    @Override
+    public Integer read(Decoder decoder, Object reuse) throws IOException {
+      long micros = decoder.readLong();
+      // Flink only support time mills, just erase micros.
+      return (int) (micros / 1000);
+    }
+  }
+
+  private static class TimestampMillsReader implements ValueReader<TimestampData> {
+    private static final TimestampMillsReader INSTANCE = new TimestampMillsReader();
+
+    @Override
+    public TimestampData read(Decoder decoder, Object reuse) throws IOException {
+      return TimestampData.fromEpochMillis(decoder.readLong());
+    }
+  }
+
+  private static class TimestampMicrosReader implements ValueReader<TimestampData> {
+    private static final TimestampMicrosReader INSTANCE = new TimestampMicrosReader();
+
+    @Override
+    public TimestampData read(Decoder decoder, Object reuse) throws IOException {
+      long micros = decoder.readLong();
+      long mills = micros / 1000;
+      int nanos = ((int) (micros % 1000)) * 1000;
+      if (nanos < 0) {
+        nanos += 1_000_000;
+        mills -= 1;
+      }

Review comment:
       @openinx, that might influence fixing the timestamp types in ORC!




----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] JingsongLi commented on a change in pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
JingsongLi commented on a change in pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#discussion_r465468004



##########
File path: flink/src/main/java/org/apache/iceberg/flink/data/FlinkValueReaders.java
##########
@@ -0,0 +1,312 @@
+/*
+ * 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.flink.data;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Map;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.util.Utf8;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.data.GenericMapData;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.MapData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.iceberg.avro.ValueReader;
+import org.apache.iceberg.avro.ValueReaders;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.types.Types;
+
+public class FlinkValueReaders {
+
+  private FlinkValueReaders() {}
+
+  static ValueReader<StringData> strings() {
+    return StringReader.INSTANCE;
+  }
+
+  static ValueReader<StringData> enums(List<String> symbols) {
+    return new EnumReader(symbols);
+  }
+
+  static ValueReader<byte[]> uuids() {
+    return ValueReaders.fixed(16);
+  }
+
+  static ValueReader<Integer> timeMicros() {
+    return TimeMicrosReader.INSTANCE;
+  }
+
+  static ValueReader<TimestampData> timestampMills() {
+    return TimestampMillsReader.INSTANCE;
+  }
+
+  static ValueReader<TimestampData> timestampMicros() {
+    return TimestampMicrosReader.INSTANCE;
+  }
+
+  static ValueReader<DecimalData> decimal(ValueReader<byte[]> unscaledReader, int precision, int scale) {
+    return new DecimalReader(unscaledReader, precision, scale);
+  }
+
+  static ValueReader<ArrayData> array(ValueReader<?> elementReader) {
+    return new ArrayReader(elementReader);
+  }
+
+  static ValueReader<MapData> arrayMap(ValueReader<?> keyReader,
+                                       ValueReader<?> valueReader) {
+    return new ArrayMapReader(keyReader, valueReader);
+  }
+
+  static ValueReader<MapData> map(ValueReader<?> keyReader, ValueReader<?> valueReader) {
+    return new MapReader(keyReader, valueReader);
+  }
+
+  static ValueReader<RowData> struct(List<ValueReader<?>> readers, Types.StructType struct,
+                                     Map<Integer, ?> idToConstant) {
+    return new StructReader(readers, struct, idToConstant);
+  }
+
+  private static class StringReader implements ValueReader<StringData> {
+    private static final StringReader INSTANCE = new StringReader();
+
+    private StringReader() {
+    }
+
+    @Override
+    public StringData read(Decoder decoder, Object reuse) throws IOException {
+      // use the decoder's readString(Utf8) method because it may be a resolving decoder
+      Utf8 utf8 = null;
+      if (reuse instanceof StringData) {
+        utf8 = new Utf8(((StringData) reuse).toBytes());
+      }
+
+      Utf8 string = decoder.readString(utf8);
+      return StringData.fromBytes(string.getBytes(), 0, string.getByteLength());
+    }
+  }
+
+  private static class EnumReader implements ValueReader<StringData> {
+    private final StringData[] symbols;
+
+    private EnumReader(List<String> symbols) {
+      this.symbols = new StringData[symbols.size()];
+      for (int i = 0; i < this.symbols.length; i += 1) {
+        this.symbols[i] = StringData.fromBytes(symbols.get(i).getBytes(StandardCharsets.UTF_8));
+      }
+    }
+
+    @Override
+    public StringData read(Decoder decoder, Object ignore) throws IOException {
+      int index = decoder.readEnum();
+      return symbols[index];
+    }
+  }
+
+  private static class DecimalReader implements ValueReader<DecimalData> {
+    private final ValueReader<byte[]> bytesReader;
+    private final int precision;
+    private final int scale;
+
+    private DecimalReader(ValueReader<byte[]> bytesReader, int precision, int scale) {
+      this.bytesReader = bytesReader;
+      this.precision = precision;
+      this.scale = scale;
+    }
+
+    @Override
+    public DecimalData read(Decoder decoder, Object reuse) throws IOException {
+      byte[] bytes = bytesReader.read(decoder, null);
+      return DecimalData.fromBigDecimal(new BigDecimal(new BigInteger(bytes), scale), precision, scale);
+    }
+  }
+
+  private static class TimeMicrosReader implements ValueReader<Integer> {
+    private static final TimeMicrosReader INSTANCE = new TimeMicrosReader();
+
+    @Override
+    public Integer read(Decoder decoder, Object reuse) throws IOException {
+      long micros = decoder.readLong();
+      // Flink only support time mills, just erase micros.
+      return (int) (micros / 1000);
+    }
+  }
+
+  private static class TimestampMillsReader implements ValueReader<TimestampData> {
+    private static final TimestampMillsReader INSTANCE = new TimestampMillsReader();
+
+    @Override
+    public TimestampData read(Decoder decoder, Object reuse) throws IOException {
+      return TimestampData.fromEpochMillis(decoder.readLong());
+    }
+  }
+
+  private static class TimestampMicrosReader implements ValueReader<TimestampData> {
+    private static final TimestampMicrosReader INSTANCE = new TimestampMicrosReader();
+
+    @Override
+    public TimestampData read(Decoder decoder, Object reuse) throws IOException {
+      long micros = decoder.readLong();
+      long mills = micros / 1000;
+      int nanos = ((int) (micros % 1000)) * 1000;
+      if (nanos < 0) {
+        nanos += 1_000_000;
+        mills -= 1;
+      }

Review comment:
       I wrote a simple benchmark, `Math.floor**` will be 10% slower.




----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] openinx commented on a change in pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
openinx commented on a change in pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#discussion_r464934678



##########
File path: flink/src/main/java/org/apache/iceberg/flink/TaskWriterFactory.java
##########
@@ -103,7 +103,7 @@ protected PartitionKey partition(Row row) {
 
           case AVRO:
             return Avro.write(outputFile)
-                .createWriterFunc(FlinkAvroWriter::new)
+                .createWriterFunc(ignore -> new FlinkAvroWriter(FlinkSchemaUtil.convert(schema)))

Review comment:
       nit: this should not work,  because the FlinkAvroWriter is a writer with `RowData` parameter type,  while we need a `Row` type `FileAppender`.   we may could remove this avro write building.  Once the `RowDataWrapper` is ready,  we would rewrite this part. 

##########
File path: flink/src/main/java/org/apache/iceberg/flink/data/FlinkValueReaders.java
##########
@@ -0,0 +1,312 @@
+/*
+ * 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.flink.data;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Map;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.util.Utf8;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.data.GenericMapData;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.MapData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.iceberg.avro.ValueReader;
+import org.apache.iceberg.avro.ValueReaders;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.types.Types;
+
+public class FlinkValueReaders {
+
+  private FlinkValueReaders() {}
+
+  static ValueReader<StringData> strings() {
+    return StringReader.INSTANCE;
+  }
+
+  static ValueReader<StringData> enums(List<String> symbols) {
+    return new EnumReader(symbols);
+  }
+
+  static ValueReader<byte[]> uuids() {
+    return ValueReaders.fixed(16);
+  }
+
+  static ValueReader<Integer> timeMicros() {
+    return TimeMicrosReader.INSTANCE;
+  }
+
+  static ValueReader<TimestampData> timestampMills() {
+    return TimestampMillsReader.INSTANCE;
+  }
+
+  static ValueReader<TimestampData> timestampMicros() {
+    return TimestampMicrosReader.INSTANCE;
+  }
+
+  static ValueReader<DecimalData> decimal(ValueReader<byte[]> unscaledReader, int precision, int scale) {
+    return new DecimalReader(unscaledReader, precision, scale);
+  }
+
+  static ValueReader<ArrayData> array(ValueReader<?> elementReader) {
+    return new ArrayReader(elementReader);
+  }
+
+  static ValueReader<MapData> arrayMap(ValueReader<?> keyReader,
+                                       ValueReader<?> valueReader) {
+    return new ArrayMapReader(keyReader, valueReader);
+  }
+
+  static ValueReader<MapData> map(ValueReader<?> keyReader, ValueReader<?> valueReader) {
+    return new MapReader(keyReader, valueReader);
+  }
+
+  static ValueReader<RowData> struct(List<ValueReader<?>> readers, Types.StructType struct,
+                                     Map<Integer, ?> idToConstant) {
+    return new StructReader(readers, struct, idToConstant);
+  }
+
+  private static class StringReader implements ValueReader<StringData> {
+    private static final StringReader INSTANCE = new StringReader();
+
+    private StringReader() {
+    }
+
+    @Override
+    public StringData read(Decoder decoder, Object reuse) throws IOException {
+      // use the decoder's readString(Utf8) method because it may be a resolving decoder
+      Utf8 utf8 = null;
+      if (reuse instanceof StringData) {
+        utf8 = new Utf8(((StringData) reuse).toBytes());
+      }
+
+      Utf8 string = decoder.readString(utf8);
+      return StringData.fromBytes(string.getBytes(), 0, string.getByteLength());
+    }
+  }
+
+  private static class EnumReader implements ValueReader<StringData> {
+    private final StringData[] symbols;
+
+    private EnumReader(List<String> symbols) {
+      this.symbols = new StringData[symbols.size()];
+      for (int i = 0; i < this.symbols.length; i += 1) {
+        this.symbols[i] = StringData.fromBytes(symbols.get(i).getBytes(StandardCharsets.UTF_8));
+      }
+    }
+
+    @Override
+    public StringData read(Decoder decoder, Object ignore) throws IOException {
+      int index = decoder.readEnum();
+      return symbols[index];
+    }
+  }
+
+  private static class DecimalReader implements ValueReader<DecimalData> {
+    private final ValueReader<byte[]> bytesReader;
+    private final int precision;
+    private final int scale;
+
+    private DecimalReader(ValueReader<byte[]> bytesReader, int precision, int scale) {
+      this.bytesReader = bytesReader;
+      this.precision = precision;
+      this.scale = scale;
+    }
+
+    @Override
+    public DecimalData read(Decoder decoder, Object reuse) throws IOException {
+      byte[] bytes = bytesReader.read(decoder, null);
+      return DecimalData.fromBigDecimal(new BigDecimal(new BigInteger(bytes), scale), precision, scale);
+    }
+  }
+
+  private static class TimeMicrosReader implements ValueReader<Integer> {
+    private static final TimeMicrosReader INSTANCE = new TimeMicrosReader();
+
+    @Override
+    public Integer read(Decoder decoder, Object reuse) throws IOException {
+      long micros = decoder.readLong();
+      // Flink only support time mills, just erase micros.
+      return (int) (micros / 1000);
+    }
+  }
+
+  private static class TimestampMillsReader implements ValueReader<TimestampData> {
+    private static final TimestampMillsReader INSTANCE = new TimestampMillsReader();
+
+    @Override
+    public TimestampData read(Decoder decoder, Object reuse) throws IOException {
+      return TimestampData.fromEpochMillis(decoder.readLong());
+    }
+  }
+
+  private static class TimestampMicrosReader implements ValueReader<TimestampData> {
+    private static final TimestampMicrosReader INSTANCE = new TimestampMicrosReader();
+
+    @Override
+    public TimestampData read(Decoder decoder, Object reuse) throws IOException {
+      long micros = decoder.readLong();
+      long mills = micros / 1000;
+      int nanos = ((int) (micros % 1000)) * 1000;
+      if (nanos < 0) {
+        nanos += 1_000_000;
+        mills -= 1;
+      }

Review comment:
       Here it's simple to use `floorDiv` and `floorMod` : 
   ```java
   long mills = Math.floorDiv(micros, 1000);
   int nanos = Math.floorMod(micros, 1000)*1000;
   ```

##########
File path: flink/src/main/java/org/apache/iceberg/flink/data/FlinkValueReaders.java
##########
@@ -0,0 +1,312 @@
+/*
+ * 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.flink.data;
+
+import java.io.IOException;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.charset.StandardCharsets;
+import java.util.List;
+import java.util.Map;
+import org.apache.avro.io.Decoder;
+import org.apache.avro.util.Utf8;
+import org.apache.flink.table.data.ArrayData;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.GenericArrayData;
+import org.apache.flink.table.data.GenericMapData;
+import org.apache.flink.table.data.GenericRowData;
+import org.apache.flink.table.data.MapData;
+import org.apache.flink.table.data.RowData;
+import org.apache.flink.table.data.StringData;
+import org.apache.flink.table.data.TimestampData;
+import org.apache.iceberg.avro.ValueReader;
+import org.apache.iceberg.avro.ValueReaders;
+import org.apache.iceberg.relocated.com.google.common.collect.Lists;
+import org.apache.iceberg.relocated.com.google.common.collect.Maps;
+import org.apache.iceberg.types.Types;
+
+public class FlinkValueReaders {
+
+  private FlinkValueReaders() {}
+
+  static ValueReader<StringData> strings() {
+    return StringReader.INSTANCE;
+  }
+
+  static ValueReader<StringData> enums(List<String> symbols) {
+    return new EnumReader(symbols);
+  }
+
+  static ValueReader<byte[]> uuids() {
+    return ValueReaders.fixed(16);
+  }
+
+  static ValueReader<Integer> timeMicros() {
+    return TimeMicrosReader.INSTANCE;
+  }
+
+  static ValueReader<TimestampData> timestampMills() {
+    return TimestampMillsReader.INSTANCE;
+  }
+
+  static ValueReader<TimestampData> timestampMicros() {
+    return TimestampMicrosReader.INSTANCE;
+  }
+
+  static ValueReader<DecimalData> decimal(ValueReader<byte[]> unscaledReader, int precision, int scale) {
+    return new DecimalReader(unscaledReader, precision, scale);
+  }
+
+  static ValueReader<ArrayData> array(ValueReader<?> elementReader) {
+    return new ArrayReader(elementReader);
+  }
+
+  static ValueReader<MapData> arrayMap(ValueReader<?> keyReader,
+                                       ValueReader<?> valueReader) {
+    return new ArrayMapReader(keyReader, valueReader);
+  }
+
+  static ValueReader<MapData> map(ValueReader<?> keyReader, ValueReader<?> valueReader) {
+    return new MapReader(keyReader, valueReader);
+  }
+
+  static ValueReader<RowData> struct(List<ValueReader<?>> readers, Types.StructType struct,
+                                     Map<Integer, ?> idToConstant) {
+    return new StructReader(readers, struct, idToConstant);
+  }
+
+  private static class StringReader implements ValueReader<StringData> {
+    private static final StringReader INSTANCE = new StringReader();
+
+    private StringReader() {
+    }
+
+    @Override
+    public StringData read(Decoder decoder, Object reuse) throws IOException {
+      // use the decoder's readString(Utf8) method because it may be a resolving decoder
+      Utf8 utf8 = null;
+      if (reuse instanceof StringData) {
+        utf8 = new Utf8(((StringData) reuse).toBytes());
+      }
+
+      Utf8 string = decoder.readString(utf8);
+      return StringData.fromBytes(string.getBytes(), 0, string.getByteLength());
+    }
+  }
+
+  private static class EnumReader implements ValueReader<StringData> {
+    private final StringData[] symbols;
+
+    private EnumReader(List<String> symbols) {
+      this.symbols = new StringData[symbols.size()];
+      for (int i = 0; i < this.symbols.length; i += 1) {
+        this.symbols[i] = StringData.fromBytes(symbols.get(i).getBytes(StandardCharsets.UTF_8));
+      }
+    }
+
+    @Override
+    public StringData read(Decoder decoder, Object ignore) throws IOException {
+      int index = decoder.readEnum();
+      return symbols[index];
+    }
+  }
+
+  private static class DecimalReader implements ValueReader<DecimalData> {
+    private final ValueReader<byte[]> bytesReader;
+    private final int precision;
+    private final int scale;
+
+    private DecimalReader(ValueReader<byte[]> bytesReader, int precision, int scale) {
+      this.bytesReader = bytesReader;
+      this.precision = precision;
+      this.scale = scale;
+    }
+
+    @Override
+    public DecimalData read(Decoder decoder, Object reuse) throws IOException {
+      byte[] bytes = bytesReader.read(decoder, null);
+      return DecimalData.fromBigDecimal(new BigDecimal(new BigInteger(bytes), scale), precision, scale);
+    }
+  }
+
+  private static class TimeMicrosReader implements ValueReader<Integer> {
+    private static final TimeMicrosReader INSTANCE = new TimeMicrosReader();
+
+    @Override
+    public Integer read(Decoder decoder, Object reuse) throws IOException {
+      long micros = decoder.readLong();
+      // Flink only support time mills, just erase micros.
+      return (int) (micros / 1000);
+    }
+  }
+
+  private static class TimestampMillsReader implements ValueReader<TimestampData> {
+    private static final TimestampMillsReader INSTANCE = new TimestampMillsReader();
+
+    @Override
+    public TimestampData read(Decoder decoder, Object reuse) throws IOException {
+      return TimestampData.fromEpochMillis(decoder.readLong());
+    }
+  }
+
+  private static class TimestampMicrosReader implements ValueReader<TimestampData> {
+    private static final TimestampMicrosReader INSTANCE = new TimestampMicrosReader();
+
+    @Override
+    public TimestampData read(Decoder decoder, Object reuse) throws IOException {
+      long micros = decoder.readLong();
+      long mills = micros / 1000;
+      int nanos = ((int) (micros % 1000)) * 1000;
+      if (nanos < 0) {
+        nanos += 1_000_000;
+        mills -= 1;
+      }
+      return TimestampData.fromEpochMillis(mills, nanos);
+    }
+  }
+
+  private static class ArrayReader implements ValueReader<ArrayData> {
+    private final ValueReader<?> elementReader;
+    private final List<Object> reusedList = Lists.newArrayList();
+
+    private ArrayReader(ValueReader<?> elementReader) {
+      this.elementReader = elementReader;
+    }
+
+    @Override
+    public GenericArrayData read(Decoder decoder, Object reuse) throws IOException {
+      reusedList.clear();
+      long chunkLength = decoder.readArrayStart();
+
+      while (chunkLength > 0) {
+        for (int i = 0; i < chunkLength; i += 1) {
+          reusedList.add(elementReader.read(decoder, null));
+        }
+
+        chunkLength = decoder.arrayNext();
+      }
+
+      // this will convert the list to an array so it is okay to reuse the list
+      return new GenericArrayData(reusedList.toArray());
+    }
+  }
+
+  private static MapData kvArrayToMap(List<Object> keyList, List<Object> valueList) {
+    Map<Object, Object> map = Maps.newHashMap();
+    Object[] keys = keyList.toArray();
+    Object[] values = valueList.toArray();
+    for (int i = 0; i < keys.length; i++) {
+      map.put(keys[i], values[i]);
+    }
+
+    return new GenericMapData(map);
+  }
+
+  private static class ArrayMapReader implements ValueReader<MapData> {
+    private final ValueReader<?> keyReader;
+    private final ValueReader<?> valueReader;
+
+    private final List<Object> reusedKeyList = Lists.newArrayList();
+    private final List<Object> reusedValueList = Lists.newArrayList();
+
+    private ArrayMapReader(ValueReader<?> keyReader, ValueReader<?> valueReader) {
+      this.keyReader = keyReader;
+      this.valueReader = valueReader;
+    }
+
+    @Override
+    public MapData read(Decoder decoder, Object reuse) throws IOException {
+      reusedKeyList.clear();
+      reusedValueList.clear();
+
+      long chunkLength = decoder.readArrayStart();
+
+      while (chunkLength > 0) {
+        for (int i = 0; i < chunkLength; i += 1) {
+          reusedKeyList.add(keyReader.read(decoder, null));
+          reusedValueList.add(valueReader.read(decoder, null));
+        }
+
+        chunkLength = decoder.arrayNext();
+      }
+
+      return kvArrayToMap(reusedKeyList, reusedValueList);
+    }
+  }
+
+  private static class MapReader implements ValueReader<MapData> {
+    private final ValueReader<?> keyReader;
+    private final ValueReader<?> valueReader;
+
+    private final List<Object> reusedKeyList = Lists.newArrayList();
+    private final List<Object> reusedValueList = Lists.newArrayList();
+
+    private MapReader(ValueReader<?> keyReader, ValueReader<?> valueReader) {
+      this.keyReader = keyReader;
+      this.valueReader = valueReader;
+    }
+
+    @Override
+    public MapData read(Decoder decoder, Object reuse) throws IOException {
+      reusedKeyList.clear();
+      reusedValueList.clear();
+
+      long chunkLength = decoder.readMapStart();
+
+      while (chunkLength > 0) {
+        for (int i = 0; i < chunkLength; i += 1) {
+          reusedKeyList.add(keyReader.read(decoder, null));
+          reusedValueList.add(valueReader.read(decoder, null));
+        }
+
+        chunkLength = decoder.mapNext();
+      }
+
+      return kvArrayToMap(reusedKeyList, reusedValueList);
+    }
+  }
+
+  static class StructReader extends ValueReaders.StructReader<RowData> {
+    private final int numFields;
+
+    protected StructReader(List<ValueReader<?>> readers, Types.StructType struct, Map<Integer, ?> idToConstant) {

Review comment:
       No need to be protected. 




----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] JingsongLi commented on pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
JingsongLi commented on pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#issuecomment-669643732


   Thanks @rdblue for your patient review, I will continue to pay attention to and participate in the follow-up improvement.


----------------------------------------------------------------
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.

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



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


[GitHub] [iceberg] rdblue commented on pull request #1232: Flink: Using RowData to avro reader and writer

Posted by GitBox <gi...@apache.org>.
rdblue commented on pull request #1232:
URL: https://github.com/apache/iceberg/pull/1232#issuecomment-669499547


   @JingsongLi, this looks ready to go so I merged it. I think we can still improve some of the tests by validating the read and write paths separately and comparing records against Iceberg generics. But I believe that @chenjunjiedada is working on the validations or assert methods in another PR so we can get that done later.
   
   Thanks for working on this, it looks great.


----------------------------------------------------------------
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.

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



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