-
Notifications
You must be signed in to change notification settings - Fork 1
/
translateFnc.go
216 lines (180 loc) · 5.62 KB
/
translateFnc.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
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
// Copyright (c) 2021 OpenM++
// This code is licensed under the MIT license (see LICENSE.txt for details)
package db
import (
"errors"
"strings"
"unicode"
"unicode/utf8"
)
// left side name delimiters
const leftDelims = ",(+-*/%^|&~!=<>"
// right side name delimiters
const rightDelims = ")+-*/%^|&~!=<>"
// non-aggregation functions
var simpleFncLst = []string{"OM_IF", "OM_DIV_BY"}
// translate (substitute) all non-aggregation functions: OM_DIV_BY OM_IF...
func translateAllSimpleFnc(expr string) (string, error) {
// do substitution of all non-aggregation functions by sql
for {
// find most left (top level) non-aggregation function
fncName, namePos, arg, nAfter, err := findFirstFnc(expr, simpleFncLst)
if err != nil {
return "", err
}
if fncName == "" { // all done: no any functions found
break
}
// translate (substitute) non-aggreagtion function by sql fragment
t, err := translateSimpleFnc(fncName, arg, expr)
if err != nil {
return "", err
}
// replace source
if nAfter >= len(expr) {
expr = expr[:namePos] + t
} else {
expr = expr[:namePos] + t + expr[nAfter:]
}
}
return expr, nil
}
// find first (left most) function in source expression.
// return function name, name position, argument and position after argumnet end.
func findFirstFnc(src string, fncNameLst []string) (string, int, string, int, error) {
// find left most function name in source expression
nFnc, namePos, err := findFirstNameFnc(src, fncNameLst)
if err != nil {
return "", 0, "", 0, err
}
if nFnc < 0 {
return "", 0, "", 0, nil // not found
}
// find open and closing bracket at the same level
nAfter := namePos + len(fncNameLst[nFnc])
if nAfter >= len(src) {
return "", 0, "", 0, errors.New("Error in expression, missing brackets after: " + fncNameLst[nFnc] + ": " + src)
}
level := 0
nOpen := -1
nClose := -1
isInside := false
for n, c := range src[nAfter:] {
if !isInside && c == '(' { // open bracket: down to next level
if level == 0 {
nOpen = nAfter + n
}
level++
continue
}
if !isInside && level <= 0 && !unicode.IsSpace(c) {
return "", 0, "", 0, errors.New("Error in expression, missing brackets after: " + fncNameLst[nFnc] + ": " + src)
}
if !isInside && c == ')' { // close bracket: up to previous level
level--
if level < 0 {
return "", 0, "", 0, errors.New("Error in expression, unbalanced brackets after: " + fncNameLst[nFnc] + ": " + src)
}
if level == 0 {
nClose = nAfter + n
break // done: close bracket found
}
continue
}
if c == '\'' {
isInside = !isInside // begin or end of 'quoted' sql
}
}
if level != 0 {
return "", 0, "", 0, errors.New("Error in expression, unbalanced brackets after: " + fncNameLst[nFnc] + ": " + src)
}
if nOpen < nAfter || nClose < nAfter || nOpen >= nClose || nClose >= len(src) {
return "", 0, "", 0, errors.New("Error in expression, missing brackets after: " + fncNameLst[nFnc] + ": " + src)
}
return fncNameLst[nFnc], namePos, src[nOpen+1 : nClose], nClose + 1, nil
}
// find first (left most) function name in src source expression from the fncNameLst name list.
// return index of function and name position.
func findFirstNameFnc(src string, fncNameLst []string) (int, int, error) {
nFnc := -1
namePos := len(src)
for nf := 0; nf < len(fncNameLst); nf++ {
// for each 'unquoted' part of source expression search for left most position of function name
nFirst := 0
var err error = nil
for nLast := 0; nFirst >= 0 && nLast >= 0; {
if nFirst, nLast, err = nextUnquoted(src, nFirst); err != nil {
return -1, 0, err
}
if nFirst < 0 || nLast < 0 { // end of source sql string
break
}
// find function name and delimited by space or open bracket
nc := nFirst
for n := nc; n < nLast; {
n = strings.Index(src[nc:nLast], fncNameLst[nf])
if n < 0 || n+nc >= namePos {
break // skip: function name not found or it is not a left most
}
n += nc
nl := len(fncNameLst[nf])
// check if before function name is a space or left delimiter or name is at the beginning
isOk := true
if n > 0 {
r, _ := utf8.DecodeRuneInString(src[n-1:])
isOk = unicode.IsSpace(r) || strings.ContainsRune(leftDelims, r)
}
if !isOk {
nc += nl
continue // skip: it is not a function name
}
// if function name is at the end then skip it: it must be at least open ( bracket after
if n+nl >= nLast {
nc += nl
continue // skip
}
// check if after function name is a space or right delimiter or name is at the end
isOk = src[n+nl] == '('
if !isOk {
r, _ := utf8.DecodeRuneInString(src[n+nl:])
isOk = unicode.IsSpace(r)
}
if !isOk {
nc += nl
continue // skip: it is not a function name
}
// found function name
namePos = n
nFnc = nf
break
}
// found function name
if nFnc >= 0 {
break
}
nFirst = nLast // to the next 'unquoted' part of source expression
}
}
return nFnc, namePos, nil
}
// translate (substitute) non-aggregation function:
//
// OM_DIV_BY(acc1)
// =>
// CASE WHEN ABS(acc1) > 1.0e-37 THEN acc1 ELSE NULL END
//
// OM_IF(acc1 > 1.5 THEN acc1 ELSE 1.5)
// =>
// CASE WHEN acc1 > 1.5 THEN acc1 ELSE 1.5 END
func translateSimpleFnc(name, arg string, src string) (string, error) {
if len(arg) <= 0 {
return "", errors.New("invalid (empty) function argument: " + name + " : " + src)
}
switch name {
case "OM_IF":
return "CASE WHEN " + arg + " END", nil
case "OM_DIV_BY":
return "CASE WHEN ABS(" + arg + ") > 1.0e-37 THEN " + arg + " ELSE NULL END", nil
}
return "", errors.New("unknown non-aggregation function: " + name + " : " + src)
}