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.
Some comments:
format or something similar to more closely convey its semantics (it doesn't really "print" anything).format(varchar...)@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.
Most helpful comment
@martint, agree, but I think that is still better than the
concat+coalescestuff above. Also, in future versions we could support padding syntax which is nice in some cases.