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