Search in sources :

Example 51 with ILangExpression

use of org.apache.asterix.lang.common.base.ILangExpression in project asterixdb by apache.

the class VariableCheckAndRewriteVisitor method visit.

@Override
public Expression visit(FieldAccessor fa, ILangExpression parent) throws CompilationException {
    Expression leadingExpr = fa.getExpr();
    if (leadingExpr.getKind() != Kind.VARIABLE_EXPRESSION) {
        fa.setExpr(leadingExpr.accept(this, fa));
        return fa;
    } else {
        VariableExpr varExpr = (VariableExpr) leadingExpr;
        String lastIdentifier = fa.getIdent().getValue();
        Expression resolvedExpr = resolve(varExpr, /** Resolves within the dataverse that has the same name as the variable name. */
        SqlppVariableUtil.toUserDefinedVariableName(varExpr.getVar().getValue()).getValue(), lastIdentifier, fa, parent);
        if (resolvedExpr.getKind() == Kind.CALL_EXPRESSION) {
            CallExpr callExpr = (CallExpr) resolvedExpr;
            if (callExpr.getFunctionSignature().equals(datasetFunction)) {
                // The field access is resolved to be a dataset access in the form of "dataverse.dataset".
                return resolvedExpr;
            }
        }
        fa.setExpr(resolvedExpr);
        return fa;
    }
}
Also used : Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) CallExpr(org.apache.asterix.lang.common.expression.CallExpr)

Example 52 with ILangExpression

use of org.apache.asterix.lang.common.base.ILangExpression in project asterixdb by apache.

the class SqlppGroupBySugarVisitor method visit.

@Override
public Expression visit(CallExpr callExpr, ILangExpression arg) throws CompilationException {
    List<Expression> newExprList = new ArrayList<>();
    FunctionSignature signature = callExpr.getFunctionSignature();
    boolean aggregate = FunctionMapUtil.isSql92AggregateFunction(signature);
    boolean rewritten = false;
    for (Expression expr : callExpr.getExprList()) {
        Expression newExpr = aggregate ? wrapAggregationArgument(expr) : expr;
        rewritten |= newExpr != expr;
        newExprList.add(newExpr.accept(this, arg));
    }
    if (rewritten) {
        // Rewrites the SQL-92 function name to core functions,
        // e.g., SUM --> coll_sum
        callExpr.setFunctionSignature(FunctionMapUtil.sql92ToCoreAggregateFunction(signature));
    }
    callExpr.setExprList(newExprList);
    return callExpr;
}
Also used : ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) ArrayList(java.util.ArrayList) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Example 53 with ILangExpression

use of org.apache.asterix.lang.common.base.ILangExpression in project asterixdb by apache.

the class DeepCopyVisitor method visit.

@Override
public ILangExpression visit(CaseExpression caseExpr, Void arg) throws CompilationException {
    Expression conditionExpr = (Expression) caseExpr.getConditionExpr().accept(this, arg);
    List<Expression> whenExprList = copyExprList(caseExpr.getWhenExprs(), arg);
    List<Expression> thenExprList = copyExprList(caseExpr.getThenExprs(), arg);
    Expression elseExpr = (Expression) caseExpr.getElseExpr().accept(this, arg);
    return new CaseExpression(conditionExpr, whenExprList, thenExprList, elseExpr);
}
Also used : ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression)

Example 54 with ILangExpression

use of org.apache.asterix.lang.common.base.ILangExpression in project asterixdb by apache.

the class SqlppBuiltinFunctionRewriteVisitor method visit.

@Override
public Expression visit(CaseExpression caseExpr, ILangExpression arg) throws CompilationException {
    // Visits it as usual first.
    Expression expr = super.visit(caseExpr, arg);
    if (expr != caseExpr) {
        return expr.accept(this, arg);
    }
    CaseExpression newCaseExpr = normalizeCaseExpr(caseExpr);
    if (SqlppRewriteUtil.constainsSubquery(newCaseExpr)) {
        // If the CASE expression contains a subquery, we do not rewrite it to a switch-case function call.
        return newCaseExpr;
    }
    // If the CASE expression does not contain a subquery, we rewrite it to a switch-case function call.
    FunctionSignature functionSignature = new FunctionSignature(MetadataConstants.METADATA_DATAVERSE_NAME, "switch-case", FunctionIdentifier.VARARGS);
    List<Expression> whenExprList = newCaseExpr.getWhenExprs();
    List<Expression> thenExprList = newCaseExpr.getThenExprs();
    List<Expression> newExprList = new ArrayList<>();
    newExprList.add(newCaseExpr.getConditionExpr());
    for (int index = 0; index < whenExprList.size(); ++index) {
        newExprList.add(whenExprList.get(index));
        newExprList.add(thenExprList.get(index));
    }
    newExprList.add(newCaseExpr.getElseExpr());
    return new CallExpr(functionSignature, newExprList);
}
Also used : Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) ArrayList(java.util.ArrayList) CallExpr(org.apache.asterix.lang.common.expression.CallExpr) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression)

Example 55 with ILangExpression

use of org.apache.asterix.lang.common.base.ILangExpression in project asterixdb by apache.

the class SqlppBuiltinFunctionRewriteVisitor method visit.

@Override
public Expression visit(CallExpr callExpr, ILangExpression arg) throws CompilationException {
    //TODO(buyingyi): rewrite SQL temporal functions
    FunctionSignature functionSignature = callExpr.getFunctionSignature();
    callExpr.setFunctionSignature(FunctionMapUtil.normalizeBuiltinFunctionSignature(functionSignature, true));
    List<Expression> newExprList = new ArrayList<>();
    for (Expression expr : callExpr.getExprList()) {
        newExprList.add(expr.accept(this, arg));
    }
    callExpr.setExprList(newExprList);
    return callExpr;
}
Also used : Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression) ArrayList(java.util.ArrayList) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Aggregations

ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)56 Expression (org.apache.asterix.lang.common.base.Expression)34 Pair (org.apache.hyracks.algebricks.common.utils.Pair)34 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)28 ArrayList (java.util.ArrayList)23 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)23 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)18 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)18 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)16 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)15 CaseExpression (org.apache.asterix.lang.sqlpp.expression.CaseExpression)9 LetClause (org.apache.asterix.lang.common.clause.LetClause)8 HashMap (java.util.HashMap)5 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)5 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)4 CallExpr (org.apache.asterix.lang.common.expression.CallExpr)4 FromClause (org.apache.asterix.lang.sqlpp.clause.FromClause)4 ByteArrayOutputStream (java.io.ByteArrayOutputStream)3 PrintWriter (java.io.PrintWriter)3 FLWOGRExpression (org.apache.asterix.lang.aql.expression.FLWOGRExpression)3