You are viewing a plain text version of this content. The canonical link for it is here.
Posted to issues@spark.apache.org by "David Mollitor (Jira)" <ji...@apache.org> on 2026/09/16 02:39:00 UTC

[jira] [Updated] (SPARK-59557) Avoid intermediate java.lang.String when casting integral and boolean values to string

     [ https://issues.apache.org/jira/browse/SPARK-59557?page=com.atlassian.jira.plugin.system.issuetabpanels:all-tabpanel ]

David Mollitor updated SPARK-59557:
-----------------------------------
    Description: 
h2. Summary

Casting an integral (byte/short/int/long) or boolean value to string currently routes through {{{}java.lang.String{}}}, allocating several throwaway objects per row. Both the interpreted and codegen cast paths in {{ToStringBase}} emit {{UTF8String.fromString(String.valueOf(x))}} (codegen) / {{UTF8String.fromString(x.toString)}} (interpreted). {{UTF8String.fromString(String)}} then does {{{}str.getBytes(StandardCharsets.UTF_8){}}}, so a single \{{CAST(<long> AS STRING) }}allocates:
 * a {{java.lang.String}} (from {{{}Long.toString{}}}),
 * that String's backing {{{}byte[]{}}},
 * a re-encoded UTF-8 {{byte[]}} (from {{{}getBytes{}}}), and
 * the result {{{}UTF8String{}}}.

Only the last two are essential. {{CAST(<integral> AS STRING)}} is a very common expression (key building, string concatenation, display), so the redundant {{java.lang.String}} round-trip is broad, avoidable per-row overhead.

  was:
h2. Summary

Casting an integral (byte/short/int/long) or boolean value to string currently routes through {{{}java.lang.String{}}}, allocating several throwaway objects per row. Both the interpreted and codegen cast paths in {{ToStringBase}} emit {{UTF8String.fromString(String.valueOf(x))}} (codegen) / {{UTF8String.fromString(x.toString)}} (interpreted). {{UTF8String.fromString(String)}} then does {{{}str.getBytes(StandardCharsets.UTF_8){}}}, so a single \{{CAST(<long> AS STRING) }}allocates:
 * a {{java.lang.String}} (from {{{}Long.toString{}}}),
 * that String's backing {{{}byte[]{}}},
 * a re-encoded UTF-8 {{byte[]}} (from {{{}getBytes{}}}), and
 * the result {{{}UTF8String{}}}.

Only the last two are essential. {{CAST(<integral> AS STRING)}} is a very common
expression (key building, string concatenation, display), so the redundant
{{java.lang.String}} round-trip is broad, avoidable per-row overhead.


> Avoid intermediate java.lang.String when casting integral and boolean values to string
> --------------------------------------------------------------------------------------
>
>                 Key: SPARK-59557
>                 URL: https://issues.apache.org/jira/browse/SPARK-59557
>             Project: Spark
>          Issue Type: Improvement
>          Components: SQL
>    Affects Versions: 4.1.0
>            Reporter: David Mollitor
>            Priority: Minor
>
> h2. Summary
> Casting an integral (byte/short/int/long) or boolean value to string currently routes through {{{}java.lang.String{}}}, allocating several throwaway objects per row. Both the interpreted and codegen cast paths in {{ToStringBase}} emit {{UTF8String.fromString(String.valueOf(x))}} (codegen) / {{UTF8String.fromString(x.toString)}} (interpreted). {{UTF8String.fromString(String)}} then does {{{}str.getBytes(StandardCharsets.UTF_8){}}}, so a single \{{CAST(<long> AS STRING) }}allocates:
>  * a {{java.lang.String}} (from {{{}Long.toString{}}}),
>  * that String's backing {{{}byte[]{}}},
>  * a re-encoded UTF-8 {{byte[]}} (from {{{}getBytes{}}}), and
>  * the result {{{}UTF8String{}}}.
> Only the last two are essential. {{CAST(<integral> AS STRING)}} is a very common expression (key building, string concatenation, display), so the redundant {{java.lang.String}} round-trip is broad, avoidable per-row overhead.



--
This message was sent by Atlassian Jira
(v8.20.10#820010)

---------------------------------------------------------------------
To unsubscribe, e-mail: issues-unsubscribe@spark.apache.org
For additional commands, e-mail: issues-help@spark.apache.org