use of com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery in project elide by yahoo.
the class SQLQueryEngine method executeQuery.
@Override
public QueryResult executeQuery(Query query, Transaction transaction) {
SqlTransaction sqlTransaction = (SqlTransaction) transaction;
ConnectionDetails details = query.getConnectionDetails();
DataSource dataSource = details.getDataSource();
SQLDialect dialect = details.getDialect();
Query expandedQuery = expandMetricQueryPlans(query);
// Translate the query into SQL.
NativeQuery sql = toSQL(expandedQuery, dialect);
String queryString = sql.toString();
QueryResult.QueryResultBuilder resultBuilder = QueryResult.builder();
NamedParamPreparedStatement stmt;
Pagination pagination = query.getPagination();
if (returnPageTotals(pagination)) {
resultBuilder.pageTotals(getPageTotal(expandedQuery, sql, query, sqlTransaction));
}
log.debug("SQL Query: " + queryString);
stmt = sqlTransaction.initializeStatement(queryString, dataSource);
// Supply the query parameters to the query
supplyFilterQueryParameters(query, stmt, dialect);
// Run the primary query and log the time spent.
ResultSet resultSet = runQuery(stmt, queryString, Function.identity());
resultBuilder.data(new EntityHydrator(resultSet, query, metadataDictionary));
return resultBuilder.build();
}
use of com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery in project elide by yahoo.
the class SQLQueryEngine method explain.
/**
* Returns the actual query string(s) that would be executed for the input {@link Query}.
*
* @param query The query customized for a particular persistent storage or storage client.
* @param dialect SQL dialect to use for this storage.
* @return List of SQL string(s) corresponding to the given query.
*/
public List<String> explain(Query query, SQLDialect dialect) {
List<String> queries = new ArrayList<>();
Query expandedQuery = expandMetricQueryPlans(query);
NativeQuery sql = toSQL(expandedQuery, dialect);
Pagination pagination = query.getPagination();
if (returnPageTotals(pagination)) {
NativeQuery paginationSql = toPageTotalSQL(expandedQuery, sql, dialect);
if (paginationSql != null) {
queries.add(paginationSql.toString());
}
}
queries.add(sql.toString());
return queries;
}
use of com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery in project elide by yahoo.
the class SQLQueryEngine method toPageTotalSQL.
/**
* Takes a SQLQuery and creates a new clone that instead returns the total number of records of the original
* query.
*
* @param query The client query
* @param sql The generated SQL query
* @param sqlDialect the SQL dialect
* @return A new query that returns the total number of records.
*/
private NativeQuery toPageTotalSQL(Query query, NativeQuery sql, SQLDialect sqlDialect) {
// TODO: refactor this method
String groupByDimensions = query.getAllDimensionProjections().stream().map(SQLColumnProjection.class::cast).filter(SQLColumnProjection::isProjected).map((column) -> column.toSQL(query, metaDataStore)).collect(Collectors.joining(", "));
if (groupByDimensions.isEmpty()) {
// Metric projection without group by dimension will return onely 1 record.
return null;
}
NativeQuery innerQuery = NativeQuery.builder().projectionClause(groupByDimensions).fromClause(sql.getFromClause()).joinClause(sql.getJoinClause()).whereClause(sql.getWhereClause()).groupByClause(String.format("GROUP BY %s", groupByDimensions)).havingClause(sql.getHavingClause()).build();
return NativeQuery.builder().projectionClause("COUNT(*)").fromClause(QueryTranslator.getFromClause("(" + innerQuery + ")", applyQuotes("pagination_subquery", sqlDialect), sqlDialect)).build();
}
use of com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery in project elide by yahoo.
the class AggregationDataStoreTransactionTest method loadObjectsPopulatesCache.
@Test
public void loadObjectsPopulatesCache() {
Mockito.reset(queryLogger);
QueryResult queryResult = QueryResult.builder().data(DATA).build();
NativeQuery myQuery = NativeQuery.builder().fromClause(playerStatsTable.getName()).projectionClause(" ").build();
when(queryEngine.getTableVersion(playerStatsTable, qeTransaction)).thenReturn("foo");
when(queryEngine.executeQuery(query, qeTransaction)).thenReturn(queryResult);
when(queryEngine.explain(query)).thenReturn(Arrays.asList(myQuery.toString()));
AggregationDataStoreTransaction transaction = new MyAggregationDataStoreTransaction(queryEngine, cache, queryLogger);
EntityProjection entityProjection = EntityProjection.builder().type(PlayerStats.class).build();
assertEquals(DATA, Lists.newArrayList(transaction.loadObjects(entityProjection, scope)));
String cacheKey = "foo;" + queryKey;
Mockito.verify(cache).get(cacheKey);
Mockito.verify(cache).put(cacheKey, queryResult);
Mockito.verifyNoMoreInteractions(cache);
Mockito.verify(queryLogger, times(1)).acceptQuery(Mockito.eq(scope.getRequestId()), any(), any(), any(), any(), any());
Mockito.verify(queryLogger, times(1)).processQuery(Mockito.eq(scope.getRequestId()), any(), any(), Mockito.eq(false));
Mockito.verify(queryLogger, times(1)).completeQuery(Mockito.eq(scope.getRequestId()), any());
}
use of com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery in project elide by yahoo.
the class AggregationDataStoreTransactionTest method loadObjectsPassesPagination.
@Test
public void loadObjectsPassesPagination() {
Mockito.reset(queryLogger);
QueryResult queryResult = QueryResult.builder().data(DATA).pageTotals(314L).build();
NativeQuery myQuery = NativeQuery.builder().fromClause(playerStatsTable.getName()).projectionClause(" ").build();
when(cache.get(anyString())).thenReturn(queryResult);
when(queryEngine.getTableVersion(playerStatsTable, qeTransaction)).thenReturn("foo");
when(queryEngine.explain(query)).thenReturn(Arrays.asList(myQuery.toString()));
AggregationDataStoreTransaction transaction = new MyAggregationDataStoreTransaction(queryEngine, cache, queryLogger);
Pagination pagination = new PaginationImpl(String.class, null, null, DEFAULT_PAGE_LIMIT, MAX_PAGE_LIMIT, true, false);
EntityProjection entityProjection = EntityProjection.builder().type(PlayerStats.class).pagination(pagination).build();
assertEquals(DATA, Lists.newArrayList(transaction.loadObjects(entityProjection, scope)));
assertEquals(314L, entityProjection.getPagination().getPageTotals());
String cacheKey = "foo;" + queryKey;
Mockito.verify(queryEngine, never()).executeQuery(any(), any());
Mockito.verify(cache).get(cacheKey);
Mockito.verifyNoMoreInteractions(cache);
Mockito.verify(queryLogger, times(1)).acceptQuery(Mockito.eq(scope.getRequestId()), any(), any(), any(), any(), any());
Mockito.verify(queryLogger, times(1)).processQuery(Mockito.eq(scope.getRequestId()), any(), any(), Mockito.eq(true));
Mockito.verify(queryLogger, times(1)).completeQuery(Mockito.eq(scope.getRequestId()), any());
}
Aggregations