Function mz_sql::plan::query::plan_rows_from

source ยท
fn plan_rows_from(
    qcx: &QueryContext<'_>,
    functions: &[Function<Aug>],
    alias: Option<&TableAlias>,
    with_ordinality: bool,
) -> Result<(HirRelationExpr, Scope), PlanError>
Expand description

Plans a ROWS FROM expression.

ROWS FROM concatenates table functions into a single table, filling in NULLs in places where one table function has fewer rows than another. We can achieve this by augmenting each table function with a row number, doing a FULL JOIN between each table function on the row number and eventually projecting away the row number columns. Concretely, the following query using ROWS FROM

SELECT
    *
FROM
    ROWS FROM (
        generate_series(1, 2),
        information_schema._pg_expandarray(ARRAY[9]),
        generate_series(3, 6)
    );

is equivalent to the following query that does not use ROWS FROM:

SELECT
    gs1.generate_series, expand.x, expand.n, gs2.generate_series
FROM
    generate_series(1, 2) WITH ORDINALITY AS gs1
    FULL JOIN information_schema._pg_expandarray(ARRAY[9]) WITH ORDINALITY AS expand
        ON gs1.ordinality = expand.ordinality
    FULL JOIN generate_series(3, 6) WITH ORDINALITY AS gs3
        ON coalesce(gs1.ordinality, expand.ordinality) = gs3.ordinality;

Note the call to coalesce in the last join condition, which ensures that gs3 will align with whichever of gs1 or expand has more rows.

This function creates a HirRelationExpr that follows the structure of the latter query.

with_ordinality can be used to have the output expression contain a single coalesced ordinality column at the end of the entire expression.