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

roachtest/sqlsmith: projected column has inconsistent type #91817

Closed
cockroach-teamcity opened this issue Nov 14, 2022 · 10 comments · Fixed by #105736
Closed

roachtest/sqlsmith: projected column has inconsistent type #91817

cockroach-teamcity opened this issue Nov 14, 2022 · 10 comments · Fixed by #105736
Assignees
Labels
A-sql-optimizer SQL logical planning and optimizations. branch-release-22.1 Used to mark release blockers and technical advisories for 22.1 C-test-failure Broken test (automatically or manually discovered). O-roachtest O-robot Originated from a bot. S-3 Medium-low impact: incurs increased costs for some users (incl lower avail, recoverable bad data) T-sql-queries SQL Queries Team
Projects
Milestone

Comments

@cockroach-teamcity
Copy link
Member

cockroach-teamcity commented Nov 14, 2022

roachtest.sqlsmith/setup=rand-tables/setting=no-ddl failed with artifacts on release-22.1 @ 4ca19806493494cfbe881cb86685079fe15a9f0d:

The test failed on branch=release-22.1, cloud=gce:
test artifacts and logs in: /artifacts/sqlsmith/setup=rand-tables/setting=no-ddl/run_1
	sqlsmith.go:265,sqlsmith.go:324,test_runner.go:883: error: pq: internal error: unexpected error from the vectorized engine: interface conversion: coldata.Column is coldata.Int64s, not coldata.Int16s
		stmt:
		SELECT
			st_extent('BOX(-0.7749563079365274 0.41180487013507916,-0.19096830653024166 0.8891165377985752)':::BOX2D::GEOMETRY)::BOX2D
				AS col_193163,
			28728:::INT8 AS col_193164,
			tab_104592.col3_2 AS col_193165,
			var_pop(tab_104592.col3_3::INT8) OVER (PARTITION BY tab_104592.col3_3 ORDER BY tab_104592.col3_1, tab_104592.tableoid DESC)::DECIMAL
				AS col_193166,
			1529871493:::OID AS col_193167,
			tab_104592.col3_2 AS col_193168
		FROM
			defaultdb.public.table3@table3_pkey AS tab_104592
		GROUP BY
			tab_104592.col3_2,
			tab_104592.col3_1,
			tab_104592.col3_0,
			tab_104592.crdb_internal_mvcc_timestamp,
			tab_104592.col3_3,
			tab_104592.tableoid
		HAVING
			every(true::BOOL)::BOOL
		ORDER BY
			tab_104592.tableoid, tab_104592.crdb_internal_mvcc_timestamp DESC;
Help

See: roachtest README

See: How To Investigate (internal)

/cc @cockroachdb/sql-queries

This test on roachdash | Improve this report!

Jira issue: CRDB-21441

@cockroach-teamcity cockroach-teamcity added branch-release-22.1 Used to mark release blockers and technical advisories for 22.1 C-test-failure Broken test (automatically or manually discovered). O-roachtest O-robot Originated from a bot. labels Nov 14, 2022
@cockroach-teamcity cockroach-teamcity added this to the 22.1 milestone Nov 14, 2022
@cockroach-teamcity cockroach-teamcity added this to Triage in SQL Queries Nov 14, 2022
@blathers-crl blathers-crl bot added the T-sql-queries SQL Queries Team label Nov 14, 2022
@DrewKimball
Copy link
Collaborator

Stack trace:

E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680  encountered internal error:
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +unexpected error from the vectorized engine: interface conversion: coldata.Column is coldata.Int64s, not coldata.Int16s
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +(1)
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +Wraps: (2) assertion failure
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +Wraps: (3) attached stack trace
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  -- stack trace:
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError.func1
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:89
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | runtime.gopanic
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	GOROOT/src/runtime/panic.go:1038
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | runtime.panicdottypeE
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	GOROOT/src/runtime/iface.go:261
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | runtime.panicdottypeI
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	GOROOT/src/runtime/iface.go:271
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/col/coldata.(*memColumn).Int16
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/col/coldata/vec.go:225
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase.(*castInt2IntOp).Next.func1
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/bazel-out/k8-opt/bin/pkg/sql/colexec/colexecbase/cast.eg.go:2705
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colmem.(*Allocator).PerformOperation
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colmem/allocator.go:432
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase.(*castInt2IntOp).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/bazel-out/k8-opt/bin/pkg/sql/colexec/colexecbase/cast.eg.go:2703
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase.(*simpleProjectOp).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase/simple_project.go:125
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecutils.(*vectorTypeEnforcer).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecutils/operator.go:139
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase.(*castIntInt2Op).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/bazel-out/k8-opt/bin/pkg/sql/colexec/colexecbase/cast.eg.go:3831
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase.(*simpleProjectOp).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase/simple_project.go:125
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*panicInjector).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colflow/panic_injector.go:70
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec.(*Materializer).next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexec/materializer.go:266
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec.(*Materializer).nextAdapter
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexec/materializer.go:291
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:92
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec.(*Materializer).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexec/materializer.go:297
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/rowexec.(*windower).accumulateRows
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/rowexec/windower.go:269
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/rowexec.(*windower).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/rowexec/windower.go:221
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec.(*Columnarizer).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexec/columnarizer.go:210
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*panicInjector).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colflow/panic_injector.go:70
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec.(*allSpooler).spool
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexec/sort.go:136
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec.(*sortOp).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexec/sort.go:280
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec.(*diskSpillerBase).Next.func1
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexec/disk_spiller.go:198
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:92
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec.(*diskSpillerBase).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexec/disk_spiller.go:196
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase.(*simpleProjectOp).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexec/colexecbase/simple_project.go:125
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*panicInjector).Next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colflow/panic_injector.go:70
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*BatchFlowCoordinator).nextAdapter
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colflow/flow_coordinator.go:240
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colexecerror.CatchVectorizedRuntimeError
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:92
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*BatchFlowCoordinator).next
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 	github.com/cockroachdb/cockroach/pkg/sql/colflow/flow_coordinator.go:244
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | github.com/cockroachdb/cockroach/pkg/sql/colflow.(*BatchFlowCoordinator).Run
E221114 06:35:13.187676 2504 sql/sqltelemetry/report.go:57 ⋮ [n1,client=35.237.216.176:40716,user=root] 1680 +  | 

@DrewKimball
Copy link
Collaborator

Snippet from the EXPLAIN (OPT, TYPES) output:

        │    │    │    │    ├── project
        │    │    │    │    │    ├── columns: column9:9(geometry!null) column12:12(bool!null) col3_3:4(int2!null) col3_0:1(int2!null) col3_1:2(box2d) col3_2:3(int2!null) crdb_internal_mvcc_timestamp:7(decimal) tableoid:8(oid)
        │    │    │    │    │    ├── scan table3
        │    │    │    │    │    └── projections
        │    │    │    │    │         ├── const: '010300000001000000050000004052CD2B71CCE8BF881364D0025BDA3F4052CD2B71CCE8BF22AF8586A473EC3F98588F43A671C8BF22AF8586A473EC3F98588F43A671C8BF881364D0025BDA3F4052CD2B71CCE8BF881364D0025BDA3F' [as=column9:9, type=geometry]
        │    │    │    │    │         ├── true [as=column12:12, type=bool]
        │    │    │    │    │         └── plus [as=col3_3:4, type=int, outer=(1), immutable]
        │    │    │    │    │              ├── variable: col3_0:1 [type=int2]
        │    │    │    │    │              └── const: 28376 [type=int]

Notice how in the columns list for the project col3_3:4 has type int2, whereas in the projection where it is synthesized it has type int.

@DrewKimball DrewKimball changed the title roachtest: sqlsmith/setup=rand-tables/setting=no-ddl failed roachtest/sqlsmith: projected column has inconsistent type Nov 15, 2022
@michae2 michae2 moved this from Triage to Up Next in SQL Queries Nov 15, 2022
@michae2 michae2 added the A-sql-optimizer SQL logical planning and optimizations. label Nov 15, 2022
@michae2
Copy link
Collaborator

michae2 commented Nov 15, 2022

[triage] we think this is in the optimizer

@mgartner
Copy link
Collaborator

@DrewKimball were you able to reduce a reproduction for this?

@DrewKimball
Copy link
Collaborator

Reproduces with this:

CREATE TABLE table3 (col3_0 INT2 NOT NULL, col3_1 BOX2D NULL, col3_2 INT2 NOT NULL, col3_3 INT2 AS (col3_0 + 28376:::INT8) VIRTUAL, PRIMARY KEY (col3_0 ASC), UNIQUE (col3_3, (col3_2 + 0:::INT8), col3_0 ASC, (col3_0 + (-19133):::INT8) DESC) STORING (col3_1) WHERE ((table3.col3_3 <= 1:::INT8) OR (table3.col3_2 >= (-128):::INT8)) OR (table3.col3_0 = 0:::INT8), UNIQUE (col3_0 DESC, col3_3 DESC) WHERE (table3.col3_0 <= (-1):::INT8) AND (table3.col3_3 >= (-32768):::INT8), INDEX (col3_0));

INSERT INTO
  defaultdb.public.table3 AS tab_84 (col3_0, col3_2)
SELECT
  (-20532):::INT8 AS col_178, (-8715):::INT8 AS col_179

SELECT
  st_extent('BOX(-0.7749563079365274 0.41180487013507916,-0.19096830653024166 0.8891165377985752)':::BOX2D::GEOMETRY)::BOX2D AS col_193163,
  28728:::INT8 AS col_193164,
  tab_104592.col3_2 AS col_193165,
  var_pop(tab_104592.col3_3::INT8) OVER (PARTITION BY tab_104592.col3_3 ORDER BY tab_104592.col3_1, tab_104592.tableoid DESC)::DECIMAL AS col_193166,
  1529871493:::OID AS col_193167,
  tab_104592.col3_2 AS col_193168
FROM
  defaultdb.public.table3@table3_pkey AS tab_104592
GROUP BY
  tab_104592.col3_2,
  tab_104592.col3_1,
  tab_104592.col3_0,
  tab_104592.crdb_internal_mvcc_timestamp,
  tab_104592.col3_3,
  tab_104592.tableoid
HAVING
  every(true::BOOL)::BOOL
ORDER BY
  tab_104592.tableoid, tab_104592.crdb_internal_mvcc_timestamp DESC;

@DrewKimball
Copy link
Collaborator

DrewKimball commented Nov 29, 2022

Reduced:

CREATE TABLE table3 (col3_0 INT2 NOT NULL, col3_1 BOX2D NULL, col3_2 INT2 NOT NULL, col3_3 INT2 AS (col3_0 + 28376:::INT8) VIRTUAL, PRIMARY KEY (col3_0 ASC), UNIQUE (col3_3, (col3_2 + 0:::INT8), col3_0 ASC, (col3_0 + (-19133):::INT8) DESC) STORING (col3_1) WHERE ((table3.col3_3 <= 1:::INT8) OR (table3.col3_2 >= (-128):::INT8)) OR (table3.col3_0 = 0:::INT8), UNIQUE (col3_0 DESC, col3_3 DESC) WHERE (table3.col3_0 <= (-1):::INT8) AND (table3.col3_3 >= (-32768):::INT8), INDEX (col3_0));

INSERT INTO
  defaultdb.public.table3 AS tab_84 (col3_0, col3_2)
SELECT
  (-20532):::INT8 AS col_178, (-8715):::INT8 AS col_179;

SELECT
  var_pop(tab_104592.col3_3::INT8) OVER ()
FROM
  defaultdb.public.table3 AS tab_104592
GROUP BY
  tab_104592.col3_0,
  tab_104592.col3_3
HAVING
  every(true::BOOL)::BOOL;

@mgartner mgartner moved this from Up Next to Bugs to Fix in SQL Queries Jan 19, 2023
@mgartner mgartner added the S-3 Medium-low impact: incurs increased costs for some users (incl lower avail, recoverable bad data) label Jun 8, 2023
@mgartner
Copy link
Collaborator

I'm going to look into this one.

@mgartner mgartner self-assigned this Jun 27, 2023
@mgartner
Copy link
Collaborator

Reduced further:

CREATE TABLE t (
  k INT2 PRIMARY KEY,
  v INT2 GENERATED ALWAYS AS (k + 1) VIRTUAL
);

INSERT INTO t VALUES (0);

SELECT var_pop(v::INT8) OVER ()
FROM t
GROUP BY k, v
HAVING every(true);

@mgartner
Copy link
Collaborator

I believe the fix is to add an assigment cast when projecting the virtual column expression to ensure that the type of the projected expression matches the column type of the virtual column. That's a fairly easy change to make.

However, I'm worried about the fallout of such a change without also fixing #81698. I believe that would make it possible to add a virtual column to a table without error, but then all subsequent SELECTs could fail if the existing data in the table cannot be assignment cast to the column type. For example:

CREATE TABLE t2 (s STRING);

INSERT INTO t2 VALUES ('foo');

-- This should error, but does not due to #81698.
ALTER TABLE t2 ADD COLUMN v CHAR AS (s) VIRTUAL;

-- This will error with "value too long for type CHAR".
SELECT * FROM t2;

So I think we'll need to also fix #81698 to avoid collateral damage.

@mgartner
Copy link
Collaborator

Here's an example showing the root problem in this issue:

CREATE TABLE t (
  s STRING,
  comp_s "char" AS (s) STORED,
  comp_v "char" AS (s) VIRTUAL
);

INSERT INTO t VALUES ('foo');

SELECT * FROM t;
--    s  | comp_s | comp_v
-- ------+--------+---------
--   foo | f      | foo
-- (1 row)

The values of both computed columns should be the same, but they are different because the STORED column value undergoes an assignment cast when it is written, while the VIRTUAL column value doesn't undergo the same assignment cast when it is read.

mgartner added a commit to mgartner/cockroach that referenced this issue Jun 28, 2023
optbuilder wraps an assignment cast around a virtual computed column
expression when writing to the table if the expression does not have the
same type as the virtual column. This matches the behavior of all writes
to columns of expressions that don't match the target column type.

However, the same cast was not applied to the projection of the virtual
column expression when reading from the table. This cast is necessary in
order to produce the correct the value of the column - the projection
must produce the same value that would have been written to the table if
the column was a stored computed column. This commit corrects optbuilder
so that the cast is correctly added to the projection.

Note that this commit adds a standard cast, not an assignment cast, as a
temporary workaround until cockroachdb#81698 is addressed. This is because an
assignment cast can fail in cases when a regular cast will not. Because
we don't validate that all existing rows can successfully produce a new
virtual column expression during a backfill, adding an assignment cast
to the projection of the virtual column could cause future reads to
error. Once cockroachdb#81698 is addressed, we can change these casts to assignment
casts so that they directly match the values that would be written to
the same column if it were stored.

Fixes cockroachdb#91817
Informs cockroachdb#81698

Release note (bug fix): A bug has been fixed that could produce incorrect
values for virtual computed columns in rare cases. The bug only
presented when the virtual column expression's type did not match the
type of the virtual column.
mgartner added a commit to mgartner/cockroach that referenced this issue Jun 29, 2023
optbuilder wraps an assignment cast around a virtual computed column
expression when writing to the table if the expression does not have the
same type as the virtual column. This matches the behavior of all writes
to columns of expressions that don't match the target column type.

However, the same cast was not applied to the projection of the virtual
column expression when reading from the table. This cast is necessary in
order to produce the correct the value of the column - the projection
must produce the same value that would have been written to the table if
the column was a stored computed column. This commit corrects optbuilder
so that the cast is correctly added to the projection.

Note that this commit adds a standard cast, not an assignment cast, as a
temporary workaround until cockroachdb#81698 is addressed. This is because an
assignment cast can fail in cases when a regular cast will not. Because
we don't validate that all existing rows can successfully produce a new
virtual column expression during a backfill, adding an assignment cast
to the projection of the virtual column could cause future reads to
error. Once cockroachdb#81698 is addressed, we can change these casts to assignment
casts so that they directly match the values that would be written to
the same column if it were stored.

Fixes cockroachdb#91817
Informs cockroachdb#81698

Release note (bug fix): A bug has been fixed that could produce incorrect
values for virtual computed columns in rare cases. The bug only
presented when the virtual column expression's type did not match the
type of the virtual column.
craig bot pushed a commit that referenced this issue Jul 5, 2023
105736: opt: wrap virtual computed column projections in a cast r=mgartner a=mgartner

#### sql/logictest: remove assignment cast TODO

This commit removes a TODO that was partially addressed by #82022.

Informs #73743

Release note: None

#### sql/types: fix T.Identical

This commit fixes a bug in `types.T.Identical` which caused it to return
false for collated string types with a different string-representation
of locales that represents the same locale logically. For example,
collated string types with locales `en_US` and `en_us` would not be
identical, even though these are both valid representations of the same
locale.

There is no release note because this has not caused any known bugs.

Release note: None

#### opt: wrap virtual computed column projections in a cast

optbuilder wraps an assignment cast around a virtual computed column
expression when writing to the table if the expression does not have the
same type as the virtual column. This matches the behavior of all writes
to columns of expressions that don't match the target column type.

However, the same cast was not applied to the projection of the virtual
column expression when reading from the table. This cast is necessary in
order to produce the correct value of the column - the projection must
produce the same value that would have been written to the table if the
column was a stored computed column. This commit corrects optbuilder so
that the cast is correctly added to the projection.

Note that this commit adds a standard cast, not an assignment cast, as a
temporary workaround until #81698 is addressed. This is because an
assignment cast can fail in cases when a regular cast will not. Because
we don't validate that all existing rows can successfully produce a new
virtual column expression during a backfill, adding an assignment cast
to the projection of the virtual column could cause future reads to
error. Once #81698 is addressed, we can change these casts to assignment
casts so that they directly match the values that would be written to
the same column if it were stored.

Fixes #91817
Informs #81698

Release note (bug fix): A bug has been fixed that could produce incorrect
values for virtual computed columns in rare cases. The bug only
presented when the virtual column expression's type did not match the
type of the virtual column.


105932: opt/exec: add passthrough cols to DELETE USING result cols in explain r=yuzefovich,DrewKimball a=michae2

Now that we support `DELETE USING`, delete nodes can have passthrough columns. Add these to the result columns used by `EXPLAIN (TYPES)`.

Fixes: #105803

Release note (sql change): Fix an internal error when using `EXPLAIN (TYPES)` on a `DELETE FROM ... USING ... RETURNING` statement.

Co-authored-by: Marcus Gartner <marcus@cockroachlabs.com>
Co-authored-by: Michael Erickson <michae2@cockroachlabs.com>
@craig craig bot closed this as completed in 437d4ef Jul 5, 2023
SQL Queries automation moved this from Bugs to Fix to Done Jul 5, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
A-sql-optimizer SQL logical planning and optimizations. branch-release-22.1 Used to mark release blockers and technical advisories for 22.1 C-test-failure Broken test (automatically or manually discovered). O-roachtest O-robot Originated from a bot. S-3 Medium-low impact: incurs increased costs for some users (incl lower avail, recoverable bad data) T-sql-queries SQL Queries Team
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants