Search in sources :

Example 1 with Queryable

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

the class ColumnContext method getJoinContext.

protected ColumnContext getJoinContext(String key) {
    SQLJoin sqlJoin = this.queryable.getJoin(key);
    String joinExpression = sqlJoin.getJoinExpression();
    PhysicalRefColumnContext context = PhysicalRefColumnContext.physicalRefContextBuilder().queryable(this.getQueryable()).metaDataStore(this.getMetaDataStore()).column(this.getColumn()).tableArguments(this.getTableArguments()).build();
    // This will resolve everything within join expression except Physical References, Use this resolved value
    // to create alias for join dynamically.
    String resolvedJoinExpr = context.resolve(joinExpression);
    String joinAlias = createSafeAlias(appendAlias(this.alias, key), resolvedJoinExpr);
    Queryable joinQueryable = metaDataStore.getTable(sqlJoin.getJoinTableType());
    ColumnContext joinCtx = ColumnContext.builder().queryable(joinQueryable).alias(joinAlias).metaDataStore(this.metaDataStore).column(this.column).tableArguments(mergedArgumentMap(joinQueryable.getArguments(), this.getTableArguments())).build();
    return joinCtx;
}
Also used : Queryable(com.yahoo.elide.datastores.aggregation.query.Queryable) SQLJoin(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLJoin) Argument.getArgumentMapFromString(com.yahoo.elide.core.request.Argument.getArgumentMapFromString) ToString(lombok.ToString)

Example 2 with Queryable

use of com.yahoo.elide.datastores.aggregation.query.Queryable 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 3 with Queryable

use of com.yahoo.elide.datastores.aggregation.query.Queryable 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)

Example 4 with Queryable

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

the class SQLMetricProjection method nest.

@Override
public Pair<ColumnProjection, Set<ColumnProjection>> nest(Queryable source, MetaDataStore metaDataStore, boolean joinInOuter) {
    SQLDialect dialect = source.getConnectionDetails().getDialect();
    String sql = toSQL(source, metaDataStore);
    SqlParser sqlParser = SqlParser.create(sql, CalciteUtils.constructParserConfig(dialect));
    SqlNode node;
    try {
        node = sqlParser.parseExpression();
    } catch (SqlParseException e) {
        throw new IllegalStateException(e);
    }
    CalciteInnerAggregationExtractor innerExtractor = new CalciteInnerAggregationExtractor(dialect);
    List<List<String>> innerAggExpressions = node.accept(innerExtractor);
    List<List<String>> innerAggLabels = innerAggExpressions.stream().map(list -> list.stream().map((expression) -> getAggregationLabel(dialect.getCalciteDialect(), expression)).collect(Collectors.toList())).collect(Collectors.toList());
    Set<ColumnProjection> innerAggProjections = new LinkedHashSet<>();
    Iterator<String> labelIt = innerAggLabels.stream().flatMap(List::stream).iterator();
    Iterator<String> expressionIt = innerAggExpressions.stream().flatMap(List::stream).iterator();
    while (labelIt.hasNext() && expressionIt.hasNext()) {
        String innerAggExpression = expressionIt.next();
        String innerAggLabel = labelIt.next();
        innerAggProjections.add(SQLMetricProjection.builder().projected(true).name(innerAggLabel).alias(innerAggLabel).expression(innerAggExpression).columnType(columnType).valueType(valueType).arguments(arguments).build());
    }
    CalciteOuterAggregationExtractor outerExtractor = new CalciteOuterAggregationExtractor(dialect, innerAggLabels);
    SqlNode transformedParseTree = node.accept(outerExtractor);
    String outerAggExpression = transformedParseTree.toSqlString(dialect.getCalciteDialect()).getSql();
    // replace INNER_AGG_... with {{$INNER_AGG...}}
    outerAggExpression = outerAggExpression.replaceAll(dialect.getBeginQuote() + "?(" + getAggregationLabelPrefix(dialect.getCalciteDialect()) + "\\w+)" + dialect.getEndQuote() + "?", "{{\\$" + "$1" + "}}");
    String columnId = source.isRoot() ? getName() : getAlias();
    boolean inProjection = source.getColumnProjection(columnId, arguments, true) != null;
    ColumnProjection outerProjection = SQLMetricProjection.builder().projected(inProjection).expression(outerAggExpression).name(name).alias(alias).valueType(valueType).columnType(columnType).arguments(arguments).build();
    return Pair.of(outerProjection, innerAggProjections);
}
Also used : CalciteOuterAggregationExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.CalciteOuterAggregationExtractor) Argument(com.yahoo.elide.core.request.Argument) CalciteInnerAggregationExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.CalciteInnerAggregationExtractor) SqlNode(org.apache.calcite.sql.SqlNode) Pair(org.apache.commons.lang3.tuple.Pair) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) Map(java.util.Map) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) MetricProjection(com.yahoo.elide.datastores.aggregation.query.MetricProjection) LinkedHashSet(java.util.LinkedHashSet) Queryable(com.yahoo.elide.datastores.aggregation.query.Queryable) Iterator(java.util.Iterator) ColumnType(com.yahoo.elide.datastores.aggregation.metadata.enums.ColumnType) ValueType(com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType) Casing(org.apache.calcite.avatica.util.Casing) Set(java.util.Set) QueryPlan(com.yahoo.elide.datastores.aggregation.query.QueryPlan) Collectors(java.util.stream.Collectors) SqlDialect(org.apache.calcite.sql.SqlDialect) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) Builder(lombok.Builder) CalciteUtils(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.CalciteUtils) SqlParser(org.apache.calcite.sql.parser.SqlParser) Data(lombok.Data) Pattern(java.util.regex.Pattern) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) Metric(com.yahoo.elide.datastores.aggregation.metadata.models.Metric) LinkedHashSet(java.util.LinkedHashSet) SqlParseException(org.apache.calcite.sql.parser.SqlParseException) CalciteInnerAggregationExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.CalciteInnerAggregationExtractor) SqlParser(org.apache.calcite.sql.parser.SqlParser) CalciteOuterAggregationExtractor(com.yahoo.elide.datastores.aggregation.queryengines.sql.calcite.CalciteOuterAggregationExtractor) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) List(java.util.List) SqlNode(org.apache.calcite.sql.SqlNode)

Example 5 with Queryable

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

the class HasJoinVisitorTest method testPhysicalColumnNoJoin.

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

Aggregations

Queryable (com.yahoo.elide.datastores.aggregation.query.Queryable)8 ColumnProjection (com.yahoo.elide.datastores.aggregation.query.ColumnProjection)5 Test (org.junit.jupiter.api.Test)3 Argument.getArgumentMapFromString (com.yahoo.elide.core.request.Argument.getArgumentMapFromString)2 JoinPath (com.yahoo.elide.datastores.aggregation.core.JoinPath)2 MetaDataStore (com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore)2 SQLDialect (com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect)2 SQLJoin (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLJoin)2 LinkedHashSet (java.util.LinkedHashSet)2 List (java.util.List)2 Set (java.util.Set)2 Collectors (java.util.stream.Collectors)2 ToString (lombok.ToString)2 Pair (org.apache.commons.lang3.tuple.Pair)2 Path (com.yahoo.elide.core.Path)1 Argument (com.yahoo.elide.core.request.Argument)1 ColumnContext (com.yahoo.elide.datastores.aggregation.metadata.ColumnContext)1 PhysicalRefColumnContext (com.yahoo.elide.datastores.aggregation.metadata.PhysicalRefColumnContext)1 ColumnType (com.yahoo.elide.datastores.aggregation.metadata.enums.ColumnType)1 ValueType (com.yahoo.elide.datastores.aggregation.metadata.enums.ValueType)1