Skip to content

Commit 613fe12

Browse files
committed
patched binary walk
1 parent 1049190 commit 613fe12

File tree

2 files changed

+37
-2
lines changed

2 files changed

+37
-2
lines changed

expr/binary.go

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,7 @@ func (b *Binary) Walk(fn func(ident node.Node, values *Values, operator, parentO
4848

4949
func (b *Binary) walk(fn func(ident node.Node, values *Values, operator, parentOperator string) error, operator string) error {
5050
switch b.Op[0] {
51-
case 'A', 'O':
51+
case 'A', 'O', 'a', 'o':
5252
if x, ok := b.X.(*Binary); ok {
5353
if err := x.walk(fn, b.Op); err != nil {
5454
return err
@@ -61,12 +61,24 @@ func (b *Binary) walk(fn func(ident node.Node, values *Values, operator, parentO
6161
}
6262
return nil
6363
}
64+
6465
sel, values, err := b.Predicate()
6566
if err != nil {
6667
return err
6768
}
68-
return fn(sel, values, b.Op, operator)
69+
err = fn(sel, values, b.Op, operator)
70+
if err != nil {
71+
return err
72+
}
73+
if binY, ok := b.Y.(*Binary); ok {
74+
if nested, ok := binY.Y.(*Binary); ok {
75+
if err = nested.walk(fn, b.Op); err != nil {
76+
return err
77+
}
78+
}
6979

80+
}
81+
return nil
7082
}
7183

7284
// Predicate binary predicate or nil

query_test.go

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,11 +5,28 @@ import (
55
"fmt"
66
"github.com/stretchr/testify/assert"
77
"github.com/viant/parsly"
8+
"github.com/viant/sqlparser/expr"
9+
"github.com/viant/sqlparser/node"
810
"github.com/viant/sqlparser/query"
911
"strings"
1012
"testing"
1113
)
1214

15+
func TestBinaryWalk(t *testing.T) {
16+
query, _ := ParseQuery("SELECT * FROM t WHERE a = 1 AND b = 2 AND c IN(1,2)")
17+
binary, ok := query.Qualify.X.(*expr.Binary)
18+
if !assert.True(t, ok) {
19+
return
20+
}
21+
var actualColumns = make([]string, 0)
22+
binary.Walk(func(ident node.Node, values *expr.Values, operator, parentOperator string) error {
23+
actualColumns = append(actualColumns, Stringify(ident))
24+
return nil
25+
})
26+
assert.EqualValues(t, []string{"a", "b", "c"}, actualColumns)
27+
28+
}
29+
1330
func TestParseSelect(t *testing.T) {
1431

1532
{
@@ -313,6 +330,12 @@ func TestParseSelect(t *testing.T) {
313330
SQL: `SELECT ID,NAME FROM AAA ORDER BY 2 DESC, 1 ASC`,
314331
expect: `SELECT ID, NAME FROM AAA ORDER BY 2 DESC, 1 ASC`,
315332
},
333+
334+
{
335+
description: "",
336+
SQL: `SELECT col1, col2 FROM table1/tt t JOIN xx/e v ON v.ID=t.ID`,
337+
expect: `SELECT col1, col2 FROM table1/tt t JOIN xx/e v ON v.ID = t.ID`,
338+
},
316339
}
317340

318341
for _, testCase := range testCases {

0 commit comments

Comments
 (0)