Search in sources :

Example 51 with JClass

use of com.sun.codemodel.JClass 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 52 with JClass

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

the class DrillAggFuncHolder method renderStart.

@Override
public JVar[] renderStart(ClassGenerator<?> g, HoldingContainer[] inputVariables) {
    if (!g.getMappingSet().isHashAggMapping()) {
        //Declare workspace vars for non-hash-aggregation.
        JVar[] workspaceJVars = declareWorkspaceVariables(g);
        generateBody(g, BlockType.SETUP, setup(), null, workspaceJVars, true);
        return workspaceJVars;
    } else {
        //Declare workspace vars and workspace vectors for hash aggregation.
        JVar[] workspaceJVars = declareWorkspaceVectors(g);
        JBlock setupBlock = g.getSetupBlock();
        //Loop through all workspace vectors, to get the minimum of size of all workspace vectors.
        JVar sizeVar = setupBlock.decl(g.getModel().INT, "vectorSize", JExpr.lit(Integer.MAX_VALUE));
        JClass mathClass = g.getModel().ref(Math.class);
        for (int id = 0; id < getWorkspaceVars().length; id++) {
            if (!getWorkspaceVars()[id].isInject()) {
                setupBlock.assign(sizeVar, mathClass.staticInvoke("min").arg(sizeVar).arg(g.getWorkspaceVectors().get(getWorkspaceVars()[id]).invoke("getValueCapacity")));
            }
        }
        for (int i = 0; i < getWorkspaceVars().length; i++) {
            if (!getWorkspaceVars()[i].isInject()) {
                setupBlock.assign(workspaceJVars[i], JExpr._new(g.getHolderType(getWorkspaceVars()[i].getMajorType())));
            }
        }
        //Use for loop to initialize entries in the workspace vectors.
        JForLoop forLoop = setupBlock._for();
        JVar ivar = forLoop.init(g.getModel().INT, "drill_internal_i", JExpr.lit(0));
        forLoop.test(ivar.lt(sizeVar));
        forLoop.update(ivar.assignPlus(JExpr.lit(1)));
        JBlock subBlock = generateInitWorkspaceBlockHA(g, BlockType.SETUP, setup(), workspaceJVars, ivar);
        forLoop.body().add(subBlock);
        return workspaceJVars;
    }
}
Also used : JClass(com.sun.codemodel.JClass) JBlock(com.sun.codemodel.JBlock) JForLoop(com.sun.codemodel.JForLoop) JVar(com.sun.codemodel.JVar)

Aggregations

JClass (com.sun.codemodel.JClass)52 JVar (com.sun.codemodel.JVar)25 JDefinedClass (com.sun.codemodel.JDefinedClass)22 JMethod (com.sun.codemodel.JMethod)20 JInvocation (com.sun.codemodel.JInvocation)15 JBlock (com.sun.codemodel.JBlock)13 Map (java.util.Map)9 JFieldVar (com.sun.codemodel.JFieldVar)8 JType (com.sun.codemodel.JType)8 HashMap (java.util.HashMap)8 JCodeModel (com.sun.codemodel.JCodeModel)7 JExpression (com.sun.codemodel.JExpression)7 JPackage (com.sun.codemodel.JPackage)7 JsonNode (com.fasterxml.jackson.databind.JsonNode)6 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)5 ObjectNode (com.fasterxml.jackson.databind.node.ObjectNode)5 Test (org.junit.Test)5 ArrayDataSchema (com.linkedin.data.schema.ArrayDataSchema)4 DataSchema (com.linkedin.data.schema.DataSchema)4 Schema (org.jsonschema2pojo.Schema)4