|
| 1 | +/* |
| 2 | + * Copyright (2025) The Delta Lake Project Authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package io.delta.spark.internal.v2.utils; |
| 17 | + |
| 18 | +import io.delta.kernel.data.ArrayValue; |
| 19 | +import io.delta.kernel.data.ColumnVector; |
| 20 | +import io.delta.kernel.data.MapValue; |
| 21 | +import io.delta.kernel.internal.data.StructRow; |
| 22 | +import io.delta.kernel.types.*; |
| 23 | +import java.util.ArrayList; |
| 24 | +import java.util.HashMap; |
| 25 | +import java.util.List; |
| 26 | +import java.util.Map; |
| 27 | +import org.apache.spark.sql.GenericRowWithSchema; |
| 28 | +import org.apache.spark.sql.Row; |
| 29 | + |
| 30 | +/** |
| 31 | + * Zero-copy wrapper that presents a Kernel {@link io.delta.kernel.data.Row} as a Spark {@link Row}. |
| 32 | + * Primitive field access delegates directly to the Kernel Row with no data copy. Complex types |
| 33 | + * (Map, Array, Struct) are lazily converted on access. |
| 34 | + */ |
| 35 | +public class KernelRowToSparkRow implements Row { |
| 36 | + |
| 37 | + private final io.delta.kernel.data.Row kernelRow; |
| 38 | + private final StructType kernelSchema; |
| 39 | + private final org.apache.spark.sql.types.StructType sparkSchema; |
| 40 | + |
| 41 | + public KernelRowToSparkRow(io.delta.kernel.data.Row kernelRow) { |
| 42 | + this(kernelRow, SchemaUtils.convertKernelSchemaToSparkSchema(kernelRow.getSchema())); |
| 43 | + } |
| 44 | + |
| 45 | + /** |
| 46 | + * Constructor that accepts a pre-computed Spark schema to avoid redundant schema conversion when |
| 47 | + * wrapping many rows that share the same schema. |
| 48 | + */ |
| 49 | + public KernelRowToSparkRow( |
| 50 | + io.delta.kernel.data.Row kernelRow, org.apache.spark.sql.types.StructType sparkSchema) { |
| 51 | + this.kernelRow = kernelRow; |
| 52 | + this.kernelSchema = kernelRow.getSchema(); |
| 53 | + this.sparkSchema = sparkSchema; |
| 54 | + } |
| 55 | + |
| 56 | + @Override |
| 57 | + public int length() { |
| 58 | + return kernelSchema.length(); |
| 59 | + } |
| 60 | + |
| 61 | + @Override |
| 62 | + public org.apache.spark.sql.types.StructType schema() { |
| 63 | + return sparkSchema; |
| 64 | + } |
| 65 | + |
| 66 | + @Override |
| 67 | + public Object get(int i) { |
| 68 | + if (kernelRow.isNullAt(i)) { |
| 69 | + return null; |
| 70 | + } |
| 71 | + return extractSparkValue(kernelRow, i, kernelSchema.at(i).getDataType()); |
| 72 | + } |
| 73 | + |
| 74 | + @Override |
| 75 | + public Row copy() { |
| 76 | + Object[] values = new Object[length()]; |
| 77 | + for (int i = 0; i < values.length; i++) { |
| 78 | + values[i] = get(i); |
| 79 | + } |
| 80 | + return new GenericRowWithSchema(values, sparkSchema); |
| 81 | + } |
| 82 | + |
| 83 | + // ---- type dispatch: Kernel Row field -> Spark-compatible Object ---- |
| 84 | + |
| 85 | + private static Object extractSparkValue(io.delta.kernel.data.Row row, int ordinal, DataType dt) { |
| 86 | + if (dt instanceof BooleanType) { |
| 87 | + return row.getBoolean(ordinal); |
| 88 | + } else if (dt instanceof ByteType) { |
| 89 | + return row.getByte(ordinal); |
| 90 | + } else if (dt instanceof ShortType) { |
| 91 | + return row.getShort(ordinal); |
| 92 | + } else if (dt instanceof IntegerType || dt instanceof DateType) { |
| 93 | + return row.getInt(ordinal); |
| 94 | + } else if (dt instanceof LongType |
| 95 | + || dt instanceof TimestampType |
| 96 | + || dt instanceof TimestampNTZType) { |
| 97 | + return row.getLong(ordinal); |
| 98 | + } else if (dt instanceof FloatType) { |
| 99 | + return row.getFloat(ordinal); |
| 100 | + } else if (dt instanceof DoubleType) { |
| 101 | + return row.getDouble(ordinal); |
| 102 | + } else if (dt instanceof StringType) { |
| 103 | + return row.getString(ordinal); |
| 104 | + } else if (dt instanceof DecimalType) { |
| 105 | + return row.getDecimal(ordinal); |
| 106 | + } else if (dt instanceof BinaryType) { |
| 107 | + return row.getBinary(ordinal); |
| 108 | + } else if (dt instanceof StructType) { |
| 109 | + return new KernelRowToSparkRow( |
| 110 | + row.getStruct(ordinal), SchemaUtils.convertKernelSchemaToSparkSchema((StructType) dt)); |
| 111 | + } else if (dt instanceof MapType) { |
| 112 | + return mapValueToScalaMap(row.getMap(ordinal), (MapType) dt); |
| 113 | + } else if (dt instanceof ArrayType) { |
| 114 | + return arrayValueToScalaSeq(row.getArray(ordinal), (ArrayType) dt); |
| 115 | + } |
| 116 | + throw new UnsupportedOperationException("Unsupported Kernel DataType: " + dt); |
| 117 | + } |
| 118 | + |
| 119 | + // ---- Kernel ColumnVector element -> Spark-compatible Object ---- |
| 120 | + |
| 121 | + static Object vectorValueToSpark(ColumnVector cv, int rowId, DataType dt) { |
| 122 | + if (cv.isNullAt(rowId)) { |
| 123 | + return null; |
| 124 | + } |
| 125 | + if (dt instanceof BooleanType) { |
| 126 | + return cv.getBoolean(rowId); |
| 127 | + } else if (dt instanceof ByteType) { |
| 128 | + return cv.getByte(rowId); |
| 129 | + } else if (dt instanceof ShortType) { |
| 130 | + return cv.getShort(rowId); |
| 131 | + } else if (dt instanceof IntegerType || dt instanceof DateType) { |
| 132 | + return cv.getInt(rowId); |
| 133 | + } else if (dt instanceof LongType |
| 134 | + || dt instanceof TimestampType |
| 135 | + || dt instanceof TimestampNTZType) { |
| 136 | + return cv.getLong(rowId); |
| 137 | + } else if (dt instanceof FloatType) { |
| 138 | + return cv.getFloat(rowId); |
| 139 | + } else if (dt instanceof DoubleType) { |
| 140 | + return cv.getDouble(rowId); |
| 141 | + } else if (dt instanceof StringType) { |
| 142 | + return cv.getString(rowId); |
| 143 | + } else if (dt instanceof DecimalType) { |
| 144 | + return cv.getDecimal(rowId); |
| 145 | + } else if (dt instanceof BinaryType) { |
| 146 | + return cv.getBinary(rowId); |
| 147 | + } else if (dt instanceof StructType) { |
| 148 | + return new KernelRowToSparkRow( |
| 149 | + StructRow.fromStructVector(cv, rowId), |
| 150 | + SchemaUtils.convertKernelSchemaToSparkSchema((StructType) dt)); |
| 151 | + } else if (dt instanceof MapType) { |
| 152 | + return mapValueToScalaMap(cv.getMap(rowId), (MapType) dt); |
| 153 | + } else if (dt instanceof ArrayType) { |
| 154 | + return arrayValueToScalaSeq(cv.getArray(rowId), (ArrayType) dt); |
| 155 | + } |
| 156 | + throw new UnsupportedOperationException("Unsupported Kernel DataType: " + dt); |
| 157 | + } |
| 158 | + |
| 159 | + // ---- MapValue -> scala.collection.Map ---- |
| 160 | + |
| 161 | + static scala.collection.Map<Object, Object> mapValueToScalaMap(MapValue mv, MapType mt) { |
| 162 | + ColumnVector keys = mv.getKeys(); |
| 163 | + ColumnVector values = mv.getValues(); |
| 164 | + Map<Object, Object> javaMap = new HashMap<>(); |
| 165 | + for (int i = 0; i < mv.getSize(); i++) { |
| 166 | + Object key = vectorValueToSpark(keys, i, mt.getKeyType()); |
| 167 | + Object value = vectorValueToSpark(values, i, mt.getValueType()); |
| 168 | + javaMap.put(key, value); |
| 169 | + } |
| 170 | + return scala.jdk.javaapi.CollectionConverters.asScala(javaMap); |
| 171 | + } |
| 172 | + |
| 173 | + // ---- ArrayValue -> scala.collection.Seq ---- |
| 174 | + |
| 175 | + static scala.collection.Seq<Object> arrayValueToScalaSeq(ArrayValue av, ArrayType at) { |
| 176 | + ColumnVector elements = av.getElements(); |
| 177 | + List<Object> javaList = new ArrayList<>(); |
| 178 | + for (int i = 0; i < av.getSize(); i++) { |
| 179 | + javaList.add(vectorValueToSpark(elements, i, at.getElementType())); |
| 180 | + } |
| 181 | + return scala.jdk.javaapi.CollectionConverters.asScala(javaList).toList(); |
| 182 | + } |
| 183 | +} |
0 commit comments