@@ -25,8 +25,6 @@ import org.apache.spark.sql.json.JsonRDD.{enforceCorrectType, compatibleType}
2525import org .apache .spark .sql .QueryTest
2626import org .apache .spark .sql .test .TestSQLContext ._
2727
28- protected case class Schema (output : Seq [Attribute ]) extends LeafNode
29-
3028class JsonSuite extends QueryTest {
3129 import TestJsonData ._
3230 TestJsonData
@@ -176,16 +174,16 @@ class JsonSuite extends QueryTest {
176174 test(" Primitive field and type inferring" ) {
177175 val jsonSchemaRDD = jsonRDD(primitiveFieldAndType)
178176
179- val expectedSchema =
180- AttributeReference (" bigInteger" , DecimalType , true )( ) ::
181- AttributeReference (" boolean" , BooleanType , true )( ) ::
182- AttributeReference (" double" , DoubleType , true )( ) ::
183- AttributeReference (" integer" , IntegerType , true )( ) ::
184- AttributeReference (" long" , LongType , true )( ) ::
185- AttributeReference (" null" , StringType , true )( ) ::
186- AttributeReference (" string" , StringType , true )() :: Nil
177+ val expectedSchema = StructType (
178+ StructField (" bigInteger" , DecimalType , true ) ::
179+ StructField (" boolean" , BooleanType , true ) ::
180+ StructField (" double" , DoubleType , true ) ::
181+ StructField (" integer" , IntegerType , true ) ::
182+ StructField (" long" , LongType , true ) ::
183+ StructField (" null" , StringType , true ) ::
184+ StructField (" string" , StringType , true ) :: Nil )
187185
188- comparePlans( Schema ( expectedSchema), Schema ( jsonSchemaRDD.logicalPlan.output) )
186+ assert( expectedSchema === jsonSchemaRDD.schema )
189187
190188 jsonSchemaRDD.registerAsTable(" jsonTable" )
191189
@@ -204,27 +202,28 @@ class JsonSuite extends QueryTest {
204202 test(" Complex field and type inferring" ) {
205203 val jsonSchemaRDD = jsonRDD(complexFieldAndType)
206204
207- val expectedSchema =
208- AttributeReference (" arrayOfArray1" , ArrayType (ArrayType (StringType )), true )() ::
209- AttributeReference (" arrayOfArray2" , ArrayType (ArrayType (DoubleType )), true )() ::
210- AttributeReference (" arrayOfBigInteger" , ArrayType (DecimalType ), true )() ::
211- AttributeReference (" arrayOfBoolean" , ArrayType (BooleanType ), true )() ::
212- AttributeReference (" arrayOfDouble" , ArrayType (DoubleType ), true )() ::
213- AttributeReference (" arrayOfInteger" , ArrayType (IntegerType ), true )() ::
214- AttributeReference (" arrayOfLong" , ArrayType (LongType ), true )() ::
215- AttributeReference (" arrayOfNull" , ArrayType (StringType , true ), true )() ::
216- AttributeReference (" arrayOfString" , ArrayType (StringType ), true )() ::
217- AttributeReference (" arrayOfStruct" , ArrayType (
218- StructType (StructField (" field1" , BooleanType , true ) ::
219- StructField (" field2" , StringType , true ) :: Nil )), true )() ::
220- AttributeReference (" struct" , StructType (
221- StructField (" field1" , BooleanType , true ) ::
222- StructField (" field2" , DecimalType , true ) :: Nil ), true )() ::
223- AttributeReference (" structWithArrayFields" , StructType (
205+ val expectedSchema = StructType (
206+ StructField (" arrayOfArray1" , ArrayType (ArrayType (StringType )), true ) ::
207+ StructField (" arrayOfArray2" , ArrayType (ArrayType (DoubleType )), true ) ::
208+ StructField (" arrayOfBigInteger" , ArrayType (DecimalType ), true ) ::
209+ StructField (" arrayOfBoolean" , ArrayType (BooleanType ), true ) ::
210+ StructField (" arrayOfDouble" , ArrayType (DoubleType ), true ) ::
211+ StructField (" arrayOfInteger" , ArrayType (IntegerType ), true ) ::
212+ StructField (" arrayOfLong" , ArrayType (LongType ), true ) ::
213+ StructField (" arrayOfNull" , ArrayType (StringType , true ), true ) ::
214+ StructField (" arrayOfString" , ArrayType (StringType ), true ) ::
215+ StructField (" arrayOfStruct" , ArrayType (
216+ StructType (
217+ StructField (" field1" , BooleanType , true ) ::
218+ StructField (" field2" , StringType , true ) :: Nil )), true ) ::
219+ StructField (" struct" , StructType (
220+ StructField (" field1" , BooleanType , true ) ::
221+ StructField (" field2" , DecimalType , true ) :: Nil ), true ) ::
222+ StructField (" structWithArrayFields" , StructType (
224223 StructField (" field1" , ArrayType (IntegerType ), true ) ::
225- StructField (" field2" , ArrayType (StringType ), true ) :: Nil ), true )() :: Nil
224+ StructField (" field2" , ArrayType (StringType ), true ) :: Nil ), true ) :: Nil )
226225
227- comparePlans( Schema ( expectedSchema), Schema ( jsonSchemaRDD.logicalPlan.output) )
226+ assert( expectedSchema === jsonSchemaRDD.schema )
228227
229228 jsonSchemaRDD.registerAsTable(" jsonTable" )
230229
@@ -313,15 +312,15 @@ class JsonSuite extends QueryTest {
313312 test(" Type conflict in primitive field values" ) {
314313 val jsonSchemaRDD = jsonRDD(primitiveFieldValueTypeConflict)
315314
316- val expectedSchema =
317- AttributeReference (" num_bool" , StringType , true )( ) ::
318- AttributeReference (" num_num_1" , LongType , true )( ) ::
319- AttributeReference (" num_num_2" , DecimalType , true )( ) ::
320- AttributeReference (" num_num_3" , DoubleType , true )( ) ::
321- AttributeReference (" num_str" , StringType , true )( ) ::
322- AttributeReference (" str_bool" , StringType , true )() :: Nil
315+ val expectedSchema = StructType (
316+ StructField (" num_bool" , StringType , true ) ::
317+ StructField (" num_num_1" , LongType , true ) ::
318+ StructField (" num_num_2" , DecimalType , true ) ::
319+ StructField (" num_num_3" , DoubleType , true ) ::
320+ StructField (" num_str" , StringType , true ) ::
321+ StructField (" str_bool" , StringType , true ) :: Nil )
323322
324- comparePlans( Schema ( expectedSchema), Schema ( jsonSchemaRDD.logicalPlan.output) )
323+ assert( expectedSchema === jsonSchemaRDD.schema )
325324
326325 jsonSchemaRDD.registerAsTable(" jsonTable" )
327326
@@ -438,15 +437,15 @@ class JsonSuite extends QueryTest {
438437 test(" Type conflict in complex field values" ) {
439438 val jsonSchemaRDD = jsonRDD(complexFieldValueTypeConflict)
440439
441- val expectedSchema =
442- AttributeReference (" array" , ArrayType (IntegerType ), true )( ) ::
443- AttributeReference (" num_struct" , StringType , true )( ) ::
444- AttributeReference (" str_array" , StringType , true )( ) ::
445- AttributeReference (" struct" , StructType (
446- StructField (" field" , StringType , true ) :: Nil ), true )() ::
447- AttributeReference (" struct_array" , StringType , true )() :: Nil
440+ val expectedSchema = StructType (
441+ StructField (" array" , ArrayType (IntegerType ), true ) ::
442+ StructField (" num_struct" , StringType , true ) ::
443+ StructField (" str_array" , StringType , true ) ::
444+ StructField (" struct" , StructType (
445+ StructField (" field" , StringType , true ) :: Nil ), true ) ::
446+ StructField (" struct_array" , StringType , true ) :: Nil )
448447
449- comparePlans( Schema ( expectedSchema), Schema ( jsonSchemaRDD.logicalPlan.output) )
448+ assert( expectedSchema === jsonSchemaRDD.schema )
450449
451450 jsonSchemaRDD.registerAsTable(" jsonTable" )
452451
@@ -462,12 +461,12 @@ class JsonSuite extends QueryTest {
462461 test(" Type conflict in array elements" ) {
463462 val jsonSchemaRDD = jsonRDD(arrayElementTypeConflict)
464463
465- val expectedSchema =
466- AttributeReference (" array1" , ArrayType (StringType , true ), true )( ) ::
467- AttributeReference (" array2" , ArrayType (StructType (
468- StructField (" field" , LongType , true ) :: Nil )), true )() :: Nil
464+ val expectedSchema = StructType (
465+ StructField (" array1" , ArrayType (StringType , true ), true ) ::
466+ StructField (" array2" , ArrayType (StructType (
467+ StructField (" field" , LongType , true ) :: Nil )), true ) :: Nil )
469468
470- comparePlans( Schema ( expectedSchema), Schema ( jsonSchemaRDD.logicalPlan.output) )
469+ assert( expectedSchema === jsonSchemaRDD.schema )
471470
472471 jsonSchemaRDD.registerAsTable(" jsonTable" )
473472
@@ -487,15 +486,15 @@ class JsonSuite extends QueryTest {
487486 test(" Handling missing fields" ) {
488487 val jsonSchemaRDD = jsonRDD(missingFields)
489488
490- val expectedSchema =
491- AttributeReference (" a" , BooleanType , true )( ) ::
492- AttributeReference (" b" , LongType , true )( ) ::
493- AttributeReference (" c" , ArrayType (IntegerType ), true )( ) ::
494- AttributeReference (" d" , StructType (
495- StructField (" field" , BooleanType , true ) :: Nil ), true )() ::
496- AttributeReference (" e" , StringType , true )() :: Nil
489+ val expectedSchema = StructType (
490+ StructField (" a" , BooleanType , true ) ::
491+ StructField (" b" , LongType , true ) ::
492+ StructField (" c" , ArrayType (IntegerType ), true ) ::
493+ StructField (" d" , StructType (
494+ StructField (" field" , BooleanType , true ) :: Nil ), true ) ::
495+ StructField (" e" , StringType , true ) :: Nil )
497496
498- comparePlans( Schema ( expectedSchema), Schema ( jsonSchemaRDD.logicalPlan.output) )
497+ assert( expectedSchema === jsonSchemaRDD.schema )
499498
500499 jsonSchemaRDD.registerAsTable(" jsonTable" )
501500 }
@@ -506,16 +505,16 @@ class JsonSuite extends QueryTest {
506505 primitiveFieldAndType.map(record => record.replaceAll(" \n " , " " )).saveAsTextFile(path)
507506 val jsonSchemaRDD = jsonFile(path)
508507
509- val expectedSchema =
510- AttributeReference (" bigInteger" , DecimalType , true )( ) ::
511- AttributeReference (" boolean" , BooleanType , true )( ) ::
512- AttributeReference (" double" , DoubleType , true )( ) ::
513- AttributeReference (" integer" , IntegerType , true )( ) ::
514- AttributeReference (" long" , LongType , true )( ) ::
515- AttributeReference (" null" , StringType , true )( ) ::
516- AttributeReference (" string" , StringType , true )() :: Nil
508+ val expectedSchema = StructType (
509+ StructField (" bigInteger" , DecimalType , true ) ::
510+ StructField (" boolean" , BooleanType , true ) ::
511+ StructField (" double" , DoubleType , true ) ::
512+ StructField (" integer" , IntegerType , true ) ::
513+ StructField (" long" , LongType , true ) ::
514+ StructField (" null" , StringType , true ) ::
515+ StructField (" string" , StringType , true ) :: Nil )
517516
518- comparePlans( Schema ( expectedSchema), Schema ( jsonSchemaRDD.logicalPlan.output) )
517+ assert( expectedSchema === jsonSchemaRDD.schema )
519518
520519 jsonSchemaRDD.registerAsTable(" jsonTable" )
521520
@@ -530,4 +529,53 @@ class JsonSuite extends QueryTest {
530529 " this is a simple string." ) :: Nil
531530 )
532531 }
532+
533+ test(" Applying schemas" ) {
534+ val file = getTempFilePath(" json" )
535+ val path = file.toString
536+ primitiveFieldAndType.map(record => record.replaceAll(" \n " , " " )).saveAsTextFile(path)
537+
538+ val schema = StructType (
539+ StructField (" bigInteger" , DecimalType , true ) ::
540+ StructField (" boolean" , BooleanType , true ) ::
541+ StructField (" double" , DoubleType , true ) ::
542+ StructField (" integer" , IntegerType , true ) ::
543+ StructField (" long" , LongType , true ) ::
544+ StructField (" null" , StringType , true ) ::
545+ StructField (" string" , StringType , true ) :: Nil )
546+
547+ val jsonSchemaRDD1 = jsonFile(path, schema)
548+
549+ assert(schema === jsonSchemaRDD1.schema)
550+
551+ jsonSchemaRDD1.registerAsTable(" jsonTable1" )
552+
553+ checkAnswer(
554+ sql(" select * from jsonTable1" ),
555+ (BigDecimal (" 92233720368547758070" ),
556+ true ,
557+ 1.7976931348623157E308 ,
558+ 10 ,
559+ 21474836470L ,
560+ null ,
561+ " this is a simple string." ) :: Nil
562+ )
563+
564+ val jsonSchemaRDD2 = jsonRDD(primitiveFieldAndType, schema)
565+
566+ assert(schema === jsonSchemaRDD2.schema)
567+
568+ jsonSchemaRDD2.registerAsTable(" jsonTable2" )
569+
570+ checkAnswer(
571+ sql(" select * from jsonTable2" ),
572+ (BigDecimal (" 92233720368547758070" ),
573+ true ,
574+ 1.7976931348623157E308 ,
575+ 10 ,
576+ 21474836470L ,
577+ null ,
578+ " this is a simple string." ) :: Nil
579+ )
580+ }
533581}
0 commit comments