Skip to content

sql: type-checking code should not ignore reject flags #108166

@mgartner

Description

@mgartner

There's several TypeCheck methods that replace the semaCtx's reject flags with new ones, causing the original reject flags set by the caller to be ignored. For example:

semaCtx.Properties.Require("CASE", RejectGenerators)

This can cause subtle bugs where expressions that should be rejected in certain contexts are not rejected when wrapped in expressions whose TypeCheck method replaces the flags.

As one example, aggregate functions are not allowed in ORDER BY clauses of DELETE and UPDATE as of #107641. But the rejection fails and we get an internal error if we wrap the aggregate function in a COALESCE:

CREATE TABLE t (a INT);

DELETE FROM t WHERE a > 0 ORDER BY COALESCE(sum(a), 1) LIMIT 1;
-- ERROR: internal error: top-level relational expression cannot have outer columns: (9
-- )
-- SQLSTATE: XX000
-- DETAIL: stack trace:
-- github.com/cockroachdb/cockroach/pkg/sql/opt/xform/optimizer.go:280: Optimize()
-- github.com/cockroachdb/cockroach/pkg/sql/plan_opt.go:592: buildExecMemo()
-- github.com/cockroachdb/cockroach/pkg/sql/plan_opt.go:240: makeOptimizerPlan()
-- github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:1969: makeExecPlan()
-- github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:1485: dispatchToExecu
-- tionEngine()
-- github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:965: execStmtInOpenState()
-- github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:142: func1()
-- github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:2991: execWithProfiling()
-- github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:141: execStmt()
-- github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:2220: func1()
-- github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:2225: execCmd()
-- github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:2142: run()
-- github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:954: ServeConn()
-- github.com/cockroachdb/cockroach/pkg/sql/pgwire/conn.go:244: processCommands()
-- github.com/cockroachdb/cockroach/pkg/sql/pgwire/server.go:996: func3()
-- GOROOT/src/runtime/asm_arm64.s:1172: goexit()

Jira issue: CRDB-30341

Metadata

Metadata

Assignees

Labels

C-bugCode not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.

Type

No type

Projects

Status

Done

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions