Search in sources :

Example 6 with MetaDataStore

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

the class SQLUnitTest method init.

public static void init(SQLDialect sqlDialect, Set<Optimizer> optimizers, MetaDataStore metaDataStore) {
    Properties properties = new Properties();
    properties.put("driverClassName", "org.h2.Driver");
    String jdbcUrl = "jdbc:h2:mem:db1;DB_CLOSE_DELAY=-1" + ";NON_KEYWORDS=VALUE,USER" + ";DATABASE_TO_UPPER=FALSE" + getCompatabilityMode(sqlDialect.getDialectType());
    properties.put("jdbcUrl", jdbcUrl);
    HikariConfig config = new HikariConfig(properties);
    DataSource dataSource = new HikariDataSource(config);
    try (Connection h2Conn = dataSource.getConnection()) {
        h2Conn.createStatement().execute("RUNSCRIPT FROM 'classpath:prepare_tables.sql'");
    } catch (SQLException e) {
        ((HikariDataSource) dataSource).close();
        throw new IllegalStateException(e);
    }
    SQLUnitTest.metaDataStore = metaDataStore;
    dictionary = EntityDictionary.builder().build();
    dictionary.bindEntity(PlayerStatsWithView.class);
    dictionary.bindEntity(PlayerStatsView.class);
    dictionary.bindEntity(PlayerStats.class);
    dictionary.bindEntity(Country.class);
    dictionary.bindEntity(SubCountry.class);
    dictionary.bindEntity(Player.class);
    dictionary.bindEntity(CountryView.class);
    dictionary.bindEntity(CountryViewNested.class);
    dictionary.bindEntity(Continent.class);
    dictionary.bindEntity(GameRevenue.class);
    filterParser = RSQLFilterDialect.builder().dictionary(dictionary).build();
    // Manually register the serdes because we are not running a complete Elide service.
    CoerceUtil.register(Day.class, new Day.DaySerde());
    CoerceUtil.register(Hour.class, new Hour.HourSerde());
    CoerceUtil.register(ISOWeek.class, new ISOWeek.ISOWeekSerde());
    CoerceUtil.register(Minute.class, new Minute.MinuteSerde());
    CoerceUtil.register(Month.class, new Month.MonthSerde());
    CoerceUtil.register(Quarter.class, new Quarter.QuarterSerde());
    CoerceUtil.register(Second.class, new Second.SecondSerde());
    CoerceUtil.register(Week.class, new Week.WeekSerde());
    CoerceUtil.register(Year.class, new Year.YearSerde());
    metaDataStore.populateEntityDictionary(dictionary);
    // Need to provide details for connections used by all available models.
    Map<String, ConnectionDetails> connectionDetailsMap = new HashMap<>();
    connectionDetailsMap.put("mycon", new ConnectionDetails(dataSource, sqlDialect));
    connectionDetailsMap.put("SalesDBConnection", new ConnectionDetails(DUMMY_DATASOURCE, sqlDialect));
    Function<String, ConnectionDetails> connectionLookup = (name) -> connectionDetailsMap.getOrDefault(name, new ConnectionDetails(dataSource, sqlDialect));
    engine = new SQLQueryEngine(metaDataStore, connectionLookup, optimizers, new DefaultQueryPlanMerger(metaDataStore), new DefaultQueryValidator(metaDataStore.getMetadataDictionary()));
    playerStatsTable = (SQLTable) metaDataStore.getTable("playerStats", NO_VERSION);
    videoGameTable = (SQLTable) metaDataStore.getTable("videoGame", NO_VERSION);
    playerStatsViewTable = (SQLTable) metaDataStore.getTable("playerStatsView", NO_VERSION);
    playerStatsViewTableArgs = new HashMap<>();
    playerStatsViewTableArgs.put("rating", Argument.builder().name("overallRating").value("Great").build());
    playerStatsViewTableArgs.put("minScore", Argument.builder().name("minScore").value("0").build());
}
Also used : Quarter(com.yahoo.elide.datastores.aggregation.timegrains.Quarter) Year(com.yahoo.elide.datastores.aggregation.timegrains.Year) BeforeEach(org.junit.jupiter.api.BeforeEach) FilterPredicate(com.yahoo.elide.core.filter.predicates.FilterPredicate) Arrays(java.util.Arrays) Path(com.yahoo.elide.core.Path) Connection(java.sql.Connection) Provider(javax.inject.Provider) Include(com.yahoo.elide.annotation.Include) ParseException(com.yahoo.elide.core.filter.dialect.ParseException) SortingImpl(com.yahoo.elide.core.sort.SortingImpl) ClassType(com.yahoo.elide.core.type.ClassType) Argument(com.yahoo.elide.core.request.Argument) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) Continent(example.dimensions.Continent) ISOWeek(com.yahoo.elide.datastores.aggregation.timegrains.ISOWeek) OrFilterExpression(com.yahoo.elide.core.filter.expression.OrFilterExpression) Map(java.util.Map) MetricProjection(com.yahoo.elide.datastores.aggregation.query.MetricProjection) DefaultQueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.DefaultQueryPlanMerger) SQLDialectFactory(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialectFactory) STRING_TYPE(com.yahoo.elide.core.type.ClassType.STRING_TYPE) DefaultClassScanner(com.yahoo.elide.core.utils.DefaultClassScanner) Set(java.util.Set) CoerceUtil(com.yahoo.elide.core.utils.coerce.CoerceUtil) Collectors(java.util.stream.Collectors) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) PlayerStats(example.PlayerStats) ConnectionDetails(com.yahoo.elide.datastores.aggregation.queryengines.sql.ConnectionDetails) Query(com.yahoo.elide.datastores.aggregation.query.Query) List(java.util.List) Hour(com.yahoo.elide.datastores.aggregation.timegrains.Hour) ImmutablePagination(com.yahoo.elide.datastores.aggregation.query.ImmutablePagination) TypeHelper.getClassType(com.yahoo.elide.core.utils.TypeHelper.getClassType) HikariDataSource(com.zaxxer.hikari.HikariDataSource) AndFilterExpression(com.yahoo.elide.core.filter.expression.AndFilterExpression) Optimizer(com.yahoo.elide.datastores.aggregation.query.Optimizer) Pattern(java.util.regex.Pattern) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) Assertions.assertDoesNotThrow(org.junit.jupiter.api.Assertions.assertDoesNotThrow) Attribute(com.yahoo.elide.core.request.Attribute) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) SQLQueryEngine(com.yahoo.elide.datastores.aggregation.queryengines.sql.SQLQueryEngine) CountryViewNested(example.dimensions.CountryViewNested) Day(com.yahoo.elide.datastores.aggregation.timegrains.Day) Quarter(com.yahoo.elide.datastores.aggregation.timegrains.Quarter) SubCountry(example.dimensions.SubCountry) Second(com.yahoo.elide.datastores.aggregation.timegrains.Second) Week(com.yahoo.elide.datastores.aggregation.timegrains.Week) HashMap(java.util.HashMap) TimeGrain(com.yahoo.elide.datastores.aggregation.metadata.enums.TimeGrain) CountryView(example.dimensions.CountryView) Function(java.util.function.Function) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) SQLException(java.sql.SQLException) SQLDialect(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialect) Month(com.yahoo.elide.datastores.aggregation.timegrains.Month) DataSource(javax.sql.DataSource) NO_VERSION(com.yahoo.elide.core.dictionary.EntityDictionary.NO_VERSION) StreamSupport(java.util.stream.StreamSupport) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) LinkedHashSet(java.util.LinkedHashSet) PlayerStatsView(example.PlayerStatsView) QueryEngine(com.yahoo.elide.datastores.aggregation.QueryEngine) DimensionProjection(com.yahoo.elide.datastores.aggregation.query.DimensionProjection) Properties(java.util.Properties) Sorting(com.yahoo.elide.core.request.Sorting) GameRevenue(example.GameRevenue) Minute(com.yahoo.elide.datastores.aggregation.timegrains.Minute) DefaultQueryValidator(com.yahoo.elide.datastores.aggregation.DefaultQueryValidator) Player(example.Player) HikariConfig(com.zaxxer.hikari.HikariConfig) AfterEach(org.junit.jupiter.api.AfterEach) Country(example.dimensions.Country) TreeMap(java.util.TreeMap) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) Type(com.yahoo.elide.core.type.Type) Operator(com.yahoo.elide.core.filter.Operator) PlayerStatsWithView(example.PlayerStatsWithView) SQLQueryEngine(com.yahoo.elide.datastores.aggregation.queryengines.sql.SQLQueryEngine) HikariDataSource(com.zaxxer.hikari.HikariDataSource) SQLException(java.sql.SQLException) HashMap(java.util.HashMap) ConnectionDetails(com.yahoo.elide.datastores.aggregation.queryengines.sql.ConnectionDetails) DefaultQueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.DefaultQueryPlanMerger) Properties(java.util.Properties) DefaultQueryValidator(com.yahoo.elide.datastores.aggregation.DefaultQueryValidator) Month(com.yahoo.elide.datastores.aggregation.timegrains.Month) Minute(com.yahoo.elide.datastores.aggregation.timegrains.Minute) Hour(com.yahoo.elide.datastores.aggregation.timegrains.Hour) Connection(java.sql.Connection) HikariConfig(com.zaxxer.hikari.HikariConfig) HikariDataSource(com.zaxxer.hikari.HikariDataSource) DataSource(javax.sql.DataSource) ISOWeek(com.yahoo.elide.datastores.aggregation.timegrains.ISOWeek) Second(com.yahoo.elide.datastores.aggregation.timegrains.Second) Year(com.yahoo.elide.datastores.aggregation.timegrains.Year) Day(com.yahoo.elide.datastores.aggregation.timegrains.Day) ISOWeek(com.yahoo.elide.datastores.aggregation.timegrains.ISOWeek) Week(com.yahoo.elide.datastores.aggregation.timegrains.Week)

Example 7 with MetaDataStore

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

the class LoopCountryB method testReferenceLoop.

@Test
public void testReferenceLoop() {
    MetaDataStore metaDataStore = new MetaDataStore(DefaultClassScanner.getInstance(), getClassType(Sets.newHashSet(DimensionLoop.class)), true);
    IllegalArgumentException exception = assertThrows(IllegalArgumentException.class, () -> new SQLQueryEngine(metaDataStore, (unused) -> DUMMY_CONNECTION));
    assertTrue(exception.getMessage().startsWith("Formula reference loop found:"));
}
Also used : SQLQueryEngine(com.yahoo.elide.datastores.aggregation.queryengines.sql.SQLQueryEngine) Assertions.assertThrows(org.junit.jupiter.api.Assertions.assertThrows) SQLDialectFactory(com.yahoo.elide.datastores.aggregation.queryengines.sql.dialects.SQLDialectFactory) Entity(javax.persistence.Entity) Setter(lombok.Setter) Arrays(java.util.Arrays) ManyToOne(javax.persistence.ManyToOne) SQLQueryEngine(com.yahoo.elide.datastores.aggregation.queryengines.sql.SQLQueryEngine) DefaultClassScanner(com.yahoo.elide.core.utils.DefaultClassScanner) JoinColumn(javax.persistence.JoinColumn) Include(com.yahoo.elide.annotation.Include) Formula(org.hibernate.annotations.Formula) DimensionFormula(com.yahoo.elide.datastores.aggregation.annotation.DimensionFormula) Sets(com.google.common.collect.Sets) ConnectionDetails(com.yahoo.elide.datastores.aggregation.queryengines.sql.ConnectionDetails) Test(org.junit.jupiter.api.Test) TypeHelper.getClassType(com.yahoo.elide.core.utils.TypeHelper.getClassType) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) HikariDataSource(com.zaxxer.hikari.HikariDataSource) Data(lombok.Data) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) Id(javax.persistence.Id) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) Test(org.junit.jupiter.api.Test)

Example 8 with MetaDataStore

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

the class TemplateConfigValidator method validate.

@Override
public void validate(Map<String, ConfigFile> resourceMap) {
    MetaDataStore metaDataStore = rebuildMetaDataStore(resourceMap);
    metaDataStore.getTables().forEach(table -> {
        SQLTable sqlTable = (SQLTable) table;
        checkForCycles(sqlTable, metaDataStore);
        TableArgumentValidator tableArgValidator = new TableArgumentValidator(metaDataStore, sqlTable);
        tableArgValidator.validate();
        sqlTable.getAllColumns().forEach(column -> {
            ColumnArgumentValidator colArgValidator = new ColumnArgumentValidator(metaDataStore, sqlTable, column);
            colArgValidator.validate();
        });
    });
}
Also used : MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)

Example 9 with MetaDataStore

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

the class TemplateConfigValidator method rebuildMetaDataStore.

// Rebuilds the MetaDataStore for each validation so that we can validate templates.
MetaDataStore rebuildMetaDataStore(Map<String, ConfigFile> resourceMap) {
    DynamicConfigValidator validator = new DynamicConfigValidator(scanner, configRoot);
    validator.validate(resourceMap);
    MetaDataStore metaDataStore = new MetaDataStore(scanner, validator.getTables(), validator.getNamespaceConfigurations(), false);
    // Populates the metadata store with SQL tables.
    new SQLQueryEngine(metaDataStore, (unused) -> null, new HashSet<>(), new DefaultQueryPlanMerger(metaDataStore), new DefaultQueryValidator(metaDataStore.getMetadataDictionary()));
    return metaDataStore;
}
Also used : SQLQueryEngine(com.yahoo.elide.datastores.aggregation.queryengines.sql.SQLQueryEngine) DefaultQueryValidator(com.yahoo.elide.datastores.aggregation.DefaultQueryValidator) MetaDataStore(com.yahoo.elide.datastores.aggregation.metadata.MetaDataStore) DefaultQueryPlanMerger(com.yahoo.elide.datastores.aggregation.query.DefaultQueryPlanMerger) DynamicConfigValidator(com.yahoo.elide.modelconfig.validator.DynamicConfigValidator)

Example 10 with MetaDataStore

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

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