@@ -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}
0 commit comments