use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.
the class FunctionParser method getFunctionDecl.
public FunctionDecl getFunctionDecl(Function function) throws CompilationException {
String functionBody = function.getFunctionBody();
List<String> params = function.getParams();
List<VarIdentifier> varIdentifiers = new ArrayList<VarIdentifier>();
StringBuilder builder = new StringBuilder();
builder.append(" use dataverse " + function.getDataverseName() + ";");
builder.append(" declare function " + function.getName().split("@")[0]);
builder.append("(");
boolean first = true;
for (String param : params) {
VarIdentifier varId = new VarIdentifier(param);
varIdentifiers.add(varId);
if (first) {
first = false;
} else {
builder.append(",");
}
builder.append(param);
}
builder.append("){\n").append(functionBody).append("\n}");
IParser parser = parserFactory.createParser(new CharSequenceReader(builder));
List<Statement> statements = parser.parse();
FunctionDecl decl = (FunctionDecl) statements.get(1);
return decl;
}
use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.
the class AbstractInlineUdfsVisitor method inlineUdfsInExpr.
protected Pair<Boolean, Expression> inlineUdfsInExpr(Expression expr, List<FunctionDecl> arg) throws CompilationException {
if (expr.getKind() != Kind.CALL_EXPRESSION) {
boolean r = expr.accept(this, arg);
return new Pair<>(r, expr);
}
CallExpr f = (CallExpr) expr;
boolean r = expr.accept(this, arg);
FunctionDecl implem = findFuncDeclaration(f.getFunctionSignature(), arg);
if (implem == null) {
return new Pair<>(r, expr);
} else {
// Rewrite the function body itself (without setting unbounded variables to dataset access).
// TODO(buyingyi): throw an exception for recursive function definition or limit the stack depth.
implem.setFuncBody(rewriteFunctionBody(implem.getFuncBody()));
// it's one of the functions we want to inline
List<LetClause> clauses = new ArrayList<>();
Iterator<VarIdentifier> paramIter = implem.getParamList().iterator();
VariableSubstitutionEnvironment subts = new VariableSubstitutionEnvironment();
for (Expression e : f.getExprList()) {
VarIdentifier param = paramIter.next();
// variable inlining to take care of this.
if (e.getKind() == Kind.VARIABLE_EXPRESSION) {
subts.addSubstituion(new VariableExpr(param), e);
} else {
VarIdentifier newV = context.newVariable();
Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = e.accept(cloneVisitor, new VariableSubstitutionEnvironment());
LetClause c = new LetClause(new VariableExpr(newV), (Expression) p1.first);
clauses.add(c);
subts.addSubstituion(new VariableExpr(param), new VariableExpr(newV));
}
}
Pair<ILangExpression, VariableSubstitutionEnvironment> p2 = implem.getFuncBody().accept(cloneVisitor, subts);
Expression resExpr;
if (clauses.isEmpty()) {
resExpr = (Expression) p2.first;
} else {
resExpr = generateQueryExpression(clauses, (Expression) p2.first);
}
return new Pair<>(true, resExpr);
}
}
use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.
the class CloneAndSubstituteVariablesVisitor method visit.
@Override
public Pair<ILangExpression, VariableSubstitutionEnvironment> visit(FunctionDecl fd, VariableSubstitutionEnvironment env) throws CompilationException {
List<VarIdentifier> newList = new ArrayList<>(fd.getParamList().size());
for (VarIdentifier vi : fd.getParamList()) {
VariableExpr varExpr = new VariableExpr(vi);
if (!env.constainsOldVar(varExpr)) {
throw new CompilationException("Parameter " + vi + " does not appear in the substitution list.");
}
Expression newExpr = env.findSubstitution(varExpr);
if (newExpr.getKind() != Kind.VARIABLE_EXPRESSION) {
throw new CompilationException("Parameter " + vi + " cannot be substituted by a non-variable expression.");
}
newList.add(((VariableExpr) newExpr).getVar());
}
Pair<ILangExpression, VariableSubstitutionEnvironment> p1 = fd.getFuncBody().accept(this, env);
FunctionDecl newF = new FunctionDecl(fd.getSignature(), newList, (Expression) p1.first);
return new Pair<>(newF, env);
}
use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.
the class LangRewritingContext method mapOldId.
/**
* Generate a new variable with the same identifier (varValue) but a different Id.
*
* @param oldId
* , the old variable id
* @param varValue
* , the identifier
* @return the new varible.
*/
public VarIdentifier mapOldId(Integer oldId, String varValue) {
int n = newId();
VarIdentifier newVar = new VarIdentifier(varValue);
newVar.setId(n);
oldVarIdToNewVarId.put(oldId, newVar);
return newVar;
}
use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.
the class SqlppGroupByVisitor method visit.
@Override
public Expression visit(GroupbyClause gc, ILangExpression arg) throws CompilationException {
// Puts all FROM binding variables into withVarList.
FromClause fromClause = (FromClause) arg;
Collection<VariableExpr> fromBindingVars = fromClause == null ? new ArrayList<>() : SqlppVariableUtil.getBindingVariables(fromClause);
Map<Expression, VariableExpr> withVarMap = new HashMap<>();
for (VariableExpr fromBindingVar : fromBindingVars) {
VariableExpr varExpr = new VariableExpr();
varExpr.setIsNewVar(false);
varExpr.setVar(fromBindingVar.getVar());
VariableExpr newVarExpr = (VariableExpr) SqlppRewriteUtil.deepCopy(varExpr);
withVarMap.put(varExpr, newVarExpr);
}
// Sets the field list for the group variable.
List<Pair<Expression, Identifier>> groupFieldList = new ArrayList<>();
if (!gc.hasGroupFieldList()) {
for (VariableExpr varExpr : fromBindingVars) {
Pair<Expression, Identifier> varIdPair = new Pair<>(new VariableExpr(varExpr.getVar()), SqlppVariableUtil.toUserDefinedVariableName(varExpr.getVar()));
groupFieldList.add(varIdPair);
}
gc.setGroupFieldList(groupFieldList);
} else {
for (Pair<Expression, Identifier> groupField : gc.getGroupFieldList()) {
Expression newFieldExpr = groupField.first.accept(this, arg);
groupFieldList.add(new Pair<>(newFieldExpr, groupField.second));
// Adds a field binding variable into withVarList.
VariableExpr bindingVar = new VariableExpr(new VarIdentifier(SqlppVariableUtil.toInternalVariableName(groupField.second.getValue())));
withVarMap.put(newFieldExpr, bindingVar);
}
}
gc.setGroupFieldList(groupFieldList);
// Sets the group variable.
if (!gc.hasGroupVar()) {
VariableExpr groupVar = new VariableExpr(context.newVariable());
gc.setGroupVar(groupVar);
}
// Adds the group variable into the "with" (i.e., re-binding) variable list.
VariableExpr gbyVarRef = new VariableExpr(gc.getGroupVar().getVar());
gbyVarRef.setIsNewVar(false);
withVarMap.put(gbyVarRef, (VariableExpr) SqlppRewriteUtil.deepCopy(gbyVarRef));
gc.setWithVarMap(withVarMap);
// Call super.visit(...) to scope variables.
return super.visit(gc, arg);
}
Aggregations