forked from dolthub/go-mysql-server
/
json_agg.go
177 lines (145 loc) · 5.03 KB
/
json_agg.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
// Copyright 2021 Dolthub, Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package aggregation
import (
"fmt"
"gopkg.in/src-d/go-errors.v1"
"github.com/gabereiser/go-mysql-server/sql"
"github.com/gabereiser/go-mysql-server/sql/types"
)
// ErrUnsupportedJSONFunction is returned when a unsupported JSON function is called.
var ErrUnsupportedJSONFunction = errors.NewKind("unsupported JSON function: %s")
// JSON_OBJECTAGG(key, value) [over_clause]
//
// JSONObjectAgg Takes two column names or expressions as arguments, the first of these being used as a key and the
// second as a value, and returns a JSON object containing key-value pairs. Returns NULL if the result contains no rows,
// or in the event of an error. An error occurs if any key name is NULL or the number of arguments is not equal to 2.
//
// https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_json-objectagg
//
// see also: https://dev.mysql.com/doc/refman/8.0/en/json.html#json-normalization
type JSONObjectAgg struct {
key sql.Expression
value sql.Expression
window *sql.WindowDefinition
}
var _ sql.FunctionExpression = (*JSONObjectAgg)(nil)
var _ sql.Aggregation = (*JSONObjectAgg)(nil)
var _ sql.WindowAdaptableExpression = (*JSONObjectAgg)(nil)
// NewJSONObjectAgg creates a new JSONObjectAgg function.
func NewJSONObjectAgg(key, value sql.Expression) sql.Expression {
return &JSONObjectAgg{key: key, value: value}
}
// FunctionName implements sql.FunctionExpression
func (j *JSONObjectAgg) FunctionName() string {
return "json_objectagg"
}
// Description implements sql.FunctionExpression
func (j *JSONObjectAgg) Description() string {
return "returns result set as a single JSON object."
}
// Resolved implements the Expression interface.
func (j *JSONObjectAgg) Resolved() bool {
return j.key.Resolved() && j.value.Resolved()
}
func (j *JSONObjectAgg) String() string {
return fmt.Sprintf("JSON_OBJECTAGG(%s, %s)", j.key, j.value)
}
// Type implements the Expression interface.
func (j *JSONObjectAgg) Type() sql.Type {
return types.JSON
}
// IsNullable implements the Expression interface.
func (j *JSONObjectAgg) IsNullable() bool {
return false
}
// Children implements the Expression interface.
func (j *JSONObjectAgg) Children() []sql.Expression {
return []sql.Expression{j.key, j.value}
}
// WithChildren implements the Expression interface.
func (j *JSONObjectAgg) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) != 2 {
return nil, sql.ErrInvalidChildrenNumber.New(j, len(children), 2)
}
return NewJSONObjectAgg(children[0], children[1]), nil
}
// WithWindow implements sql.Aggregation
func (j *JSONObjectAgg) WithWindow(window *sql.WindowDefinition) (sql.Aggregation, error) {
nj := *j
nj.window = window
return &nj, nil
}
// Window implements sql.Aggregation
func (j *JSONObjectAgg) Window() *sql.WindowDefinition {
return j.window
}
// NewBuffer implements the Aggregation interface.
func (j *JSONObjectAgg) NewBuffer() (sql.AggregationBuffer, error) {
row := make(map[string]interface{})
return &jsonObjectBuffer{row, j}, nil
}
// NewWindowFunctionAggregation implements sql.WindowAdaptableExpression
func (j *JSONObjectAgg) NewWindowFunction() (sql.WindowFunction, error) {
return NewWindowedJSONObjectAgg(j), nil
}
// Eval implements the Expression interface.
func (j *JSONObjectAgg) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return nil, ErrEvalUnsupportedOnAggregation.New("JSONObjectAgg")
}
type jsonObjectBuffer struct {
vals map[string]interface{}
joa *JSONObjectAgg
}
// Update implements the AggregationBuffer interface.
func (j *jsonObjectBuffer) Update(ctx *sql.Context, row sql.Row) error {
key, err := j.joa.key.Eval(ctx, row)
if err != nil {
return err
}
// An error occurs if any key name is NULL
if key == nil {
return sql.ErrJSONObjectAggNullKey.New()
}
val, err := j.joa.value.Eval(ctx, row)
if err != nil {
return err
}
// unwrap JSON values
if js, ok := val.(types.JSONValue); ok {
doc, err := js.Unmarshall(ctx)
if err != nil {
return err
}
val = doc.Val
}
// Update the map.
keyAsString, err := types.LongText.Convert(key)
if err != nil {
return nil
}
j.vals[keyAsString.(string)] = val
return nil
}
// Eval implements the AggregationBuffer interface.
func (j *jsonObjectBuffer) Eval(ctx *sql.Context) (interface{}, error) {
// When no rows are present return NULL
if len(j.vals) == 0 {
return nil, nil
}
return types.JSONDocument{Val: j.vals}, nil
}
// Dispose implements the Disposable interface.
func (j *jsonObjectBuffer) Dispose() {
}