-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathjoin.go
More file actions
183 lines (171 loc) · 5.55 KB
/
Copy pathjoin.go
File metadata and controls
183 lines (171 loc) · 5.55 KB
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
package sqlparser
import (
"github.com/viant/parsly"
"github.com/viant/sqlparser/expr"
"github.com/viant/sqlparser/node"
"github.com/viant/sqlparser/query"
"strings"
)
func parseJoin(cursor *parsly.Cursor, join *query.Join, dest *query.Select, expectOn bool) error {
defer func() {
if join.Span.End == 0 {
join.Span.End = uint32(cursor.Pos)
}
}()
if err := parseJoinTarget(cursor, join); err != nil {
return err
}
if join.With == nil {
return cursor.NewError(parenthesesMatcher, selectorMatcher)
}
if join.Alias == "" {
var err error
if join.Alias, err = discoverAlias(cursor); err != nil {
return err
}
}
join.End = uint32(cursor.Pos)
match := cursor.MatchAfterOptional(whitespaceMatcher, commentBlockMatcher, onKeywordMatcher)
if match.Code == commentBlock {
join.Comments = match.Text(cursor)
match = cursor.MatchAfterOptional(whitespaceMatcher, onKeywordMatcher)
}
switch match.Code {
case onKeyword:
begin := match.Offset
binary := &expr.Binary{}
join.On = &expr.Qualify{}
join.On.X = binary
if err := parseBinaryExpr(cursor, binary); err != nil {
return err
}
raw := string(cursor.Input[begin:cursor.Pos])
trimmed := strings.TrimSpace(raw)
start := begin + len(raw) - len(strings.TrimLeft(raw, " \t\r\n"))
join.OnSpan = node.Span{Begin: uint32(start), End: uint32(start + len(trimmed))}
default:
if expectOn {
return cursor.NewError(onKeywordMatcher)
}
}
join.Span.End = uint32(len(strings.TrimRight(string(cursor.Input[:cursor.Pos]), " \t\r\n")))
match = cursor.MatchAfterOptional(whitespaceMatcher, joinMatcher, groupByMatcher, havingKeywordMatcher, whereKeywordMatcher, orderByKeywordMatcher, windowMatcher, unionMatcher)
if match.Code == parsly.EOF {
return nil
}
if match.Code == commentBlock {
join.Comments = match.Text(cursor)
match = cursor.MatchAfterOptional(whitespaceMatcher, joinMatcher, groupByMatcher, havingKeywordMatcher, whereKeywordMatcher, orderByKeywordMatcher, windowMatcher, unionMatcher)
if match.Code == parsly.EOF {
return nil
}
}
hasMatch, err := matchPostFrom(cursor, dest, match)
if !hasMatch && err == nil {
err = cursor.NewError(joinMatcher, groupByMatcher, havingKeywordMatcher, whereKeywordMatcher, orderByKeywordMatcher, windowMatcher, unionMatcher)
}
return err
}
func parseJoinTarget(cursor *parsly.Cursor, join *query.Join) error {
pos := cursor.Pos
if match := cursor.MatchAfterOptional(whitespaceMatcher, bracedTableMatcher); match.Code == tableTokenCode {
join.With = expr.NewSelector(match.Text(cursor))
return nil
}
cursor.Pos = pos
operand, err := expectOperand(cursor)
if err != nil {
return err
}
if operand != nil {
join.With = operand
return nil
}
cursor.Pos = pos
match := cursor.MatchAfterOptional(whitespaceMatcher, parenthesesMatcher, exprMatcher, selectorMatcher)
switch match.Code {
case parenthesesCode:
join.With = expr.NewRaw(match.Text(cursor))
case selectorTokenCode:
identityOrAlias := match.Text(cursor)
match = cursor.MatchAfterOptional(whitespaceMatcher, parenthesesMatcher)
if match.Code == parenthesesCode {
identityOrAlias += match.Text(cursor)
}
join.With = expr.NewSelector(identityOrAlias)
}
return nil
}
func parseDeleteJoin(cursor *parsly.Cursor, join *query.Join) (*parsly.TokenMatch, error) {
match := cursor.MatchAfterOptional(whitespaceMatcher, parenthesesMatcher, selectorMatcher)
switch match.Code {
case parenthesesCode:
join.With = expr.NewRaw(match.Text(cursor))
case selectorTokenCode:
join.With = expr.NewSelector(match.Text(cursor))
}
var err error
if join.Alias, err = discoverAlias(cursor); err != nil {
return match, err
}
match = cursor.MatchAfterOptional(whitespaceMatcher, commentBlockMatcher, onKeywordMatcher)
if match.Code == commentBlock {
join.Comments = match.Text(cursor)
match = cursor.MatchAfterOptional(whitespaceMatcher, onKeywordMatcher)
}
onBegin := match.Offset
switch match.Code {
case onKeyword:
default:
return match, cursor.NewError(onKeywordMatcher)
}
binary := &expr.Binary{}
join.On = &expr.Qualify{}
join.On.X = binary
if err := parseBinaryExpr(cursor, binary); err != nil {
return match, err
}
onEnd := trimSourceWhitespace(cursor.Input, cursor.Pos)
join.OnSpan = node.Span{Begin: uint32(onBegin), End: uint32(onEnd)}
join.End = uint32(onEnd)
match = cursor.MatchAfterOptional(whitespaceMatcher, joinMatcher, groupByMatcher, havingKeywordMatcher, whereKeywordMatcher, orderByKeywordMatcher, windowMatcher)
if match.Code == parsly.EOF {
return match, nil
}
if match.Code == commentBlock {
join.Comments = match.Text(cursor)
match = cursor.MatchAfterOptional(whitespaceMatcher, joinMatcher, groupByMatcher, havingKeywordMatcher, whereKeywordMatcher, orderByKeywordMatcher, windowMatcher)
if match.Code == parsly.EOF {
return match, nil
}
}
return match, nil
}
func trimSourceWhitespace(input []byte, end int) int {
if end > len(input) {
end = len(input)
}
for end > 0 {
switch input[end-1] {
case ' ', '\t', '\n', '\r':
end--
default:
return end
}
}
return end
}
func appendJoin(cursor *parsly.Cursor, match *parsly.TokenMatch, dest *query.Select, expectOn bool) error {
join := query.NewJoin(match.Text(cursor))
join.Span.Begin = uint32(match.Offset)
// CROSS JOIN has no required join condition. Normalize the matched
// whitespace and case while retaining the authored text for serialization.
if strings.EqualFold(strings.Join(strings.Fields(join.Raw), " "), "CROSS JOIN") {
expectOn = false
}
dest.Joins = append(dest.Joins, join)
if err := parseJoin(cursor, join, dest, expectOn); err != nil {
return err
}
return nil
}