Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

sql: panic when adding a default value that is assignment cast to the required type #93398

Closed
SteveLeungYL opened this issue Dec 10, 2022 · 8 comments · Fixed by #95398
Closed
Assignees
Labels
branch-release-22.2 Used to mark GA and release blockers, technical advisories, and bugs for 22.2 C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. C-technical-advisory Caused a technical advisory O-community Originated from the community T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-blathers-triaged blathers was able to find an owner

Comments

@SteveLeungYL
Copy link

SteveLeungYL commented Dec 10, 2022

Describe the problem

The latest version of the CockroachDB (dc93fa8) crashes when executing the following query:

CREATE TABLE t (c1 INT);
INSERT INTO t VALUES (0);
ALTER TABLE t ADD COLUMN c2 DECIMAL DEFAULT pi();
SELECT * FROM t;

To Reproduce

Here is the detail steps to reproduce the bug.

  1. In system Ubuntu 20.04, download the CockroachDB source code from the github source.
  2. Use the latest version of the CockroachDB code (tested version: dc93fa8)
  3. Directly make install in the root repository folder.
  4. Run ./cockroach demo, and then paste the PoC query to the cockroach cli environment.
  5. Observe the crashing and log the crashing information.

Expected behavior
The SELECT statement should be executed correctly and it should return matched rows.

Additional data / screenshots

Here is the crashing stack trace:

*
* ERROR: a SQL panic has occurred while executing the following statement:
* SELECT ta3.c2 FROM v0 AS ta3 WHERE ta3.c2 < 0
*
*
* ERROR: a panic has occurred!
* runtime error: slice bounds out of range [:64] with capacity 7
* (1) attached stack trace
*   -- stack trace:
*   | github.com/cockroachdb/cockroach/pkg/sql.(*Server).ServeConn.func1
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:832
*   | [...repeated from below...]
* Wraps: (2) while executing: SELECT _._ FROM _ AS _ WHERE _._ < _
* Wraps: (3) attached stack trace
*   -- stack trace:
*   | github.com/cockroachdb/cockroach/pkg/sql.(*Server).ServeConn.func1
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:832
*   | runtime.gopanic
*   | 	/usr/local/go/src/runtime/panic.go:884
*   | github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError.func1
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:58
*   | runtime.gopanic
*   | 	/usr/local/go/src/runtime/panic.go:884
*   | github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError.func1
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:58
*   | runtime.gopanic
*   | 	/usr/local/go/src/runtime/panic.go:884
*   | github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError.func1
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:58
*   | runtime.gopanic
*   | 	/usr/local/go/src/runtime/panic.go:884
*   | runtime.goPanicSliceAcap
*   | 	/usr/local/go/src/runtime/panic.go:139
*   | github.com/cockroachdb/cockroach/pkg/util/encoding.DecodeIntoUntaggedDecimalValue
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/util/encoding/encoding.go:2894
*   | github.com/cockroachdb/cockroach/pkg/sql/colencoding.DecodeTableValueToCol
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colencoding/value_encoding.go:72
*   | github.com/cockroachdb/cockroach/pkg/sql/colfetcher.(*cFetcher).processValueBytes
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colfetcher/cfetcher.go:1180
*   | github.com/cockroachdb/cockroach/pkg/sql/colfetcher.(*cFetcher).processValue
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colfetcher/cfetcher.go:981
*   | github.com/cockroachdb/cockroach/pkg/sql/colfetcher.(*cFetcher).NextBatch
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colfetcher/cfetcher.go:760
*   | github.com/cockroachdb/cockroach/pkg/sql/colfetcher.(*ColBatchScan).Next
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colfetcher/colbatch_scan.go:108
*   | github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecutils.(*CancelChecker).Next
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecutils/cancel_checker.go:59
*   | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*batchInfoCollector).next
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/stats.go:116
*   | github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:92
*   | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*batchInfoCollector).Next
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/stats.go:124
*   | github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecsel.(*selLTInt64Int64ConstOp).Next
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecsel/selection_ops.eg.go:24779
*   | github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase.(*simpleProjectOp).Next
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase/simple_project.go:124
*   | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*batchInfoCollector).next
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/stats.go:116
*   | github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:92
*   | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*batchInfoCollector).Next
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/stats.go:124
*   | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*BatchFlowCoordinator).nextAdapter
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/flow_coordinator.go:252
*   | github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:92
*   | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*BatchFlowCoordinator).next
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/flow_coordinator.go:256
*   | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*BatchFlowCoordinator).Run
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/flow_coordinator.go:294
*   | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*vectorizedFlow).Run
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/vectorized_flow.go:309
*   | github.com/cockroachdb/cockroach/pkg/sql.(*DistSQLPlanner).Run
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/distsql_running.go:815
*   | github.com/cockroachdb/cockroach/pkg/sql.(*DistSQLPlanner).PlanAndRun
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/distsql_running.go:1768
*   | github.com/cockroachdb/cockroach/pkg/sql.(*DistSQLPlanner).PlanAndRunAll
*   | 	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/distsql_running.go:1520
* Wraps: (4) runtime error: slice bounds out of range [:64] with capacity 7
* Error types: (1) *withstack.withStack (2) *safedetails.withSafeDetails (3) *withstack.withStack (4) runtime.boundsError
*
panic: runtime error: slice bounds out of range [:64] with capacity 7 [recovered]
	panic: runtime error: slice bounds out of range [:64] with capacity 7 [recovered]
	panic: runtime error: slice bounds out of range [:64] with capacity 7 [recovered]
	panic: runtime error: slice bounds out of range [:64] with capacity 7 [recovered]
	panic: runtime error: slice bounds out of range [:64] with capacity 7

goroutine 4894 [running]:
github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).closeWrapper(0xc004903000, {0x82aa4d0, 0xc005078c40}, {0x516e3a0?, 0xc00546c7f8?})
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:1080 +0x1b4
github.com/cockroachdb/cockroach/pkg/sql.(*Server).ServeConn.func1({0x82aa4d0, 0xc005078c40}, {0xbac2f0e?})
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:832 +0x4a
panic({0x516e3a0, 0xc00546c7f8})
	/usr/local/go/src/runtime/panic.go:884 +0x212
github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError.func1()
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:58 +0x407
panic({0x516e3a0, 0xc00546c7f8})
	/usr/local/go/src/runtime/panic.go:884 +0x212
github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError.func1()
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:58 +0x407
panic({0x516e3a0, 0xc00546c7f8})
	/usr/local/go/src/runtime/panic.go:884 +0x212
github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError.func1()
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:58 +0x407
panic({0x516e3a0, 0xc00546c7f8})
	/usr/local/go/src/runtime/panic.go:884 +0x212
github.com/cockroachdb/cockroach/pkg/util/encoding.DecodeIntoUntaggedDecimalValue(0xc00546c7ea?, {0xc00546c7ec?, 0xa?, 0xc005896da8?})
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/util/encoding/encoding.go:2894 +0xe5
github.com/cockroachdb/cockroach/pkg/sql/colencoding.DecodeTableValueToCol(0x0?, 0xc001609978, 0x7fae8a61f9f8?, 0x0, 0xc00453717b?, 0x1?, 0xc0045fbec0?, {0xc00546c7eb, 0x9, 0x9})
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colencoding/value_encoding.go:72 +0x2c5
github.com/cockroachdb/cockroach/pkg/sql/colfetcher.(*cFetcher).processValueBytes(0xc001609880, {0x14?, 0x14?}, 0xc0034ccc00, {0xc00546c7e5, 0xf, 0xf}, {0x0, 0x0})
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colfetcher/cfetcher.go:1180 +0x3ee
github.com/cockroachdb/cockroach/pkg/sql/colfetcher.(*cFetcher).processValue(0xc001609880, {0x82aa578, 0xc005838f00}, 0x0?)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colfetcher/cfetcher.go:981 +0xea7
github.com/cockroachdb/cockroach/pkg/sql/colfetcher.(*cFetcher).NextBatch(0xc001609880, {0x82aa578, 0xc005838f00})
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colfetcher/cfetcher.go:760 +0x1029
github.com/cockroachdb/cockroach/pkg/sql/colfetcher.(*ColBatchScan).Next(0xc0017197c0)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colfetcher/colbatch_scan.go:108 +0x30
github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecutils.(*CancelChecker).Next(0xc001d56b00)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecutils/cancel_checker.go:59 +0x30
github.com/cockroachdb/cockroach/pkg/sql/colflow.(*batchInfoCollector).next(0xc0035a3770)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/stats.go:116 +0x29
github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError(0xc001d56b40?)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:92 +0x62
github.com/cockroachdb/cockroach/pkg/sql/colflow.(*batchInfoCollector).Next(0xc0035a3770)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/stats.go:124 +0x52
github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecsel.(*selLTInt64Int64ConstOp).Next(0xc005838db0)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecsel/selection_ops.eg.go:24779 +0x32
github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase.(*simpleProjectOp).Next(0xc004b90720)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase/simple_project.go:124 +0x3f
github.com/cockroachdb/cockroach/pkg/sql/colflow.(*batchInfoCollector).next(0xc0035a3950)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/stats.go:116 +0x29
github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError(0xc001d56c00?)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:92 +0x62
github.com/cockroachdb/cockroach/pkg/sql/colflow.(*batchInfoCollector).Next(0xc0035a3950)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/stats.go:124 +0x52
github.com/cockroachdb/cockroach/pkg/sql/colflow.(*BatchFlowCoordinator).nextAdapter(0xc003224460)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/flow_coordinator.go:252 +0x2a
github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError(0xc00430c000?)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:92 +0x62
github.com/cockroachdb/cockroach/pkg/sql/colflow.(*BatchFlowCoordinator).next(...)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/flow_coordinator.go:256
github.com/cockroachdb/cockroach/pkg/sql/colflow.(*BatchFlowCoordinator).Run(0xc003224460, {0x82aa4d0?, 0xc001d56ac0?})
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/flow_coordinator.go:294 +0x2e5
github.com/cockroachdb/cockroach/pkg/sql/colflow.(*vectorizedFlow).Run(0xc00182d2c0, {0x82aa4d0, 0xc001d56ac0})
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/colflow/vectorized_flow.go:309 +0x1d8
github.com/cockroachdb/cockroach/pkg/sql.(*DistSQLPlanner).Run(0xc003828dc0, {0x82aa578, 0xc0058387e0}, 0xc00167ea80, 0xc001dfab00, 0xc001d3e400, 0xc001609c00, 0xc0049038b0, 0x0)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/distsql_running.go:815 +0x984
github.com/cockroachdb/cockroach/pkg/sql.(*DistSQLPlanner).PlanAndRun(0xc005838870?, {0x82aa578, 0xc0058387e0}, 0x0?, 0xc00167ea80, 0x0?, {{0x82acb50?, 0xc004edb5e0?}, 0x0?}, 0xc001609c00)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/distsql_running.go:1768 +0x1f9
github.com/cockroachdb/cockroach/pkg/sql.(*DistSQLPlanner).PlanAndRunAll(0xc00167ea80?, {0x82aa578, 0xc0058387e0}, 0xc004903540?, 0x1?, 0xc004903540, 0xc001609c00, 0xc003b31d40?)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/distsql_running.go:1520 +0x206
github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execWithDistSQLEngine(0xc004903000, {0x82aa578, 0xc0058387e0}, 0xc004903540, 0xc0058387e0?, {0x7fae8a79ac00?, 0xc003dacb10}, 0x0?, 0xc004099ad8)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:1624 +0x4c8
github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).dispatchToExecutionEngine(0xc004903000, {0x82aa578, 0xc005838000}, 0xc004903540, {0x7fae8a79ac00, 0xc003dacb10})
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:1245 +0xc13
github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execStmtInOpenState(0xc004903000, {0x82aa578?, 0xc0051fbf80?}, {{0x82d3800, 0xc00444bb30}, {0x0, 0x0, 0x0}, {0xc0042085a8, 0x2d}, ...}, ...)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:737 +0x2634
github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execStmt.func1({0x82aa578?, 0xc0051fbf80?})
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:130 +0xbc
github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execWithProfiling(0x82aa578?, {0x82aa578?, 0xc0051fbf80?}, {0x82d3800?, 0xc00444bb30?}, 0x0?, 0xaf47700?)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:2513 +0x330
github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execStmt(0xc004903000, {0x82aa578, 0xc0051fbf80}, {{0x82d3800, 0xc00444bb30}, {0x0, 0x0, 0x0}, {0xc0042085a8, 0x2d}, ...}, ...)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:129 +0x4fa
github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execCmd.func1(0xc005899ac0, 0xc004903000, 0xc0058997b8, 0xc005899870, 0xc0058998b0, 0xc005899890, 0xc005899880, 0xc0058998a0)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:1955 +0x30f
github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execCmd(0xc004903000)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:1960 +0x1127
github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).run(0xc004903000, {0x82aa4d0, 0xc005078c40}, 0x4194ca8ec8000000?, 0xc004cc1dd8?, 0xc004e3dc30?)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:1878 +0x20b
github.com/cockroachdb/cockroach/pkg/sql.(*Server).ServeConn(0xc003b2ab40?, {0x82aa4d0?, 0xc005078c40?}, {0xc001c39100?}, 0x3?, 0xc0060f75a0?)
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:834 +0xe6
github.com/cockroachdb/cockroach/pkg/sql/pgwire.(*conn).processCommandsAsync.func1()
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/pgwire/conn.go:728 +0x40d
created by github.com/cockroachdb/cockroach/pkg/sql/pgwire.(*conn).processCommandsAsync
	/home/cockroach/go_projects/src/github.com/cockroachdb/cockroach/pkg/sql/pgwire/conn.go:639 +0x22e

Environment:

  • CockroachDB version: v23.1.0-alpha
  • Server OS: Ubuntu 20.04 LTS
  • Client app: CockroachDB demo command. (./cockroach demo)
  • Detailed CockroachDB version output:
Build Tag:        v23.1.0-alpha.00000000-1118-gdc93fa8ccf-dirty
Build Time:       2022/12/10 21:03:15
Distribution:     CCL
Platform:         linux amd64 (x86_64-linux-gnu)
Go Version:       go1.19
C Compiler:       gcc 9.4.0
Build Commit ID:  dc93fa8ccfc0d08afd81a3222426d5608fd7a529
Build Type:       development

Additional context

The crashing problem may not happen in the real-world application, since it requires the specific setup: SET testing_optimizer_disable_rule_probability = 1.0;, which is a developer option used to control the query optimization. However, a crash is still unexpected from the resulted behavior. Therefore, this problem is reported as a crashing bug.

Jira issue: CRDB-22312

@SteveLeungYL SteveLeungYL added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label Dec 10, 2022
@blathers-crl
Copy link

blathers-crl bot commented Dec 10, 2022

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:

  • @cockroachdb/sql-schema (found keywords: ALTER TABLE)
  • @cockroachdb/sql-queries (found keywords: optimizer,Vectorized,DistSQL,Plan)

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

  • Join our community slack channel and ask on #cockroachdb.
  • Try find someone from here if you know they worked closely on the area and CC them.

🦉 Hoot! I am a Blathers, a bot for CockroachDB. My owner is otan.

@blathers-crl blathers-crl bot added O-community Originated from the community X-blathers-triaged blathers was able to find an owner labels Dec 10, 2022
@blathers-crl blathers-crl bot added the T-sql-queries SQL Queries Team label Dec 10, 2022
@yuzefovich yuzefovich self-assigned this Jan 9, 2023
@yuzefovich
Copy link
Member

yuzefovich commented Jan 9, 2023

@SteveLeungYL thanks for the report! It is actually possible to reproduce this crash without the testing_optimizer_disable_rule_probability testing knob, so I updated the issue description.

CREATE TABLE t (c1 INT);
INSERT INTO t VALUES (0);
ALTER TABLE t ADD COLUMN c2 DECIMAL DEFAULT pi();
SELECT * FROM t;

Note that the problem no longer reproduces

  • if the second DECIMAL column is included when the table is created
  • if adding the column happens before the insert.

On 22.1 and 21.2 the ALTER statement results in an error

ERROR: expected DEFAULT expression to have type decimal, but 'pi()' has type float

If I change the ALTER statement either by adding an explicit cast to decimal (i.e. DEFAULT pi()::DECIMAL) or changing the column type to float, then it works. Disabling the new declarative schema changer doesn't seem to affect the reproduction.

This all makes me think we have a regression with default expressions typing and the backfill process, going to put back into triage.

@yuzefovich yuzefovich removed their assignment Jan 9, 2023
@Xiang-Gu
Copy link
Contributor

Xiang-Gu commented Jan 9, 2023

I am curious why, on older versions,

ALTER TABLE t ADD COLUMN c2 DECIMAL DEFAULT pi();

returns an error? I.e. what's wrong with converting a float to a decimal? This page says it should be alright.

@yuzefovich
Copy link
Member

I think it was e4a01e0 where we stopped returning an error. This diff re-introduces the error back:

diff --git a/pkg/sql/catalog/tabledesc/table.go b/pkg/sql/catalog/tabledesc/table.go
index 518129f91e4..3fb97e1ea2b 100644
--- a/pkg/sql/catalog/tabledesc/table.go
+++ b/pkg/sql/catalog/tabledesc/table.go
@@ -161,7 +161,7 @@ func MakeColumnDefDescs(
                // Verify the default expression type is compatible with the column type
                // and does not contain invalid functions.
                ret.DefaultExpr, err = schemaexpr.SanitizeVarFreeExpr(
-                       ctx, d.DefaultExpr.Expr, resType, "DEFAULT", semaCtx, volatility.Volatile, true, /*allowAssignmentCast*/
+                       ctx, d.DefaultExpr.Expr, resType, "DEFAULT", semaCtx, volatility.Volatile, false, /*allowAssignmentCast*/
                )
                if err != nil {
                        return nil, err

It appears that the assignment cast (from a float to a decimal) is expected to happen here during the backfill process, but it doesn't occur, so we encode the value as a float, and then we attempt to read it as a decimal.

@mgartner
Copy link
Collaborator

@e-mbrown do you think this is related to your work on allowing default column values of different types than the column? I think we should be adding an assignment cast when performing the backfill, but it looks like we aren't.

@blathers-crl blathers-crl bot added the T-sql-schema-deprecated Use T-sql-foundations instead label Jan 10, 2023
@rafiss
Copy link
Collaborator

rafiss commented Jan 10, 2023

#74093 and #81863 are also relevant PRs for DEFAULT expressions, but they also don't change anything about backfills.

@rafiss rafiss changed the title sql: panic when executing query sql: panic when adding a default value that is assignment cast to the required type Jan 13, 2023
@ajwerner
Copy link
Contributor

Well, we'll need to do the same casts when evaluating the expression as part of backfills, no?

@yuzefovich
Copy link
Member

Yes. I tried to find the spot where we'd do it when I was initially debugging the ticket but failed, so I'm hoping someone more familiar with the backfill code do it.

craig bot pushed a commit that referenced this issue Jan 18, 2023
95398: sql/catalog/schemaexpr: fix bug when new column default has different… r=ajwerner a=ajwerner

… type

Since 22.2 we permit default expressions to contain types which are not exactly the same as the column type; it is valid to have an expression which can be cast to the column type in an assignment context. Generally, the optimizer handles inserting the assignment cast into the execution of the relevant mutations. Unfortunately, the cast was not present for backfills.

This PR detects the situation where a cast is needed and insert it directly into the plan of the backfill (or import).

Epic: None

Fixes: #93398

Release note (bug fix): Since 22.2, default expressions can have a type which differs from the type of the column so long as the expression's type can be cast in an assignment context. Unfortunately, this code had a bug when adding new columns. The code in the backfill logic was not sophisticated enough to know to add the cast; when such a default expression was added to a new column it would result in a panic during the backfill. This bug has now been fixed.

95414: sql: fix CLOSE ALL so it doesn't ignore the ALL flag r=jordanlewis a=rafiss

informs #83061

Release note (bug fix): Fixed a bug where CLOSE ALL would not respect the "ALL" flag and would instead attempt to close a cursor with no name.

95426: gossip: don't resolve addresses while holding mutex r=erikgrinaker a=erikgrinaker

This patch removes a DNS resolution call performed while holding the gossip mutex. This can lead to severe process stalls if the DNS lookup is not immediate, since we need to acquire gossip read locks in several performance critical code paths, including Raft processing. However, the DNS lookup was only done when validating a remote forwarding address, which presumably happens fairly rarely. Removing it should not cause any problems, since the address will necessarily be validated later when attempting to connect to it.

Epic: none
Release note (bug fix): Fixed a bug where a DNS lookup was performed during gossip remote forwarding while holding the gossip mutex. This could cause processing stalls if the DNS server was slow to respond.

Co-authored-by: Andrew Werner <[email protected]>
Co-authored-by: Rafi Shamim <[email protected]>
Co-authored-by: Erik Grinaker <[email protected]>
@craig craig bot closed this as completed in 4516a84 Jan 18, 2023
@exalate-issue-sync exalate-issue-sync bot removed the T-sql-queries SQL Queries Team label Jan 18, 2023
ajwerner added a commit to ajwerner/cockroach that referenced this issue Jan 18, 2023
… type

Since 22.2 we permit default expressions to contain types which are not exactly
the same as the column type; it is valid to have an expression which can be
cast to the column type in an assignment context. Generally, the optimizer
handles inserting the assignment cast into the execution of the relevant
mutations. Unfortunately, the cast was not present for backfills.

This PR detects the situation where a cast is needed and insert it directly
into the plan of the backfill (or import).

Epic: None

Fixes: cockroachdb#93398

Release note (bug fix): Since 22.2, default expressions can have a type which
differs from the type of the column so long as the expression's type can be
cast in an assignment context. Unfortunately, this code had a bug when adding
new columns. The code in the backfill logic was not sophisticated enough to
know to add the cast; when such a default expression was added to a new column
it would result in a panic during the backfill. This bug has now been fixed.
mgartner added a commit to mgartner/cockroach that referenced this issue Jan 18, 2023
In cockroachdb#95398, casts were added to default expressions during backfills when
the default expression's type was not equivalent to the target column's
type. This commit fixes two minor bugs with the previous fix:

  1. Casts were only added when types were not equivalent, i.e., when
     they had different families. Assignment casts must be added for
     non-identical types with the same family. For example, a `TEXT`
     default expression must be assignment cast to a `CHAR(1)` column,
     even though they are both in the string family.
  2. Explicit casts were added instead of assignment casts, which have
     different behavior. For example, an assignment cast will error when
     casting `TEXT` to `CHAR(1)` if the string has a length greater than
     one.

Some tests were added to show that default column backfills behave
consistently with Postgres.

Informs cockroachdb#93398

Epic: None

Release note: None
craig bot pushed a commit that referenced this issue Jan 21, 2023
92431: roachtest: introduce new mixed-version testing API r=srosenberg a=renatolabs

**roachtest: introduce `clusterupgrade` package**
The `clusterupgrade` package is introduced in this commit as a means
towards the end goal of reusing upgrade-related functionality in
roachtests. Specifically, as we work towards a new API for
mixed-version testing in roachtests, we want to continue to support
existing upgrade tests (at least for the time being). For that reason,
it's useful to be able to reuse certain low level building blocks that
both APIs rely on. The `clusterupgrade` package includes functions to
query the binary and cluster versions on a node, to replace the binary
running on nodes, upload a certain binary, etc. Most of these
functions were moved from the `versionupgrade.go` file.

**roachtest: introduce new mixed-version testing API**
This introduces a new mixed-version API for roachtests, located under
`roachtestutil/mixedversion`. The goal of this API is to provide a
higher level framework than the existing `versionUpgradeTest` set of
functions.

Currently, all mixed-version roachtests rely on teams having to
specify, step by step, which release version nodes should start from,
how and in what order they upgrade, whether they attempt a rollback,
at what point the feature being tested is invoked, etc. All of this
makes the process of writing mixed-version roachtests time consuming
and takes the focus away from what is actually being tested in
mixed-version. In addition, it's typically not feasible to test
multiple combinations of event orderings in mixed-version tests, and
as a consequence most tests only exercise functionality after all
nodes have upgraded to the new version, potentially hiding bugs that
are only exposed if _some_ nodes are running a newer binary, while
others are running an older release.

The new API has the goal of automatically exploring different
orderings in each run. In the most common scenario, teams will only
need to specify what they want tested in a mixed-version setting, and
the framework will decide when that feature will be invoked: in some
runs, it will happen when all nodes are migrated to the new
binary (most common scenario in existing tests); in other runs, it
will happen when only some nodes have upgraded; sometimes it will run
while upgrade migrations are running in the background; etc. It's also
possible to specify multiple features to be tested in mixed-version
state, and the framework will randomly schedule them during the test,
potentially running them concurrently. The randomness is controlled by
a seed that is logged when the test is executed. By reusing the same
seed, the same mixed-version test is generated which helps with
reproducibility (but, of course, does not make the test
deterministic).

Mixed-version tests that use the `mixedversion` package can be broken
down into two stages: _planning_ and _execution_. During planning, all
mixed-version hooks will be scheduled and a test plan is
generated. All randomness is introduced during test plan
generation. The execution stage just takes care of running the plan,
and does not introduce any other randomness. When the mixed-version
test is run, the test plan is printed in a tree-like format, providing
a high level overview of what the test is doing. In addition, each
step of the test is assigned a numeric ID, which can be used to
reference the output of that step during execution. Each step logs to
a different file, and the artifacts containing the output of each
step will live in `mixed-version-test/{ID}.log`.

The new framework builds on top of the recently introduced
`clusterupgrade` package, which provides basic, low-level fiunctions
that are  also used by the existing mixed-version tests.

**roachtest: rewrite acceptance/version-upgrade to use new API**
This commit migrates the `acceptace/version-upgrade` test to use the
new API in the `mixedversion` package. Two features were previously
being tested in mixed-version state in this test: backups, and basic
functionality that can be tested by running a SQL statement.

This change simplifies the code as it is no longer required to set up
the nodes, and the concept of the `versionFeatureStep` was removed as
it is not necessary and was only being used by this test.

Epic: CRDB-19321
Release note: None

94431: tsearch: support weight matches; stripped vectors r=jordanlewis a=jordanlewis

Previously, the tsearch package ignored weights when computing matches. It also computed incorrect results against stripped vectors (vectors without position information on all or some of their lexemes).

Weight matches are enabled when a tsquery has an explicit weight set on a term, a b c or d. If that's the case, the matching term in the tsvector must have a matching weight to actually be a match.

Stripped vectors are handled differently as well. If a vector is stripped, it must return false for any queries that relate to expected positions. This means that the followed by operator must keep track of whether it saw any stripped matches in either of its arms, and return false if it does.

This patch now passes the pg_regress test suite for tsvector, and test cases were added to ensure this fact.

Epic: CRDB-22357
Release note: None (no release with these issues)

95459: sql/catalog/schemaexpr: fix default expression assignment casts r=mgartner a=mgartner

In #95398, casts were added to default expressions during backfills when
the default expression's type was not equivalent to the target column's
type. This commit fixes two minor bugs with the previous fix:

  1. Casts were only added when types were not equivalent, i.e., when
     they had different families. Assignment casts must be added for
     non-identical types with the same family. For example, a `TEXT`
     default expression must be assignment cast to a `CHAR(1)` column,
     even though they are both in the string family.
  2. Explicit casts were added instead of assignment casts, which have
     different behavior. For example, an assignment cast will error when
     casting `TEXT` to `CHAR(1)` if the string has a length greater than
     one.

Some tests were added to show that default column backfills behave
consistently with Postgres.

Informs #93398

Epic: None

Release note: None


Co-authored-by: Renato Costa <[email protected]>
Co-authored-by: Jordan Lewis <[email protected]>
Co-authored-by: Marcus Gartner <[email protected]>
mgartner added a commit to mgartner/cockroach that referenced this issue Jan 22, 2023
In cockroachdb#95398, casts were added to default expressions during backfills when
the default expression's type was not equivalent to the target column's
type. This commit fixes two minor bugs with the previous fix:

  1. Casts were only added when types were not equivalent, i.e., when
     they had different families. Assignment casts must be added for
     non-identical types with the same family. For example, a `TEXT`
     default expression must be assignment cast to a `CHAR(1)` column,
     even though they are both in the string family.
  2. Explicit casts were added instead of assignment casts, which have
     different behavior. For example, an assignment cast will error when
     casting `TEXT` to `CHAR(1)` if the string has a length greater than
     one.

Some tests were added to show that default column backfills behave
consistently with Postgres.

Informs cockroachdb#93398

Epic: None

Release note: None
@exalate-issue-sync exalate-issue-sync bot added T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) and removed T-sql-schema-deprecated Use T-sql-foundations instead labels May 10, 2023
@mgartner mgartner moved this to Done in SQL Queries Jul 24, 2023
@rytaft rytaft added C-technical-advisory Caused a technical advisory branch-release-22.2 Used to mark GA and release blockers, technical advisories, and bugs for 22.2 labels Dec 6, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
branch-release-22.2 Used to mark GA and release blockers, technical advisories, and bugs for 22.2 C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. C-technical-advisory Caused a technical advisory O-community Originated from the community T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions) X-blathers-triaged blathers was able to find an owner
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

7 participants