use of org.apache.pinot.common.request.context.OrderByExpressionContext in project trino by trinodb.
the class DynamicTableBuilder method buildFromPql.
public static DynamicTable buildFromPql(PinotMetadata pinotMetadata, SchemaTableName schemaTableName, PinotClient pinotClient) {
requireNonNull(pinotMetadata, "pinotMetadata is null");
requireNonNull(schemaTableName, "schemaTableName is null");
String query = schemaTableName.getTableName();
BrokerRequest request = REQUEST_COMPILER.compileToBrokerRequest(query);
PinotQuery pinotQuery = request.getPinotQuery();
QueryContext queryContext = BrokerRequestToQueryContextConverter.convert(request);
String pinotTableName = stripSuffix(request.getQuerySource().getTableName());
Optional<String> suffix = getSuffix(request.getQuerySource().getTableName());
Map<String, ColumnHandle> columnHandles = pinotMetadata.getPinotColumnHandles(pinotTableName);
List<OrderByExpression> orderBy = ImmutableList.of();
PinotTypeResolver pinotTypeResolver = new PinotTypeResolver(pinotClient, pinotTableName);
List<PinotColumnHandle> selectColumns = ImmutableList.of();
Map<String, PinotColumnNameAndTrinoType> aggregateTypes = ImmutableMap.of();
if (queryContext.getAggregationFunctions() != null) {
checkState(queryContext.getAggregationFunctions().length > 0, "Aggregation Functions is empty");
aggregateTypes = getAggregateTypes(schemaTableName, queryContext, columnHandles);
}
if (queryContext.getSelectExpressions() != null) {
checkState(!queryContext.getSelectExpressions().isEmpty(), "Pinot selections is empty");
selectColumns = getPinotColumns(schemaTableName, queryContext.getSelectExpressions(), queryContext.getAliasList(), columnHandles, pinotTypeResolver, aggregateTypes);
}
if (queryContext.getOrderByExpressions() != null) {
ImmutableList.Builder<OrderByExpression> orderByBuilder = ImmutableList.builder();
for (OrderByExpressionContext orderByExpressionContext : queryContext.getOrderByExpressions()) {
ExpressionContext expressionContext = orderByExpressionContext.getExpression();
PinotColumnHandle pinotColumnHandle = getPinotColumnHandle(schemaTableName, expressionContext, Optional.empty(), columnHandles, pinotTypeResolver, aggregateTypes);
orderByBuilder.add(new OrderByExpression(pinotColumnHandle.getExpression(), orderByExpressionContext.isAsc()));
}
orderBy = orderByBuilder.build();
}
List<PinotColumnHandle> groupByColumns = ImmutableList.of();
if (queryContext.getGroupByExpressions() != null) {
groupByColumns = getPinotColumns(schemaTableName, queryContext.getGroupByExpressions(), ImmutableList.of(), columnHandles, pinotTypeResolver, aggregateTypes);
}
Optional<String> filter = Optional.empty();
if (pinotQuery.getFilterExpression() != null) {
String formatted = formatFilter(schemaTableName, queryContext.getFilter(), columnHandles);
filter = Optional.of(formatted);
}
return new DynamicTable(pinotTableName, suffix, selectColumns, filter, groupByColumns, ImmutableList.of(), orderBy, OptionalLong.of(queryContext.getLimit()), getOffset(queryContext), query);
}
Aggregations