TRANSFORM

Description

The TRANSFORM clause is used to specify a Hive-style transform query specification to transform the inputs by running a user-specified command or script.

Spark’s script transform supports two modes:

  1. Hive support disabled: Spark script transform can run with spark.sql.catalogImplementation=in-memory or without SparkSession.builder.enableHiveSupport(). In this case, now Spark only uses the script transform with ROW FORMAT DELIMITED and treats all values passed to the script as strings.
  2. Hive support enabled: When Spark is run with spark.sql.catalogImplementation=hive or Spark SQL is started with SparkSession.builder.enableHiveSupport(), Spark can use the script transform with both Hive SerDe and ROW FORMAT DELIMITED.

Syntax

  1. SELECT TRANSFORM ( expression [ , ... ] )
  2. [ ROW FORMAT row_format ]
  3. [ RECORDWRITER record_writer_class ]
  4. USING command_or_script [ AS ( [ col_name [ col_type ] ] [ , ... ] ) ]
  5. [ ROW FORMAT row_format ]
  6. [ RECORDREADER record_reader_class ]

Parameters

  • expression

    Specifies a combination of one or more values, operators and SQL functions that results in a value.

  • row_format

    Specifies the row format for input and output. See HIVE FORMAT for more syntax details.

  • RECORDWRITER

    Specifies a fully-qualified class name of a custom RecordWriter. The default value is org.apache.hadoop.hive.ql.exec.TextRecordWriter.

  • RECORDREADER

    Specifies a fully-qualified class name of a custom RecordReader. The default value is org.apache.hadoop.hive.ql.exec.TextRecordReader.

  • command_or_script

    Specifies a command or a path to script to process data.

ROW FORMAT DELIMITED BEHAVIOR

When Spark uses ROW FORMAT DELIMITED format:

  • Spark uses the character \u0001 as the default field delimiter and this delimiter can be overridden by FIELDS TERMINATED BY.
  • Spark uses the character \n as the default line delimiter and this delimiter can be overridden by LINES TERMINATED BY.
  • Spark uses a string \N as the default NULL value in order to differentiate NULL values from the literal string NULL. This delimiter can be overridden by NULL DEFINED AS.
  • Spark casts all columns to STRING and combines columns by tabs before feeding to the user script. For complex types such as ARRAY/MAP/STRUCT, Spark uses to_json casts it to an input JSON string and uses from_json to convert the result output JSON string to ARRAY/MAP/STRUCT data.
  • COLLECTION ITEMS TERMINATED BY and MAP KEYS TERMINATED BY are delimiters to split complex data such as ARRAY/MAP/STRUCT, Spark uses to_json and from_json to handle complex data types with JSON format. So COLLECTION ITEMS TERMINATED BY and MAP KEYS TERMINATED BY won’t work in default row format.
  • The standard output of the user script is treated as tab-separated STRING columns. Any cell containing only a string \N is re-interpreted as a literal NULL value, and then the resulting STRING column will be cast to the data types specified in col_type.
  • If the actual number of output columns is less than the number of specified output columns, additional output columns will be filled with NULL. For example:

    1. output tabs: 1, 2
    2. output columns: A: INT, B INT, C: INT
    3. result:
    4. +---+---+------+
    5. | a| b| c|
    6. +---+---+------+
    7. | 1| 2| NULL|
    8. +---+---+------+
  • If the actual number of output columns is more than the number of specified output columns, the output columns only select the corresponding columns, and the remaining part will be discarded. For example, if the output has three tabs and there are only two output columns:

    1. output tabs: 1, 2, 3
    2. output columns: A: INT, B INT
    3. result:
    4. +---+---+
    5. | a| b|
    6. +---+---+
    7. | 1| 2|
    8. +---+---+
  • If there is no AS clause after USING my_script, the output schema is key: STRING, value: STRING. The key column contains all the characters before the first tab and the value column contains the remaining characters after the first tab. If there are no tabs, Spark returns the NULL value. For example:

    1. output tabs: 1, 2, 3
    2. output columns:
    3. result:
    4. +-----+-------+
    5. | key| value|
    6. +-----+-------+
    7. | 1| 2|
    8. +-----+-------+
    9. output tabs: 1, 2
    10. output columns:
    11. result:
    12. +-----+-------+
    13. | key| value|
    14. +-----+-------+
    15. | 1| NULL|
    16. +-----+-------+

Hive SerDe behavior

When Hive support is enabled and Hive SerDe mode is used:

  • Spark uses the Hive SerDe org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe by default, so columns are cast to STRING and combined by tabs before feeding to the user script.
  • All literal NULL values are converted to a string \N in order to differentiate literal NULL values from the literal string NULL.
  • The standard output of the user script is treated as tab-separated STRING columns, any cell containing only a string \N is re-interpreted as a NULL value, and then the resulting STRING column will be cast to the data type specified in col_type.
  • If the actual number of output columns is less than the number of specified output columns, additional output columns will be filled with NULL.
  • If the actual number of output columns is more than the number of specified output columns, the output columns only select the corresponding columns, and the remaining part will be discarded.
  • If there is no AS clause after USING my_script, the output schema is key: STRING, value: STRING. The key column contains all the characters before the first tab and the value column contains the remaining characters after the first tab. If there is no tab, Spark returns the NULL value.
  • These defaults can be overridden with ROW FORMAT SERDE or ROW FORMAT DELIMITED.

Examples

  1. CREATE TABLE person (zip_code INT, name STRING, age INT);
  2. INSERT INTO person VALUES
  3. (94588, 'Zen Hui', 50),
  4. (94588, 'Dan Li', 18),
  5. (94588, 'Anil K', 27),
  6. (94588, 'John V', NULL),
  7. (94511, 'David K', 42),
  8. (94511, 'Aryan B.', 18),
  9. (94511, 'Lalit B.', NULL);
  10. -- With specified output without data type
  11. SELECT TRANSFORM(zip_code, name, age)
  12. USING 'cat' AS (a, b, c)
  13. FROM person
  14. WHERE zip_code > 94511;
  15. +-------+---------+-----+
  16. | a | b| c|
  17. +-------+---------+-----+
  18. | 94588| Anil K| 27|
  19. | 94588| John V| NULL|
  20. | 94588| Zen Hui| 50|
  21. | 94588| Dan Li| 18|
  22. +-------+---------+-----+
  23. -- With specified output with data type
  24. SELECT TRANSFORM(zip_code, name, age)
  25. USING 'cat' AS (a STRING, b STRING, c STRING)
  26. FROM person
  27. WHERE zip_code > 94511;
  28. +-------+---------+-----+
  29. | a | b| c|
  30. +-------+---------+-----+
  31. | 94588| Anil K| 27|
  32. | 94588| John V| NULL|
  33. | 94588| Zen Hui| 50|
  34. | 94588| Dan Li| 18|
  35. +-------+---------+-----+
  36. -- Using ROW FORMAT DELIMITED
  37. SELECT TRANSFORM(name, age)
  38. ROW FORMAT DELIMITED
  39. FIELDS TERMINATED BY ','
  40. LINES TERMINATED BY '\n'
  41. NULL DEFINED AS 'NULL'
  42. USING 'cat' AS (name_age string)
  43. ROW FORMAT DELIMITED
  44. FIELDS TERMINATED BY '@'
  45. LINES TERMINATED BY '\n'
  46. NULL DEFINED AS 'NULL'
  47. FROM person;
  48. +---------------+
  49. | name_age|
  50. +---------------+
  51. | Anil K,27|
  52. | John V,null|
  53. | ryan B.,18|
  54. | David K,42|
  55. | Zen Hui,50|
  56. | Dan Li,18|
  57. | Lalit B.,null|
  58. +---------------+
  59. -- Using Hive Serde
  60. SELECT TRANSFORM(zip_code, name, age)
  61. ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
  62. WITH SERDEPROPERTIES (
  63. 'field.delim' = '\t'
  64. )
  65. USING 'cat' AS (a STRING, b STRING, c STRING)
  66. ROW FORMAT SERDE 'org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe'
  67. WITH SERDEPROPERTIES (
  68. 'field.delim' = '\t'
  69. )
  70. FROM person
  71. WHERE zip_code > 94511;
  72. +-------+---------+-----+
  73. | a | b| c|
  74. +-------+---------+-----+
  75. | 94588| Anil K| 27|
  76. | 94588| John V| NULL|
  77. | 94588| Zen Hui| 50|
  78. | 94588| Dan Li| 18|
  79. +-------+---------+-----+
  80. -- Schema-less mode
  81. SELECT TRANSFORM(zip_code, name, age)
  82. USING 'cat'
  83. FROM person
  84. WHERE zip_code > 94500;
  85. +-------+---------------------+
  86. | key| value|
  87. +-------+---------------------+
  88. | 94588| Anil K 27|
  89. | 94588| John V \N|
  90. | 94511| Aryan B. 18|
  91. | 94511| David K 42|
  92. | 94588| Zen Hui 50|
  93. | 94588| Dan Li 18|
  94. | 94511| Lalit B. \N|
  95. +-------+---------------------+