Search in sources :

Example 1 with Reference

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

the class SubqueryFilterSplitter method visitPredicate.

@Override
public SplitFilter visitPredicate(FilterPredicate filterPredicate) {
    Type<?> tableType = filterPredicate.getEntityType();
    String fieldName = filterPredicate.getField();
    SQLTable table = metaDataStore.getTable(tableType);
    List<Reference> references = parser.parse(table, table.getColumnProjection(fieldName));
    boolean hasJoin = references.stream().anyMatch(ref -> ref.accept(new HasJoinVisitor()));
    if (hasJoin) {
        return SplitFilter.builder().outer(filterPredicate).build();
    }
    return SplitFilter.builder().inner(filterPredicate).build();
}
Also used : Reference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) HasJoinVisitor(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.HasJoinVisitor)

Example 2 with Reference

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

the class QueryTranslator method extractJoinExpressions.

/**
 * Get required join expressions for given column in given Query.
 * @param column {@link ColumnProjection}
 * @param query Expanded Query.
 * @return A set of Join expressions that capture a relationship traversal.
 */
private Set<String> extractJoinExpressions(ColumnProjection column, Query query) {
    Set<String> joinExpressions = new LinkedHashSet<>();
    ColumnContext context = ColumnContext.builder().queryable(query).alias(query.getSource().getAlias()).metaDataStore(metaDataStore).column(column).tableArguments(query.getArguments()).build();
    JoinExpressionExtractor visitor = new JoinExpressionExtractor(context);
    List<Reference> references = parser.parse(query.getSource(), column);
    references.forEach(ref -> joinExpressions.addAll(ref.accept(visitor)));
    return joinExpressions;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) JoinExpressionExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinExpressionExtractor) Reference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference) ColumnContext(com.yahoo.elide.datastores.aggregation.metadata.ColumnContext)

Example 3 with Reference

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

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

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

Reference (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference)5 ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)3 ExpressionParser (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser)3 ColumnContext (com.yahoo.elide.datastores.aggregation.metadata.ColumnContext)2 ColumnArgReference (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ColumnArgReference)2 HasJoinVisitor (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.HasJoinVisitor)2 JoinReference (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinReference)2 PhysicalReference (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.PhysicalReference)2 LinkedHashSet (java.util.LinkedHashSet)2 MetaDataStore (com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore)1 PhysicalRefColumnContext (com.yahoo.elide.datastores.aggregation.metadata.PhysicalRefColumnContext)1 TimeDimensionGrain (com.yahoo.elide.datastores.aggregation.metadata.models.TimeDimensionGrain)1 Queryable (com.yahoo.elide.datastores.aggregation.query.Queryable)1 SyntaxVerifier (com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.SyntaxVerifier)1 SQLDialect (com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect)1 JoinExpressionExtractor (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinExpressionExtractor)1 ReferenceExtractor (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ReferenceExtractor)1 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)1 Arrays (java.util.Arrays)1 List (java.util.List)1