@@ -19,9 +19,11 @@ package org.apache.spark.sql.catalyst.expressions
1919
2020import  java .nio .charset .StandardCharsets 
2121
22+ import  scala .reflect .runtime .universe .{typeTag , TypeTag }
23+ 
2224import  org .apache .spark .SparkFunSuite 
2325import  org .apache .spark .sql .Row 
24- import  org .apache .spark .sql .catalyst .CatalystTypeConverters 
26+ import  org .apache .spark .sql .catalyst .{ CatalystTypeConverters ,  ScalaReflection } 
2527import  org .apache .spark .sql .catalyst .encoders .ExamplePointUDT 
2628import  org .apache .spark .sql .catalyst .util .DateTimeUtils 
2729import  org .apache .spark .sql .types ._ 
@@ -75,6 +77,9 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
7577  test(" boolean literals" 
7678    checkEvaluation(Literal (true ), true )
7779    checkEvaluation(Literal (false ), false )
80+ 
81+     checkEvaluation(Literal .create(true ), true )
82+     checkEvaluation(Literal .create(false ), false )
7883  }
7984
8085  test(" int literals" 
@@ -83,36 +88,60 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
8388      checkEvaluation(Literal (d.toLong), d.toLong)
8489      checkEvaluation(Literal (d.toShort), d.toShort)
8590      checkEvaluation(Literal (d.toByte), d.toByte)
91+ 
92+       checkEvaluation(Literal .create(d), d)
93+       checkEvaluation(Literal .create(d.toLong), d.toLong)
94+       checkEvaluation(Literal .create(d.toShort), d.toShort)
95+       checkEvaluation(Literal .create(d.toByte), d.toByte)
8696    }
8797    checkEvaluation(Literal (Long .MinValue ), Long .MinValue )
8898    checkEvaluation(Literal (Long .MaxValue ), Long .MaxValue )
99+ 
100+     checkEvaluation(Literal .create(Long .MinValue ), Long .MinValue )
101+     checkEvaluation(Literal .create(Long .MaxValue ), Long .MaxValue )
89102  }
90103
91104  test(" double literals" 
92105    List (0.0 , - 0.0 , Double .NegativeInfinity , Double .PositiveInfinity ).foreach { d => 
93106      checkEvaluation(Literal (d), d)
94107      checkEvaluation(Literal (d.toFloat), d.toFloat)
108+ 
109+       checkEvaluation(Literal .create(d), d)
110+       checkEvaluation(Literal .create(d.toFloat), d.toFloat)
95111    }
96112    checkEvaluation(Literal (Double .MinValue ), Double .MinValue )
97113    checkEvaluation(Literal (Double .MaxValue ), Double .MaxValue )
98114    checkEvaluation(Literal (Float .MinValue ), Float .MinValue )
99115    checkEvaluation(Literal (Float .MaxValue ), Float .MaxValue )
100116
117+     checkEvaluation(Literal .create(Double .MinValue ), Double .MinValue )
118+     checkEvaluation(Literal .create(Double .MaxValue ), Double .MaxValue )
119+     checkEvaluation(Literal .create(Float .MinValue ), Float .MinValue )
120+     checkEvaluation(Literal .create(Float .MaxValue ), Float .MaxValue )
121+ 
101122  }
102123
103124  test(" string literals" 
104125    checkEvaluation(Literal (" " " " 
105126    checkEvaluation(Literal (" test" " test" 
106127    checkEvaluation(Literal (" \u0000 " " \u0000 " 
128+ 
129+     checkEvaluation(Literal .create(" " " " 
130+     checkEvaluation(Literal .create(" test" " test" 
131+     checkEvaluation(Literal .create(" \u0000 " " \u0000 " 
107132  }
108133
109134  test(" sum two literals" 
110135    checkEvaluation(Add (Literal (1 ), Literal (1 )), 2 )
136+     checkEvaluation(Add (Literal .create(1 ), Literal .create(1 )), 2 )
111137  }
112138
113139  test(" binary literals" 
114140    checkEvaluation(Literal .create(new  Array [Byte ](0 ), BinaryType ), new  Array [Byte ](0 ))
115141    checkEvaluation(Literal .create(new  Array [Byte ](2 ), BinaryType ), new  Array [Byte ](2 ))
142+ 
143+     checkEvaluation(Literal .create(new  Array [Byte ](0 )), new  Array [Byte ](0 ))
144+     checkEvaluation(Literal .create(new  Array [Byte ](2 )), new  Array [Byte ](2 ))
116145  }
117146
118147  test(" decimal" 
@@ -124,24 +153,63 @@ class LiteralExpressionSuite extends SparkFunSuite with ExpressionEvalHelper {
124153        Decimal ((d *  1000L ).toLong, 10 , 3 ))
125154      checkEvaluation(Literal (BigDecimal (d.toString)), Decimal (d))
126155      checkEvaluation(Literal (new  java.math.BigDecimal (d.toString)), Decimal (d))
156+ 
157+       checkEvaluation(Literal .create(Decimal (d)), Decimal (d))
158+       checkEvaluation(Literal .create(Decimal (d.toInt)), Decimal (d.toInt))
159+       checkEvaluation(Literal .create(Decimal (d.toLong)), Decimal (d.toLong))
160+       checkEvaluation(Literal .create(Decimal ((d *  1000L ).toLong, 10 , 3 )),
161+         Decimal ((d *  1000L ).toLong, 10 , 3 ))
162+       checkEvaluation(Literal .create(BigDecimal (d.toString)), Decimal (d))
163+       checkEvaluation(Literal .create(new  java.math.BigDecimal (d.toString)), Decimal (d))
164+ 
127165    }
128166  }
129167
168+   private  def  toCatalyst [T :  TypeTag ](value : T ):  Any  =  {
169+     val  ScalaReflection .Schema (dataType, _) =  ScalaReflection .schemaFor[T ]
170+     CatalystTypeConverters .createToCatalystConverter(dataType)(value)
171+   }
172+ 
130173  test(" array" 
131-     def  checkArrayLiteral (a : Array [_], elementType : DataType ):  Unit  =  {
132-       val  toCatalyst  =  (a : Array [_], elementType : DataType ) =>  {
133-         CatalystTypeConverters .createToCatalystConverter(ArrayType (elementType))(a)
134-       }
135-       checkEvaluation(Literal (a), toCatalyst(a, elementType))
174+     def  checkArrayLiteral [T :  TypeTag ](a : Array [T ]):  Unit  =  {
175+       checkEvaluation(Literal (a), toCatalyst(a))
176+       checkEvaluation(Literal .create(a), toCatalyst(a))
177+     }
178+     checkArrayLiteral(Array (1 , 2 , 3 ))
179+     checkArrayLiteral(Array (" a" " b" " c" 
180+     checkArrayLiteral(Array (1.0 , 4.0 ))
181+     checkArrayLiteral(Array (CalendarInterval .MICROS_PER_DAY , CalendarInterval .MICROS_PER_HOUR ))
182+   }
183+ 
184+   test(" seq" 
185+     def  checkSeqLiteral [T :  TypeTag ](a : Seq [T ], elementType : DataType ):  Unit  =  {
186+       checkEvaluation(Literal .create(a), toCatalyst(a))
136187    }
137-     checkArrayLiteral( Array (1 , 2 , 3 ), IntegerType )
138-     checkArrayLiteral( Array (" a" " b" " c" StringType )
139-     checkArrayLiteral( Array (1.0 , 4.0 ), DoubleType )
140-     checkArrayLiteral( Array (CalendarInterval .MICROS_PER_DAY , CalendarInterval .MICROS_PER_HOUR ),
188+     checkSeqLiteral( Seq (1 , 2 , 3 ), IntegerType )
189+     checkSeqLiteral( Seq (" a" " b" " c" StringType )
190+     checkSeqLiteral( Seq (1.0 , 4.0 ), DoubleType )
191+     checkSeqLiteral( Seq (CalendarInterval .MICROS_PER_DAY , CalendarInterval .MICROS_PER_HOUR ),
141192      CalendarIntervalType )
142193  }
143194
144-   test(" unsupported types (map and struct) in literals" 
195+   test(" map" 
196+     def  checkMapLiteral [T :  TypeTag ](m : T ):  Unit  =  {
197+       checkEvaluation(Literal .create(m), toCatalyst(m))
198+     }
199+     checkMapLiteral(Map (" a" ->  1 , " b" ->  2 , " c" ->  3 ))
200+     checkMapLiteral(Map (" 1" ->  1.0 , " 2" ->  2.0 , " 3" ->  3.0 ))
201+   }
202+ 
203+   test(" struct" 
204+     def  checkStructLiteral [T :  TypeTag ](s : T ):  Unit  =  {
205+       checkEvaluation(Literal .create(s), toCatalyst(s))
206+     }
207+     checkStructLiteral((1 , 3.0 , " abcde" 
208+     checkStructLiteral((" de" 1 , 2.0f ))
209+     checkStructLiteral((1 , (" fgh" 3.0 )))
210+   }
211+ 
212+   test(" unsupported types (map and struct) in Literal.apply" 
145213    def  checkUnsupportedTypeInLiteral (v : Any ):  Unit  =  {
146214      val  errMsgMap  =  intercept[RuntimeException ] {
147215        Literal (v)
0 commit comments