|
| 1 | +/* |
| 2 | + * Licensed to the Apache Software Foundation (ASF) under one or more |
| 3 | + * contributor license agreements. See the NOTICE file distributed with |
| 4 | + * this work for additional information regarding copyright ownership. |
| 5 | + * The ASF licenses this file to You under the Apache License, Version 2.0 |
| 6 | + * (the "License"); you may not use this file except in compliance with |
| 7 | + * the License. You may obtain a copy of the License at |
| 8 | + * |
| 9 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | + * |
| 11 | + * Unless required by applicable law or agreed to in writing, software |
| 12 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | + * See the License for the specific language governing permissions and |
| 15 | + * limitations under the License. |
| 16 | + */ |
| 17 | + |
| 18 | +package org.apache.fluss.spark.row |
| 19 | + |
| 20 | +import org.apache.fluss.row.{BinaryString, Decimal, InternalArray => FlussInternalArray, InternalRow => FlussInternalRow, TimestampLtz, TimestampNtz} |
| 21 | + |
| 22 | +import org.apache.spark.sql.catalyst.util.{ArrayData => SparkArrayData} |
| 23 | +import org.apache.spark.sql.types.{ArrayType => SparkArrayType, DataType => SparkDataType, StructType} |
| 24 | + |
| 25 | +/** Wraps a Spark [[SparkArrayData]] as a Fluss [[FlussInternalArray]]. */ |
| 26 | +class SparkAsFlussArray(arrayData: SparkArrayData, elementType: SparkDataType) |
| 27 | + extends FlussInternalArray |
| 28 | + with Serializable { |
| 29 | + |
| 30 | + /** Returns the number of elements in this array. */ |
| 31 | + override def size(): Int = arrayData.numElements() |
| 32 | + |
| 33 | + override def toBooleanArray: Array[Boolean] = arrayData.toBooleanArray() |
| 34 | + |
| 35 | + override def toByteArray: Array[Byte] = arrayData.toByteArray() |
| 36 | + |
| 37 | + override def toShortArray: Array[Short] = arrayData.toShortArray() |
| 38 | + |
| 39 | + override def toIntArray: Array[Int] = arrayData.toIntArray() |
| 40 | + |
| 41 | + override def toLongArray: Array[Long] = arrayData.toLongArray() |
| 42 | + |
| 43 | + override def toFloatArray: Array[Float] = arrayData.toFloatArray() |
| 44 | + |
| 45 | + override def toDoubleArray: Array[Double] = arrayData.toDoubleArray() |
| 46 | + |
| 47 | + /** Returns true if the element is null at the given position. */ |
| 48 | + override def isNullAt(pos: Int): Boolean = arrayData.isNullAt(pos) |
| 49 | + |
| 50 | + /** Returns the boolean value at the given position. */ |
| 51 | + override def getBoolean(pos: Int): Boolean = arrayData.getBoolean(pos) |
| 52 | + |
| 53 | + /** Returns the byte value at the given position. */ |
| 54 | + override def getByte(pos: Int): Byte = arrayData.getByte(pos) |
| 55 | + |
| 56 | + /** Returns the short value at the given position. */ |
| 57 | + override def getShort(pos: Int): Short = arrayData.getShort(pos) |
| 58 | + |
| 59 | + /** Returns the integer value at the given position. */ |
| 60 | + override def getInt(pos: Int): Int = arrayData.getInt(pos) |
| 61 | + |
| 62 | + /** Returns the long value at the given position. */ |
| 63 | + override def getLong(pos: Int): Long = arrayData.getLong(pos) |
| 64 | + |
| 65 | + /** Returns the float value at the given position. */ |
| 66 | + override def getFloat(pos: Int): Float = arrayData.getFloat(pos) |
| 67 | + |
| 68 | + /** Returns the double value at the given position. */ |
| 69 | + override def getDouble(pos: Int): Double = arrayData.getDouble(pos) |
| 70 | + |
| 71 | + /** Returns the string value at the given position with fixed length. */ |
| 72 | + override def getChar(pos: Int, length: Int): BinaryString = |
| 73 | + BinaryString.fromBytes(arrayData.getUTF8String(pos).getBytes) |
| 74 | + |
| 75 | + /** Returns the string value at the given position. */ |
| 76 | + override def getString(pos: Int): BinaryString = |
| 77 | + BinaryString.fromBytes(arrayData.getUTF8String(pos).getBytes) |
| 78 | + |
| 79 | + /** |
| 80 | + * Returns the decimal value at the given position. |
| 81 | + * |
| 82 | + * <p>The precision and scale are required to determine whether the decimal value was stored in a |
| 83 | + * compact representation (see [[Decimal]]). |
| 84 | + */ |
| 85 | + override def getDecimal(pos: Int, precision: Int, scale: Int): Decimal = { |
| 86 | + val sparkDecimal = arrayData.getDecimal(pos, precision, scale) |
| 87 | + if (sparkDecimal.precision <= org.apache.spark.sql.types.Decimal.MAX_LONG_DIGITS) |
| 88 | + Decimal.fromUnscaledLong( |
| 89 | + sparkDecimal.toUnscaledLong, |
| 90 | + sparkDecimal.precision, |
| 91 | + sparkDecimal.scale) |
| 92 | + else |
| 93 | + Decimal.fromBigDecimal( |
| 94 | + sparkDecimal.toJavaBigDecimal, |
| 95 | + sparkDecimal.precision, |
| 96 | + sparkDecimal.scale) |
| 97 | + } |
| 98 | + |
| 99 | + /** |
| 100 | + * Returns the timestamp value at the given position. |
| 101 | + * |
| 102 | + * <p>The precision is required to determine whether the timestamp value was stored in a compact |
| 103 | + * representation (see [[TimestampNtz]]). |
| 104 | + */ |
| 105 | + override def getTimestampNtz(pos: Int, precision: Int): TimestampNtz = |
| 106 | + TimestampNtz.fromMicros(arrayData.getLong(pos)) |
| 107 | + |
| 108 | + /** |
| 109 | + * Returns the timestamp value at the given position. |
| 110 | + * |
| 111 | + * <p>The precision is required to determine whether the timestamp value was stored in a compact |
| 112 | + * representation (see [[TimestampLtz]]). |
| 113 | + */ |
| 114 | + override def getTimestampLtz(pos: Int, precision: Int): TimestampLtz = |
| 115 | + TimestampLtz.fromEpochMicros(arrayData.getLong(pos)) |
| 116 | + |
| 117 | + /** Returns the binary value at the given position with fixed length. */ |
| 118 | + override def getBinary(pos: Int, length: Int): Array[Byte] = arrayData.getBinary(pos) |
| 119 | + |
| 120 | + /** Returns the binary value at the given position. */ |
| 121 | + override def getBytes(pos: Int): Array[Byte] = arrayData.getBinary(pos) |
| 122 | + |
| 123 | + /** Returns the array value at the given position. */ |
| 124 | + override def getArray(pos: Int) = new SparkAsFlussArray( |
| 125 | + arrayData.getArray(pos), |
| 126 | + elementType.asInstanceOf[SparkArrayType].elementType) |
| 127 | + |
| 128 | + /** Returns the row value at the given position. */ |
| 129 | + override def getRow(pos: Int, numFields: Int): FlussInternalRow = |
| 130 | + new SparkAsFlussRow(elementType.asInstanceOf[StructType]) |
| 131 | + .replace(arrayData.getStruct(pos, numFields)) |
| 132 | +} |
0 commit comments