Presto: Feature request: support for PRINTF or better string formatting

Created on 23 Aug 2018  路  6Comments  路  Source: prestodb/presto

There is no direct alternative in Presto to Hive's PRINTF and to achieve the same functionality, users have to do CAST and || themselvels. Here is an example/comparison between Hive's string formatting and Presto's :

In Hive, users can just do

PRINT('a=%s#b=%s#c=%s', a, b, c)

In Presto, users have to do something like

'a=' || COALESCE(CAST(a AS VARCHAR), 'null') || 'b=' || COALESCE(CAST(b AS VARCHAR), 'null') || 'c=' || COALESCE(CAST(c AS VARCHAR), 'null') 

Because CAST(NULL AS VARCHAR) returnsNULL while PRINTF('%s', NULL) returns 'null' and besides users have to split the format string themselves.

stale

Most helpful comment

@martint, agree, but I think that is still better than the concat + coalesce stuff above. Also, in future versions we could support padding syntax which is nice in some cases.

All 6 comments

Some comments:

  • I'd probably name the function format or something similar to more closely convey its semantics (it doesn't really "print" anything).
  • It'd be hard to implement the function as described in this issue. These are the current limitations:

    • Not having a type hierarchy. i.e., the function can't take "any" or "object" as the arguments.

    • No implicit coercions from arbitrary types to VARCHAR, so it can't be modeled as format(varchar...)

    • Possibly (need to verify), functions can't have mixed varargs and non-varargs arguments (especially of different types)

@martint I was thinking the same issue about coercions/casting. @haonans would only supporting %s be good enough?

Even with %s, you'd need to call it as follows due to lack of implicit casts between arbitrary types and varchar:

format('a=%s, b=%s, c=%s', cast(a as varchar), cast(b as varchar), cast(c as varchar)))

@martint, agree, but I think that is still better than the concat + coalesce stuff above. Also, in future versions we could support padding syntax which is nice in some cases.

Doing format + cast is a lot better than doing concat + cast + coalesce since otherwise the string format is broken into pieces and will cause a lot of difficulties on understanding and debugging the code.

This issue has been automatically marked as stale because it has not had any activity in the last 2 years. If you feel that this issue is important, just comment and the stale tag will be removed; otherwise it will be closed in 7 days. This is an attempt to ensure that our open issues remain valuable and relevant so that we can keep track of what needs to be done and prioritize the right things.

Was this page helpful?
0 / 5 - 0 ratings