Skip to content
Merged
Show file tree
Hide file tree
Changes from 22 commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
0ed23de
PPL tostring() implementation issue #4492
asifabashar Oct 16, 2025
5a5b778
removed sql changes
asifabashar Oct 16, 2025
9d71a95
doc changes
asifabashar Oct 16, 2025
be2c2e2
docs changes
asifabashar Oct 16, 2025
fc763a4
reverted string doc changes
asifabashar Oct 16, 2025
a04eb14
removed extra word
asifabashar Oct 16, 2025
590a8e6
added any type
asifabashar Oct 16, 2025
6938e2c
doc formatting fixes
asifabashar Oct 16, 2025
314fccd
description for boolean example
asifabashar Oct 16, 2025
0ee17b9
added format_time call from calcite , added duration_millis as splunk…
asifabashar Oct 17, 2025
6e24aa3
added doc update to specifically set 2nd argument as optional
asifabashar Oct 17, 2025
454cfc8
mentioned as value instead of number specifically
asifabashar Oct 17, 2025
b221e8c
fixed wrong bullet point
asifabashar Oct 17, 2025
1ed88e1
removed extra quote
asifabashar Oct 20, 2025
5cbedcc
we have duplicated example
asifabashar Oct 21, 2025
682eb9d
applied recommended changes
asifabashar Oct 21, 2025
d0b809a
recommended changes
asifabashar Oct 26, 2025
ba3553b
requested changes
asifabashar Oct 26, 2025
08429c1
requested changes
asifabashar Oct 26, 2025
7e6e9ac
updated with try catch for string parsing.
asifabashar Oct 28, 2025
1420e8f
merge conflict fix
asifabashar Oct 28, 2025
42d705a
Merge branch 'default-main'
asifabashar Oct 28, 2025
f9c9a12
added back missing tostring doc
asifabashar Nov 2, 2025
123fcd5
updated fix for doctest
asifabashar Nov 5, 2025
e6d0c8c
fix doctest failures
asifabashar Nov 5, 2025
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ private PPLOperandTypes() {}
SqlTypeFamily.INTEGER,
SqlTypeFamily.INTEGER));

public static final UDFOperandMetadata NUMERIC_STRING_OR_STRING_STRING =
UDFOperandMetadata.wrap(
(CompositeOperandTypeChecker)
(OperandTypes.family(SqlTypeFamily.NUMERIC, SqlTypeFamily.STRING))
.or(OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.STRING)));

public static final UDFOperandMetadata NUMERIC_NUMERIC_OPTIONAL_NUMERIC =
UDFOperandMetadata.wrap(
(CompositeOperandTypeChecker)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@
import org.opensearch.sql.expression.function.udf.RexExtractMultiFunction;
import org.opensearch.sql.expression.function.udf.RexOffsetFunction;
import org.opensearch.sql.expression.function.udf.SpanFunction;
import org.opensearch.sql.expression.function.udf.ToStringFunction;
import org.opensearch.sql.expression.function.udf.condition.EarliestFunction;
import org.opensearch.sql.expression.function.udf.condition.EnhancedCoalesceFunction;
import org.opensearch.sql.expression.function.udf.condition.LatestFunction;
Expand Down Expand Up @@ -413,6 +414,7 @@ public class PPLBuiltinOperators extends ReflectiveSqlOperatorTable {
RELEVANCE_QUERY_FUNCTION_INSTANCE.toUDF("multi_match", false);
public static final SqlOperator NUMBER_TO_STRING =
new NumberToStringFunction().toUDF("NUMBER_TO_STRING");
public static final SqlOperator TOSTRING = new ToStringFunction().toUDF("TOSTRING");
public static final SqlOperator WIDTH_BUCKET =
new org.opensearch.sql.expression.function.udf.binning.WidthBucketFunction()
.toUDF("WIDTH_BUCKET");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,7 @@
import static org.opensearch.sql.expression.function.BuiltinFunctionName.TIMESTAMPDIFF;
import static org.opensearch.sql.expression.function.BuiltinFunctionName.TIME_FORMAT;
import static org.opensearch.sql.expression.function.BuiltinFunctionName.TIME_TO_SEC;
import static org.opensearch.sql.expression.function.BuiltinFunctionName.TOSTRING;
import static org.opensearch.sql.expression.function.BuiltinFunctionName.TO_DAYS;
import static org.opensearch.sql.expression.function.BuiltinFunctionName.TO_SECONDS;
import static org.opensearch.sql.expression.function.BuiltinFunctionName.TRANSFORM;
Expand Down Expand Up @@ -944,6 +945,13 @@ void populate() {
registerOperator(WEEKOFYEAR, PPLBuiltinOperators.WEEK);

registerOperator(INTERNAL_PATTERN_PARSER, PPLBuiltinOperators.PATTERN_PARSER);
registerOperator(TOSTRING, PPLBuiltinOperators.TOSTRING);
register(
TOSTRING,
(FunctionImp1)
(builder, source) ->
builder.makeCast(TYPE_FACTORY.createSqlType(SqlTypeName.VARCHAR, true), source),
PPLTypeChecker.family(SqlTypeFamily.ANY));

// Register MVJOIN to use Calcite's ARRAY_JOIN
register(
Expand Down Expand Up @@ -1112,6 +1120,7 @@ void populate() {
SqlTypeFamily.INTEGER,
SqlTypeFamily.INTEGER)),
false));

register(
LOG,
(FunctionImp2)
Expand Down
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) {
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) {
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) {
try {
BigDecimal bd = new BigDecimal(str);
return toString(bd, format);
} catch (Exception e) {
return null;
}
}
}
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);
}
}
1 change: 1 addition & 0 deletions ppl/src/main/antlr/OpenSearchPPLLexer.g4
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,7 @@ STRFTIME: 'STRFTIME';
// TEXT FUNCTIONS
SUBSTR: 'SUBSTR';
SUBSTRING: 'SUBSTRING';
TOSTRING: 'TOSTRING';
LTRIM: 'LTRIM';
RTRIM: 'RTRIM';
TRIM: 'TRIM';
Expand Down
4 changes: 4 additions & 0 deletions ppl/src/main/antlr/OpenSearchPPLParser.g4
Original file line number Diff line number Diff line change
Expand Up @@ -845,11 +845,13 @@ evalFunctionCall
: evalFunctionName LT_PRTHS functionArgs RT_PRTHS
;


// cast function
dataTypeFunctionCall
: CAST LT_PRTHS logicalExpression AS convertedDataType RT_PRTHS
;


convertedDataType
: typeName = DATE
| typeName = TIME
Expand Down Expand Up @@ -1214,6 +1216,7 @@ systemFunctionName
textFunctionName
: SUBSTR
| SUBSTRING
| TOSTRING
| TRIM
| LTRIM
| RTRIM
Expand Down Expand Up @@ -1432,6 +1435,7 @@ searchableKeyWord
| USING
| VALUE
| CAST
| TOSTRING
| GET_FORMAT
| EXTRACT
| INTERVAL
Expand Down
Loading
Loading