Search in sources :

Example 6 with LiteralExpr

use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.

the class SqlppExpressionToPlanTranslator method generateReturnExpr.

// Generates the return expression for a select clause.
private Expression generateReturnExpr(SelectClause selectClause, SelectBlock selectBlock) {
    SelectRegular selectRegular = selectClause.getSelectRegular();
    List<FieldBinding> fieldBindings = new ArrayList<>();
    List<Projection> projections = selectRegular.getProjections();
    for (Projection projection : projections) {
        if (projection.star()) {
            if (selectBlock.hasGroupbyClause()) {
                fieldBindings.addAll(getGroupBindings(selectBlock.getGroupbyClause()));
            } else if (selectBlock.hasFromClause()) {
                fieldBindings.addAll(getFromBindings(selectBlock.getFromClause()));
            }
        } else {
            fieldBindings.add(new FieldBinding(new LiteralExpr(new StringLiteral(projection.getName())), projection.getExpression()));
        }
    }
    return new RecordConstructor(fieldBindings);
}
Also used : SelectRegular(org.apache.asterix.lang.sqlpp.clause.SelectRegular) StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) ArrayList(java.util.ArrayList) FieldBinding(org.apache.asterix.lang.common.expression.FieldBinding) Projection(org.apache.asterix.lang.sqlpp.clause.Projection) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) RecordConstructor(org.apache.asterix.lang.common.expression.RecordConstructor)

Example 7 with LiteralExpr

use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.

the class LangExpressionToPlanTranslator method visit.

@Override
public Pair<ILogicalOperator, LogicalVariable> visit(GroupbyClause gc, Mutable<ILogicalOperator> tupSource) throws CompilationException {
    Mutable<ILogicalOperator> topOp = tupSource;
    if (gc.hasGroupVar()) {
        List<Pair<Expression, Identifier>> groupFieldList = gc.getGroupFieldList();
        List<Mutable<ILogicalExpression>> groupRecordConstructorArgList = new ArrayList<>();
        for (Pair<Expression, Identifier> groupField : groupFieldList) {
            ILogicalExpression groupFieldNameExpr = langExprToAlgExpression(new LiteralExpr(new StringLiteral(groupField.second.getValue())), topOp).first;
            groupRecordConstructorArgList.add(new MutableObject<>(groupFieldNameExpr));
            ILogicalExpression groupFieldExpr = langExprToAlgExpression(groupField.first, topOp).first;
            groupRecordConstructorArgList.add(new MutableObject<>(groupFieldExpr));
        }
        LogicalVariable groupVar = context.newVarFromExpression(gc.getGroupVar());
        AssignOperator groupVarAssignOp = new AssignOperator(groupVar, new MutableObject<>(new ScalarFunctionCallExpression(FunctionUtil.getFunctionInfo(BuiltinFunctions.OPEN_RECORD_CONSTRUCTOR), groupRecordConstructorArgList)));
        groupVarAssignOp.getInputs().add(topOp);
        topOp = new MutableObject<>(groupVarAssignOp);
    }
    GroupByOperator gOp = new GroupByOperator();
    for (GbyVariableExpressionPair ve : gc.getGbyPairList()) {
        VariableExpr vexpr = ve.getVar();
        LogicalVariable v = vexpr == null ? context.newVar() : context.newVarFromExpression(vexpr);
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(ve.getExpr(), topOp);
        gOp.addGbyExpression(v, eo.first);
        topOp = eo.second;
    }
    for (GbyVariableExpressionPair ve : gc.getDecorPairList()) {
        VariableExpr vexpr = ve.getVar();
        LogicalVariable v = vexpr == null ? context.newVar() : context.newVarFromExpression(vexpr);
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> eo = langExprToAlgExpression(ve.getExpr(), topOp);
        gOp.addDecorExpression(v, eo.first);
        topOp = eo.second;
    }
    gOp.getInputs().add(topOp);
    for (Entry<Expression, VariableExpr> entry : gc.getWithVarMap().entrySet()) {
        Pair<ILogicalExpression, Mutable<ILogicalOperator>> listifyInput = langExprToAlgExpression(entry.getKey(), new MutableObject<>(new NestedTupleSourceOperator(new MutableObject<>(gOp))));
        List<Mutable<ILogicalExpression>> flArgs = new ArrayList<>(1);
        flArgs.add(new MutableObject<>(listifyInput.first));
        AggregateFunctionCallExpression fListify = BuiltinFunctions.makeAggregateFunctionExpression(BuiltinFunctions.LISTIFY, flArgs);
        LogicalVariable aggVar = context.newVar();
        AggregateOperator agg = new AggregateOperator(mkSingletonArrayList(aggVar), mkSingletonArrayList(new MutableObject<>(fListify)));
        agg.getInputs().add(listifyInput.second);
        ILogicalPlan plan = new ALogicalPlanImpl(new MutableObject<>(agg));
        gOp.getNestedPlans().add(plan);
        // Hide the variable that was part of the "with", replacing it with
        // the one bound by the aggregation op.
        context.setVar(entry.getValue(), aggVar);
    }
    gOp.setGroupAll(gc.isGroupAll());
    gOp.getAnnotations().put(OperatorAnnotations.USE_HASH_GROUP_BY, gc.hasHashGroupByHint());
    return new Pair<>(gOp, null);
}
Also used : NestedTupleSourceOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.NestedTupleSourceOperator) ArrayList(java.util.ArrayList) Identifier(org.apache.asterix.lang.common.struct.Identifier) FunctionIdentifier(org.apache.hyracks.algebricks.core.algebra.functions.FunctionIdentifier) ALogicalPlanImpl(org.apache.hyracks.algebricks.core.algebra.plan.ALogicalPlanImpl) AggregateOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AggregateOperator) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) Pair(org.apache.hyracks.algebricks.common.utils.Pair) QuantifiedPair(org.apache.asterix.lang.common.struct.QuantifiedPair) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) MutableObject(org.apache.commons.lang3.mutable.MutableObject) LogicalVariable(org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) GroupByOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.GroupByOperator) ILogicalOperator(org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator) AssignOperator(org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator) Mutable(org.apache.commons.lang3.mutable.Mutable) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) AggregateFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AggregateFunctionCallExpression) Expression(org.apache.asterix.lang.common.base.Expression) VariableReferenceExpression(org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) ScalarFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ScalarFunctionCallExpression) ConstantExpression(org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression) ILogicalExpression(org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression) AbstractFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.AbstractFunctionCallExpression) UnnestingFunctionCallExpression(org.apache.hyracks.algebricks.core.algebra.expressions.UnnestingFunctionCallExpression) GbyVariableExpressionPair(org.apache.asterix.lang.common.expression.GbyVariableExpressionPair) ILogicalPlan(org.apache.hyracks.algebricks.core.algebra.base.ILogicalPlan) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 8 with LiteralExpr

use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.

the class VariableCheckAndRewriteVisitor method wrapWithDatasetFunction.

private Expression wrapWithDatasetFunction(String dataverseName, String datasetName) throws CompilationException {
    String fullyQualifiedName = dataverseName == null ? datasetName : dataverseName + "." + datasetName;
    List<Expression> argList = new ArrayList<>();
    argList.add(new LiteralExpr(new StringLiteral(fullyQualifiedName)));
    return new CallExpr(datasetFunction, argList);
}
Also used : StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) Expression(org.apache.asterix.lang.common.base.Expression) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) ArrayList(java.util.ArrayList) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) CallExpr(org.apache.asterix.lang.common.expression.CallExpr)

Example 9 with LiteralExpr

use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.

the class SqlppBuiltinFunctionRewriteVisitor method normalizeCaseExpr.

// Normalizes WHEN expressions so that it can have correct NULL/MISSING semantics as well
// as type promotion semantics.
private CaseExpression normalizeCaseExpr(CaseExpression caseExpr) throws CompilationException {
    LiteralExpr trueLiteral = new LiteralExpr(TrueLiteral.INSTANCE);
    Expression conditionExpr = caseExpr.getConditionExpr();
    if (trueLiteral.equals(conditionExpr)) {
        return caseExpr;
    }
    List<Expression> normalizedWhenExprs = new ArrayList<>();
    for (Expression expr : caseExpr.getWhenExprs()) {
        OperatorExpr operatorExpr = new OperatorExpr();
        operatorExpr.addOperand((Expression) SqlppRewriteUtil.deepCopy(expr));
        operatorExpr.addOperand(caseExpr.getConditionExpr());
        operatorExpr.addOperator("=");
        normalizedWhenExprs.add(operatorExpr);
    }
    return new CaseExpression(trueLiteral, normalizedWhenExprs, caseExpr.getThenExprs(), caseExpr.getElseExpr());
}
Also used : OperatorExpr(org.apache.asterix.lang.common.expression.OperatorExpr) 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) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) CaseExpression(org.apache.asterix.lang.sqlpp.expression.CaseExpression)

Example 10 with LiteralExpr

use of org.apache.asterix.lang.common.expression.LiteralExpr in project asterixdb by apache.

the class AbstractSqlppExpressionScopingVisitor method wrapWithResolveFunction.

// Rewrites for an undefined variable reference, which potentially could be a syntatic sugar.
protected Expression wrapWithResolveFunction(VariableExpr expr, Set<VariableExpr> liveVars) throws CompilationException {
    List<Expression> argList = new ArrayList<>();
    //Ignore the parser-generated prefix "$" for a dataset.
    String varName = SqlppVariableUtil.toUserDefinedVariableName(expr.getVar().getValue()).getValue();
    argList.add(new LiteralExpr(new StringLiteral(varName)));
    argList.addAll(liveVars);
    return new CallExpr(resolveFunction, argList);
}
Also used : StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) QuantifiedExpression(org.apache.asterix.lang.common.expression.QuantifiedExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) ArrayList(java.util.ArrayList) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) CallExpr(org.apache.asterix.lang.common.expression.CallExpr)

Aggregations

LiteralExpr (org.apache.asterix.lang.common.expression.LiteralExpr)13 ArrayList (java.util.ArrayList)8 Expression (org.apache.asterix.lang.common.base.Expression)8 StringLiteral (org.apache.asterix.lang.common.literal.StringLiteral)7 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)6 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)5 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)4 CallExpr (org.apache.asterix.lang.common.expression.CallExpr)4 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)3 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)3 Identifier (org.apache.asterix.lang.common.struct.Identifier)3 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)3 Pair (org.apache.hyracks.algebricks.common.utils.Pair)3 ILogicalExpression (org.apache.hyracks.algebricks.core.algebra.base.ILogicalExpression)3 ILogicalOperator (org.apache.hyracks.algebricks.core.algebra.base.ILogicalOperator)3 LogicalVariable (org.apache.hyracks.algebricks.core.algebra.base.LogicalVariable)3 ConstantExpression (org.apache.hyracks.algebricks.core.algebra.expressions.ConstantExpression)3 VariableReferenceExpression (org.apache.hyracks.algebricks.core.algebra.expressions.VariableReferenceExpression)3 AssignOperator (org.apache.hyracks.algebricks.core.algebra.operators.logical.AssignOperator)3 CompilationException (org.apache.asterix.common.exceptions.CompilationException)2