fx19880617 commented on a change in pull request #5513: URL: https://github.com/apache/incubator-pinot/pull/5513#discussion_r437774641
########## 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: so if the expression is literal, then the column name can only be literal, unless there is an `AS` function. ---------------------------------------------------------------- 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