Skip to content

Commit 9414578

Browse files
committed
[SPARK-25908][SQL][FOLLOW-UP] Add back unionAll
## What changes were proposed in this pull request? This PR is to add back `unionAll`, which is widely used. The name is also consistent with our ANSI SQL. We also have the corresponding `intersectAll` and `exceptAll`, which were introduced in Spark 2.4. ## How was this patch tested? Added a test case in DataFrameSuite Closes apache#23131 from gatorsmile/addBackUnionAll. Authored-by: gatorsmile <[email protected]> Signed-off-by: gatorsmile <[email protected]>
1 parent c5daccb commit 9414578

File tree

9 files changed

+53
-1
lines changed

9 files changed

+53
-1
lines changed

R/pkg/NAMESPACE

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -169,6 +169,7 @@ exportMethods("arrange",
169169
"toJSON",
170170
"transform",
171171
"union",
172+
"unionAll",
172173
"unionByName",
173174
"unique",
174175
"unpersist",

R/pkg/R/DataFrame.R

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2732,6 +2732,20 @@ setMethod("union",
27322732
dataFrame(unioned)
27332733
})
27342734

2735+
#' Return a new SparkDataFrame containing the union of rows
2736+
#'
2737+
#' This is an alias for `union`.
2738+
#'
2739+
#' @rdname union
2740+
#' @name unionAll
2741+
#' @aliases unionAll,SparkDataFrame,SparkDataFrame-method
2742+
#' @note unionAll since 1.4.0
2743+
setMethod("unionAll",
2744+
signature(x = "SparkDataFrame", y = "SparkDataFrame"),
2745+
function(x, y) {
2746+
union(x, y)
2747+
})
2748+
27352749
#' Return a new SparkDataFrame containing the union of rows, matched by column names
27362750
#'
27372751
#' Return a new SparkDataFrame containing the union of rows in this SparkDataFrame

R/pkg/R/generics.R

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -631,6 +631,9 @@ setGeneric("toRDD", function(x) { standardGeneric("toRDD") })
631631
#' @rdname union
632632
setGeneric("union", function(x, y) { standardGeneric("union") })
633633

634+
#' @rdname union
635+
setGeneric("unionAll", function(x, y) { standardGeneric("unionAll") })
636+
634637
#' @rdname unionByName
635638
setGeneric("unionByName", function(x, y) { standardGeneric("unionByName") })
636639

R/pkg/tests/fulltests/test_sparkSQL.R

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2458,6 +2458,7 @@ test_that("union(), unionByName(), rbind(), except(), and intersect() on a DataF
24582458
expect_equal(count(unioned), 6)
24592459
expect_equal(first(unioned)$name, "Michael")
24602460
expect_equal(count(arrange(suppressWarnings(union(df, df2)), df$age)), 6)
2461+
expect_equal(count(arrange(suppressWarnings(unionAll(df, df2)), df$age)), 6)
24612462

24622463
df1 <- select(df2, "age", "name")
24632464
unioned1 <- arrange(unionByName(df1, df), df1$age)

docs/sparkr.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -718,4 +718,4 @@ You can inspect the search path in R with [`search()`](https://stat.ethz.ch/R-ma
718718
## Upgrading to SparkR 3.0.0
719719

720720
- The deprecated methods `sparkR.init`, `sparkRSQL.init`, `sparkRHive.init` have been removed. Use `sparkR.session` instead.
721-
- The deprecated methods `parquetFile`, `saveAsParquetFile`, `jsonFile`, `registerTempTable`, `createExternalTable`, `dropTempTable`, `unionAll` have been removed. Use `read.parquet`, `write.parquet`, `read.json`, `createOrReplaceTempView`, `createTable`, `dropTempView`, `union` instead.
721+
- The deprecated methods `parquetFile`, `saveAsParquetFile`, `jsonFile`, `registerTempTable`, `createExternalTable`, and `dropTempTable` have been removed. Use `read.parquet`, `write.parquet`, `read.json`, `createOrReplaceTempView`, `createTable`, `dropTempView`, `union` instead.

docs/sql-migration-guide-upgrade.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ displayTitle: Spark SQL Upgrading Guide
99

1010
## Upgrading From Spark SQL 2.4 to 3.0
1111

12+
- Since Spark 3.0, the Dataset and DataFrame API `unionAll` is not deprecated any more. It is an alias for `union`.
13+
1214
- In PySpark, when creating a `SparkSession` with `SparkSession.builder.getOrCreate()`, if there is an existing `SparkContext`, the builder was trying to update the `SparkConf` of the existing `SparkContext` with configurations specified to the builder, but the `SparkContext` is shared by all `SparkSession`s, so we should not update them. Since 3.0, the builder comes to not update the configurations. This is the same behavior as Java/Scala API in 2.3 and above. If you want to update them, you need to update them prior to creating a `SparkSession`.
1315

1416
- In Spark version 2.4 and earlier, the parser of JSON data source treats empty strings as null for some data types such as `IntegerType`. For `FloatType` and `DoubleType`, it fails on empty strings and throws exceptions. Since Spark 3.0, we disallow empty strings and will throw exceptions for data types except for `StringType` and `BinaryType`.

python/pyspark/sql/dataframe.py

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1448,6 +1448,17 @@ def union(self, other):
14481448
"""
14491449
return DataFrame(self._jdf.union(other._jdf), self.sql_ctx)
14501450

1451+
@since(1.3)
1452+
def unionAll(self, other):
1453+
""" Return a new :class:`DataFrame` containing union of rows in this and another frame.
1454+
1455+
This is equivalent to `UNION ALL` in SQL. To do a SQL-style set union
1456+
(that does deduplication of elements), use this function followed by :func:`distinct`.
1457+
1458+
Also as standard in SQL, this function resolves columns by position (not by name).
1459+
"""
1460+
return self.union(other)
1461+
14511462
@since(2.3)
14521463
def unionByName(self, other):
14531464
""" Returns a new :class:`DataFrame` containing union of rows in this and another frame.

sql/core/src/main/scala/org/apache/spark/sql/Dataset.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1852,6 +1852,20 @@ class Dataset[T] private[sql](
18521852
CombineUnions(Union(logicalPlan, other.logicalPlan))
18531853
}
18541854

1855+
/**
1856+
* Returns a new Dataset containing union of rows in this Dataset and another Dataset.
1857+
* This is an alias for `union`.
1858+
*
1859+
* This is equivalent to `UNION ALL` in SQL. To do a SQL-style set union (that does
1860+
* deduplication of elements), use this function followed by a [[distinct]].
1861+
*
1862+
* Also as standard in SQL, this function resolves columns by position (not by name).
1863+
*
1864+
* @group typedrel
1865+
* @since 2.0.0
1866+
*/
1867+
def unionAll(other: Dataset[T]): Dataset[T] = union(other)
1868+
18551869
/**
18561870
* Returns a new Dataset containing union of rows in this Dataset and another Dataset.
18571871
*

sql/core/src/test/scala/org/apache/spark/sql/DataFrameSuite.scala

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,12 @@ class DataFrameSuite extends QueryTest with SharedSQLContext {
9797
unionDF.agg(avg('key), max('key), min('key), sum('key)),
9898
Row(50.5, 100, 1, 25250) :: Nil
9999
)
100+
101+
// unionAll is an alias of union
102+
val unionAllDF = testData.unionAll(testData).unionAll(testData)
103+
.unionAll(testData).unionAll(testData)
104+
105+
checkAnswer(unionDF, unionAllDF)
100106
}
101107

102108
test("union should union DataFrames with UDTs (SPARK-13410)") {

0 commit comments

Comments
 (0)