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
16 changes: 13 additions & 3 deletions sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala
Original file line number Diff line number Diff line change
Expand Up @@ -1456,7 +1456,7 @@ class Dataset[T] private[sql](
* @group typedrel
* @since 2.0.0
*/
def union(other: Dataset[T]): Dataset[T] = withTypedPlan {
def union(other: Dataset[T]): Dataset[T] = withSetOperator {
// This breaks caching, but it's usually ok because it addresses a very specific use case:
// using union to union many files or partitions.
CombineUnions(Union(logicalPlan, other.logicalPlan))
Expand All @@ -1472,7 +1472,7 @@ class Dataset[T] private[sql](
* @group typedrel
* @since 1.6.0
*/
def intersect(other: Dataset[T]): Dataset[T] = withTypedPlan {
def intersect(other: Dataset[T]): Dataset[T] = withSetOperator {
Intersect(logicalPlan, other.logicalPlan)
}

Expand All @@ -1486,7 +1486,7 @@ class Dataset[T] private[sql](
* @group typedrel
* @since 2.0.0
*/
def except(other: Dataset[T]): Dataset[T] = withTypedPlan {
def except(other: Dataset[T]): Dataset[T] = withSetOperator {
Except(logicalPlan, other.logicalPlan)
}

Expand Down Expand Up @@ -2607,4 +2607,14 @@ class Dataset[T] private[sql](
@inline private def withTypedPlan[U : Encoder](logicalPlan: => LogicalPlan): Dataset[U] = {
Dataset(sparkSession, logicalPlan)
}

/** A convenient function to wrap a set based logical plan and produce a Dataset. */
@inline private def withSetOperator[U : Encoder](logicalPlan: => LogicalPlan): Dataset[U] = {
if (classTag.runtimeClass.isAssignableFrom(classOf[Row])) {
// Set operators widen types (change the schema), so we cannot reuse the row encoder.
Dataset.ofRows(sparkSession, logicalPlan).asInstanceOf[Dataset[U]]
} else {
Dataset(sparkSession, logicalPlan)
}
}
}
16 changes: 16 additions & 0 deletions sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package org.apache.spark.sql

import java.io.File
import java.nio.charset.StandardCharsets
import java.sql.{Date, Timestamp}
import java.util.UUID

import scala.language.postfixOps
Expand Down Expand Up @@ -1585,4 +1586,19 @@ class DataFrameSuite extends QueryTest with SharedSQLContext {
}
}
}

test("SPARK-17123: Performing set operations that combine non-scala native types") {
val dates = Seq(
(BigDecimal.valueOf(1), new Timestamp(2)),
(BigDecimal.valueOf(4), new Timestamp(5))
).toDF("decimal", "timestamp")

val widenTypedRows = Seq(
(10.5D, "string")
).toDF("decimal", "timestamp")

dates.union(widenTypedRows).collect()
dates.except(widenTypedRows).collect()
dates.intersect(widenTypedRows).collect()
}
}