Search in sources :

Example 1 with BrokerRequest

use of org.apache.pinot.common.request.BrokerRequest in project trino by trinodb.

the class PinotQueryClient method queryPinotServerForDataTable.

public Map<ServerInstance, DataTable> queryPinotServerForDataTable(String query, String serverHost, List<String> segments, long connectionTimeoutInMillis, int pinotRetryCount) {
    // TODO: separate into offline and realtime methods
    BrokerRequest brokerRequest;
    try {
        brokerRequest = REQUEST_COMPILER.compileToBrokerRequest(query);
    } catch (SqlCompilationException e) {
        throw new PinotException(PINOT_INVALID_PQL_GENERATED, Optional.of(query), format("Parsing error with on %s, Error = %s", serverHost, e.getMessage()), e);
    }
    ServerInstance serverInstance = pinotHostMapper.getServerInstance(serverHost);
    Map<ServerInstance, List<String>> routingTable = new HashMap<>();
    routingTable.put(serverInstance, new ArrayList<>(segments));
    String tableName = brokerRequest.getQuerySource().getTableName();
    String rawTableName = TableNameBuilder.extractRawTableName(tableName);
    Map<ServerInstance, List<String>> offlineRoutingTable = TableNameBuilder.isOfflineTableResource(tableName) ? routingTable : null;
    Map<ServerInstance, List<String>> realtimeRoutingTable = TableNameBuilder.isRealtimeTableResource(tableName) ? routingTable : null;
    BrokerRequest offlineBrokerRequest = TableNameBuilder.isOfflineTableResource(tableName) ? brokerRequest : null;
    BrokerRequest realtimeBrokerRequest = TableNameBuilder.isRealtimeTableResource(tableName) ? brokerRequest : null;
    AsyncQueryResponse asyncQueryResponse = doWithRetries(pinotRetryCount, requestId -> queryRouter.submitQuery(requestId, rawTableName, offlineBrokerRequest, offlineRoutingTable, realtimeBrokerRequest, realtimeRoutingTable, connectionTimeoutInMillis));
    try {
        Map<ServerRoutingInstance, ServerResponse> response = asyncQueryResponse.getResponse();
        Map<ServerInstance, DataTable> dataTableMap = new HashMap<>();
        for (Map.Entry<ServerRoutingInstance, ServerResponse> entry : response.entrySet()) {
            ServerResponse serverResponse = entry.getValue();
            DataTable dataTable = serverResponse.getDataTable();
            dataTableMap.put(toServerInstance(entry.getKey()), dataTable);
        }
        return dataTableMap;
    } catch (InterruptedException e) {
        throw new PinotException(PINOT_EXCEPTION, Optional.of(query), "Pinot query execution was interrupted", e);
    }
}
Also used : ServerResponse(org.apache.pinot.core.transport.ServerResponse) PinotException(io.trino.plugin.pinot.PinotException) DataTable(org.apache.pinot.common.utils.DataTable) HashMap(java.util.HashMap) ServerRoutingInstance(org.apache.pinot.core.transport.ServerRoutingInstance) SqlCompilationException(org.apache.pinot.sql.parsers.SqlCompilationException) BrokerRequest(org.apache.pinot.common.request.BrokerRequest) ArrayList(java.util.ArrayList) List(java.util.List) AsyncQueryResponse(org.apache.pinot.core.transport.AsyncQueryResponse) ServerInstance(org.apache.pinot.core.transport.ServerInstance) HashMap(java.util.HashMap) Map(java.util.Map)

Example 2 with BrokerRequest

use of org.apache.pinot.common.request.BrokerRequest 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);
}
Also used : PinotColumnHandle.fromNonAggregateColumnHandle(io.trino.plugin.pinot.PinotColumnHandle.fromNonAggregateColumnHandle) ColumnHandle(io.trino.spi.connector.ColumnHandle) PinotColumnHandle(io.trino.plugin.pinot.PinotColumnHandle) PinotColumnHandle(io.trino.plugin.pinot.PinotColumnHandle) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList) QueryContext(org.apache.pinot.core.query.request.context.QueryContext) OrderByExpressionContext(org.apache.pinot.common.request.context.OrderByExpressionContext) ExpressionContext(org.apache.pinot.common.request.context.ExpressionContext) OrderByExpressionContext(org.apache.pinot.common.request.context.OrderByExpressionContext) PinotQuery(org.apache.pinot.common.request.PinotQuery) BrokerRequest(org.apache.pinot.common.request.BrokerRequest)

Aggregations

BrokerRequest (org.apache.pinot.common.request.BrokerRequest)2 ImmutableList (com.google.common.collect.ImmutableList)1 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)1 PinotColumnHandle (io.trino.plugin.pinot.PinotColumnHandle)1 PinotColumnHandle.fromNonAggregateColumnHandle (io.trino.plugin.pinot.PinotColumnHandle.fromNonAggregateColumnHandle)1 PinotException (io.trino.plugin.pinot.PinotException)1 ColumnHandle (io.trino.spi.connector.ColumnHandle)1 ArrayList (java.util.ArrayList)1 HashMap (java.util.HashMap)1 List (java.util.List)1 Map (java.util.Map)1 PinotQuery (org.apache.pinot.common.request.PinotQuery)1 ExpressionContext (org.apache.pinot.common.request.context.ExpressionContext)1 OrderByExpressionContext (org.apache.pinot.common.request.context.OrderByExpressionContext)1 DataTable (org.apache.pinot.common.utils.DataTable)1 QueryContext (org.apache.pinot.core.query.request.context.QueryContext)1 AsyncQueryResponse (org.apache.pinot.core.transport.AsyncQueryResponse)1 ServerInstance (org.apache.pinot.core.transport.ServerInstance)1 ServerResponse (org.apache.pinot.core.transport.ServerResponse)1 ServerRoutingInstance (org.apache.pinot.core.transport.ServerRoutingInstance)1