Search in sources :

Example 1 with ReferenceExtractor

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ReferenceExtractor in project elide by yahoo.

the class QueryPlanTranslator method addHiddenProjections.

public static Query.QueryBuilder addHiddenProjections(MetaDataStore metaDataStore, Query.QueryBuilder builder, Query query) {
    Set<ColumnProjection> directReferencedColumns = Streams.concat(query.getColumnProjections().stream(), query.getFilterProjections(query.getWhereFilter(), ColumnProjection.class).stream()).collect(Collectors.toSet());
    ExpressionParser parser = new ExpressionParser(metaDataStore);
    Set<ColumnProjection> indirectReferenceColumns = new HashSet<>();
    directReferencedColumns.forEach(column -> {
        parser.parse(query.getSource(), column).stream().map(reference -> reference.accept(new ReferenceExtractor<LogicalReference>(LogicalReference.class, metaDataStore, ReferenceExtractor.Mode.SAME_QUERY))).flatMap(Set::stream).map(LogicalReference::getColumn).forEach(indirectReferenceColumns::add);
    });
    Streams.concat(directReferencedColumns.stream(), indirectReferenceColumns.stream()).forEach(column -> {
        if (query.getColumnProjection(column.getAlias(), column.getArguments()) == null) {
            builder.column(column.withProjected(false));
        }
    });
    return builder;
}
Also used : ReferenceExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ReferenceExtractor) Queryable(com.yahoo.elide.datastores.aggregation.query.Queryable) Set(java.util.Set) QueryPlan(com.yahoo.elide.datastores.aggregation.query.QueryPlan) Streams(com.google.common.collect.Streams) Collectors(java.util.stream.Collectors) HashSet(java.util.HashSet) Query(com.yahoo.elide.datastores.aggregation.query.Query) LogicalReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.LogicalReference) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) DefaultQueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.DefaultQueryPlanMerger) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) QueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.QueryPlanMerger) ExpressionParser(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser) QueryVisitor(com.yahoo.elide.datastores.aggregation.query.QueryVisitor) Set(java.util.Set) HashSet(java.util.HashSet) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) ExpressionParser(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser) LogicalReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.LogicalReference) HashSet(java.util.HashSet)

Example 2 with ReferenceExtractor

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ReferenceExtractor in project elide by yahoo.

the class SQLColumnProjection method canNest.

@Override
default boolean canNest(Queryable source, MetaDataStore metaDataStore) {
    SQLDialect dialect = source.getConnectionDetails().getDialect();
    String sql = toSQL(source, metaDataStore);
    SyntaxVerifier verifier = new SyntaxVerifier(dialect);
    boolean canNest = verifier.verify(sql);
    if (!canNest) {
        LOGGER.debug("Unable to nest {} because {}", this.getName(), verifier.getLastError());
        return false;
    }
    List<Reference> references = new ExpressionParser(metaDataStore).parse(source, this);
    // because rewriting the SQL in the outer expression will lose the context of the calling $$column.
    return references.stream().map(reference -> reference.accept(new ReferenceExtractor<JoinReference>(JoinReference.class, metaDataStore, ReferenceExtractor.Mode.SAME_QUERY))).flatMap(Set::stream).map(reference -> reference.accept(new ReferenceExtractor<ColumnArgReference>(ColumnArgReference.class, metaDataStore))).flatMap(Set::stream).collect(Collectors.toSet()).isEmpty();
}
Also used : SyntaxVerifier(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.SyntaxVerifier) ReferenceExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ReferenceExtractor) Arrays(java.util.Arrays) Logger(org.slf4j.Logger) Queryable(com.yahoo.elide.datastores.aggregation.query.Queryable) ColumnContext(com.yahoo.elide.datastores.aggregation.metadata.ColumnContext) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) JoinReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinReference) Collectors(java.util.stream.Collectors) PhysicalRefColumnContext(com.yahoo.elide.datastores.aggregation.metadata.PhysicalRefColumnContext) List(java.util.List) Pair(org.apache.commons.lang3.tuple.Pair) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) PhysicalReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.PhysicalReference) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) HasJoinVisitor(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.HasJoinVisitor) ExpressionParser(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser) Reference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference) LinkedHashSet(java.util.LinkedHashSet) ColumnArgReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ColumnArgReference) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) ColumnArgReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ColumnArgReference) JoinReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinReference) PhysicalReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.PhysicalReference) Reference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference) ColumnArgReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ColumnArgReference) JoinReference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinReference) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) SyntaxVerifier(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.SyntaxVerifier) ExpressionParser(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser)

Aggregations

MetaDataStore (com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore)2 ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)2 Queryable (com.yahoo.elide.datastores.aggregation.query.Queryable)2 ExpressionParser (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser)2 ReferenceExtractor (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ReferenceExtractor)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 Streams (com.google.common.collect.Streams)1 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)1 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)1 ColumnContext (com.yahoo.elide.datastores.aggregation.metadata.ColumnContext)1 PhysicalRefColumnContext (com.yahoo.elide.datastores.aggregation.metadata.PhysicalRefColumnContext)1 DefaultQueryPlanMerger (com.yahoo.elide.datastores.aggregation.query.DefaultQueryPlanMerger)1 Query (com.yahoo.elide.datastores.aggregation.query.Query)1 QueryPlan (com.yahoo.elide.datastores.aggregation.query.QueryPlan)1 QueryPlanMerger (com.yahoo.elide.datastores.aggregation.query.QueryPlanMerger)1 QueryVisitor (com.yahoo.elide.datastores.aggregation.query.QueryVisitor)1 SyntaxVerifier (com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.SyntaxVerifier)1 SQLDialect (com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect)1 ColumnArgReference (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ColumnArgReference)1