Skip to content
Draft
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
8 changes: 7 additions & 1 deletion native/shuffle/src/spark_unsafe/list.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use arrow::array::{
builder::{
ArrayBuilder, BinaryBuilder, BooleanBuilder, Date32Builder, Decimal128Builder,
Float32Builder, Float64Builder, Int16Builder, Int32Builder, Int64Builder, Int8Builder,
ListBuilder, StringBuilder, StructBuilder, TimestampMicrosecondBuilder,
ListBuilder, NullBuilder, StringBuilder, StructBuilder, TimestampMicrosecondBuilder,
},
MapBuilder,
};
Expand Down Expand Up @@ -393,6 +393,12 @@ pub fn append_to_builder<const NULLABLE: bool>(
let builder = downcast_builder_ref!(Date32Builder, builder);
array.append_dates_to_builder::<NULLABLE>(builder);
}
DataType::Null => {
let builder = downcast_builder_ref!(NullBuilder, builder);
for _ in 0..array.get_num_elements() {
builder.append_null();
}
}
DataType::Binary => {
add_values!(
BinaryBuilder,
Expand Down
15 changes: 13 additions & 2 deletions native/shuffle/src/spark_unsafe/row.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,8 @@ use arrow::array::{
builder::{
ArrayBuilder, BinaryBuilder, BinaryDictionaryBuilder, BooleanBuilder, Date32Builder,
Decimal128Builder, Float32Builder, Float64Builder, Int16Builder, Int32Builder,
Int64Builder, Int8Builder, ListBuilder, MapBuilder, StringBuilder, StringDictionaryBuilder,
StructBuilder, TimestampMicrosecondBuilder,
Int64Builder, Int8Builder, ListBuilder, MapBuilder, NullBuilder, StringBuilder,
StringDictionaryBuilder, StructBuilder, TimestampMicrosecondBuilder,
},
types::Int32Type,
Array, ArrayRef, RecordBatch, RecordBatchOptions,
Expand Down Expand Up @@ -267,6 +267,10 @@ pub(super) fn append_field(
append_field_to_builder!(Date32Builder, |builder: &mut Date32Builder| builder
.append_value(row.get_date(idx)));
}
DataType::Null => {
let field_builder = get_field_builder!(struct_builder, NullBuilder, idx);
field_builder.append_null();
}
DataType::Timestamp(TimeUnit::Microsecond, _) => {
append_field_to_builder!(
TimestampMicrosecondBuilder,
Expand Down Expand Up @@ -1148,6 +1152,12 @@ fn append_columns(
.append_value(row.get_date(idx))
);
}
DataType::Null => {
let null_builder = downcast_builder_ref!(NullBuilder, builder);
for _ in row_start..row_end {
null_builder.append_null();
}
}
DataType::Timestamp(TimeUnit::Microsecond, _) => {
append_column_to_builder!(
TimestampMicrosecondBuilder,
Expand Down Expand Up @@ -1252,6 +1262,7 @@ fn make_builders(
}
}
DataType::Date32 => Box::new(Date32Builder::with_capacity(row_num)),
DataType::Null => Box::new(NullBuilder::new()),
DataType::Timestamp(TimeUnit::Microsecond, _) => {
Box::new(TimestampMicrosecondBuilder::with_capacity(row_num).with_data_type(dt.clone()))
}
Expand Down
2 changes: 1 addition & 1 deletion spark/src/main/scala/org/apache/comet/CometConf.scala
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ object CometConf extends ShimCometConf {
val COMET_EXEC_TAKE_ORDERED_AND_PROJECT_ENABLED: ConfigEntry[Boolean] =
createExecEnabledConfig("takeOrderedAndProject", defaultValue = true)
val COMET_EXEC_LOCAL_TABLE_SCAN_ENABLED: ConfigEntry[Boolean] =
createExecEnabledConfig("localTableScan", defaultValue = false)
createExecEnabledConfig("localTableScan", defaultValue = true)

val COMET_NATIVE_COLUMNAR_TO_ROW_ENABLED: ConfigEntry[Boolean] =
conf(s"$COMET_EXEC_CONFIG_PREFIX.columnarToRow.native.enabled")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@

package org.apache.spark.sql.comet

import scala.collection.mutable.ListBuffer

import org.apache.spark.TaskContext
import org.apache.spark.rdd.RDD
import org.apache.spark.sql.catalyst.InternalRow
Expand All @@ -27,11 +29,13 @@ import org.apache.spark.sql.comet.CometLocalTableScanExec.createMetricsIterator
import org.apache.spark.sql.comet.execution.arrow.CometArrowConverters
import org.apache.spark.sql.execution.{LeafExecNode, LocalTableScanExec}
import org.apache.spark.sql.execution.metric.{SQLMetric, SQLMetrics}
import org.apache.spark.sql.types.{DataType, NullType}
import org.apache.spark.sql.vectorized.ColumnarBatch

import com.google.common.base.Objects

import org.apache.comet.{CometConf, ConfigEntry}
import org.apache.comet.{CometConf, ConfigEntry, DataTypeSupport}
import org.apache.comet.CometSparkSessionExtensions.withInfo
import org.apache.comet.serde.OperatorOuterClass.Operator
import org.apache.comet.serde.operator.CometSink

Expand Down Expand Up @@ -104,14 +108,38 @@ case class CometLocalTableScanExec(
override def hashCode(): Int = Objects.hashCode(originalPlan, originalPlan.schema, output)
}

object CometLocalTableScanExec extends CometSink[LocalTableScanExec] {
object CometLocalTableScanExec extends CometSink[LocalTableScanExec] with DataTypeSupport {

// uses CometArrowConverters, which re-uses arrays
override def isFfiSafe: Boolean = false

override def enabledConfig: Option[ConfigEntry[Boolean]] = Some(
CometConf.COMET_EXEC_LOCAL_TABLE_SCAN_ENABLED)

// CometArrowConverters / ArrowWriter support NullType (via Utils.toArrowType +
// NullWriter). Other types not on DataTypeSupport's allow list (e.g. TimeType,
// intervals) lack ArrowWriter coverage and must fall back to Spark.
override def isTypeSupported(
dt: DataType,
name: String,
fallbackReasons: ListBuffer[String]): Boolean = dt match {
case _: NullType => true
case _ => super.isTypeSupported(dt, name, fallbackReasons)
}

override def convert(
op: LocalTableScanExec,
builder: Operator.Builder,
childOp: Operator*): Option[Operator] = {
val fallbackReasons = new ListBuffer[String]()
if (!isSchemaSupported(op.schema, fallbackReasons)) {
withInfo(op, fallbackReasons.mkString("; "))
None
} else {
super.convert(op, builder, childOp: _*)
}
}

override def createExec(nativeOp: Operator, op: LocalTableScanExec): CometNativeExec = {
CometScanWrapper(nativeOp, CometLocalTableScanExec(op, op.rows, op.output))
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -40,7 +40,7 @@ import org.apache.spark.sql.execution.adaptive.ShuffleQueryStageExec
import org.apache.spark.sql.execution.exchange.{ENSURE_REQUIREMENTS, ShuffleExchangeExec, ShuffleExchangeLike, ShuffleOrigin}
import org.apache.spark.sql.execution.metric.{SQLMetric, SQLMetrics, SQLShuffleReadMetricsReporter, SQLShuffleWriteMetricsReporter}
import org.apache.spark.sql.internal.SQLConf
import org.apache.spark.sql.types.{ArrayType, BinaryType, BooleanType, ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, MapType, ShortType, StringType, StructType, TimestampNTZType, TimestampType}
import org.apache.spark.sql.types.{ArrayType, BinaryType, BooleanType, ByteType, DataType, DateType, DecimalType, DoubleType, FloatType, IntegerType, LongType, MapType, NullType, ShortType, StringType, StructType, TimestampNTZType, TimestampType}
import org.apache.spark.sql.vectorized.ColumnarBatch
import org.apache.spark.util.MutablePair
import org.apache.spark.util.collection.unsafe.sort.{PrefixComparators, RecordComparator}
Expand Down Expand Up @@ -364,7 +364,7 @@ object CometShuffleExchangeExec
def supportedSerializableDataType(dt: DataType): Boolean = dt match {
case _: BooleanType | _: ByteType | _: ShortType | _: IntegerType | _: LongType |
_: FloatType | _: DoubleType | _: StringType | _: BinaryType | _: TimestampType |
_: TimestampNTZType | _: DecimalType | _: DateType =>
_: TimestampNTZType | _: DecimalType | _: DateType | _: NullType =>
true
case StructType(fields) =>
fields.nonEmpty && fields.forall(f => supportedSerializableDataType(f.dataType))
Expand Down Expand Up @@ -487,7 +487,7 @@ object CometShuffleExchangeExec
def supportedSerializableDataType(dt: DataType): Boolean = dt match {
case _: BooleanType | _: ByteType | _: ShortType | _: IntegerType | _: LongType |
_: FloatType | _: DoubleType | _: StringType | _: BinaryType | _: TimestampType |
_: TimestampNTZType | _: DecimalType | _: DateType =>
_: TimestampNTZType | _: DecimalType | _: DateType | _: NullType =>
true
case StructType(fields) =>
fields.nonEmpty && fields.forall(f => supportedSerializableDataType(f.dataType)) &&
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,7 @@ object Utils extends CometTypeShim with Logging {
}
case TimestampNTZType =>
new ArrowType.Timestamp(TimeUnit.MICROSECOND, null)
case NullType => ArrowType.Null.INSTANCE
case dt if isTimeType(dt) =>
new ArrowType.Time(TimeUnit.NANOSECOND, 64)
case _ =>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,14 +22,13 @@ package org.apache.comet.exec
import java.nio.file.{Files, Paths}

import scala.reflect.runtime.universe._
import scala.util.Random

import org.scalactic.source.Position
import org.scalatest.Tag

import org.apache.hadoop.fs.Path
import org.apache.spark.{Partitioner, SparkConf}
import org.apache.spark.sql.{CometTestBase, DataFrame, RandomDataGenerator, Row}
import org.apache.spark.sql.{CometTestBase, DataFrame, Row}
import org.apache.spark.sql.comet.execution.shuffle.{CometShuffleDependency, CometShuffleExchangeExec, CometShuffleManager}
import org.apache.spark.sql.execution.adaptive.{AdaptiveSparkPlanHelper, AQEShuffleReadExec, ShuffleQueryStageExec}
import org.apache.spark.sql.execution.exchange.ReusedExchangeExec
Expand Down Expand Up @@ -94,22 +93,16 @@ abstract class CometColumnarShuffleSuite extends CometTestBase with AdaptiveSpar
""".stripMargin))
}

test("Fallback to Spark for unsupported input besides ordering") {
val dataGenerator = RandomDataGenerator
.forType(
dataType = NullType,
nullable = true,
new Random(System.nanoTime()),
validJulianDatetime = false)
.get

val schema = new StructType()
.add("index", IntegerType, nullable = false)
.add("col", NullType, nullable = true)
val rdd =
spark.sparkContext.parallelize((1 to 20).map(i => Row(i, dataGenerator())))
val df = spark.createDataFrame(rdd, schema).orderBy("index").coalesce(1)
checkSparkAnswer(df)
test("columnar shuffle with NullType passthrough column") {
val df = sql("SELECT x, y FROM VALUES ('a', null), ('b', null), ('c', null) AS t(x, y)")
val shuffled = df.repartition(2, $"x")
checkShuffleAnswer(shuffled, 1)
}

test("columnar shuffle with Map[_, NullType] column") {
val df = sql("SELECT id, map(id, null) AS m FROM VALUES (1), (2), (3) AS t(id)")
val shuffled = df.repartition(2, $"id")
checkShuffleAnswer(shuffled, 1)
}

test("columnar shuffle on nested struct including nulls") {
Expand Down
31 changes: 31 additions & 0 deletions spark/src/test/scala/org/apache/comet/exec/CometExecSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -3925,6 +3925,37 @@ class CometExecSuite extends CometTestBase {
}
}

test("CometLocalTableScanExec handles NullType column") {
withSQLConf(CometConf.COMET_EXEC_LOCAL_TABLE_SCAN_ENABLED.key -> "true") {
val df = spark.sql("SELECT * FROM VALUES ('a', null), ('b', null) AS t(x, y)")
checkSparkAnswer(df)
}
}

test("CometLocalTableScanExec handles NullType nested in struct/array/map") {
withSQLConf(CometConf.COMET_EXEC_LOCAL_TABLE_SCAN_ENABLED.key -> "true") {
checkSparkAnswer(
spark.sql("SELECT named_struct('a', 1, 'b', null) AS s, array(null, null) AS a, " +
"map('k', null) AS m"))
}
}

test("CometLocalTableScanExec falls back when schema contains TimeType") {
assume(
org.apache.comet.CometSparkSessionExtensions.isSpark41Plus,
"TimeType requires Spark 4.1+")
// spark.sql.timeType.enabled defaults to Utils.isTesting; enable explicitly so the
// row encoder accepts TIME (matches Spark's own TimeFunctionsSuiteBase setup).
withSQLConf(
"spark.sql.timeType.enabled" -> "true",
CometConf.COMET_EXEC_LOCAL_TABLE_SCAN_ENABLED.key -> "true") {
// VALUES folds to a LocalRelation, exercising the CometLocalTableScanExec convert
// path; the TimeType column should drive the schema-level fallback.
val df = spark.sql("SELECT * FROM VALUES (TIME '12:34:56'), (TIME '01:02:03') AS t(c)")
checkSparkAnswer(df)
}
}

test("Native_datafusion reports correct files and bytes scanned") {
val inputFiles = 2

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -218,6 +218,18 @@ class CometNativeShuffleSuite extends CometTestBase with AdaptiveSparkPlanHelper
}
}

test("native shuffle with NullType passthrough column") {
val df = spark.sql("SELECT x, y FROM VALUES ('a', null), ('b', null), ('c', null) AS t(x, y)")
val shuffled = df.repartition(2, $"x")
checkShuffleAnswer(shuffled, 1)
}

test("native shuffle with Map[_, NullType] column") {
val df = spark.sql("SELECT id, map(id, null) AS m FROM VALUES (1), (2), (3) AS t(id)")
val shuffled = df.repartition(2, $"id")
checkShuffleAnswer(shuffled, 1)
}

test("fix: Comet native shuffle with binary data") {
withParquetTable((0 until 5).map(i => (i, (i + 1).toLong)), "tbl") {
val df = sql("SELECT cast(cast(_1 as STRING) as BINARY) as binary, _2 FROM tbl")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,7 @@ class CometWindowExecSuite extends CometTestBase {
val cometShuffles = collect(df2.queryExecution.executedPlan) {
case _: CometShuffleExchangeExec => true
}
if (shuffleMode == "jvm" || shuffleMode == "auto") {
assert(cometShuffles.length == 1)
} else {
// we fall back to Spark for shuffle because we do not support
// native shuffle with a LocalTableScan input, and we do not fall
// back to Comet columnar shuffle due to
// https://github.com/apache/datafusion-comet/issues/1248
assert(cometShuffles.isEmpty)
}
assert(cometShuffles.length == 1)
}
}
}
Expand Down
Loading