use of com.yahoo.elide.datastores.aggregation.query.Optimizer in project elide by yahoo.
the class AggregateBeforeJoinOptimizerTest method init.
@BeforeAll
public static void init() {
MetaDataStore metaDataStore = new MetaDataStore(DefaultClassScanner.getInstance(), getClassType(dictionary.getScanner().getAnnotatedClasses("example", Include.class)), false);
Set<Optimizer> optimizers = new HashSet<>(Arrays.asList(new AggregateBeforeJoinOptimizer(metaDataStore)));
init(new H2Dialect(), optimizers, metaDataStore);
}
use of com.yahoo.elide.datastores.aggregation.query.Optimizer 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.query.Optimizer in project elide by yahoo.
the class SkipOptimizerTest method init.
@BeforeAll
public static void init() {
MetaDataStore metaDataStore = new MetaDataStore(DefaultClassScanner.getInstance(), getClassType(DefaultClassScanner.getInstance().getAnnotatedClasses("example", Include.class)), false);
Set<Optimizer> optimizers = new HashSet<>(Arrays.asList(optimizer));
init(new H2Dialect(), optimizers, metaDataStore);
}
use of com.yahoo.elide.datastores.aggregation.query.Optimizer in project elide by yahoo.
the class SQLQueryEngine method expandMetricQueryPlans.
/**
* Transforms a client query into a potentially nested/complex query by expanding each metric into
* its respective query plan - and then merging the plans together into a consolidated query.
* @param query The client query.
* @return A query that reflects each metric's individual query plan.
*/
private Query expandMetricQueryPlans(Query query) {
// Expand each metric into its own query plan.
List<QueryPlan> toMerge = query.getMetricProjections().stream().map(projection -> projection.resolve(query)).collect(Collectors.toList());
// Merge all the queries together.
List<QueryPlan> mergedPlans = merger.merge(toMerge);
// TODO - Support joins across plans rather than rejecting plans that don't merge.
if (mergedPlans.size() != 1) {
throw new UnsupportedOperationException("Incompatible metrics in client query. Cannot merge " + "into a single query");
}
QueryPlan mergedPlan = mergedPlans.get(0);
QueryPlanTranslator queryPlanTranslator = new QueryPlanTranslator(query, metaDataStore, merger);
Query merged = (mergedPlan == null) ? QueryPlanTranslator.addHiddenProjections(metaDataStore, query).build() : queryPlanTranslator.translate(mergedPlan);
for (Optimizer optimizer : optimizers) {
SQLTable table = (SQLTable) query.getSource();
// override table hints.
if (table.getHints().contains(optimizer.negateHint())) {
continue;
}
if (!table.getHints().contains(optimizer.hint())) {
continue;
}
if (optimizer.canOptimize(merged)) {
merged = optimizer.optimize(merged);
}
}
return merged;
}
Aggregations