generated from amazon-archives/__template_Custom
-
Couldn't load subscription status.
- Fork 176
Fix return types of MOD and DIVIDE UDFs #3513
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
Merged
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
34 changes: 34 additions & 0 deletions
34
core/src/main/java/org/opensearch/sql/calcite/udf/mathUDF/DivideFunction.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,34 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.udf.mathUDF; | ||
|
|
||
| import org.opensearch.sql.calcite.udf.UserDefinedFunction; | ||
| import org.opensearch.sql.calcite.utils.MathUtils; | ||
| import org.opensearch.sql.calcite.utils.UserDefinedFunctionUtils; | ||
|
|
||
| public class DivideFunction implements UserDefinedFunction { | ||
|
|
||
| @Override | ||
| public Object eval(Object... args) { | ||
| if (UserDefinedFunctionUtils.containsNull(args)) { | ||
| return null; | ||
| } | ||
|
|
||
| Number dividend = (Number) args[0]; | ||
| Number divisor = (Number) args[1]; | ||
|
|
||
| if (divisor.doubleValue() == 0) { | ||
| return null; | ||
| } | ||
|
|
||
| if (MathUtils.isIntegral(dividend) && MathUtils.isIntegral(divisor)) { | ||
| long result = dividend.longValue() / divisor.longValue(); | ||
| return MathUtils.coerceToWidestIntegralType(dividend, divisor, result); | ||
| } | ||
| double result = dividend.doubleValue() / divisor.doubleValue(); | ||
| return MathUtils.coerceToWidestFloatingType(dividend, divisor, result); | ||
| } | ||
| } |
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
52 changes: 52 additions & 0 deletions
52
core/src/main/java/org/opensearch/sql/calcite/utils/MathUtils.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,52 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.calcite.utils; | ||
|
|
||
| public class MathUtils { | ||
| public static boolean isIntegral(Number n) { | ||
| return n instanceof Byte || n instanceof Short || n instanceof Integer || n instanceof Long; | ||
| } | ||
|
|
||
| /** | ||
| * Converts a long value to the least restrictive integral type based on the types of two input | ||
| * numbers. | ||
| * | ||
| * <p>This is useful when performing operations like division or modulo and you want to preserve | ||
| * the most appropriate type (e.g., int vs long). | ||
| * | ||
| * @param a one operand involved in the operation | ||
| * @param b another operand involved in the operation | ||
| * @param value the result to convert to the least restrictive integral type | ||
| * @return the value converted to Byte, Short, Integer, or Long | ||
| */ | ||
| public static Number coerceToWidestIntegralType(Number a, Number b, long value) { | ||
| if (a instanceof Long || b instanceof Long) { | ||
| return value; | ||
| } else if (a instanceof Integer || b instanceof Integer) { | ||
| return (int) value; | ||
| } else if (a instanceof Short || b instanceof Short) { | ||
| return (short) value; | ||
| } else { | ||
| return (byte) value; | ||
| } | ||
| } | ||
|
|
||
| /** | ||
| * Converts a double value to the least restrictive floating type based on the types of two input | ||
| * | ||
| * @param a one operand involved in the operation | ||
| * @param b another operand involved in the operation | ||
| * @param value the result to convert to the least restrictive floating type | ||
| * @return the value converted to Float or Double | ||
| */ | ||
| public static Number coerceToWidestFloatingType(Number a, Number b, double value) { | ||
| if (a instanceof Double || b instanceof Double) { | ||
| return value; | ||
| } else { | ||
| return (float) value; | ||
| } | ||
| } | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.