Open cockroach-teamcity opened 10 months ago
error: pq: internal error: invalid datum type given: RECORD, expected RECORD
Repro (edit: simplified a bit more):
CREATE FUNCTION func()
RETURNS RECORD
CALLED ON NULL INPUT
LANGUAGE SQL
AS $funcbody$SELECT ((42)::INT8, (43)::INT8)$funcbody$;
SELECT
*
FROM
(
VALUES
(
(('aloha'::TEXT,
(44)::INT8), NULL)
),
(COALESCE(func()::RECORD, NULL))
);
Stack trace:
github.com/cockroachdb/cockroach/pkg/sql/rowenc/encoded_datum.go:195: DatumToEncDatum()
github.com/cockroachdb/cockroach/pkg/sql/plan_node_to_row_source.go:228: Next()
github.com/cockroachdb/cockroach/pkg/sql/colexec/columnarizer.go:239: Next()
github.com/cockroachdb/cockroach/pkg/sql/colflow/stats.go:118: next()
github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:92: CatchVectorizedRuntimeError()
github.com/cockroachdb/cockroach/pkg/sql/colflow/stats.go:126: Next()
github.com/cockroachdb/cockroach/pkg/sql/colflow/flow_coordinator.go:250: nextAdapter()
github.com/cockroachdb/cockroach/pkg/sql/colexecerror/error.go:92: CatchVectorizedRuntimeError()
github.com/cockroachdb/cockroach/pkg/sql/colflow/flow_coordinator.go:254: next()
github.com/cockroachdb/cockroach/pkg/sql/colflow/flow_coordinator.go:286: Run()
github.com/cockroachdb/cockroach/pkg/sql/colflow/vectorized_flow.go:320: Run()
github.com/cockroachdb/cockroach/pkg/sql/distsql_running.go:910: Run()
github.com/cockroachdb/cockroach/pkg/sql/distsql_running.go:1957: PlanAndRun()
github.com/cockroachdb/cockroach/pkg/sql/distsql_running.go:1672: func3()
github.com/cockroachdb/cockroach/pkg/sql/distsql_running.go:1675: PlanAndRunAll()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:2393: execWithDistSQLEngine()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:1931: dispatchToExecutionEngine()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:1108: execStmtInOpenState()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:146: func1()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:3374: execWithProfiling()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:145: execStmt()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:2227: func1()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:2232: execCmd()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:2149: run()
github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:953: ServeConn()
github.com/cockroachdb/cockroach/pkg/sql/pgwire/conn.go:247: processCommands()
github.com/cockroachdb/cockroach/pkg/sql/pgwire/server.go:1001: func3()
src/runtime/asm_amd64.s:1650: goexit()
cc @DrewKimball this is another example of typing issue which seems related to what #119616 tried to solve.
This seems sort of orthogonal to that: the issue is that we don't know the concrete type of a RECORD-returning routine until the call to buildScalar, which doesn't happen until after the initial type-checking. So the coalesce ends up being typed as AnyTuple
, which doesn't conflict with the Tuple(Tuple(Text, Int), Null)
in the first row of the Values operator. In the end, we think the second row of the Values is compatible with the first without needing a cast.
We attempted to fix this by calling buildScalar
before retrieving the resolved type when building a Values operator, but this is defeated by the coalesce, since the AnyTuple type that was originally resolved with doesn't change.
I'm now thinking that we should do something like this: discard a RECORD
cast when the input expression already has a composite type, and return a user-facing error in other cases. We don't have the same concept of a dynamic tuple like postgres does, so pure RECORD
casting just doesn't make sense.
I looked at this again - the cast to RECORD isn't the problem, and isn't necessary for the reproduction. The issue is that we don't resolve the concrete return type of a RECORD-returning function until after it is built in the optbuilder, and we type-check before that. After the UDF is built, its resolved type is correct, but the COALESCE that wraps it still has the incorrect resolved AnyTuple
type.
roachtest.sqlsmith/setup=empty/setting=default failed with artifacts on master @ 4f31fbae529e459c00ef2490e193c0c246e8d0f9:
Parameters:
ROACHTEST_arch=amd64
ROACHTEST_cloud=gce
ROACHTEST_coverageBuild=false
ROACHTEST_cpu=4
ROACHTEST_encrypted=false
ROACHTEST_metamorphicBuild=false
ROACHTEST_ssd=0
Help
See: roachtest README
See: How To Investigate (internal)
See: Grafana
/cc @cockroachdb/sql-queriesThis test on roachdash | Improve this report!
Jira issue: CRDB-34978