danny0405 commented on code in PR #18741:
URL: https://github.com/apache/hudi/pull/18741#discussion_r3301197292


##########
hudi-flink-datasource/hudi-flink/src/main/java/org/apache/hudi/table/format/FlinkRowDataReaderContext.java:
##########
@@ -98,18 +99,27 @@ public ClosableIterator<RowData> getFileRecordIterator(
       HoodieSchema dataSchema,
       HoodieSchema requiredSchema,
       HoodieStorage storage) throws IOException {
-    if 
(filePath.toString().endsWith(HoodieFileFormat.LANCE.getFileExtension())) {
-      throw new 
UnsupportedOperationException(HoodieFileFormat.LANCE_SPARK_ONLY_ERROR_MSG);
-    }
     boolean isLogFile = FSUtils.isLogFile(filePath);
     // disable schema evolution in fileReader if it's log file, since schema 
evolution for log file is handled in `FileGroupRecordBuffer`
     InternalSchemaManager schemaManager = isLogFile ? 
InternalSchemaManager.DISABLED : internalSchemaManager.get();
 
+    if 
(filePath.getName().endsWith(HoodieFileFormat.LANCE.getFileExtension())) {

Review Comment:
   Agreed. I kept this PR scoped to Lance support and added the explicit 
schema-evolution guard here. Removing the format-specific branching from 
`FlinkRowDataReaderContext#getFileRecordIterator` seems better as a follow-up 
issue because it touches the broader reader abstraction/factory design.



##########
hudi-client/hudi-flink-client/src/main/java/org/apache/hudi/io/storage/row/HoodieFlinkLanceArrowUtils.java:
##########
@@ -0,0 +1,283 @@
+/*
+ * 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.hudi.io.storage.row;
+
+import org.apache.hudi.exception.HoodieNotSupportedException;
+
+import org.apache.arrow.vector.BigIntVector;
+import org.apache.arrow.vector.BitVector;
+import org.apache.arrow.vector.DateDayVector;
+import org.apache.arrow.vector.DecimalVector;
+import org.apache.arrow.vector.FieldVector;
+import org.apache.arrow.vector.Float4Vector;
+import org.apache.arrow.vector.Float8Vector;
+import org.apache.arrow.vector.IntVector;
+import org.apache.arrow.vector.SmallIntVector;
+import org.apache.arrow.vector.TimeMilliVector;
+import org.apache.arrow.vector.TimeStampMicroVector;
+import org.apache.arrow.vector.TinyIntVector;
+import org.apache.arrow.vector.ValueVector;
+import org.apache.arrow.vector.VarBinaryVector;
+import org.apache.arrow.vector.VarCharVector;
+import org.apache.arrow.vector.types.DateUnit;
+import org.apache.arrow.vector.types.FloatingPointPrecision;
+import org.apache.arrow.vector.types.TimeUnit;
+import org.apache.arrow.vector.types.pojo.ArrowType;
+import org.apache.arrow.vector.types.pojo.Field;
+import org.apache.arrow.vector.types.pojo.FieldType;
+import org.apache.arrow.vector.types.pojo.Schema;
+import org.apache.flink.table.data.DecimalData;
+import org.apache.flink.table.data.GenericRowData;
+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.DecimalType;
+import org.apache.flink.table.types.logical.LocalZonedTimestampType;
+import org.apache.flink.table.types.logical.LogicalType;
+import org.apache.flink.table.types.logical.RowType;
+import org.apache.flink.table.types.logical.TimestampType;
+
+import java.math.BigDecimal;
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.List;
+
+import static 
org.apache.flink.table.types.logical.utils.LogicalTypeChecks.getPrecision;
+
+/**
+ * Primitive RowData/Arrow conversion helpers for Flink Lance base files.
+ */
+public final class HoodieFlinkLanceArrowUtils {
+
+  private HoodieFlinkLanceArrowUtils() {
+  }
+
+  public static Schema toArrowSchema(RowType rowType) {
+    List<Field> fields = new ArrayList<>(rowType.getFieldCount());
+    for (RowType.RowField field : rowType.getFields()) {
+      fields.add(toArrowField(field.getName(), field.getType()));
+    }
+    return new Schema(fields);
+  }
+
+  public static RowType toRowType(Schema schema) {
+    List<RowType.RowField> fields = new ArrayList<>(schema.getFields().size());
+    for (Field field : schema.getFields()) {
+      fields.add(new RowType.RowField(field.getName(), 
toLogicalType(field.getType())));
+    }
+    return new RowType(fields);
+  }
+
+  public static RowData toRowData(RowType rowType, List<FieldVector> vectors, 
int rowId) {
+    GenericRowData rowData = new GenericRowData(vectors.size());
+    for (int i = 0; i < vectors.size(); i++) {
+      FieldVector vector = vectors.get(i);
+      if (vector.isNull(rowId)) {
+        rowData.setField(i, null);
+      } else {
+        rowData.setField(i, readValue(rowType.getTypeAt(i), vector, rowId));
+      }
+    }
+    return rowData;
+  }
+
+  public static void writeValue(LogicalType type, FieldVector vector, int 
rowId, RowData rowData, int ordinal) {
+    if (rowData.isNullAt(ordinal)) {
+      vector.setNull(rowId);
+      return;
+    }
+    switch (type.getTypeRoot()) {
+      case BOOLEAN:
+        ((BitVector) vector).setSafe(rowId, rowData.getBoolean(ordinal) ? 1 : 
0);
+        return;
+      case TINYINT:
+        ((TinyIntVector) vector).setSafe(rowId, rowData.getByte(ordinal));
+        return;
+      case SMALLINT:
+        ((SmallIntVector) vector).setSafe(rowId, rowData.getShort(ordinal));
+        return;
+      case INTEGER:
+        ((IntVector) vector).setSafe(rowId, rowData.getInt(ordinal));
+        return;
+      case DATE:
+        ((DateDayVector) vector).setSafe(rowId, rowData.getInt(ordinal));
+        return;
+      case TIME_WITHOUT_TIME_ZONE:
+        ((TimeMilliVector) vector).setSafe(rowId, rowData.getInt(ordinal));
+        return;
+      case BIGINT:
+        ((BigIntVector) vector).setSafe(rowId, rowData.getLong(ordinal));
+        return;
+      case FLOAT:
+        ((Float4Vector) vector).setSafe(rowId, rowData.getFloat(ordinal));
+        return;
+      case DOUBLE:
+        ((Float8Vector) vector).setSafe(rowId, rowData.getDouble(ordinal));
+        return;
+      case CHAR:
+      case VARCHAR:
+        ((VarCharVector) vector).setSafe(rowId, 
rowData.getString(ordinal).toBytes());
+        return;
+      case BINARY:
+      case VARBINARY:
+        ((VarBinaryVector) vector).setSafe(rowId, rowData.getBinary(ordinal));
+        return;
+      case DECIMAL:
+        DecimalType decimalType = (DecimalType) type;
+        DecimalData decimal = rowData.getDecimal(ordinal, 
decimalType.getPrecision(), decimalType.getScale());
+        ((DecimalVector) vector).setSafe(rowId, decimal.toBigDecimal());
+        return;
+      case TIMESTAMP_WITHOUT_TIME_ZONE:
+      case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+        TimestampData timestamp = rowData.getTimestamp(ordinal, 
getPrecision(type));
+        long micros = timestamp.getMillisecond() * 1000L + 
timestamp.getNanoOfMillisecond() / 1000L;
+        ((TimeStampMicroVector) vector).setSafe(rowId, micros);
+        return;
+      default:
+        throw unsupported(type);
+    }
+  }
+
+  private static Object readValue(LogicalType type, ValueVector vector, int 
rowId) {
+    switch (type.getTypeRoot()) {
+      case BOOLEAN:
+        return ((BitVector) vector).get(rowId) == 1;
+      case TINYINT:
+        return ((TinyIntVector) vector).get(rowId);
+      case SMALLINT:
+        return ((SmallIntVector) vector).get(rowId);
+      case INTEGER:
+        return ((IntVector) vector).get(rowId);
+      case DATE:
+        return ((DateDayVector) vector).get(rowId);
+      case TIME_WITHOUT_TIME_ZONE:
+        return ((TimeMilliVector) vector).get(rowId);
+      case BIGINT:
+        return ((BigIntVector) vector).get(rowId);
+      case FLOAT:
+        return ((Float4Vector) vector).get(rowId);
+      case DOUBLE:
+        return ((Float8Vector) vector).get(rowId);
+      case CHAR:
+      case VARCHAR:
+        return StringData.fromBytes(((VarCharVector) vector).get(rowId));
+      case BINARY:
+      case VARBINARY:
+        return ((VarBinaryVector) vector).get(rowId);
+      case DECIMAL:
+        DecimalType decimalType = (DecimalType) type;
+        BigDecimal decimal = ((DecimalVector) vector).getObject(rowId);
+        return DecimalData.fromBigDecimal(decimal, decimalType.getPrecision(), 
decimalType.getScale());
+      case TIMESTAMP_WITHOUT_TIME_ZONE:
+      case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+        long micros = ((TimeStampMicroVector) vector).get(rowId);
+        return TimestampData.fromEpochMillis(micros / 1000L, (int) (micros % 
1000L) * 1000);
+      default:
+        throw unsupported(type);
+    }
+  }
+
+  private static Field toArrowField(String name, LogicalType type) {
+    return new Field(name, FieldType.nullable(toArrowType(type)), 
Collections.emptyList());
+  }
+
+  private static ArrowType toArrowType(LogicalType type) {
+    switch (type.getTypeRoot()) {
+      case BOOLEAN:
+        return ArrowType.Bool.INSTANCE;
+      case TINYINT:
+        return new ArrowType.Int(8, true);
+      case SMALLINT:
+        return new ArrowType.Int(16, true);
+      case INTEGER:
+        return new ArrowType.Int(32, true);
+      case BIGINT:
+        return new ArrowType.Int(64, true);
+      case FLOAT:
+        return new ArrowType.FloatingPoint(FloatingPointPrecision.SINGLE);
+      case DOUBLE:
+        return new ArrowType.FloatingPoint(FloatingPointPrecision.DOUBLE);
+      case CHAR:
+      case VARCHAR:
+        return ArrowType.Utf8.INSTANCE;
+      case BINARY:
+      case VARBINARY:
+        return ArrowType.Binary.INSTANCE;
+      case DATE:
+        return new ArrowType.Date(DateUnit.DAY);
+      case TIME_WITHOUT_TIME_ZONE:
+        return new ArrowType.Time(TimeUnit.MILLISECOND, 32);
+      case DECIMAL:
+        DecimalType decimalType = (DecimalType) type;
+        return new ArrowType.Decimal(decimalType.getPrecision(), 
decimalType.getScale(), 128);
+      case TIMESTAMP_WITHOUT_TIME_ZONE:
+        return new ArrowType.Timestamp(TimeUnit.MICROSECOND, null);
+      case TIMESTAMP_WITH_LOCAL_TIME_ZONE:
+        return new ArrowType.Timestamp(TimeUnit.MICROSECOND, "UTC");
+      default:
+        throw unsupported(type);
+    }
+  }
+
+  private static LogicalType toLogicalType(ArrowType arrowType) {

Review Comment:
   I tried this locally but reverted it. `HoodieSchema` does not represent some 
Arrow/Flink primitive widths such as `TINYINT` and `SMALLINT`, so routing 
through `HoodieSchemaConverter` would lose those direct mappings. I kept the 
direct Arrow-to-Flink mapping, but replaced the fully qualified logical type 
constructions with normal imports.



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