@@ -177,6 +177,55 @@ class CodeGenerationSuite extends SparkFunSuite with ExpressionEvalHelper {
177177 assert(internalRow === internalRow2)
178178 }
179179
180+ test(" reuse cached code" ) {
181+ val schema1 = new StructType (Array (
182+ StructField (" a1" , StringType , true ),
183+ StructField (" b1" , IntegerType , true ),
184+ StructField (" c1" , new StructType (Array (
185+ StructField (" aa1" , StringType , true ),
186+ StructField (" bb1" , IntegerType , true )
187+ )), true ),
188+ StructField (" d1" , new StructType (Array (
189+ StructField (" a1" , new StructType (Array (
190+ StructField (" b1" , StringType , true ),
191+ StructField (" " , IntegerType , true )
192+ )), true )
193+ )), true )
194+ ))
195+
196+ val schema2 = new StructType (Array (
197+ StructField (" a2" , StringType , true ),
198+ StructField (" b2" , IntegerType , true ),
199+ StructField (" c2" , new StructType (Array (
200+ StructField (" aa2" , StringType , true ),
201+ StructField (" bb2" , IntegerType , true )
202+ )), true ),
203+ StructField (" d2" , new StructType (Array (
204+ StructField (" a2" , new StructType (Array (
205+ StructField (" b2" , StringType , true ),
206+ StructField (" " , IntegerType , true )
207+ )), true )
208+ )), true )
209+ ))
210+
211+ val row = Row (" a" , 1 , Row (" b" , 2 ), Row (Row (" c" , 3 )))
212+ val lit = Literal .create(row, schema1)
213+ val internalRow = lit.value.asInstanceOf [InternalRow ]
214+
215+ // The first UnsafeProjection.create(schema1) should return an instance of a class
216+ // which is generated, compiled and cached.
217+ // The second UnsafeProjection.create(schema2) should return an instance of the class
218+ // which is already cached.
219+ // So the class of unsafeProj1 is same for unsafeProj2
220+ val unsafeProj1 = UnsafeProjection .create(schema1)
221+ val unsafeProj2 = UnsafeProjection .create(schema2)
222+ assert(unsafeProj1.getClass eq unsafeProj2.getClass)
223+
224+ val unsafeRow1 = unsafeProj1(internalRow)
225+ val unsafeRow2 = unsafeProj2(internalRow)
226+ assert(unsafeRow1 == unsafeRow2)
227+ }
228+
180229 test(" */ in the data" ) {
181230 // When */ appears in a comment block (i.e. in /**/), code gen will break.
182231 // So, in Expression and CodegenFallback, we escape */ to \*\/.
0 commit comments