Skip to content

Commit

Permalink
planner: fix pointer dereference
Browse files Browse the repository at this point in the history
  • Loading branch information
brennanjl authored and Yaiba committed Jan 15, 2025
1 parent 3b498c8 commit 9f9184f
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 1 deletion.
6 changes: 5 additions & 1 deletion node/engine/planner/logical/planner.go
Original file line number Diff line number Diff line change
Expand Up @@ -1573,6 +1573,10 @@ func (s *scopeContext) exprWithAggRewrite(node parse.Expression, currentRel *Rel
// might be in the outer relation, correlated
field, err = s.OuterRelation.Search(node.Table, node.Column)
if errors.Is(err, ErrColumnNotFound) {
if s.preGroupRelation == nil {
return nil, nil, false, fmt.Errorf(`%w: unknown column reference "%s"`, ErrColumnNotFound, node.String())
}

// if not found, see if it is in the relation but not grouped
field, err2 := s.preGroupRelation.Search(node.Table, node.Column)
// if the column exist in the outer relation, then it might be part of an expression
Expand Down Expand Up @@ -1942,7 +1946,7 @@ func (s *scopeContext) exprWithAggRewrite(node parse.Expression, currentRel *Rel
}
}

return subqExpr, rel.Fields[0], shouldRewrite, nil
return cast(subqExpr, rel.Fields[0])
}
}

Expand Down
21 changes: 21 additions & 0 deletions node/engine/planner/logical/planner_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,6 +616,27 @@ func Test_Planner(t *testing.T) {
" └─Filter: r.n < 10\n" +
" └─Scan Table: r [cte]\n",
},
{
// this caused a nil pointer dereference.
// It is an illegal query, but it should not crash the planner.
name: "inserting a cte",
sql: `with a as (select 1)
INSERT INTO users VALUES ('123e4567-e89b-12d3-a456-426614174000'::uuid, 'satoshi', a)`,
err: logical.ErrColumnNotFound,
},
{
// this is a regression test for a previous uncaught type case
name: "subquery type cast",
sql: `select * from users where age = (select sum(age) from users)::int8`,
wt: "Return: id [uuid], name [text], age [int8]\n" +
"└─Project: users.id; users.name; users.age\n" +
" └─Filter: users.age = [subquery (scalar) (subplan_id=0) (uncorrelated)]::int8\n" +
" └─Scan Table: users [physical]\n" +
"Subplan [subquery] [id=0]\n" +
"└─Project: {#ref(A)}\n" +
" └─Aggregate: {#ref(A) = sum(users.age)}\n" +
" └─Scan Table: users [physical]\n",
},
}

for _, test := range tests {
Expand Down

0 comments on commit 9f9184f

Please sign in to comment.