mark-bathori commented on code in PR #6368:
URL: https://github.com/apache/nifi/pull/6368#discussion_r982318544


##########
nifi-nar-bundles/nifi-iceberg-bundle/nifi-iceberg-processors/src/main/java/org/apache/nifi/processors/iceberg/converter/IcebergRecordConverter.java:
##########
@@ -0,0 +1,190 @@
+/*
+ * 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.nifi.processors.iceberg.converter;
+
+import org.apache.commons.lang.Validate;
+import org.apache.iceberg.FileFormat;
+import org.apache.iceberg.Schema;
+import org.apache.iceberg.data.GenericRecord;
+import org.apache.iceberg.schema.SchemaWithPartnerVisitor;
+import org.apache.iceberg.types.Type;
+import org.apache.iceberg.types.Types;
+import org.apache.nifi.serialization.record.DataType;
+import org.apache.nifi.serialization.record.Record;
+import org.apache.nifi.serialization.record.RecordField;
+import org.apache.nifi.serialization.record.RecordFieldType;
+import org.apache.nifi.serialization.record.RecordSchema;
+import org.apache.nifi.serialization.record.type.ArrayDataType;
+import org.apache.nifi.serialization.record.type.MapDataType;
+import org.apache.nifi.serialization.record.type.RecordDataType;
+
+import java.util.List;
+import java.util.Optional;
+
+/**
+ * This class is responsible for schema traversal and data conversion between 
NiFi and Iceberg internal record structure.
+ */
+public class IcebergRecordConverter {
+
+    private final DataConverter<Record, GenericRecord> converter;
+
+    public GenericRecord convert(Record record) {
+        return converter.convert(record);
+    }
+
+    @SuppressWarnings("unchecked")
+    public IcebergRecordConverter(Schema schema, RecordSchema recordSchema, 
FileFormat fileFormat) {
+        this.converter = (DataConverter<Record, GenericRecord>) 
IcebergSchemaVisitor.visit(schema, new RecordDataType(recordSchema), 
fileFormat);
+    }
+
+    private static class IcebergSchemaVisitor extends 
SchemaWithPartnerVisitor<DataType, DataConverter<?, ?>> {
+
+        public static DataConverter<?, ?> visit(Schema schema, RecordDataType 
recordDataType, FileFormat fileFormat) {
+            return visit(schema, recordDataType, new IcebergSchemaVisitor(), 
new IcebergPartnerAccessors(fileFormat));
+        }
+
+        @Override
+        public DataConverter<?, ?> schema(Schema schema, DataType dataType, 
DataConverter<?, ?> converter) {
+            return converter;
+        }
+
+        @Override
+        public DataConverter<?, ?> field(Types.NestedField field, DataType 
dataType, DataConverter<?, ?> converter) {
+            return converter;
+        }
+
+        @Override
+        public DataConverter<?, ?> primitive(Type.PrimitiveType type, DataType 
dataType) {
+            if (type.typeId() != null) {
+                switch (type.typeId()) {
+                    case BOOLEAN:
+                    case INTEGER:
+                    case LONG:
+                    case FLOAT:
+                    case DOUBLE:
+                    case DATE:
+                    case STRING:
+                        return 
GenericDataConverters.SameTypeConverter.INSTANCE;
+                    case TIME:
+                        return GenericDataConverters.TimeConverter.INSTANCE;
+                    case TIMESTAMP:
+                        Types.TimestampType timestampType = 
(Types.TimestampType) type;
+                        if (timestampType.shouldAdjustToUTC()) {
+                            return 
GenericDataConverters.TimestampWithTimezoneConverter.INSTANCE;
+                        }
+                        return 
GenericDataConverters.TimestampConverter.INSTANCE;
+                    case UUID:
+                        UUIDDataType uuidType = (UUIDDataType) dataType;
+                        if (uuidType.getFileFormat() == FileFormat.PARQUET) {
+                            return 
GenericDataConverters.UUIDtoByteArrayConverter.INSTANCE;
+                        }
+                        return 
GenericDataConverters.SameTypeConverter.INSTANCE;
+                    case FIXED:
+                        Types.FixedType fixedType = (Types.FixedType) type;
+                        return new 
GenericDataConverters.FixedConverter(fixedType.length());
+                    case BINARY:
+                        return GenericDataConverters.BinaryConverter.INSTANCE;
+                    case DECIMAL:
+                        Types.DecimalType decimalType = (Types.DecimalType) 
type;
+                        return new 
GenericDataConverters.BigDecimalConverter(decimalType.precision(), 
decimalType.scale());
+                    default:
+                        throw new UnsupportedOperationException("Unsupported 
type: " + type.typeId());
+                }
+            }
+            return null;

Review Comment:
   The original method that is overridden returns with null by default, so 
that's why I followed this approach when `PrimitiveType` doesn't have a type id.



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