-
Notifications
You must be signed in to change notification settings - Fork 117
/
column_time_range.go
174 lines (152 loc) · 4.18 KB
/
column_time_range.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
package queries
import (
"context"
"errors"
"fmt"
"time"
"github.com/marcboeker/go-duckdb"
runtimev1 "github.com/rilldata/rill/proto/gen/rill/runtime/v1"
"github.com/rilldata/rill/runtime"
"github.com/rilldata/rill/runtime/drivers"
"google.golang.org/protobuf/types/known/timestamppb"
)
type ColumnTimeRange struct {
TableName string
ColumnName string
Result *runtimev1.TimeRangeSummary
}
var _ runtime.Query = &ColumnTimeRange{}
func (q *ColumnTimeRange) Key() string {
return fmt.Sprintf("ColumnTimeRange:%s:%s", q.TableName, q.ColumnName)
}
func (q *ColumnTimeRange) Deps() []string {
return []string{q.TableName}
}
func (q *ColumnTimeRange) MarshalResult() any {
return q.Result
}
func (q *ColumnTimeRange) UnmarshalResult(v any) error {
res, ok := v.(*runtimev1.TimeRangeSummary)
if !ok {
return fmt.Errorf("ColumnTimeRange: mismatched unmarshal input")
}
q.Result = res
return nil
}
func (q *ColumnTimeRange) Resolve(ctx context.Context, rt *runtime.Runtime, instanceID string, priority int) error {
olap, err := rt.OLAP(ctx, instanceID)
if err != nil {
return err
}
switch olap.Dialect() {
case drivers.DialectDuckDB:
return q.resolveDuckDB(ctx, olap, priority)
case drivers.DialectDruid:
return q.resolveDruid(ctx, olap, priority)
default:
return fmt.Errorf("not available for dialect '%s'", olap.Dialect())
}
}
func (q *ColumnTimeRange) resolveDuckDB(ctx context.Context, olap drivers.OLAPStore, priority int) error {
rangeSQL := fmt.Sprintf(
"SELECT min(%[1]s) as \"min\", max(%[1]s) as \"max\", max(%[1]s) - min(%[1]s) as \"interval\" FROM %[2]s",
safeName(q.ColumnName),
safeName(q.TableName),
)
rows, err := olap.Execute(ctx, &drivers.Statement{
Query: rangeSQL,
Priority: priority,
})
if err != nil {
return err
}
defer rows.Close()
if rows.Next() {
summary := &runtimev1.TimeRangeSummary{}
rowMap := make(map[string]any)
err = rows.MapScan(rowMap)
if err != nil {
return err
}
if v := rowMap["min"]; v != nil {
minTime, ok := v.(time.Time)
if !ok {
return fmt.Errorf("not a timestamp column")
}
summary.Min = timestamppb.New(minTime)
summary.Max = timestamppb.New(rowMap["max"].(time.Time))
summary.Interval, err = handleDuckDBInterval(rowMap["interval"])
if err != nil {
return err
}
}
q.Result = summary
return nil
}
err = rows.Err()
if err != nil {
return err
}
return errors.New("no rows returned")
}
func handleDuckDBInterval(interval any) (*runtimev1.TimeRangeSummary_Interval, error) {
switch i := interval.(type) {
case duckdb.Interval:
result := new(runtimev1.TimeRangeSummary_Interval)
result.Days = i.Days
result.Months = i.Months
result.Micros = i.Micros
return result, nil
case int64:
// for date type column interval is difference in num days for two dates
result := new(runtimev1.TimeRangeSummary_Interval)
result.Days = int32(i)
return result, nil
}
return nil, fmt.Errorf("cannot handle interval type %T", interval)
}
func (q *ColumnTimeRange) resolveDruid(ctx context.Context, olap drivers.OLAPStore, priority int) error {
rangeSQL := fmt.Sprintf(
"SELECT min(%[1]s) as \"min\", max(%[1]s) as \"max\", timestampdiff(SECOND, min(%[1]s), max(%[1]s)) as \"interval\" FROM %[2]s",
safeName(q.ColumnName),
safeName(q.TableName),
)
rows, err := olap.Execute(ctx, &drivers.Statement{
Query: rangeSQL,
Priority: priority,
})
if err != nil {
return err
}
defer rows.Close()
if rows.Next() {
summary := &runtimev1.TimeRangeSummary{}
rowMap := make(map[string]any)
err = rows.MapScan(rowMap)
if err != nil {
return err
}
if v := rowMap["min"]; v != nil {
minTime, ok := v.(time.Time)
if !ok {
return fmt.Errorf("not a timestamp column")
}
summary.Min = timestamppb.New(minTime)
summary.Max = timestamppb.New(rowMap["max"].(time.Time))
interval, ok := rowMap["interval"].(int64)
if !ok {
return fmt.Errorf("cannot handle interval type %T", interval)
}
summary.Interval = &runtimev1.TimeRangeSummary_Interval{
Micros: interval * int64(time.Second) / int64(time.Microsecond),
}
}
q.Result = summary
return nil
}
err = rows.Err()
if err != nil {
return err
}
return errors.New("no rows returned")
}