-
Notifications
You must be signed in to change notification settings - Fork 0
/
group_concat.go
executable file
·293 lines (240 loc) · 7.07 KB
/
group_concat.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
// 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"
"sort"
"strings"
"github.com/dolthub/vitess/go/vt/proto/query"
"github.com/Rock-liyi/p2pdb-store/sql"
"github.com/Rock-liyi/p2pdb-store/sql/expression"
)
type GroupConcat struct {
distinct string
sf sql.SortFields
separator string
selectExprs []sql.Expression
maxLen int
returnType sql.Type
}
var _ sql.FunctionExpression = &GroupConcat{}
var _ sql.Aggregation = &GroupConcat{}
func NewEmptyGroupConcat() sql.Expression {
return &GroupConcat{}
}
// FunctionName implements sql.FunctionExpression
func (g *GroupConcat) FunctionName() string {
return "group_concat"
}
// Description implements sql.FunctionExpression
func (g *GroupConcat) Description() string {
return "returns a string result with the concatenated non-NULL values from a group."
}
func NewGroupConcat(distinct string, orderBy sql.SortFields, separator string, selectExprs []sql.Expression, maxLen int) (*GroupConcat, error) {
return &GroupConcat{distinct: distinct, sf: orderBy, separator: separator, selectExprs: selectExprs, maxLen: maxLen}, nil
}
// NewBuffer creates a new buffer for the aggregation.
func (g *GroupConcat) NewBuffer() (sql.AggregationBuffer, error) {
var rows []sql.Row
distinctSet := make(map[string]bool)
return &groupConcatBuffer{g, rows, distinctSet}, nil
}
// Eval implements the Expression interface.
func (g *GroupConcat) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return nil, ErrEvalUnsupportedOnAggregation.New("GroupConcat")
}
// Resolved implements the Expression interface.
func (g *GroupConcat) Resolved() bool {
for _, se := range g.selectExprs {
if !se.Resolved() {
return false
}
}
sfs := g.sf.ToExpressions()
for _, sf := range sfs {
if !sf.Resolved() {
return false
}
}
return true
}
func (g *GroupConcat) String() string {
sb := strings.Builder{}
sb.WriteString("group_concat(")
if g.distinct != "" {
sb.WriteString(fmt.Sprintf("distinct %s", g.distinct))
}
if g.selectExprs != nil {
var exprs = make([]string, len(g.selectExprs))
for i, expr := range g.selectExprs {
exprs[i] = expr.String()
}
sb.WriteString(strings.Join(exprs, ", "))
}
if len(g.sf) > 0 {
sb.WriteString(" order by ")
for i, ob := range g.sf {
if i > 0 {
sb.WriteString(", ")
}
sb.WriteString(ob.String())
}
}
if g.separator != "," {
sb.WriteString(" separator ")
sb.WriteString(fmt.Sprintf("'%s'", g.separator))
}
sb.WriteString(")")
return sb.String()
}
// Type implements the Expression interface.
// cc: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat for explanations
// on return type.
func (g *GroupConcat) Type() sql.Type {
if g.returnType == sql.Blob {
if g.maxLen <= 512 {
return sql.MustCreateString(query.Type_VARBINARY, 512, sql.Collation_binary)
} else {
return sql.Blob
}
} else {
if g.maxLen <= 512 {
return sql.MustCreateString(query.Type_VARCHAR, 512, sql.Collation_Default)
} else {
return sql.Text
}
}
}
// IsNullable implements the Expression interface.
func (g *GroupConcat) IsNullable() bool {
return false
}
// Children implements the Expression interface.
func (g *GroupConcat) Children() []sql.Expression {
return append(g.sf.ToExpressions(), g.selectExprs...)
}
// WithChildren implements the Expression interface.
func (g *GroupConcat) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) == 0 {
return nil, sql.ErrInvalidChildrenNumber.New(GroupConcat{}, len(children), 2)
}
// Get the order by expression using the length of the sort fields.
sortFieldMarker := len(g.sf)
orderByExpr := children[:len(g.sf)]
return NewGroupConcat(g.distinct, g.sf.FromExpressions(orderByExpr), g.separator, children[sortFieldMarker:], g.maxLen)
}
type groupConcatBuffer struct {
gc *GroupConcat
rows []sql.Row
distinctSet map[string]bool
}
// Update implements the AggregationBuffer interface.
func (g *groupConcatBuffer) Update(ctx *sql.Context, originalRow sql.Row) error {
evalRow, retType, err := evalExprs(ctx, g.gc.selectExprs, originalRow)
if err != nil {
return err
}
g.gc.returnType = retType
// Skip if this is a null row
if evalRow == nil {
return nil
}
var v interface{}
if retType == sql.Blob {
v, err = sql.Blob.Convert(evalRow[0])
} else {
v, err = sql.LongText.Convert(evalRow[0])
}
if err != nil {
return err
}
if v == nil {
return nil
}
vs := v.(string)
// Get the current array of rows and the map
// Check if distinct is active if so look at and update our map
if g.gc.distinct != "" {
// If this value exists go ahead and return nil
if _, ok := g.distinctSet[vs]; ok {
return nil
} else {
g.distinctSet[vs] = true
}
}
// Append the current value to the end of the row. We want to preserve the row's original structure for
// for sort ordering in the final step.
g.rows = append(g.rows, append(originalRow, nil, vs))
return nil
}
// Eval implements the AggregationBuffer interface.
// cc: https://dev.mysql.com/doc/refman/8.0/en/aggregate-functions.html#function_group-concat
func (g *groupConcatBuffer) Eval(ctx *sql.Context) (interface{}, error) {
rows := g.rows
if len(rows) == 0 {
return nil, nil
}
// Execute the order operation if it exists.
if g.gc.sf != nil {
sorter := &expression.Sorter{
SortFields: g.gc.sf,
Rows: rows,
Ctx: ctx,
}
sort.Stable(sorter)
if sorter.LastError != nil {
return nil, sorter.LastError
}
}
sb := strings.Builder{}
for i, row := range rows {
lastIdx := len(row) - 1
if i == 0 {
sb.WriteString(row[lastIdx].(string))
} else {
sb.WriteString(g.gc.separator)
sb.WriteString(row[lastIdx].(string))
}
// Don't allow the string to cross maxlen
if sb.Len() >= g.gc.maxLen {
break
}
}
ret := sb.String()
// There might be a couple of character differences even if we broke early in the loop
if len(ret) > g.gc.maxLen {
ret = ret[:g.gc.maxLen]
}
// Add this to handle any one off errors.
return ret, nil
}
// Dispose implements the Disposable interface.
func (g *groupConcatBuffer) Dispose() {
}
func evalExprs(ctx *sql.Context, exprs []sql.Expression, row sql.Row) (sql.Row, sql.Type, error) {
result := make(sql.Row, len(exprs))
retType := sql.Blob
for i, expr := range exprs {
var err error
result[i], err = expr.Eval(ctx, row)
if err != nil {
return nil, nil, err
}
// If every expression returns Blob type return Blob otherwise return Text.
if expr.Type() != sql.Blob {
retType = sql.Text
}
}
return result, retType, nil
}