Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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