@@ -24,23 +24,128 @@ import org.scalatest.FunSuite
2424import org .apache .spark .sql .catalyst .expressions ._
2525import org .apache .spark .sql .catalyst .types ._
2626
27+ case class PrimitiveData (
28+ intField : Int ,
29+ longField : Long ,
30+ doubleField : Double ,
31+ floatField : Float ,
32+ shortField : Short ,
33+ byteField : Byte ,
34+ booleanField : Boolean )
35+
36+ case class NullableData (
37+ intField : java.lang.Integer ,
38+ longField : java.lang.Long ,
39+ doubleField : java.lang.Double ,
40+ floatField : java.lang.Float ,
41+ shortField : java.lang.Short ,
42+ byteField : java.lang.Byte ,
43+ booleanField : java.lang.Boolean ,
44+ stringField : String ,
45+ decimalField : BigDecimal ,
46+ timestampField : Timestamp ,
47+ binaryField : Array [Byte ])
48+
49+ case class OptionalData (
50+ intField : Option [Int ],
51+ longField : Option [Long ],
52+ doubleField : Option [Double ],
53+ floatField : Option [Float ],
54+ shortField : Option [Short ],
55+ byteField : Option [Byte ],
56+ booleanField : Option [Boolean ])
57+
58+ case class ComplexData (
59+ arrayField : Seq [Int ],
60+ mapField : Map [Int , String ],
61+ structField : PrimitiveData )
62+
2763case class GenericData [A ](
2864 genericField : A )
2965
3066class ScalaReflectionSuite extends FunSuite {
67+ import ScalaReflection ._
68+
69+ test(" primitive data" ) {
70+ val schema = schemaFor[PrimitiveData ]
71+ assert(schema === Schema (
72+ StructType (Seq (
73+ StructField (" intField" , IntegerType , nullable = false ),
74+ StructField (" longField" , LongType , nullable = false ),
75+ StructField (" doubleField" , DoubleType , nullable = false ),
76+ StructField (" floatField" , FloatType , nullable = false ),
77+ StructField (" shortField" , ShortType , nullable = false ),
78+ StructField (" byteField" , ByteType , nullable = false ),
79+ StructField (" booleanField" , BooleanType , nullable = false ))),
80+ nullable = true ))
81+ }
82+
83+ test(" nullable data" ) {
84+ val schema = schemaFor[NullableData ]
85+ assert(schema === Schema (
86+ StructType (Seq (
87+ StructField (" intField" , IntegerType , nullable = true ),
88+ StructField (" longField" , LongType , nullable = true ),
89+ StructField (" doubleField" , DoubleType , nullable = true ),
90+ StructField (" floatField" , FloatType , nullable = true ),
91+ StructField (" shortField" , ShortType , nullable = true ),
92+ StructField (" byteField" , ByteType , nullable = true ),
93+ StructField (" booleanField" , BooleanType , nullable = true ),
94+ StructField (" stringField" , StringType , nullable = true ),
95+ StructField (" decimalField" , DecimalType , nullable = true ),
96+ StructField (" timestampField" , TimestampType , nullable = true ),
97+ StructField (" binaryField" , BinaryType , nullable = true ))),
98+ nullable = true ))
99+ }
100+
101+ test(" optinal data" ) {
102+ val schema = schemaFor[OptionalData ]
103+ assert(schema === Schema (
104+ StructType (Seq (
105+ StructField (" intField" , IntegerType , nullable = true ),
106+ StructField (" longField" , LongType , nullable = true ),
107+ StructField (" doubleField" , DoubleType , nullable = true ),
108+ StructField (" floatField" , FloatType , nullable = true ),
109+ StructField (" shortField" , ShortType , nullable = true ),
110+ StructField (" byteField" , ByteType , nullable = true ),
111+ StructField (" booleanField" , BooleanType , nullable = true ))),
112+ nullable = true ))
113+ }
114+
115+ test(" complex data" ) {
116+ val schema = schemaFor[ComplexData ]
117+ assert(schema === Schema (
118+ StructType (Seq (
119+ StructField (" arrayField" , ArrayType (IntegerType ), nullable = true ),
120+ StructField (" mapField" , MapType (IntegerType , StringType ), nullable = true ),
121+ StructField (
122+ " structField" ,
123+ StructType (Seq (
124+ StructField (" intField" , IntegerType , nullable = false ),
125+ StructField (" longField" , LongType , nullable = false ),
126+ StructField (" doubleField" , DoubleType , nullable = false ),
127+ StructField (" floatField" , FloatType , nullable = false ),
128+ StructField (" shortField" , ShortType , nullable = false ),
129+ StructField (" byteField" , ByteType , nullable = false ),
130+ StructField (" booleanField" , BooleanType , nullable = false ))),
131+ nullable = true ))),
132+ nullable = true ))
133+ }
31134
32135 test(" generic data" ) {
33136 val schema = ScalaReflection .schemaFor[GenericData [Int ]]
34- assert(schema ===
137+ assert(schema === Schema (
35138 StructType (Seq (
36- StructField (" genericField" , IntegerType , nullable = true ))))
139+ StructField (" genericField" , IntegerType , nullable = false ))),
140+ nullable = true ))
37141 }
38142
39143 test(" tuple data" ) {
40144 val schema = ScalaReflection .schemaFor[(Int , String )]
41- assert(schema ===
145+ assert(schema === Schema (
42146 StructType (Seq (
43- StructField (" _1" , IntegerType , nullable = true ),
44- StructField (" _2" , StringType , nullable = true ))))
147+ StructField (" _1" , IntegerType , nullable = false ),
148+ StructField (" _2" , StringType , nullable = true ))),
149+ nullable = true ))
45150 }
46151}
0 commit comments