Uploaded image for project: 'Spark'
  1. Spark
  2. SPARK-23774

`Cast` to CHAR/VARCHAR should truncate the values

    XMLWordPrintableJSON

    Details

    • Type: Bug
    • Status: Resolved
    • Priority: Major
    • Resolution: Won't Do
    • Affects Version/s: 2.1.2, 2.2.1, 2.3.0
    • Fix Version/s: None
    • Component/s: SQL
    • Labels:
      None

      Description

      This issue aims to fix the following `CAST` behavior on `CHAR/VARCHAR` types.
      Since HiveStringType is used only in parsing, this PR is also about parsing.

      Spark

      scala> sql("SELECT CAST('123' AS CHAR(1)), CAST('123' AS VARCHAR(1))").show
      +-------------------+-------------------+
      |CAST(123 AS STRING)|CAST(123 AS STRING)|
      +-------------------+-------------------+
      |                123|                123|
      +-------------------+-------------------+
      
      scala> sql("SELECT CAST('123' AS CHAR(0)), CAST('123' AS VARCHAR(0))").show
      +-------------------+-------------------+
      |CAST(123 AS STRING)|CAST(123 AS STRING)|
      +-------------------+-------------------+
      |                123|                123|
      +-------------------+-------------------+
      

      Hive

      hive> SELECT CAST('123' AS CHAR(1)), CAST('123' AS VARCHAR(1));
      OK
      1	1
      
      hive> SELECT CAST('123' AS CHAR(0)), CAST('123' AS VARCHAR(0));
      FAILED: RuntimeException Char length 0 out of allowed range [1, 255]
      

        Attachments

          Issue Links

            Activity

              People

              • Assignee:
                Unassigned
                Reporter:
                dongjoon Dongjoon Hyun
              • Votes:
                0 Vote for this issue
                Watchers:
                3 Start watching this issue

                Dates

                • Created:
                  Updated:
                  Resolved: