Search in sources :

Example 51 with Argument

use of com.yahoo.elide.core.request.Argument in project elide by yahoo.

the class DefaultQueryValidator method validateQueryArguments.

@Override
public void validateQueryArguments(Query query) {
    SQLTable table = (SQLTable) query.getSource();
    table.getArgumentDefinitions().forEach(tableArgument -> {
        Argument clientArgument = query.getArguments().get(tableArgument.getName());
        validateArgument(Optional.ofNullable(clientArgument), tableArgument, "table '" + query.getSource().getName() + "'");
    });
}
Also used : Argument(com.yahoo.elide.core.request.Argument) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)

Example 52 with Argument

use of com.yahoo.elide.core.request.Argument in project elide by yahoo.

the class ColumnContext method resolveSQLHandlebar.

private Object resolveSQLHandlebar(final Object context, final Options options) throws UnsupportedEncodingException {
    String from = options.hash("from");
    String columnName = options.hash("column");
    int argsIndex = columnName.indexOf('[');
    String invokedColumnName = columnName;
    ColumnContext currentCtx = (ColumnContext) context;
    // 'from' is optional, so if not provided use the same table context.
    ColumnContext invokedCtx = isBlank(from) ? currentCtx : (ColumnContext) currentCtx.get(from);
    Map<String, Argument> pinnedArgs = new HashMap<>();
    if (argsIndex >= 0) {
        pinnedArgs = getArgumentMapFromString(columnName.substring(argsIndex));
        invokedColumnName = columnName.substring(0, argsIndex);
    }
    // Physical References starts with $
    if (invokedColumnName.lastIndexOf('$') == 0) {
        return resolvePhysicalReference(invokedCtx, invokedColumnName);
    }
    ColumnProjection column = invokedCtx.getQueryable().getSource().getColumnProjection(invokedColumnName);
    if (column != null) {
        ColumnProjection newColumn = column.withArguments(mergedArgumentMap(column.getArguments(), invokedCtx.getColumn().getArguments(), pinnedArgs));
        return getNewContext(invokedCtx, newColumn).resolve(newColumn.getExpression());
    }
    throw new HandlebarsException(new Throwable("Couldn't find: " + invokedColumnName));
}
Also used : Argument(com.yahoo.elide.core.request.Argument) HashMap(java.util.HashMap) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) Argument.getArgumentMapFromString(com.yahoo.elide.core.request.Argument.getArgumentMapFromString) ToString(lombok.ToString) HandlebarsException(com.github.jknack.handlebars.HandlebarsException)

Example 53 with Argument

use of com.yahoo.elide.core.request.Argument in project elide by yahoo.

the class ExpressionParser method parse.

/**
 * Parses the column or join expression and returns the list of discovered references.
 * @param source The source table where the column or join expression lives.
 * @param expression The expression to parse.
 * @param callingColumnArgs Arguments available with calling column.
 * @return A list of discovered references.
 */
public List<Reference> parse(Queryable source, String expression, Map<String, Argument> callingColumnArgs) {
    List<String> referenceNames = resolveFormulaReferences(expression);
    List<Reference> results = new ArrayList<>();
    Map<String, Argument> fixedArguments = new HashMap<>();
    for (String referenceName : referenceNames) {
        // Change `sql from='joinName' column='columnName[a1:v1][a2:v2]'` to `joinName.columnName`
        if (referenceName.startsWith(SQL_HELPER_PREFIX)) {
            try {
                Template template = handlebars.compileInline(toFormulaReference(referenceName));
                referenceName = template.apply(Collections.emptyMap());
                int argsIndex = referenceName.indexOf('[');
                if (argsIndex >= 0) {
                    fixedArguments = getArgumentMapFromString(referenceName.substring(argsIndex));
                    referenceName = referenceName.substring(0, argsIndex);
                }
            } catch (IOException e) {
                throw new IllegalStateException(e.getMessage());
            }
        }
        // ignore $$column.expr
        if (referenceName.equals(COLUMN_EXPR)) {
            continue;
        }
        if (referenceName.startsWith(COLUMN_ARGS_PREFIX)) {
            results.add(ColumnArgReference.builder().argName(referenceName.substring(COLUMN_ARGS_PREFIX.length())).build());
        } else if (referenceName.startsWith(TABLE_ARGS_PREFIX)) {
            results.add(TableArgReference.builder().argName(referenceName.substring(TABLE_ARGS_PREFIX.length())).build());
        } else if (referenceName.startsWith("$")) {
            results.add(PhysicalReference.builder().source(source).name(referenceName.substring(1)).build());
        } else if (referenceName.contains(".")) {
            results.add(buildJoin(source, referenceName, callingColumnArgs, fixedArguments));
        } else {
            ColumnProjection referencedColumn = source.getColumnProjection(referenceName);
            Preconditions.checkNotNull(referencedColumn, String.format("Couldn't find column: '%s' for table: '%s'", referenceName, source.getName()));
            ColumnProjection newColumn = referencedColumn.withArguments(mergedArgumentMap(referencedColumn.getArguments(), callingColumnArgs, fixedArguments));
            List<Reference> references = buildReferenceForColumn(source, newColumn);
            results.add(LogicalReference.builder().source(source).column(newColumn).references(references).build());
        }
    }
    return results;
}
Also used : Argument(com.yahoo.elide.core.request.Argument) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) Argument.getArgumentMapFromString(com.yahoo.elide.core.request.Argument.getArgumentMapFromString) IOException(java.io.IOException) Template(com.github.jknack.handlebars.Template) ColumnProjection(com.yahoo.elide.datastores.aggregation.query.ColumnProjection) ArrayList(java.util.ArrayList) List(java.util.List)

Example 54 with Argument

use of com.yahoo.elide.core.request.Argument in project elide by yahoo.

the class AggregationDataStoreTransactionTest method testRequiredColumnFilterArguments.

@Test
public void testRequiredColumnFilterArguments() throws Exception {
    Type<PlayerStatsWithRequiredFilter> tableType = ClassType.of(PlayerStatsWithRequiredFilter.class);
    EntityDictionary dictionary = EntityDictionary.builder().build();
    dictionary.bindEntity(PlayerStatsWithRequiredFilter.class);
    SQLTable table = new SQLTable(new Namespace(DEFAULT_NAMESPACE), tableType, dictionary);
    RSQLFilterDialect filterDialect = RSQLFilterDialect.builder().dictionary(dictionary).build();
    FilterExpression where = filterDialect.parse(tableType, new HashSet<>(), "recordedDate>2019-07-12T00:00Z", NO_VERSION);
    Query query = Query.builder().column(SQLMetricProjection.builder().name("highScore").alias("highScore").build()).whereFilter(where).source(table).build();
    AggregationDataStoreTransaction tx = new AggregationDataStoreTransaction(queryEngine, cache, queryLogger);
    Query modifiedQuery = tx.addColumnFilterArguments(table, query, dictionary);
    Map<String, Argument> columnArguments = modifiedQuery.getColumnProjection("highScore").getArguments();
    assertTrue(columnArguments.containsKey("recordedDate"));
    assertEquals(1, columnArguments.size());
}
Also used : NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) Query(com.yahoo.elide.datastores.aggregation.query.Query) Argument(com.yahoo.elide.core.request.Argument) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Namespace(com.yahoo.elide.datastores.aggregation.metadata.models.Namespace) PlayerStatsWithRequiredFilter(example.PlayerStatsWithRequiredFilter) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Example 55 with Argument

use of com.yahoo.elide.core.request.Argument in project elide by yahoo.

the class AggregationDataStoreTransactionTest method testRequiredTableFilterArguments.

@Test
public void testRequiredTableFilterArguments() throws Exception {
    Type<PlayerStatsWithRequiredFilter> tableType = ClassType.of(PlayerStatsWithRequiredFilter.class);
    EntityDictionary dictionary = EntityDictionary.builder().build();
    dictionary.bindEntity(PlayerStatsWithRequiredFilter.class);
    SQLTable table = new SQLTable(new Namespace(DEFAULT_NAMESPACE), tableType, dictionary);
    RSQLFilterDialect filterDialect = RSQLFilterDialect.builder().dictionary(dictionary).build();
    FilterExpression where = filterDialect.parse(tableType, new HashSet<>(), "recordedDate>=2019-07-12T00:00Z;recordedDate<2030-07-12T00:00Z", NO_VERSION);
    Query query = Query.builder().source(table).whereFilter(where).build();
    AggregationDataStoreTransaction tx = new AggregationDataStoreTransaction(queryEngine, cache, queryLogger);
    Query modifiedQuery = tx.addTableFilterArguments(table, query, dictionary);
    Map<String, Argument> tableArguments = modifiedQuery.getArguments();
    assertTrue(tableArguments.containsKey("start"));
    assertTrue(tableArguments.containsKey("end"));
    assertEquals(2, tableArguments.size());
}
Also used : NativeQuery(com.yahoo.elide.datastores.aggregation.queryengines.sql.query.NativeQuery) Query(com.yahoo.elide.datastores.aggregation.query.Query) Argument(com.yahoo.elide.core.request.Argument) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Namespace(com.yahoo.elide.datastores.aggregation.metadata.models.Namespace) PlayerStatsWithRequiredFilter(example.PlayerStatsWithRequiredFilter) SQLTable(com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable) FilterExpression(com.yahoo.elide.core.filter.expression.FilterExpression) EntityDictionary(com.yahoo.elide.core.dictionary.EntityDictionary) RSQLFilterDialect(com.yahoo.elide.core.filter.dialect.RSQLFilterDialect) SQLUnitTest(com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest) Test(org.junit.jupiter.api.Test)

Aggregations

Argument (com.yahoo.elide.core.request.Argument)71 Test (org.junit.jupiter.api.Test)59 HashMap (java.util.HashMap)48 Query (com.yahoo.elide.datastores.aggregation.query.Query)38 FilterExpression (com.yahoo.elide.core.filter.expression.FilterExpression)35 SQLUnitTest (com.yahoo.elide.datastores.aggregation.framework.SQLUnitTest)25 SQLTable (com.yahoo.elide.datastores.aggregation.queryengines.sql.metadata.SQLTable)22 FilterPredicate (com.yahoo.elide.core.filter.predicates.FilterPredicate)15 Day (com.yahoo.elide.datastores.aggregation.timegrains.Day)14 HashSet (java.util.HashSet)14 Path (com.yahoo.elide.core.Path)12 ToString (lombok.ToString)12 OrFilterExpression (com.yahoo.elide.core.filter.expression.OrFilterExpression)11 AndFilterExpression (com.yahoo.elide.core.filter.expression.AndFilterExpression)10 Date (java.util.Date)10 SQLDimensionProjection (com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLDimensionProjection)9 GameRevenue (example.GameRevenue)7 PlayerStats (example.PlayerStats)6 SQLMetricProjection (com.yahoo.elide.datastores.aggregation.queryengines.sql.query.SQLMetricProjection)5 Month (com.yahoo.elide.datastores.aggregation.timegrains.Month)5