use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.
the class ClauseComparator method mergeConsecutiveWhereClauses.
// Merge consecutive "where" clauses.
private void mergeConsecutiveWhereClauses(List<Clause> clauses) throws CompilationException {
List<Clause> results = new ArrayList<Clause>();
int size = clauses.size();
for (int index = 0; index < size; ) {
Clause clause = clauses.get(index);
if (clause.getClauseType() != ClauseType.WHERE_CLAUSE) {
results.add(clause);
++index;
} else {
List<Expression> expressions = new ArrayList<Expression>();
Clause firstWhereClause = clause;
do {
WhereClause whereClause = (WhereClause) clause;
expressions.add(whereClause.getWhereExpr());
if (++index >= size) {
break;
}
clause = clauses.get(index);
} while (clause.getClauseType() == ClauseType.WHERE_CLAUSE);
if (expressions.size() > 1) {
OperatorExpr newWhereExpr = new OperatorExpr();
newWhereExpr.setExprList(expressions);
newWhereExpr.setCurrentop(true);
for (int operatorIndex = 0; operatorIndex < expressions.size(); ++operatorIndex) {
newWhereExpr.addOperator("and");
}
results.add(new WhereClause(newWhereExpr));
} else {
results.add(firstWhereClause);
}
}
}
clauses.clear();
clauses.addAll(results);
}
use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.
the class ClauseComparator method extractDefinedCollectionVariables.
// Extracts the variables to be substituted with a path access.
private Map<VariableExpr, Expression> extractDefinedCollectionVariables(List<Clause> clauses, GroupbyClause cuttingGbyClause, String generatedAlias) {
Map<VariableExpr, Expression> varExprMap = new HashMap<VariableExpr, Expression>();
List<VariableExpr> varToSubstitute = collectProducedVariablesFromGroupby(cuttingGbyClause);
int gbyIndex = clauses.indexOf(cuttingGbyClause);
for (int i = gbyIndex + 1; i < clauses.size(); i++) {
Clause cl = clauses.get(i);
if (cl.getClauseType() == ClauseType.LET_CLAUSE) {
varToSubstitute.add(((LetClause) cl).getVarExpr());
}
}
for (VariableExpr var : varToSubstitute) {
varExprMap.put(var, new FieldAccessor(new VariableExpr(new VarIdentifier(generatedAlias)), new VarIdentifier(var.getVar().getValue().substring(1))));
}
return varExprMap;
}
use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.
the class AqlDeleteRewriteVisitor method visit.
@Override
public Void visit(DeleteStatement deleteStmt, Void visitArg) {
List<Expression> arguments = new ArrayList<>();
Identifier dataverseName = deleteStmt.getDataverseName();
Identifier datasetName = deleteStmt.getDatasetName();
String arg = dataverseName == null ? datasetName.getValue() : dataverseName.getValue() + "." + datasetName.getValue();
LiteralExpr argumentLiteral = new LiteralExpr(new StringLiteral(arg));
arguments.add(argumentLiteral);
CallExpr callExpression = new CallExpr(new FunctionSignature(FunctionConstants.ASTERIX_NS, "dataset", 1), arguments);
List<Clause> clauseList = new ArrayList<>();
VariableExpr var = deleteStmt.getVariableExpr();
Clause forClause = new ForClause(var, callExpression);
clauseList.add(forClause);
Clause whereClause = null;
Expression condition = deleteStmt.getCondition();
if (condition != null) {
whereClause = new WhereClause(condition);
clauseList.add(whereClause);
}
VariableExpr returnExpr = new VariableExpr(var.getVar());
returnExpr.setIsNewVar(false);
FLWOGRExpression flowgr = new FLWOGRExpression(clauseList, returnExpr);
Query query = new Query(false);
query.setBody(flowgr);
deleteStmt.setQuery(query);
return null;
}
use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.
the class AqlQueryRewriter method inlineDeclaredUdfs.
private void inlineDeclaredUdfs() throws CompilationException {
if (topStatement == null) {
return;
}
List<FunctionSignature> funIds = new ArrayList<FunctionSignature>();
for (FunctionDecl fdecl : declaredFunctions) {
funIds.add(fdecl.getSignature());
}
List<FunctionDecl> storedFunctionDecls = new ArrayList<>();
for (Expression topLevelExpr : topStatement.getDirectlyEnclosedExpressions()) {
storedFunctionDecls.addAll(FunctionUtil.retrieveUsedStoredFunctions(metadataProvider, topLevelExpr, funIds, null, expr -> getFunctionCalls(expr), func -> functionParser.getFunctionDecl(func), signature -> CommonFunctionMapUtil.normalizeBuiltinFunctionSignature(signature)));
declaredFunctions.addAll(storedFunctionDecls);
}
if (!declaredFunctions.isEmpty()) {
AQLInlineUdfsVisitor visitor = new AQLInlineUdfsVisitor(context, new AQLRewriterFactory(), declaredFunctions, metadataProvider);
while (topStatement.accept(visitor, declaredFunctions)) {
// loop until no more changes
}
}
declaredFunctions.removeAll(storedFunctionDecls);
}
use of org.apache.asterix.lang.common.base.Expression in project asterixdb by apache.
the class RangeMapBuilder method parseHint.
public static IRangeMap parseHint(Object hint) throws CompilationException {
ArrayBackedValueStorage abvs = new ArrayBackedValueStorage();
DataOutput out = abvs.getDataOutput();
;
abvs.reset();
IParser parser = parserFactory.createParser((String) hint);
List<Statement> hintStatements = parser.parse();
if (hintStatements.size() != 1) {
throw new CompilationException("Only one range statement is allowed for the range hint.");
}
// Translate the query into a Range Map
if (hintStatements.get(0).getKind() != Statement.Kind.QUERY) {
throw new CompilationException("Not a proper query for the range hint.");
}
Query q = (Query) hintStatements.get(0);
if (q.getBody().getKind() != Kind.LIST_CONSTRUCTOR_EXPRESSION) {
throw new CompilationException("The range hint must be a list.");
}
List<Expression> el = ((ListConstructor) q.getBody()).getExprList();
int[] offsets = new int[el.size()];
// Loop over list of literals
for (int i = 0; i < el.size(); ++i) {
Expression item = el.get(i);
if (item.getKind() == Kind.LITERAL_EXPRESSION) {
parseLiteralToBytes(item, out);
offsets[i] = abvs.getLength();
}
// TODO Add support for composite fields.
}
return new RangeMap(1, abvs.getByteArray(), offsets);
}
Aggregations