Search in sources :

Example 1 with ExpressionParser

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser 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 ExpressionParser

use of com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser 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)

Example 3 with ExpressionParser

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

the class SQLColumnProjection method nest.

@Override
default Pair<ColumnProjection, Set<ColumnProjection>> nest(Queryable source, MetaDataStore store, boolean joinInOuter) {
    List<Reference> references = new ExpressionParser(store).parse(source, getExpression());
    boolean requiresJoin = requiresJoin(references);
    String columnId = source.isRoot() ? getName() : getAlias();
    boolean inProjection = source.getColumnProjection(columnId, getArguments(), true) != null;
    ColumnProjection outerProjection;
    Set<ColumnProjection> innerProjections;
    if (requiresJoin && joinInOuter) {
        String outerProjectionExpression = toPhysicalReferences(source, store);
        outerProjection = withExpression(outerProjectionExpression, inProjection);
        innerProjections = extractPhysicalReferences(source, references, store);
    } else {
        outerProjection = withExpression("{{$" + this.getSafeAlias() + "}}", isProjected());
        innerProjections = new LinkedHashSet<>(Arrays.asList(this));
    }
    return Pair.of(outerProjection, innerProjections);
}
Also used : 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) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) ExpressionParser(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser)

Example 4 with ExpressionParser

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

the class SQLTimeDimensionProjection method nest.

@Override
public Pair<ColumnProjection, Set<ColumnProjection>> nest(Queryable source, MetaDataStore store, boolean joinInOuter) {
    List<Reference> references = new ExpressionParser(store).parse(source, getExpression());
    boolean requiresJoin = SQLColumnProjection.requiresJoin(references);
    String columnId = source.isRoot() ? getName() : getAlias();
    boolean inProjection = source.getColumnProjection(columnId, getArguments(), true) != null;
    ColumnProjection outerProjection;
    Set<ColumnProjection> innerProjections;
    if (requiresJoin && joinInOuter) {
        String outerProjectionExpression = toPhysicalReferences(source, store);
        outerProjection = withExpression(outerProjectionExpression, inProjection);
        innerProjections = SQLColumnProjection.extractPhysicalReferences(source, references, store);
    } else {
        outerProjection = SQLTimeDimensionProjection.builder().name(name).alias(alias).valueType(valueType).columnType(columnType).grain(new TimeDimensionGrain(this.getName(), grain.getGrain())).expression("{{$" + this.getSafeAlias() + "}}").projected(isProjected()).arguments(arguments).timeZone(timeZone).build();
        innerProjections = new LinkedHashSet<>(Arrays.asList(this));
    }
    return Pair.of(outerProjection, innerProjections);
}
Also used : Reference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) TimeDimensionGrain(com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimensionGrain) ExpressionParser(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser)

Aggregations

ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)4 ExpressionParser (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser)4 Reference (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference)3 MetaDataStore (com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore)2 Queryable (com.yahoo.elide.datastores.aggregation.query.Queryable)2 ColumnArgReference (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ColumnArgReference)2 JoinReference (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinReference)2 PhysicalReference (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.PhysicalReference)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 TimeDimensionGrain (com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimensionGrain)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