Jackie-Jiang commented on a change in pull request #5513: URL: https://github.com/apache/incubator-pinot/pull/5513#discussion_r439136268
########## File path: pinot-integration-tests/src/test/java/org/apache/pinot/integration/tests/OfflineClusterIntegrationTest.java ########## @@ -317,6 +317,43 @@ public void testTimeFunc() assertEquals(todayStr, expectedTodayStr); } + @Test + public void testLiteralOnlyFunc() + throws Exception { + long currentTsMin = System.currentTimeMillis(); + String sqlQuery = "SELECT 1, now() as currentTs, 'abc', toDateTime(now(), 'yyyy-MM-dd z') as today, now()"; + JsonNode response = postSqlQuery(sqlQuery, _brokerBaseApiUrl); + long currentTsMax = System.currentTimeMillis(); + + assertEquals(response.get("resultTable").get("dataSchema").get("columnNames").get(0).asText(), "1"); + assertEquals(response.get("resultTable").get("dataSchema").get("columnNames").get(1).asText(), "currentTs"); + assertEquals(response.get("resultTable").get("dataSchema").get("columnNames").get(2).asText(), "abc"); + assertEquals(response.get("resultTable").get("dataSchema").get("columnNames").get(3).asText(), "today"); + String nowColumnName = response.get("resultTable").get("dataSchema").get("columnNames").get(4).asText(); Review comment: Ideally the column name should be `now()` right? This is not critical, but better put a TODO? ########## File path: pinot-broker/src/main/java/org/apache/pinot/broker/requesthandler/BaseBrokerRequestHandler.java ########## @@ -435,6 +455,115 @@ static void handleQueryLimitOverride(BrokerRequest brokerRequest, int queryLimit } } + /** + * Check if a SQL parsed BrokerRequest is a literal only query. + * @param brokerRequest + * @return true if this query selects only Literals + * + */ + @VisibleForTesting + static boolean isLiteralOnlyQuery(BrokerRequest brokerRequest) { + if (brokerRequest.getPinotQuery() != null) { + for (Expression e: brokerRequest.getPinotQuery().getSelectList()) { + if (!CalciteSqlParser.isLiteralOnlyExpression(e)) { + return false; + } + } + return true; + } + return false; + } + + /** + * Compute BrokerResponse for literal only query. + * + * @param brokerRequest + * @param compilationStartTimeNs + * @param requestStatistics + * @return BrokerResponse + */ + private BrokerResponse processLiteralOnlyBrokerRequest(BrokerRequest brokerRequest, long compilationStartTimeNs, + RequestStatistics requestStatistics) + throws IllegalStateException { + BrokerResponseNative brokerResponse = new BrokerResponseNative(); + List<String> columnNames = new ArrayList<>(); + List<DataSchema.ColumnDataType> columnTypes = new ArrayList<>(); + List<Object> row = new ArrayList<>(); + for (Expression e : brokerRequest.getPinotQuery().getSelectList()) { + computeResultsForExpression(e, columnNames, columnTypes, row); + } + DataSchema dataSchema = + new DataSchema(columnNames.toArray(new String[0]), columnTypes.toArray(new DataSchema.ColumnDataType[0])); + List<Object[]> rows = new ArrayList<>(); + rows.add(row.toArray()); + ResultTable resultTable = new ResultTable(dataSchema, rows); + brokerResponse.setResultTable(resultTable); + + long totalTimeMs = TimeUnit.NANOSECONDS.toMillis(System.nanoTime() - compilationStartTimeNs); + brokerResponse.setTimeUsedMs(totalTimeMs); + requestStatistics.setQueryProcessingTime(totalTimeMs); + requestStatistics.setStatistics(brokerResponse); + return brokerResponse; + } + + private void computeResultsForExpression(Expression e, List<String> columnNames, List<DataSchema.ColumnDataType> columnTypes, + List<Object> row) { + if (e.getType() == ExpressionType.LITERAL) { + computeResultsForLiteral(e.getLiteral(), columnNames, columnTypes, row); + } + if (e.getType() == ExpressionType.FUNCTION) { + if (e.getFunctionCall().getOperator().equalsIgnoreCase(SqlKind.AS.toString())) { + String columnName = e.getFunctionCall().getOperands().get(1).getIdentifier().getName(); + computeResultsForExpression(e.getFunctionCall().getOperands().get(0), columnNames, columnTypes, row); + columnNames.set(columnNames.size() - 1, columnName); + } else { + throw new IllegalStateException( + "No able to compute results for function - " + e.getFunctionCall().getOperator()); + } + } + } + + private void computeResultsForLiteral(Literal literal, List<String> columnNames, List<DataSchema.ColumnDataType> columnTypes, + List<Object> row) { + Object fieldValue = literal.getFieldValue(); + columnNames.add(fieldValue.toString()); Review comment: The problem here is that we already lost the context of the function (e.g. `now()`). Maybe add a TODO here to pass in the original expression and use that as the column name? ---------------------------------------------------------------- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org