@@ -21,7 +21,7 @@ import java.io.File
21
21
22
22
import org .scalatest .BeforeAndAfterAll
23
23
24
- import org .apache .spark .sql .{AnalysisException , Row }
24
+ import org .apache .spark .sql .{SaveMode , AnalysisException , Row }
25
25
import org .apache .spark .util .Utils
26
26
27
27
class InsertSuite extends DataSourceTest with BeforeAndAfterAll {
@@ -100,23 +100,48 @@ class InsertSuite extends DataSourceTest with BeforeAndAfterAll {
100
100
test(" INSERT OVERWRITE a JSONRelation multiple times" ) {
101
101
sql(
102
102
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
+ )
105
109
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" )
106
113
sql(
107
114
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
+ )
110
121
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" )
111
125
sql(
112
126
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)
116
129
checkAnswer(
117
130
sql(" SELECT a, b FROM jsonTable" ),
118
131
(1 to 10 ).map(i => Row (i, s " str $i" ))
119
132
)
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" )
120
145
}
121
146
122
147
test(" INSERT INTO not supported for JSONRelation for now" ) {
@@ -128,6 +153,20 @@ class InsertSuite extends DataSourceTest with BeforeAndAfterAll {
128
153
}
129
154
}
130
155
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
+
131
170
test(" it is not allowed to write to a table while querying it." ) {
132
171
val message = intercept[AnalysisException ] {
133
172
sql(
0 commit comments