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