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.14k stars 3.81k forks source link

sql: return type mismatch error in plpgsql function returning composite type #114854

Open rharding6373 opened 12 months ago

rharding6373 commented 12 months ago

Repro:

create type compostype as (x int, y varchar);
create or replace function compos() returns compostype as $$            
  declare x int := 42;                                                    
  begin                                                                   
    return x;                                                             
  end;                                                                    
  $$ language plpgsql;                                                    
select * from compos();

Stacktrace:

ERROR: internal error: runtime error: index out of range [0] with length 0
SQLSTATE: XX000
DETAIL: stack trace:
github.com/cockroachdb/cockroach/pkg/util/errorutil/catch.go:29: ShouldCatch()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:211: func1()
GOROOT/src/runtime/panic.go:914: gopanic()
GOROOT/src/runtime/panic.go:114: goPanicIndex()
github.com/cockroachdb/cockroach/pkg/sql/sem/tree/expr.go:1703: NewTypedColumnAccessExpr()
github.com/cockroachdb/cockroach/pkg/sql/opt/norm/fold_constants_funcs.go:610: FoldColumnAccess()
github.com/cockroachdb/cockroach/bazel-out/darwin-fastbuild/bin/pkg/sql/opt/norm/factory.og.go:20645: ConstructColumnAccess()
github.com/cockroachdb/cockroach/bazel-out/darwin-fastbuild/bin/pkg/sql/opt/norm/factory.og.go:23690: Replace()
github.com/cockroachdb/cockroach/pkg/sql/opt/norm/inline_funcs.go:78: func1()
github.com/cockroachdb/cockroach/pkg/sql/opt/norm/inline_funcs.go:80: inlineConstants()
github.com/cockroachdb/cockroach/pkg/sql/opt/norm/inline_funcs.go:40: InlineProjectionConstants()
github.com/cockroachdb/cockroach/bazel-out/darwin-fastbuild/bin/pkg/sql/opt/norm/factory.og.go:1598: ConstructProject()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/project.go:62: constructProject()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/routine.go:379: finishBuildLastStmt()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/routine.go:304: buildRoutine()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/routine.go:66: buildUDF()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/scalar.go:551: buildFunction()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/scalar.go:332: buildScalar()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/srfs.go:152: buildZip()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:184: buildDataSource()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:94: buildDataSource()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:1435: buildFromWithLateral()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:1359: buildFromTables()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:1289: buildFrom()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:1205: buildSelectClause()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:1141: buildSelectStmtWithoutParens()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:1099: func1()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/with.go:116: processWiths()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:1098: buildSelect()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:341: buildStmt()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:285: buildStmtAtRootWithScope()
github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:266: buildStmtAtRoot()

HINT: You have encountered an unexpected error.

Please check the public issue tracker to check whether this problem is
already tracked. If you cannot find it there, please report the error
with details by creating a new issue.

If you would rather not post publicly, please contact us directly
using the support form.

We appreciate your feedback.

postgres returns the following error:

ERROR:  cannot return non-composite value from function returning composite type 
CONTEXT:  PL/pgSQL function compos() line 4 at RETURN      

Found in pg_regress plpgsql

Jira issue: CRDB-33718

DrewKimball commented 11 months ago

Should be fixed by the type-coercion fix.

rharding6373 commented 11 months ago

Confirmed that we no longer have an internal error, however our error and error code differs from postgres:

CRDB:

ERROR: could not parse "42" as type tuple{int AS x, varchar AS y}: record must be enclosed in ( and )
SQLSTATE: 22P02

postgres:

ERROR:  42804: cannot return non-composite value from function returning composite type

I think that postgres's Data Type Mismatch is much more helpful than Invalid Text Representation. But since they both error I think this is a low priority and probably easy fix.