Skip to content

Commit a7ebeb2

Browse files
author
Nathan Howell
committed
Increase coverage of inserts into a JSONRelation
1 parent e06a1dd commit a7ebeb2

File tree

1 file changed

+47
-8
lines changed

1 file changed

+47
-8
lines changed

sql/core/src/test/scala/org/apache/spark/sql/sources/InsertSuite.scala

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import java.io.File
2121

2222
import org.scalatest.BeforeAndAfterAll
2323

24-
import org.apache.spark.sql.{AnalysisException, Row}
24+
import org.apache.spark.sql.{SaveMode, AnalysisException, Row}
2525
import org.apache.spark.util.Utils
2626

2727
class InsertSuite extends DataSourceTest with BeforeAndAfterAll {
@@ -100,23 +100,48 @@ class InsertSuite extends DataSourceTest with BeforeAndAfterAll {
100100
test("INSERT OVERWRITE a JSONRelation multiple times") {
101101
sql(
102102
s"""
103-
|INSERT OVERWRITE TABLE jsonTable SELECT a, b FROM jt
104-
""".stripMargin)
103+
|INSERT OVERWRITE TABLE jsonTable SELECT a, b FROM jt
104+
""".stripMargin)
105+
checkAnswer(
106+
sql("SELECT a, b FROM jsonTable"),
107+
(1 to 10).map(i => Row(i, s"str$i"))
108+
)
105109

110+
// Writing the table to less part files.
111+
val rdd1 = sparkContext.parallelize((1 to 10).map(i => s"""{"a":$i, "b":"str${i}"}"""), 5)
112+
jsonRDD(rdd1).registerTempTable("jt1")
106113
sql(
107114
s"""
108-
|INSERT OVERWRITE TABLE jsonTable SELECT a, b FROM jt
109-
""".stripMargin)
115+
|INSERT OVERWRITE TABLE jsonTable SELECT a, b FROM jt1
116+
""".stripMargin)
117+
checkAnswer(
118+
sql("SELECT a, b FROM jsonTable"),
119+
(1 to 10).map(i => Row(i, s"str$i"))
120+
)
110121

122+
// Writing the table to more part files.
123+
val rdd2 = sparkContext.parallelize((1 to 10).map(i => s"""{"a":$i, "b":"str${i}"}"""), 10)
124+
jsonRDD(rdd2).registerTempTable("jt2")
111125
sql(
112126
s"""
113-
|INSERT OVERWRITE TABLE jsonTable SELECT a, b FROM jt
114-
""".stripMargin)
115-
127+
|INSERT OVERWRITE TABLE jsonTable SELECT a, b FROM jt2
128+
""".stripMargin)
116129
checkAnswer(
117130
sql("SELECT a, b FROM jsonTable"),
118131
(1 to 10).map(i => Row(i, s"str$i"))
119132
)
133+
134+
sql(
135+
s"""
136+
|INSERT OVERWRITE TABLE jsonTable SELECT a * 10, b FROM jt1
137+
""".stripMargin)
138+
checkAnswer(
139+
sql("SELECT a, b FROM jsonTable"),
140+
(1 to 10).map(i => Row(i * 10, s"str$i"))
141+
)
142+
143+
dropTempTable("jt1")
144+
dropTempTable("jt2")
120145
}
121146

122147
test("INSERT INTO not supported for JSONRelation for now") {
@@ -128,6 +153,20 @@ class InsertSuite extends DataSourceTest with BeforeAndAfterAll {
128153
}
129154
}
130155

156+
test("save directly to the path of a JSON table") {
157+
table("jt").selectExpr("a * 5 as a", "b").save(path.toString, "json", SaveMode.Overwrite)
158+
checkAnswer(
159+
sql("SELECT a, b FROM jsonTable"),
160+
(1 to 10).map(i => Row(i * 5, s"str$i"))
161+
)
162+
163+
table("jt").save(path.toString, "json", SaveMode.Overwrite)
164+
checkAnswer(
165+
sql("SELECT a, b FROM jsonTable"),
166+
(1 to 10).map(i => Row(i, s"str$i"))
167+
)
168+
}
169+
131170
test("it is not allowed to write to a table while querying it.") {
132171
val message = intercept[AnalysisException] {
133172
sql(

0 commit comments

Comments
 (0)