Search in sources :

Example 1 with StringLiteral

use of org.apache.asterix.lang.common.literal.StringLiteral in project asterixdb by apache.

the class SqlppDeleteRewriteVisitor 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);
    // From clause.
    VariableExpr var = deleteStmt.getVariableExpr();
    FromTerm fromTerm = new FromTerm(callExpression, var, null, null);
    @SuppressWarnings("unchecked") FromClause fromClause = new FromClause(Collections.singletonList(fromTerm));
    // Where clause.
    WhereClause whereClause = null;
    Expression condition = deleteStmt.getCondition();
    if (condition != null) {
        whereClause = new WhereClause(condition);
    }
    // Select clause.
    VariableExpr returnExpr = new VariableExpr(var.getVar());
    returnExpr.setIsNewVar(false);
    SelectElement selectElement = new SelectElement(returnExpr);
    SelectClause selectClause = new SelectClause(selectElement, null, false);
    // Construct the select expression.
    SelectBlock selectBlock = new SelectBlock(selectClause, fromClause, null, whereClause, null, null, null);
    SelectSetOperation selectSetOperation = new SelectSetOperation(new SetOperationInput(selectBlock, null), null);
    SelectExpression selectExpression = new SelectExpression(null, selectSetOperation, null, null, false);
    Query query = new Query(false, false, selectExpression, 0);
    query.setBody(selectExpression);
    // return the delete statement.
    deleteStmt.setQuery(query);
    return null;
}
Also used : SelectElement(org.apache.asterix.lang.sqlpp.clause.SelectElement) SelectClause(org.apache.asterix.lang.sqlpp.clause.SelectClause) Query(org.apache.asterix.lang.common.statement.Query) ArrayList(java.util.ArrayList) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) Identifier(org.apache.asterix.lang.common.struct.Identifier) StringLiteral(org.apache.asterix.lang.common.literal.StringLiteral) SelectBlock(org.apache.asterix.lang.sqlpp.clause.SelectBlock) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) FromClause(org.apache.asterix.lang.sqlpp.clause.FromClause) SetOperationInput(org.apache.asterix.lang.sqlpp.struct.SetOperationInput) SelectSetOperation(org.apache.asterix.lang.sqlpp.clause.SelectSetOperation) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) CallExpr(org.apache.asterix.lang.common.expression.CallExpr) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) FromTerm(org.apache.asterix.lang.sqlpp.clause.FromTerm)

Example 2 with StringLiteral

use of org.apache.asterix.lang.common.literal.StringLiteral 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 3 with StringLiteral

use of org.apache.asterix.lang.common.literal.StringLiteral 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 4 with StringLiteral

use of org.apache.asterix.lang.common.literal.StringLiteral 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 5 with StringLiteral

use of org.apache.asterix.lang.common.literal.StringLiteral 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)7 StringLiteral (org.apache.asterix.lang.common.literal.StringLiteral)7 ArrayList (java.util.ArrayList)6 Expression (org.apache.asterix.lang.common.base.Expression)5 CallExpr (org.apache.asterix.lang.common.expression.CallExpr)4 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)3 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)3 Identifier (org.apache.asterix.lang.common.struct.Identifier)3 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)2 WhereClause (org.apache.asterix.lang.common.clause.WhereClause)2 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)2 Query (org.apache.asterix.lang.common.statement.Query)2 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)2 CompilationException (org.apache.asterix.common.exceptions.CompilationException)1 ForClause (org.apache.asterix.lang.aql.clause.ForClause)1 FLWOGRExpression (org.apache.asterix.lang.aql.expression.FLWOGRExpression)1 Clause (org.apache.asterix.lang.common.base.Clause)1 Literal (org.apache.asterix.lang.common.base.Literal)1 FieldBinding (org.apache.asterix.lang.common.expression.FieldBinding)1 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)1