-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathquert_parser.go
164 lines (138 loc) · 3.57 KB
/
quert_parser.go
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
package query_parser
import (
"strings"
"github.com/orkes-io/query_parser/stack"
"github.com/orkes-io/query_parser/util"
)
func ConvertWhereClauseToMongoFilter(whereClause string) map[string]any {
query := ReplaceLogicalOperators(whereClause)
tokens := Tokenize(query)
return ConvertEquivalentMongoQuery(tokens)
}
func Tokenize(query string) []string {
tokens := make([]string, 0)
temp := ""
for i := 0; i < len(query); i++ {
if query[i] == '(' {
tokens = append(tokens, string(query[i]))
} else if query[i] == ')' {
tokens = append(tokens, temp, string(query[i]))
temp = ""
} else if IsLogicalOperator(string(query[i])) {
tokens = append(tokens, temp, string(query[i]))
temp = ""
} else {
temp = temp + string(query[i])
}
}
if len(temp) != 0 {
tokens = append(tokens, temp)
}
tokens = util.SliceMap(tokens, func(s string, _ int) string {
return strings.TrimSpace(s)
})
return util.SliceFilter(tokens, func(s string, _ int) bool {
return len(strings.TrimSpace(s)) > 0
})
}
func ReplaceLogicalOperators(query string) string {
query = strings.ReplaceAll(query, " AND ", " & ")
query = strings.ReplaceAll(query, " and ", " & ")
query = strings.ReplaceAll(query, " OR ", " | ")
query = strings.ReplaceAll(query, " or ", " | ")
return query
}
func IsLogicalOperator(char string) bool {
operators := []string{"&", "|"}
return util.SliceContains(operators, char)
}
func ConvertEquivalentMongoQuery(tokens []string) map[string]any {
postfixTokens := ConvertInfixToPostfix(tokens)
evaluationStack := stack.NewStack[map[string]any]()
for _, token := range postfixTokens {
if IsLogicalOperator(token) {
operand2 := evaluationStack.Pop()
operand1 := evaluationStack.Pop()
evaluationStack.Push(map[string]any{
ConvertEquivalentMongoOperator(token): []map[string]any{
operand1, operand2,
},
})
} else {
evaluationStack.Push(ConvertEquivalentMongoMap(token))
}
}
return evaluationStack.Pop()
}
func ConvertEquivalentMongoMap(token string) map[string]any {
fields := strings.Fields(token)
operand1, operator, operand2 := fields[0], fields[1], fields[2]
mongoMap := map[string]any{
operand1: map[string]any{
ConvertEquivalentMongoOperator(operator): operand2,
},
}
return mongoMap
}
func ConvertEquivalentMongoOperator(operator string) string {
switch strings.TrimSpace(operator) {
case "&":
return "$and"
case "|":
return "$or"
case "=":
return "$eq"
case "!=":
return "$ne"
case ">":
return "$gt"
case ">=":
return "$ge"
case "<":
return "$lt"
case "<=":
return "$le"
case "IN":
return "$in"
case "NOT IN":
return "$ni"
default:
return ""
}
}
func ConvertInfixToPostfix(infixTokens []string) []string {
postfixTokens := make([]string, 0, len(infixTokens))
operatorStack := stack.NewStack[string]()
for _, token := range infixTokens {
if IsLogicalOperator(token) {
// pop high priority operators
for !operatorStack.IsEmpty() && Precedence(token) <= Precedence(operatorStack.Top()) {
postfixTokens = append(postfixTokens, operatorStack.Pop())
}
operatorStack.Push(token)
} else if token == "(" {
operatorStack.Push(token)
} else if token == ")" {
for operatorStack.Top() != "(" {
postfixTokens = append(postfixTokens, operatorStack.Pop())
}
operatorStack.Pop()
} else {
postfixTokens = append(postfixTokens, token)
}
}
for !operatorStack.IsEmpty() {
postfixTokens = append(postfixTokens, operatorStack.Pop())
}
return postfixTokens
}
func Precedence(s string) int {
switch s {
case "&":
return 9
case "|":
return 8
default:
return 0
}
}