From bd6afb7056610a060cd8d85746436763345b92de Mon Sep 17 00:00:00 2001 From: Vadim Ogievetsky Date: Tue, 28 Nov 2023 03:12:35 -0800 Subject: [PATCH] time shift literals also (#15433) --- .../explore-view/modules/utils/utils.spec.ts | 13 +++++++++ .../views/explore-view/modules/utils/utils.ts | 28 +++++++++++-------- 2 files changed, 30 insertions(+), 11 deletions(-) diff --git a/web-console/src/views/explore-view/modules/utils/utils.spec.ts b/web-console/src/views/explore-view/modules/utils/utils.spec.ts index 76f6f5811c4..9db276e6a0b 100644 --- a/web-console/src/views/explore-view/modules/utils/utils.spec.ts +++ b/web-console/src/views/explore-view/modules/utils/utils.spec.ts @@ -42,4 +42,17 @@ describe('shiftTimeInWhere', () => { `(TIME_SHIFT(TIME_SHIFT(MAX_DATA_TIME(), 'PT1H', -1), 'PT1H', -1) <= "__time" AND "__time" < TIME_SHIFT(MAX_DATA_TIME(), 'PT1H', -1))`, ); }); + + it('works with relative time (specific timestamps)', () => { + expect( + shiftTimeInWhere( + SqlExpression.parse( + `TIMESTAMP '2016-06-27 20:31:02.498' <= "__time" AND "__time" < TIMESTAMP '2016-06-27 21:31:02.498'`, + ), + 'PT1H', + ).toString(), + ).toEqual( + `TIME_SHIFT(TIMESTAMP '2016-06-27 20:31:02.498', 'PT1H', -1) <= "__time" AND "__time" < TIME_SHIFT(TIMESTAMP '2016-06-27 21:31:02.498', 'PT1H', -1)`, + ); + }); }); diff --git a/web-console/src/views/explore-view/modules/utils/utils.ts b/web-console/src/views/explore-view/modules/utils/utils.ts index 4aeac16e603..7320ca52404 100644 --- a/web-console/src/views/explore-view/modules/utils/utils.ts +++ b/web-console/src/views/explore-view/modules/utils/utils.ts @@ -17,22 +17,28 @@ */ import type { SqlExpression } from '@druid-toolkit/query'; -import { F, SqlFunction } from '@druid-toolkit/query'; +import { F, SqlFunction, SqlLiteral } from '@druid-toolkit/query'; export function shiftTimeInWhere(where: SqlExpression, period: string): SqlExpression { return where.walk(ex => { - if (!(ex instanceof SqlFunction)) return ex; - const effectiveFunctionName = ex.getEffectiveFunctionName(); + if (ex instanceof SqlLiteral) { + // Works with: __time < TIMESTAMP '2022-01-02 03:04:05' + if (ex.isDate()) { + return F('TIME_SHIFT', ex, period, -1); + } + } else if (ex instanceof SqlFunction) { + const effectiveFunctionName = ex.getEffectiveFunctionName(); - // Works with: TIME_IN_INTERVAL(__time, '') - if (effectiveFunctionName === 'TIME_IN_INTERVAL') { - return ex.changeArgs(ex.args!.change(0, F('TIME_SHIFT', ex.getArg(0), period, 1))); - } + // Works with: TIME_IN_INTERVAL(__time, '') + if (effectiveFunctionName === 'TIME_IN_INTERVAL') { + return ex.changeArgs(ex.args!.change(0, F('TIME_SHIFT', ex.getArg(0), period, 1))); + } - // Works with: TIME_SHIFT(...) <= __time - // and: __time < MAX_DATA_TIME() - if (effectiveFunctionName === 'TIME_SHIFT' || effectiveFunctionName === 'MAX_DATA_TIME') { - return F('TIME_SHIFT', ex, period, -1); + // Works with: TIME_SHIFT(...) <= __time + // and: __time < MAX_DATA_TIME() + if (effectiveFunctionName === 'TIME_SHIFT' || effectiveFunctionName === 'MAX_DATA_TIME') { + return F('TIME_SHIFT', ex, period, -1); + } } return ex;