Skip to content

Commit

Permalink
add dropFields user-facing examples
Browse files Browse the repository at this point in the history
  • Loading branch information
fqaiser94 committed Aug 4, 2020
1 parent 7342514 commit 948fc9c
Showing 1 changed file with 43 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -1452,7 +1452,7 @@ class ColumnExpressionSuite extends QueryTest with SharedSparkSession {
.select($"struct_col".withField("a.c", lit(3)))
}.getMessage should include("Ambiguous reference to fields")
}

test("dropFields should throw an exception if called on a non-StructType column") {
intercept[AnalysisException] {
testData.withColumn("key", $"key".dropFields("a"))
Expand Down Expand Up @@ -1759,4 +1759,46 @@ class ColumnExpressionSuite extends QueryTest with SharedSparkSession {
StructField("c", IntegerType, nullable = false))), nullable = false))),
nullable = false))))
}

test("dropFields user-facing examples") {
checkAnswer(
sql("SELECT named_struct('a', 1, 'b', 2) struct_col")
.select($"struct_col".dropFields("b")),
Row(Row(1)))

checkAnswer(
sql("SELECT named_struct('a', 1, 'b', 2) struct_col")
.select($"struct_col".dropFields("c")),
Row(Row(1, 2)))

checkAnswer(
sql("SELECT named_struct('a', 1, 'b', 2, 'c', 3) struct_col")
.select($"struct_col".dropFields("b", "c")),
Row(Row(1)))

intercept[AnalysisException] {
sql("SELECT named_struct('a', 1, 'b', 2) struct_col")
.select($"struct_col".dropFields("a", "b"))
}.getMessage should include("cannot drop all fields in struct")

checkAnswer(
sql("SELECT CAST(NULL AS struct<a:int,b:int>) struct_col")
.select($"struct_col".dropFields("b")),
Row(null))

checkAnswer(
sql("SELECT named_struct('a', 1, 'b', 2, 'b', 3) struct_col")
.select($"struct_col".dropFields("b")),
Row(Row(1)))

checkAnswer(
sql("SELECT named_struct('a', named_struct('a', 1, 'b', 2)) struct_col")
.select($"struct_col".dropFields("a.b")),
Row(Row(Row(1))))

intercept[AnalysisException] {
sql("SELECT named_struct('a', named_struct('b', 1), 'a', named_struct('c', 2)) struct_col")
.select($"struct_col".dropFields("a.c"))
}.getMessage should include("Ambiguous reference to fields")
}
}

0 comments on commit 948fc9c

Please sign in to comment.