-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathquery.go
156 lines (137 loc) · 4.25 KB
/
query.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
package zenodb
import (
"context"
"errors"
"fmt"
"time"
"github.com/getlantern/bytemap"
"github.com/getlantern/zenodb/common"
"github.com/getlantern/zenodb/core"
"github.com/getlantern/zenodb/encoding"
"github.com/getlantern/zenodb/planner"
"github.com/getlantern/zenodb/sql"
)
var (
ErrOutOfMemory = errors.New("out of memory")
)
func (db *DB) Query(sqlString string, isSubQuery bool, subQueryResults [][]interface{}, includeMemStore bool) (core.FlatRowSource, error) {
q, err := sql.Parse(sqlString)
if err != nil {
return nil, err
}
if q.ForceFresh {
db.log.Debug("Query requires fresh results, including mem store")
includeMemStore = true
}
opts := &planner.Opts{
GetTable: func(table string, outFields func(tableFields core.Fields) (core.Fields, error)) (planner.Table, error) {
return db.getQueryable(table, outFields, includeMemStore)
},
Now: db.now,
IsSubQuery: isSubQuery,
SubQueryResults: subQueryResults,
}
if db.opts.Passthrough {
opts.QueryCluster = func(ctx context.Context, sqlString string, isSubQuery bool, subQueryResults [][]interface{}, unflat bool, onFields core.OnFields, onRow core.OnRow, onFlatRow core.OnFlatRow) (interface{}, error) {
return db.queryCluster(ctx, sqlString, isSubQuery, subQueryResults, includeMemStore, unflat, onFields, onRow, onFlatRow)
}
}
plan, err := planner.Plan(sqlString, opts)
if err != nil {
return nil, err
}
db.log.Debugf("\n------------ Query Plan ------------\n\n%v\n\n%v\n----------- End Query Plan ----------", sqlString, core.FormatSource(plan))
return plan, nil
}
func (db *DB) getQueryable(table string, outFields func(tableFields core.Fields) (core.Fields, error), includeMemStore bool) (*queryable, error) {
t := db.getTable(table)
if t == nil {
return nil, fmt.Errorf("Table %v not found", table)
}
if t.Virtual {
return nil, fmt.Errorf("Table %v is virtual and cannot be queried", table)
}
until := encoding.RoundTimeUp(db.clock.Now(), t.Resolution)
asOf := encoding.RoundTimeUp(until.Add(-1*t.RetentionPeriod), t.Resolution)
fields := t.getFields()
out, err := outFields(fields)
if err != nil {
return nil, err
}
if out == nil {
out = t.getFields()
}
return &queryable{db, t, out, asOf, until, includeMemStore}, nil
}
func MetaDataFor(source core.FlatRowSource, fields core.Fields) *common.QueryMetaData {
return &common.QueryMetaData{
FieldNames: fields.Names(),
AsOf: source.GetAsOf(),
Until: source.GetUntil(),
Resolution: source.GetResolution(),
Plan: core.FormatSource(source),
}
}
type queryable struct {
db *DB
t *table
fields core.Fields
asOf time.Time
until time.Time
includeMemStore bool
}
func (q *queryable) GetGroupBy() []core.GroupBy {
return q.t.GroupBy
}
func (q *queryable) GetResolution() time.Duration {
return q.t.Resolution
}
func (q *queryable) GetAsOf() time.Time {
return q.asOf
}
func (q *queryable) GetUntil() time.Time {
return q.until
}
func (q *queryable) GetPartitionBy() []string {
return q.t.PartitionBy
}
func (q *queryable) String() string {
return q.t.Name
}
func (q *queryable) Iterate(ctx context.Context, onFields core.OnFields, onRow core.OnRow) (interface{}, error) {
// We report all fields from the table
err := onFields(q.fields)
if err != nil {
return nil, err
}
if len(q.fields) == 0 {
return nil, errors.New("No fields found!")
}
i := 1
// When iterating, as an optimization, we read only the needed fields (not
// all table fields).
highWaterMarks, err := q.t.iterate(ctx, q.fields, q.includeMemStore, func(key bytemap.ByteMap, vals []encoding.Sequence) (bool, error) {
if i%1000 == 0 {
// every 1000 rows, check and cap memory size
if !q.db.capMemorySize(false) {
q.t.log.Error("Returning ErrOutOfMemory")
return false, ErrOutOfMemory
}
}
i++
return onRow(key, vals)
})
if err != nil {
q.t.log.Errorf("Error on iterating: %v", err)
}
numSuccessfulPartitions := 0
if err == nil {
numSuccessfulPartitions = 1
}
return &common.QueryStats{
NumPartitions: 1,
NumSuccessfulPartitions: numSuccessfulPartitions,
LowestHighWaterMark: common.TimeToMillis(highWaterMarks.LowestTS()),
HighestHighWaterMark: common.TimeToMillis(highWaterMarks.HighestTS()),
}, err
}