Search in sources :

Example 6 with FieldBinding

use of org.apache.asterix.lang.common.expression.FieldBinding 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 FieldBinding

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

the class QueryPrintVisitor method visit.

@Override
public Void visit(RecordConstructor rc, Integer step) throws CompilationException {
    out.println(skip(step) + "RecordConstructor [");
    // fbList accept visitor
    for (FieldBinding fb : rc.getFbList()) {
        out.println(skip(step + 1) + "(");
        fb.getLeftExpr().accept(this, step + 2);
        out.println(skip(step + 2) + ":");
        fb.getRightExpr().accept(this, step + 2);
        out.println(skip(step + 1) + ")");
    }
    out.println(skip(step) + "]");
    return null;
}
Also used : FieldBinding(org.apache.asterix.lang.common.expression.FieldBinding)

Example 8 with FieldBinding

use of org.apache.asterix.lang.common.expression.FieldBinding 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 9 with FieldBinding

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

the class LangRecordParseUtil method parseRecord.

public static void parseRecord(RecordConstructor recordValue, ArrayBackedValueStorage serialized, boolean tagged, List<Pair<String, String>> defaults) throws HyracksDataException {
    AMutableString fieldNameString = new AMutableString(null);
    ArrayBackedValueStorage fieldName = new ArrayBackedValueStorage();
    ArrayBackedValueStorage fieldValue = new ArrayBackedValueStorage();
    RecordBuilder recordBuilder = new RecordBuilder();
    recordBuilder.reset(RecordUtil.FULLY_OPEN_RECORD_TYPE);
    recordBuilder.init();
    List<FieldBinding> fbList = recordValue.getFbList();
    HashSet<String> fieldNames = new HashSet<>();
    for (FieldBinding fb : fbList) {
        fieldName.reset();
        fieldValue.reset();
        // get key
        fieldNameString.setValue(exprToStringLiteral(fb.getLeftExpr()).getStringValue());
        if (!fieldNames.add(fieldNameString.getStringValue())) {
            throw new HyracksDataException("Field " + fieldNameString.getStringValue() + " was specified multiple times");
        }
        stringSerde.serialize(fieldNameString, fieldName.getDataOutput());
        // get value
        parseExpression(fb.getRightExpr(), fieldValue);
        recordBuilder.addField(fieldName, fieldValue);
    }
    // defaults
    for (Pair<String, String> kv : defaults) {
        if (!fieldNames.contains(kv.first)) {
            fieldName.reset();
            fieldValue.reset();
            stringSerde.serialize(new AString(kv.first), fieldName.getDataOutput());
            stringSerde.serialize(new AString(kv.second), fieldValue.getDataOutput());
            recordBuilder.addField(fieldName, fieldValue);
        }
    }
    recordBuilder.write(serialized.getDataOutput(), tagged);
}
Also used : ArrayBackedValueStorage(org.apache.hyracks.data.std.util.ArrayBackedValueStorage) RecordBuilder(org.apache.asterix.builders.RecordBuilder) FieldBinding(org.apache.asterix.lang.common.expression.FieldBinding) AMutableString(org.apache.asterix.om.base.AMutableString) AString(org.apache.asterix.om.base.AString) AMutableString(org.apache.asterix.om.base.AMutableString) AString(org.apache.asterix.om.base.AString) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) HashSet(java.util.HashSet)

Example 10 with FieldBinding

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

the class FormatPrintVisitor method visit.

@Override
public Void visit(RecordConstructor rc, Integer step) throws CompilationException {
    out.print("{");
    // print all field bindings
    int size = rc.getFbList().size();
    int index = 0;
    for (FieldBinding fb : rc.getFbList()) {
        fb.getLeftExpr().accept(this, step + 2);
        out.print(":");
        fb.getRightExpr().accept(this, step + 2);
        if (++index < size) {
            out.print(COMMA);
        }
    }
    out.print("}");
    return null;
}
Also used : FieldBinding(org.apache.asterix.lang.common.expression.FieldBinding)

Aggregations

FieldBinding (org.apache.asterix.lang.common.expression.FieldBinding)14 ILangExpression (org.apache.asterix.lang.common.base.ILangExpression)4 ArrayList (java.util.ArrayList)3 RecordConstructor (org.apache.asterix.lang.common.expression.RecordConstructor)3 Expression (org.apache.asterix.lang.common.base.Expression)2 GbyVariableExpressionPair (org.apache.asterix.lang.common.expression.GbyVariableExpressionPair)2 LiteralExpr (org.apache.asterix.lang.common.expression.LiteralExpr)2 QuantifiedPair (org.apache.asterix.lang.common.struct.QuantifiedPair)2 Pair (org.apache.hyracks.algebricks.common.utils.Pair)2 HashMap (java.util.HashMap)1 HashSet (java.util.HashSet)1 RecordBuilder (org.apache.asterix.builders.RecordBuilder)1 QuantifiedExpression (org.apache.asterix.lang.common.expression.QuantifiedExpression)1 VariableExpr (org.apache.asterix.lang.common.expression.VariableExpr)1 StringLiteral (org.apache.asterix.lang.common.literal.StringLiteral)1 VariableSubstitutionEnvironment (org.apache.asterix.lang.common.rewrites.VariableSubstitutionEnvironment)1 VarIdentifier (org.apache.asterix.lang.common.struct.VarIdentifier)1 Projection (org.apache.asterix.lang.sqlpp.clause.Projection)1 SelectRegular (org.apache.asterix.lang.sqlpp.clause.SelectRegular)1 SelectExpression (org.apache.asterix.lang.sqlpp.expression.SelectExpression)1