cockroachdb / cockroach

CockroachDB — the cloud native, distributed SQL database designed for high availability, effortless scale, and control over data placement.
https://www.cockroachlabs.com
Other
30.11k stars 3.81k forks source link

sql: Internal Error with ill-formed Case return #130813

Open SteveLeungYL opened 1 month ago

SteveLeungYL commented 1 month ago

Describe the problem

The latest version of the CockroachDB v24.2.0 triggers Internal Error when executing the following query:

CREATE TABLE v00 (v01 STRING);
UPSERT INTO v00 ( v01 ) ( SELECT 'random_str' ) RETURNING CASE WHEN NULL THEN IFERROR ( NULL, 0 ) END;

To Reproduce

  1. In operating system Ubuntu 20.04 LTS, download the pre-build CockroachDB binaries (v24.2.0) from link
  2. Run ./cockroach demo, and then paste the PoC query to the cockroach cli environment.
  3. Observe the crash and log the stack information.

Expected behavior The CockroachDB should return error from the statement.

Additional data / screenshots

This is the stack frame from the crash (v24.2.0):

ERROR: internal error: inconsistent Case return types unknown int
SQLSTATE: XX000
DETAIL: stack trace:
github.com/cockroachdb/cockroach/pkg/sql/opt/memo/typing.go:84: InferWhensType()
github.com/cockroachdb/cockroach/pkg/sql/opt/norm/scalar_funcs.go:198: SimplifyWhens()
github.com/cockroachdb/cockroach/bazel-out/k8-opt/bin/pkg/sql/opt/norm/factory.og.go:20569: ConstructCase()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/scalar.go:278: buildScalar()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/project.go:206: buildProjectionList()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/mutation_builder.go:1171: buildReturning()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/insert.go:943: buildUpsert()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/insert.go:335: buildInsert()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:368: func2()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/with.go:116: processWiths()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:367: buildStmt()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:299: buildStmtAtRootWithScope()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:280: buildStmtAtRoot()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:259: Build()
github.com/cockroachdb/cockroach/pkg/sql/plan_opt.go:862: buildExecMemo()
github.com/cockroachdb/cockroach/pkg/sql/plan_opt.go:251: makeOptimizerPlan()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:2291: makeExecPlan()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:1841: dispatchToExecutionEngine()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:1174: execStmtInOpenState()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:146: func1()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:3429: execWithProfiling()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:145: execStmt()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:2316: func1()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:2321: execCmd()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:2238: run()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:963: ServeConn()
github.com/cockroachdb/cockroach/pkg/sql/pgwire/conn.go:253: processCommands()
github.com/cockroachdb/cockroach/pkg/sql/pgwire/server.go:1036: func4()
src/runtime/asm_arm64.s:1222: goexit()

Environment:

cockroach version details:
Build Tag:        v24.2.0
Build Time:       2024/08/08 01:50:30
Distribution:     CCL
Platform:         linux arm64 (aarch64-unknown-linux-gnu)
Go Version:       go1.22.5 X:nocoverageredesign
C Compiler:       gcc 6.5.0
Build Commit ID:  953d09cce5a01cc0053e138516d80a27454f027c
Build Type:       release

Additional context

The bug could be potentially relate to: https://github.com/cockroachdb/cockroach/issues/125115

Jira issue: CRDB-42251

blathers-crl[bot] commented 1 month ago

Hi @SteveLeungYL, please add branch-* labels to identify which branch(es) this C-bug affects.

:owl: Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.

blathers-crl[bot] commented 1 month ago

Hello, I am Blathers. I am here to help you get the issue triaged.

Hoot - a bug! Though bugs are the bane of my existence, rest assured the wretched thing will get the best of care here.

I have CC'd a few people who may be able to assist you:

If we have not gotten back to your issue within a few business days, you can try the following:

:owl: Hoot! I am a Blathers, a bot for CockroachDB. My owner is dev-inf.