@Test public void testServerQueryBackwardCompatible() { String pqlQuery = "SELECT foo, bar FROM testTable LIMIT 50, 100"; BrokerRequest brokerRequest = new Pql2Compiler().compileToBrokerRequest(pqlQuery); brokerRequest.setLimit(0); QueryContext queryContext = BrokerRequestToQueryContextConverter.convert(brokerRequest); List<ExpressionContext> selectExpressions = queryContext.getSelectExpressions(); assertEquals(selectExpressions.size(), 2); assertEquals(selectExpressions.get(0), ExpressionContext.forIdentifier("foo")); assertEquals(selectExpressions.get(0).toString(), "foo"); assertEquals(selectExpressions.get(1), ExpressionContext.forIdentifier("bar")); assertEquals(selectExpressions.get(1).toString(), "bar"); assertTrue(queryContext.getAliasMap().isEmpty()); assertNull(queryContext.getFilter()); List<OrderByExpressionContext> orderByExpressions = queryContext.getOrderByExpressions(); assertNull(orderByExpressions); assertNull(queryContext.getHavingFilter()); assertEquals(queryContext.getLimit(), 100); assertEquals(queryContext.getOffset(), 50); assertEquals(QueryContextUtils.getAllColumns(queryContext), new HashSet<>(Arrays.asList("foo", "bar"))); assertFalse(QueryContextUtils.isAggregationQuery(queryContext)); }
public static QueryContext convert(BrokerRequest brokerRequest) { PinotQuery pinotQuery = brokerRequest.getPinotQuery(); List<ExpressionContext> selectExpressions; Map<ExpressionContext, String> aliasMap; List<ExpressionContext> groupByExpressions = null; int limit; int offset = 0; if (pinotQuery != null) { aliasMap = new HashMap<>(); List<Expression> selectList = pinotQuery.getSelectList(); selectExpressions = new ArrayList<>(selectList.size()); for (Expression thriftExpression : selectList) { ExpressionContext expression; if (thriftExpression.getType() == ExpressionType.FUNCTION && thriftExpression.getFunctionCall().getOperator() .equalsIgnoreCase("AS")) { List<Expression> operands = thriftExpression.getFunctionCall().getOperands(); expression = QueryContextConverterUtils.getExpression(operands.get(0)); aliasMap.put(expression, operands.get(1).getIdentifier().getName()); } else { expression = QueryContextConverterUtils.getExpression(thriftExpression); } selectExpressions.add(expression); } List<Expression> groupByList = pinotQuery.getGroupByList(); if (CollectionUtils.isNotEmpty(groupByList)) { groupByExpressions = new ArrayList<>(groupByList.size()); for (Expression thriftExpression : groupByList) { groupByExpressions.add(QueryContextConverterUtils.getExpression(thriftExpression)); } } limit = pinotQuery.getLimit(); offset = pinotQuery.getOffset(); } else { aliasMap = Collections.emptyMap(); Selection selections = brokerRequest.getSelections(); if (selections != null) { List<String> selectionColumns = selections.getSelectionColumns(); selectExpressions = new ArrayList<>(selectionColumns.size()); for (String expression : selectionColumns) { selectExpressions.add(QueryContextConverterUtils.getExpression(expression)); } groupByExpressions = null; limit = brokerRequest.getLimit(); offset = selections.getOffset(); if (limit == 0) { limit = selections.getSize(); } } else { List<AggregationInfo> aggregationsInfo = brokerRequest.getAggregationsInfo(); selectExpressions = new ArrayList<>(aggregationsInfo.size()); for (AggregationInfo aggregationInfo : aggregationsInfo) { String functionName = StringUtils.remove(aggregationInfo.getAggregationType(), '_'); List<String> stringExpressions = aggregationInfo.getExpressions(); int numArguments = stringExpressions.size(); List<ExpressionContext> arguments = new ArrayList<>(numArguments); if (functionName.equalsIgnoreCase(AggregationFunctionType.DISTINCT.getName())) { for (String expression : stringExpressions) { arguments.add(QueryContextConverterUtils.getExpression(expression)); } } else { arguments.add(QueryContextConverterUtils.getExpression(stringExpressions.get(0))); for (int i = 1; i < numArguments; i++) { arguments.add(ExpressionContext.forLiteral(stringExpressions.get(i))); } } FunctionContext function = new FunctionContext(FunctionContext.Type.AGGREGATION, functionName, arguments); selectExpressions.add(ExpressionContext.forFunction(function)); } GroupBy groupBy = brokerRequest.getGroupBy(); if (groupBy != null) { List<String> stringExpressions = groupBy.getExpressions(); groupByExpressions = new ArrayList<>(stringExpressions.size()); for (String stringExpression : stringExpressions) { groupByExpressions.add(QueryContextConverterUtils.getExpression(stringExpression)); } limit = (int) groupBy.getTopN(); } else { limit = brokerRequest.getLimit(); } } } List<OrderByExpressionContext> orderByExpressions = null; if (pinotQuery != null) { List<Expression> orderByList = pinotQuery.getOrderByList(); if (CollectionUtils.isNotEmpty(orderByList)) { orderByExpressions = new ArrayList<>(orderByList.size()); Set<ExpressionContext> expressionSet = new HashSet<>(); for (Expression orderBy : orderByList) { Function thriftFunction = orderBy.getFunctionCall(); ExpressionContext expression = QueryContextConverterUtils.getExpression(thriftFunction.getOperands().get(0)); if (expressionSet.add(expression)) { boolean isAsc = thriftFunction.getOperator().equalsIgnoreCase("ASC"); orderByExpressions.add(new OrderByExpressionContext(expression, isAsc)); } } } } else { List<SelectionSort> orderBy = brokerRequest.getOrderBy(); if (CollectionUtils.isNotEmpty(orderBy)) { orderByExpressions = new ArrayList<>(orderBy.size()); Set<ExpressionContext> expressionSet = new HashSet<>(); for (SelectionSort selectionSort : orderBy) { ExpressionContext expression = QueryContextConverterUtils.getExpression(selectionSort.getColumn()); if (expressionSet.add(expression)) { orderByExpressions.add(new OrderByExpressionContext(expression, selectionSort.isIsAsc())); } } } } FilterContext filter = null; FilterQueryTree root = RequestUtils.generateFilterQueryTree(brokerRequest); if (root != null) { filter = QueryContextConverterUtils.getFilter(root); } FilterContext havingFilter = null; if (pinotQuery != null) { Expression havingExpression = pinotQuery.getHavingExpression(); if (havingExpression != null) { havingFilter = QueryContextConverterUtils.getFilter(havingExpression); } } return new QueryContext.Builder().setSelectExpressions(selectExpressions).setAliasMap(aliasMap).setFilter(filter) .setGroupByExpressions(groupByExpressions).setOrderByExpressions(orderByExpressions) .setHavingFilter(havingFilter).setLimit(limit).setOffset(offset) .setQueryOptions(brokerRequest.getQueryOptions()).setDebugOptions(brokerRequest.getDebugOptions()) .setBrokerRequest(brokerRequest).build(); }
BrokerRequestToQueryContextConverter { public static QueryContext convert(BrokerRequest brokerRequest) { PinotQuery pinotQuery = brokerRequest.getPinotQuery(); List<ExpressionContext> selectExpressions; Map<ExpressionContext, String> aliasMap; List<ExpressionContext> groupByExpressions = null; int limit; int offset = 0; if (pinotQuery != null) { aliasMap = new HashMap<>(); List<Expression> selectList = pinotQuery.getSelectList(); selectExpressions = new ArrayList<>(selectList.size()); for (Expression thriftExpression : selectList) { ExpressionContext expression; if (thriftExpression.getType() == ExpressionType.FUNCTION && thriftExpression.getFunctionCall().getOperator() .equalsIgnoreCase("AS")) { List<Expression> operands = thriftExpression.getFunctionCall().getOperands(); expression = QueryContextConverterUtils.getExpression(operands.get(0)); aliasMap.put(expression, operands.get(1).getIdentifier().getName()); } else { expression = QueryContextConverterUtils.getExpression(thriftExpression); } selectExpressions.add(expression); } List<Expression> groupByList = pinotQuery.getGroupByList(); if (CollectionUtils.isNotEmpty(groupByList)) { groupByExpressions = new ArrayList<>(groupByList.size()); for (Expression thriftExpression : groupByList) { groupByExpressions.add(QueryContextConverterUtils.getExpression(thriftExpression)); } } limit = pinotQuery.getLimit(); offset = pinotQuery.getOffset(); } else { aliasMap = Collections.emptyMap(); Selection selections = brokerRequest.getSelections(); if (selections != null) { List<String> selectionColumns = selections.getSelectionColumns(); selectExpressions = new ArrayList<>(selectionColumns.size()); for (String expression : selectionColumns) { selectExpressions.add(QueryContextConverterUtils.getExpression(expression)); } groupByExpressions = null; limit = brokerRequest.getLimit(); offset = selections.getOffset(); if (limit == 0) { limit = selections.getSize(); } } else { List<AggregationInfo> aggregationsInfo = brokerRequest.getAggregationsInfo(); selectExpressions = new ArrayList<>(aggregationsInfo.size()); for (AggregationInfo aggregationInfo : aggregationsInfo) { String functionName = StringUtils.remove(aggregationInfo.getAggregationType(), '_'); List<String> stringExpressions = aggregationInfo.getExpressions(); int numArguments = stringExpressions.size(); List<ExpressionContext> arguments = new ArrayList<>(numArguments); if (functionName.equalsIgnoreCase(AggregationFunctionType.DISTINCT.getName())) { for (String expression : stringExpressions) { arguments.add(QueryContextConverterUtils.getExpression(expression)); } } else { arguments.add(QueryContextConverterUtils.getExpression(stringExpressions.get(0))); for (int i = 1; i < numArguments; i++) { arguments.add(ExpressionContext.forLiteral(stringExpressions.get(i))); } } FunctionContext function = new FunctionContext(FunctionContext.Type.AGGREGATION, functionName, arguments); selectExpressions.add(ExpressionContext.forFunction(function)); } GroupBy groupBy = brokerRequest.getGroupBy(); if (groupBy != null) { List<String> stringExpressions = groupBy.getExpressions(); groupByExpressions = new ArrayList<>(stringExpressions.size()); for (String stringExpression : stringExpressions) { groupByExpressions.add(QueryContextConverterUtils.getExpression(stringExpression)); } limit = (int) groupBy.getTopN(); } else { limit = brokerRequest.getLimit(); } } } List<OrderByExpressionContext> orderByExpressions = null; if (pinotQuery != null) { List<Expression> orderByList = pinotQuery.getOrderByList(); if (CollectionUtils.isNotEmpty(orderByList)) { orderByExpressions = new ArrayList<>(orderByList.size()); Set<ExpressionContext> expressionSet = new HashSet<>(); for (Expression orderBy : orderByList) { Function thriftFunction = orderBy.getFunctionCall(); ExpressionContext expression = QueryContextConverterUtils.getExpression(thriftFunction.getOperands().get(0)); if (expressionSet.add(expression)) { boolean isAsc = thriftFunction.getOperator().equalsIgnoreCase("ASC"); orderByExpressions.add(new OrderByExpressionContext(expression, isAsc)); } } } } else { List<SelectionSort> orderBy = brokerRequest.getOrderBy(); if (CollectionUtils.isNotEmpty(orderBy)) { orderByExpressions = new ArrayList<>(orderBy.size()); Set<ExpressionContext> expressionSet = new HashSet<>(); for (SelectionSort selectionSort : orderBy) { ExpressionContext expression = QueryContextConverterUtils.getExpression(selectionSort.getColumn()); if (expressionSet.add(expression)) { orderByExpressions.add(new OrderByExpressionContext(expression, selectionSort.isIsAsc())); } } } } FilterContext filter = null; FilterQueryTree root = RequestUtils.generateFilterQueryTree(brokerRequest); if (root != null) { filter = QueryContextConverterUtils.getFilter(root); } FilterContext havingFilter = null; if (pinotQuery != null) { Expression havingExpression = pinotQuery.getHavingExpression(); if (havingExpression != null) { havingFilter = QueryContextConverterUtils.getFilter(havingExpression); } } return new QueryContext.Builder().setSelectExpressions(selectExpressions).setAliasMap(aliasMap).setFilter(filter) .setGroupByExpressions(groupByExpressions).setOrderByExpressions(orderByExpressions) .setHavingFilter(havingFilter).setLimit(limit).setOffset(offset) .setQueryOptions(brokerRequest.getQueryOptions()).setDebugOptions(brokerRequest.getDebugOptions()) .setBrokerRequest(brokerRequest).build(); } }
BrokerRequestToQueryContextConverter { public static QueryContext convert(BrokerRequest brokerRequest) { PinotQuery pinotQuery = brokerRequest.getPinotQuery(); List<ExpressionContext> selectExpressions; Map<ExpressionContext, String> aliasMap; List<ExpressionContext> groupByExpressions = null; int limit; int offset = 0; if (pinotQuery != null) { aliasMap = new HashMap<>(); List<Expression> selectList = pinotQuery.getSelectList(); selectExpressions = new ArrayList<>(selectList.size()); for (Expression thriftExpression : selectList) { ExpressionContext expression; if (thriftExpression.getType() == ExpressionType.FUNCTION && thriftExpression.getFunctionCall().getOperator() .equalsIgnoreCase("AS")) { List<Expression> operands = thriftExpression.getFunctionCall().getOperands(); expression = QueryContextConverterUtils.getExpression(operands.get(0)); aliasMap.put(expression, operands.get(1).getIdentifier().getName()); } else { expression = QueryContextConverterUtils.getExpression(thriftExpression); } selectExpressions.add(expression); } List<Expression> groupByList = pinotQuery.getGroupByList(); if (CollectionUtils.isNotEmpty(groupByList)) { groupByExpressions = new ArrayList<>(groupByList.size()); for (Expression thriftExpression : groupByList) { groupByExpressions.add(QueryContextConverterUtils.getExpression(thriftExpression)); } } limit = pinotQuery.getLimit(); offset = pinotQuery.getOffset(); } else { aliasMap = Collections.emptyMap(); Selection selections = brokerRequest.getSelections(); if (selections != null) { List<String> selectionColumns = selections.getSelectionColumns(); selectExpressions = new ArrayList<>(selectionColumns.size()); for (String expression : selectionColumns) { selectExpressions.add(QueryContextConverterUtils.getExpression(expression)); } groupByExpressions = null; limit = brokerRequest.getLimit(); offset = selections.getOffset(); if (limit == 0) { limit = selections.getSize(); } } else { List<AggregationInfo> aggregationsInfo = brokerRequest.getAggregationsInfo(); selectExpressions = new ArrayList<>(aggregationsInfo.size()); for (AggregationInfo aggregationInfo : aggregationsInfo) { String functionName = StringUtils.remove(aggregationInfo.getAggregationType(), '_'); List<String> stringExpressions = aggregationInfo.getExpressions(); int numArguments = stringExpressions.size(); List<ExpressionContext> arguments = new ArrayList<>(numArguments); if (functionName.equalsIgnoreCase(AggregationFunctionType.DISTINCT.getName())) { for (String expression : stringExpressions) { arguments.add(QueryContextConverterUtils.getExpression(expression)); } } else { arguments.add(QueryContextConverterUtils.getExpression(stringExpressions.get(0))); for (int i = 1; i < numArguments; i++) { arguments.add(ExpressionContext.forLiteral(stringExpressions.get(i))); } } FunctionContext function = new FunctionContext(FunctionContext.Type.AGGREGATION, functionName, arguments); selectExpressions.add(ExpressionContext.forFunction(function)); } GroupBy groupBy = brokerRequest.getGroupBy(); if (groupBy != null) { List<String> stringExpressions = groupBy.getExpressions(); groupByExpressions = new ArrayList<>(stringExpressions.size()); for (String stringExpression : stringExpressions) { groupByExpressions.add(QueryContextConverterUtils.getExpression(stringExpression)); } limit = (int) groupBy.getTopN(); } else { limit = brokerRequest.getLimit(); } } } List<OrderByExpressionContext> orderByExpressions = null; if (pinotQuery != null) { List<Expression> orderByList = pinotQuery.getOrderByList(); if (CollectionUtils.isNotEmpty(orderByList)) { orderByExpressions = new ArrayList<>(orderByList.size()); Set<ExpressionContext> expressionSet = new HashSet<>(); for (Expression orderBy : orderByList) { Function thriftFunction = orderBy.getFunctionCall(); ExpressionContext expression = QueryContextConverterUtils.getExpression(thriftFunction.getOperands().get(0)); if (expressionSet.add(expression)) { boolean isAsc = thriftFunction.getOperator().equalsIgnoreCase("ASC"); orderByExpressions.add(new OrderByExpressionContext(expression, isAsc)); } } } } else { List<SelectionSort> orderBy = brokerRequest.getOrderBy(); if (CollectionUtils.isNotEmpty(orderBy)) { orderByExpressions = new ArrayList<>(orderBy.size()); Set<ExpressionContext> expressionSet = new HashSet<>(); for (SelectionSort selectionSort : orderBy) { ExpressionContext expression = QueryContextConverterUtils.getExpression(selectionSort.getColumn()); if (expressionSet.add(expression)) { orderByExpressions.add(new OrderByExpressionContext(expression, selectionSort.isIsAsc())); } } } } FilterContext filter = null; FilterQueryTree root = RequestUtils.generateFilterQueryTree(brokerRequest); if (root != null) { filter = QueryContextConverterUtils.getFilter(root); } FilterContext havingFilter = null; if (pinotQuery != null) { Expression havingExpression = pinotQuery.getHavingExpression(); if (havingExpression != null) { havingFilter = QueryContextConverterUtils.getFilter(havingExpression); } } return new QueryContext.Builder().setSelectExpressions(selectExpressions).setAliasMap(aliasMap).setFilter(filter) .setGroupByExpressions(groupByExpressions).setOrderByExpressions(orderByExpressions) .setHavingFilter(havingFilter).setLimit(limit).setOffset(offset) .setQueryOptions(brokerRequest.getQueryOptions()).setDebugOptions(brokerRequest.getDebugOptions()) .setBrokerRequest(brokerRequest).build(); } private  BrokerRequestToQueryContextConverter(); }
BrokerRequestToQueryContextConverter { public static QueryContext convert(BrokerRequest brokerRequest) { PinotQuery pinotQuery = brokerRequest.getPinotQuery(); List<ExpressionContext> selectExpressions; Map<ExpressionContext, String> aliasMap; List<ExpressionContext> groupByExpressions = null; int limit; int offset = 0; if (pinotQuery != null) { aliasMap = new HashMap<>(); List<Expression> selectList = pinotQuery.getSelectList(); selectExpressions = new ArrayList<>(selectList.size()); for (Expression thriftExpression : selectList) { ExpressionContext expression; if (thriftExpression.getType() == ExpressionType.FUNCTION && thriftExpression.getFunctionCall().getOperator() .equalsIgnoreCase("AS")) { List<Expression> operands = thriftExpression.getFunctionCall().getOperands(); expression = QueryContextConverterUtils.getExpression(operands.get(0)); aliasMap.put(expression, operands.get(1).getIdentifier().getName()); } else { expression = QueryContextConverterUtils.getExpression(thriftExpression); } selectExpressions.add(expression); } List<Expression> groupByList = pinotQuery.getGroupByList(); if (CollectionUtils.isNotEmpty(groupByList)) { groupByExpressions = new ArrayList<>(groupByList.size()); for (Expression thriftExpression : groupByList) { groupByExpressions.add(QueryContextConverterUtils.getExpression(thriftExpression)); } } limit = pinotQuery.getLimit(); offset = pinotQuery.getOffset(); } else { aliasMap = Collections.emptyMap(); Selection selections = brokerRequest.getSelections(); if (selections != null) { List<String> selectionColumns = selections.getSelectionColumns(); selectExpressions = new ArrayList<>(selectionColumns.size()); for (String expression : selectionColumns) { selectExpressions.add(QueryContextConverterUtils.getExpression(expression)); } groupByExpressions = null; limit = brokerRequest.getLimit(); offset = selections.getOffset(); if (limit == 0) { limit = selections.getSize(); } } else { List<AggregationInfo> aggregationsInfo = brokerRequest.getAggregationsInfo(); selectExpressions = new ArrayList<>(aggregationsInfo.size()); for (AggregationInfo aggregationInfo : aggregationsInfo) { String functionName = StringUtils.remove(aggregationInfo.getAggregationType(), '_'); List<String> stringExpressions = aggregationInfo.getExpressions(); int numArguments = stringExpressions.size(); List<ExpressionContext> arguments = new ArrayList<>(numArguments); if (functionName.equalsIgnoreCase(AggregationFunctionType.DISTINCT.getName())) { for (String expression : stringExpressions) { arguments.add(QueryContextConverterUtils.getExpression(expression)); } } else { arguments.add(QueryContextConverterUtils.getExpression(stringExpressions.get(0))); for (int i = 1; i < numArguments; i++) { arguments.add(ExpressionContext.forLiteral(stringExpressions.get(i))); } } FunctionContext function = new FunctionContext(FunctionContext.Type.AGGREGATION, functionName, arguments); selectExpressions.add(ExpressionContext.forFunction(function)); } GroupBy groupBy = brokerRequest.getGroupBy(); if (groupBy != null) { List<String> stringExpressions = groupBy.getExpressions(); groupByExpressions = new ArrayList<>(stringExpressions.size()); for (String stringExpression : stringExpressions) { groupByExpressions.add(QueryContextConverterUtils.getExpression(stringExpression)); } limit = (int) groupBy.getTopN(); } else { limit = brokerRequest.getLimit(); } } } List<OrderByExpressionContext> orderByExpressions = null; if (pinotQuery != null) { List<Expression> orderByList = pinotQuery.getOrderByList(); if (CollectionUtils.isNotEmpty(orderByList)) { orderByExpressions = new ArrayList<>(orderByList.size()); Set<ExpressionContext> expressionSet = new HashSet<>(); for (Expression orderBy : orderByList) { Function thriftFunction = orderBy.getFunctionCall(); ExpressionContext expression = QueryContextConverterUtils.getExpression(thriftFunction.getOperands().get(0)); if (expressionSet.add(expression)) { boolean isAsc = thriftFunction.getOperator().equalsIgnoreCase("ASC"); orderByExpressions.add(new OrderByExpressionContext(expression, isAsc)); } } } } else { List<SelectionSort> orderBy = brokerRequest.getOrderBy(); if (CollectionUtils.isNotEmpty(orderBy)) { orderByExpressions = new ArrayList<>(orderBy.size()); Set<ExpressionContext> expressionSet = new HashSet<>(); for (SelectionSort selectionSort : orderBy) { ExpressionContext expression = QueryContextConverterUtils.getExpression(selectionSort.getColumn()); if (expressionSet.add(expression)) { orderByExpressions.add(new OrderByExpressionContext(expression, selectionSort.isIsAsc())); } } } } FilterContext filter = null; FilterQueryTree root = RequestUtils.generateFilterQueryTree(brokerRequest); if (root != null) { filter = QueryContextConverterUtils.getFilter(root); } FilterContext havingFilter = null; if (pinotQuery != null) { Expression havingExpression = pinotQuery.getHavingExpression(); if (havingExpression != null) { havingFilter = QueryContextConverterUtils.getFilter(havingExpression); } } return new QueryContext.Builder().setSelectExpressions(selectExpressions).setAliasMap(aliasMap).setFilter(filter) .setGroupByExpressions(groupByExpressions).setOrderByExpressions(orderByExpressions) .setHavingFilter(havingFilter).setLimit(limit).setOffset(offset) .setQueryOptions(brokerRequest.getQueryOptions()).setDebugOptions(brokerRequest.getDebugOptions()) .setBrokerRequest(brokerRequest).build(); } private  BrokerRequestToQueryContextConverter(); static QueryContext convert(BrokerRequest brokerRequest); }
BrokerRequestToQueryContextConverter { public static QueryContext convert(BrokerRequest brokerRequest) { PinotQuery pinotQuery = brokerRequest.getPinotQuery(); List<ExpressionContext> selectExpressions; Map<ExpressionContext, String> aliasMap; List<ExpressionContext> groupByExpressions = null; int limit; int offset = 0; if (pinotQuery != null) { aliasMap = new HashMap<>(); List<Expression> selectList = pinotQuery.getSelectList(); selectExpressions = new ArrayList<>(selectList.size()); for (Expression thriftExpression : selectList) { ExpressionContext expression; if (thriftExpression.getType() == ExpressionType.FUNCTION && thriftExpression.getFunctionCall().getOperator() .equalsIgnoreCase("AS")) { List<Expression> operands = thriftExpression.getFunctionCall().getOperands(); expression = QueryContextConverterUtils.getExpression(operands.get(0)); aliasMap.put(expression, operands.get(1).getIdentifier().getName()); } else { expression = QueryContextConverterUtils.getExpression(thriftExpression); } selectExpressions.add(expression); } List<Expression> groupByList = pinotQuery.getGroupByList(); if (CollectionUtils.isNotEmpty(groupByList)) { groupByExpressions = new ArrayList<>(groupByList.size()); for (Expression thriftExpression : groupByList) { groupByExpressions.add(QueryContextConverterUtils.getExpression(thriftExpression)); } } limit = pinotQuery.getLimit(); offset = pinotQuery.getOffset(); } else { aliasMap = Collections.emptyMap(); Selection selections = brokerRequest.getSelections(); if (selections != null) { List<String> selectionColumns = selections.getSelectionColumns(); selectExpressions = new ArrayList<>(selectionColumns.size()); for (String expression : selectionColumns) { selectExpressions.add(QueryContextConverterUtils.getExpression(expression)); } groupByExpressions = null; limit = brokerRequest.getLimit(); offset = selections.getOffset(); if (limit == 0) { limit = selections.getSize(); } } else { List<AggregationInfo> aggregationsInfo = brokerRequest.getAggregationsInfo(); selectExpressions = new ArrayList<>(aggregationsInfo.size()); for (AggregationInfo aggregationInfo : aggregationsInfo) { String functionName = StringUtils.remove(aggregationInfo.getAggregationType(), '_'); List<String> stringExpressions = aggregationInfo.getExpressions(); int numArguments = stringExpressions.size(); List<ExpressionContext> arguments = new ArrayList<>(numArguments); if (functionName.equalsIgnoreCase(AggregationFunctionType.DISTINCT.getName())) { for (String expression : stringExpressions) { arguments.add(QueryContextConverterUtils.getExpression(expression)); } } else { arguments.add(QueryContextConverterUtils.getExpression(stringExpressions.get(0))); for (int i = 1; i < numArguments; i++) { arguments.add(ExpressionContext.forLiteral(stringExpressions.get(i))); } } FunctionContext function = new FunctionContext(FunctionContext.Type.AGGREGATION, functionName, arguments); selectExpressions.add(ExpressionContext.forFunction(function)); } GroupBy groupBy = brokerRequest.getGroupBy(); if (groupBy != null) { List<String> stringExpressions = groupBy.getExpressions(); groupByExpressions = new ArrayList<>(stringExpressions.size()); for (String stringExpression : stringExpressions) { groupByExpressions.add(QueryContextConverterUtils.getExpression(stringExpression)); } limit = (int) groupBy.getTopN(); } else { limit = brokerRequest.getLimit(); } } } List<OrderByExpressionContext> orderByExpressions = null; if (pinotQuery != null) { List<Expression> orderByList = pinotQuery.getOrderByList(); if (CollectionUtils.isNotEmpty(orderByList)) { orderByExpressions = new ArrayList<>(orderByList.size()); Set<ExpressionContext> expressionSet = new HashSet<>(); for (Expression orderBy : orderByList) { Function thriftFunction = orderBy.getFunctionCall(); ExpressionContext expression = QueryContextConverterUtils.getExpression(thriftFunction.getOperands().get(0)); if (expressionSet.add(expression)) { boolean isAsc = thriftFunction.getOperator().equalsIgnoreCase("ASC"); orderByExpressions.add(new OrderByExpressionContext(expression, isAsc)); } } } } else { List<SelectionSort> orderBy = brokerRequest.getOrderBy(); if (CollectionUtils.isNotEmpty(orderBy)) { orderByExpressions = new ArrayList<>(orderBy.size()); Set<ExpressionContext> expressionSet = new HashSet<>(); for (SelectionSort selectionSort : orderBy) { ExpressionContext expression = QueryContextConverterUtils.getExpression(selectionSort.getColumn()); if (expressionSet.add(expression)) { orderByExpressions.add(new OrderByExpressionContext(expression, selectionSort.isIsAsc())); } } } } FilterContext filter = null; FilterQueryTree root = RequestUtils.generateFilterQueryTree(brokerRequest); if (root != null) { filter = QueryContextConverterUtils.getFilter(root); } FilterContext havingFilter = null; if (pinotQuery != null) { Expression havingExpression = pinotQuery.getHavingExpression(); if (havingExpression != null) { havingFilter = QueryContextConverterUtils.getFilter(havingExpression); } } return new QueryContext.Builder().setSelectExpressions(selectExpressions).setAliasMap(aliasMap).setFilter(filter) .setGroupByExpressions(groupByExpressions).setOrderByExpressions(orderByExpressions) .setHavingFilter(havingFilter).setLimit(limit).setOffset(offset) .setQueryOptions(brokerRequest.getQueryOptions()).setDebugOptions(brokerRequest.getDebugOptions()) .setBrokerRequest(brokerRequest).build(); } private  BrokerRequestToQueryContextConverter(); static QueryContext convert(BrokerRequest brokerRequest);  }