Search in sources :

Example 11 with ColumnProjection

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

the class HasColumnArgsVisitorTest method testJoinWithLogicalReferenceThatReferencesPhysical.

@Test
public void testJoinWithLogicalReferenceThatReferencesPhysical() throws Exception {
    SQLTable table = metaDataStore.getTable(ClassType.of(TableC.class));
    ColumnProjection projection = table.getColumnProjection("joinWithPhysicalToPhysical");
    assertFalse(matches(table, projection));
}
Also used : SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) Test(org.junit.jupiter.api.Test)

Example 12 with ColumnProjection

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

the class EntityProjectionTranslatorTest method testBasicTranslation.

@Test
public void testBasicTranslation() {
    EntityProjectionTranslator translator = new EntityProjectionTranslator(engine, playerStatsTable, basicProjection, scope, true);
    Query query = translator.getQuery();
    assertEquals(playerStatsTable, query.getSource());
    assertEquals(1, query.getMetricProjections().size());
    String actual = query.getMetricProjections().stream().map(MetricProjection::getAlias).findFirst().orElse(null);
    assertEquals("lowScore", actual);
    assertEquals(1, query.getAllDimensionProjections().size());
    List<ColumnProjection> dimensions = new ArrayList<>(query.getAllDimensionProjections());
    assertEquals("overallRating", dimensions.get(0).getName());
}
Also used : MetricProjection(com.yahoo.elide.datastores.aggregation.query.MetricProjection) Query(com.yahoo.elide.datastores.aggregation.query.Query) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) ArrayList(java.util.ArrayList) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 13 with ColumnProjection

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

the class ColumnArgumentValidator method verifyLogicalReference.

private void verifyLogicalReference(LogicalReference reference) {
    SQLTable sqlTable = (SQLTable) reference.getSource();
    ColumnProjection columnProj = reference.getColumn();
    // This will have dependent column's defined arguments merged with pinned arguments used to invoke this column.
    Map<String, Argument> mergedArguments = columnProj.getArguments();
    Column refColumn = sqlTable.getColumn(Column.class, columnProj.getName());
    verifyPinnedArguments(mergedArguments, refColumn, String.format(errorMsgPrefix + "Type mismatch of Fixed value provided for Dependent Column: '%s' in table: '%s'. ", refColumn.getName(), sqlTable.getName()));
    refColumn.getArgumentDefinitions().forEach(argDef -> {
        String argName = argDef.getName();
        if (column.hasArgumentDefinition(argName)) {
            if (argDef.getType() != column.getArgumentDefinition(argName).getType()) {
                throw new IllegalStateException(String.format(errorMsgPrefix + "Argument type mismatch. Dependent Column: '%s' in table: '%s' has same" + " Argument: '%s' with type '%s'.", refColumn.getName(), sqlTable.getName(), argName, argDef.getType()));
            }
        } else if (StringUtils.isBlank(argDef.getDefaultValue().toString()) && StringUtils.isBlank(mergedArguments.get(argName).getValue().toString())) {
            throw new IllegalStateException(String.format(errorMsgPrefix + "Argument '%s' with type '%s' is not defined but is required for" + " Dependent Column: '%s' in table: '%s'.", argName, argDef.getType(), refColumn.getName(), sqlTable.getName()));
        }
    });
}
Also used : Argument(com.yahoo.elide.core.request.Argument) Column(com.yahoo.elide.datastores.aggregation.metadata.models.Column) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection)

Example 14 with ColumnProjection

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

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

the class LogicalReferenceExtractorTest method testLogicalReference.

@Test
public void testLogicalReference() {
    ColumnProjection playerLevel = playerStats.getColumnProjection("playerLevel");
    List<Reference> references = parser.parse(playerStats, playerLevel.getExpression());
    assertTrue(references.size() == 1);
    ReferenceExtractor<LogicalReference> extractor = new ReferenceExtractor(LogicalReference.class, metaDataStore, ReferenceExtractor.Mode.SAME_QUERY);
    Set<LogicalReference> logicalReferences = references.get(0).accept(extractor);
    assertEquals(1, logicalReferences.size());
    assertTrue(logicalReferences.contains(LogicalReference.builder().source(playerStats).column(playerStats.getColumnProjection("overallRating")).reference(PhysicalReference.builder().source(playerStats).name("overallRating").build()).build()));
}
Also used : 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