Skip to content

Commit

Permalink
Single Quote String Literal for function invoker results (#5456)
Browse files Browse the repository at this point in the history
  • Loading branch information
xiangfu0 committed May 28, 2020
1 parent b6cb44c commit 7f10c5c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 3 deletions.
Expand Up @@ -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));
Expand Down
Expand Up @@ -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);
}

Expand All @@ -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);
Expand All @@ -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);
Expand Down
Expand Up @@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit 7f10c5c

Please sign in to comment.