Skip to content

Commit d0b809a

Browse files
committed
recommended changes
Signed-off-by: Asif Bashar <[email protected]>
1 parent 682eb9d commit d0b809a

File tree

5 files changed

+26
-89
lines changed

5 files changed

+26
-89
lines changed

core/src/main/java/org/opensearch/sql/calcite/utils/PPLOperandTypes.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -112,11 +112,10 @@ private PPLOperandTypes() {}
112112
SqlTypeFamily.INTEGER,
113113
SqlTypeFamily.INTEGER));
114114

115-
public static final UDFOperandMetadata BOOLEAN_OR_NUMERIC_STRING_OR_STRING_STRING =
115+
public static final UDFOperandMetadata NUMERIC_STRING_OR_STRING_STRING =
116116
UDFOperandMetadata.wrap(
117117
(CompositeOperandTypeChecker)
118-
OperandTypes.family(SqlTypeFamily.BOOLEAN)
119-
.or(OperandTypes.family(SqlTypeFamily.NUMERIC, SqlTypeFamily.STRING))
118+
(OperandTypes.family(SqlTypeFamily.NUMERIC, SqlTypeFamily.STRING))
120119
.or(OperandTypes.family(SqlTypeFamily.STRING, SqlTypeFamily.STRING)));
121120

122121
public static final UDFOperandMetadata NUMERIC_NUMERIC_OPTIONAL_NUMERIC =

core/src/main/java/org/opensearch/sql/expression/function/PPLFuncImpTable.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -889,6 +889,12 @@ void populate() {
889889

890890
registerOperator(INTERNAL_PATTERN_PARSER, PPLBuiltinOperators.PATTERN_PARSER);
891891
registerOperator(TOSTRING, PPLBuiltinOperators.TOSTRING);
892+
register(
893+
TOSTRING,
894+
(FunctionImp1)
895+
(builder, source) ->
896+
builder.makeCast(TYPE_FACTORY.createSqlType(SqlTypeName.VARCHAR, true), source),
897+
PPLTypeChecker.family(SqlTypeFamily.ANY));
892898

893899
// Register MVJOIN to use Calcite's ARRAY_JOIN
894900
register(

core/src/main/java/org/opensearch/sql/expression/function/udf/ToStringFunction.java

Lines changed: 10 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ public ToStringFunction() {
4848
public static final String BINARY_FORMAT = "binary";
4949
public static final SqlFunctions.DateFormatFunction dateTimeFormatter =
5050
new SqlFunctions.DateFormatFunction();
51-
public static final String format24hour = "%H:%M:%S"; // 24-hour format
51+
public static final String FORMAT_24_HOUR = "%H:%M:%S";
5252

5353
@Override
5454
public SqlReturnTypeInference getReturnTypeInference() {
@@ -57,7 +57,7 @@ public SqlReturnTypeInference getReturnTypeInference() {
5757

5858
@Override
5959
public UDFOperandMetadata getOperandMetadata() {
60-
return PPLOperandTypes.BOOLEAN_OR_NUMERIC_STRING_OR_STRING_STRING;
60+
return PPLOperandTypes.NUMERIC_STRING_OR_STRING_STRING;
6161
}
6262

6363
public static class ToStringImplementor implements NotNullImplementor {
@@ -66,39 +66,20 @@ public static class ToStringImplementor implements NotNullImplementor {
6666
public Expression implement(
6767
RexToLixTranslator translator, RexCall call, List<Expression> translatedOperands) {
6868
Expression fieldValue = translatedOperands.get(0);
69-
70-
if (translatedOperands.size() > 1) {
71-
Expression format = translatedOperands.get(1);
72-
return Expressions.call(ToStringFunction.class, "toString", fieldValue, format);
73-
} else {
74-
return Expressions.call(ToStringFunction.class, "toString", fieldValue);
75-
}
76-
}
77-
}
78-
79-
@Strict
80-
public static String toString(boolean fieldValue) {
81-
if (fieldValue) {
82-
return "True";
83-
} else {
84-
return "False";
69+
Expression format = translatedOperands.get(1);
70+
return Expressions.call(ToStringFunction.class, "toString", fieldValue, format);
8571
}
8672
}
8773

88-
@Strict
89-
public static String toString(String fieldValue) {
90-
return toString(Boolean.parseBoolean(fieldValue));
91-
}
92-
9374
@Strict
9475
public static String toString(BigDecimal num, String format) {
9576
if (format.equals(DURATION_FORMAT)) {
9677

97-
return dateTimeFormatter.formatTime(format24hour, num.toBigInteger().intValue() * 1000);
78+
return dateTimeFormatter.formatTime(FORMAT_24_HOUR, num.toBigInteger().intValue() * 1000);
9879

9980
} else if (format.equals(DURATION_MILLIS_FORMAT)) {
10081

101-
return dateTimeFormatter.formatTime(format24hour, num.toBigInteger().intValue());
82+
return dateTimeFormatter.formatTime(FORMAT_24_HOUR, num.toBigInteger().intValue());
10283

10384
} else if (format.equals(HEX_FORMAT)) {
10485
return num.toBigInteger().toString(16);
@@ -117,53 +98,17 @@ public static String toString(BigDecimal num, String format) {
11798

11899
@Strict
119100
public static String toString(double num, String format) {
120-
if (format.equals(DURATION_FORMAT)) {
121-
return dateTimeFormatter.formatTime(format24hour, ((int) Math.round(num)) * 1000);
122-
} else if (format.equals(DURATION_MILLIS_FORMAT)) {
123-
124-
return dateTimeFormatter.formatTime(format24hour, ((int) Math.round(num)));
125-
126-
} else if (format.equals(HEX_FORMAT)) {
127-
return Double.toHexString(num);
128-
} else if (format.equals(COMMAS_FORMAT)) {
129-
NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault());
130-
return nf.format(num);
131-
} else if (format.equals(BINARY_FORMAT)) {
132-
return Long.toBinaryString(Double.doubleToLongBits(num));
133-
}
134-
return Double.toString(num);
135-
}
136-
137-
@Strict
138-
public static String toString(short num, String format) {
139-
int i = (int) num;
140-
return toString(i, format);
101+
return toString(BigDecimal.valueOf(num), format);
141102
}
142103

143104
@Strict
144105
public static String toString(int num, String format) {
145-
146-
if (format.equals(DURATION_FORMAT)) {
147-
return dateTimeFormatter.formatTime(format24hour, num * 1000);
148-
} else if (format.equals(DURATION_MILLIS_FORMAT)) {
149-
return dateTimeFormatter.formatTime(format24hour, num);
150-
} else if (format.equals(HEX_FORMAT)) {
151-
return Integer.toHexString(num);
152-
} else if (format.equals(COMMAS_FORMAT)) {
153-
NumberFormat nf = NumberFormat.getNumberInstance(Locale.getDefault());
154-
return nf.format(num);
155-
} else if (format.equals(BINARY_FORMAT)) {
156-
return Integer.toBinaryString(num);
157-
}
158-
return Integer.toString(num);
106+
return toString(BigDecimal.valueOf(num), format);
159107
}
160108

161109
@Strict
162110
public static String toString(String str, String format) {
163-
if (str.contains(".") || (str.length() > 10)) {
164-
return toString(Double.parseDouble(str), format);
165-
} else {
166-
return toString(Integer.parseInt(str), format);
167-
}
111+
BigDecimal bd = new BigDecimal(str);
112+
return toString(bd, format);
168113
}
169114
}

core/src/test/java/org/opensearch/sql/expression/function/udf/ToStringFunctionTest.java

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -15,19 +15,6 @@ public class ToStringFunctionTest {
1515

1616
private final ToStringFunction function = new ToStringFunction();
1717

18-
@Test
19-
void testBooleanToString() {
20-
assertEquals("True", ToStringFunction.toString(true));
21-
assertEquals("False", ToStringFunction.toString(false));
22-
}
23-
24-
@Test
25-
void testStringBooleanToString() {
26-
assertEquals("True", ToStringFunction.toString("true"));
27-
assertEquals("False", ToStringFunction.toString("false"));
28-
assertEquals("False", ToStringFunction.toString("anythingElse"));
29-
}
30-
3118
@Test
3219
void testBigDecimalToStringDurationFormat() {
3320
BigDecimal num = new BigDecimal("3661"); // 1 hour 1 minute 1 second
@@ -74,7 +61,7 @@ void testDoubleToStringDurationFormat() {
7461
void testDoubleToStringHexFormat() {
7562
double num = 10.5;
7663
String result = ToStringFunction.toString(num, ToStringFunction.HEX_FORMAT);
77-
assertTrue(result.startsWith("0x"));
64+
assertTrue(result.equals("a"));
7865
}
7966

8067
@Test

docs/user/ppl/functions/conversion.rst

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -123,16 +123,16 @@ Example::
123123

124124
os> source=EMP | eval salary_hex = tostring(SAL, "hex") | fields ENAME, salary_hex, SAL
125125
fetched rows / total rows = 1/1
126-
+---------------+------------------+------------+
127-
| ENAME | salary_hex | SAL |
128-
|---------------+------------------+------------+
129-
| SMITH | 13880 | 80000.00 |
130-
+---------------+---------------+---------------+
126+
+---------------+---------------+------------+
127+
| ENAME | salary_hex | SAL |
128+
|---------------+------------------+---------+
129+
| SMITH | 13880 | 80000.00 |
130+
+---------------+---------------+------------+
131131

132132
The following example formats the column totalSales to display values with commas.
133133
Example::
134134

135-
os> source=EMP | eval salary_commas = tostring(SAL, "commas") | fields ENAME, salary_commas, SAL
135+
os> source=EMP | eval salary_commas = tostring(SAL, "commas") | fields ENAME, salary_commas, SAL
136136
fetched rows / total rows = 1/1
137137
+---------------+------------------+------------+
138138
| ENAME | salary_commas | SAL |
@@ -143,7 +143,7 @@ Example::
143143
The following example converts number of seconds to HH:MM:SS format representing hours, minutes and seconds.
144144
Example::
145145

146-
os> source=EMP | eval duration = tostring(6500, "duration") | fields ENAME, duration
146+
os> source=EMP | eval duration = tostring(6500, "duration") | fields ENAME, duration
147147
fetched rows / total rows = 1/1
148148
+---------------+-------------+
149149
| ENAME | duration |

0 commit comments

Comments
 (0)