Skip to content

Commit

Permalink
planner: make var_samp can be used as a window function (#53130) (#…
Browse files Browse the repository at this point in the history
  • Loading branch information
ti-chi-bot committed May 21, 2024
1 parent ba9904c commit 2c0e47b
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
9 changes: 9 additions & 0 deletions pkg/executor/window_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -539,3 +539,12 @@ func TestIssue45964And46050(t *testing.T) {
testReturnColumnNullableAttribute(tk, "cume_dist()", false)
testReturnColumnNullableAttribute(tk, "percent_rank()", false)
}

func TestVarSampAsAWindowFunction(t *testing.T) {
store := testkit.CreateMockStore(t)
tk := testkit.NewTestKit(t, store)
tk.MustExec("use test")
tk.MustExec("create table t1 (c1 int)")
tk.MustExec("select var_samp(c1) from t1")
tk.MustExec("select c1, var_samp(c1) over (partition by c1) from t1")
}
6 changes: 5 additions & 1 deletion pkg/parser/parser.go
Original file line number Diff line number Diff line change
Expand Up @@ -18256,7 +18256,11 @@ yynewstate:
}
case 1538:
{
parser.yyVAL.expr = &ast.AggregateFuncExpr{F: yyS[yypt-5].ident, Args: []ast.ExprNode{yyS[yypt-2].expr}, Distinct: yyS[yypt-3].item.(bool)}
if yyS[yypt-0].item != nil {
parser.yyVAL.expr = &ast.WindowFuncExpr{Name: yyS[yypt-5].ident, Args: []ast.ExprNode{yyS[yypt-2].expr}, Distinct: yyS[yypt-3].item.(bool), Spec: *(yyS[yypt-0].item.(*ast.WindowSpec))}
} else {
parser.yyVAL.expr = &ast.AggregateFuncExpr{F: yyS[yypt-5].ident, Args: []ast.ExprNode{yyS[yypt-2].expr}, Distinct: yyS[yypt-3].item.(bool)}
}
}
case 1539:
{
Expand Down
6 changes: 5 additions & 1 deletion pkg/parser/parser.y
Original file line number Diff line number Diff line change
Expand Up @@ -8372,7 +8372,11 @@ SumExpr:
}
| builtinVarSamp '(' BuggyDefaultFalseDistinctOpt Expression ')' OptWindowingClause
{
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4}, Distinct: $3.(bool)}
if $6 != nil {
$$ = &ast.WindowFuncExpr{Name: $1, Args: []ast.ExprNode{$4}, Distinct: $3.(bool), Spec: *($6.(*ast.WindowSpec))}
} else {
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4}, Distinct: $3.(bool)}
}
}
| "JSON_ARRAYAGG" '(' Expression ')' OptWindowingClause
{
Expand Down

0 comments on commit 2c0e47b

Please sign in to comment.