generated from amazon-archives/__template_Custom
-
Notifications
You must be signed in to change notification settings - Fork 177
PPL tostring() implementation issue #4492 #4497
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
Open
asifabashar
wants to merge
22
commits into
opensearch-project:main
Choose a base branch
from
asifabashar:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
0ed23de
PPL tostring() implementation issue #4492
asifabashar 5a5b778
removed sql changes
asifabashar 9d71a95
doc changes
asifabashar be2c2e2
docs changes
asifabashar fc763a4
reverted string doc changes
asifabashar a04eb14
removed extra word
asifabashar 590a8e6
added any type
asifabashar 6938e2c
doc formatting fixes
asifabashar 314fccd
description for boolean example
asifabashar 0ee17b9
added format_time call from calcite , added duration_millis as splunk…
asifabashar 6e24aa3
added doc update to specifically set 2nd argument as optional
asifabashar 454cfc8
mentioned as value instead of number specifically
asifabashar b221e8c
fixed wrong bullet point
asifabashar 1ed88e1
removed extra quote
asifabashar 5cbedcc
we have duplicated example
asifabashar 682eb9d
applied recommended changes
asifabashar d0b809a
recommended changes
asifabashar ba3553b
requested changes
asifabashar 08429c1
requested changes
asifabashar 7e6e9ac
updated with try catch for string parsing.
asifabashar 1420e8f
merge conflict fix
asifabashar 42d705a
Merge branch 'default-main'
asifabashar 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
Some comments aren't visible on the classic Files Changed page.
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
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
118 changes: 118 additions & 0 deletions
118
core/src/main/java/org/opensearch/sql/expression/function/udf/ToStringFunction.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,118 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.expression.function.udf; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.math.BigInteger; | ||
| import java.text.NumberFormat; | ||
| import java.util.List; | ||
| import java.util.Locale; | ||
| import org.apache.calcite.adapter.enumerable.NotNullImplementor; | ||
| import org.apache.calcite.adapter.enumerable.NullPolicy; | ||
| import org.apache.calcite.adapter.enumerable.RexToLixTranslator; | ||
| import org.apache.calcite.linq4j.function.Strict; | ||
| import org.apache.calcite.linq4j.tree.Expression; | ||
| import org.apache.calcite.linq4j.tree.Expressions; | ||
| import org.apache.calcite.rex.RexCall; | ||
| import org.apache.calcite.runtime.SqlFunctions; | ||
| import org.apache.calcite.sql.*; | ||
| import org.apache.calcite.sql.type.SqlReturnTypeInference; | ||
| import org.opensearch.sql.calcite.utils.PPLOperandTypes; | ||
| import org.opensearch.sql.calcite.utils.PPLReturnTypes; | ||
| import org.opensearch.sql.expression.function.ImplementorUDF; | ||
| import org.opensearch.sql.expression.function.UDFOperandMetadata; | ||
|
|
||
| /** | ||
| * A custom implementation of number/boolean to string . | ||
| * | ||
| * <p>This operator is necessary because tostring has following requirements "binary" Converts a | ||
| * number to a binary value. "hex" Converts the number to a hexadecimal value. "commas" Formats the | ||
| * number with commas. If the number includes a decimal, the function rounds the number to nearest | ||
| * two decimal places. "duration" Converts the value in seconds to the readable time format | ||
| * HH:MM:SS. if not format parameter provided, then consider value as boolean | ||
| */ | ||
| public class ToStringFunction extends ImplementorUDF { | ||
| public ToStringFunction() { | ||
| super( | ||
| new org.opensearch.sql.expression.function.udf.ToStringFunction.ToStringImplementor(), | ||
| NullPolicy.ANY); | ||
| } | ||
|
|
||
| public static final String DURATION_FORMAT = "duration"; | ||
| public static final String DURATION_MILLIS_FORMAT = "duration_millis"; | ||
| public static final String HEX_FORMAT = "hex"; | ||
| public static final String COMMAS_FORMAT = "commas"; | ||
| public static final String BINARY_FORMAT = "binary"; | ||
| public static final SqlFunctions.DateFormatFunction dateTimeFormatter = | ||
| new SqlFunctions.DateFormatFunction(); | ||
| public static final String FORMAT_24_HOUR = "%H:%M:%S"; | ||
|
|
||
| @Override | ||
| public SqlReturnTypeInference getReturnTypeInference() { | ||
| return PPLReturnTypes.STRING_FORCE_NULLABLE; | ||
| } | ||
|
|
||
| @Override | ||
| public UDFOperandMetadata getOperandMetadata() { | ||
| return PPLOperandTypes.NUMERIC_STRING_OR_STRING_STRING; | ||
| } | ||
|
|
||
| public static class ToStringImplementor implements NotNullImplementor { | ||
|
|
||
| @Override | ||
| public Expression implement( | ||
| RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) { | ||
| Expression fieldValue = translatedOperands.get(0); | ||
| Expression format = translatedOperands.get(1); | ||
| return Expressions.call(ToStringFunction.class, "toString", fieldValue, format); | ||
| } | ||
| } | ||
|
|
||
| @Strict | ||
| public static String toString(BigDecimal num, String format) { | ||
asifabashar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if (format.equals(DURATION_FORMAT)) { | ||
|
|
||
| return dateTimeFormatter.formatTime(FORMAT_24_HOUR, num.toBigInteger().intValue() * 1000); | ||
|
|
||
| } else if (format.equals(DURATION_MILLIS_FORMAT)) { | ||
|
|
||
| return dateTimeFormatter.formatTime(FORMAT_24_HOUR, num.toBigInteger().intValue()); | ||
|
|
||
| } else if (format.equals(HEX_FORMAT)) { | ||
| return num.toBigInteger().toString(16); | ||
| } else if (format.equals(COMMAS_FORMAT)) { | ||
| NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault()); | ||
| nf.setMinimumFractionDigits(0); | ||
| nf.setMaximumFractionDigits(2); | ||
| return nf.format(num); | ||
|
|
||
| } else if (format.equals(BINARY_FORMAT)) { | ||
| BigInteger integerPart = num.toBigInteger(); // 42 | ||
| return integerPart.toString(2); | ||
| } | ||
| return num.toString(); | ||
| } | ||
|
|
||
| @Strict | ||
| public static String toString(double num, String format) { | ||
asifabashar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| return toString(BigDecimal.valueOf(num), format); | ||
| } | ||
|
|
||
| @Strict | ||
| public static String toString(int num, String format) { | ||
| return toString(BigDecimal.valueOf(num), format); | ||
| } | ||
|
|
||
| @Strict | ||
| public static String toString(String str, String format) { | ||
asifabashar marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| try { | ||
| BigDecimal bd = new BigDecimal(str); | ||
| return toString(bd, format); | ||
| } catch (Exception e) { | ||
| return null; | ||
| } | ||
| } | ||
| } | ||
135 changes: 135 additions & 0 deletions
135
core/src/test/java/org/opensearch/sql/expression/function/udf/ToStringFunctionTest.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,135 @@ | ||
| /* | ||
| * Copyright OpenSearch Contributors | ||
| * SPDX-License-Identifier: Apache-2.0 | ||
| */ | ||
|
|
||
| package org.opensearch.sql.expression.function.udf; | ||
|
|
||
| import static org.junit.jupiter.api.Assertions.*; | ||
|
|
||
| import java.math.BigDecimal; | ||
| import java.util.Locale; | ||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| public class ToStringFunctionTest { | ||
|
|
||
| private final ToStringFunction function = new ToStringFunction(); | ||
|
|
||
| @Test | ||
| void testBigDecimalToStringDurationFormat() { | ||
| BigDecimal num = new BigDecimal("3661"); // 1 hour 1 minute 1 second | ||
| String result = ToStringFunction.toString(num, ToStringFunction.DURATION_FORMAT); | ||
| assertEquals("01:01:01", result); | ||
| } | ||
|
|
||
| @Test | ||
| void testBigDecimalToStringHexFormat() { | ||
| BigDecimal num = new BigDecimal("255"); | ||
| String result = ToStringFunction.toString(num, ToStringFunction.HEX_FORMAT); | ||
| assertEquals("ff", result); | ||
| } | ||
|
|
||
| @Test | ||
| void testBigDecimalToStringCommasFormat() { | ||
| Locale.setDefault(Locale.US); // Ensure predictable comma placement | ||
| BigDecimal num = new BigDecimal("1234567.891"); | ||
| String result = ToStringFunction.toString(num, ToStringFunction.COMMAS_FORMAT); | ||
| assertTrue(result.contains(",")); | ||
| } | ||
|
|
||
| @Test | ||
| void testBigDecimalToStringBinaryFormat() { | ||
| BigDecimal num = new BigDecimal("10"); | ||
| String result = ToStringFunction.toString(num, ToStringFunction.BINARY_FORMAT); | ||
| assertEquals("1010", result); | ||
| } | ||
|
|
||
| @Test | ||
| void testBigDecimalToStringDefault() { | ||
| BigDecimal num = new BigDecimal("123.45"); | ||
| assertEquals("123.45", ToStringFunction.toString(num, "unknown")); | ||
| } | ||
|
|
||
| @Test | ||
| void testDoubleToStringDurationFormat() { | ||
| double num = 3661.4; | ||
| String result = ToStringFunction.toString(num, ToStringFunction.DURATION_FORMAT); | ||
| assertEquals("01:01:01", result); | ||
| } | ||
|
|
||
| @Test | ||
| void testDoubleToStringHexFormat() { | ||
| double num = 10.5; | ||
| String result = ToStringFunction.toString(num, ToStringFunction.HEX_FORMAT); | ||
| assertTrue(result.equals("a")); | ||
| } | ||
|
|
||
| @Test | ||
| void testDoubleToStringCommasFormat() { | ||
| Locale.setDefault(Locale.US); | ||
| double num = 12345.678; | ||
| String result = ToStringFunction.toString(num, ToStringFunction.COMMAS_FORMAT); | ||
| assertTrue(result.contains(",")); | ||
| } | ||
|
|
||
| @Test | ||
| void testDoubleToStringBinaryFormat() { | ||
| double num = 10.0; | ||
| String result = ToStringFunction.toString(num, ToStringFunction.BINARY_FORMAT); | ||
| assertNotNull(result); | ||
| assertFalse(result.isEmpty()); | ||
| } | ||
|
|
||
| @Test | ||
| void testDoubleToStringDefault() { | ||
| assertEquals("10.5", ToStringFunction.toString(10.5, "unknown")); | ||
| } | ||
|
|
||
| @Test | ||
| void testIntToStringDurationFormat() { | ||
| int num = 3661; | ||
| String result = ToStringFunction.toString(num, ToStringFunction.DURATION_FORMAT); | ||
| assertEquals("01:01:01", result); | ||
| } | ||
|
|
||
| @Test | ||
| void testIntToStringHexFormat() { | ||
| assertEquals("ff", ToStringFunction.toString(255, ToStringFunction.HEX_FORMAT)); | ||
| } | ||
|
|
||
| @Test | ||
| void testIntToStringCommasFormat() { | ||
| Locale.setDefault(Locale.US); | ||
| String result = ToStringFunction.toString(1234567, ToStringFunction.COMMAS_FORMAT); | ||
| assertTrue(result.contains(",")); | ||
| } | ||
|
|
||
| @Test | ||
| void testIntToStringBinaryFormat() { | ||
| assertEquals("1010", ToStringFunction.toString(10, ToStringFunction.BINARY_FORMAT)); | ||
| } | ||
|
|
||
| @Test | ||
| void testIntToStringDefault() { | ||
| assertEquals("123", ToStringFunction.toString(123, "unknown")); | ||
| } | ||
|
|
||
| @Test | ||
| void testStringNumericToStringIntFormat() { | ||
| String result = ToStringFunction.toString("42", ToStringFunction.HEX_FORMAT); | ||
| assertEquals("2a", result); | ||
| } | ||
|
|
||
| @Test | ||
| void testStringNumericToStringDoubleFormat() { | ||
| String result = ToStringFunction.toString("42.5", ToStringFunction.COMMAS_FORMAT); | ||
| assertTrue(result.contains("42")); | ||
| } | ||
|
|
||
| @Test | ||
| void testStringLargeNumberAsDouble() { | ||
| String largeNum = "1234567890123"; | ||
| String result = ToStringFunction.toString(largeNum, ToStringFunction.BINARY_FORMAT); | ||
| assertNotNull(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
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.