-
Notifications
You must be signed in to change notification settings - Fork 45
/
sql_to_datastore.go
819 lines (747 loc) · 24.1 KB
/
sql_to_datastore.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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
package datastore
import (
"database/sql/driver"
"encoding/json"
"fmt"
"strings"
"time"
"cloud.google.com/go/datastore"
u "github.com/araddon/gou"
"golang.org/x/net/context"
"github.com/araddon/qlbridge/datasource"
"github.com/araddon/qlbridge/exec"
"github.com/araddon/qlbridge/expr"
"github.com/araddon/qlbridge/lex"
"github.com/araddon/qlbridge/plan"
"github.com/araddon/qlbridge/rel"
"github.com/araddon/qlbridge/schema"
"github.com/araddon/qlbridge/value"
"github.com/araddon/qlbridge/vm"
)
var (
// DefaultLimit LIMIT on DataStore Queries
DefaultLimit = 1000
// ensure we implement appropriate interfaces
_ schema.Conn = (*SQLToDatstore)(nil)
_ plan.SourcePlanner = (*SQLToDatstore)(nil)
_ exec.ExecutorSource = (*SQLToDatstore)(nil)
_ schema.ConnMutation = (*SQLToDatstore)(nil)
)
// SQLToDatstore transforms a Sql AST statement into a Google Datastore request
// - a dialect translator (sql -> datastore query languages)
// - matches Task interface to operate in qlbridge exec interfaces
type SQLToDatstore struct {
*exec.TaskBase
resp *ResultReader
tbl *schema.Table
p *plan.Source
sel *rel.SqlSelect
stmt rel.SqlStatement
schema *schema.Schema
dsCtx context.Context
dsClient *datastore.Client // Stateful client for usage in request
dsq *datastore.Query // The ds query interpreted from sql
hasMultiValue bool // Multi-Value vs Single-Value aggs
hasSingleValue bool // single value agg
partition *schema.Partition // current partition for this request
needsPolyFill bool // do we request that features be polyfilled?
}
// NewSQLToDatstore Create a new translator to re-write a SQL AST query
// into google data store
func NewSQLToDatstore(table *schema.Table, cl *datastore.Client, ctx context.Context) *SQLToDatstore {
m := &SQLToDatstore{
tbl: table,
schema: table.Schema,
dsCtx: ctx,
dsClient: cl,
}
u.Debugf("create sqltodatasource %p", m)
return m
}
func (m *SQLToDatstore) query(req *rel.SqlSelect) (*ResultReader, error) {
// Create our google data store query and we will walk
// our ast modifying it
u.Debugf("query: %v", m.tbl.NameOriginal)
m.dsq = datastore.NewQuery(m.tbl.NameOriginal)
var err error
m.sel = req
limit := req.Limit
if limit == 0 {
limit = DefaultLimit
}
if req.Where != nil {
err = m.WalkWhereNode(req.Where.Expr)
if err != nil {
u.Warnf("Could Not evaluate Where Node %s %v", req.Where.Expr.String(), err)
return nil, err
}
}
// Evaluate the Select columns
//err = m.WalkSelectList()
// if err != nil {
// u.Warnf("Could Not evaluate Columns/Aggs %s %v", req.Columns.String(), err)
// return nil, err
// }
// if len(req.GroupBy) > 0 {
// err = m.WalkGroupBy()
// if err != nil {
// u.Warnf("Could Not evaluate GroupBys %s %v", req.GroupBy.String(), err)
// return nil, err
// }
// }
//u.Debugf("OrderBy? %v", len(m.sel.OrderBy))
if len(m.sel.OrderBy) > 0 {
for _, col := range m.sel.OrderBy {
// We really need to look at any funcs? walk this out
switch col.Order {
case "ASC":
m.dsq = m.dsq.Order(fmt.Sprintf("%q", col.Key()))
case "DESC":
m.dsq = m.dsq.Order(fmt.Sprintf("-%q", col.Key()))
default:
m.dsq = m.dsq.Order(fmt.Sprintf("%q", col.Key()))
}
}
}
resultReader := NewResultReader(m)
m.resp = resultReader
//resultReader.Finalize()
return resultReader, nil
}
func (m *SQLToDatstore) getEntity(req *rel.SqlSelect) (*Row, error) {
m.dsq = datastore.NewQuery(m.tbl.NameOriginal)
if req.Where != nil {
err := m.WalkWhereNode(req.Where.Expr)
if err != nil {
u.Warnf("Could Not evaluate Where Node %s %v", req.Where.Expr.String(), err)
return nil, err
}
}
var rows []*Row
keys, err := m.dsClient.GetAll(m.dsCtx, m.dsq, &rows)
if err != nil {
return nil, err
}
if len(keys) == 1 {
rows[0].key = keys[0]
return rows[0], nil
}
return nil, fmt.Errorf("expected one row got %v", len(rows))
}
// WalkSourceSelect part of planner interface for source.
func (m *SQLToDatstore) WalkSourceSelect(planner plan.Planner, p *plan.Source) (plan.Task, error) {
//u.Debugf("VisitSourceSelect(): %T %#v", visitor, visitor)
m.p = p
return nil, nil
}
// WalkExecSource part of planner interface allowing sources to do their own execution
// planning.
func (m *SQLToDatstore) WalkExecSource(p *plan.Source) (exec.Task, error) {
if p.Stmt == nil {
return nil, fmt.Errorf("Plan did not include Sql Statement?")
}
if p.Stmt.Source == nil {
return nil, fmt.Errorf("Plan did not include Sql Select Statement?")
}
if m.TaskBase == nil {
m.TaskBase = exec.NewTaskBase(p.Context())
}
if m.p == nil {
u.Debugf("custom? %v", p.Custom)
m.p = p
if p.Custom.Bool("poly_fill") {
m.needsPolyFill = true
}
if partitionId := p.Custom.String("partition"); partitionId != "" {
if p.Tbl.Partition != nil {
for _, pt := range p.Tbl.Partition.Partitions {
if pt.Id == partitionId {
//u.Debugf("partition: %s %#v", partitionId, pt)
m.partition = pt
if pt.Left == "" {
//m.filter = bson.M{p.Tbl.Partition.Keys[0]: bson.M{"$lt": pt.Right}}
} else if pt.Right == "" {
//m.filter = bson.M{p.Tbl.Partition.Keys[0]: bson.M{"$gte": pt.Left}}
} else {
}
}
}
}
}
}
u.Debugf("WalkExecSource(): %T %#v", p, p)
u.Debugf("%p walkexec: %#v", m, m.TaskBase)
m.Ctx = p.Context()
m.TaskBase = exec.NewTaskBase(m.Ctx)
reader, err := m.query(p.Stmt.Source)
if err != nil {
return nil, nil
}
return reader, nil
}
// CreateMutator interface allowing a connection for mutation.
func (m *SQLToDatstore) CreateMutator(pc interface{}) (schema.ConnMutator, error) {
if ctx, ok := pc.(*plan.Context); ok && ctx != nil {
m.TaskBase = exec.NewTaskBase(ctx)
m.stmt = ctx.Stmt
return m, nil
}
return nil, fmt.Errorf("Expected *plan.Context but got %T", pc)
}
// Put interface for Upsert.Put()
func (m *SQLToDatstore) Put(ctx context.Context, key schema.Key, val interface{}) (schema.Key, error) {
if key == nil {
u.Debugf("didn't have key? %v", val)
//return nil, fmt.Errorf("Must have key for updates in DataStore")
}
if m.schema == nil {
u.Warnf("must have schema")
return nil, fmt.Errorf("Must have schema for updates in DataStore")
}
/*
TODO:
we need to (optionally?) wrap this in a
transaction and do a read, write in transaction
*/
var dskey *datastore.Key
props := make([]datastore.Property, 0)
cols := m.tbl.Columns()
var row []driver.Value
colNames := make(map[string]int, len(cols))
for i, colName := range cols {
colNames[colName] = i
//u.Debugf("col.name=%v col.as=%s", col.Name, col.As)
}
curRow := make([]driver.Value, len(cols))
if key != nil {
dskey = datastore.NameKey(m.tbl.NameOriginal, fmt.Sprintf("%v", key.Key()), nil)
}
var sel *rel.SqlSelect
//u.Debugf("stmt: %T", m.stmt)
switch sqlReq := m.stmt.(type) {
case *rel.SqlInsert:
cols = sqlReq.ColumnNames()
//u.Infof("cols: %v", cols)
case *rel.SqlUpdate:
// need to fetch first?
sel = sqlReq.SqlSelect()
case *rel.SqlUpsert:
sel = sqlReq.SqlSelect()
}
if sel != nil {
u.Debugf("fetch first w Select: %s", sel)
entity, err := m.getEntity(sel)
if err != nil {
u.Errorf("could not retrieve current entity state for update? %v", err)
return nil, err
}
if len(entity.props) > 0 {
curRow = entity.Vals(colNames)
} else {
u.Warnf("should only have one to update in Put(): %v %#v", len(entity.props), entity)
}
}
switch valT := val.(type) {
case []driver.Value:
row = valT
//u.Infof("row len=%v fieldlen=%v col len=%v", len(row), len(m.tbl.Fields), len(cols))
for _, f := range m.tbl.Fields {
for i, colName := range cols {
if f.Name == colName {
if len(row) <= i-1 {
u.Errorf("bad column count? %d vs %d col: %+v", len(row), i, f)
} else {
//u.Debugf("col info? %d vs %d col: %+v", len(row), i, f)
switch val := row[i].(type) {
case string, []byte, int, int64, bool, time.Time:
//u.Debugf("PUT field: i=%d col=%s row[i]=%v T:%T", i, colName, row[i], row[i])
props = append(props, datastore.Property{Name: f.Name, Value: val})
case []value.Value:
by, err := json.Marshal(val)
if err != nil {
u.Errorf("Error converting field %v err=%v", val, err)
}
//u.Debugf("PUT field: i=%d col=%s row[i]=%v T:%T", i, colName, string(by), by)
props = append(props, datastore.Property{Name: f.Name, Value: by})
default:
u.Warnf("unsupported conversion: %T %v", val, val)
props = append(props, datastore.Property{Name: f.Name, Value: val})
}
}
break
}
}
}
// Create the key by position? HACK
dskey = datastore.NameKey(m.tbl.NameOriginal, fmt.Sprintf("%v", row[0]), nil)
case map[string]driver.Value:
for i, f := range m.tbl.Fields {
for colName, driverVal := range valT {
if f.Name == colName {
//u.Debugf("PUT field: i=%d col=%s val=%v T:%T cur:%v", i, colName, driverVal, driverVal, curRow[i])
switch val := driverVal.(type) {
case string, []byte, int, int64, bool:
curRow[i] = val
case time.Time:
curRow[i] = val
case []value.Value:
by, err := json.Marshal(val)
if err != nil {
u.Errorf("Error converting field %v err=%v", val, err)
}
curRow[i] = by
default:
u.Warnf("unsupported conversion: %T %v", val, val)
}
break
}
}
//u.Infof(" %v curRow? %d %#v", f.Name, len(curRow), curRow)
//u.Debugf("%d writing %-10s %T\t%v", i, f.Name, curRow[i], curRow[i])
props = append(props, datastore.Property{Name: f.Name, Value: curRow[i]})
}
default:
u.Warnf("unsupported type: %T %#v", val, val)
return nil, fmt.Errorf("Was not []driver.Value? %T", val)
}
//u.Debugf("has key? sourcekey: %v dskey:%#v", key, dskey)
//u.Debugf("dskey: %s table=%s", dskey, m.tbl.NameOriginal)
// u.Debugf("props: %v", props)
// for i, prop := range props {
// u.Debugf("i %d prop %#v", i, prop)
// }
pl := datastore.PropertyList(props)
dskey, err := m.dsClient.Put(m.dsCtx, dskey, &pl)
if err != nil {
u.Errorf("could not save? %v", err)
return nil, err
}
newKey := datasource.NewKeyCol("id", dskey.String())
return newKey, nil
}
func (m *SQLToDatstore) PutMulti(ctx context.Context, keys []schema.Key, src interface{}) ([]schema.Key, error) {
return nil, fmt.Errorf("not implemented")
}
func (m *SQLToDatstore) Delete(key driver.Value) (int, error) {
dskey := datastore.NameKey(m.tbl.NameOriginal, fmt.Sprintf("%v", key), nil)
u.Infof("dskey: %s table=%s", dskey, m.tbl.NameOriginal)
err := m.dsClient.Delete(m.dsCtx, dskey)
if err != nil {
u.Errorf("could not delete? %v", err)
return 0, err
}
return 1, nil
}
func (m *SQLToDatstore) DeleteExpression(pln interface{}, where expr.Node) (int, error) {
delKey := datasource.KeyFromWhere(where)
if delKey != nil {
return m.Delete(delKey.Key())
}
return 0, fmt.Errorf("Could not delete with that where expression: %s", where)
}
func (m *SQLToDatstore) eval(arg expr.Node) (value.Value, bool) {
switch arg := arg.(type) {
case *expr.NumberNode, *expr.StringNode:
return vm.Eval(nil, arg)
case *expr.IdentityNode:
return value.NewStringValue(arg.Text), true
}
return nil, false
}
// WalkWhereNode an expression, and its AND logic to create an appropriately
// request for google datastore queries
//
// Limititations of Google Datastore
// - https://cloud.google.com/datastore/docs/concepts/queries#Datastore_Restrictions_on_queries
// - no OR filters
func (m *SQLToDatstore) WalkWhereNode(cur expr.Node) error {
//u.Debugf("WalkWhereNode: %s", cur)
switch curNode := cur.(type) {
case *expr.NumberNode, *expr.StringNode:
nodeVal, ok := vm.Eval(nil, cur)
if !ok {
u.Warnf("not ok %v", cur)
return fmt.Errorf("could not evaluate: %v", cur.String())
}
u.Infof("nodeval? %v", nodeVal)
return nil
// What do we do here?
case *expr.BinaryNode:
return m.walkFilterBinary(curNode)
case *expr.TriNode: // Between
return fmt.Errorf("Between and other Tri-Node ops not implemented")
case *expr.UnaryNode:
return fmt.Errorf("Not implemented urnary function: %v", curNode.String())
case *expr.FuncNode:
return fmt.Errorf("Not implemented function: %v", curNode.String())
case *expr.IdentityNode:
u.Warnf("what uses identity node in Where? %v", curNode.String())
return fmt.Errorf("Not implemented identity node: %v", curNode.String())
case *expr.ArrayNode:
return fmt.Errorf("Not implemented expr.ArrayNode: %v", curNode.String())
default:
u.Warnf("eh? %T %#v", cur, cur)
return fmt.Errorf("Not implemented node: %v", curNode.String())
}
return nil
}
// Walk Binary Node: convert to mostly Filters if possible
//
// x = y => x = y
// x != y => there are special limits in Google Datastore, must only have one unequal filter
// x like "list%" = prefix filter in
//
// TODO: Poly Fill features
// x like "%list%"
// - ancestor filters?
func (m *SQLToDatstore) walkFilterBinary(node *expr.BinaryNode) error {
// How do we detect if this is a prefix query? Probably would
// have a column-level flag on schema?
lhval, lhok := m.eval(node.Args[0])
rhval, rhok := vm.Eval(nil, node.Args[1])
if !lhok || !rhok {
u.Warnf("not ok: %v l:%v r:%v", node, lhval, rhval)
return fmt.Errorf("could not evaluate: %v", node.String())
}
u.Debugf("walkBinary: %v l:%v r:%v %T %T", node, lhval, rhval, lhval, rhval)
switch node.Operator.T {
case lex.TokenLogicAnd:
// AND is assumed by datastore
for _, arg := range node.Args {
err := m.WalkWhereNode(arg)
if err != nil {
u.Errorf("could not evaluate where nodes? %v %s", err, arg)
return fmt.Errorf("could not evaluate: %s", arg.String())
}
}
case lex.TokenLogicOr:
return fmt.Errorf("DataStore does not implement OR: %v", node.String())
case lex.TokenEqual, lex.TokenEqualEqual:
//u.Debugf("dsq: %p", m.dsq)
m.dsq = m.dsq.Filter(fmt.Sprintf("%q =", lhval.ToString()), rhval.Value())
case lex.TokenNE:
// WARNING: datastore only allows 1, warn?
m.dsq = m.dsq.Filter(fmt.Sprintf("%q !=", lhval.ToString()), rhval.Value())
case lex.TokenLE:
m.dsq = m.dsq.Filter(fmt.Sprintf("%q <=", lhval.ToString()), rhval.Value())
case lex.TokenLT:
m.dsq = m.dsq.Filter(fmt.Sprintf("%q <", lhval.ToString()), rhval.Value())
case lex.TokenGE:
m.dsq = m.dsq.Filter(fmt.Sprintf("%q >=", lhval.ToString()), rhval.Value())
case lex.TokenGT:
m.dsq = m.dsq.Filter(fmt.Sprintf("%q >", lhval.ToString()), rhval.Value())
case lex.TokenLike:
// Ancestors support some type of prefix query?
// this only works on String columns?
lhs := lhval.ToString()
if strings.HasPrefix(lhs, "%") {
// Need to polyFill? Or Error
return fmt.Errorf("Google Datastore does not support %v prefix for %v", "%", lhs)
}
m.dsq = m.dsq.Filter(fmt.Sprintf("%q >=", lhs), rhval.Value())
default:
u.Warnf("not implemented: %v", node.Operator)
return fmt.Errorf("not implemented %v", node.String())
}
return nil
}
/*
func (m *SQLToDatstore) walkFilterTri(node *expr.TriNode, q *bson.M) (value.Value, error) {
arg1val, aok := vm.Eval(nil, node.Args[0])
//u.Debugf("arg1? %v ok?%v", arg1val, aok)
arg2val, bok := vm.Eval(nil, node.Args[1])
arg3val, cok := vm.Eval(nil, node.Args[2])
u.Debugf("walkTri: %v %v %v %v", node, arg1val, arg2val, arg3val)
if !aok || !bok || !cok {
return nil, fmt.Errorf("Could not evaluate args: %v", node.String())
}
u.Debugf("walkTri: %v %v %v %v", node, arg1val, arg2val, arg3val)
switch node.Operator.T {
case lex.TokenBetween:
*q = bson.M{arg1val.ToString(): bson.M{"$gte": arg2val.Value(), "$lte": arg3val.Value()}}
default:
u.Warnf("not implemented ")
}
if q != nil {
return nil, nil
}
return nil, fmt.Errorf("not implemented")
}
// Mutli Arg expressions:
//
// year IN (1990,1992) =>
//
func (m *SQLToDatstore) walkMultiFilter(node *expr.MultiArgNode, q *bson.M) (value.Value, error) {
// First argument must be field name in this context
fldName := node.Args[0].String()
u.Debugf("walkMulti: %v", node.String())
switch node.Operator.T {
case lex.TokenIN:
//q = bson.M{"range": bson.M{arg1val.ToString(): bson.M{"gte": arg2val.ToString(), "lte": arg3val.ToString()}}}
terms := make([]interface{}, len(node.Args)-1)
*q = bson.M{fldName: bson.M{"$in": terms}}
for i := 1; i < len(node.Args); i++ {
// Do we eval here?
v, ok := vm.Eval(nil, node.Args[i])
if ok {
u.Debugf("in? %T %v value=%v", v, v, v.Value())
terms[i-1] = v.Value()
} else {
u.Warnf("could not evaluate arg: %v", node.Args[i])
}
}
default:
u.Warnf("not implemented %v", node.String())
return nil, fmt.Errorf("Not implemented: %v", node.String())
}
if q != nil {
u.Debug(string(u.JsonHelper(*q).PrettyJson()))
return nil, nil
}
return nil, fmt.Errorf("Uknown Error")
}
// Take an expression func, ensure we don't do runtime-checking (as the function)
// doesn't really exist, then map that function to an ES Filter
//
// exists(fieldname)
// regex(fieldname,value)
//
func (m *SQLToDatstore) walkFilterFunc(node *expr.FuncNode, q *bson.M) (value.Value, error) {
switch funcName := strings.ToLower(node.Name); funcName {
case "exists", "missing":
op := true
if funcName == "missing" {
op = false
}
// { field: { $exists: <boolean> } }
fieldName := ""
if len(node.Args) != 1 {
return nil, fmt.Errorf("Invalid func")
}
switch node.Args[0].NodeType() {
case expr.IdentityNodeType:
fieldName = node.Args[0].String()
default:
val, ok := eval(node.Args[0])
if !ok {
u.Errorf("Must be valid: %v", node.String())
} else {
fieldName = val.ToString()
}
}
*q = bson.M{fieldName: bson.M{"$exists": op}}
case "regex":
// user_id regex("%bc%")
// db.users.find( { user_id: /bc/ } )
if len(node.Args) < 2 {
return nil, fmt.Errorf(`Invalid func regex: regex(fieldname,"/regvalue/i")`)
}
fieldVal, ok := eval(node.Args[0])
if !ok {
u.Errorf("Must be valid: %v", node.String())
return value.ErrValue, fmt.Errorf(`Invalid func regex: regex(fieldname,"/regvalue/i")`)
}
regexval, ok := eval(node.Args[0])
if !ok {
u.Errorf("Must be valid: %v", node.String())
return value.ErrValue, fmt.Errorf(`Invalid func regex: regex(fieldname,"/regvalue/i")`)
}
*q = bson.M{fieldVal.ToString(): bson.M{"$regex": regexval.ToString()}}
default:
u.Warnf("not implemented ")
}
u.Debugf("func: %v", q)
if q != nil {
return nil, nil
}
return nil, fmt.Errorf("not implemented %v", node.String())
}
// Take an expression func, ensure we don't do runtime-checking (as the function)
// doesn't really exist, then map that function to an ES aggregation
//
// min, max, avg, sum, cardinality, terms
//
// Single Value Aggregates:
// min, max, avg, sum, cardinality, count
//
// MultiValue aggregats:
// terms, ??
//
func (m *SQLToDatstore) walkAggFunc(node *expr.FuncNode) (q bson.M, _ error) {
switch funcName := strings.ToLower(node.Name); funcName {
case "max", "min", "avg", "sum", "cardinality":
m.hasSingleValue = true
if len(node.Args) != 1 {
return nil, fmt.Errorf("Invalid func")
}
val, ok := eval(node.Args[0])
if !ok {
u.Errorf("Must be valid: %v", node.String())
}
// "min_price" : { "min" : { "field" : "price" } }
q = bson.M{funcName: bson.M{"field": val.ToString()}}
case "terms":
m.hasMultiValue = true
// "products" : { "terms" : {"field" : "product", "size" : 5 }}
if len(node.Args) == 0 || len(node.Args) > 2 {
return nil, fmt.Errorf("Invalid terms function terms(field,10) OR terms(field)")
}
val, ok := eval(node.Args[0])
if !ok {
u.Errorf("Must be valid: %v", node.String())
}
if len(node.Args) >= 2 {
size, ok := vm.Eval(nil, node.Args[1])
if !ok {
u.Errorf("Must be valid size: %v", node.Args[1].String())
}
// "products" : { "terms" : {"field" : "product", "size" : 5 }}
q = bson.M{funcName: bson.M{"field": val.ToString(), "size": size.Value()}}
} else {
q = bson.M{funcName: bson.M{"field": val.ToString()}}
}
case "count":
m.hasSingleValue = true
u.Debugf("how do we want to use count(*)? hit.hits? or exists()?")
val, ok := eval(node.Args[0])
if !ok {
u.Errorf("Must be valid: %v", node.String())
return nil, fmt.Errorf("Invalid argument: %v", node.String())
}
if val.ToString() == "*" {
return nil, nil
} else {
return bson.M{"exists": bson.M{"field": val.ToString()}}, nil
}
default:
u.Warnf("not implemented ")
}
u.Debugf("func: %v", q)
if q != nil {
return q, nil
}
return nil, fmt.Errorf("not implemented")
}
func eval(cur expr.Node) (value.Value, bool) {
switch curNode := cur.(type) {
case *expr.IdentityNode:
return value.NewStringValue(curNode.Text), true
case *expr.StringNode:
return value.NewStringValue(curNode.Text), true
default:
u.Errorf("unrecognized T:%T %v", cur, cur)
}
return value.NilValueVal, false
}
*/
/*
// Aggregations from the <select_list>
//
// SELECT <select_list> FROM ... WHERE
//
func (m *SQLToDatstore) WalkSelectList() error {
m.aggs = bson.M{}
for i := len(m.sel.Columns) - 1; i >= 0; i-- {
col := m.sel.Columns[i]
//u.Debugf("i=%d of %d %v %#v ", i, len(m.sel.Columns), col.Key(), col)
if col.Expr != nil {
switch curNode := col.Expr.(type) {
// case *expr.NumberNode:
// return nil, value.NewNumberValue(curNode.Float64), nil
// case *expr.BinaryNode:
// return m.walkBinary(curNode)
// case *expr.TriNode: // Between
// return m.walkTri(curNode)
// case *expr.UnaryNode:
// //return m.walkUnary(curNode)
// u.Warnf("not implemented: %#v", curNode)
case *expr.FuncNode:
// All Func Nodes are Aggregates
esm, err := m.WalkAggs(curNode)
if err == nil && len(esm) > 0 {
m.aggs[col.As] = esm
} else if err != nil {
u.Error(err)
return err
}
//u.Debugf("esm: %v:%v", col.As, esm)
//u.Debugf(curNode.String())
// case *expr.MultiArgNode:
// return m.walkMulti(curNode)
// case *expr.IdentityNode:
// return nil, value.NewStringValue(curNode.Text), nil
// case *expr.StringNode:
// return nil, value.NewStringValue(curNode.Text), nil
default:
//u.Debugf("likely a projection, not agg T:%T %v", curNode, curNode)
//panic("Unrecognized node type")
}
}
}
return nil
}
// Aggregations from the <select_list>
//
// WHERE .. GROUP BY x,y,z
//
func (m *SqlToDatstore) WalkGroupBy() error {
for i, col := range m.sel.GroupBy {
if col.Expr != nil {
u.Infof("Walk group by %s %T", col.Expr.String(), col.Expr)
switch col.Expr.(type) {
case *expr.IdentityNode, *expr.FuncNode:
esm := bson.M{}
_, err := m.WalkNode(col.Expr, &esm)
fld := strings.Replace(expr.FindIdentityField(col.Expr), ".", "", -1)
u.Infof("gb: %s %s", fld, u.JsonHelper(esm).PrettyJson())
if err == nil {
if len(m.innergb) > 0 {
esm["aggs"] = bson.M{fmt.Sprintf("group_by_%d", i): m.innergb}
// esm["aggs"] = bson.M{"group_by_" + fld: m.innergb}
} else {
esm = esm
}
m.innergb = esm
u.Infof("esm: %v", esm)
} else {
u.Error(err)
return err
}
}
}
}
m.groupby = bson.M{"aggs": bson.M{"group_by": m.innergb}}
return nil
}
// WalkAggs() aggregate expressions when used ast part of <select_list>
// - For Aggregates (functions) it builds aggs
// - For Projectsion (non-functions) it does nothing, that will be done later during projection
func (m *SqlToDatstore) WalkAggs(cur expr.Node) (q bson.M, _ error) {
switch curNode := cur.(type) {
// case *expr.NumberNode:
// return nil, value.NewNumberValue(curNode.Float64), nil
// case *expr.BinaryNode:
// return m.walkBinary(curNode)
// case *expr.TriNode: // Between
// return m.walkTri(curNode)
// case *expr.UnaryNode:
// //return m.walkUnary(curNode)
// u.Warnf("not implemented: %#v", curNode)
case *expr.FuncNode:
return m.walkAggFunc(curNode)
// case *expr.MultiArgNode:
// return m.walkMulti(curNode)
// case *expr.IdentityNode:
// return nil, value.NewStringValue(curNode.Text), nil
// case *expr.StringNode:
// return nil, value.NewStringValue(curNode.Text), nil
default:
u.Debugf("likely a projection, not agg T:%T %v", cur, cur)
//panic("Unrecognized node type")
}
// if cur.Negate {
// q = bson.M{"not": q}
// }
return q, nil
}
*/