Partager via


Fonction de générateur table stack 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

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

Par défaut, le columns produit est nommé 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é que dans l'SELECTlist comme racine d’une expression ou en suivant un LATERAL VIEW. Quand vous placez la fonction dans la SELECTlist, il ne doit pas y avoir d’autre fonction de générateur dans la même liste SELECTlist ou UNSUPPORTED_GENERATOR.MULTI_GENERATOR est déclenché.

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

    L’appel de la clause LATERAL VIEW ou de la clause SELECTlist 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