Appel de fonction table (TVF)
S’applique à : Databricks SQL
Databricks Runtime
Appelle une fonction qui renvoie un groupe ou un ensemble de lignes sous la forme d’une référence de table.
Un TVF peut être un :
Fonction de table SQL définie par l’utilisateur.
La fonction table range.
Toute fonction de générateur table, telle que l’explosion.
S’applique à :
Databricks SQL
Databricks Runtime 12.2 LTS et versions ultérieures.
Remarque
UDTF Hive ne peut pas être appelé comme référence de table, mais doit être appelé depuis SELECT
ou en utilisant la LATERAL VIEWclause .
Syntaxe
function_name ( [ expression [, ...] ] ) [ table_alias ]
Paramètres
-
Fonction table.
-
Combinaison d’un ou plusieurs opérateurs, valeurs et fonctions SQL qui produit une valeur.
-
Étiquette facultative pour référencer le résultat de la fonction et ses colonnes.
Exemples
-- range call with end
> SELECT * FROM range(6 + cos(3));
0
1
2
3
4
-- range call with start and end
> SELECT * FROM range(5, 10);
5
6
7
8
9
-- range call with numPartitions
> SELECT * FROM range(0, 10, 2, 200);
0
2
4
6
8
-- range call with a table alias
> SELECT * FROM range(5, 8) AS test;
5
6
7
-- Create a SQL UDTF and invoke it
> CREATE OR REPLACE FUNCTION table_func(a INT) RETURNS TABLE
RETURN SELECT a * c1 AS res FROM VALUES(1), (2), (3), (4) AS T(c1)
> SELECT * FROM table_func(5);
5
10
15
20
-- Using lateral correlation
> SELECT table_func.res FROM VALUES(10), (20) AS S(c1), LATERAL table_func(c1);
10
20
20
40
30
60
40
80
-- Scalar functions are not allowed in the FROM clause
> SELECT * FROM trim('hello ');
Error
Sur Databricks SQL et Databricks Runtime 12.2 LTS et versions ultérieures :
> SELECT * FROM explode(array(10, 20));
10
20
> SELECT * FROM inline(array(struct(1, 'a'), struct(2, 'b')));
col1 col2
---- ----
1 a
2 b
> SELECT * FROM posexplode(array(10,20));
pos col
--- ---
0 10
1 20
> SELECT * FROM stack(2, 1, 2, 3);
col0 col1
---- ----
1 2
3 null
> SELECT * FROM json_tuple('{"a":1, "b":2}', 'a', 'b');
c0 c1
--- ---
1 2
> SELECT * FROM parse_url('http://spark.apache.org/path?query=1', 'HOST');
spark.apache.org
> SELECT * FROM VALUES(1), (2) AS t1(c1), LATERAL explode (ARRAY(3,4)) AS t2(c2);
c1 c2
-- --
1 3
1 4
2 3
2 4