@@ -6,48 +6,117 @@ import (
6
6
7
7
"github.com/gitql/gitql/sql"
8
8
"github.com/gitql/gitql/sql/expression"
9
+ "github.com/gitql/gitql/sql/plan"
9
10
10
- "github.com/stretchr/testify/require "
11
+ "github.com/stretchr/testify/assert "
11
12
)
12
13
13
- const testSelectFromWhere = `SELECT foo, bar FROM foo WHERE foo = bar;`
14
- const testSelectFrom = `SELECT foo, bar FROM foo;`
15
-
16
- func TestParseSelectFromWhere (t * testing.T ) {
17
- p := newParser (strings .NewReader (testSelectFromWhere ))
18
- require .Nil (t , p .parse ())
19
-
20
- require .Equal (t , p .projection , []sql.Expression {
21
- expression .NewUnresolvedColumn ("foo" ),
22
- expression .NewUnresolvedColumn ("bar" ),
23
- })
24
-
25
- require .Equal (t , p .relation , "foo" )
26
-
27
- require .Equal (t , p .filterClauses , []sql.Expression {
28
- expression .NewEquals (
14
+ var fixtures = map [string ]sql.Node {
15
+ `SELECT foo, bar FROM foo;` : plan .NewProject (
16
+ []sql.Expression {
29
17
expression .NewUnresolvedColumn ("foo" ),
30
18
expression .NewUnresolvedColumn ("bar" ),
19
+ },
20
+ plan .NewUnresolvedRelation ("foo" ),
21
+ ),
22
+ `SELECT foo, bar FROM foo WHERE foo = bar;` : plan .NewProject (
23
+ []sql.Expression {
24
+ expression .NewUnresolvedColumn ("foo" ),
25
+ expression .NewUnresolvedColumn ("bar" ),
26
+ },
27
+ plan .NewFilter (
28
+ expression .NewEquals (
29
+ expression .NewUnresolvedColumn ("foo" ),
30
+ expression .NewUnresolvedColumn ("bar" ),
31
+ ),
32
+ plan .NewUnresolvedRelation ("foo" ),
31
33
),
32
- })
33
-
34
- require .Nil (t , p .sortFields )
35
- require .Nil (t , p .err )
36
- require .Equal (t , DoneState , p .stateStack .pop ())
34
+ ),
35
+ `SELECT foo, bar FROM foo WHERE foo = 'bar';` : plan .NewProject (
36
+ []sql.Expression {
37
+ expression .NewUnresolvedColumn ("foo" ),
38
+ expression .NewUnresolvedColumn ("bar" ),
39
+ },
40
+ plan .NewFilter (
41
+ expression .NewEquals (
42
+ expression .NewUnresolvedColumn ("foo" ),
43
+ expression .NewLiteral ("bar" , sql .String ),
44
+ ),
45
+ plan .NewUnresolvedRelation ("foo" ),
46
+ ),
47
+ ),
48
+ `SELECT foo, bar FROM foo LIMIT 10;` : plan .NewLimit (int64 (10 ),
49
+ plan .NewProject (
50
+ []sql.Expression {
51
+ expression .NewUnresolvedColumn ("foo" ),
52
+ expression .NewUnresolvedColumn ("bar" ),
53
+ },
54
+ plan .NewUnresolvedRelation ("foo" ),
55
+ ),
56
+ ),
57
+ `SELECT foo, bar FROM foo ORDER BY baz DESC;` : plan .NewSort (
58
+ []plan.SortField {{expression .NewUnresolvedColumn ("baz" ), plan .Descending }},
59
+ plan .NewProject (
60
+ []sql.Expression {
61
+ expression .NewUnresolvedColumn ("foo" ),
62
+ expression .NewUnresolvedColumn ("bar" ),
63
+ },
64
+ plan .NewUnresolvedRelation ("foo" ),
65
+ ),
66
+ ),
67
+ `SELECT foo, bar FROM foo WHERE foo = bar LIMIT 10;` : plan .NewLimit (int64 (10 ),
68
+ plan .NewProject (
69
+ []sql.Expression {
70
+ expression .NewUnresolvedColumn ("foo" ),
71
+ expression .NewUnresolvedColumn ("bar" ),
72
+ },
73
+ plan .NewFilter (
74
+ expression .NewEquals (
75
+ expression .NewUnresolvedColumn ("foo" ),
76
+ expression .NewUnresolvedColumn ("bar" ),
77
+ ),
78
+ plan .NewUnresolvedRelation ("foo" ),
79
+ ),
80
+ ),
81
+ ),
82
+ `SELECT foo, bar FROM foo ORDER BY baz DESC LIMIT 1;` : plan .NewLimit (int64 (1 ),
83
+ plan .NewSort (
84
+ []plan.SortField {{expression .NewUnresolvedColumn ("baz" ), plan .Descending }},
85
+ plan .NewProject (
86
+ []sql.Expression {
87
+ expression .NewUnresolvedColumn ("foo" ),
88
+ expression .NewUnresolvedColumn ("bar" ),
89
+ },
90
+ plan .NewUnresolvedRelation ("foo" ),
91
+ ),
92
+ ),
93
+ ),
94
+ `SELECT foo, bar FROM foo WHERE qux = 1 ORDER BY baz DESC LIMIT 1;` : plan .NewLimit (int64 (1 ),
95
+ plan .NewSort (
96
+ []plan.SortField {{expression .NewUnresolvedColumn ("baz" ), plan .Descending }},
97
+ plan .NewProject (
98
+ []sql.Expression {
99
+ expression .NewUnresolvedColumn ("foo" ),
100
+ expression .NewUnresolvedColumn ("bar" ),
101
+ },
102
+ plan .NewFilter (
103
+ expression .NewEquals (
104
+ expression .NewUnresolvedColumn ("qux" ),
105
+ expression .NewLiteral (int64 (1 ), sql .BigInteger ),
106
+ ),
107
+ plan .NewUnresolvedRelation ("foo" ),
108
+ ),
109
+ ),
110
+ ),
111
+ ),
37
112
}
38
113
39
- func TestParseSelectFrom (t * testing.T ) {
40
- p := newParser (strings .NewReader (testSelectFrom ))
41
- require .Nil (t , p .parse ())
42
-
43
- require .Equal (t , p .projection , []sql.Expression {
44
- expression .NewUnresolvedColumn ("foo" ),
45
- expression .NewUnresolvedColumn ("bar" ),
46
- })
47
-
48
- require .Equal (t , p .relation , "foo" )
49
-
50
- require .Nil (t , p .sortFields )
51
- require .Nil (t , p .err )
52
- require .Equal (t , DoneState , p .stateStack .pop ())
114
+ func TestParse (t * testing.T ) {
115
+ assert := assert .New (t )
116
+ for query , expectedPlan := range fixtures {
117
+ p , err := Parse (strings .NewReader (query ))
118
+ assert .Nil (err )
119
+ assert .Exactly (expectedPlan , p ,
120
+ "plans do not match for query '%s'" , query )
121
+ }
53
122
}
0 commit comments