From 6ffe4b8e0c9c4c37ae301911569e13b2ecec1bae Mon Sep 17 00:00:00 2001 From: James Cor Date: Wed, 4 Dec 2024 13:58:17 -0800 Subject: [PATCH] fix `AS OF` clause panic for certain expressions (#2779) --- enginetest/queries/queries.go | 4 ++++ sql/plan/subquery.go | 8 ++++++++ sql/planbuilder/show.go | 3 +++ 3 files changed, 15 insertions(+) diff --git a/enginetest/queries/queries.go b/enginetest/queries/queries.go index 32db69e1dc..a6c321dec0 100644 --- a/enginetest/queries/queries.go +++ b/enginetest/queries/queries.go @@ -10751,6 +10751,10 @@ var ErrorQueries = []QueryErrorTest{ Query: "SELECT i FROM myhistorytable AS OF MAX(i)", ExpectedErr: sql.ErrInvalidAsOfExpression, }, + { + Query: "SELECT i FROM myhistorytable AS OF (SELECT 1)", + ExpectedErrStr: "invalid AS OF expression type", + }, { Query: "SELECT pk FROM one_pk WHERE pk > ?", ExpectedErr: sql.ErrUnboundPreparedStatementVariable, diff --git a/sql/plan/subquery.go b/sql/plan/subquery.go index 2d0d5f7147..cbb5880039 100644 --- a/sql/plan/subquery.go +++ b/sql/plan/subquery.go @@ -349,6 +349,10 @@ func (s *Subquery) evalMultiple(ctx *sql.Context, row sql.Row) ([]interface{}, e return nil, err } + if s.b == nil { + return nil, fmt.Errorf("attempted to evaluate uninitialized subquery") + } + iter, err := s.b.Build(ctx, q, row) if err != nil { return nil, err @@ -434,6 +438,10 @@ func (s *Subquery) HasResultRow(ctx *sql.Context, row sql.Row) (bool, error) { return false, err } + if s.b == nil { + return false, fmt.Errorf("attempted to evaluate uninitialized subquery") + } + iter, err := s.b.Build(ctx, q, row) if err != nil { return false, err diff --git a/sql/planbuilder/show.go b/sql/planbuilder/show.go index dfcae8e063..2a04a0cf49 100644 --- a/sql/planbuilder/show.go +++ b/sql/planbuilder/show.go @@ -615,7 +615,10 @@ func (b *Builder) buildAsOfExpr(inScope *scope, time ast.Expr) sql.Expression { err := sql.ErrInvalidAsOfExpression.New(v) b.handleErr(err) } + case *ast.ConvertExpr: + case ast.InjectedExpr: default: + b.handleErr(fmt.Errorf("invalid AS OF expression type")) } return b.buildScalar(b.newScope(), time) }