kishoreg commented on a change in pull request #4216: PQL -> SQL enhancement - phase 1 - new Pinot Query Struct URL: https://github.com/apache/incubator-pinot/pull/4216#discussion_r292143790
########## File path: pinot-common/src/main/java/org/apache/pinot/pql/parsers/PinotQuery2BrokerRequestConverter.java ########## @@ -0,0 +1,362 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.pinot.pql.parsers; + +import java.util.ArrayList; +import java.util.HashMap; +import java.util.List; +import java.util.Map; +import org.apache.pinot.common.request.AggregationInfo; +import org.apache.pinot.common.request.BrokerRequest; +import org.apache.pinot.common.request.Expression; +import org.apache.pinot.common.request.ExpressionType; +import org.apache.pinot.common.request.FilterOperator; +import org.apache.pinot.common.request.FilterQuery; +import org.apache.pinot.common.request.FilterQueryMap; +import org.apache.pinot.common.request.Function; +import org.apache.pinot.common.request.GroupBy; +import org.apache.pinot.common.request.Literal; +import org.apache.pinot.common.request.PinotQuery; +import org.apache.pinot.common.request.QuerySource; +import org.apache.pinot.common.request.QueryType; +import org.apache.pinot.common.request.Selection; +import org.apache.pinot.common.request.SelectionSort; +import org.apache.pinot.pql.parsers.pql2.ast.FilterKind; + + +public class PinotQuery2BrokerRequestConverter { + + static Map<FilterKind, FilterOperator> filterOperatorMapping; + + public BrokerRequest convert(PinotQuery pinotQuery) { + BrokerRequest brokerRequest = new BrokerRequest(); + + //Query Source + QuerySource querySource = new QuerySource(); + querySource.setTableName(pinotQuery.getDataSource().getTableName()); + brokerRequest.setQuerySource(querySource); + + handleFilter(pinotQuery, brokerRequest); + + //Handle select list + handleSelectList(pinotQuery, brokerRequest); + + //Handle order by + handleOrderBy(pinotQuery, brokerRequest); + + //Handle group by + handleGroupBy(pinotQuery, brokerRequest); + + //Query Type + QueryType queryType = new QueryType(); + if (brokerRequest.getAggregationsInfo() != null && brokerRequest.getAggregationsInfo().size() > 0) { + if (brokerRequest.getGroupBy() != null) { + queryType.setHasGroup_by(true); + } else { + queryType.setHasAggregation(true); + } + } else { + queryType.setHasSelection(true); + } + // Commenting this out since the current code does not set it. + // brokerRequest.setQueryType(queryType); + + //TODO: these should not be part of the query? + //brokerRequest.setEnableTrace(); + //brokerRequest.setDebugOptions(); + brokerRequest.setQueryOptions(pinotQuery.getQueryOptions()); + //brokerRequest.setBucketHashKey(); + //brokerRequest.setDuration(); + + return brokerRequest; + } + + private void handleOrderBy(PinotQuery pinotQuery, BrokerRequest brokerRequest) { + if (brokerRequest.getSelections() == null || pinotQuery.getOrderByList() == null) { + return; + } + List<SelectionSort> sortSequenceList = new ArrayList<>(); + final List<Expression> orderByList = pinotQuery.getOrderByList(); + for (Expression orderByExpr : orderByList) { + SelectionSort selectionSort = new SelectionSort(); + if (orderByExpr.getFunctionCall().getOperator().equalsIgnoreCase("ASC")) { + selectionSort.setIsAsc(true); + } else { + selectionSort.setIsAsc(false); + } + selectionSort.setColumn(orderByExpr.getFunctionCall().getOperands().get(0).getIdentifier().getName()); Review comment: Actually, that line is buggy. It assumes that orderby is always a function. Will fix that. Good catch. ---------------------------------------------------------------- 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 With regards, Apache Git Services --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org