Search in sources :

Example 1 with ColumnProjection

use of com.yahoo.elide.datastores.aggregation.query.ColumnProjection in project elide by yahoo.

the class ColumnContext method get.

public Object get(Object key) {
    String keyStr = key.toString();
    // Physical References starts with $
    if (keyStr.lastIndexOf('$') == 0) {
        return resolvePhysicalReference(this, keyStr);
    }
    if (keyStr.equals(TBL_PREFIX)) {
        return TableSubContext.tableSubContextBuilder().tableArguments(this.tableArguments).build();
    }
    if (keyStr.equals(COL_PREFIX)) {
        return ColumnSubContext.columnSubContextBuilder().queryable(this.getQueryable()).alias(this.getAlias()).metaDataStore(this.getMetaDataStore()).column(this.getColumn()).tableArguments(this.getTableArguments()).build();
    }
    if (this.queryable.hasJoin(keyStr)) {
        return getJoinContext(keyStr);
    }
    // Check if key exists in Map.
    Object value = getOrDefault(key, null);
    if (value != null) {
        return value;
    }
    // In case of colA references colB and user query has both colA and colB,
    // we should use default arguments for colB while resolving colA instead of user provided argument for colB.
    // so taking colB's details from current queryable's source.
    ColumnProjection column = this.getQueryable().getSource().getColumnProjection(keyStr);
    if (column != null) {
        ColumnProjection newColumn = column.withArguments(mergedArgumentMap(column.getArguments(), this.getColumn().getArguments()));
        return getNewContext(this, newColumn).resolve(newColumn.getExpression());
    }
    throw new HandlebarsException(new Throwable("Couldn't find: " + keyStr));
}
Also used : ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) Argument.getArgumentMapFromString(com.yahoo.elide.core.request.Argument.getArgumentMapFromString) ToString(lombok.ToString) HandlebarsException(com.github.jknack.handlebars.HandlebarsException)

Example 2 with ColumnProjection

use of com.yahoo.elide.datastores.aggregation.query.ColumnProjection in project elide by yahoo.

the class DefaultQueryValidator method validateHavingClause.

@Override
public void validateHavingClause(Query query) {
    FilterExpression havingClause = query.getHavingFilter();
    if (havingClause == null) {
        return;
    }
    havingClause.accept(new PredicateExtractionVisitor()).forEach(predicate -> {
        Path path = predicate.getPath();
        if (path.getPathElements().size() > 1) {
            throw new InvalidOperationException("Relationship traversal not supported for analytic queries.");
        }
        validatePredicate(query, predicate);
        extractFilterProjections(query, havingClause).stream().forEach(projection -> {
            Predicate<ColumnProjection> filterByNameAndArgs = (column) -> (column.getAlias().equals(projection.getAlias()) || column.getName().equals(projection.getName())) && column.getArguments().equals(projection.getArguments());
            // Query by (alias or name) and arguments.   The filter may or may not be using the alias.
            if (query.getColumnProjection(filterByNameAndArgs) == null) {
                Predicate<ColumnProjection> filterByName = (column) -> (column.getAlias().equals(projection.getAlias()) || column.getName().equals(projection.getName()));
                // The column wasn't projected at all.
                if (query.getColumnProjection(filterByName) == null) {
                    throw new InvalidOperationException(String.format("Post aggregation filtering on '%s' requires the field to be projected in the response", projection.getAlias()));
                // The column was projected but arguments didn't match.
                } else {
                    throw new InvalidOperationException(String.format("Post aggregation filtering on '%s' requires the field to be projected " + "in the response with matching arguments", projection.getAlias()));
                }
            }
        });
    });
}
Also used : Path(com.yahoo.elide.core.Path) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Path(com.yahoo.elide.core.Path) Queryable.extractFilterProjections(com.yahoo.elide.datastores.aggregation.query.Queryable.extractFilterProjections) Argument(com.yahoo.elide.core.request.Argument) HashSet(java.util.HashSet) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) Map(java.util.Map) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) LinkedHashSet(java.util.LinkedHashSet) Sorting(com.yahoo.elide.core.request.Sorting) Predicate(java.util.function.Predicate) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) ArgumentDefinition(com.yahoo.elide.datastores.aggregation.metadata.models.ArgumentDefinition) Set(java.util.Set) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) Stream(java.util.stream.Stream) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) Type(com.yahoo.elide.core.type.Type) Operator(com.yahoo.elide.core.filter.Operator) Optional(java.util.Optional) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) InvalidOperationException(com.yahoo.elide.core.exceptions.InvalidOperationException) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression)

Example 3 with ColumnProjection

use of com.yahoo.elide.datastores.aggregation.query.ColumnProjection in project elide by yahoo.

the class JoinExpressionExtractor method visitLogicalReference.

@Override
public Set<String> visitLogicalReference(LogicalReference reference) {
    /**
     * For the scenario: col1:{{col2}}, col2:{{col3}}, col3:{{join.col1}}
     * Creating new visitor with new ColumnProjection.
     */
    ColumnProjection newColumn = reference.getColumn();
    ColumnContext newCtx = ColumnContext.builder().queryable(this.context.getQueryable()).alias(this.context.getAlias()).metaDataStore(this.context.getMetaDataStore()).column(newColumn).tableArguments(this.context.getTableArguments()).build();
    JoinExpressionExtractor visitor = new JoinExpressionExtractor(newCtx);
    reference.getReferences().forEach(ref -> {
        joinExpressions.addAll(ref.accept(visitor));
    });
    return joinExpressions;
}
Also used : ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) ColumnContext(com.yahoo.elide.datastores.aggregation.metadata.ColumnContext)

Example 4 with ColumnProjection

use of com.yahoo.elide.datastores.aggregation.query.ColumnProjection in project elide by yahoo.

the class HasJoinVisitorTest method testHasJoin.

@Test
void testHasJoin() {
    Queryable source = mock(Queryable.class);
    JoinPath path = mock(JoinPath.class);
    ColumnProjection column = mock(ColumnProjection.class);
    Reference reference = LogicalReference.builder().source(source).column(column).reference(PhysicalReference.builder().name("foo").source(source).build()).reference(JoinReference.builder().source(source).path(path).reference(PhysicalReference.builder().name("bar").source(source).build()).build()).build();
    assertTrue(reference.accept(new HasJoinVisitor()));
}
Also used : JoinPath(com.yahoo.elide.datastores.aggregation.core.JoinPath) Queryable(com.yahoo.elide.datastores.aggregation.query.Queryable) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) Test(org.junit.jupiter.api.Test)

Example 5 with ColumnProjection

use of com.yahoo.elide.datastores.aggregation.query.ColumnProjection in project elide by yahoo.

the class HasJoinVisitorTest method testLogicalColumnNoJoin.

@Test
void testLogicalColumnNoJoin() {
    Queryable source = mock(Queryable.class);
    ColumnProjection column = mock(ColumnProjection.class);
    Reference reference = LogicalReference.builder().source(source).column(column).reference(PhysicalReference.builder().name("foo").source(source).build()).build();
    assertFalse(reference.accept(new HasJoinVisitor()));
}
Also used : Queryable(com.yahoo.elide.datastores.aggregation.query.Queryable) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) Test(org.junit.jupiter.api.Test)

Aggregations

ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)36 Test (org.junit.jupiter.api.Test)22 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)13 Argument (com.yahoo.elide.core.request.Argument)7 Queryable (com.yahoo.elide.datastores.aggregation.query.Queryable)6 Query (com.yahoo.elide.datastores.aggregation.query.Query)5 Argument.getArgumentMapFromString (com.yahoo.elide.core.request.Argument.getArgumentMapFromString)4 ValueType (com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType)4 Column (com.yahoo.elide.datastores.aggregation.metadata.models.Column)4 ExpressionParser (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser)4 List (java.util.List)4 Set (java.util.Set)4 Collectors (java.util.stream.Collectors)4 Path (com.yahoo.elide.core.Path)3 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)3 MetaDataStore (com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore)3 Reference (com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference)3 HashMap (java.util.HashMap)3 HandlebarsException (com.github.jknack.handlebars.HandlebarsException)2 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)2