Skip to content

Commit

Permalink
feat: json object agg can take expressions too
Browse files Browse the repository at this point in the history
Signed-off-by: Manan Gupta <[email protected]>
  • Loading branch information
GuptaManan100 committed Jun 24, 2024
1 parent 7c81d62 commit 376e4b4
Show file tree
Hide file tree
Showing 11 changed files with 819 additions and 826 deletions.
12 changes: 5 additions & 7 deletions go/vt/sqlparser/ast.go
Original file line number Diff line number Diff line change
Expand Up @@ -2584,8 +2584,8 @@ type (
// JSONObjectAgg is an aggregation expression that creates a JSON Object.
// For more information, visit https://dev.mysql.com/doc/refman/8.4/en/aggregate-functions.html#function_json-objectagg
JSONObjectAgg struct {
Key *ColName
Value *ColName
Key Expr
Value Expr
OverClause *OverClause
}

Expand Down Expand Up @@ -3448,9 +3448,7 @@ func (varS *VarSamp) SetArg(expr Expr) { varS.Arg = expr }
func (variance *Variance) SetArg(expr Expr) { variance.Arg = expr }
func (av *AnyValue) SetArg(expr Expr) { av.Arg = expr }
func (jaa *JSONArrayAgg) SetArg(expr Expr) { jaa.Expr = expr }
func (joa *JSONObjectAgg) SetArg(expr Expr) {
joa.Key = getColNameForExpression(expr, "JSONObjectAgg")
}
func (joa *JSONObjectAgg) SetArg(expr Expr) { joa.Key = expr }

func (min *Min) SetArgs(exprs Exprs) error { return setFuncArgs(min, exprs, "MIN") }
func (sum *Sum) SetArgs(exprs Exprs) error { return setFuncArgs(sum, exprs, "SUM") }
Expand All @@ -3473,8 +3471,8 @@ func (joa *JSONObjectAgg) SetArgs(exprs Exprs) error {
if len(exprs) != 2 {
return vterrors.VT13001("JSONObjectAgg takes in 2 expressions")
}
joa.Key = getColNameForExpression(exprs[0], "JSONObjectAgg")
joa.Value = getColNameForExpression(exprs[1], "JSONObjectAgg")
joa.Key = exprs[0]
joa.Value = exprs[1]
return nil
}

Expand Down
4 changes: 2 additions & 2 deletions go/vt/sqlparser/ast_clone.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 4 additions & 4 deletions go/vt/sqlparser/ast_copy_on_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go/vt/sqlparser/ast_equals.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 0 additions & 9 deletions go/vt/sqlparser/ast_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -2184,15 +2184,6 @@ func ContainsAggregation(e SQLNode) bool {
return hasAggregates
}

// getColNameForExpression gets the column name for the given expression.
func getColNameForExpression(expr Expr, name string) *ColName {
colName, isColName := expr.(*ColName)
if !isColName {
panic(vterrors.VT13001(fmt.Sprintf("Column name required in %v", name)))
}
return colName
}

// setFuncArgs sets the arguments for the aggregation function, while checking that there is only one argument
func setFuncArgs(aggr AggrFunc, exprs Exprs, name string) error {
if len(exprs) != 1 {
Expand Down
8 changes: 4 additions & 4 deletions go/vt/sqlparser/ast_rewrite.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go/vt/sqlparser/ast_visit.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

14 changes: 9 additions & 5 deletions go/vt/sqlparser/cached_size.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions go/vt/sqlparser/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6062,8 +6062,8 @@ var (
input: "select count(1) from user where x_id = 'abc' group by n_id having json_arrayagg(x, y) = '[]'",
output: "syntax error at position 83",
}, {
input: "select count(1) from user where x_id = 'abc' group by n_id having json_objectagg(x + 1, y) = '[]'",
output: "syntax error at position 85",
input: "select count(1) from user where x_id = 'abc' group by n_id having json_objectagg(x, y, z) = '[]'",
output: "syntax error at position 87",
}, {
input: "select next 1+1 values from a",
output: "syntax error at position 15",
Expand Down
Loading

0 comments on commit 376e4b4

Please sign in to comment.