mirror of https://github.com/apache/druid.git
time shift literals also (#15433)
This commit is contained in:
parent
eb6b9cb4a9
commit
bd6afb7056
|
@ -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)`,
|
||||
);
|
||||
});
|
||||
});
|
||||
|
|
|
@ -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;
|
||||
|
|
Loading…
Reference in New Issue