Skip to content

Conversation

@asifabashar
Copy link

@asifabashar asifabashar commented Oct 10, 2025

Description

Is your feature request related to a problem?
SPL allows tostring which implemented here.
part of RFC #4287 - tostring implementation
issue #4492

What solution would you like?
Function: tostring(,)
Description
This function converts a value to a string. If the value is a number, this function reformats it as a string. If the value is a Boolean value, it returns the corresponding string value, "True" or "False".
Usage
You can use this function with the eval commands and as part of eval expressions.
The value argument can be a field name or a value.

Number is accepted as input to the function.

When you use the tostring function with the eval command, the returned values might not sort as expected. Use the tostring function to format the displayed values.

The format argument is optional and is only used when the value argument is a number. The tostring function supports the following formats.

Format Description
"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.
Binary conversion
You can use this function to convert a number to a string of its binary representation. For example, the result of the following function is 1001, because the binary representation of 9 is 1001.:
eval result = tostring(9, "binary")

For information about bitwise functions that you can use with the tostring function, see Bitwise functions.

Basic examples
The following example returns "True 0xF 12,345.68".
... | eval n=tostring(1==1) + " " + tostring(15, "hex") + " " + tostring(12345.6789, "commas")
The following example returns foo=615 and foo2=00:10:15. The 615 seconds is converted into minutes and seconds.

... | eval foo=615 | eval foo2 = tostring(foo, "duration")
The following example formats the column totalSales to display values with a currency symbol and commas. You must use a period between the currency value and the tostring function.

Related Issues

Resolves #[Issue number to be closed when this PR is merged]

Check List

  • New functionality includes testing.
  • New functionality has been documented.
  • New functionality has javadoc added.
  • New functionality has a user manual doc added.
  • New PPL command checklist all confirmed.
  • API changes companion pull request created.
  • Commits are signed per the DCO using --signoff or -s.
  • Public documentation issue/PR created.

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@asifabashar
Copy link
Author

@penghuo Please review

@penghuo penghuo added PPL Piped processing language enhancement New feature or request labels Oct 10, 2025
@asifabashar
Copy link
Author

@penghuo Thanks for your comments and review. I have made the changes, please check.

Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
… default duration is in seconds which will be used for duration format , added cast call for tostring with 1 argument

Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
@asifabashar
Copy link
Author

@penghuo please review

@asifabashar
Copy link
Author

please review @penghuo

@asifabashar asifabashar requested a review from penghuo October 21, 2025 04:44
@asifabashar
Copy link
Author

@penghuo please review

Signed-off-by: Asif Bashar <[email protected]>
Copy link
Author

@asifabashar asifabashar left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please review @penghuo

@asifabashar asifabashar requested a review from penghuo October 26, 2025 03:33
Signed-off-by: Asif Bashar <[email protected]>
Signed-off-by: Asif Bashar <[email protected]>
Comment on lines +109 to +113
@Strict
public static String toString(String str, String format) {
BigDecimal bd = new BigDecimal(str);
return toString(bd, format);
}
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

With string->numberic auto-conversion feature, this function may not required. e.g. tostring("42", "hex"), at query analysis stage, this query will be rewrite as tostring(SAFE_CAST("42" as double), "hex")
Please double confrim.

If this function still required, add try-catch to return null value is string. can not converted to number.

Comment on lines +115 to +119
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)));
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if leverage string->number auto-conversion, let's remove <STRING, STRING> signature.
https://github.com/opensearch-project/sql/pull/4497/files#r2466347484

@penghuo
Copy link
Collaborator

penghuo commented Oct 27, 2025

@asifabashar Please resolve conflicts.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

enhancement New feature or request PPL Piped processing language

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants