Skip to content

Commit 5722930

Browse files
authored
planner: make var_samp can be used as a window function (#53130) (#53285)
close #52933
1 parent eb27609 commit 5722930

File tree

3 files changed

+40
-23
lines changed

3 files changed

+40
-23
lines changed

executor/window_test.go

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -498,3 +498,12 @@ func TestIssue29947(t *testing.T) {
498498
result.Check(testkit.Rows("2", "3"))
499499
tk.MustExec("commit")
500500
}
501+
502+
func TestVarSampAsAWindowFunction(t *testing.T) {
503+
store := testkit.CreateMockStore(t)
504+
tk := testkit.NewTestKit(t, store)
505+
tk.MustExec("use test")
506+
tk.MustExec("create table t1 (c1 int)")
507+
tk.MustExec("select var_samp(c1) from t1")
508+
tk.MustExec("select c1, var_samp(c1) over (partition by c1) from t1")
509+
}

parser/parser.go

Lines changed: 5 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

parser/parser.y

Lines changed: 26 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -50,10 +50,11 @@ import (
5050
%token <ident>
5151

5252
/*yy:token "%c" */
53-
identifier "identifier"
54-
asof "AS OF"
55-
toTimestamp "TO TIMESTAMP"
56-
toTSO "TO TSO"
53+
identifier "identifier"
54+
asof "AS OF"
55+
toTimestamp "TO TIMESTAMP"
56+
toTSO "TO TSO"
57+
5758
/*yy:token "_%c" */
5859
underscoreCS "UNDERSCORE_CHARSET"
5960

@@ -2621,44 +2622,44 @@ FlashbackToTimestampStmt:
26212622
"FLASHBACK" "CLUSTER" toTimestamp stringLit
26222623
{
26232624
$$ = &ast.FlashBackToTimestampStmt{
2624-
FlashbackTS: ast.NewValueExpr($4, "", ""),
2625+
FlashbackTS: ast.NewValueExpr($4, "", ""),
26252626
FlashbackTSO: 0,
26262627
}
26272628
}
26282629
| "FLASHBACK" "TABLE" TableNameList toTimestamp stringLit
26292630
{
26302631
$$ = &ast.FlashBackToTimestampStmt{
2631-
Tables: $3.([]*ast.TableName),
2632-
FlashbackTS: ast.NewValueExpr($5, "", ""),
2632+
Tables: $3.([]*ast.TableName),
2633+
FlashbackTS: ast.NewValueExpr($5, "", ""),
26332634
FlashbackTSO: 0,
26342635
}
26352636
}
26362637
| "FLASHBACK" DatabaseSym DBName toTimestamp stringLit
26372638
{
26382639
$$ = &ast.FlashBackToTimestampStmt{
2639-
DBName: model.NewCIStr($3),
2640-
FlashbackTS: ast.NewValueExpr($5, "", ""),
2640+
DBName: model.NewCIStr($3),
2641+
FlashbackTS: ast.NewValueExpr($5, "", ""),
26412642
FlashbackTSO: 0,
26422643
}
26432644
}
26442645
| "FLASHBACK" "CLUSTER" toTSO LengthNum
26452646
{
26462647
if tsoValue, ok := $4.(uint64); ok && tsoValue > 0 {
26472648
$$ = &ast.FlashBackToTimestampStmt{
2648-
FlashbackTSO: tsoValue,
2649-
}
2649+
FlashbackTSO: tsoValue,
2650+
}
26502651
} else {
2651-
yylex.AppendError(yylex.Errorf("Invalid TSO value provided: %d", $4))
2652-
return 1
2652+
yylex.AppendError(yylex.Errorf("Invalid TSO value provided: %d", $4))
2653+
return 1
26532654
}
26542655
}
26552656
| "FLASHBACK" "TABLE" TableNameList toTSO LengthNum
26562657
{
26572658
if tsoValue, ok := $5.(uint64); ok && tsoValue > 0 {
26582659
$$ = &ast.FlashBackToTimestampStmt{
2659-
Tables: $3.([]*ast.TableName),
2660-
FlashbackTSO: tsoValue,
2661-
}
2660+
Tables: $3.([]*ast.TableName),
2661+
FlashbackTSO: tsoValue,
2662+
}
26622663
} else {
26632664
yylex.AppendError(yylex.Errorf("Invalid TSO value provided: %d", $5))
26642665
return 1
@@ -2668,16 +2669,15 @@ FlashbackToTimestampStmt:
26682669
{
26692670
if tsoValue, ok := $5.(uint64); ok && tsoValue > 0 {
26702671
$$ = &ast.FlashBackToTimestampStmt{
2671-
DBName: model.NewCIStr($3),
2672-
FlashbackTSO: tsoValue,
2672+
DBName: model.NewCIStr($3),
2673+
FlashbackTSO: tsoValue,
26732674
}
26742675
} else {
26752676
yylex.AppendError(yylex.Errorf("Invalid TSO value provided: %d", $5))
26762677
return 1
26772678
}
26782679
}
26792680

2680-
26812681
/*******************************************************************
26822682
*
26832683
* Flush Back Table Statement
@@ -7868,7 +7868,11 @@ SumExpr:
78687868
}
78697869
| builtinVarSamp '(' BuggyDefaultFalseDistinctOpt Expression ')' OptWindowingClause
78707870
{
7871-
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4}, Distinct: $3.(bool)}
7871+
if $6 != nil {
7872+
$$ = &ast.WindowFuncExpr{F: $1, Args: []ast.ExprNode{$4}, Distinct: $3.(bool), Spec: *($6.(*ast.WindowSpec))}
7873+
} else {
7874+
$$ = &ast.AggregateFuncExpr{F: $1, Args: []ast.ExprNode{$4}, Distinct: $3.(bool)}
7875+
}
78727876
}
78737877
| "JSON_ARRAYAGG" '(' Expression ')' OptWindowingClause
78747878
{
@@ -9772,8 +9776,8 @@ SetOprStmtWoutLimitOrderBy:
97729776
setOprList2 = []ast.Node{x}
97739777
with2 = x.With
97749778
case *ast.SetOprStmt:
9775-
// child setOprStmt's limit and order should also make sense
9776-
// we should separate it out from other normal SetOprSelectList.
9779+
// child setOprStmt's limit and order should also make sense
9780+
// we should separate it out from other normal SetOprSelectList.
97779781
setOprList2 = x.SelectList.Selects
97789782
with2 = x.With
97799783
limit2 = x.Limit

0 commit comments

Comments
 (0)