forked from apache/spark
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[SPARK-49506][SQL] Optimize ArrayBinarySearch for foldable array
### What changes were proposed in this pull request? The pr aims to - optimize `ArrayBinarySearch` for `foldable` array. - fix a bug in the original implementation. ### Why are the changes needed? The changes improve performance of the `array_binary_search()` function. - create an instance of `foldable{DataType}ArrayData` only once at the initialization ( avoid frequent calls to `ArrayData.to{DataType}Array()` ), and reuse it inside of `replacement` in the case when the `array` parameter is foldable. Before: ``` Running benchmark: array binary search Running case: no foldable optimize Stopped after 100 iterations, 93668 ms OpenJDK 64-Bit Server VM 17.0.10+7-LTS on Mac OS X 14.6.1 Apple M2 array binary search: Best Time(ms) Avg Time(ms) Stdev(ms) Rate(M/s) Per Row(ns) Relative ------------------------------------------------------------------------------------------------------------------------ no foldable optimize 916 937 24 10.9 91.6 1.0X ``` After: ``` Running benchmark: array binary search Running case: has foldable optimize Stopped after 100 iterations, 17206 ms OpenJDK 64-Bit Server VM 17.0.10+7-LTS on Mac OS X 14.6.1 Apple M2 array binary search: Best Time(ms) Avg Time(ms) Stdev(ms) Rate(M/s) Per Row(ns) Relative ------------------------------------------------------------------------------------------------------------------------ has foldable optimize 164 172 22 61.1 16.4 1.0X ``` ### Does this PR introduce _any_ user-facing change? No. ### How was this patch tested? - Update existed UT. - Pass GA. ### Was this patch authored or co-authored using generative AI tooling? No. Closes apache#48225 from panbingkun/SPARK-49506_FOLLOWUP. Authored-by: panbingkun <[email protected]> Signed-off-by: Wenchen Fan <[email protected]>
- Loading branch information
1 parent
4d30048
commit 6a36c43
Showing
6 changed files
with
534 additions
and
103 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
112 changes: 112 additions & 0 deletions
112
sql/catalyst/src/main/java/org/apache/spark/sql/catalyst/expressions/ToJavaArrayUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,112 @@ | ||
/* | ||
* Licensed to the Apache Software Foundation (ASF) under one or more | ||
* contributor license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright ownership. | ||
* The ASF licenses this file to You under the Apache License, Version 2.0 | ||
* (the "License"); you may not use this file except in compliance with | ||
* the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
package org.apache.spark.sql.catalyst.expressions; | ||
|
||
import scala.reflect.ClassTag$; | ||
|
||
import org.apache.spark.sql.catalyst.util.ArrayData; | ||
|
||
import static org.apache.spark.sql.types.DataTypes.BooleanType; | ||
import static org.apache.spark.sql.types.DataTypes.ByteType; | ||
import static org.apache.spark.sql.types.DataTypes.DoubleType; | ||
import static org.apache.spark.sql.types.DataTypes.FloatType; | ||
import static org.apache.spark.sql.types.DataTypes.IntegerType; | ||
import static org.apache.spark.sql.types.DataTypes.LongType; | ||
import static org.apache.spark.sql.types.DataTypes.ShortType; | ||
|
||
public class ToJavaArrayUtils { | ||
|
||
// boolean | ||
// boolean non-nullable | ||
public static boolean[] toBooleanArray(ArrayData arrayData) { | ||
return arrayData.toBooleanArray(); | ||
} | ||
|
||
// Boolean nullable | ||
public static Boolean[] toBoxedBooleanArray(ArrayData arrayData) { | ||
return (Boolean[]) arrayData.toArray(BooleanType, | ||
ClassTag$.MODULE$.apply(java.lang.Boolean.class)); | ||
} | ||
|
||
// byte | ||
// byte non-nullable | ||
public static byte[] toByteArray(ArrayData arrayData) { | ||
return arrayData.toByteArray(); | ||
} | ||
|
||
// Byte nullable | ||
public static Byte[] toBoxedByteArray(ArrayData arrayData) { | ||
return (Byte[]) arrayData.toArray(ByteType, ClassTag$.MODULE$.apply(java.lang.Byte.class)); | ||
} | ||
|
||
// short | ||
// short non-nullable | ||
public static short[] toShortArray(ArrayData arrayData) { | ||
return arrayData.toShortArray(); | ||
} | ||
|
||
// Short nullable | ||
public static Short[] toBoxedShortArray(ArrayData arrayData) { | ||
return (Short[]) arrayData.toArray(ShortType, ClassTag$.MODULE$.apply(java.lang.Short.class)); | ||
} | ||
|
||
// int | ||
// int non-nullable | ||
public static int[] toIntegerArray(ArrayData arrayData) { | ||
return arrayData.toIntArray(); | ||
} | ||
|
||
// Integer nullable | ||
public static Integer[] toBoxedIntegerArray(ArrayData arrayData) { | ||
return (Integer[]) arrayData.toArray(IntegerType, | ||
ClassTag$.MODULE$.apply(java.lang.Integer.class)); | ||
} | ||
|
||
// long | ||
// long non-nullable | ||
public static long[] toLongArray(ArrayData arrayData) { | ||
return arrayData.toLongArray(); | ||
} | ||
|
||
// Long nullable | ||
public static Long[] toBoxedLongArray(ArrayData arrayData) { | ||
return (Long[]) arrayData.toArray(LongType, ClassTag$.MODULE$.apply(java.lang.Long.class)); | ||
} | ||
|
||
// float | ||
// float non-nullable | ||
public static float[] toFloatArray(ArrayData arrayData) { | ||
return arrayData.toFloatArray(); | ||
} | ||
|
||
// Float nullable | ||
public static Float[] toBoxedFloatArray(ArrayData arrayData) { | ||
return (Float[]) arrayData.toArray(FloatType, ClassTag$.MODULE$.apply(java.lang.Float.class)); | ||
} | ||
|
||
// double | ||
// double non-nullable | ||
public static double[] toDoubleArray(ArrayData arrayData) { | ||
return arrayData.toDoubleArray(); | ||
} | ||
|
||
// Double nullable | ||
public static Double[] toBoxedDoubleArray(ArrayData arrayData) { | ||
return (Double[]) arrayData.toArray(DoubleType, | ||
ClassTag$.MODULE$.apply(java.lang.Double.class)); | ||
} | ||
} |
Oops, something went wrong.