Repository navigation
Expand file tree
/
Copy pathparser_function.go
More file actions
135 lines (113 loc) · 4.37 KB
/
Copy pathparser_function.go
File metadata and controls
135 lines (113 loc) · 4.37 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
package main
import (
"strings"
pgQuery "github.com/pganalyze/pg_query_go/v6"
)
type ParserFunction struct {
config *Config
utils *ParserUtils
}
func NewParserFunction(config *Config) *ParserFunction {
return &ParserFunction{config: config, utils: NewParserUtils(config)}
}
func (parser *ParserFunction) FunctionCall(targetNode *pgQuery.Node) *pgQuery.FuncCall {
return targetNode.GetResTarget().Val.GetFuncCall()
}
func (parser *ParserFunction) FirstArgumentToString(functionCall *pgQuery.FuncCall) string {
if len(functionCall.Args) < 1 {
return ""
}
return functionCall.Args[0].GetAConst().GetSval().Sval
}
// n from (FUNCTION()).n
func (parser *ParserFunction) IndirectionName(targetNode *pgQuery.Node) string {
indirection := targetNode.GetResTarget().Val.GetAIndirection()
if indirection != nil && len(indirection.Indirection) == 1 && indirection.Indirection[0].GetString_() != nil {
return indirection.Indirection[0].GetString_().Sval
}
return ""
}
func (parser *ParserFunction) NestedFunctionCalls(functionCall *pgQuery.FuncCall) []*pgQuery.FuncCall {
nestedFunctionCalls := []*pgQuery.FuncCall{}
for _, arg := range functionCall.Args {
nestedFunctionCalls = append(nestedFunctionCalls, arg.GetFuncCall())
}
return nestedFunctionCalls
}
func (parser *ParserFunction) SchemaFunction(functionCall *pgQuery.FuncCall) *QuerySchemaFunction {
return parser.utils.SchemaFunction(functionCall)
}
// pg_catalog.func() -> main.func()
func (parser *ParserFunction) RemapSchemaToMain(functionCall *pgQuery.FuncCall) {
switch len(functionCall.Funcname) {
case 1:
functionCall.Funcname = append([]*pgQuery.Node{pgQuery.MakeStrNode(DUCKDB_SCHEMA_MAIN)}, functionCall.Funcname...)
case 2:
functionCall.Funcname[0] = pgQuery.MakeStrNode(DUCKDB_SCHEMA_MAIN)
default:
Panic(parser.config, "Unexpected function name length: "+IntToString(len(functionCall.Funcname)))
}
}
// jsonb_agg(...) -> to_json(array_agg(...))
//
// DuckDB does not support jsonb_agg. We can't remap and fully support all features with just a macro:
// Function "jsonb_agg" is a Macro Function. "DISTINCT", "FILTER", and "ORDER BY" are only applicable to aggregate functions.
func (parser *ParserFunction) RemapJsonbAgg(functionCall *pgQuery.FuncCall) {
originalArgs := functionCall.Args
originalAggOrder := functionCall.AggOrder
originalAggFilter := functionCall.AggFilter
originalAggWithinGroup := functionCall.AggWithinGroup
originalAggStar := functionCall.AggStar
originalAggDistinct := functionCall.AggDistinct
nestedFunctionCallNode := pgQuery.MakeFuncCallNode(
[]*pgQuery.Node{pgQuery.MakeStrNode("array_agg")},
originalArgs,
0,
)
nestedFunctionCall := nestedFunctionCallNode.GetFuncCall()
nestedFunctionCall.AggOrder = originalAggOrder
nestedFunctionCall.AggFilter = originalAggFilter
nestedFunctionCall.AggWithinGroup = originalAggWithinGroup
nestedFunctionCall.AggStar = originalAggStar
nestedFunctionCall.AggDistinct = originalAggDistinct
functionCall.Funcname = []*pgQuery.Node{pgQuery.MakeStrNode("to_json")}
functionCall.Args = []*pgQuery.Node{nestedFunctionCallNode}
functionCall.AggOrder = nil
functionCall.AggFilter = nil
functionCall.AggWithinGroup = false
functionCall.AggStar = false
functionCall.AggDistinct = false
}
// format('%s %1$s', str) -> printf('%1$s %1$s', str)
func (parser *ParserFunction) RemapFormatToPrintf(functionCall *pgQuery.FuncCall) {
format := parser.FirstArgumentToString(functionCall)
for i := range functionCall.Args[1:] {
format = strings.Replace(format, "%s", "%"+IntToString(i+1)+"$s", 1)
}
functionCall.Funcname = []*pgQuery.Node{pgQuery.MakeStrNode("printf")}
functionCall.Args[0] = pgQuery.MakeAConstStrNode(format, 0)
}
// encode(sha256(...), 'hex') -> sha256(...)
func (parser *ParserFunction) RemoveEncode(functionCall *pgQuery.FuncCall) {
if len(functionCall.Args) != 2 {
return
}
firstArg := functionCall.Args[0]
nestedFunctionCall := firstArg.GetFuncCall()
schemaFunction := parser.utils.SchemaFunction(nestedFunctionCall)
if schemaFunction.Function != "sha256" {
return
}
secondArg := functionCall.Args[1]
var format string
if secondArg.GetAConst() != nil {
format = secondArg.GetAConst().GetSval().Sval
} else if secondArg.GetTypeCast() != nil {
format = secondArg.GetTypeCast().Arg.GetAConst().GetSval().Sval
}
if format != "hex" {
return
}
functionCall.Funcname = nestedFunctionCall.Funcname
functionCall.Args = nestedFunctionCall.Args
}