forked from dolthub/go-mysql-server
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.go
119 lines (98 loc) · 3.28 KB
/
common.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
// Copyright 2020-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/expression"
)
var ErrEvalUnsupportedOnAggregation = errors.NewKind("Unimplemented %s.Eval(). The code should have used AggregationBuffer.Eval(ctx).")
// unaryAggBase is the generic embedded class optgen
// uses to codegen single expression aggregate functions.
type unaryAggBase struct {
expression.UnaryExpression
window *sql.WindowDefinition
functionName string
description string
typ sql.Type
}
var _ sql.Aggregation = (*unaryAggBase)(nil)
func (a *unaryAggBase) NewWindowFunction() (sql.WindowFunction, error) {
panic("unaryAggBase is a base type, type must implement NewWindowFunction")
}
func (a *unaryAggBase) NewBuffer() (sql.AggregationBuffer, error) {
panic("unaryAggBase is a base type, type must implement NewWindowFunction")
}
// WithWindow returns a new unaryAggBase to be embedded in wrapping type
func (a *unaryAggBase) WithWindow(window *sql.WindowDefinition) (sql.Aggregation, error) {
na := *a
na.window = window
return &na, nil
}
func (a *unaryAggBase) Window() *sql.WindowDefinition {
return a.window
}
func (a *unaryAggBase) String() string {
return fmt.Sprintf("%s(%s)", a.functionName, a.Child)
}
func (a *unaryAggBase) Type() sql.Type {
return a.typ
}
func (a *unaryAggBase) Eval(ctx *sql.Context, row sql.Row) (interface{}, error) {
return nil, ErrEvalUnsupportedOnAggregation.New(a.FunctionName())
}
func (a *unaryAggBase) Children() []sql.Expression {
children := []sql.Expression{a.Child}
if a.window != nil {
children = append(children, a.window.ToExpressions()...)
}
return children
}
func (a *unaryAggBase) Resolved() bool {
if _, ok := a.Child.(*expression.Star); ok {
return true
} else if !a.Child.Resolved() {
return false
}
if a.window == nil {
return true
}
return windowResolved(a.window)
}
// WithChildren returns a new unaryAggBase to be embedded in wrapping type
func (a *unaryAggBase) WithChildren(children ...sql.Expression) (sql.Expression, error) {
if len(children) < 1 {
return nil, sql.ErrInvalidChildrenNumber.New(a, len(children), 1)
}
na := *a
na.UnaryExpression = expression.UnaryExpression{Child: children[0]}
if len(children) > 1 && a.window != nil {
w, err := a.window.FromExpressions(children[1:])
if err != nil {
return nil, err
}
return na.WithWindow(w)
}
return &na, nil
}
func (a unaryAggBase) FunctionName() string {
return a.functionName
}
func (a unaryAggBase) Description() string {
return a.description
}
func windowResolved(w *sql.WindowDefinition) bool {
return expression.ExpressionsResolved(append(w.OrderBy.ToExpressions(), w.PartitionBy...)...)
}