use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.
the class SetOperationVisitor method visit.
@Override
public Expression visit(SelectExpression selectExpression, ILangExpression arg) throws CompilationException {
// Recursively visit nested select expressions.
SelectSetOperation selectSetOperation = selectExpression.getSelectSetOperation();
if (!selectSetOperation.hasRightInputs() || !(selectExpression.hasOrderby() || selectExpression.hasLimit())) {
return super.visit(selectExpression, arg);
}
OrderbyClause orderBy = selectExpression.getOrderbyClause();
LimitClause limit = selectExpression.getLimitClause();
// Wraps the set operation part with a subquery.
SelectExpression nestedSelectExpression = new SelectExpression(null, selectSetOperation, null, null, true);
// Binding variable for the subquery.
VariableExpr newBindingVar = new VariableExpr(context.newVariable());
FromTerm newFromTerm = new FromTerm(nestedSelectExpression, newBindingVar, null, null);
FromClause newFromClause = new FromClause(new ArrayList<>(Collections.singletonList(newFromTerm)));
SelectClause selectClause = new SelectClause(new SelectElement(newBindingVar), null, false);
SelectBlock selectBlock = new SelectBlock(selectClause, newFromClause, null, null, null, null, null);
SelectSetOperation newSelectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
// Puts together the generated select-from-where query and order by/limit.
SelectExpression newSelectExpression = new SelectExpression(selectExpression.getLetList(), newSelectSetOperation, orderBy, limit, selectExpression.isSubquery());
return super.visit(newSelectExpression, arg);
}
use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.
the class SqlppRewriteUtil method getFreeVariable.
public static Set<VariableExpr> getFreeVariable(Expression expr) throws CompilationException {
Set<VariableExpr> vars = new HashSet<>();
FreeVariableVisitor visitor = new FreeVariableVisitor();
expr.accept(visitor, vars);
return vars;
}
use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.
the class SqlppVariableUtil method getBindingVariables.
public static Collection<VariableExpr> getBindingVariables(GroupbyClause gbyClause) {
List<VariableExpr> bindingVars = new ArrayList<>();
if (gbyClause == null) {
return bindingVars;
}
for (GbyVariableExpressionPair gbyKey : gbyClause.getGbyPairList()) {
VariableExpr var = gbyKey.getVar();
if (var != null) {
bindingVars.add(var);
}
}
for (GbyVariableExpressionPair gbyKey : gbyClause.getDecorPairList()) {
VariableExpr var = gbyKey.getVar();
if (var != null) {
bindingVars.add(var);
}
}
if (gbyClause.hasWithMap()) {
bindingVars.addAll(gbyClause.getWithVarMap().values());
}
bindingVars.add(gbyClause.getGroupVar());
return bindingVars;
}
use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.
the class QueryPrintVisitor method visit.
@Override
public Void visit(GroupbyClause gc, Integer step) throws CompilationException {
out.println(skip(step) + "Groupby");
for (GbyVariableExpressionPair pair : gc.getGbyPairList()) {
if (pair.getVar() != null) {
pair.getVar().accept(this, step + 1);
out.println(skip(step + 1) + ":=");
}
pair.getExpr().accept(this, step + 1);
}
if (gc.hasDecorList()) {
out.println(skip(step + 1) + "Decor");
for (GbyVariableExpressionPair pair : gc.getDecorPairList()) {
if (pair.getVar() != null) {
pair.getVar().accept(this, step + 1);
out.println(skip(step + 1) + ":=");
}
pair.getExpr().accept(this, step + 1);
}
}
if (gc.hasWithMap()) {
out.println(skip(step + 1) + "With");
for (Entry<Expression, VariableExpr> entry : gc.getWithVarMap().entrySet()) {
Expression key = entry.getKey();
VariableExpr value = entry.getValue();
key.accept(this, step + 1);
if (!key.equals(value)) {
out.println(skip(step + 1) + "AS");
value.accept(this, step + 1);
}
}
}
out.println();
return null;
}
use of org.apache.asterix.lang.common.expression.VariableExpr in project asterixdb by apache.
the class LangExpressionToPlanTranslator method visit.
@Override
public Pair<ILogicalOperator, LogicalVariable> visit(CallExpr fcall, Mutable<ILogicalOperator> tupSource) throws CompilationException {
LogicalVariable v = context.newVar();
FunctionSignature signature = fcall.getFunctionSignature();
List<Mutable<ILogicalExpression>> args = new ArrayList<>();
Mutable<ILogicalOperator> topOp = tupSource;
for (Expression expr : fcall.getExprList()) {
switch(expr.getKind()) {
case VARIABLE_EXPRESSION:
LogicalVariable var = context.getVar(((VariableExpr) expr).getVar().getId());
args.add(new MutableObject<>(new VariableReferenceExpression(var)));
break;
case LITERAL_EXPRESSION:
LiteralExpr val = (LiteralExpr) expr;
args.add(new MutableObject<>(new ConstantExpression(new AsterixConstantValue(ConstantHelper.objectFromLiteral(val.getValue())))));
break;
default:
Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(expr, topOp);
AbstractLogicalOperator o1 = (AbstractLogicalOperator) eo.second.getValue();
args.add(new MutableObject<>(eo.first));
if (o1 != null && !(o1.getOperatorTag() == LogicalOperatorTag.ASSIGN && hasOnlyChild(o1, topOp))) {
topOp = eo.second;
}
break;
}
}
AbstractFunctionCallExpression f;
if ((f = lookupUserDefinedFunction(signature, args)) == null) {
f = lookupBuiltinFunction(signature.getName(), signature.getArity(), args);
}
if (f == null) {
throw new CompilationException(" Unknown function " + signature.getName() + "@" + signature.getArity());
}
// Put hints into function call expr.
if (fcall.hasHints()) {
for (IExpressionAnnotation hint : fcall.getHints()) {
f.getAnnotations().put(hint, hint);
}
}
AssignOperator op = new AssignOperator(v, new MutableObject<>(f));
if (topOp != null) {
op.getInputs().add(topOp);
}
return new Pair<>(op, v);
}
Aggregations