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

opt: v22.2.0-alpha.4: index out of range when resolving a function #89046

Closed
cockroach-teamcity opened this issue Sep 30, 2022 · 5 comments · Fixed by #89184
Closed

opt: v22.2.0-alpha.4: index out of range when resolving a function #89046

cockroach-teamcity opened this issue Sep 30, 2022 · 5 comments · Fixed by #89184
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. GA-blocker O-sentry Originated from an in-the-wild panic report. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)

Comments

@cockroach-teamcity
Copy link
Member

cockroach-teamcity commented Sep 30, 2022

This issue was autofiled by Sentry. It represents a crash or reported error on a live cluster with telemetry enabled.

Sentry link: https://sentry.io/organizations/cockroach-labs/issues/3632953670/?referrer=webhooks_plugin

Panic message:

catch.go:29: runtime error: index out of range [0] with length 0
(1) assertion failure
Wraps: (2) attached stack trace
-- stack trace:
| github.com/cockroachdb/cockroach/pkg/util/errorutil.ShouldCatch
| github.com/cockroachdb/cockroach/pkg/util/errorutil/catch.go:29
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*Builder).Build.func1
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:192
| runtime.gopanic
| GOROOT/src/runtime/panic.go:884
| runtime.goPanicIndex
| GOROOT/src/runtime/panic.go:113
| github.com/cockroachdb/cockroach/pkg/sql/sem/tree.(*ResolvedFunctionDefinition).GetClass
| github.com/cockroachdb/cockroach/pkg/sql/sem/tree/function_definition.go:330
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.isClass
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/groupby.go:898
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.isGenerator
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/groupby.go:890
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*scope).VisitPre
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/scope.go:1061
| github.com/cockroachdb/cockroach/pkg/sql/sem/tree.WalkExpr
| github.com/cockroachdb/cockroach/pkg/sql/sem/tree/walk.go:824
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*scope).walkExprTree
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/scope.go:428
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*scope).resolveType
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/scope.go:467
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*Builder).analyzeSelectList
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/project.go:160
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*Builder).analyzeProjectionList
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/project.go:94
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*Builder).buildSelectClause
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:1059
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*Builder).buildSelectStmtWithoutParens
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:996
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*Builder).buildSelect.func1
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:965
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*Builder).processWiths
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/with.go:115
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*Builder).buildSelect
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/select.go:964
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*Builder).buildStmt
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:305
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*Builder).buildStmtAtRoot
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:252
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder.(*Builder).Build
| github.com/cockroachdb/cockroach/pkg/sql/opt/optbuilder/builder.go:226
| github.com/cockroachdb/cockroach/pkg/sql.(*optPlanningCtx).buildExecMemo
| github.com/cockroachdb/cockroach/pkg/sql/plan_opt.go:562
| github.com/cockroachdb/cockroach/pkg/sql.(*planner).makeOptimizerPlan
| github.com/cockroachdb/cockroach/pkg/sql/plan_opt.go:233
| github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).makeExecPlan
| github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:1432
| github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).dispatchToExecutionEngine
| github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:1058
| github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execStmtInOpenState
| github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:687
| github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execStmt.func1
| github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:129
| github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execWithProfiling
| github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:2382
| github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execStmt
| github.com/cockroachdb/cockroach/pkg/sql/conn_executor_exec.go:128
| github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execCmd.func1
| github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:1919
| github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).execCmd
| github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:1923
| github.com/cockroachdb/cockroach/pkg/sql.(*connExecutor).run
| github.com/cockroachdb/cockroach/pkg/sql/conn_executor.go:1845
Wraps: (3) runtime error: index out of range [0] with length 0
| -- cause hidden behind barrier
| runtime error: index out of range [0] with length 0
| (1) runtime error: index out of range [0] with length 0
| Error types: (1) runtime.boundsError
Error types: (1) *assert.withAssertionFailure (2) *withstack.withStack (3) *barriers.barrierErr
-- report composition:
*barriers.barrierErr: masked error: runtime error: index out of range [0] with length 0
catch.go:29: *withstack.withStack (top exception)
*assert.withAssertionFailure

Stacktrace (expand for inline code snippets):

// get reported to Sentry.
err = errors.HandleAsAssertionFailure(err)
}
in pkg/util/errorutil.ShouldCatch
// manipulate locks.
if ok, e := errorutil.ShouldCatch(r); ok {
err = e
in pkg/sql/opt/optbuilder.(*Builder).Build.func1
GOROOT/src/runtime/panic.go#L883-L885 in runtime.gopanic
GOROOT/src/runtime/panic.go#L112-L114 in runtime.goPanicIndex
func (fd *ResolvedFunctionDefinition) GetClass() (FunctionClass, error) {
ret := fd.Overloads[0].Class
for i := range fd.Overloads {
in pkg/sql/sem/tree.(*ResolvedFunctionDefinition).GetClass
func isClass(def *tree.ResolvedFunctionDefinition, want tree.FunctionClass) bool {
cls, err := def.GetClass()
if err != nil {
in pkg/sql/opt/optbuilder.isClass
func isGenerator(def *tree.ResolvedFunctionDefinition) bool {
return isClass(def, tree.GeneratorClass)
}
in pkg/sql/opt/optbuilder.isGenerator
if isGenerator(def) && s.replaceSRFs {
expr = s.replaceSRF(t, def)
in pkg/sql/opt/optbuilder.(*scope).VisitPre
func WalkExpr(v Visitor, expr Expr) (newExpr Expr, changed bool) {
recurse, newExpr := v.VisitPre(expr)
in pkg/sql/sem/tree.WalkExpr
expr, _ = tree.WalkExpr(s, expr)
s.builder.semaCtx.IVarContainer = s
in pkg/sql/opt/optbuilder.(*scope).walkExprTree
func (s *scope) resolveType(expr tree.Expr, desired *types.T) tree.TypedExpr {
expr = s.walkExprTree(expr)
texpr, err := tree.TypeCheck(s.builder.ctx, expr, s.builder.semaCtx, desired)
in pkg/sql/opt/optbuilder.(*scope).resolveType
texpr = inScope.resolveType(e.Expr, desired)
}
in pkg/sql/opt/optbuilder.(*Builder).analyzeSelectList
b.analyzeSelectList(selects, desiredTypes, inScope, outScope)
}
in pkg/sql/opt/optbuilder.(*Builder).analyzeProjectionList
// aggInScope and aggOutScope.
b.analyzeProjectionList(sel.Exprs, desiredTypes, fromScope, projectionsScope)
in pkg/sql/opt/optbuilder.(*Builder).buildSelectClause
case *tree.SelectClause:
outScope = b.buildSelectClause(t, orderBy, locking, desiredTypes, inScope)
in pkg/sql/opt/optbuilder.(*Builder).buildSelectStmtWithoutParens
return b.processWiths(with, inScope, func(inScope *scope) *scope {
return b.buildSelectStmtWithoutParens(
wrapped, orderBy, limit, locking, desiredTypes, inScope,
in pkg/sql/opt/optbuilder.(*Builder).buildSelect.func1
inScope.atRoot = false
outScope := buildStmt(inScope)
outScope.expr = b.buildWiths(outScope.expr, correlatedCTEs)
in pkg/sql/opt/optbuilder.(*Builder).processWiths
return b.processWiths(with, inScope, func(inScope *scope) *scope {
return b.buildSelectStmtWithoutParens(
in pkg/sql/opt/optbuilder.(*Builder).buildSelect
case *tree.Select:
return b.buildSelect(stmt, noRowLocking, desiredTypes, inScope)
in pkg/sql/opt/optbuilder.(*Builder).buildStmt
b.ctes = nil
outScope = b.buildStmt(stmt, desiredTypes, inScope)
// Build With operators for any CTEs hoisted to the top level.
in pkg/sql/opt/optbuilder.(*Builder).buildStmtAtRoot
// and physical properties.
outScope := b.buildStmtAtRoot(b.stmt, nil /* desiredTypes */)
in pkg/sql/opt/optbuilder.(*Builder).Build
bld := optbuilder.New(ctx, &p.semaCtx, p.EvalContext(), &opc.catalog, f, opc.p.stmt.AST)
if err := bld.Build(); err != nil {
return nil, err
in pkg/sql.(*optPlanningCtx).buildExecMemo
execMemo, err := opc.buildExecMemo(ctx)
if err != nil {
in pkg/sql.(*planner).makeOptimizerPlan
func (ex *connExecutor) makeExecPlan(ctx context.Context, planner *planner) error {
if err := planner.makeOptimizerPlan(ctx); err != nil {
log.VEventf(ctx, 1, "optimizer plan failed: %v", err)
in pkg/sql.(*connExecutor).makeExecPlan
// between here and there needs to happen even if there's an error.
err := ex.makeExecPlan(ctx, planner)
// We'll be closing the plan manually below after execution; this
in pkg/sql.(*connExecutor).dispatchToExecutionEngine
if err := ex.dispatchToExecutionEngine(stmtCtx, p, res); err != nil {
stmtThresholdSpan.Finish()
in pkg/sql.(*connExecutor).execStmtInOpenState
err = ex.execWithProfiling(ctx, ast, prepared, func(ctx context.Context) error {
ev, payload, err = ex.execStmtInOpenState(ctx, parserStmt, prepared, pinfo, res, canAutoCommit)
return err
in pkg/sql.(*connExecutor).execStmt.func1
} else {
err = op(ctx)
}
in pkg/sql.(*connExecutor).execWithProfiling
case stateOpen:
err = ex.execWithProfiling(ctx, ast, prepared, func(ctx context.Context) error {
ev, payload, err = ex.execStmtInOpenState(ctx, parserStmt, prepared, pinfo, res, canAutoCommit)
in pkg/sql.(*connExecutor).execStmt
canAutoCommit := ex.implicitTxn() && (tcmd.LastInBatch || !implicitTxnForBatch)
ev, payload, err = ex.execStmt(
ctx, tcmd.Statement, nil /* prepared */, nil /* pinfo */, stmtRes, canAutoCommit,
in pkg/sql.(*connExecutor).execCmd.func1
return err
}()
// Note: we write to ex.statsCollector.PhaseTimes, instead of ex.phaseTimes,
in pkg/sql.(*connExecutor).execCmd
var err error
if err = ex.execCmd(); err != nil {
if errors.IsAny(err, io.EOF, errDrainingComplete) {
in pkg/sql.(*connExecutor).run

pkg/util/errorutil/catch.go in pkg/util/errorutil.ShouldCatch at line 29
pkg/sql/opt/optbuilder/builder.go in pkg/sql/opt/optbuilder.(*Builder).Build.func1 at line 192
GOROOT/src/runtime/panic.go in runtime.gopanic at line 884
GOROOT/src/runtime/panic.go in runtime.goPanicIndex at line 113
pkg/sql/sem/tree/function_definition.go in pkg/sql/sem/tree.(*ResolvedFunctionDefinition).GetClass at line 330
pkg/sql/opt/optbuilder/groupby.go in pkg/sql/opt/optbuilder.isClass at line 898
pkg/sql/opt/optbuilder/groupby.go in pkg/sql/opt/optbuilder.isGenerator at line 890
pkg/sql/opt/optbuilder/scope.go in pkg/sql/opt/optbuilder.(*scope).VisitPre at line 1061
pkg/sql/sem/tree/walk.go in pkg/sql/sem/tree.WalkExpr at line 824
pkg/sql/opt/optbuilder/scope.go in pkg/sql/opt/optbuilder.(*scope).walkExprTree at line 428
pkg/sql/opt/optbuilder/scope.go in pkg/sql/opt/optbuilder.(*scope).resolveType at line 467
pkg/sql/opt/optbuilder/project.go in pkg/sql/opt/optbuilder.(*Builder).analyzeSelectList at line 160
pkg/sql/opt/optbuilder/project.go in pkg/sql/opt/optbuilder.(*Builder).analyzeProjectionList at line 94
pkg/sql/opt/optbuilder/select.go in pkg/sql/opt/optbuilder.(*Builder).buildSelectClause at line 1059
pkg/sql/opt/optbuilder/select.go in pkg/sql/opt/optbuilder.(*Builder).buildSelectStmtWithoutParens at line 996
pkg/sql/opt/optbuilder/select.go in pkg/sql/opt/optbuilder.(*Builder).buildSelect.func1 at line 965
pkg/sql/opt/optbuilder/with.go in pkg/sql/opt/optbuilder.(*Builder).processWiths at line 115
pkg/sql/opt/optbuilder/select.go in pkg/sql/opt/optbuilder.(*Builder).buildSelect at line 964
pkg/sql/opt/optbuilder/builder.go in pkg/sql/opt/optbuilder.(*Builder).buildStmt at line 305
pkg/sql/opt/optbuilder/builder.go in pkg/sql/opt/optbuilder.(*Builder).buildStmtAtRoot at line 252
pkg/sql/opt/optbuilder/builder.go in pkg/sql/opt/optbuilder.(*Builder).Build at line 226
pkg/sql/plan_opt.go in pkg/sql.(*optPlanningCtx).buildExecMemo at line 562
pkg/sql/plan_opt.go in pkg/sql.(*planner).makeOptimizerPlan at line 233
pkg/sql/conn_executor_exec.go in pkg/sql.(*connExecutor).makeExecPlan at line 1432
pkg/sql/conn_executor_exec.go in pkg/sql.(*connExecutor).dispatchToExecutionEngine at line 1058
pkg/sql/conn_executor_exec.go in pkg/sql.(*connExecutor).execStmtInOpenState at line 687
pkg/sql/conn_executor_exec.go in pkg/sql.(*connExecutor).execStmt.func1 at line 129
pkg/sql/conn_executor_exec.go in pkg/sql.(*connExecutor).execWithProfiling at line 2382
pkg/sql/conn_executor_exec.go in pkg/sql.(*connExecutor).execStmt at line 128
pkg/sql/conn_executor.go in pkg/sql.(*connExecutor).execCmd.func1 at line 1919
pkg/sql/conn_executor.go in pkg/sql.(*connExecutor).execCmd at line 1923
pkg/sql/conn_executor.go in pkg/sql.(*connExecutor).run at line 1845
Tag Value
Cockroach Release v22.2.0-alpha.4
Cockroach SHA: aac413c
Platform windows amd64
Distribution CCL
Environment v22.2.0-alpha.4
Command server
Go Version ``
# of CPUs
# of Goroutines

Jira issue: CRDB-20083

@cockroach-teamcity cockroach-teamcity added C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. O-sentry Originated from an in-the-wild panic report. labels Sep 30, 2022
@michae2 michae2 added the T-sql-queries SQL Queries Team label Sep 30, 2022
@yuzefovich yuzefovich changed the title sentry: catch.go:29: runtime error: index out of range [0] with length 0 (1) assertion failure Wraps: (2) attached stack trace -- stack trace: | github.com/cockroachdb/cockroach/pkg/util/errorutil.ShouldC... opt: v22.2.0-alpha.4: index out of range when resolving a function Sep 30, 2022
@rytaft
Copy link
Collaborator

rytaft commented Sep 30, 2022

@chengxiong-ruan looks like it's possible to call GetClass on a function without overloads. Maybe you need to add a check for the length of fd.Overloads?

@blathers-crl blathers-crl bot added the T-sql-schema-deprecated Use T-sql-foundations instead label Sep 30, 2022
@chengxiong-ruan
Copy link
Contributor

@rytaft thanks for letting me know. Is there a way to get the query causing this panic so we can repro? In theory a function name, if exists, should have at least one overload.

I'll take a deeper look on Monday if this is not urgent. But I have a theory that this is a bug in the Schema descriptor that when a function is dropped and there is zero overload left we didn't delete the function name (the key of mapping from function name to a list of overloads). Basically need to delete the key from the map when length of overload is zero here so that function resolution would return function not found instead of returning a bad FunctionDefinition.

I'll have a fix for it on Monday.

@rytaft
Copy link
Collaborator

rytaft commented Oct 1, 2022

Usually sentry includes the statement fingerprint, but this one doesn't for some reason. Seems like we might want to consider this a GA blocker, so I'll add the label. But definitely no need to look at this before Monday. Thanks for taking a look!

@rytaft rytaft added GA-blocker branch-release-22.2 Used to mark GA and release blockers, technical advisories, and bugs for 22.2 labels Oct 1, 2022
@chengxiong-ruan chengxiong-ruan self-assigned this Oct 3, 2022
chengxiong-ruan added a commit to chengxiong-ruan/cockroach that referenced this issue Oct 3, 2022
Backport fixes: cockroachdb#89046
Previously we just remove an overload from the slice when dropping a
function. This is problematic if there's zero overloads left after the
drop because it pretends that there is some function with the name but
actually nothing. So we need to delete the key if there is not overload
for the name.

Release note: None
Release justification: GA blocker bug fix.
@chengxiong-ruan
Copy link
Contributor

yeah, I was able to repro with:

create function f(int) returns int language sql as $$ select 1 $$;
select f(123);
drop function f;
select f(321);

having a fix for it with #89184

craig bot pushed a commit that referenced this issue Oct 3, 2022
88944: sql/covering: make OverlapCoveringMerge benchmark deterministic r=cucaroach a=cucaroach

Previously we seeded RNG with time and benchmark results varied widely.
In order to make sure this doesn't make noise when comparing different
versions make the seed a constant.

Fixes: #88919

Release note: None


89184: sql: delete function name key from schema if no overload left after drop r=chengxiong-ruan a=chengxiong-ruan

Backport fixes: #89046
Previously we just remove an overload from the slice when dropping a function. This is problematic if there's zero overloads left after the drop because it pretends that there is some function with the name but actually nothing. So we need to delete the key if there is not overload for the name.

Release note: None
Release justification: GA blocker bug fix.

89189: opgen: ensure GC jobs for temp indexes have proper description r=postamar a=postamar

Fixes #82169.

Release note: None

Co-authored-by: Tommy Reilly <[email protected]>
Co-authored-by: Chengxiong Ruan <[email protected]>
Co-authored-by: Marius Posta <[email protected]>
@craig craig bot closed this as completed in 5ae9e2c Oct 3, 2022
chengxiong-ruan added a commit to chengxiong-ruan/cockroach that referenced this issue Oct 3, 2022
Backport fixes: cockroachdb#89046
Previously we just remove an overload from the slice when dropping a
function. This is problematic if there's zero overloads left after the
drop because it pretends that there is some function with the name but
actually nothing. So we need to delete the key if there is not overload
for the name.

Release note: None
Release justification: GA blocker bug fix.
@chengxiong-ruan
Copy link
Contributor

closed with #89219

@exalate-issue-sync exalate-issue-sync bot removed the T-sql-queries SQL Queries Team label Oct 3, 2022
@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
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. GA-blocker O-sentry Originated from an in-the-wild panic report. T-sql-foundations SQL Foundations Team (formerly SQL Schema + SQL Sessions)
Projects
Archived in project
Development

Successfully merging a pull request may close this issue.

4 participants