-
Notifications
You must be signed in to change notification settings - Fork 29k
[SPARK-23921][SQL] Add array_sort function #21021
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from 1 commit
72f31b3
a9b6e3b
d57c14a
9977f64
172b2c5
f2798f9
175d981
d1b0483
04a3ae5
9f63a76
e3fcaaa
2ad6bb8
2c4404c
21521d8
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
- Loading branch information
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -186,7 +186,21 @@ trait ArraySortUtil extends ExpectsInputTypes { | |
| val genericArrayData = classOf[GenericArrayData].getName | ||
| val array = ctx.freshName("array") | ||
| val c = ctx.freshName("c") | ||
| val sort = if (elementType == NullType) "" else { | ||
| val dataTypes = elementType match { | ||
| case DecimalType.Fixed(p, s) => | ||
| s"org.apache.spark.sql.types.DataTypes.createDecimalType($p, $s)" | ||
| case ArrayType(et, cn) => | ||
| val dt = s"org.apache.spark.sql.types.$et$$.MODULE$$" | ||
| s"org.apache.spark.sql.types.DataTypes.createArrayType($dt, $cn)" | ||
| case StructType(f) => | ||
| "org.apache.spark.sql.types.StructType$.MODULE$." + | ||
| s"apply(new java.util.ArrayList(${f.length}))" | ||
| case _ => | ||
| s"org.apache.spark.sql.types.$elementType$$.MODULE$$" | ||
| } | ||
| if (elementType == NullType) { | ||
| s"${ev.value} = (($arrayData) $base).copy();" | ||
|
||
| } else { | ||
| val sortOrder = ctx.freshName("sortOrder") | ||
| val o1 = ctx.freshName("o1") | ||
| val o2 = ctx.freshName("o2") | ||
|
|
@@ -204,6 +218,7 @@ trait ArraySortUtil extends ExpectsInputTypes { | |
| s"int $c = ${ctx.genComp(elementType, s"(($jt) $o1)", s"(($jt) $o2)")};" | ||
| } | ||
| s""" | ||
| |Object[] $array = (Object[]) (($arrayData) $base).toObjectArray($dataTypes); | ||
|
||
| |final int $sortOrder = $order ? 1 : -1; | ||
| |java.util.Arrays.sort($array, new java.util.Comparator() { | ||
| | @Override public int compare(Object $o1, Object $o2) { | ||
|
|
@@ -218,27 +233,9 @@ trait ArraySortUtil extends ExpectsInputTypes { | |
| | return $sortOrder * $c; | ||
| | } | ||
| |}); | ||
| |${ev.value} = new $genericArrayData($array); | ||
| """.stripMargin | ||
| } | ||
| val dataTypes = elementType match { | ||
| case DecimalType.Fixed(p, s) => | ||
| s"org.apache.spark.sql.types.DataTypes.createDecimalType($p, $s)" | ||
| case ArrayType(et, cn) => | ||
| s"org.apache.spark.sql.types.DataTypes.createArrayType($et, $cn)" | ||
| case MapType(kt, vt, cn) => | ||
| s"org.apache.spark.sql.types.DataTypes.createMapType($kt, $vt, $cn)" | ||
| case StructType(f) => | ||
| "org.apache.spark.sql.types.StructType$.MODULE$." + | ||
| s"apply(new java.util.ArrayList(${f.length}))" | ||
| case _ => | ||
| s"org.apache.spark.sql.types.DataTypes.$elementType" | ||
| } | ||
| s""" | ||
| |Object[] $array = (Object[]) (($arrayData) $base).toArray( | ||
| | $dataTypes, scala.reflect.ClassTag$$.MODULE$$.AnyRef()); | ||
| |$sort | ||
| |${ev.value} = new $genericArrayData($array); | ||
| """.stripMargin | ||
| } | ||
|
|
||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm still wondering whether this will work or not. What if
elementTypeisArrayType(ArrayType(IntegerType))?Can't we use a reference object of
elementType?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could you also add some tests using
ArrayTypeandStructTypeforelementType?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Definitely, I added some complex test cases with nests.