Skip to content

Commit c4b4c44

Browse files
committed
test: add comprehensive map_sort tests
1 parent ada3501 commit c4b4c44

1 file changed

Lines changed: 163 additions & 0 deletions

File tree

spark/src/test/scala/org/apache/comet/CometMapExpressionSuite.scala

Lines changed: 163 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,4 +236,167 @@ class CometMapExpressionSuite extends CometTestBase {
236236
}
237237
}
238238

239+
240+
241+
test("map_sort with integer keys") {
242+
withTempDir { dir =>
243+
withTempView("t1") {
244+
val path = new Path(dir.toURI.toString, "test.parquet")
245+
withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
246+
val df = spark
247+
.range(5)
248+
.select(map(lit(3), lit("c"), lit(1), lit("a"), lit(2), lit("b")).alias("m"))
249+
df.write.parquet(path.toString)
250+
}
251+
spark.read.parquet(path.toString).createOrReplaceTempView("t1")
252+
checkSparkAnswerAndOperator(sql("SELECT map_sort(m) FROM t1"))
253+
}
254+
}
255+
}
256+
257+
test("map_sort with string keys") {
258+
withTempDir { dir =>
259+
withTempView("t1") {
260+
val path = new Path(dir.toURI.toString, "test.parquet")
261+
withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
262+
val df = spark
263+
.range(5)
264+
.select(map(lit("z"), lit(1), lit("a"), lit(2), lit("m"), lit(3)).alias("m"))
265+
df.write.parquet(path.toString)
266+
}
267+
spark.read.parquet(path.toString).createOrReplaceTempView("t1")
268+
checkSparkAnswerAndOperator(sql("SELECT map_sort(m) FROM t1"))
269+
}
270+
}
271+
}
272+
273+
test("map_sort with double keys") {
274+
withTempDir { dir =>
275+
withTempView("t1") {
276+
val path = new Path(dir.toURI.toString, "test.parquet")
277+
withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
278+
val df = spark
279+
.range(5)
280+
.select(map(lit(3.5), lit("c"), lit(1.2), lit("a"), lit(2.8), lit("b")).alias("m"))
281+
df.write.parquet(path.toString)
282+
}
283+
spark.read.parquet(path.toString).createOrReplaceTempView("t1")
284+
checkSparkAnswerAndOperator(sql("SELECT map_sort(m) FROM t1"))
285+
}
286+
}
287+
}
288+
289+
test("map_sort with null and empty maps") {
290+
withTempDir { dir =>
291+
withTempView("t1") {
292+
val path = new Path(dir.toURI.toString, "test.parquet")
293+
withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
294+
val df = spark
295+
.range(5)
296+
.select(
297+
when(col("id") === 0, lit(null))
298+
.when(col("id") === 1, map())
299+
.when(col("id") === 2, map(lit(1), lit("a")))
300+
.otherwise(map(lit(3), lit("c"), lit(2), lit("b")))
301+
.alias("m"))
302+
df.write.parquet(path.toString)
303+
}
304+
spark.read.parquet(path.toString).createOrReplaceTempView("t1")
305+
checkSparkAnswerAndOperator(sql("SELECT map_sort(m) FROM t1"))
306+
}
307+
}
308+
}
309+
310+
test("map_sort with struct keys") {
311+
withTempDir { dir =>
312+
withTempView("t1") {
313+
val path = new Path(dir.toURI.toString, "test.parquet")
314+
withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
315+
val df = spark
316+
.range(3)
317+
.select(
318+
map(
319+
struct(lit(2), lit("b")),
320+
lit("second"),
321+
struct(lit(1), lit("a")),
322+
lit("first"),
323+
struct(lit(3), lit("c")),
324+
lit("third")).alias("m"))
325+
df.write.parquet(path.toString)
326+
}
327+
spark.read.parquet(path.toString).createOrReplaceTempView("t1")
328+
checkSparkAnswerAndOperator(sql("SELECT map_sort(m) FROM t1"))
329+
}
330+
}
331+
}
332+
333+
test("map_sort with array keys") {
334+
withTempDir { dir =>
335+
withTempView("t1") {
336+
val path = new Path(dir.toURI.toString, "test.parquet")
337+
withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
338+
val df = spark
339+
.range(3)
340+
.select(
341+
map(
342+
array(lit(2), lit(3)),
343+
lit("array2"),
344+
array(lit(1), lit(2)),
345+
lit("array1"),
346+
array(lit(3), lit(4)),
347+
lit("array3")).alias("m"))
348+
df.write.parquet(path.toString)
349+
}
350+
spark.read.parquet(path.toString).createOrReplaceTempView("t1")
351+
checkSparkAnswerAndOperator(sql("SELECT map_sort(m) FROM t1"))
352+
}
353+
}
354+
}
355+
356+
test("map_sort with complex values") {
357+
withTempDir { dir =>
358+
withTempView("t1") {
359+
val path = new Path(dir.toURI.toString, "test.parquet")
360+
withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
361+
val df = spark
362+
.range(3)
363+
.select(
364+
map(
365+
lit(3),
366+
struct(lit("c"), array(lit(30), lit(31))),
367+
lit(1),
368+
struct(lit("a"), array(lit(10), lit(11))),
369+
lit(2),
370+
struct(lit("b"), array(lit(20), lit(21)))).alias("m"))
371+
df.write.parquet(path.toString)
372+
}
373+
spark.read.parquet(path.toString).createOrReplaceTempView("t1")
374+
checkSparkAnswerAndOperator(sql("SELECT map_sort(m) FROM t1"))
375+
}
376+
}
377+
}
378+
379+
test("map_sort fallback for non-orderable keys") {
380+
withTempDir { dir =>
381+
withTempView("t1") {
382+
val path = new Path(dir.toURI.toString, "test.parquet")
383+
withSQLConf(CometConf.COMET_ENABLED.key -> "false") {
384+
val df = spark
385+
.range(3)
386+
.select(
387+
map(
388+
map(lit(1), lit("inner1")),
389+
lit("outer1"),
390+
map(lit(2), lit("inner2")),
391+
lit("outer2")).alias("m"))
392+
df.write.parquet(path.toString)
393+
}
394+
spark.read.parquet(path.toString).createOrReplaceTempView("t1")
395+
checkSparkAnswerAndFallbackReason(
396+
sql("SELECT map_sort(m) FROM t1"),
397+
"map_sort requires orderable key type")
398+
}
399+
}
400+
}
401+
239402
}

0 commit comments

Comments
 (0)