Skip to content

Commit 0d17d84

Browse files
craig[bot]massimo-ua
andcommitted
Merge #142068
142068: sql: fix CDC expression planning for tables without columns r=asg0451 a=massimo-ua Fixes an issue in the CDC expression planning that was causing an error when planning for tables without columns. The error occurred because the planner returned an assertion failure when it couldn't determine any result columns. The issue happens when using a CDC expression with a table that has no user-defined columns (e.g. created with CREATE TABLE empty()) and running a query like SELECT * FROM empty. Since there are no columns to select, the presentation list is empty, which triggered the assertion failure. This fix removes the check that returns an error when the presentation is empty, allowing CDC expressions to work with tables that have no user-defined columns. The planner now correctly handles this edge case and returns a valid plan with an empty presentation. Fixes #114058. Fixes https://cockroachlabs.atlassian.net/browse/CRDB-33318. Release note (bug fix): Fixed an issue where changefeed expressions on tables without columns would fail with an internal error: "unable to determine result columns". Co-authored-by: maksym.hryshkov <[email protected]>
2 parents 2e34a5d + f81e534 commit 0d17d84

File tree

2 files changed

+34
-1
lines changed

2 files changed

+34
-1
lines changed

pkg/ccl/changefeedccl/changefeed_test.go

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11072,3 +11072,28 @@ func assertEqualTSNSHLCWalltime(t *testing.T, tsns int64, tshlc string) {
1107211072
tsHLCWallTimeNano := parseTimeToHLC(t, tshlc).WallTime
1107311073
require.EqualValues(t, tsns, tsHLCWallTimeNano)
1107411074
}
11075+
11076+
// TestChangefeedAsSelectForEmptyTable verifies that a changefeed
11077+
// yields a proper user error on an empty table and in the same
11078+
// allows hidden columns to be selected.
11079+
func TestChangefeedAsSelectForEmptyTable(t *testing.T) {
11080+
defer leaktest.AfterTest(t)()
11081+
defer log.Scope(t).Close(t)
11082+
11083+
testFn := func(t *testing.T, s TestServer, f cdctest.TestFeedFactory) {
11084+
sqlDB := sqlutils.MakeSQLRunner(s.DB)
11085+
sqlDB.Exec(t, `CREATE TABLE empty()`)
11086+
sqlDB.Exec(t, `INSERT INTO empty DEFAULT VALUES`)
11087+
// Should fail when no columns are selected.
11088+
// Use expectErrCreatingFeed which handles sinkless feeds correctly by
11089+
// attempting to read from the feed if no error occurs at creation time
11090+
expectErrCreatingFeed(t, f, `CREATE CHANGEFEED AS SELECT * FROM empty`, `SELECT yields no columns`)
11091+
11092+
// Should succeed when a rowid column is explicitly selected.
11093+
feed, err := f.Feed(`CREATE CHANGEFEED AS SELECT rowid FROM empty`)
11094+
require.NoError(t, err)
11095+
defer closeFeed(t, feed)
11096+
}
11097+
11098+
cdcTest(t, testFn)
11099+
}

pkg/sql/distsql_plan_changefeed.go

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,8 +127,16 @@ func PlanCDCExpression(
127127

128128
// The top node contains the list of columns to return.
129129
presentation := planColumns(p.curPlan.main.planNode)
130+
131+
// SELECT statement provided by the user yields zero columns.
132+
// If some user actually wants to have a changefeed for the
133+
// hidden rowid column of a zero-column table, then they can be explicit
134+
// about it: CREATE CHANGEFEED ... SELECT rowid ....
130135
if len(presentation) == 0 {
131-
return CDCExpressionPlan{}, errors.AssertionFailedf("unable to determine result columns")
136+
return CDCExpressionPlan{}, errors.WithHintf(
137+
errors.New("SELECT yields no columns"),
138+
"Specify at least one column in your SELECT statement or use SELECT rowid if you need a changefeed for the hidden rowid column of a zero-column table.",
139+
)
132140
}
133141

134142
// Walk the plan, perform sanity checks and extract information we need.

0 commit comments

Comments
 (0)