time shift literals also (#15433)

This commit is contained in:
Vadim Ogievetsky 2023-11-28 03:12:35 -08:00 committed by GitHub
parent eb6b9cb4a9
commit bd6afb7056
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 30 additions and 11 deletions

View File

@ -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)`,
);
});
});

View File

@ -17,11 +17,16 @@
*/
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;
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, '<interval>')
@ -34,6 +39,7 @@ export function shiftTimeInWhere(where: SqlExpression, period: string): SqlExpre
if (effectiveFunctionName === 'TIME_SHIFT' || effectiveFunctionName === 'MAX_DATA_TIME') {
return F('TIME_SHIFT', ex, period, -1);
}
}
return ex;
}) as SqlExpression;