JulianJaffePinterest commented on a change in pull request #11823: URL: https://github.com/apache/druid/pull/11823#discussion_r749913511
########## File path: spark/src/main/scala/org/apache/druid/spark/v2/reader/DruidColumnarInputPartitionReader.scala ########## @@ -0,0 +1,376 @@ +/* + * 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.druid.spark.v2.reader + +import org.apache.druid.java.util.common.{FileUtils, IAE, ISE, StringUtils} +import org.apache.druid.query.dimension.DefaultDimensionSpec +import org.apache.druid.query.filter.DimFilter +import org.apache.druid.segment.column.ValueType +import org.apache.druid.segment.vector.{VectorColumnSelectorFactory, VectorCursor} +import org.apache.druid.segment.{QueryableIndexStorageAdapter, VirtualColumns} +import org.apache.druid.spark.configuration.{Configuration, SerializableHadoopConfiguration} +import org.apache.druid.spark.mixins.Logging +import org.apache.druid.spark.registries.ComplexMetricRegistry +import org.apache.spark.broadcast.Broadcast +import org.apache.spark.sql.execution.vectorized.{OnHeapColumnVector, WritableColumnVector} +import org.apache.spark.sql.sources.v2.reader.InputPartitionReader +import org.apache.spark.sql.types.{ArrayType, DoubleType, FloatType, LongType, StringType, + StructField, StructType, TimestampType} +import org.apache.spark.sql.vectorized.{ColumnVector, ColumnarBatch} + +class DruidColumnarInputPartitionReader( + segmentStr: String, + schema: StructType, + filter: Option[DimFilter], + columnTypes: Option[Set[String]], + broadcastedHadoopConf: Broadcast[SerializableHadoopConfiguration], + conf: Configuration, + useSparkConfForDeepStorage: Boolean, + useCompactSketches: Boolean, + useDefaultNullHandling: Boolean, + batchSize: Int + ) + extends DruidBaseInputPartitionReader( + segmentStr, + columnTypes, + broadcastedHadoopConf, + conf, + useSparkConfForDeepStorage, + useCompactSketches, + useDefaultNullHandling + ) with InputPartitionReader[ColumnarBatch] with Logging { + + private val adapter = new QueryableIndexStorageAdapter(queryableIndex) + + private val cursor: VectorCursor = adapter.makeVectorCursor( + filter.map(_.toOptimizedFilter).orNull, + adapter.getInterval, + VirtualColumns.EMPTY, + false, + batchSize, + null) // scalastyle:ignore null + + private val columnVectors: Array[OnHeapColumnVector] = OnHeapColumnVector.allocateColumns(batchSize, schema) + private val resultBatch: ColumnarBatch = new ColumnarBatch(columnVectors.map(_.asInstanceOf[ColumnVector])) + + override def next(): Boolean = { + if (!cursor.isDone) { + fillVectors() + true + } else { + false + } + } + + override def get(): ColumnarBatch = { + resultBatch + } + + override def close(): Unit = { + resultBatch.close() Review comment: The Spark elements (`resultBatch`, columnVectors`) shouldn't. The Druid elements can throw RuntimeExceptions, but I'm not sure what recovery actions we would take if the call failed - log and rethrow? Either way, any unhandled or raised exception here is going to result in the loss of the task and the destruction of the JVM, so the only leak I think we'd face is in the temporary directory. Spark should clear out the temporary directories created by a run when it handles a failing task, but adding explicit guards and a finally block is a good idea 👍. -- 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] --------------------------------------------------------------------- To unsubscribe, e-mail: [email protected] For additional commands, e-mail: [email protected]
