Skip to content

Commit

Permalink
Merge pull request #119712 from mgartner/backport23.1-119538
Browse files Browse the repository at this point in the history
release-23.1: opt/execbuilder: fix EXPORT when input expression has hidden columns
  • Loading branch information
mgartner committed Feb 27, 2024
2 parents 3febcea + 21377a0 commit 5e662ff
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 4 deletions.
13 changes: 13 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/export
Original file line number Diff line number Diff line change
Expand Up @@ -8,3 +8,16 @@ WITH cte AS (EXPORT INTO CSV 'nodelocal://1/export1/' FROM SELECT * FROM t) SELE

statement ok
WITH cte AS (EXPORT INTO PARQUET 'nodelocal://1/export1/' FROM SELECT * FROM t) SELECT filename FROM cte;

# Regression test for #115290. Correctly handle the case where the Export's
# input expression has NOT NULL columns that are not part of the presentation of
# the expression.
statement ok
CREATE TABLE t115290 (
id INT PRIMARY KEY,
a INT NOT NULL,
b INT
);

statement ok
EXPORT INTO PARQUET 'nodelocal://1/export1/' FROM SELECT b FROM t115290 ORDER BY a;
18 changes: 14 additions & 4 deletions pkg/sql/opt/exec/execbuilder/statement.go
Original file line number Diff line number Diff line change
Expand Up @@ -396,17 +396,27 @@ func (b *Builder) buildExport(export *memo.ExportExpr) (execPlan, error) {
return execPlan{}, err
}
}
notNullColsSet, err := input.getNodeColumnOrdinalSet(export.Input.Relational().NotNullCols)
if err != nil {
return execPlan{}, err

var notNullOrds exec.NodeColumnOrdinalSet
notNullCols := export.Input.Relational().NotNullCols
for col, ok := notNullCols.Next(0); ok; col, ok = notNullCols.Next(col + 1) {
// Ignore NOT NULL columns that are not part of the input execPlan's
// output columns. This can happen when applyPresentation projects-away
// some output columns of the input expression. For example, a Sort
// expression must output a column it orders by, but that column must be
// projected-away after the sort if the presentation does not require
// the column.
if ord, ok := input.outputCols.Get(int(col)); ok {
notNullOrds.Add(ord)
}
}

node, err := b.factory.ConstructExport(
input.root,
fileName,
export.FileFormat,
opts,
notNullColsSet,
notNullOrds,
)
if err != nil {
return execPlan{}, err
Expand Down

0 comments on commit 5e662ff

Please sign in to comment.