Partager via


stack Fonction génératrice à valeurs de table

S’applique à :case marquée oui Databricks SQL case marquée oui Databricks Runtime

Sépare expr1, …, exprN en numRows lignes.

Syntaxe

stack(numRows, expr1 [, ...] )

Arguments

  • numRows : littéral de type entier INTEGER supérieur à 0 spécifiant le nombre de lignes produites.
  • exprN : Expression de tout type. Le type de toute exprN doit correspondre au type de expr(N+numRows).

Retours

Ensemble de numRows lignes comprenant max(1, (N/numRows)) colonnes, produites par cette fonction. Une ligne incomplète est remplie avec des NULL.

Par défaut, les colonnes produites sont nommées col0, … col(n-1).

stack équivaut à la VALUES clause.

  • S’applique à :oui coché Databricks Runtime 12.1 et versions précédentes :

    stack ne peut être placé dans la SELECT liste que comme racine d’une expression ou après un LATERAL VIEW. Lorsque vous placez la fonction dans la SELECT liste, il ne doit pas y avoir d’autre fonction génératrice dans la même SELECT liste ou UNSUPPORTED_GENERATOR.MULTI_GENERATOR est levée.

  • S’applique à :coche marquée oui Databricks SQL oui coché Databricks Runtime 12.2 LTS et versions ultérieures :

    L’appel depuis la clause LATERAL VIEW ou la SELECT liste est déconseillé. En lieu et place, appelez stack en tant que table_reference.

Exemples

S’applique à :oui coché Databricks Runtime 12.1 et versions précédentes :

> SELECT 'hello', stack(2, 1, 2, 3) AS (first, second), 'world';
 hello 1  2    world
 hello 3  NULL world

> SELECT 'hello', stack(2, 1, 2, 3) AS (first, second), stack(2, 'a', 'b') AS (third) 'world';
 Error: UNSUPPORTED_GENERATOR.MULTI_GENERATOR

-- Equivalent usage of VALUES
> SELECT 'hello', s1.*, s2.*, 'world'
    FROM VALUES(1, 2), (3, NULL) AS s1(first, second),
         VALUES('a'), ('b') AS s2(third);
 hello  1   2       a   world
 hello  3   NULL    a   world
 hello  1   2       b   world
 hello  3   NULL    b   world

S’applique à :coche marquée oui Databricks SQL oui coché Databricks Runtime 12.2 LTS et versions ultérieures :

> SELECT 'hello', s.*, 'world'
    FROM stack(2, 1, 2, 3) AS s(first, second);
 hello 1  2    world
 hello 3  NULL world

> SELECT 'hello', s1.*, s2.*, 'world'
    FROM stack(2, 1, 2, 3) AS s1(first, second),
         stack(2, 'a', 'b') AS s2(third);
  hello 1  2    a  world
  hello 3  NULL a  world
  hello 1  2    b  world
  hello 3  NULL b  world