@@ -157,4 +157,165 @@ class CometMapExpressionSuite extends CometTestBase {
157157 }
158158 }
159159
160+ test(" map_sort with integer keys" ) {
161+ withTempDir { dir =>
162+ withTempView(" t1" ) {
163+ val path = new Path (dir.toURI.toString, " test.parquet" )
164+ withSQLConf(CometConf .COMET_ENABLED .key -> " false" ) {
165+ val df = spark
166+ .range(5 )
167+ .select(map(lit(3 ), lit(" c" ), lit(1 ), lit(" a" ), lit(2 ), lit(" b" )).alias(" m" ))
168+ df.write.parquet(path.toString)
169+ }
170+ spark.read.parquet(path.toString).createOrReplaceTempView(" t1" )
171+ checkSparkAnswerAndOperator(sql(" SELECT map_sort(m) FROM t1" ))
172+ }
173+ }
174+ }
175+
176+ test(" map_sort with string keys" ) {
177+ withTempDir { dir =>
178+ withTempView(" t1" ) {
179+ val path = new Path (dir.toURI.toString, " test.parquet" )
180+ withSQLConf(CometConf .COMET_ENABLED .key -> " false" ) {
181+ val df = spark
182+ .range(5 )
183+ .select(map(lit(" z" ), lit(1 ), lit(" a" ), lit(2 ), lit(" m" ), lit(3 )).alias(" m" ))
184+ df.write.parquet(path.toString)
185+ }
186+ spark.read.parquet(path.toString).createOrReplaceTempView(" t1" )
187+ checkSparkAnswerAndOperator(sql(" SELECT map_sort(m) FROM t1" ))
188+ }
189+ }
190+ }
191+
192+ test(" map_sort with double keys" ) {
193+ withTempDir { dir =>
194+ withTempView(" t1" ) {
195+ val path = new Path (dir.toURI.toString, " test.parquet" )
196+ withSQLConf(CometConf .COMET_ENABLED .key -> " false" ) {
197+ val df = spark
198+ .range(5 )
199+ .select(map(lit(3.5 ), lit(" c" ), lit(1.2 ), lit(" a" ), lit(2.8 ), lit(" b" )).alias(" m" ))
200+ df.write.parquet(path.toString)
201+ }
202+ spark.read.parquet(path.toString).createOrReplaceTempView(" t1" )
203+ checkSparkAnswerAndOperator(sql(" SELECT map_sort(m) FROM t1" ))
204+ }
205+ }
206+ }
207+
208+ test(" map_sort with null and empty maps" ) {
209+ withTempDir { dir =>
210+ withTempView(" t1" ) {
211+ val path = new Path (dir.toURI.toString, " test.parquet" )
212+ withSQLConf(CometConf .COMET_ENABLED .key -> " false" ) {
213+ val df = spark
214+ .range(5 )
215+ .select(
216+ when(col(" id" ) === 0 , lit(null ))
217+ .when(col(" id" ) === 1 , map())
218+ .when(col(" id" ) === 2 , map(lit(1 ), lit(" a" )))
219+ .otherwise(map(lit(3 ), lit(" c" ), lit(2 ), lit(" b" )))
220+ .alias(" m" ))
221+ df.write.parquet(path.toString)
222+ }
223+ spark.read.parquet(path.toString).createOrReplaceTempView(" t1" )
224+ checkSparkAnswerAndOperator(sql(" SELECT map_sort(m) FROM t1" ))
225+ }
226+ }
227+ }
228+
229+ test(" map_sort with struct keys" ) {
230+ withTempDir { dir =>
231+ withTempView(" t1" ) {
232+ val path = new Path (dir.toURI.toString, " test.parquet" )
233+ withSQLConf(CometConf .COMET_ENABLED .key -> " false" ) {
234+ val df = spark
235+ .range(3 )
236+ .select(
237+ map(
238+ struct(lit(2 ), lit(" b" )),
239+ lit(" second" ),
240+ struct(lit(1 ), lit(" a" )),
241+ lit(" first" ),
242+ struct(lit(3 ), lit(" c" )),
243+ lit(" third" )).alias(" m" ))
244+ df.write.parquet(path.toString)
245+ }
246+ spark.read.parquet(path.toString).createOrReplaceTempView(" t1" )
247+ checkSparkAnswerAndOperator(sql(" SELECT map_sort(m) FROM t1" ))
248+ }
249+ }
250+ }
251+
252+ test(" map_sort with array keys" ) {
253+ withTempDir { dir =>
254+ withTempView(" t1" ) {
255+ val path = new Path (dir.toURI.toString, " test.parquet" )
256+ withSQLConf(CometConf .COMET_ENABLED .key -> " false" ) {
257+ val df = spark
258+ .range(3 )
259+ .select(
260+ map(
261+ array(lit(2 ), lit(3 )),
262+ lit(" array2" ),
263+ array(lit(1 ), lit(2 )),
264+ lit(" array1" ),
265+ array(lit(3 ), lit(4 )),
266+ lit(" array3" )).alias(" m" ))
267+ df.write.parquet(path.toString)
268+ }
269+ spark.read.parquet(path.toString).createOrReplaceTempView(" t1" )
270+ checkSparkAnswerAndOperator(sql(" SELECT map_sort(m) FROM t1" ))
271+ }
272+ }
273+ }
274+
275+ test(" map_sort with complex values" ) {
276+ withTempDir { dir =>
277+ withTempView(" t1" ) {
278+ val path = new Path (dir.toURI.toString, " test.parquet" )
279+ withSQLConf(CometConf .COMET_ENABLED .key -> " false" ) {
280+ val df = spark
281+ .range(3 )
282+ .select(
283+ map(
284+ lit(3 ),
285+ struct(lit(" c" ), array(lit(30 ), lit(31 ))),
286+ lit(1 ),
287+ struct(lit(" a" ), array(lit(10 ), lit(11 ))),
288+ lit(2 ),
289+ struct(lit(" b" ), array(lit(20 ), lit(21 )))).alias(" m" ))
290+ df.write.parquet(path.toString)
291+ }
292+ spark.read.parquet(path.toString).createOrReplaceTempView(" t1" )
293+ checkSparkAnswerAndOperator(sql(" SELECT map_sort(m) FROM t1" ))
294+ }
295+ }
296+ }
297+
298+ test(" map_sort fallback for non-orderable keys" ) {
299+ withTempDir { dir =>
300+ withTempView(" t1" ) {
301+ val path = new Path (dir.toURI.toString, " test.parquet" )
302+ withSQLConf(CometConf .COMET_ENABLED .key -> " false" ) {
303+ val df = spark
304+ .range(3 )
305+ .select(
306+ map(
307+ map(lit(1 ), lit(" inner1" )),
308+ lit(" outer1" ),
309+ map(lit(2 ), lit(" inner2" )),
310+ lit(" outer2" )).alias(" m" ))
311+ df.write.parquet(path.toString)
312+ }
313+ spark.read.parquet(path.toString).createOrReplaceTempView(" t1" )
314+ checkSparkAnswerAndFallbackReason(
315+ sql(" SELECT map_sort(m) FROM t1" ),
316+ " map_sort requires orderable key type" )
317+ }
318+ }
319+ }
320+
160321}
0 commit comments