Skip to content

Commit f47fda1

Browse files
committed
Unit tests.
1 parent 2b59723 commit f47fda1

File tree

2 files changed

+46
-7
lines changed

2 files changed

+46
-7
lines changed

sql/core/src/main/scala/org/apache/spark/sql/execution/commands.scala

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,6 @@ case class DescribeCommand(
177177
override val output: Seq[Attribute]) extends RunnableCommand {
178178

179179
override def run(sqlContext: SQLContext) = {
180-
Row("# Registered as a temporary table", null, null) +:
181-
child.output.map(field => Row(field.name, field.dataType.toString, null))
180+
child.output.map(field => Row(field.name, field.dataType.toString, null))
182181
}
183182
}

sql/hive/src/test/scala/org/apache/spark/sql/hive/MetastoreDataSourcesSuite.scala

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,45 @@ class MetastoreDataSourcesSuite extends QueryTest with BeforeAndAfterEach {
5151

5252
}
5353

54+
test ("persistent JSON table with a user specified schema") {
55+
sql(
56+
"""
57+
|CREATE TABLE jsonTable (a string, b String)
58+
|USING org.apache.spark.sql.json.DefaultSource
59+
|OPTIONS (
60+
| path 'src/test/resources/data/files/sample.json'
61+
|)
62+
""".stripMargin)
63+
64+
checkAnswer(
65+
sql("SELECT * FROM jsonTable"),
66+
jsonFile("src/test/resources/data/files/sample.json").collect().toSeq)
67+
68+
}
69+
70+
test ("persistent JSON table with a user specified schema with a subset of fields") {
71+
// This works because JSON objects are self-describing and JSONRelation can get needed
72+
// field values based on field names.
73+
sql(
74+
"""
75+
|CREATE TABLE jsonTable (b String)
76+
|USING org.apache.spark.sql.json.DefaultSource
77+
|OPTIONS (
78+
| path 'src/test/resources/data/files/sample.json'
79+
|)
80+
""".stripMargin)
81+
82+
val expectedSchema = StructType(StructField("b", StringType, true) :: Nil)
83+
84+
assert(expectedSchema == table("jsonTable").schema)
85+
86+
checkAnswer(
87+
sql("SELECT * FROM jsonTable"),
88+
jsonFile("src/test/resources/data/files/sample.json").collect().map(
89+
r => Seq(r.getString(1))).toSeq)
90+
91+
}
92+
5493
test("resolve shortened provider names") {
5594
sql(
5695
"""
@@ -106,19 +145,20 @@ class MetastoreDataSourcesSuite extends QueryTest with BeforeAndAfterEach {
106145
("a", "b") :: Nil)
107146

108147
FileUtils.deleteDirectory(tempDir)
109-
sparkContext.parallelize(("a", "b", "c") :: Nil).toJSON.saveAsTextFile(tempDir.getCanonicalPath)
148+
sparkContext.parallelize(("a1", "b1", "c1") :: Nil).toJSON.saveAsTextFile(tempDir.getCanonicalPath)
110149

111-
// Schema is cached so answer does not change.
150+
// Schema is cached so the new column does not show. The updated values in existing columns
151+
// will show.
112152
checkAnswer(
113153
sql("SELECT * FROM jsonTable"),
114-
("a", "b") :: Nil)
154+
("a1", "b1") :: Nil)
115155

116156
refreshTable("jsonTable")
117157

118158
// Check that the refresh worked
119159
checkAnswer(
120160
sql("SELECT * FROM jsonTable"),
121-
("a", "b", "c") :: Nil)
161+
("a1", "b1", "c1") :: Nil)
122162
FileUtils.deleteDirectory(tempDir)
123163
}
124164

@@ -160,4 +200,4 @@ class MetastoreDataSourcesSuite extends QueryTest with BeforeAndAfterEach {
160200
("a", "b", "c") :: Nil)
161201
FileUtils.deleteDirectory(tempDir)
162202
}
163-
}
203+
}

0 commit comments

Comments
 (0)