Search in sources :

Example 26 with JInvocation

use of com.sun.codemodel.JInvocation in project drill by apache.

the class ClassGenerator method declareVectorValueSetupAndMember.

/**
   * Creates class variable for the value vector using metadata from {@code fieldId}
   * and initializes it using setup blocks.
   *
   * @param batchName expression for invoking {@code getValueAccessorById} method
   * @param fieldId   metadata of the field that should be declared
   * @return a newly generated class field
   */
public JVar declareVectorValueSetupAndMember(DirectExpression batchName, TypedFieldId fieldId) {
    // declares field in the inner class if innerClassGenerator has been created
    if (innerClassGenerator != null) {
        return innerClassGenerator.declareVectorValueSetupAndMember(batchName, fieldId);
    }
    final ValueVectorSetup setup = new ValueVectorSetup(batchName, fieldId);
    //    JVar var = this.vvDeclaration.get(setup);
    //    if(var != null) return var;
    Class<?> valueVectorClass = fieldId.getIntermediateClass();
    JClass vvClass = model.ref(valueVectorClass);
    JClass retClass = vvClass;
    String vectorAccess = "getValueVector";
    if (fieldId.isHyperReader()) {
        retClass = retClass.array();
        vectorAccess = "getValueVectors";
    }
    JVar vv = declareClassField("vv", retClass);
    JClass t = model.ref(SchemaChangeException.class);
    JType objClass = model.ref(Object.class);
    JBlock b = getSetupBlock();
    //JExpr.newArray(model.INT).
    JVar fieldArr = b.decl(model.INT.array(), "fieldIds" + index++, JExpr.newArray(model.INT, fieldId.getFieldIds().length));
    int[] fieldIndices = fieldId.getFieldIds();
    for (int i = 0; i < fieldIndices.length; i++) {
        b.assign(fieldArr.component(JExpr.lit(i)), JExpr.lit(fieldIndices[i]));
    }
    JInvocation invoke = batchName.invoke(//
    "getValueAccessorById").arg(vvClass.dotclass()).arg(fieldArr);
    JVar obj = b.decl(objClass, getNextVar("tmp"), invoke.invoke(vectorAccess));
    b._if(obj.eq(JExpr._null()))._then()._throw(JExpr._new(t).arg(JExpr.lit(String.format("Failure while loading vector %s with id: %s.", vv.name(), fieldId.toString()))));
    //b.assign(vv, JExpr.cast(retClass, ((JExpression) JExpr.cast(wrapperClass, obj)).invoke(vectorAccess)));
    b.assign(vv, JExpr.cast(retClass, obj));
    vvDeclaration.put(setup, vv);
    return vv;
}
Also used : JClass(com.sun.codemodel.JClass) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Example 27 with JInvocation

use of com.sun.codemodel.JInvocation in project drill by apache.

the class ClassGenerator method flushCode.

/**
   * Creates methods from the signature {@code sig} with body from the appropriate {@code blocks}.
   */
void flushCode() {
    JVar innerClassField = null;
    if (innerClassGenerator != null) {
        blocks = oldBlocks;
        innerClassField = clazz.field(JMod.NONE, model.ref(innerClassGenerator.clazz.name()), "innerClassField");
        innerClassGenerator.flushCode();
    }
    int i = 0;
    for (CodeGeneratorMethod method : sig) {
        JMethod outer = clazz.method(JMod.PUBLIC, model._ref(method.getReturnType()), method.getMethodName());
        for (CodeGeneratorArgument arg : method) {
            outer.param(arg.getType(), arg.getName());
        }
        for (Class<?> c : method.getThrowsIterable()) {
            outer._throws(model.ref(c));
        }
        outer._throws(SchemaChangeException.class);
        int methodIndex = 0;
        int exprsInMethod = 0;
        boolean isVoidMethod = method.getReturnType() == void.class;
        for (SizedJBlock sb : blocks[i++]) {
            JBlock b = sb.getBlock();
            if (!b.isEmpty()) {
                if (optionManager != null && exprsInMethod > optionManager.getOption(ExecConstants.CODE_GEN_EXP_IN_METHOD_SIZE_VALIDATOR)) {
                    JMethod inner = clazz.method(JMod.PRIVATE, model._ref(method.getReturnType()), method.getMethodName() + methodIndex);
                    JInvocation methodCall = JExpr.invoke(inner);
                    for (CodeGeneratorArgument arg : method) {
                        inner.param(arg.getType(), arg.getName());
                        methodCall.arg(JExpr.direct(arg.getName()));
                    }
                    for (Class<?> c : method.getThrowsIterable()) {
                        inner._throws(model.ref(c));
                    }
                    inner._throws(SchemaChangeException.class);
                    if (isVoidMethod) {
                        outer.body().add(methodCall);
                    } else {
                        outer.body()._return(methodCall);
                    }
                    outer = inner;
                    exprsInMethod = 0;
                    ++methodIndex;
                }
                outer.body().add(b);
                exprsInMethod += sb.getCount();
            }
        }
        if (innerClassField != null) {
            // creates inner class instance and initializes innerClassField
            if (method.getMethodName().equals("__DRILL_INIT__")) {
                JInvocation rhs = JExpr._new(innerClassGenerator.clazz);
                JBlock block = new JBlock().assign(innerClassField, rhs);
                outer.body().add(block);
            }
            List<JType> argTypes = new ArrayList<>();
            for (CodeGeneratorArgument arg : method) {
                argTypes.add(model._ref(arg.getType()));
            }
            JMethod inner = innerClassGenerator.clazz.getMethod(method.getMethodName(), argTypes.toArray(new JType[0]));
            if (inner != null) {
                // removes empty method from the inner class
                if (inner.body().isEmpty()) {
                    innerClassGenerator.clazz.methods().remove(inner);
                    continue;
                }
                JInvocation methodCall = innerClassField.invoke(inner);
                for (CodeGeneratorArgument arg : method) {
                    methodCall.arg(JExpr.direct(arg.getName()));
                }
                if (isVoidMethod) {
                    outer.body().add(methodCall);
                } else {
                    outer.body()._return(methodCall);
                }
            }
        }
    }
    for (ClassGenerator<T> child : innerClasses.values()) {
        child.flushCode();
    }
}
Also used : ArrayList(java.util.ArrayList) JInvocation(com.sun.codemodel.JInvocation) CodeGeneratorArgument(org.apache.drill.exec.compile.sig.CodeGeneratorArgument) CodeGeneratorMethod(org.apache.drill.exec.compile.sig.CodeGeneratorMethod) JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) JType(com.sun.codemodel.JType) JVar(com.sun.codemodel.JVar)

Example 28 with JInvocation

use of com.sun.codemodel.JInvocation in project drill by apache.

the class GetSetVectorHelper method read.

// eval.add(getValueAccessor.arg(indexVariable).arg(out.getHolder()));
public static void read(MajorType type, JExpression vector, JBlock eval, HoldingContainer out, JCodeModel model, JExpression indexVariable) {
    JInvocation getValueAccessor = vector.invoke("getAccessor");
    switch(type.getMode()) {
        case OPTIONAL:
            eval.assign(out.getIsSet(), getValueAccessor.invoke("isSet").arg(indexVariable));
            eval = eval._if(out.getIsSet().eq(JExpr.lit(1)))._then();
        // fall through
        case REQUIRED:
            switch(type.getMinorType()) {
                case BIGINT:
                case FLOAT4:
                case FLOAT8:
                case INT:
                case MONEY:
                case SMALLINT:
                case TINYINT:
                case UINT1:
                case UINT2:
                case UINT4:
                case UINT8:
                case INTERVALYEAR:
                case DATE:
                case TIME:
                case TIMESTAMP:
                case BIT:
                    eval.assign(out.getValue(), getValueAccessor.invoke("get").arg(indexVariable));
                    return;
                case DECIMAL9:
                case DECIMAL18:
                    eval.assign(out.getHolder().ref("scale"), vector.invoke("getField").invoke("getScale"));
                    eval.assign(out.getHolder().ref("precision"), vector.invoke("getField").invoke("getPrecision"));
                    eval.assign(out.getValue(), getValueAccessor.invoke("get").arg(indexVariable));
                    return;
                case DECIMAL28DENSE:
                case DECIMAL28SPARSE:
                case DECIMAL38DENSE:
                case DECIMAL38SPARSE:
                    eval.assign(out.getHolder().ref("scale"), vector.invoke("getField").invoke("getScale"));
                    eval.assign(out.getHolder().ref("precision"), vector.invoke("getField").invoke("getPrecision"));
                    eval.assign(out.getHolder().ref("start"), JExpr.lit(TypeHelper.getSize(type)).mul(indexVariable));
                    eval.assign(out.getHolder().ref("buffer"), vector.invoke("getBuffer"));
                    return;
                case INTERVAL:
                    {
                        JVar start = eval.decl(model.INT, "start", JExpr.lit(TypeHelper.getSize(type)).mul(indexVariable));
                        JVar data = eval.decl(model.ref(DrillBuf.class), "data", vector.invoke("getBuffer"));
                        eval.assign(out.getHolder().ref("months"), data.invoke("getInt").arg(start));
                        eval.assign(out.getHolder().ref("days"), data.invoke("getInt").arg(start.plus(JExpr.lit(4))));
                        eval.assign(out.getHolder().ref("milliseconds"), data.invoke("getInt").arg(start.plus(JExpr.lit(8))));
                        return;
                    }
                case INTERVALDAY:
                    {
                        JVar start = eval.decl(model.INT, "start", JExpr.lit(TypeHelper.getSize(type)).mul(indexVariable));
                        eval.assign(out.getHolder().ref("days"), vector.invoke("getBuffer").invoke("getInt").arg(start));
                        eval.assign(out.getHolder().ref("milliseconds"), vector.invoke("getBuffer").invoke("getInt").arg(start.plus(JExpr.lit(4))));
                        return;
                    }
                case VAR16CHAR:
                case VARBINARY:
                case VARCHAR:
                    eval.assign(out.getHolder().ref("buffer"), vector.invoke("getBuffer"));
                    JVar se = eval.decl(model.LONG, "startEnd", getValueAccessor.invoke("getStartEnd").arg(indexVariable));
                    eval.assign(out.getHolder().ref("start"), JExpr.cast(model._ref(int.class), se));
                    eval.assign(out.getHolder().ref("end"), JExpr.cast(model._ref(int.class), se.shr(JExpr.lit(32))));
                    return;
            }
    }
    // fallback.
    eval.add(getValueAccessor.invoke("get").arg(indexVariable).arg(out.getHolder()));
}
Also used : JInvocation(com.sun.codemodel.JInvocation) JVar(com.sun.codemodel.JVar)

Aggregations

JInvocation (com.sun.codemodel.JInvocation)28 JVar (com.sun.codemodel.JVar)17 JClass (com.sun.codemodel.JClass)16 JMethod (com.sun.codemodel.JMethod)15 JBlock (com.sun.codemodel.JBlock)14 JFieldVar (com.sun.codemodel.JFieldVar)9 JExpression (com.sun.codemodel.JExpression)7 HashMap (java.util.HashMap)7 JDefinedClass (com.sun.codemodel.JDefinedClass)5 ArrayList (java.util.ArrayList)5 ByteString (com.linkedin.data.ByteString)3 ActionSchemaArray (com.linkedin.restli.restspec.ActionSchemaArray)3 JType (com.sun.codemodel.JType)3 Arrays (java.util.Arrays)3 Map (java.util.Map)3 JsonNode (com.fasterxml.jackson.databind.JsonNode)2 ArrayNode (com.fasterxml.jackson.databind.node.ArrayNode)2 DataList (com.linkedin.data.DataList)2 DataMap (com.linkedin.data.DataMap)2 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)2