forked from goccy/go-zetasqlite
-
Notifications
You must be signed in to change notification settings - Fork 1
/
function_aggregate_option.go
151 lines (137 loc) · 3.51 KB
/
function_aggregate_option.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
package internal
import (
"github.com/goccy/go-json"
)
type AggregatorFuncOption struct {
Type AggregatorFuncOptionType `json:"type"`
Value interface{} `json:"value"`
}
func (o *AggregatorFuncOption) UnmarshalJSON(b []byte) error {
type aggregatorFuncOption AggregatorFuncOption
var v aggregatorFuncOption
if err := json.Unmarshal(b, &v); err != nil {
return err
}
o.Type = v.Type
switch v.Type {
case AggregatorFuncOptionDistinct:
case AggregatorFuncOptionIgnoreNulls:
case AggregatorFuncOptionLimit:
var value struct {
Value int64 `json:"value"`
}
if err := json.Unmarshal(b, &value); err != nil {
return err
}
o.Value = value.Value
case AggregatorFuncOptionOrderBy:
var value struct {
Value *AggregateOrderBy `json:"value"`
}
if err := json.Unmarshal(b, &value); err != nil {
return err
}
o.Value = value.Value
}
return nil
}
type AggregatorFuncOptionType string
const (
AggregatorFuncOptionUnknown AggregatorFuncOptionType = "aggregate_unknown"
AggregatorFuncOptionDistinct AggregatorFuncOptionType = "aggregate_distinct"
AggregatorFuncOptionLimit AggregatorFuncOptionType = "aggregate_limit"
AggregatorFuncOptionOrderBy AggregatorFuncOptionType = "aggregate_order_by"
AggregatorFuncOptionIgnoreNulls AggregatorFuncOptionType = "aggregate_ignore_nulls"
)
func DISTINCT() (Value, error) {
b, _ := json.Marshal(&AggregatorFuncOption{
Type: AggregatorFuncOptionDistinct,
})
return StringValue(string(b)), nil
}
func LIMIT(limit int64) (Value, error) {
b, _ := json.Marshal(&AggregatorFuncOption{
Type: AggregatorFuncOptionLimit,
Value: limit,
})
return StringValue(string(b)), nil
}
func IGNORE_NULLS() (Value, error) {
b, _ := json.Marshal(&AggregatorFuncOption{
Type: AggregatorFuncOptionIgnoreNulls,
})
return StringValue(string(b)), nil
}
type AggregateOrderBy struct {
Value Value `json:"value"`
IsAsc bool `json:"isAsc"`
}
func (a *AggregateOrderBy) UnmarshalJSON(b []byte) error {
var v struct {
Value interface{} `json:"value"`
IsAsc bool `json:"isAsc"`
}
if err := json.Unmarshal(b, &v); err != nil {
return err
}
value, err := ValueFromGoValue(v.Value)
if err != nil {
return err
}
a.Value = value
a.IsAsc = v.IsAsc
return nil
}
func ORDER_BY(value Value, isAsc bool) (Value, error) {
b, _ := json.Marshal(&AggregatorFuncOption{
Type: AggregatorFuncOptionOrderBy,
Value: &AggregateOrderBy{
Value: value,
IsAsc: isAsc,
},
})
return StringValue(string(b)), nil
}
type AggregatorOption struct {
Distinct bool
IgnoreNulls bool
Limit *int64
OrderBy []*AggregateOrderBy
}
func parseAggregateOptions(args ...Value) ([]Value, *AggregatorOption, error) {
var (
filteredArgs []Value
opt = &AggregatorOption{}
)
for _, arg := range args {
if arg == nil {
filteredArgs = append(filteredArgs, nil)
continue
}
text, err := arg.ToString()
if err != nil {
filteredArgs = append(filteredArgs, arg)
continue
}
var v AggregatorFuncOption
if err := json.Unmarshal([]byte(text), &v); err != nil {
filteredArgs = append(filteredArgs, arg)
continue
}
switch v.Type {
case AggregatorFuncOptionDistinct:
opt.Distinct = true
case AggregatorFuncOptionIgnoreNulls:
opt.IgnoreNulls = true
case AggregatorFuncOptionLimit:
i64 := v.Value.(int64)
opt.Limit = &i64
case AggregatorFuncOptionOrderBy:
opt.OrderBy = append(opt.OrderBy, v.Value.(*AggregateOrderBy))
default:
filteredArgs = append(filteredArgs, arg)
continue
}
}
return filteredArgs, opt, nil
}