yuzelin commented on code in PR #1171:
URL: https://github.com/apache/incubator-paimon/pull/1171#discussion_r1204925874


##########
paimon-format/src/main/java/org/apache/paimon/format/avro/AvroToRowDataConverters.java:
##########
@@ -97,7 +103,7 @@ private static AvroToRowDataConverter 
createNullableConverter(DataType type) {
     }
 
     /** Creates a runtime converter which assuming input object is not null. */
-    private static AvroToRowDataConverter createConverter(DataType type) {
+    public static AvroToRowDataConverter createConverter(DataType type) {

Review Comment:
   Use `@VisibleForTesting` ?



##########
paimon-format/src/test/java/org/apache/paimon/format/avro/AvroFileFormatTest.java:
##########
@@ -89,7 +94,6 @@ public void testUnsupportedDataTypes() {
         dataFields.add(new DataField(index++, "timestamp_type", 
DataTypes.TIMESTAMP(6)));
 
         RowType rowType = new RowType(dataFields);
-        Assertions.assertThrows(
-                IllegalArgumentException.class, () -> 
fileFormat.validateDataFields(rowType));
+        Assertions.assertDoesNotThrow(() -> 
fileFormat.validateDataFields(rowType));

Review Comment:
   We can remove this method.



##########
paimon-format/src/test/java/org/apache/paimon/format/avro/AvroBulkFormatTest.java:
##########
@@ -49,35 +50,63 @@
 /** Tests for {@link AbstractAvroBulkFormat}. */
 class AvroBulkFormatTest {
 
+    private static final long TIMESTAMP = System.currentTimeMillis();
+
     private static final List<InternalRow> TEST_DATA =
             Arrays.asList(
                     // -------- batch 0, block start 232 --------
                     GenericRow.of(
                             BinaryString.fromString("AvroBulk"),
-                            BinaryString.fromString("FormatTest")),
+                            BinaryString.fromString("FormatTest"),
+                            Timestamp.fromEpochMillis(TIMESTAMP),
+                            Timestamp.fromMicros(TIMESTAMP * 1000 + 123),
+                            Timestamp.fromEpochMillis(TIMESTAMP),
+                            Timestamp.fromMicros(TIMESTAMP * 1000 + 123)),
                     GenericRow.of(
-                            BinaryString.fromString("Apache"), 
BinaryString.fromString("Paimon")),
+                            BinaryString.fromString("Apache"),
+                            BinaryString.fromString("Paimon"),
+                            Timestamp.fromEpochMillis(TIMESTAMP),
+                            Timestamp.fromMicros(TIMESTAMP * 1000 + 123),
+                            Timestamp.fromEpochMillis(TIMESTAMP),
+                            Timestamp.fromMicros(TIMESTAMP * 1000 + 123)),
                     GenericRow.of(
                             BinaryString.fromString(
                                     "永和九年,岁在癸丑,暮春之初,会于会稽山阴之兰亭,修禊事也。群贤毕至,少"
                                             + "长咸集。此地有崇山峻岭,茂林修竹,又有清流激湍,映带左右。引"
                                             + "以为流觞曲水,列坐其次。虽无丝竹管弦之盛,一觞一咏,亦足以畅"
                                             + "叙幽情。"),
-                            BinaryString.fromString("")),
+                            BinaryString.fromString(""),
+                            Timestamp.fromEpochMillis(TIMESTAMP),
+                            Timestamp.fromMicros(TIMESTAMP * 1000 + 123),
+                            Timestamp.fromEpochMillis(TIMESTAMP),
+                            Timestamp.fromMicros(TIMESTAMP * 1000 + 123)),
                     // -------- batch 1, block start 593 --------
                     GenericRow.of(
-                            BinaryString.fromString("File"), 
BinaryString.fromString("Format")),
+                            BinaryString.fromString("File"),
+                            BinaryString.fromString("Format"),
+                            Timestamp.fromEpochMillis(TIMESTAMP),
+                            Timestamp.fromMicros(TIMESTAMP * 1000 + 123),
+                            Timestamp.fromEpochMillis(TIMESTAMP),
+                            Timestamp.fromMicros(TIMESTAMP * 1000 + 123)),
                     GenericRow.of(
                             null,
                             BinaryString.fromString(
-                                    "This is a string with English, 中文 and 
even 🍎🍌🍑🥝🍍🥭🍐")),
+                                    "This is a string with English, 中文 and 
even 🍎🍌🍑🥝🍍🥭🍐"),
+                            Timestamp.fromEpochMillis(TIMESTAMP),
+                            Timestamp.fromMicros(TIMESTAMP * 1000 + 123),
+                            Timestamp.fromEpochMillis(TIMESTAMP),
+                            Timestamp.fromMicros(TIMESTAMP * 1000 + 123)),
                     // -------- batch 2, block start 705 --------
                     GenericRow.of(
                             BinaryString.fromString("block with"),
-                            BinaryString.fromString("only one record"))
+                            BinaryString.fromString("only one record"),
+                            Timestamp.fromEpochMillis(TIMESTAMP),
+                            Timestamp.fromMicros(TIMESTAMP * 1000 + 123),
+                            Timestamp.fromEpochMillis(TIMESTAMP),
+                            Timestamp.fromMicros(TIMESTAMP * 1000 + 123))
                     // -------- file length 752 --------
                     );
-    private static final List<Long> BLOCK_STARTS = Arrays.asList(233L, 595L, 
707L);
+    private static final List<Long> BLOCK_STARTS = Arrays.asList(689L, 1147L, 
1323L);

Review Comment:
   Can you correct the comments above (batch x, block start xxx) ?



##########
paimon-format/src/main/java/org/apache/paimon/format/avro/AvroToRowDataConverters.java:
##########
@@ -114,7 +120,23 @@ private static AvroToRowDataConverter 
createConverter(DataType type) {
             case TIME_WITHOUT_TIME_ZONE:
                 return AvroToRowDataConverters::convertToTime;
             case TIMESTAMP_WITHOUT_TIME_ZONE:
-                return AvroToRowDataConverters::convertToTimestamp;
+                int precision = ((TimestampType) type).getPrecision();
+                if (precision == 3) {
+                    return 
AvroToRowDataConverters::convertToTimestampFromMillis;
+                } else if (precision == 6) {
+                    return 
AvroToRowDataConverters::convertToTimestampFromMicros;

Review Comment:
   Should the constraints be `precision <= 3` and `precision <= 6` ?
   I changed the constraints and and test in 
`AvroToRowDataConvertersTest#testConvertToTimestamp`:
   `Assertions.assertEquals(NOW, 
AvroToRowDataConverters.convertToTimestamp(TS_MILLIS, 1));` 
   It works.



##########
paimon-format/src/main/java/org/apache/paimon/format/avro/RowDataToAvroConverters.java:
##########
@@ -142,7 +144,39 @@ public Object convert(Schema schema, Object object) {
 
                             @Override
                             public Object convert(Schema schema, Object 
object) {
-                                return ((Timestamp) 
object).toInstant().toEpochMilli();
+                                LogicalType logicalType = 
schema.getLogicalType();
+
+                                if (logicalType instanceof 
LogicalTypes.TimestampMillis) {
+                                    return ((Timestamp) 
object).toInstant().toEpochMilli();
+                                } else if (logicalType instanceof 
LogicalTypes.TimestampMicros) {
+                                    return ((Timestamp) 
object).toInstant().toEpochMilli() * 1000
+                                            + ((Timestamp) 
object).getNanoOfMillisecond() / 1000;

Review Comment:
   Seems it's equal to `Timestamp#toMicros`.



##########
paimon-format/src/test/java/org/apache/paimon/format/avro/RowDataToAvroConvertersTest.java:
##########
@@ -0,0 +1,57 @@
+/*
+ * 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.paimon.format.avro;
+
+import org.apache.paimon.data.Timestamp;
+import org.apache.paimon.types.DataTypes;
+import org.apache.paimon.types.LocalZonedTimestampType;
+
+import org.apache.avro.Schema;
+import org.junit.jupiter.api.Assertions;
+import org.junit.jupiter.api.Test;
+
+import java.time.LocalDate;
+import java.time.LocalTime;
+import java.time.OffsetDateTime;
+import java.time.ZoneOffset;
+
+/** Test for row data to avro converters. */
+public class RowDataToAvroConvertersTest {
+
+    @Test
+    public void testTimestampWithTimeType() {
+        LocalZonedTimestampType zonedTimestampType = 
DataTypes.TIMESTAMP_WITH_LOCAL_TIME_ZONE(3);
+        RowDataToAvroConverters.RowDataToAvroConverter rowDataToAvroConverter =
+                RowDataToAvroConverters.createConverter(zonedTimestampType);
+
+        OffsetDateTime offsetDateTime =
+                OffsetDateTime.of(LocalDate.of(2023, 1, 1), LocalTime.of(0, 0, 
0), ZoneOffset.UTC);
+
+        Schema schema = 
AvroSchemaConverter.convertToSchema(zonedTimestampType);
+        long timestamp = offsetDateTime.toInstant().toEpochMilli();
+        Timestamp millis = Timestamp.fromEpochMillis(timestamp);
+        Object converted = rowDataToAvroConverter.convert(schema, millis);
+
+        AvroToRowDataConverters.AvroToRowDataConverter avroToRowDataConverter =
+                AvroToRowDataConverters.createConverter(zonedTimestampType);
+
+        Object original = avroToRowDataConverter.convert(converted);
+        Assertions.assertEquals(millis, (Timestamp) original);

Review Comment:
   The cast `(Timestamp)` is redundant.



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

To unsubscribe, e-mail: [email protected]

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

Reply via email to