|
| 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.kyuubi.spark.connector.tpch |
| 19 | + |
| 20 | +import java.time.LocalDate |
| 21 | +import java.time.format.DateTimeFormatter |
| 22 | + |
| 23 | +import scala.collection.mutable.ArrayBuffer |
| 24 | + |
| 25 | +import io.trino.tpch._ |
| 26 | +import io.trino.tpch.GenerateUtils.formatDate |
| 27 | +import io.trino.tpch.TpchColumnType.Base._ |
| 28 | +import org.apache.spark.sql.SparkSession |
| 29 | +import org.apache.spark.sql.catalyst.InternalRow |
| 30 | +import org.apache.spark.sql.connector.read._ |
| 31 | +import org.apache.spark.sql.types._ |
| 32 | +import org.apache.spark.unsafe.types.UTF8String |
| 33 | + |
| 34 | +case class TPCHTableChuck(table: String, scale: Int, parallelism: Int, index: Int) |
| 35 | + extends InputPartition |
| 36 | + |
| 37 | +class TPCHBatchScan( |
| 38 | + @transient table: TpchTable[_], |
| 39 | + scale: Int, |
| 40 | + schema: StructType) extends ScanBuilder |
| 41 | + with Scan with Batch with Serializable { |
| 42 | + |
| 43 | + private val _numRows: Long = TPCHStatisticsUtils.numRows(table, scale) |
| 44 | + |
| 45 | + private val rowCountPerTask: Int = 1000000 |
| 46 | + |
| 47 | + private val parallelism: Int = |
| 48 | + if (table.equals(TpchTable.NATION) || table.equals(TpchTable.REGION)) 1 |
| 49 | + else math.max( |
| 50 | + SparkSession.active.sparkContext.defaultParallelism, |
| 51 | + (_numRows / rowCountPerTask.toDouble).ceil.toInt) |
| 52 | + |
| 53 | + override def build: Scan = this |
| 54 | + |
| 55 | + override def toBatch: Batch = this |
| 56 | + |
| 57 | + override def description: String = |
| 58 | + s"Scan TPC-H sf$scale.${table.getTableName}, count: ${_numRows}, parallelism: $parallelism" |
| 59 | + |
| 60 | + override def readSchema: StructType = schema |
| 61 | + |
| 62 | + override def planInputPartitions: Array[InputPartition] = |
| 63 | + (1 to parallelism).map { i => |
| 64 | + TPCHTableChuck(table.getTableName, scale, parallelism, i) |
| 65 | + }.toArray |
| 66 | + |
| 67 | + def createReaderFactory: PartitionReaderFactory = (partition: InputPartition) => { |
| 68 | + val chuck = partition.asInstanceOf[TPCHTableChuck] |
| 69 | + new TPCHPartitionReader(chuck.table, chuck.scale, chuck.parallelism, chuck.index, schema) |
| 70 | + } |
| 71 | + |
| 72 | +} |
| 73 | + |
| 74 | +class TPCHPartitionReader( |
| 75 | + table: String, |
| 76 | + scale: Int, |
| 77 | + parallelism: Int, |
| 78 | + index: Int, |
| 79 | + schema: StructType) extends PartitionReader[InternalRow] { |
| 80 | + |
| 81 | + private val tpchTable = TpchTable.getTable(table) |
| 82 | + |
| 83 | + private val columns = tpchTable.getColumns |
| 84 | + .asInstanceOf[java.util.List[TpchColumn[TpchEntity]]] |
| 85 | + |
| 86 | + private lazy val dateFmt: DateTimeFormatter = DateTimeFormatter.ofPattern("yyyy-MM-dd") |
| 87 | + |
| 88 | + private val iterator = tpchTable.createGenerator(scale, index, parallelism).iterator |
| 89 | + |
| 90 | + private var currentRow: InternalRow = _ |
| 91 | + |
| 92 | + override def next(): Boolean = { |
| 93 | + val hasNext = iterator.hasNext |
| 94 | + if (hasNext) currentRow = { |
| 95 | + val row = iterator.next().asInstanceOf[TpchEntity] |
| 96 | + val rowValue = new ArrayBuffer[String]() |
| 97 | + columns.stream().forEach(column => { |
| 98 | + val baseType = column.getType.getBase |
| 99 | + var value: String = "" |
| 100 | + baseType match { |
| 101 | + case IDENTIFIER => value += column.getIdentifier(row) |
| 102 | + case INTEGER => value += column.getInteger(row) |
| 103 | + case DATE => value += column.getDate(row) |
| 104 | + case DOUBLE => value += column.getDouble(row) |
| 105 | + case VARCHAR => value += column.getString(row) |
| 106 | + } |
| 107 | + rowValue += value |
| 108 | + }) |
| 109 | + val rowAny = new ArrayBuffer[Any]() |
| 110 | + rowValue.zipWithIndex.map { case (value, i) => |
| 111 | + (value, schema(i).dataType) match { |
| 112 | + case (null, _) => null |
| 113 | + case ("", _) => null |
| 114 | + case (value, IntegerType) => rowAny += value.toInt |
| 115 | + case (value, LongType) => rowAny += value.toLong |
| 116 | + case (value, DoubleType) => rowAny += value.toDouble |
| 117 | + case (value, DateType) => rowAny += LocalDate.parse(formatDate(value.toInt), dateFmt) |
| 118 | + .toEpochDay.toInt |
| 119 | + case (value, StringType) => rowAny += UTF8String.fromString(value) |
| 120 | + case (value, CharType(_)) => rowAny += UTF8String.fromString(value) |
| 121 | + case (value, VarcharType(_)) => rowAny += UTF8String.fromString(value) |
| 122 | + case (value, DecimalType()) => rowAny += Decimal(value) |
| 123 | + case (value, dt) => throw new IllegalArgumentException(s"value: $value, type: $dt") |
| 124 | + } |
| 125 | + } |
| 126 | + InternalRow.fromSeq(rowAny) |
| 127 | + } |
| 128 | + hasNext |
| 129 | + } |
| 130 | + |
| 131 | + override def get(): InternalRow = currentRow |
| 132 | + |
| 133 | + override def close(): Unit = {} |
| 134 | + |
| 135 | +} |
0 commit comments