Skip to content

Commit

Permalink
mongo projection push down support left right comparisons
Browse files Browse the repository at this point in the history
  • Loading branch information
Aaron Raddon committed Nov 8, 2017
1 parent 7cc7f09 commit ef7cd20
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 64 deletions.
70 changes: 14 additions & 56 deletions backends/mongo/mgo_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -585,62 +585,20 @@ func TestSelectAggsGroupBy(t *testing.T) {
})
}
*/
func TestSelectWhereEqual(t *testing.T) {
/*
data := struct {
Title string
Count int
Deleted bool
Updated time.Time
}{}
validateQuerySpec(t, tu.QuerySpec{
Sql: "select title, count, deleted, updated " +
"from article WHERE Count = 22;",
ExpectRowCt: 1,
ValidateRowData: func() {
//u.Infof("%v", data)
assert.True(t, data.Title != "", "%v", data)
//assert.True(t, data.Language == "Go", "%v", data)
},
RowData: &data,
})
// Test when we have compound where clause
validateQuerySpec(t, tu.QuerySpec{
Sql: "select `actor`, `repository.name`, `repository.stargazers_count`, `repository.language` " +
" from github_watch where " +
"`repository.language` == \"Go\" AND `repository.forks_count` > 1000;",
ExpectRowCt: 20,
ValidateRowData: func() {
//u.Infof("%v", data)
assert.True(t, data.Language == "Go", "%v", data)
assert.True(t, data.Stars > 1000, "must have filterd by forks: %v", data)
assert.True(t, data.Actor != "", "%v", data)
},
RowData: &data,
})
return
// TODO: This isn't working yet bc the nested 3 where clauses
validateQuerySpec(t, tu.QuerySpec{
Sql: `
SELECT
actor, repository.name, repository.stargazers_count, repository.language
FROM github_watch
WHERE
repository.language = "Go"
AND repository.forks_count > 1000
AND repository.description NOT LIKE "docker";`,
ExpectRowCt: 20,
ValidateRowData: func() {
//u.Infof("%v", data)
assert.True(t, data.Language == "Go", "%v", data)
assert.True(t, data.Stars > 1000, "must have filterd by forks: %v", data)
assert.True(t, data.Actor != "", "%v", data)
},
RowData: &data,
})
*/
func TestSelectWhereCompare(t *testing.T) {
data := struct {
Title string
Count int
Deleted bool
}{}
validateQuerySpec(t, tu.QuerySpec{
Sql: "select title, count, deleted from article WHERE `author` != `title`",
ExpectRowCt: 4,
ValidateRowData: func() {
u.Infof("%v", data)
},
RowData: &data,
})
}

func TestSelectWhereLike(t *testing.T) {
Expand Down
26 changes: 18 additions & 8 deletions backends/mongo/sql_to_mgo.go
Original file line number Diff line number Diff line change
Expand Up @@ -249,14 +249,17 @@ func (m *SqlToMgo) WalkExecSource(p *plan.Source) (exec.Task, error) {
return resultReader, nil
}

func (m *SqlToMgo) eval(arg expr.Node) (value.Value, bool) {
// eval() returns
// value, isOk, isIdentity
func (m *SqlToMgo) eval(arg expr.Node) (value.Value, bool, bool) {
switch arg := arg.(type) {
case *expr.NumberNode, *expr.StringNode:
return vm.Eval(nil, arg)
val, ok := vm.Eval(nil, arg)
return val, ok, false
case *expr.IdentityNode:
return value.NewStringValue(arg.Text), true
return value.NewStringValue(arg.Text), true, true
}
return nil, false
return nil, false, false
}

// Aggregations from the <select_list>
Expand Down Expand Up @@ -429,7 +432,7 @@ func (m *SqlToMgo) walkNode(cur expr.Node, q *bson.M) (value.Value, error) {
//
func (m *SqlToMgo) walkFilterTri(node *expr.TriNode, q *bson.M) (value.Value, error) {

arg1val, aok := m.eval(node.Args[0])
arg1val, aok, _ := m.eval(node.Args[0])
if !aok {
return nil, fmt.Errorf("Could not evaluate args: %v", node.String())
}
Expand Down Expand Up @@ -512,15 +515,22 @@ func (m *SqlToMgo) walkFilterBinary(node *expr.BinaryNode, q *bson.M) (value.Val
return nil, nil
}

lhval, lhok := m.eval(node.Args[0])
lhval, lhok, isLident := m.eval(node.Args[0])
rhval, rhok, isRident := m.eval(node.Args[1])
if !lhok {
return nil, fmt.Errorf("Could not evaluate args: %v", node.String())
return nil, fmt.Errorf("Could not evaluate left arg: %v", node.String())
}
rhval, rhok := m.eval(node.Args[1])
if !rhok {
u.Warnf("not ok: %v l:%v r:%v", node, lhval, rhval)
return nil, fmt.Errorf("could not evaluate: %v", node.String())
}
if isLident && isRident {
// comparison of left/right isn't mongos strong suit
// https://stackoverflow.com/questions/4442453/mongodb-query-condition-on-comparing-2-fields
// db.T.find( { $where : "this.Grade1 > this.Grade2" } );
*q = bson.M{"$where": fmt.Sprintf("this.%s %s this.%s", lhval.ToString(), node.Operator.V, rhval.ToString())}
return nil, nil
}
//u.Debugf("walkBinary: %v l:%v r:%v %T %T", node, lhval, rhval, lhval, rhval)
switch node.Operator.T {
case lex.TokenEqual, lex.TokenEqualEqual:
Expand Down

0 comments on commit ef7cd20

Please sign in to comment.