Search in sources :

Example 1 with ExpressionContext

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

the class PinotExpressionRewriter method rewriteExpression.

public static ExpressionContext rewriteExpression(SchemaTableName schemaTableName, ExpressionContext expressionContext, Map<String, ColumnHandle> columnHandles) {
    requireNonNull(expressionContext, "expressionContext is null");
    Context context = new Context() {

        @Override
        public SchemaTableName getSchemaTableName() {
            return schemaTableName;
        }

        @Override
        public Map<String, ColumnHandle> getColumnHandles() {
            return columnHandles;
        }
    };
    return rewriteExpression(expressionContext, context);
}
Also used : FunctionContext(org.apache.pinot.common.request.context.FunctionContext) ExpressionContext(org.apache.pinot.common.request.context.ExpressionContext) PinotSqlFormatter.getColumnHandle(io.trino.plugin.pinot.query.PinotSqlFormatter.getColumnHandle) ColumnHandle(io.trino.spi.connector.ColumnHandle)

Example 2 with ExpressionContext

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

the class PinotSqlFormatter method formatExpression.

public static String formatExpression(SchemaTableName schemaTableName, ExpressionContext expressionContext, Optional<Map<String, ColumnHandle>> columnHandles) {
    requireNonNull(expressionContext, "expressionContext is null");
    Context context = new Context() {

        @Override
        public SchemaTableName getSchemaTableName() {
            return schemaTableName;
        }

        @Override
        public Optional<Map<String, ColumnHandle>> getColumnHandles() {
            return columnHandles;
        }
    };
    return formatExpression(expressionContext, context);
}
Also used : FunctionContext(org.apache.pinot.common.request.context.FunctionContext) ExpressionContext(org.apache.pinot.common.request.context.ExpressionContext) FilterContext(org.apache.pinot.common.request.context.FilterContext) PinotPatterns.functionContext(io.trino.plugin.pinot.query.PinotPatterns.functionContext) Map(java.util.Map) Maps.immutableEnumMap(com.google.common.collect.Maps.immutableEnumMap) HashMap(java.util.HashMap)

Example 3 with ExpressionContext

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

the class DynamicTableBuilder method getPinotColumns.

private static List<PinotColumnHandle> getPinotColumns(SchemaTableName schemaTableName, List<ExpressionContext> expressions, List<String> aliases, Map<String, ColumnHandle> columnHandles, PinotTypeResolver pinotTypeResolver, Map<String, PinotColumnNameAndTrinoType> aggregateTypes) {
    ImmutableList.Builder<PinotColumnHandle> pinotColumnsBuilder = ImmutableList.builder();
    for (int index = 0; index < expressions.size(); index++) {
        ExpressionContext expressionContext = expressions.get(index);
        Optional<String> alias = getAlias(aliases, index);
        // Since Pinot doesn't support subqueries yet, we can only have one occurrence of SELECT *
        if (expressionContext.getType() == ExpressionContext.Type.IDENTIFIER && expressionContext.getIdentifier().equals(WILDCARD)) {
            pinotColumnsBuilder.addAll(columnHandles.values().stream().map(handle -> fromNonAggregateColumnHandle((PinotColumnHandle) handle)).collect(toImmutableList()));
        } else {
            pinotColumnsBuilder.add(getPinotColumnHandle(schemaTableName, expressionContext, alias, columnHandles, pinotTypeResolver, aggregateTypes));
        }
    }
    return pinotColumnsBuilder.build();
}
Also used : PinotColumnHandle(io.trino.plugin.pinot.PinotColumnHandle) ExpressionContext(org.apache.pinot.common.request.context.ExpressionContext) OrderByExpressionContext(org.apache.pinot.common.request.context.OrderByExpressionContext) ImmutableList.toImmutableList(com.google.common.collect.ImmutableList.toImmutableList) ImmutableList(com.google.common.collect.ImmutableList)

Example 4 with ExpressionContext

use of org.apache.pinot.common.request.context.ExpressionContext 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)

Example 5 with ExpressionContext

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

the class DynamicTableBuilder method getPinotColumnHandle.

private static PinotColumnHandle getPinotColumnHandle(SchemaTableName schemaTableName, ExpressionContext expressionContext, Optional<String> alias, Map<String, ColumnHandle> columnHandles, PinotTypeResolver pinotTypeResolver, Map<String, PinotColumnNameAndTrinoType> aggregateTypes) {
    ExpressionContext rewritten = rewriteExpression(schemaTableName, expressionContext, columnHandles);
    // If there is no alias, pinot autogenerates the column name:
    String columnName = rewritten.toString();
    String pinotExpression = formatExpression(schemaTableName, rewritten);
    Type trinoType;
    boolean isAggregate = hasAggregate(rewritten);
    if (isAggregate) {
        trinoType = requireNonNull(aggregateTypes.get(columnName).getTrinoType(), format("Unexpected aggregate expression: '%s'", rewritten));
        // For aggregation queries, the column name is set by the schema returned from PostAggregationHandler, see getAggregateTypes
        columnName = aggregateTypes.get(columnName).getPinotColumnName();
    } else {
        trinoType = getTrinoTypeFromPinotType(pinotTypeResolver.resolveExpressionType(rewritten, schemaTableName, columnHandles));
        if (!aggregateTypes.isEmpty() && trinoType instanceof ArrayType) {
            trinoType = ((ArrayType) trinoType).getElementType();
        }
    }
    return new PinotColumnHandle(alias.orElse(columnName), trinoType, pinotExpression, alias.isPresent(), isAggregate, isReturnNullOnEmptyGroup(expressionContext), Optional.empty(), Optional.empty());
}
Also used : ArrayType(io.trino.spi.type.ArrayType) PinotColumnHandle(io.trino.plugin.pinot.PinotColumnHandle) ArrayType(io.trino.spi.type.ArrayType) PinotColumnHandle.getTrinoTypeFromPinotType(io.trino.plugin.pinot.PinotColumnHandle.getTrinoTypeFromPinotType) AggregationFunctionType.getAggregationFunctionType(org.apache.pinot.segment.spi.AggregationFunctionType.getAggregationFunctionType) Type(io.trino.spi.type.Type) AggregationFunctionType(org.apache.pinot.segment.spi.AggregationFunctionType) ExpressionContext(org.apache.pinot.common.request.context.ExpressionContext) OrderByExpressionContext(org.apache.pinot.common.request.context.OrderByExpressionContext)

Aggregations

ExpressionContext (org.apache.pinot.common.request.context.ExpressionContext)6 OrderByExpressionContext (org.apache.pinot.common.request.context.OrderByExpressionContext)4 PinotColumnHandle (io.trino.plugin.pinot.PinotColumnHandle)3 ImmutableList (com.google.common.collect.ImmutableList)2 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)2 ColumnHandle (io.trino.spi.connector.ColumnHandle)2 FunctionContext (org.apache.pinot.common.request.context.FunctionContext)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 Maps.immutableEnumMap (com.google.common.collect.Maps.immutableEnumMap)1 PinotColumnHandle.fromNonAggregateColumnHandle (io.trino.plugin.pinot.PinotColumnHandle.fromNonAggregateColumnHandle)1 PinotColumnHandle.getTrinoTypeFromPinotType (io.trino.plugin.pinot.PinotColumnHandle.getTrinoTypeFromPinotType)1 PinotPatterns.functionContext (io.trino.plugin.pinot.query.PinotPatterns.functionContext)1 PinotSqlFormatter.getColumnHandle (io.trino.plugin.pinot.query.PinotSqlFormatter.getColumnHandle)1 ArrayType (io.trino.spi.type.ArrayType)1 Type (io.trino.spi.type.Type)1 HashMap (java.util.HashMap)1 Map (java.util.Map)1 BrokerRequest (org.apache.pinot.common.request.BrokerRequest)1 PinotQuery (org.apache.pinot.common.request.PinotQuery)1 FilterContext (org.apache.pinot.common.request.context.FilterContext)1