Search in sources :

Example 31 with MetaDataStore

use of com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore in project elide by yahoo.

the class TableArgumentValidatorTest method testArgumentValues.

@Test
public void testArgumentValues() {
    Table mainTable = mainTableBuilder.argument(Argument.builder().name("testArg").type(Type.INTEGER).values(Sets.newHashSet("1", "2.5")).defaultValue("").build()).build();
    Set<Table> tables = new HashSet<>();
    tables.add(mainTable);
    MetaDataStore metaDataStore = new MetaDataStore(DefaultClassScanner.getInstance(), tables, this.namespaceConfigs, true);
    QueryPlanMerger merger = new DefaultQueryPlanMerger(metaDataStore);
    Exception e = assertThrows(IllegalStateException.class, () -> new SQLQueryEngine(metaDataStore, connectionLookup, optimizers, merger, queryValidator));
    assertEquals("Failed to verify table arguments for table: namespace_MainTable. Value: '2.5' for Argument 'testArg' with Type 'INTEGER' is invalid.", e.getMessage());
}
Also used : SQLQueryEngine(com.yahoo.elide.datastores.aggregation.queryengines.sql.SQLQueryEngine) DefaultQueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.DefaultQueryPlanMerger) QueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.QueryPlanMerger) Table(com.yahoo.elide.modelconfig.model.Table) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) DefaultQueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.DefaultQueryPlanMerger) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 32 with MetaDataStore

use of com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore 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 33 with MetaDataStore

use of com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore in project elide by yahoo.

the class QueryTranslator method visitQuery.

@Override
public NativeQuery.NativeQueryBuilder visitQuery(Query query) {
    NativeQuery.NativeQueryBuilder builder = query.getSource().accept(this);
    if (query.isNested()) {
        NativeQuery innerQuery = builder.build();
        builder = NativeQuery.builder().fromClause(getFromClause("(" + innerQuery + ")", applyQuotes(query.getSource().getAlias()), dialect));
    }
    Set<String> joinExpressions = new LinkedHashSet<>();
    builder.projectionClause(constructProjectionWithReference(query));
    // Handles join for all type of column projects - dimensions, metrics and time dimention
    joinExpressions.addAll(extractJoinExpressions(query));
    Set<ColumnProjection> groupByDimensions = query.getAllDimensionProjections().stream().map(SQLColumnProjection.class::cast).filter(SQLColumnProjection::isProjected).collect(Collectors.toCollection(LinkedHashSet::new));
    if (!groupByDimensions.isEmpty()) {
        if (!query.getMetricProjections().isEmpty()) {
            builder.groupByClause("GROUP BY " + groupByDimensions.stream().map(SQLColumnProjection.class::cast).map((column) -> column.toSQL(query, metaDataStore)).collect(Collectors.joining(", ")));
        }
    }
    if (query.getWhereFilter() != null) {
        builder.whereClause("WHERE " + translateFilterExpression(query.getWhereFilter(), path -> generatePredicatePathReference(path, query)));
        joinExpressions.addAll(extractJoinExpressions(query, query.getWhereFilter()));
    }
    if (query.getHavingFilter() != null) {
        builder.havingClause("HAVING " + translateFilterExpression(query.getHavingFilter(), (path) -> constructHavingClauseWithReference(path, query)));
        joinExpressions.addAll(extractJoinExpressions(query, query.getHavingFilter()));
    }
    if (query.getSorting() != null) {
        Map<Path, Sorting.SortOrder> sortClauses = query.getSorting().getSortingPaths();
        builder.orderByClause(extractOrderBy(sortClauses, query));
        joinExpressions.addAll(extractJoinExpressions(query, sortClauses));
    }
    Pagination pagination = query.getPagination();
    if (pagination != null) {
        builder.offsetLimitClause(dialect.generateOffsetLimitClause(pagination.getOffset(), pagination.getLimit()));
    }
    return builder.joinClause(String.join(" ", joinExpressions));
}
Also used : LinkedHashSet(java.util.LinkedHashSet) PredicateExtractionVisitor(com.yahoo.elide.core.filter.expression.PredicateExtractionVisitor) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Path(com.yahoo.elide.core.Path) Function(java.util.function.Function) Argument(com.yahoo.elide.core.request.Argument) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) Map(java.util.Map) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) FilterTranslator(com.yahoo.elide.datastores.jpql.filter.FilterTranslator) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) LinkedHashSet(java.util.LinkedHashSet) QueryVisitor(com.yahoo.elide.datastores.aggregation.query.QueryVisitor) TableContext(com.yahoo.elide.datastores.aggregation.metadata.TableContext) JoinExpressionExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.JoinExpressionExtractor) Sorting(com.yahoo.elide.core.request.Sorting) Queryable(com.yahoo.elide.datastores.aggregation.query.Queryable) FromSubquery(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromSubquery) ColumnContext(com.yahoo.elide.datastores.aggregation.metadata.ColumnContext) Collection(java.util.Collection) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) Set(java.util.Set) FromTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.annotation.FromTable) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) BadRequestException(com.yahoo.elide.core.exceptions.BadRequestException) Stream(java.util.stream.Stream) Pagination(com.yahoo.elide.core.request.Pagination) Type(com.yahoo.elide.core.type.Type) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) ExpressionParser(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.ExpressionParser) Reference(com.yahoo.elide.datastores.aggregation.queryengines.sql.expression.Reference) Path(com.yahoo.elide.core.Path) Pagination(com.yahoo.elide.core.request.Pagination) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection)

Example 34 with MetaDataStore

use of com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore 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 35 with MetaDataStore

use of com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore in project elide by yahoo.

the class DefaultQueryPlanMergerTest method testNestedMerge.

@Test
public void testNestedMerge() {
    Queryable source = mock(Queryable.class);
    // A root source.
    when(source.getSource()).thenReturn(source);
    MetricProjection m1 = mock(MetricProjection.class);
    MetricProjection m2 = mock(MetricProjection.class);
    when(m1.getName()).thenReturn("m1");
    when(m2.getName()).thenReturn("m2");
    when(m1.canNest(any(), any())).thenReturn(true);
    when(m1.nest(any(), any(), anyBoolean())).thenReturn(Pair.of(m1, Set.of(m1)));
    when(m2.canNest(any(), any())).thenReturn(true);
    when(m2.nest(any(), any(), anyBoolean())).thenReturn(Pair.of(m2, Set.of(m2)));
    DimensionProjection d1 = mock(DimensionProjection.class);
    DimensionProjection d2 = mock(DimensionProjection.class);
    when(d1.getName()).thenReturn("d1");
    when(d2.getName()).thenReturn("d2");
    when(d1.canNest(any(), any())).thenReturn(true);
    when(d1.nest(any(), any(), anyBoolean())).thenReturn(Pair.of(d1, Set.of(d1)));
    when(d2.canNest(any(), any())).thenReturn(true);
    when(d2.nest(any(), any(), anyBoolean())).thenReturn(Pair.of(d2, Set.of(d2)));
    TimeDimensionProjection t1 = mock(TimeDimensionProjection.class);
    TimeDimensionProjection t2 = mock(TimeDimensionProjection.class);
    when(t1.getName()).thenReturn("t1");
    when(t2.getName()).thenReturn("t2");
    when(t1.canNest(any(), any())).thenReturn(true);
    when(t1.nest(any(), any(), anyBoolean())).thenReturn(Pair.of(t1, Set.of(t1)));
    when(t2.canNest(any(), any())).thenReturn(true);
    when(t2.nest(any(), any(), anyBoolean())).thenReturn(Pair.of(t2, Set.of(t2)));
    FilterExpression filterExpression = mock(FilterExpression.class);
    QueryPlan a = QueryPlan.builder().source(source).whereFilter(filterExpression).metricProjection(m1).dimensionProjection(d1).timeDimensionProjection(t1).build();
    QueryPlan nested = QueryPlan.builder().source(source).metricProjection(m2).dimensionProjection(d2).timeDimensionProjection(t2).build();
    QueryPlan b = QueryPlan.builder().source(nested).metricProjection(m2).dimensionProjection(d2).timeDimensionProjection(t2).build();
    MetaDataStore metaDataStore = mock(MetaDataStore.class);
    DefaultQueryPlanMerger merger = new DefaultQueryPlanMerger(metaDataStore);
    QueryPlan c = merger.merge(a, b);
    assertNull(c.getWhereFilter());
    assertEquals(List.of(m2, m1), c.getMetricProjections());
    assertEquals(List.of(d2, d1), c.getDimensionProjections());
    assertEquals(List.of(t2, t1), c.getTimeDimensionProjections());
    QueryPlan d = (QueryPlan) c.getSource();
    assertEquals(List.of(m2, m1), d.getMetricProjections());
    assertEquals(List.of(d2, d1), d.getDimensionProjections());
    assertEquals(List.of(t2, t1), d.getTimeDimensionProjections());
    assertEquals(filterExpression, d.getWhereFilter());
}
Also used : MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) Test(org.junit.jupiter.api.Test)

Aggregations

MetaDataStore (com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore)45 Test (org.junit.jupiter.api.Test)28 HashSet (java.util.HashSet)27 SQLQueryEngine (com.yahoo.elide.datastores.aggregation.queryengines.sql.SQLQueryEngine)25 DefaultQueryPlanMerger (com.yahoo.elide.datastores.aggregation.query.DefaultQueryPlanMerger)24 QueryPlanMerger (com.yahoo.elide.datastores.aggregation.query.QueryPlanMerger)19 Table (com.yahoo.elide.modelconfig.model.Table)17 Arrays (java.util.Arrays)9 Set (java.util.Set)9 Argument (com.yahoo.elide.core.request.Argument)8 List (java.util.List)8 Map (java.util.Map)8 EntityDictionary (com.yahoo.elide.core.dictionary.EntityDictionary)7 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)7 Type (com.yahoo.elide.core.type.Type)7 DefaultClassScanner (com.yahoo.elide.core.utils.DefaultClassScanner)7 DefaultQueryValidator (com.yahoo.elide.datastores.aggregation.DefaultQueryValidator)7 ConnectionDetails (com.yahoo.elide.datastores.aggregation.queryengines.sql.ConnectionDetails)7 Collectors (java.util.stream.Collectors)7 Query (com.yahoo.elide.datastores.aggregation.query.Query)6