Fonction de générateur table stack

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 lignes numRows qui comprend des colonnes max(1, (N/numRows)) 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 ultérieures :

    stack peut uniquement être placé dans la liste SELECT en tant que racine d’une expression ou après LATERAL VIEW. Lorsque vous placez la fonction dans la liste SELECT, il ne doit pas y avoir une autre fonction de générateur dans la même liste SELECT sinon 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 à partir de la clause LATERAL VIEW ou de la liste SELECT est déconseillé. En lieu et place, appelez stack en tant que table_reference.

Exemples

S’applique à :coche marquée oui Databricks Runtime 12.1 et versions antérieures :

> 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