Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ import org.apache.spark.sql.execution.datasources.parquet.{DefaultSource => Parq
import org.apache.spark.sql.execution.metric.SQLMetrics
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.sources.BaseRelation
import org.apache.spark.sql.types.DataType
import org.apache.spark.sql.types.{DataType, StructType}

object RDDConversions {
def productToRowRdd[A <: Product](data: RDD[A], outputTypes: Seq[DataType]): RDD[InternalRow] = {
Expand Down Expand Up @@ -348,7 +348,8 @@ private[sql] object DataSourceScanExec {
}

relation match {
case r: HadoopFsRelation if r.fileFormat.supportBatch(r.sqlContext, relation.schema) =>
case r: HadoopFsRelation
if r.fileFormat.supportBatch(r.sqlContext, StructType.fromAttributes(output)) =>
BatchedDataSourceScanExec(output, rdd, relation, outputPartitioning, metadata)
case _ =>
RowDataSourceScanExec(output, rdd, relation, outputPartitioning, metadata)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -286,10 +286,6 @@ private[sql] class DefaultSource
SQLConf.PARQUET_INT96_AS_TIMESTAMP.key,
sqlContext.conf.getConf(SQLConf.PARQUET_INT96_AS_TIMESTAMP))

// Whole stage codegen (PhysicalRDD) is able to deal with batches directly
val returningBatch =
supportBatch(sqlContext, StructType(partitionSchema.fields ++ dataSchema.fields))

// Try to push down filters when filter push-down is enabled.
val pushed = if (sqlContext.getConf(SQLConf.PARQUET_FILTER_PUSHDOWN_ENABLED.key).toBoolean) {
filters
Expand All @@ -308,8 +304,11 @@ private[sql] class DefaultSource
// TODO: if you move this into the closure it reverts to the default values.
// If true, enable using the custom RecordReader for parquet. This only works for
// a subset of the types (no complex types).
val enableVectorizedParquetReader: Boolean = sqlContext.conf.parquetVectorizedReaderEnabled &&
dataSchema.forall(_.dataType.isInstanceOf[AtomicType])
val resultSchema = StructType(partitionSchema.fields ++ requiredSchema.fields)
val enableVectorizedReader: Boolean = sqlContext.conf.parquetVectorizedReaderEnabled &&
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

minor: the comment above must be moved too

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

move to where?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nothing too important. The comment // If true, enable using the custom RecordReader for parquet... could be above this line.

resultSchema.forall(_.dataType.isInstanceOf[AtomicType])
// Whole stage codegen (PhysicalRDD) is able to deal with batches directly
val returningBatch = supportBatch(sqlContext, resultSchema)

(file: PartitionedFile) => {
assert(file.partitionValues.numFields == partitionSchema.size)
Expand All @@ -329,7 +328,7 @@ private[sql] class DefaultSource
val attemptId = new TaskAttemptID(new TaskID(new JobID(), TaskType.MAP, 0), 0)
val hadoopAttemptContext = new TaskAttemptContextImpl(broadcastedConf.value.value, attemptId)

val parquetReader = if (enableVectorizedParquetReader) {
val parquetReader = if (enableVectorizedReader) {
val vectorizedReader = new VectorizedParquetRecordReader()
vectorizedReader.initialize(split, hadoopAttemptContext)
logDebug(s"Appending $partitionSchema ${file.partitionValues}")
Expand All @@ -356,7 +355,7 @@ private[sql] class DefaultSource

// UnsafeRowParquetRecordReader appends the columns internally to avoid another copy.
if (parquetReader.isInstanceOf[VectorizedParquetRecordReader] &&
enableVectorizedParquetReader) {
enableVectorizedReader) {
iter.asInstanceOf[Iterator[InternalRow]]
} else {
val fullSchema = requiredSchema.toAttributes ++ partitionSchema.toAttributes
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import org.apache.hadoop.fs.Path
import org.apache.spark.sql._
import org.apache.spark.sql.catalyst.{InternalRow, TableIdentifier}
import org.apache.spark.sql.catalyst.expressions.SpecificMutableRow
import org.apache.spark.sql.execution.BatchedDataSourceScanExec
import org.apache.spark.sql.execution.datasources.parquet.TestingUDT.{NestedStruct, NestedStructUDT}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.test.SharedSQLContext
Expand Down Expand Up @@ -589,6 +590,30 @@ class ParquetQuerySuite extends QueryTest with ParquetTest with SharedSQLContext
checkAnswer(sqlContext.read.parquet(path), df)
}
}

test("returning batch for wide table") {
withSQLConf("spark.sql.codegen.maxFields" -> "100") {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks like the maxFields value can be set lower - resulting in faster test execution.

withTempPath { dir =>
val path = dir.getCanonicalPath
val df = sqlContext.range(100).select(Seq.tabulate(110) {i => ('id + i).as(s"c$i")} : _*)
df.write.mode(SaveMode.Overwrite).parquet(path)

// donot return batch, because whole stage codegen is disabled for wide table (>200 columns)
val df2 = sqlContext.read.parquet(path)
assert(df2.queryExecution.sparkPlan.find(_.isInstanceOf[BatchedDataSourceScanExec]).isEmpty,
"Should not return batch")
checkAnswer(df2, df)

// return batch
val columns = Seq.tabulate(90) {i => s"c$i"}
val df3 = df2.selectExpr(columns : _*)
assert(
df3.queryExecution.sparkPlan.find(_.isInstanceOf[BatchedDataSourceScanExec]).isDefined,
"Should not return batch")
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should be "Should return batch"

checkAnswer(df3, df.selectExpr(columns : _*))
}
}
}
}

object TestingUDT {
Expand Down