Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Node Equality round-trip checks #147

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 56 additions & 25 deletions expr/node.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,6 +162,11 @@ type (
// If op is 0, and args nil then exactly one of these should be set
Identity string `json:"ident,omitempty"`
Value string `json:"val,omitempty"`

// Identity may possibly be split into left.right
// if ident is set, optionally may have left side component
Left string `json:"left,omitempty"`

// Really would like to use these instead of un-typed guesses above
// if we desire serialization into string representation that is fine
// Int int64
Expand Down Expand Up @@ -1059,15 +1064,24 @@ func (m *IdentityNode) Expr() *Expr {
if m.HasLeftRight() {
if IdentityMaybeQuote('`', m.left) != m.left {
u.Warnf("This will NOT round-trip l:%q r:%q original:%q text:%q", m.left, m.right, m.original, m.Text)
return &Expr{Identity: m.right, Left: m.left}
}
return &Expr{Identity: fmt.Sprintf("%s.%s", m.left, m.right)}
}
return &Expr{Identity: m.Text}
}
func (m *IdentityNode) FromExpr(e *Expr) error {
if len(e.Identity) > 0 {
m.Text = e.Identity
m.load()
if e.Left != "" {
m.left = e.Left
m.right = e.Identity
l, r := IdentityMaybeQuote(m.Quote, m.left), IdentityMaybeQuote(m.Quote, m.right)
m.original = fmt.Sprintf("%s.%s", l, r)
m.Text = IdentityTrim(m.original)
} else {
m.Text = e.Identity
m.load()
}
return nil
}
return fmt.Errorf("unrecognized identity")
Expand Down Expand Up @@ -1096,30 +1110,42 @@ func (m *IdentityNode) Equal(n Node) bool {
if m != nil && n == nil {
return false
}
if nt, ok := n.(*IdentityNode); ok {

nt, ok := n.(*IdentityNode)
if !ok {
return false
}
if nt.HasLeftRight() {
if m.left != nt.left {
return false
}
if m.right != nt.right {
return false
}
} else {
if nt.Text != m.Text {
return false
}
// Hm, should we compare quotes or not? Given they are dialect
// specific and don't affect logic i vote no?

// if nt.Quote != m.Quote {
// switch m.Quote {
// case '`':
// if nt.Quote == '\'' || nt.Quote == 0 {
// // ok
// return true
// }
// case 0:
// if nt.Quote == '\'' || nt.Quote == '`' {
// // ok
// return true
// }
// }

return true
}
return false

// Hm, should we compare quotes or not? Given they are dialect
// specific and don't affect logic i vote no?

// if nt.Quote != m.Quote {
// switch m.Quote {
// case '`':
// if nt.Quote == '\'' || nt.Quote == 0 {
// // ok
// return true
// }
// case 0:
// if nt.Quote == '\'' || nt.Quote == '`' {
// // ok
// return true
// }
// }

return true
}

// HasLeftRight Return bool if is of form `table.column` or `schema`.`table`
Expand Down Expand Up @@ -1362,16 +1388,21 @@ func (m *BinaryNode) Equal(n Node) bool {
if nt.Operator.T != m.Operator.T {
return false
}

if nt.Operator.V != m.Operator.V {
if strings.ToLower(nt.Operator.V) != strings.ToLower(m.Operator.V) {
return false
}
}
if nt.Paren != m.Paren {
return false
}
// Round-Tripping through JSON Ast is not preservering
// Paren, but it is superficial isn't it?
// if nt.Paren != m.Paren {
// u.Debugf("paren %#v", m)
// return false
// }
for i, arg := range nt.Args {
if !arg.Equal(m.Args[i]) {
u.Debugf("arg i:%d %#v", i, arg)
return false
}
}
Expand Down
12 changes: 11 additions & 1 deletion vm/filterqlvm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,16 @@ func TestFilterQlVm(t *testing.T) {
match, ok := Matches(incctx, fs)
assert.Tf(t, ok, "should be ok matching on query %q: %v", q, ok)
assert.T(t, match, q)
jsonAst, err := json.MarshalIndent(fs.Filter.Expr(), "", " ")
e := &expr.Expr{}
err = json.Unmarshal(jsonAst, e)
assert.Equal(t, nil, err)
n, err := expr.NodeFromExpr(e)
assert.Equal(t, nil, err)
jsonAst2, _ := json.MarshalIndent(n.Expr(), "", " ")
u.Debugf("\n1) %#v \n2) %#v", fs.Filter, n)
assert.Tf(t, fs.Filter.Equal(n), "Must round-trip node to json and back: %s\n%s\n%s",
q, string(jsonAst), string(jsonAst2))
}

misses := []string{
Expand All @@ -169,7 +179,7 @@ func TestFilterQlVm(t *testing.T) {

//u.Debugf("about to parse: %v", test.qlText)
sel, err := rel.ParseFilterSelect(test.query)
assert.T(t, err == nil, "expected no error but got ", err, " for ", test.query)
assert.Equalf(t, nil, err, "got %v for %s", err, test.query)

writeContext := datasource.NewContextSimple()
_, ok := EvalFilterSelect(sel, writeContext, incctx)
Expand Down