generated from amazon-archives/__template_Custom
-
Couldn't load subscription status.
- Fork 176
Add math udf #3390
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
Merged
LantaoJin
merged 28 commits into
opensearch-project:feature/calcite-engine
from
xinyual:addMathUDF
Mar 20, 2025
Merged
Add math udf #3390
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
08837b2
add math udfs
xinyual 404f43d
add log argument
xinyual 17976ef
Add math function unit tests
yuancu f04ca61
Add integration tests for Calcite math functions
yuancu ccbfefc
Rename CalcitePPLMathFunctionsIT to CalcitePPLBuiltinFunctionIT
yuancu 66859e4
Merge pull request #2 from yuancu/addMathUDF
xinyual d059299
add license
xinyual e644401
apply spot
xinyual 17b1462
Update the implementation of CONV function to align with v2's behavior
yuancu 2e94770
Improve code style:
yuancu 3a49d0f
Simplify Calcite PPL math function unit tests
yuancu 1341b75
Alter MOD and SQRT UDF to conform to documented behaviors
yuancu dcd6a66
Complicate math integration tests
yuancu c7e32b4
Handle NULL return in ASIN, ACOS, SQRT and POW by convert returned Do…
yuancu 8728534
Apply spotless on math UDFs and their tests
yuancu 8f56ced
Remove unnecessary Double cast in SQRT UDF
yuancu 0515377
Merge pull request #3 from yuancu/addMathUDF
xinyual 0e7fe63
merge from origin
xinyual eb3e669
Convert returned Double.NaN and Float.NaN from math UDFs to LITERAL_NULL
yuancu 7dae0f7
Correct math UDF integration tests
yuancu 584ca60
Update MOD UDF
yuancu efc1ab8
Modify substring ITs
yuancu 6eb74d1
Merge pull request #6 from yuancu/addMathUDF
xinyual bb77593
apply spot
xinyual 557794b
Replace containsMessage with verifyErrorMessageContains in math ITs
yuancu 58e42da
Correct MOD return types
yuancu 2a16176
Merge pull request #9 from yuancu/addMathUDF
xinyual 7b619e5
fix UT
xinyual File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
28 changes: 28 additions & 0 deletions
28
core/src/main/java/org/opensearch/sql/calcite/udf/mathUDF/CRC32Function.java
This file contains hidden or 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,28 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.udf.mathUDF; | ||
|
|
||
| import java.util.zip.CRC32; | ||
| import org.opensearch.sql.calcite.udf.UserDefinedFunction; | ||
|
|
||
| /** | ||
| * Calculate a cyclic redundancy check value and returns a 32-bit unsigned value<br> | ||
| * The supported signature of crc32 function is<br> | ||
| * STRING -> LONG | ||
| */ | ||
| public class CRC32Function implements UserDefinedFunction { | ||
| @Override | ||
| public Object eval(Object... args) { | ||
| if (args.length != 1) { | ||
| throw new IllegalArgumentException( | ||
| String.format("CRC32 function requires exactly one argument, but got %d", args.length)); | ||
| } | ||
| Object value = args[0]; | ||
| CRC32 crc = new CRC32(); | ||
| crc.update(value.toString().getBytes()); | ||
| return crc.getValue(); | ||
| } | ||
| } |
47 changes: 47 additions & 0 deletions
47
core/src/main/java/org/opensearch/sql/calcite/udf/mathUDF/ConvFunction.java
This file contains hidden or 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,47 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.udf.mathUDF; | ||
|
|
||
| import org.opensearch.sql.calcite.udf.UserDefinedFunction; | ||
|
|
||
| /** | ||
| * Convert number x from base a to base b<br> | ||
| * The supported signature of floor function is<br> | ||
| * (STRING, INTEGER, INTEGER) -> STRING<br> | ||
| * (INTEGER, INTEGER, INTEGER) -> STRING | ||
| */ | ||
| public class ConvFunction implements UserDefinedFunction { | ||
| @Override | ||
| public Object eval(Object... args) { | ||
| if (args.length != 3) { | ||
| throw new IllegalArgumentException( | ||
| String.format("CONV function requires exactly three arguments, but got %d", args.length)); | ||
| } | ||
|
|
||
| Object number = args[0]; | ||
| Object fromBase = args[1]; | ||
| Object toBase = args[2]; | ||
|
|
||
| String numStr = number.toString(); | ||
| int fromBaseInt = Integer.parseInt(fromBase.toString()); | ||
| int toBaseInt = Integer.parseInt(toBase.toString()); | ||
| return conv(numStr, fromBaseInt, toBaseInt); | ||
| } | ||
|
|
||
| /** | ||
| * Convert numStr from fromBase to toBase | ||
| * | ||
| * @param numStr the number to convert (case-insensitive for alphanumeric digits, may have a | ||
| * leading '-') | ||
| * @param fromBase base of the input number (2 to 36) | ||
| * @param toBase target base (2 to 36) | ||
| * @return the converted number in the target base (uppercase), "0" if the input is invalid, or | ||
| * null if bases are out of range. | ||
| */ | ||
| private static String conv(String numStr, int fromBase, int toBase) { | ||
| return Long.toString(Long.parseLong(numStr, fromBase), toBase); | ||
| } | ||
| } | ||
21 changes: 21 additions & 0 deletions
21
core/src/main/java/org/opensearch/sql/calcite/udf/mathUDF/EulerFunction.java
This file contains hidden or 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,21 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.udf.mathUDF; | ||
|
|
||
| import org.opensearch.sql.calcite.udf.UserDefinedFunction; | ||
|
|
||
| /** Get the Euler's number. () -> DOUBLE */ | ||
| public class EulerFunction implements UserDefinedFunction { | ||
| @Override | ||
| public Object eval(Object... args) { | ||
| if (args.length != 0) { | ||
| throw new IllegalArgumentException( | ||
| String.format("Euler function takes no argument, but got %d", args.length)); | ||
| } | ||
|
|
||
| return Math.E; | ||
| } | ||
| } |
64 changes: 64 additions & 0 deletions
64
core/src/main/java/org/opensearch/sql/calcite/udf/mathUDF/ModFunction.java
This file contains hidden or 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,64 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.udf.mathUDF; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import org.opensearch.sql.calcite.udf.UserDefinedFunction; | ||
|
|
||
| /** | ||
| * Calculate the remainder of x divided by y<br> | ||
| * The supported signature of mod function is<br> | ||
| * (x: INTEGER/LONG/FLOAT/DOUBLE, y: INTEGER/LONG/FLOAT/DOUBLE)<br> | ||
| * -> wider type between types of x and y | ||
| */ | ||
| public class ModFunction implements UserDefinedFunction { | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. consider add UT? |
||
|
|
||
| @Override | ||
| public Object eval(Object... args) { | ||
| if (args.length != 2) { | ||
| throw new IllegalArgumentException( | ||
| String.format("MOD function requires exactly two arguments, but got %d", args.length)); | ||
| } | ||
|
|
||
| Object arg0 = args[0]; | ||
| Object arg1 = args[1]; | ||
| if (!(arg0 instanceof Number num0) || !(arg1 instanceof Number num1)) { | ||
| throw new IllegalArgumentException( | ||
| String.format( | ||
| "MOD function requires two numeric arguments, but got %s and %s", | ||
| arg0.getClass().getSimpleName(), arg1.getClass().getSimpleName())); | ||
| } | ||
|
|
||
| // TODO: This precision check is arbitrary. | ||
| if (Math.abs(num1.doubleValue()) < 0.0000001) { | ||
| return null; | ||
| } | ||
|
|
||
| if (isIntegral(num0) && isIntegral(num1)) { | ||
| long l0 = num0.longValue(); | ||
| long l1 = num1.longValue(); | ||
| // It returns negative values when l0 is negative | ||
| long result = l0 % l1; | ||
| // Return the wider type between l0 and l1 | ||
| if (num0 instanceof Long || num1 instanceof Long) { | ||
| return result; | ||
| } | ||
| return (int) result; | ||
| } | ||
|
|
||
| BigDecimal b0 = new BigDecimal(num0.toString()); | ||
| BigDecimal b1 = new BigDecimal(num1.toString()); | ||
| BigDecimal result = b0.remainder(b1); | ||
| if (num0 instanceof Double || num1 instanceof Double) { | ||
| return result.doubleValue(); | ||
| } | ||
| return result.floatValue(); | ||
| } | ||
|
|
||
| private boolean isIntegral(Number n) { | ||
| return n instanceof Byte || n instanceof Short || n instanceof Integer || n instanceof Long; | ||
| } | ||
| } | ||
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
why not check args.length in ConvFunction? and check args.length in ModFunction.
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.
And need some UT or IT for edge case