[HOTFIX] Removed BatchedDataSourceScanExec class and extended directly from FileSourceScanExec
Problem: Since some of the code of BatchedDataSourceScanExec is copied from spark, it is difficult to maintain from version upgrades of spark. Currently we face issues during spark 2.3 upgrade so better remove it. Solution: Remove BatchedDataSourceScanExec and extend directly the spark class FileSourceScanExec. This closes #2400 Project: http://git-wip-us.apache.org/repos/asf/carbondata/repo Commit: http://git-wip-us.apache.org/repos/asf/carbondata/commit/3df2fd03 Tree: http://git-wip-us.apache.org/repos/asf/carbondata/tree/3df2fd03 Diff: http://git-wip-us.apache.org/repos/asf/carbondata/diff/3df2fd03 Branch: refs/heads/carbonstore Commit: 3df2fd030b634add5a2df0c85e1262d6b1e4286b Parents: 653efee Author: ravipesala <ravi.pes...@gmail.com> Authored: Fri Jun 22 18:13:59 2018 +0530 Committer: Jacky Li <jacky.li...@qq.com> Committed: Sat Jul 14 11:46:30 2018 +0800 ---------------------------------------------------------------------- .../dataload/TestGlobalSortDataLoad.scala | 5 +- .../StandardPartitionTableLoadingTestCase.scala | 9 +- .../StandardPartitionTableQueryTestCase.scala | 4 +- .../execution/BatchedDataSourceScanExec.scala | 147 ------------------- .../strategy/CarbonDataSourceScan.scala | 53 +++++++ .../strategy/CarbonLateDecodeStrategy.scala | 31 +++- .../vectorreader/VectorReaderTestCase.scala | 6 +- 7 files changed, 91 insertions(+), 164 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/carbondata/blob/3df2fd03/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/dataload/TestGlobalSortDataLoad.scala ---------------------------------------------------------------------- diff --git a/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/dataload/TestGlobalSortDataLoad.scala b/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/dataload/TestGlobalSortDataLoad.scala index d7b1172..c40526d 100644 --- a/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/dataload/TestGlobalSortDataLoad.scala +++ b/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/dataload/TestGlobalSortDataLoad.scala @@ -18,7 +18,6 @@ package org.apache.carbondata.spark.testsuite.dataload import scala.collection.JavaConverters._ - import java.io.{File, FileWriter} import org.apache.commons.io.FileUtils @@ -27,7 +26,7 @@ import org.apache.carbondata.core.constants.CarbonCommonConstants import org.apache.carbondata.core.util.CarbonProperties import org.apache.spark.sql.Row import org.apache.spark.sql.catalyst.InternalRow -import org.apache.spark.sql.execution.BatchedDataSourceScanExec +import org.apache.spark.sql.execution.strategy.CarbonDataSourceScan import org.apache.spark.sql.test.TestQueryExecutor.projectPath import org.apache.spark.sql.test.util.QueryTest import org.scalatest.{BeforeAndAfterAll, BeforeAndAfterEach} @@ -298,7 +297,7 @@ class TestGlobalSortDataLoad extends QueryTest with BeforeAndAfterEach with Befo } val df = sql("select * from carbon_globalsort") val scanRdd = df.queryExecution.sparkPlan.collect { - case b: BatchedDataSourceScanExec if b.rdd.isInstanceOf[CarbonScanRDD[InternalRow]] => + case b: CarbonDataSourceScan if b.rdd.isInstanceOf[CarbonScanRDD[InternalRow]] => b.rdd.asInstanceOf[CarbonScanRDD[InternalRow]] }.head assertResult(defaultParallelism)(scanRdd.getPartitions.length) http://git-wip-us.apache.org/repos/asf/carbondata/blob/3df2fd03/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/standardpartition/StandardPartitionTableLoadingTestCase.scala ---------------------------------------------------------------------- diff --git a/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/standardpartition/StandardPartitionTableLoadingTestCase.scala b/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/standardpartition/StandardPartitionTableLoadingTestCase.scala index b929364..1db1f4a 100644 --- a/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/standardpartition/StandardPartitionTableLoadingTestCase.scala +++ b/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/standardpartition/StandardPartitionTableLoadingTestCase.scala @@ -16,22 +16,19 @@ */ package org.apache.carbondata.spark.testsuite.standardpartition -import scala.collection.JavaConverters._ import java.io.{File, FileWriter, IOException} import java.util import java.util.concurrent.{Callable, ExecutorService, Executors} import org.apache.commons.io.FileUtils import org.apache.spark.sql.catalyst.{InternalRow, TableIdentifier} -import org.apache.spark.sql.execution.BatchedDataSourceScanExec +import org.apache.spark.sql.execution.strategy.CarbonDataSourceScan import org.apache.spark.sql.optimizer.CarbonFilters import org.apache.spark.sql.test.util.QueryTest -import org.apache.spark.sql.{AnalysisException, CarbonEnv, CarbonSession, Row} +import org.apache.spark.sql.{AnalysisException, CarbonEnv, Row} import org.scalatest.BeforeAndAfterAll import org.apache.carbondata.core.constants.CarbonCommonConstants -import org.apache.carbondata.core.datastore.filesystem.{CarbonFile, CarbonFileFilter} -import org.apache.carbondata.core.datastore.impl.FileFactory import org.apache.carbondata.core.metadata.{CarbonMetadata, SegmentFileStore} import org.apache.carbondata.core.statusmanager.SegmentStatusManager import org.apache.carbondata.core.util.CarbonProperties @@ -483,7 +480,7 @@ class StandardPartitionTableLoadingTestCase extends QueryTest with BeforeAndAfte FileUtils.deleteDirectory(folder) val dataFrame = sql("select * from smallpartitionfilesread") val scanRdd = dataFrame.queryExecution.sparkPlan.collect { - case b: BatchedDataSourceScanExec if b.rdd.isInstanceOf[CarbonScanRDD[InternalRow]] => b.rdd + case b: CarbonDataSourceScan if b.rdd.isInstanceOf[CarbonScanRDD[InternalRow]] => b.rdd .asInstanceOf[CarbonScanRDD[InternalRow]] }.head assert(scanRdd.getPartitions.length < 10) http://git-wip-us.apache.org/repos/asf/carbondata/blob/3df2fd03/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/standardpartition/StandardPartitionTableQueryTestCase.scala ---------------------------------------------------------------------- diff --git a/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/standardpartition/StandardPartitionTableQueryTestCase.scala b/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/standardpartition/StandardPartitionTableQueryTestCase.scala index 87adaae..fb554f2 100644 --- a/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/standardpartition/StandardPartitionTableQueryTestCase.scala +++ b/integration/spark-common-test/src/test/scala/org/apache/carbondata/spark/testsuite/standardpartition/StandardPartitionTableQueryTestCase.scala @@ -17,7 +17,7 @@ package org.apache.carbondata.spark.testsuite.standardpartition import org.apache.spark.sql.catalyst.InternalRow -import org.apache.spark.sql.execution.BatchedDataSourceScanExec +import org.apache.spark.sql.execution.strategy.CarbonDataSourceScan import org.apache.spark.sql.test.Spark2TestQueryExecutor import org.apache.spark.sql.test.util.QueryTest import org.apache.spark.sql.{DataFrame, Row} @@ -420,7 +420,7 @@ test("Creation of partition table should fail if the colname in table schema and private def verifyPartitionInfo(frame: DataFrame, partitionNames: Seq[String]) = { val plan = frame.queryExecution.sparkPlan val scanRDD = plan collect { - case b: BatchedDataSourceScanExec if b.rdd.isInstanceOf[CarbonScanRDD[InternalRow]] => b.rdd + case b: CarbonDataSourceScan if b.rdd.isInstanceOf[CarbonScanRDD[InternalRow]] => b.rdd .asInstanceOf[CarbonScanRDD[InternalRow]] } assert(scanRDD.nonEmpty) http://git-wip-us.apache.org/repos/asf/carbondata/blob/3df2fd03/integration/spark2/src/main/scala/org/apache/spark/sql/execution/BatchedDataSourceScanExec.scala ---------------------------------------------------------------------- diff --git a/integration/spark2/src/main/scala/org/apache/spark/sql/execution/BatchedDataSourceScanExec.scala b/integration/spark2/src/main/scala/org/apache/spark/sql/execution/BatchedDataSourceScanExec.scala deleted file mode 100644 index 3c3efaa..0000000 --- a/integration/spark2/src/main/scala/org/apache/spark/sql/execution/BatchedDataSourceScanExec.scala +++ /dev/null @@ -1,147 +0,0 @@ -/* - * 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.spark.sql.execution - -import org.apache.commons.lang3.StringUtils -import org.apache.spark.rdd.RDD -import org.apache.spark.sql.catalyst.{InternalRow, TableIdentifier} -import org.apache.spark.sql.catalyst.expressions.Attribute -import org.apache.spark.sql.catalyst.expressions.codegen.{CodegenContext, ExprCode} -import org.apache.spark.sql.catalyst.plans.physical.Partitioning -import org.apache.spark.sql.execution.datasources.LogicalRelation -import org.apache.spark.sql.execution.metric.SQLMetrics -import org.apache.spark.sql.sources.BaseRelation -import org.apache.spark.sql.types.DataType -import org.apache.spark.util.Utils - -/** Physical plan node for scanning data from a batched relation. */ -case class BatchedDataSourceScanExec( - output: Seq[Attribute], - rdd: RDD[InternalRow], - @transient relation: BaseRelation, - override val outputPartitioning: Partitioning, - override val metadata: Map[String, String], - override val metastoreTableIdentifier: Option[TableIdentifier], - @transient logicalRelation: LogicalRelation) - extends DataSourceScanExec with CodegenSupport { - - override lazy val metrics = - Map("numOutputRows" -> SQLMetrics.createMetric(sparkContext, "number of output rows"), - "scanTime" -> SQLMetrics.createTimingMetric(sparkContext, "scan time")) - - protected override def doExecute(): RDD[InternalRow] = { - // in the case of fallback, this batched scan should never fail because of: - // 1) only primitive types are supported - // 2) the number of columns should be smaller than spark.sql.codegen.maxFields - WholeStageCodegenExec(this).execute() - } - - override def simpleString: String = { - val metadataEntries = for ((key, value) <- metadata.toSeq.sorted) yield { - key + ": " + StringUtils.abbreviate(value, 100) - } - val metadataStr = Utils.truncatedString(metadataEntries, " ", ", ", "") - s"Batched$nodeName${Utils.truncatedString(output, "[", ",", "]")}$metadataStr" - } - - override def inputRDDs(): Seq[RDD[InternalRow]] = { - rdd :: Nil - } - - private def genCodeColumnVector(ctx: CodegenContext, columnVar: String, ordinal: String, - dataType: DataType, nullable: Boolean): ExprCode = { - val javaType = ctx.javaType(dataType) - val value = ctx.getValue(columnVar, dataType, ordinal) - val isNullVar = if (nullable) { ctx.freshName("isNull") } else { "false" } - val valueVar = ctx.freshName("value") - val str = s"columnVector[$columnVar, $ordinal, ${dataType.simpleString}]" - val code = s"${ctx.registerComment(str)}\n" + (if (nullable) { - s""" - boolean ${isNullVar} = ${columnVar}.isNullAt($ordinal); - $javaType ${valueVar} = ${isNullVar} ? ${ctx.defaultValue(dataType)} : ($value); - """ - } else { - s"$javaType ${valueVar} = $value;" - }).trim - ExprCode(code, isNullVar, valueVar) - } - - // Support codegen so that we can avoid the UnsafeRow conversion in all cases. Codegen - // never requires UnsafeRow as input. - override protected def doProduce(ctx: CodegenContext): String = { - val input = ctx.freshName("input") - // PhysicalRDD always just has one input - ctx.addMutableState("scala.collection.Iterator", input, s"$input = inputs[0];") - - // metrics - val numOutputRows = metricTerm(ctx, "numOutputRows") - val scanTimeMetric = metricTerm(ctx, "scanTime") - val scanTimeTotalNs = ctx.freshName("scanTime") - ctx.addMutableState("long", scanTimeTotalNs, s"$scanTimeTotalNs = 0;") - - val columnarBatchClz = "org.apache.spark.sql.execution.vectorized.ColumnarBatch" - val batch = ctx.freshName("batch") - ctx.addMutableState(columnarBatchClz, batch, s"$batch = null;") - - val columnVectorClz = "org.apache.spark.sql.execution.vectorized.ColumnVector" - val idx = ctx.freshName("batchIdx") - ctx.addMutableState("int", idx, s"$idx = 0;") - val colVars = output.indices.map(i => ctx.freshName("colInstance" + i)) - val columnAssigns = colVars.zipWithIndex.map { case (name, i) => - ctx.addMutableState(columnVectorClz, name, s"$name = null;") - s"$name = $batch.column($i);" - } - - val nextBatch = ctx.freshName("nextBatch") - ctx.addNewFunction(nextBatch, - s""" - |private void $nextBatch() throws java.io.IOException { - | long getBatchStart = System.nanoTime(); - | if ($input.hasNext()) { - | $batch = ($columnarBatchClz)$input.next(); - | $numOutputRows.add($batch.numRows()); - | $idx = 0; - | ${columnAssigns.mkString("", "\n", "\n")} - | } - | $scanTimeTotalNs += System.nanoTime() - getBatchStart; - |}""".stripMargin) - - ctx.currentVars = null - val rowidx = ctx.freshName("rowIdx") - val columnsBatchInput = (output zip colVars).map { case (attr, colVar) => - genCodeColumnVector(ctx, colVar, rowidx, attr.dataType, attr.nullable) - } - s""" - |if ($batch == null) { - | $nextBatch(); - |} - |while ($batch != null) { - | int numRows = $batch.numRows(); - | while ($idx < numRows) { - | int $rowidx = $idx++; - | ${consume(ctx, columnsBatchInput).trim} - | if (shouldStop()) return; - | } - | $batch = null; - | $nextBatch(); - |} - |$scanTimeMetric.add($scanTimeTotalNs / (1000 * 1000)); - |$scanTimeTotalNs = 0; - """.stripMargin - } -} http://git-wip-us.apache.org/repos/asf/carbondata/blob/3df2fd03/integration/spark2/src/main/scala/org/apache/spark/sql/execution/strategy/CarbonDataSourceScan.scala ---------------------------------------------------------------------- diff --git a/integration/spark2/src/main/scala/org/apache/spark/sql/execution/strategy/CarbonDataSourceScan.scala b/integration/spark2/src/main/scala/org/apache/spark/sql/execution/strategy/CarbonDataSourceScan.scala new file mode 100644 index 0000000..ccc882d --- /dev/null +++ b/integration/spark2/src/main/scala/org/apache/spark/sql/execution/strategy/CarbonDataSourceScan.scala @@ -0,0 +1,53 @@ +/* + * 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.spark.sql.execution.strategy + +import org.apache.spark.rdd.RDD +import org.apache.spark.sql.catalyst.{InternalRow, TableIdentifier} +import org.apache.spark.sql.catalyst.expressions.{Attribute, SortOrder} +import org.apache.spark.sql.catalyst.plans.physical.Partitioning +import org.apache.spark.sql.execution.FileSourceScanExec +import org.apache.spark.sql.execution.datasources.{HadoopFsRelation, LogicalRelation} + +/** + * Physical plan node for scanning data. It is applied for both tables + * USING carbondata and STORED AS CARBONDATA. + */ +class CarbonDataSourceScan( + override val output: Seq[Attribute], + val rdd: RDD[InternalRow], + @transient relation: HadoopFsRelation, + val partitioning: Partitioning, + override val metadata: Map[String, String], + identifier: Option[TableIdentifier], + @transient logicalRelation: LogicalRelation) + extends FileSourceScanExec( + relation, + output, + relation.dataSchema, + Seq.empty, + Seq.empty, + identifier) { + + override val supportsBatch: Boolean = true + + override val (outputPartitioning, outputOrdering): (Partitioning, Seq[SortOrder]) = + (partitioning, Nil) + + override def inputRDDs(): Seq[RDD[InternalRow]] = rdd :: Nil + +} http://git-wip-us.apache.org/repos/asf/carbondata/blob/3df2fd03/integration/spark2/src/main/scala/org/apache/spark/sql/execution/strategy/CarbonLateDecodeStrategy.scala ---------------------------------------------------------------------- diff --git a/integration/spark2/src/main/scala/org/apache/spark/sql/execution/strategy/CarbonLateDecodeStrategy.scala b/integration/spark2/src/main/scala/org/apache/spark/sql/execution/strategy/CarbonLateDecodeStrategy.scala index 10ae86a..91197fd 100644 --- a/integration/spark2/src/main/scala/org/apache/spark/sql/execution/strategy/CarbonLateDecodeStrategy.scala +++ b/integration/spark2/src/main/scala/org/apache/spark/sql/execution/strategy/CarbonLateDecodeStrategy.scala @@ -29,7 +29,7 @@ import org.apache.spark.sql.catalyst.planning.PhysicalOperation import org.apache.spark.sql.catalyst.plans.logical.LogicalPlan import org.apache.spark.sql.catalyst.plans.physical.{HashPartitioning, Partitioning, UnknownPartitioning} import org.apache.spark.sql.execution._ -import org.apache.spark.sql.execution.datasources.LogicalRelation +import org.apache.spark.sql.execution.datasources.{CatalogFileIndex, HadoopFsRelation, InMemoryFileIndex, LogicalRelation, SparkCarbonTableFormat} import org.apache.spark.sql.optimizer.{CarbonDecoderRelation, CarbonFilters} import org.apache.spark.sql.sources.{BaseRelation, Filter} import org.apache.spark.sql.types._ @@ -367,14 +367,14 @@ private[sql] class CarbonLateDecodeStrategy extends SparkStrategy { val table = relation.relation.asInstanceOf[CarbonDatasourceHadoopRelation] if (supportBatchedDataSource(relation.relation.sqlContext, updateRequestedColumns) && needDecoder.isEmpty) { - BatchedDataSourceScanExec( + new CarbonDataSourceScan( output, scanBuilder(updateRequestedColumns, candidatePredicates, pushedFilters, needDecoder, partitions), - relation.relation, + createHadoopFSRelation(relation), getPartitioning(table.carbonTable, updateRequestedColumns), metadata, relation.catalogTable.map(_.identifier), relation) @@ -673,4 +673,29 @@ private[sql] class CarbonLateDecodeStrategy extends SparkStrategy { supportCodegen && vectorizedReader.toBoolean && cols.forall(_.dataType.isInstanceOf[AtomicType]) } + + private def createHadoopFSRelation(relation: LogicalRelation): HadoopFsRelation = { + val sparkSession = relation.relation.sqlContext.sparkSession + relation.catalogTable match { + case Some(catalogTable) => + HadoopFsRelation( + new CatalogFileIndex( + sparkSession, + catalogTable, + sizeInBytes = relation.relation.sizeInBytes), + catalogTable.partitionSchema, + catalogTable.schema, + catalogTable.bucketSpec, + new SparkCarbonTableFormat, + catalogTable.storage.properties)(sparkSession) + case _ => + HadoopFsRelation( + new InMemoryFileIndex(sparkSession, Seq.empty, Map.empty, None), + new StructType(), + relation.relation.schema, + None, + new SparkCarbonTableFormat, + null)(sparkSession) + } + } } http://git-wip-us.apache.org/repos/asf/carbondata/blob/3df2fd03/integration/spark2/src/test/scala/org/apache/spark/carbondata/vectorreader/VectorReaderTestCase.scala ---------------------------------------------------------------------- diff --git a/integration/spark2/src/test/scala/org/apache/spark/carbondata/vectorreader/VectorReaderTestCase.scala b/integration/spark2/src/test/scala/org/apache/spark/carbondata/vectorreader/VectorReaderTestCase.scala index 89d9798..c7890a9 100644 --- a/integration/spark2/src/test/scala/org/apache/spark/carbondata/vectorreader/VectorReaderTestCase.scala +++ b/integration/spark2/src/test/scala/org/apache/spark/carbondata/vectorreader/VectorReaderTestCase.scala @@ -19,8 +19,8 @@ package org.apache.spark.carbondata.vectorreader import org.apache.spark.sql.Row import org.apache.spark.sql.common.util.Spark2QueryTest -import org.apache.spark.sql.execution.command.management.CarbonLoadDataCommand -import org.apache.spark.sql.execution.{BatchedDataSourceScanExec, RowDataSourceScanExec} +import org.apache.spark.sql.execution.RowDataSourceScanExec +import org.apache.spark.sql.execution.strategy.CarbonDataSourceScan import org.scalatest.BeforeAndAfterAll import org.apache.carbondata.core.constants.CarbonCommonConstants @@ -46,7 +46,7 @@ class VectorReaderTestCase extends Spark2QueryTest with BeforeAndAfterAll { """select * from vectorreader""".stripMargin).queryExecution.executedPlan var batchReader = false plan.collect { - case s: BatchedDataSourceScanExec => batchReader = true + case s: CarbonDataSourceScan => batchReader = true } assert(batchReader, "batch reader should exist when carbon.enable.vector.reader is true") }