forked from prestodb/presto
-
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.
Implement array_sum function in Java from SQL
- Loading branch information
1 parent
fd8d572
commit 1e80972
Showing
8 changed files
with
217 additions
and
58 deletions.
There are no files selected for viewing
32 changes: 32 additions & 0 deletions
32
presto-benchmark/src/main/java/com/facebook/presto/benchmark/SqlArraySumBenchmark.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,32 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.benchmark; | ||
|
||
import com.facebook.presto.testing.LocalQueryRunner; | ||
|
||
import static com.facebook.presto.benchmark.BenchmarkQueryRunner.createLocalQueryRunner; | ||
|
||
public class SqlArraySumBenchmark | ||
extends AbstractSqlBenchmark | ||
{ | ||
public SqlArraySumBenchmark(LocalQueryRunner localQueryRunner, String query, String name) | ||
{ | ||
super(localQueryRunner, name, 10, 10, query); | ||
} | ||
|
||
public static void main(String[] args) | ||
{ | ||
new SqlArraySumBenchmark(createLocalQueryRunner(), "SELECT ARRAY_SUM(x) FROM part cross join (SELECT transform(sequence(1,10000),y->random()) AS x)", "sql_array_sum").runBenchmark(new SimpleLineBenchmarkResultWriter(System.out)); | ||
} | ||
} |
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
59 changes: 59 additions & 0 deletions
59
presto-main/src/main/java/com/facebook/presto/operator/scalar/ArraySumBigIntFunction.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,59 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.operator.scalar; | ||
|
||
import com.facebook.presto.common.block.Block; | ||
import com.facebook.presto.common.type.Type; | ||
import com.facebook.presto.spi.function.Description; | ||
import com.facebook.presto.spi.function.OperatorDependency; | ||
import com.facebook.presto.spi.function.ScalarFunction; | ||
import com.facebook.presto.spi.function.SqlType; | ||
import com.facebook.presto.spi.function.TypeParameter; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
|
||
import static com.facebook.presto.common.function.OperatorType.ADD; | ||
import static com.facebook.presto.util.Failures.internalError; | ||
|
||
@Description("Returns the sum of all array elements, or 0 if the array is empty. Ignores null elements.") | ||
@ScalarFunction(value = "array_sum") | ||
public final class ArraySumBigIntFunction | ||
{ | ||
private ArraySumBigIntFunction() {} | ||
|
||
@SqlType("bigint") | ||
public static long arraySumBigInt( | ||
@OperatorDependency(operator = ADD, argumentTypes = {"bigint", "bigint"}) MethodHandle addFunction, | ||
@TypeParameter("bigint") Type elementType, | ||
@SqlType("array(bigint)") Block arrayBlock) | ||
{ | ||
int positionCount = arrayBlock.getPositionCount(); | ||
if (positionCount == 0) { | ||
return 0; | ||
} | ||
|
||
long sum = 0; | ||
for (int i = 0; i < positionCount; i++) { | ||
if (!arrayBlock.isNull(i)) { | ||
try { | ||
sum = (long) addFunction.invoke(sum, arrayBlock.getLong(i)); | ||
} | ||
catch (Throwable throwable) { | ||
throw internalError(throwable); | ||
} | ||
} | ||
} | ||
return sum; | ||
} | ||
} |
61 changes: 61 additions & 0 deletions
61
presto-main/src/main/java/com/facebook/presto/operator/scalar/ArraySumDoubleFunction.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,61 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.operator.scalar; | ||
|
||
import com.facebook.presto.common.block.Block; | ||
import com.facebook.presto.common.type.Type; | ||
import com.facebook.presto.spi.function.Description; | ||
import com.facebook.presto.spi.function.OperatorDependency; | ||
import com.facebook.presto.spi.function.ScalarFunction; | ||
import com.facebook.presto.spi.function.SqlType; | ||
import com.facebook.presto.spi.function.TypeParameter; | ||
|
||
import java.lang.invoke.MethodHandle; | ||
|
||
import static com.facebook.presto.common.function.OperatorType.ADD; | ||
import static com.facebook.presto.common.type.TypeUtils.readNativeValue; | ||
import static com.facebook.presto.util.Failures.internalError; | ||
|
||
@Description("Returns the sum of all array elements, or 0 if the array is empty. Ignores null elements.") | ||
@ScalarFunction(value = "array_sum") | ||
public final class ArraySumDoubleFunction | ||
{ | ||
private ArraySumDoubleFunction() {} | ||
|
||
@SqlType("double") | ||
public static double arraySumDouble( | ||
@OperatorDependency(operator = ADD, argumentTypes = {"double", "double"}) MethodHandle addFunction, | ||
@TypeParameter("double") Type elementType, | ||
@SqlType("array(double)") Block arrayBlock) | ||
{ | ||
int positionCount = arrayBlock.getPositionCount(); | ||
if (positionCount == 0) { | ||
return 0.0; | ||
} | ||
|
||
double sum = 0.0; | ||
for (int i = 0; i < positionCount; i++) { | ||
if (!arrayBlock.isNull(i)) { | ||
try { | ||
Object newValue = readNativeValue(elementType, arrayBlock, i); | ||
sum = (double) addFunction.invoke(sum, (double) newValue); | ||
} | ||
catch (Throwable throwable) { | ||
throw internalError(throwable); | ||
} | ||
} | ||
} | ||
return sum; | ||
} | ||
} |
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
61 changes: 61 additions & 0 deletions
61
presto-main/src/test/java/com/facebook/presto/operator/scalar/TestArraySumFunction.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,61 @@ | ||
/* | ||
* Licensed 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 com.facebook.presto.operator.scalar; | ||
|
||
import org.testng.annotations.Test; | ||
|
||
import static com.facebook.presto.common.type.BigintType.BIGINT; | ||
import static com.facebook.presto.common.type.DoubleType.DOUBLE; | ||
|
||
public class TestArraySumFunction | ||
extends AbstractTestFunctions | ||
{ | ||
@Test | ||
public void testBigIntType() | ||
{ | ||
assertFunction("array_sum(array[BIGINT '1', BIGINT '2'])", BIGINT, 3L); | ||
assertFunction("array_sum(array[INTEGER '1', INTEGER '2'])", BIGINT, 3L); | ||
assertFunction("array_sum(array[SMALLINT '1', SMALLINT '2'])", BIGINT, 3L); | ||
assertFunction("array_sum(array[TINYINT '1', TINYINT '2'])", BIGINT, 3L); | ||
|
||
assertFunction("array_sum(array[BIGINT '1', INTEGER '2'])", BIGINT, 3L); | ||
assertFunction("array_sum(array[INTEGER '1', SMALLINT '2'])", BIGINT, 3L); | ||
assertFunction("array_sum(array[SMALLINT '1', TINYINT '2'])", BIGINT, 3L); | ||
} | ||
|
||
@Test | ||
public void testDoubleType() | ||
{ | ||
assertFunctionWithError("array_sum(array[DOUBLE '-2.0', DOUBLE '5.3'])", DOUBLE, 3.3); | ||
assertFunctionWithError("array_sum(array[DOUBLE '-2.0', REAL '5.3'])", DOUBLE, 3.3); | ||
assertFunctionWithError("array_sum(array[DOUBLE '-2.0', DECIMAL '5.3'])", DOUBLE, 3.3); | ||
assertFunctionWithError("array_sum(array[REAL '-2.0', DECIMAL '5.3'])", DOUBLE, 3.3); | ||
|
||
assertFunctionWithError("array_sum(array[BIGINT '-2', DOUBLE '5.3'])", DOUBLE, 3.3); | ||
assertFunctionWithError("array_sum(array[INTEGER '-2', REAL '5.3'])", DOUBLE, 3.3); | ||
assertFunctionWithError("array_sum(array[SMALLINT '-2', DECIMAL '5.3'])", DOUBLE, 3.3); | ||
assertFunctionWithError("array_sum(array[TINYINT '-2', DOUBLE '5.3'])", DOUBLE, 3.3); | ||
} | ||
|
||
@Test | ||
public void testEdgeCases() | ||
{ | ||
assertFunction("array_sum(null)", BIGINT, null); | ||
assertFunction("array_sum(array[])", BIGINT, 0L); | ||
assertFunction("array_sum(array[NULL])", BIGINT, 0L); | ||
assertFunction("array_sum(array[NULL, NULL, NULL])", BIGINT, 0L); | ||
assertFunction("array_sum(array[3, NULL, 5])", BIGINT, 8L); | ||
assertFunctionWithError("array_sum(array[NULL, double '1.2', double '2.3', NULL, -3])", DOUBLE, 0.5); | ||
} | ||
} |
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
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