diff --git a/package.json b/package.json index 9c50bb3f59d..77ae286ecae 100644 --- a/package.json +++ b/package.json @@ -273,6 +273,7 @@ "@react-aria/menu": "3.10.0", "@react-aria/overlays": "3.15.0", "@react-aria/utils": "3.18.0", + "@react-awesome-query-builder/core": "6.4.1", "@react-awesome-query-builder/ui": "6.4.1", "@react-stately/collections": "3.9.0", "@react-stately/menu": "3.5.3", diff --git a/pkg/tsdb/influxdb/fsql/macro.go b/pkg/tsdb/influxdb/fsql/macro.go index b65bae1b7c2..163b3b26a28 100644 --- a/pkg/tsdb/influxdb/fsql/macro.go +++ b/pkg/tsdb/influxdb/fsql/macro.go @@ -15,13 +15,10 @@ var macros = sqlutil.Macros{ "timeGroupAlias": macroTimeGroupAlias, // The behaviors of timeFrom and timeTo as defined in the SDK are different - // from all other Grafana SQL plugins. Instead we'll take their - // implementations, rename them and define timeFrom and timeTo ourselves. - "timeRangeFrom": sqlutil.DefaultMacros["timeFrom"], - "timeRangeTo": sqlutil.DefaultMacros["timeTo"], - "timeRange": sqlutil.DefaultMacros["timeFilter"], - "timeTo": macroTo, - "timeFrom": macroFrom, + // from all other Grafana SQL plugins. Instead we'll take the implementations, + // rename them and define timeFrom and timeTo ourselves. + "timeTo": macroTo, + "timeFrom": macroFrom, } func macroTimeGroup(query *sqlutil.Query, args []string) (string, error) { @@ -84,10 +81,12 @@ func macroInterval(query *sqlutil.Query, _ []string) (string, error) { return fmt.Sprintf("interval '%d second'", int64(query.Interval.Seconds())), nil } +// https://docs.influxdata.com/influxdb/cloud-serverless/query-data/sql/cast-types/?t=CAST%28%29#cast-to-a-timestamp-type func macroFrom(query *sqlutil.Query, _ []string) (string, error) { return fmt.Sprintf("cast('%s' as timestamp)", query.TimeRange.From.Format(time.RFC3339)), nil } +// https://docs.influxdata.com/influxdb/cloud-serverless/query-data/sql/cast-types/?t=CAST%28%29#cast-to-a-timestamp-type func macroTo(query *sqlutil.Query, _ []string) (string, error) { return fmt.Sprintf("cast('%s' as timestamp)", query.TimeRange.To.Format(time.RFC3339)), nil } diff --git a/pkg/tsdb/influxdb/fsql/macro_test.go b/pkg/tsdb/influxdb/fsql/macro_test.go index 87268b1a4bd..9aeb8948faa 100644 --- a/pkg/tsdb/influxdb/fsql/macro_test.go +++ b/pkg/tsdb/influxdb/fsql/macro_test.go @@ -44,18 +44,6 @@ func TestMacros(t *testing.T) { in: `select * from x where $__timeFilter(time)`, out: `select * from x where time >= '2023-01-01T00:00:00Z' AND time <= '2023-01-01T00:10:00Z'`, }, - { - in: `select * from x where $__timeRangeFrom(time)`, - out: `select * from x where time >= '2023-01-01T00:00:00Z'`, - }, - { - in: `select * from x where $__timeRangeTo(time)`, - out: `select * from x where time <= '2023-01-01T00:10:00Z'`, - }, - { - in: `select * from x where $__timeRange(time)`, - out: `select * from x where time >= '2023-01-01T00:00:00Z' AND time <= '2023-01-01T00:10:00Z'`, - }, { in: `select * from x where time >= $__timeFrom`, out: `select * from x where time >= cast('2023-01-01T00:00:00Z' as timestamp)`, diff --git a/public/app/plugins/datasource/influxdb/fsql/fields.ts b/public/app/plugins/datasource/influxdb/fsql/fields.ts index 96eb122c35e..6177234a3db 100644 --- a/public/app/plugins/datasource/influxdb/fsql/fields.ts +++ b/public/app/plugins/datasource/influxdb/fsql/fields.ts @@ -29,6 +29,7 @@ export function mapFieldsToTypes(columns: SQLSelectableValue[]) { type = 'date'; break; } + case 'TIMESTAMP(NANOSECOND, NONE)': case 'DATETIME': { type = 'datetime'; break; diff --git a/public/app/plugins/datasource/influxdb/fsql/sqlUtil.test.ts b/public/app/plugins/datasource/influxdb/fsql/sqlUtil.test.ts index 163df82d4d4..d05aad13d4d 100644 --- a/public/app/plugins/datasource/influxdb/fsql/sqlUtil.test.ts +++ b/public/app/plugins/datasource/influxdb/fsql/sqlUtil.test.ts @@ -6,7 +6,7 @@ import { toRawSql } from './sqlUtil'; describe('toRawSql', () => { it('should render sql properly', () => { - const expected = 'SELECT host FROM iox.value1 LIMIT 50'; + const expected = 'SELECT host FROM iox.value1 WHERE time >= $__timeFrom AND time <= $__timeTo LIMIT 50'; const testQuery: SQLQuery = { refId: 'A', sql: { diff --git a/public/app/plugins/datasource/influxdb/fsql/sqlUtil.ts b/public/app/plugins/datasource/influxdb/fsql/sqlUtil.ts index 2064345fab3..f4b8eea30bb 100644 --- a/public/app/plugins/datasource/influxdb/fsql/sqlUtil.ts +++ b/public/app/plugins/datasource/influxdb/fsql/sqlUtil.ts @@ -32,8 +32,10 @@ export function toRawSql({ sql, dataset, table }: SQLQuery): string { rawQuery += `FROM ${dataset}.${table} `; } + // $__timeFrom and $__timeTo will be interpolated on the backend + rawQuery += `WHERE time >= $__timeFrom AND time <= $__timeTo `; if (sql.whereString) { - rawQuery += `WHERE ${sql.whereString} `; + rawQuery += `AND ${sql.whereString} `; } if (sql.groupBy?.[0]?.property.name) { diff --git a/yarn.lock b/yarn.lock index 533b835642a..c24aaed5cf2 100644 --- a/yarn.lock +++ b/yarn.lock @@ -6641,7 +6641,7 @@ __metadata: languageName: node linkType: hard -"@react-awesome-query-builder/core@npm:^6.4.1": +"@react-awesome-query-builder/core@npm:6.4.1, @react-awesome-query-builder/core@npm:^6.4.1": version: 6.4.1 resolution: "@react-awesome-query-builder/core@npm:6.4.1" dependencies: @@ -17670,6 +17670,7 @@ __metadata: "@react-aria/menu": 3.10.0 "@react-aria/overlays": 3.15.0 "@react-aria/utils": 3.18.0 + "@react-awesome-query-builder/core": 6.4.1 "@react-awesome-query-builder/ui": 6.4.1 "@react-stately/collections": 3.9.0 "@react-stately/menu": 3.5.3