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/29 07:40:16 UTC

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

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