Skip to content

Commit

Permalink
Override return type for NOW to TIMESTAMP in the v2 engine (#14614)
Browse files Browse the repository at this point in the history
  • Loading branch information
yashmayya authored Dec 15, 2024
1 parent fda93ab commit e89ac94
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -631,10 +631,11 @@ public void testLiteralOnlyFunc()
throws Exception {
long queryStartTimeMs = System.currentTimeMillis();
String sqlQuery =
"SELECT 1, now() as currentTs, ago('PT1H') as oneHourAgoTs, 'abc', toDateTime(now(), 'yyyy-MM-dd z') as "
+ "today, now(), ago('PT1H'), encodeUrl('key1=value 1&key2=value@!$2&key3=value%3') as encodedUrl, "
+ "decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253') as decodedUrl, toBase64"
+ "(toUtf8('hello!')) as toBase64, fromUtf8(fromBase64('aGVsbG8h')) as fromBase64";
"SELECT 1, cast(now() as bigint) as currentTs, ago('PT1H') as oneHourAgoTs, 'abc', "
+ "toDateTime(now(), 'yyyy-MM-dd z') as today, cast(now() as bigint), ago('PT1H'), "
+ "encodeUrl('key1=value 1&key2=value@!$2&key3=value%3') as encodedUrl, "
+ "decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253') as decodedUrl, "
+ "toBase64(toUtf8('hello!')) as toBase64, fromUtf8(fromBase64('aGVsbG8h')) as fromBase64";
JsonNode response = postQuery(sqlQuery);
long queryEndTimeMs = System.currentTimeMillis();

Expand All @@ -656,8 +657,9 @@ public void testLiteralOnlyFunc()
JsonNode results = resultTable.get("rows").get(0);
assertEquals(results.get(0).asInt(), 1);
long nowResult = results.get(1).asLong();
assertTrue(nowResult >= queryStartTimeMs);
assertTrue(nowResult <= queryEndTimeMs);
// Timestamp granularity is seconds
assertTrue(nowResult >= ((queryStartTimeMs / 1000) * 1000));
assertTrue(nowResult <= ((queryEndTimeMs / 1000) * 1000));
long oneHourAgoResult = results.get(2).asLong();
assertTrue(oneHourAgoResult >= queryStartTimeMs - TimeUnit.HOURS.toMillis(1));
assertTrue(oneHourAgoResult <= queryEndTimeMs - TimeUnit.HOURS.toMillis(1));
Expand All @@ -669,8 +671,8 @@ public void testLiteralOnlyFunc()
String dateTimeResult = results.get(4).asText();
assertTrue(dateTimeResult.equals(queryStartTimeDay) || dateTimeResult.equals(queryEndTimeDay));
nowResult = results.get(5).asLong();
assertTrue(nowResult >= queryStartTimeMs);
assertTrue(nowResult <= queryEndTimeMs);
assertTrue(nowResult >= ((queryStartTimeMs / 1000) * 1000));
assertTrue(nowResult <= ((queryEndTimeMs / 1000) * 1000));
oneHourAgoResult = results.get(6).asLong();
assertTrue(oneHourAgoResult >= queryStartTimeMs - TimeUnit.HOURS.toMillis(1));
assertTrue(oneHourAgoResult <= queryEndTimeMs - TimeUnit.HOURS.toMillis(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1218,10 +1218,11 @@ public void testLiteralOnlyFuncV2()
setUseMultiStageQueryEngine(true);
long queryStartTimeMs = System.currentTimeMillis();
String sqlQuery =
"SELECT 1, now() as currentTs, ago('PT1H') as oneHourAgoTs, 'abc', toDateTime(now(), 'yyyy-MM-dd z') as "
+ "today, now(), ago('PT1H'), encodeUrl('key1=value 1&key2=value@!$2&key3=value%3') as encodedUrl, "
+ "decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253') as decodedUrl, toBase64"
+ "(toUtf8('hello!')) as toBase64, fromUtf8(fromBase64('aGVsbG8h')) as fromBase64";
"SELECT 1, cast(now() as bigint) as currentTs, ago('PT1H') as oneHourAgoTs, 'abc', "
+ "toDateTime(now(), 'yyyy-MM-dd z') as today, cast(now() as bigint), ago('PT1H'), "
+ "encodeUrl('key1=value 1&key2=value@!$2&key3=value%3') as encodedUrl, "
+ "decodeUrl('key1%3Dvalue+1%26key2%3Dvalue%40%21%242%26key3%3Dvalue%253') as decodedUrl, "
+ "toBase64(toUtf8('hello!')) as toBase64, fromUtf8(fromBase64('aGVsbG8h')) as fromBase64";
JsonNode response = postQuery(sqlQuery);
long queryEndTimeMs = System.currentTimeMillis();

Expand Down Expand Up @@ -1256,8 +1257,9 @@ public void testLiteralOnlyFuncV2()
JsonNode results = resultTable.get("rows").get(0);
assertEquals(results.get(0).asInt(), 1);
long nowResult = results.get(1).asLong();
assertTrue(nowResult >= queryStartTimeMs);
assertTrue(nowResult <= queryEndTimeMs);
// Timestamp granularity is seconds
assertTrue(nowResult >= ((queryStartTimeMs / 1000) * 1000));
assertTrue(nowResult <= ((queryEndTimeMs / 1000) * 1000));
long oneHourAgoResult = results.get(2).asLong();
assertTrue(oneHourAgoResult >= queryStartTimeMs - TimeUnit.HOURS.toMillis(1));
assertTrue(oneHourAgoResult <= queryEndTimeMs - TimeUnit.HOURS.toMillis(1));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,9 @@ public static PinotOperatorTable instance() {
// TODO: Consider unifying the return type to Timestamp
new PinotSqlFunction("FROM_DATE_TIME", ReturnTypes.TIMESTAMP_NULLABLE, OperandTypes.family(
List.of(SqlTypeFamily.CHARACTER, SqlTypeFamily.CHARACTER, SqlTypeFamily.CHARACTER, SqlTypeFamily.ANY),
i -> i > 1))
i -> i > 1)),

new PinotSqlFunction("NOW", ReturnTypes.TIMESTAMP, OperandTypes.NILADIC)
);

private static final List<Pair<SqlOperator, List<String>>> PINOT_OPERATORS_WITH_ALIASES = List.of(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,8 @@ protected Object[][] provideQueries() {
new Object[]{"SELECT JSON_EXTRACT_SCALAR(col1, '$.foo', 'STRING_ARRAY') FROM a"},
new Object[]{"SELECT ts_timestamp FROM a WHERE ts_timestamp BETWEEN TIMESTAMP '2016-01-01 00:00:00' AND "
+ "TIMESTAMP '2016-01-01 10:00:00'"},
new Object[]{"SELECT ts_timestamp FROM a WHERE ts_timestamp >= CAST(1454284798000 AS TIMESTAMP)"}
new Object[]{"SELECT ts_timestamp FROM a WHERE ts_timestamp >= CAST(1454284798000 AS TIMESTAMP)"},
new Object[]{"SELECT TIMESTAMPADD(day, 10, NOW()) FROM a"}
};
}

Expand Down

0 comments on commit e89ac94

Please sign in to comment.