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());
}
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;
}
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));
}
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();
}
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());
}
Aggregations