@@ -51,6 +51,45 @@ class MetastoreDataSourcesSuite extends QueryTest with BeforeAndAfterEach {
51
51
52
52
}
53
53
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
+
54
93
test(" resolve shortened provider names" ) {
55
94
sql(
56
95
"""
@@ -106,19 +145,20 @@ class MetastoreDataSourcesSuite extends QueryTest with BeforeAndAfterEach {
106
145
(" a" , " b" ) :: Nil )
107
146
108
147
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)
110
149
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.
112
152
checkAnswer(
113
153
sql(" SELECT * FROM jsonTable" ),
114
- (" a " , " b " ) :: Nil )
154
+ (" a1 " , " b1 " ) :: Nil )
115
155
116
156
refreshTable(" jsonTable" )
117
157
118
158
// Check that the refresh worked
119
159
checkAnswer(
120
160
sql(" SELECT * FROM jsonTable" ),
121
- (" a " , " b " , " c " ) :: Nil )
161
+ (" a1 " , " b1 " , " c1 " ) :: Nil )
122
162
FileUtils .deleteDirectory(tempDir)
123
163
}
124
164
@@ -160,4 +200,4 @@ class MetastoreDataSourcesSuite extends QueryTest with BeforeAndAfterEach {
160
200
(" a" , " b" , " c" ) :: Nil )
161
201
FileUtils .deleteDirectory(tempDir)
162
202
}
163
- }
203
+ }
0 commit comments