Search in sources :

Example 6 with VarIdentifier

use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.

the class InlineColumnAliasVisitor method mapRecordConstructor.

private Map<Expression, Expression> mapRecordConstructor(RecordConstructor rc) {
    Map<Expression, Expression> exprMap = new HashMap<>();
    for (FieldBinding binding : rc.getFbList()) {
        Expression leftExpr = binding.getLeftExpr();
        // (e.g., foo.name AS name) in regular SQL SELECT.
        if (leftExpr.getKind() != Kind.LITERAL_EXPRESSION) {
            continue;
        }
        LiteralExpr literalExpr = (LiteralExpr) leftExpr;
        if (literalExpr.getValue().getLiteralType() == Literal.Type.STRING) {
            String fieldName = SqlppVariableUtil.toInternalVariableName(literalExpr.getValue().getStringValue());
            exprMap.put(new VariableExpr(new VarIdentifier(fieldName)), binding.getRightExpr());
        }
    }
    return exprMap;
}
Also used : HashMap(java.util.HashMap) ILangExpression(org.apache.asterix.lang.common.base.ILangExpression) Expression(org.apache.asterix.lang.common.base.Expression) SelectExpression(org.apache.asterix.lang.sqlpp.expression.SelectExpression) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) FieldBinding(org.apache.asterix.lang.common.expression.FieldBinding) LiteralExpr(org.apache.asterix.lang.common.expression.LiteralExpr) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 7 with VarIdentifier

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();
    StringBuilder builder = new StringBuilder();
    builder.append(" use " + function.getDataverseName() + ";");
    builder.append(" declare function " + function.getName().split("@")[0]);
    builder.append("(");
    for (String param : params) {
        VarIdentifier varId = SqlppVariableUtil.toUserDefinedVariableName(param);
        builder.append(varId);
        builder.append(",");
    }
    if (params.size() > 0) {
        builder.delete(builder.length() - 1, builder.length());
    }
    builder.append(")");
    builder.append("{");
    builder.append("\n");
    builder.append(functionBody);
    builder.append("\n");
    builder.append("}");
    IParser parser = parserFactory.createParser(new StringReader(new String(builder)));
    List<Statement> statements = parser.parse();
    FunctionDecl decl = (FunctionDecl) statements.get(1);
    return decl;
}
Also used : VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) Statement(org.apache.asterix.lang.common.base.Statement) StringReader(java.io.StringReader) IParser(org.apache.asterix.lang.common.base.IParser) FunctionDecl(org.apache.asterix.lang.common.statement.FunctionDecl)

Example 8 with VarIdentifier

use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.

the class CloneAndSubstituteVariablesVisitor method generateNewVariable.

/**
     * Generates a new variable for an existing variable.
     *
     * @param context
     *            , the language rewriting context which keeps all the rewriting variable-int-id to variable-string-identifier mappings.
     * @param varExpr
     *            , the existing variable expression.
     * @return the new variable expression.
     */
public VariableExpr generateNewVariable(LangRewritingContext context, VariableExpr varExpr) {
    VarIdentifier vi = varExpr.getVar();
    VarIdentifier newVar = context.mapOldId(vi.getId(), vi.getValue());
    return new VariableExpr(newVar);
}
Also used : VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr)

Example 9 with VarIdentifier

use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.

the class Scope method getLiveVariables.

public Set<VariableExpr> getLiveVariables() {
    Set<VariableExpr> vars = new HashSet<VariableExpr>();
    Iterator<Identifier> identifierIterator = liveSymbols();
    while (identifierIterator.hasNext()) {
        Identifier identifier = identifierIterator.next();
        if (identifier instanceof VarIdentifier) {
            vars.add(new VariableExpr((VarIdentifier) identifier));
        }
    }
    return vars;
}
Also used : VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) Identifier(org.apache.asterix.lang.common.struct.Identifier) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) HashSet(java.util.HashSet)

Example 10 with VarIdentifier

use of org.apache.asterix.lang.common.struct.VarIdentifier in project asterixdb by apache.

the class ClauseComparator method extractDefinedCollectionVariables.

// Extracts the variables to be substituted with a path access.
private Map<VariableExpr, Expression> extractDefinedCollectionVariables(List<Clause> clauses, GroupbyClause cuttingGbyClause, String generatedAlias) {
    Map<VariableExpr, Expression> varExprMap = new HashMap<VariableExpr, Expression>();
    List<VariableExpr> varToSubstitute = collectProducedVariablesFromGroupby(cuttingGbyClause);
    int gbyIndex = clauses.indexOf(cuttingGbyClause);
    for (int i = gbyIndex + 1; i < clauses.size(); i++) {
        Clause cl = clauses.get(i);
        if (cl.getClauseType() == ClauseType.LET_CLAUSE) {
            varToSubstitute.add(((LetClause) cl).getVarExpr());
        }
    }
    for (VariableExpr var : varToSubstitute) {
        varExprMap.put(var, new FieldAccessor(new VariableExpr(new VarIdentifier(generatedAlias)), new VarIdentifier(var.getVar().getValue().substring(1))));
    }
    return varExprMap;
}
Also used : HashMap(java.util.HashMap) FLWOGRExpression(org.apache.asterix.lang.aql.expression.FLWOGRExpression) Expression(org.apache.asterix.lang.common.base.Expression) VarIdentifier(org.apache.asterix.lang.common.struct.VarIdentifier) VariableExpr(org.apache.asterix.lang.common.expression.VariableExpr) ForClause(org.apache.asterix.lang.aql.clause.ForClause) DistinctClause(org.apache.asterix.lang.aql.clause.DistinctClause) GroupbyClause(org.apache.asterix.lang.common.clause.GroupbyClause) LetClause(org.apache.asterix.lang.common.clause.LetClause) Clause(org.apache.asterix.lang.common.base.Clause) WhereClause(org.apache.asterix.lang.common.clause.WhereClause) FieldAccessor(org.apache.asterix.lang.common.expression.FieldAccessor)

Aggregations

VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)15 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)10 Expression (org.apache.asterix.lang.common.base.Expression)6 ArrayList (java.util.ArrayList)5 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)4 FunctionDecl (org.apache.asterix.lang.common.statement.FunctionDecl)4 Identifier (org.apache.asterix.lang.common.struct.Identifier)4 HashMap (java.util.HashMap)3 LetClause (org.apache.asterix.lang.common.clause.LetClause)3 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)3 Pair (org.apache.hyracks.algebricks.common.utils.Pair)3 CompilationException (org.apache.asterix.common.exceptions.CompilationException)2 DistinctClause (org.apache.asterix.lang.aql.clause.DistinctClause)2 ForClause (org.apache.asterix.lang.aql.clause.ForClause)2 FLWOGRExpression (org.apache.asterix.lang.aql.expression.FLWOGRExpression)2 Clause (org.apache.asterix.lang.common.base.Clause)2 IParser (org.apache.asterix.lang.common.base.IParser)2 Statement (org.apache.asterix.lang.common.base.Statement)2 GroupbyClause (org.apache.asterix.lang.common.clause.GroupbyClause)2 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)2