Table-valued Functions (TVF)

Description

A table-valued function (TVF) is a function that returns a relation or a set of rows. There are two types of TVFs in Spark SQL:

  1. a TVF that can be specified in a FROM clause, e.g. range;
  2. a TVF that can be specified in SELECT/LATERAL VIEW clauses, e.g. explode.

Supported Table-valued Functions

TVFs that can be specified in a FROM clause:

FunctionArgument Type(s)Description
range ( end )LongCreates a table with a single LongType column named id,
containing rows in a range from 0 to end (exclusive) with step value 1.
range ( start, end )Long, LongCreates a table with a single LongType column named id,
containing rows in a range from start to end (exclusive) with step value 1.
range ( start, end, step )Long, Long, LongCreates a table with a single LongType column named id,
containing rows in a range from start to end (exclusive) with step value.
range ( start, end, step, numPartitions )Long, Long, Long, IntCreates a table with a single LongType column named id,
containing rows in a range from start to end (exclusive) with step value, with partition number numPartitions specified.

TVFs that can be specified in SELECT/LATERAL VIEW clauses:

FunctionArgument Type(s)Description
explode ( expr )Array/MapSeparates the elements of array expr into multiple rows, or the elements of map expr into multiple rows and columns. Unless specified otherwise, uses the default column name col for elements of the array or key and value for the elements of the map.
explode_outer
( expr )
Array/MapSeparates the elements of array expr into multiple rows, or the elements of map expr into multiple rows and columns. Unless specified otherwise, uses the default column name col for elements of the array or key and value for the elements of the map.
inline ( expr )ExpressionExplodes an array of structs into a table. Uses column names col1, col2, etc. by default unless specified otherwise.
inline_outer
( expr )
ExpressionExplodes an array of structs into a table. Uses column names col1, col2, etc. by default unless specified otherwise.
posexplode
( expr )
Array/MapSeparates the elements of array expr into multiple rows with positions, or the elements of map expr into multiple rows and columns with positions. Unless specified otherwise, uses the column name pos for position, col for elements of the array or key and value for elements of the map.
posexplode_outer ( expr )Array/MapSeparates the elements of array expr into multiple rows with positions, or the elements of map expr into multiple rows and columns with positions. Unless specified otherwise, uses the column name pos for position, col for elements of the array or key and value for elements of the map.
stack ( n, expr1, …, exprk )Seq[Expression]Separates expr1, …, exprk into n rows. Uses column names col0, col1, etc. by default unless specified otherwise.
json_tuple
( jsonStr, p1, p2, …, pn )
Seq[Expression]Returns a tuple like the function get_json_object, but it takes multiple names. All the input parameters and output column types are string.
parse_url
( url, partToExtract[, key] )
Seq[Expression]Extracts a part from a URL.

Examples

  1. -- range call with end
  2. SELECT * FROM range(6 + cos(3));
  3. +---+
  4. | id|
  5. +---+
  6. | 0|
  7. | 1|
  8. | 2|
  9. | 3|
  10. | 4|
  11. +---+
  12. -- range call with start and end
  13. SELECT * FROM range(5, 10);
  14. +---+
  15. | id|
  16. +---+
  17. | 5|
  18. | 6|
  19. | 7|
  20. | 8|
  21. | 9|
  22. +---+
  23. -- range call with numPartitions
  24. SELECT * FROM range(0, 10, 2, 200);
  25. +---+
  26. | id|
  27. +---+
  28. | 0|
  29. | 2|
  30. | 4|
  31. | 6|
  32. | 8|
  33. +---+
  34. -- range call with a table alias
  35. SELECT * FROM range(5, 8) AS test;
  36. +---+
  37. | id|
  38. +---+
  39. | 5|
  40. | 6|
  41. | 7|
  42. +---+
  43. SELECT explode(array(10, 20));
  44. +---+
  45. |col|
  46. +---+
  47. | 10|
  48. | 20|
  49. +---+
  50. SELECT inline(array(struct(1, 'a'), struct(2, 'b')));
  51. +----+----+
  52. |col1|col2|
  53. +----+----+
  54. | 1| a|
  55. | 2| b|
  56. +----+----+
  57. SELECT posexplode(array(10,20));
  58. +---+---+
  59. |pos|col|
  60. +---+---+
  61. | 0| 10|
  62. | 1| 20|
  63. +---+---+
  64. SELECT stack(2, 1, 2, 3);
  65. +----+----+
  66. |col0|col1|
  67. +----+----+
  68. | 1| 2|
  69. | 3|null|
  70. +----+----+
  71. SELECT json_tuple('{"a":1, "b":2}', 'a', 'b');
  72. +---+---+
  73. | c0| c1|
  74. +---+---+
  75. | 1| 2|
  76. +---+---+
  77. SELECT parse_url('http://spark.apache.org/path?query=1', 'HOST');
  78. +-----------------------------------------------------+
  79. |parse_url(http://spark.apache.org/path?query=1, HOST)|
  80. +-----------------------------------------------------+
  81. | spark.apache.org|
  82. +-----------------------------------------------------+
  83. -- Use explode in a LATERAL VIEW clause
  84. CREATE TABLE test (c1 INT);
  85. INSERT INTO test VALUES (1);
  86. INSERT INTO test VALUES (2);
  87. SELECT * FROM test LATERAL VIEW explode (ARRAY(3,4)) AS c2;
  88. +--+--+
  89. |c1|c2|
  90. +--+--+
  91. | 1| 3|
  92. | 1| 4|
  93. | 2| 3|
  94. | 2| 4|
  95. +--+--+