@@ -11,6 +11,7 @@ import (
11
11
12
12
const rsIdentifier = `([_a-zA-Z0-9]+)`
13
13
const sExpr = `\$` + rsIdentifier + `\(([^\)]*)\)`
14
+ const missingColumnMessage = "missing time column argument for macro %v"
14
15
15
16
func ReplaceAllStringSubmatchFunc (re * regexp.Regexp , str string , repl func ([]string ) string ) string {
16
17
result := ""
@@ -78,20 +79,21 @@ func SetupFillmode(configStruct *queryConfigStruct, fillmode string) error {
78
79
// evaluateMacro convert macro expression to sql expression
79
80
func evaluateMacro (name string , args []string , configStruct * queryConfigStruct ) (string , error ) {
80
81
timeRange := configStruct .TimeRange
82
+
81
83
switch name {
82
84
case "__time" :
83
85
if len (args ) == 0 || args [0 ] == "" {
84
- return "" , fmt .Errorf ("missing time column argument for macro %v" , name )
86
+ return "" , fmt .Errorf (missingColumnMessage , name )
85
87
}
86
88
return fmt .Sprintf ("TRY_TO_TIMESTAMP_NTZ(%s) AS time" , args [0 ]), nil
87
89
case "__timeEpoch" :
88
90
if len (args ) == 0 || args [0 ] == "" {
89
- return "" , fmt .Errorf ("missing time column argument for macro %v" , name )
91
+ return "" , fmt .Errorf (missingColumnMessage , name )
90
92
}
91
93
return fmt .Sprintf ("extract(epoch from %s) as time" , args [0 ]), nil
92
94
case "__timeFilter" :
93
95
if len (args ) == 0 || args [0 ] == "" {
94
- return "" , fmt .Errorf ("missing time column argument for macro %v" , name )
96
+ return "" , fmt .Errorf (missingColumnMessage , name )
95
97
}
96
98
column := args [0 ]
97
99
timezone := "'UTC'"
@@ -101,7 +103,7 @@ func evaluateMacro(name string, args []string, configStruct *queryConfigStruct)
101
103
return fmt .Sprintf ("%s > CONVERT_TIMEZONE('UTC', %s, '%s'::timestamp_ntz) AND %s < CONVERT_TIMEZONE('UTC', %s, '%s'::timestamp_ntz)" , column , timezone , timeRange .From .UTC ().Format (time .RFC3339Nano ), column , timezone , timeRange .To .UTC ().Format (time .RFC3339Nano )), nil
102
104
case "__timeTzFilter" :
103
105
if len (args ) == 0 || args [0 ] == "" {
104
- return "" , fmt .Errorf ("missing time column argument for macro %v" , name )
106
+ return "" , fmt .Errorf (missingColumnMessage , name )
105
107
}
106
108
column := args [0 ]
107
109
return fmt .Sprintf ("%s > '%s'::timestamp_tz AND %s < '%s'::timestamp_tz" , column , timeRange .From .UTC ().Format (time .RFC3339Nano ), column , timeRange .To .UTC ().Format (time .RFC3339Nano )), nil
@@ -164,12 +166,12 @@ func evaluateMacro(name string, args []string, configStruct *queryConfigStruct)
164
166
return "" , err
165
167
case "__unixEpochFilter" :
166
168
if len (args ) == 0 || args [0 ] == "" {
167
- return "" , fmt .Errorf ("missing time column argument for macro %v" , name )
169
+ return "" , fmt .Errorf (missingColumnMessage , name )
168
170
}
169
171
return fmt .Sprintf ("%s >= %d AND %s <= %d" , args [0 ], timeRange .From .UTC ().Unix (), args [0 ], timeRange .To .UTC ().Unix ()), nil
170
172
case "__unixEpochNanoFilter" :
171
173
if len (args ) == 0 || args [0 ] == "" {
172
- return "" , fmt .Errorf ("missing time column argument for macro %v" , name )
174
+ return "" , fmt .Errorf (missingColumnMessage , name )
173
175
}
174
176
return fmt .Sprintf ("%s >= %d AND %s <= %d" , args [0 ], timeRange .From .UTC ().UnixNano (), args [0 ], timeRange .To .UTC ().UnixNano ()), nil
175
177
case "__unixEpochNanoFrom" :
0 commit comments