diff --git a/pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java b/pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java index 45b3274842f..2aec8ece449 100644 --- a/pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java +++ b/pinot-common/src/main/java/org/apache/pinot/sql/parsers/CalciteSqlParser.java @@ -676,6 +676,9 @@ protected static Expression invokeCompileTimeFunctionExpression(Expression funcE try { FunctionInvoker invoker = new FunctionInvoker(functionInfo); Object result = invoker.process(arguments); + if (result instanceof String) { + result = String.format("'%s'", result); + } return RequestUtils.getLiteralExpression(result); } catch (Exception e) { throw new SqlCompilationException(new IllegalArgumentException("Unsupported function - " + funcName, e)); diff --git a/pinot-common/src/test/java/org/apache/pinot/sql/parsers/CalciteSqlCompilerTest.java b/pinot-common/src/test/java/org/apache/pinot/sql/parsers/CalciteSqlCompilerTest.java index ac0a5cfcba2..aec79170dac 100644 --- a/pinot-common/src/test/java/org/apache/pinot/sql/parsers/CalciteSqlCompilerTest.java +++ b/pinot-common/src/test/java/org/apache/pinot/sql/parsers/CalciteSqlCompilerTest.java @@ -1534,7 +1534,7 @@ public void testCompilationInvokedNestedFunctions() { Function greaterThan = pinotQuery.getFilterExpression().getFunctionCall(); String today = greaterThan.getOperands().get(1).getLiteral().getStringValue(); String expectedTodayStr = - Instant.now().atZone(ZoneId.of("UTC")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd z")); + "'" + Instant.now().atZone(ZoneId.of("UTC")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd z")) + "'"; Assert.assertEquals(today, expectedTodayStr); } @@ -1557,7 +1557,7 @@ public void testCompileTimeExpression() Assert.assertTrue(expression.getLiteral() != null); String today = expression.getLiteral().getStringValue(); String expectedTodayStr = - Instant.now().atZone(ZoneId.of("UTC")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd z")); + "'" + Instant.now().atZone(ZoneId.of("UTC")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd z")) + "'"; Assert.assertEquals(today, expectedTodayStr); expression = CalciteSqlParser.compileToExpression("toDateTime(playerName)"); Assert.assertTrue(expression.getFunctionCall() != null); @@ -1575,7 +1575,7 @@ public void testCompileTimeExpression() Assert.assertTrue(expression.getFunctionCall() != null); expression = CalciteSqlParser.invokeCompileTimeFunctionExpression(expression); Assert.assertTrue(expression.getLiteral() != null); - Assert.assertEquals(expression.getLiteral().getFieldValue(), "emaNreyalp"); + Assert.assertEquals(expression.getLiteral().getFieldValue(), "'emaNreyalp'"); expression = CalciteSqlParser.compileToExpression("count(*)"); Assert.assertTrue(expression.getFunctionCall() != null); expression = CalciteSqlParser.invokeCompileTimeFunctionExpression(expression); diff --git a/pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/OfflineClusterIntegrationTest.java b/pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/OfflineClusterIntegrationTest.java index f433a3e0b1c..223602c6377 100644 --- a/pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/OfflineClusterIntegrationTest.java +++ b/pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/OfflineClusterIntegrationTest.java @@ -24,6 +24,9 @@ import com.google.common.collect.ImmutableList; import java.io.File; import java.io.IOException; +import java.time.Instant; +import java.time.ZoneId; +import java.time.format.DateTimeFormatter; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; @@ -308,6 +311,17 @@ public void testInvertedIndexTriggering() }, 600_000L, "Failed to generate inverted index"); } + @Test + public void testTimeFunc() + throws Exception { + String sqlQuery = "SELECT toDateTime(now(), 'yyyy-MM-dd z') FROM mytable"; + JsonNode response = postSqlQuery(sqlQuery, _brokerBaseApiUrl); + String todayStr = response.get("resultTable").get("rows").get(0).get(0).asText(); + String expectedTodayStr = + Instant.now().atZone(ZoneId.of("UTC")).format(DateTimeFormatter.ofPattern("yyyy-MM-dd z")); + Assert.assertEquals(todayStr, expectedTodayStr); + } + @Test public void testRangeIndexTriggering() throws Exception {