Skip to content

Commit d0b4138

Browse files
authored
Add a $__timeTzFilter macro to handle timestamps with time zone (#82)
1 parent 2005a4d commit d0b4138

File tree

3 files changed

+12
-2
lines changed

3 files changed

+12
-2
lines changed

README.md

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -72,8 +72,9 @@ Macro example | Description
7272
------------------------------------------------------ | -------------
7373
`$__time(dateColumn)` | Will be replaced by an expression to convert to a UNIX timestamp and rename the column to `time`. For example, *TRY_TO_TIMESTAMP_NTZ(dateColumn) as time*
7474
`$__timeEpoch(dateColumn)` | Will be replaced by an expression to convert to a UNIX timestamp and rename the column to `time`.
75-
`$__timeFilter(dateColumn)` | Will be replaced by a time range filter using the specified column name. For example, *time > CONVERT_TIMEZONE('UTC', 'UTC', '2023-12-13T23:21:44Z'::timestamp_ntz) AND time < CONVERT_TIMEZONE('UTC', 'UTC', '2023-12-13T23:22:44Z'::timestamp_ntz)*
76-
`$__timeFilter(dateColumn, timezone)` | Will be replaced by a time range filter using the specified column name. For example, *time > CONVERT_TIMEZONE('UTC', 'America/New_York', '2023-12-13T23:22:46Z'::timestamp_ntz) AND time < CONVERT_TIMEZONE('UTC', 'America/New_York', '2023-12-13T23:23:46Z'::timestamp_ntz)*
75+
`$__timeFilter(dateColumn)` | Will be replaced by a time range filter using the specified column name, which is expected to be a timestamp without time zone. For example, *time > CONVERT_TIMEZONE('UTC', 'UTC', '2023-12-13T23:21:44Z'::timestamp_ntz) AND time < CONVERT_TIMEZONE('UTC', 'UTC', '2023-12-13T23:22:44Z'::timestamp_ntz)*
76+
`$__timeFilter(dateColumn, timezone)` | Will be replaced by a time range filter using the specified column name, which is expected to be a timestamp without time zone. For example, *time > CONVERT_TIMEZONE('UTC', 'America/New_York', '2023-12-13T23:22:46Z'::timestamp_ntz) AND time < CONVERT_TIMEZONE('UTC', 'America/New_York', '2023-12-13T23:23:46Z'::timestamp_ntz)*
77+
`$__timeTzFilter(dateColumn)` | Will be replaced by a time range filter using the specified column name, which is expected to be a timestamp with time zone. For example, *time > '2023-12-13T23:21:44Z'::timestamp_tz AND time < '2023-12-13T23:22:44Z'::timestamp_tz*
7778
`$__timeFrom()` | Will be replaced by the start of the currently active time selection. For example, *1494410783*
7879
`$__timeTo()` | Will be replaced by the end of the currently active time selection. For example, *1494410983*
7980
`$__timeGroup(dateColumn,'5m')` | Will be replaced by an expression usable in GROUP BY clause. For example, *floor(extract(epoch from dateColumn)/120)*120*

pkg/macros.go

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,12 @@ func evaluateMacro(name string, args []string, configStruct *queryConfigStruct)
9595
timezone = args[1]
9696
}
9797
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
98+
case "__timeTzFilter":
99+
if len(args) == 0 {
100+
return "", fmt.Errorf("missing time column argument for macro %v", name)
101+
}
102+
column := args[0]
103+
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
98104
case "__timeFrom":
99105
return fmt.Sprintf("'%s'", timeRange.From.UTC().Format(time.RFC3339Nano)), nil
100106
case "__timeTo":

pkg/macros_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,9 @@ func TestEvaluateMacro(t *testing.T) {
3838
{name: "__timeFilter", args: []string{}, err: "missing time column argument for macro __timeFilter"},
3939
{name: "__timeFilter", args: []string{"col"}, config: configStruct, response: "col > CONVERT_TIMEZONE('UTC', 'UTC', '" + timeRange.From.UTC().Format(time.RFC3339Nano) + "'::timestamp_ntz) AND col < CONVERT_TIMEZONE('UTC', 'UTC', '" + timeRange.To.UTC().Format(time.RFC3339Nano) + "'::timestamp_ntz)"},
4040
{name: "__timeFilter", args: []string{"col", "'America/New_York'"}, config: configStruct, response: "col > CONVERT_TIMEZONE('UTC', 'America/New_York', '" + timeRange.From.UTC().Format(time.RFC3339Nano) + "'::timestamp_ntz) AND col < CONVERT_TIMEZONE('UTC', 'America/New_York', '" + timeRange.To.UTC().Format(time.RFC3339Nano) + "'::timestamp_ntz)"},
41+
// __timeTzFilter
42+
{name: "__timeTzFilter", args: []string{}, err: "missing time column argument for macro __timeTzFilter"},
43+
{name: "__timeTzFilter", args: []string{"col"}, config: configStruct, response: "col > '" + timeRange.From.UTC().Format(time.RFC3339Nano) + "'::timestamp_tz AND col < '" + timeRange.To.UTC().Format(time.RFC3339Nano) + "'::timestamp_tz"},
4144
// __timeFrom
4245
{name: "__timeFrom", args: []string{}, config: configStruct, response: "'" + timeRange.From.UTC().Format(time.RFC3339Nano) + "'"},
4346
// __timeTo

0 commit comments

Comments
 (0)