Search in sources :

Example 31 with JExpression

use of com.sun.codemodel.JExpression in project rest.li by linkedin.

the class JavaRequestBuilderGenerator method generateResourceFacade.

private JDefinedClass generateResourceFacade(ResourceSchema resource, File sourceFile, Map<String, JClass> pathKeyTypes, Map<String, JClass> assocKeyTypes, Map<String, List<String>> pathToAssocKeys, String rootPath) throws JClassAlreadyExistsException, IOException {
    final ValidationResult validationResult = ValidateDataAgainstSchema.validate(resource.data(), resource.schema(), new ValidationOptions());
    if (!validationResult.isValid()) {
        throw new IllegalArgumentException(String.format("Resource validation error.  Resource File '%s', Error Details '%s'", sourceFile, validationResult.toString()));
    }
    final String packageName = resource.getNamespace();
    final JPackage clientPackage = (packageName == null || packageName.isEmpty()) ? getPackage() : getPackage(packageName);
    final String className;
    if (_version == RestliVersion.RESTLI_2_0_0) {
        className = getBuilderClassNameByVersion(RestliVersion.RESTLI_2_0_0, null, resource.getName(), true);
    } else {
        className = getBuilderClassNameByVersion(RestliVersion.RESTLI_1_0_0, null, resource.getName(), true);
    }
    final JDefinedClass facadeClass = clientPackage._class(className);
    annotate(facadeClass, sourceFile.getAbsolutePath(), rootPath);
    final JFieldVar baseUriField;
    final JFieldVar requestOptionsField;
    final JExpression baseUriGetter = JExpr.invoke("getBaseUriTemplate");
    final JExpression requestOptionsGetter = JExpr.invoke("getRequestOptions");
    if (_version == RestliVersion.RESTLI_2_0_0) {
        baseUriField = null;
        requestOptionsField = null;
        facadeClass._extends(BuilderBase.class);
    } else {
        // for old builder, instead of extending from RequestBuilderBase, add fields and getters in the class
        baseUriField = facadeClass.field(JMod.PRIVATE | JMod.FINAL, String.class, "_baseUriTemplate");
        requestOptionsField = facadeClass.field(JMod.PRIVATE, RestliRequestOptions.class, "_requestOptions");
        facadeClass.method(JMod.PRIVATE, String.class, "getBaseUriTemplate").body()._return(baseUriField);
        facadeClass.method(JMod.PUBLIC, RestliRequestOptions.class, "getRequestOptions").body()._return(requestOptionsField);
    }
    // make the original resource path available via a private final static variable.
    final JFieldVar originalResourceField = facadeClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, String.class, "ORIGINAL_RESOURCE_PATH");
    final String resourcePath = getResourcePath(resource.getPath());
    originalResourceField.init(JExpr.lit(resourcePath));
    // create reference to RestliRequestOptions.DEFAULT_OPTIONS
    final JClass restliRequestOptionsClass = getCodeModel().ref(RestliRequestOptions.class);
    final JFieldRef defaultOptionsField = restliRequestOptionsClass.staticRef("DEFAULT_OPTIONS");
    if (_version == RestliVersion.RESTLI_1_0_0) {
        // same getPathComponents() logic as in RequestBuilderBase
        final JMethod pathComponentsGetter = facadeClass.method(JMod.PUBLIC, String[].class, "getPathComponents");
        pathComponentsGetter.body()._return(getCodeModel().ref(URIParamUtils.class).staticInvoke("extractPathComponentsFromUriTemplate").arg(baseUriField));
        // method that expresses the following logic
        // (requestOptions == null) ? return RestliRequestOptions.DEFAULT_OPTIONS : requestOptions;
        final JMethod requestOptionsAssigner = facadeClass.method(JMod.PRIVATE | JMod.STATIC, RestliRequestOptions.class, "assignRequestOptions");
        final JVar requestOptionsAssignerParam = requestOptionsAssigner.param(RestliRequestOptions.class, "requestOptions");
        final JConditional requestNullCheck = requestOptionsAssigner.body()._if(requestOptionsAssignerParam.eq(JExpr._null()));
        requestNullCheck._then().block()._return(defaultOptionsField);
        requestNullCheck._else().block()._return(requestOptionsAssignerParam);
    }
    /*
    There will be 4 constructors:
      ()
      (RestliRequestOptions)
      (String)
      (String, RestliRequestOptions)
    */
    final JMethod noArgConstructor = facadeClass.constructor(JMod.PUBLIC);
    final JMethod requestOptionsOverrideConstructor = facadeClass.constructor(JMod.PUBLIC);
    final JMethod resourceNameOverrideConstructor = facadeClass.constructor(JMod.PUBLIC);
    final JMethod mainConstructor = facadeClass.constructor(JMod.PUBLIC);
    // no-argument constructor, delegates to the request options override constructor
    noArgConstructor.body().invoke(THIS).arg(defaultOptionsField);
    // request options override constructor
    final JVar requestOptionsOverrideOptionsParam = requestOptionsOverrideConstructor.param(RestliRequestOptions.class, "requestOptions");
    if (_version == RestliVersion.RESTLI_2_0_0) {
        requestOptionsOverrideConstructor.body().invoke(SUPER).arg(originalResourceField).arg(requestOptionsOverrideOptionsParam);
    } else {
        requestOptionsOverrideConstructor.body().assign(baseUriField, originalResourceField);
        final JInvocation requestOptionsOverrideAssignRequestOptions = new JBlock().invoke("assignRequestOptions").arg(requestOptionsOverrideOptionsParam);
        requestOptionsOverrideConstructor.body().assign(requestOptionsField, requestOptionsOverrideAssignRequestOptions);
    }
    // primary resource name override constructor, delegates to the main constructor
    final JVar resourceNameOverrideResourceNameParam = resourceNameOverrideConstructor.param(_stringClass, "primaryResourceName");
    resourceNameOverrideConstructor.body().invoke(THIS).arg(resourceNameOverrideResourceNameParam).arg(defaultOptionsField);
    // main constructor
    final JVar mainConsResourceNameParam = mainConstructor.param(_stringClass, "primaryResourceName");
    final JVar mainConsOptionsParam = mainConstructor.param(RestliRequestOptions.class, "requestOptions");
    final JExpression baseUriExpr;
    if (resourcePath.contains("/")) {
        baseUriExpr = originalResourceField.invoke("replaceFirst").arg(JExpr.lit("[^/]*/")).arg(mainConsResourceNameParam.plus(JExpr.lit("/")));
    } else {
        baseUriExpr = mainConsResourceNameParam;
    }
    if (_version == RestliVersion.RESTLI_2_0_0) {
        mainConstructor.body().invoke(SUPER).arg(baseUriExpr).arg(mainConsOptionsParam);
    } else {
        final JInvocation mainAssignRequestOptions = new JBlock().invoke("assignRequestOptions").arg(mainConsOptionsParam);
        mainConstructor.body().assign(baseUriField, baseUriExpr);
        mainConstructor.body().assign(requestOptionsField, mainAssignRequestOptions);
    }
    final String resourceName = CodeUtil.capitalize(resource.getName());
    final JMethod primaryResourceGetter = facadeClass.method(JMod.PUBLIC | JMod.STATIC, String.class, "getPrimaryResource");
    primaryResourceGetter.body()._return(originalResourceField);
    final List<String> pathKeys = getPathKeys(resourcePath, pathToAssocKeys);
    JClass keyTyperefClass = null;
    final JClass keyClass;
    JClass keyKeyClass = null;
    JClass keyParamsClass = null;
    final Class<?> resourceSchemaClass;
    Map<String, AssocKeyTypeInfo> assocKeyTypeInfos = Collections.emptyMap();
    StringArray supportsList = null;
    RestMethodSchemaArray restMethods = null;
    FinderSchemaArray finders = null;
    BatchFinderSchemaArray batchFinders = null;
    ResourceSchemaArray subresources = null;
    ActionSchemaArray resourceActions = null;
    ActionSchemaArray entityActions = null;
    final JFieldVar resourceSpecField = facadeClass.field(JMod.PRIVATE | JMod.STATIC | JMod.FINAL, _resourceSpecClass, "_resourceSpec");
    if (resource.getCollection() != null) {
        resourceSchemaClass = CollectionSchema.class;
        final CollectionSchema collection = resource.getCollection();
        final String keyName = collection.getIdentifier().getName();
        // ComplexKeyResource parameterized by those two.
        if (collection.getIdentifier().getParams() == null) {
            keyClass = getJavaBindingType(collection.getIdentifier().getType(), facadeClass).valueClass;
            final JClass declaredClass = getClassRefForSchema(RestSpecCodec.textToSchema(collection.getIdentifier().getType(), _schemaResolver), facadeClass);
            if (!declaredClass.equals(keyClass)) {
                keyTyperefClass = declaredClass;
            }
        } else {
            keyKeyClass = getJavaBindingType(collection.getIdentifier().getType(), facadeClass).valueClass;
            keyParamsClass = getJavaBindingType(collection.getIdentifier().getParams(), facadeClass).valueClass;
            keyClass = getCodeModel().ref(ComplexResourceKey.class).narrow(keyKeyClass, keyParamsClass);
        }
        pathKeyTypes.put(keyName, keyClass);
        supportsList = collection.getSupports();
        restMethods = collection.getMethods();
        finders = collection.getFinders();
        batchFinders = collection.getBatchFinders();
        subresources = collection.getEntity().getSubresources();
        resourceActions = collection.getActions();
        entityActions = collection.getEntity().getActions();
    } else if (resource.getAssociation() != null) {
        resourceSchemaClass = AssociationSchema.class;
        final AssociationSchema association = resource.getAssociation();
        keyClass = getCodeModel().ref(CompoundKey.class);
        supportsList = association.getSupports();
        restMethods = association.getMethods();
        finders = association.getFinders();
        batchFinders = association.getBatchFinders();
        subresources = association.getEntity().getSubresources();
        resourceActions = association.getActions();
        entityActions = association.getEntity().getActions();
        assocKeyTypeInfos = generateAssociationKey(facadeClass, association, resourceSpecField);
        final String keyName = getAssociationKey(resource, association);
        pathKeyTypes.put(keyName, keyClass);
        final List<String> assocKeys = new ArrayList<>(4);
        for (Map.Entry<String, AssocKeyTypeInfo> entry : assocKeyTypeInfos.entrySet()) {
            assocKeys.add(entry.getKey());
            assocKeyTypes.put(entry.getKey(), entry.getValue().getBindingType());
        }
        pathToAssocKeys.put(keyName, assocKeys);
    } else if (resource.getSimple() != null) {
        resourceSchemaClass = SimpleSchema.class;
        final SimpleSchema simpleSchema = resource.getSimple();
        keyClass = _voidClass;
        supportsList = simpleSchema.getSupports();
        restMethods = simpleSchema.getMethods();
        subresources = simpleSchema.getEntity().getSubresources();
        resourceActions = simpleSchema.getActions();
    } else if (resource.getActionsSet() != null) {
        resourceSchemaClass = ActionsSetSchema.class;
        final ActionsSetSchema actionsSet = resource.getActionsSet();
        resourceActions = actionsSet.getActions();
        keyClass = _voidClass;
    } else {
        throw new IllegalArgumentException("unsupported resource type for resource: '" + resourceName + '\'');
    }
    generateOptions(facadeClass, baseUriGetter, requestOptionsGetter);
    if (resourceSchemaClass == CollectionSchema.class || resourceSchemaClass == AssociationSchema.class || resourceSchemaClass == SimpleSchema.class) {
        final JClass schemaClass = getJavaBindingType(resource.getSchema(), null).schemaClass;
        final Set<ResourceMethod> supportedMethods = getSupportedMethods(supportsList);
        final JInvocation supportedMethodsExpr;
        if (supportedMethods.isEmpty()) {
            supportedMethodsExpr = _enumSetClass.staticInvoke("noneOf").arg(_resourceMethodClass.dotclass());
        } else {
            supportedMethodsExpr = _enumSetClass.staticInvoke("of");
            for (ResourceMethod resourceMethod : supportedMethods) {
                validateResourceMethod(resourceSchemaClass, resourceName, resourceMethod);
                supportedMethodsExpr.arg(_resourceMethodClass.staticRef(resourceMethod.name()));
            }
        }
        final JBlock staticInit = facadeClass.init();
        final JVar methodSchemaMap = methodMetadataMapInit(facadeClass, resourceActions, entityActions, staticInit);
        final JVar responseSchemaMap = responseMetadataMapInit(facadeClass, resourceActions, entityActions, staticInit);
        if (resourceSchemaClass == CollectionSchema.class || resourceSchemaClass == AssociationSchema.class) {
            final JClass assocKeyClass = getCodeModel().ref(TypeInfo.class);
            final JClass hashMapClass = getCodeModel().ref(HashMap.class).narrow(_stringClass, assocKeyClass);
            final JVar keyPartsVar = staticInit.decl(hashMapClass, "keyParts").init(JExpr._new(hashMapClass));
            for (Map.Entry<String, AssocKeyTypeInfo> typeInfoEntry : assocKeyTypeInfos.entrySet()) {
                final AssocKeyTypeInfo typeInfo = typeInfoEntry.getValue();
                final JInvocation typeArg = JExpr._new(assocKeyClass).arg(typeInfo.getBindingType().dotclass()).arg(typeInfo.getDeclaredType().dotclass());
                staticInit.add(keyPartsVar.invoke("put").arg(typeInfoEntry.getKey()).arg(typeArg));
            }
            staticInit.assign(resourceSpecField, JExpr._new(_resourceSpecImplClass).arg(supportedMethodsExpr).arg(methodSchemaMap).arg(responseSchemaMap).arg(keyTyperefClass == null ? keyClass.dotclass() : keyTyperefClass.dotclass()).arg(keyKeyClass == null ? JExpr._null() : keyKeyClass.dotclass()).arg(keyKeyClass == null ? JExpr._null() : keyParamsClass.dotclass()).arg(schemaClass.dotclass()).arg(keyPartsVar));
        } else // simple schema
        {
            staticInit.assign(resourceSpecField, JExpr._new(_resourceSpecImplClass).arg(supportedMethodsExpr).arg(methodSchemaMap).arg(responseSchemaMap).arg(schemaClass.dotclass()));
        }
        generateBasicMethods(facadeClass, baseUriGetter, keyClass, schemaClass, supportedMethods, restMethods, resourceSpecField, resourceName, pathKeys, pathKeyTypes, assocKeyTypes, pathToAssocKeys, requestOptionsGetter, resource.data().getDataMap("annotations"), rootPath);
        if (resourceSchemaClass == CollectionSchema.class || resourceSchemaClass == AssociationSchema.class) {
            generateFinders(facadeClass, baseUriGetter, finders, keyClass, schemaClass, assocKeyTypeInfos, resourceSpecField, resourceName, pathKeys, pathKeyTypes, assocKeyTypes, pathToAssocKeys, requestOptionsGetter, rootPath);
            generateBatchFinders(facadeClass, baseUriGetter, batchFinders, keyClass, schemaClass, assocKeyTypeInfos, resourceSpecField, resourceName, pathKeys, pathKeyTypes, assocKeyTypes, pathToAssocKeys, requestOptionsGetter, rootPath);
        }
        generateSubResources(sourceFile, subresources, pathKeyTypes, assocKeyTypes, pathToAssocKeys, rootPath);
    } else // action set
    {
        final JBlock staticInit = facadeClass.init();
        final JInvocation supportedMethodsExpr = _enumSetClass.staticInvoke("noneOf").arg(_resourceMethodClass.dotclass());
        final JVar methodSchemaMap = methodMetadataMapInit(facadeClass, resourceActions, entityActions, staticInit);
        final JVar responseSchemaMap = responseMetadataMapInit(facadeClass, resourceActions, entityActions, staticInit);
        staticInit.assign(resourceSpecField, JExpr._new(_resourceSpecImplClass).arg(supportedMethodsExpr).arg(methodSchemaMap).arg(responseSchemaMap).arg(keyClass.dotclass()).arg(JExpr._null()).arg(JExpr._null()).arg(JExpr._null()).arg(getCodeModel().ref(Collections.class).staticInvoke("<String, Class<?>>emptyMap")));
    }
    generateActions(facadeClass, baseUriGetter, resourceActions, entityActions, keyClass, resourceSpecField, resourceName, pathKeys, pathKeyTypes, assocKeyTypes, pathToAssocKeys, requestOptionsGetter, rootPath);
    generateClassJavadoc(facadeClass, resource);
    if (!checkVersionAndDeprecateBuilderClass(facadeClass, true)) {
        checkRestSpecAndDeprecateRootBuildersClass(facadeClass, resource);
    }
    return facadeClass;
}
Also used : LinkedHashMap(java.util.LinkedHashMap) HashMap(java.util.HashMap) BatchFinderSchemaArray(com.linkedin.restli.restspec.BatchFinderSchemaArray) ValidationResult(com.linkedin.data.schema.validation.ValidationResult) ValidationOptions(com.linkedin.data.schema.validation.ValidationOptions) ActionsSetSchema(com.linkedin.restli.restspec.ActionsSetSchema) StringArray(com.linkedin.data.template.StringArray) FinderSchemaArray(com.linkedin.restli.restspec.FinderSchemaArray) BatchFinderSchemaArray(com.linkedin.restli.restspec.BatchFinderSchemaArray) ActionSchemaArray(com.linkedin.restli.restspec.ActionSchemaArray) JConditional(com.sun.codemodel.JConditional) ArrayList(java.util.ArrayList) DataList(com.linkedin.data.DataList) List(java.util.List) Collections(java.util.Collections) JVar(com.sun.codemodel.JVar) AssociationSchema(com.linkedin.restli.restspec.AssociationSchema) JFieldRef(com.sun.codemodel.JFieldRef) RestMethodSchemaArray(com.linkedin.restli.restspec.RestMethodSchemaArray) CollectionSchema(com.linkedin.restli.restspec.CollectionSchema) RestliRequestOptions(com.linkedin.restli.client.RestliRequestOptions) JDefinedClass(com.sun.codemodel.JDefinedClass) SimpleSchema(com.linkedin.restli.restspec.SimpleSchema) JClass(com.sun.codemodel.JClass) JPackage(com.sun.codemodel.JPackage) ResourceSchemaArray(com.linkedin.restli.restspec.ResourceSchemaArray) JInvocation(com.sun.codemodel.JInvocation) JExpression(com.sun.codemodel.JExpression) JFieldVar(com.sun.codemodel.JFieldVar) URIParamUtils(com.linkedin.restli.internal.common.URIParamUtils) JBlock(com.sun.codemodel.JBlock) JMethod(com.sun.codemodel.JMethod) Map(java.util.Map) LinkedHashMap(java.util.LinkedHashMap) DataMap(com.linkedin.data.DataMap) TreeMap(java.util.TreeMap) HashMap(java.util.HashMap) ResourceMethod(com.linkedin.restli.common.ResourceMethod)

Example 32 with JExpression

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

the class CopyUtil method generateCopies.

public static void generateCopies(ClassGenerator<?> g, VectorAccessible batch, boolean hyper) {
    // we have parallel ids for each value vector so we don't actually have to deal with managing the ids at all.
    int fieldId = 0;
    JExpression inIndex = JExpr.direct("inIndex");
    JExpression outIndex = JExpr.direct("outIndex");
    for (VectorWrapper<?> vv : batch) {
        String copyMethod;
        if (!Types.isFixedWidthType(vv.getField().getType()) || Types.isRepeated(vv.getField().getType()) || Types.isComplex(vv.getField().getType())) {
            copyMethod = "copyFromSafe";
        } else {
            copyMethod = "copyFrom";
        }
        g.rotateBlock();
        TypedFieldId inFieldId = new TypedFieldId.Builder().finalType(vv.getField().getType()).hyper(vv.isHyper()).addId(fieldId).build();
        JVar inVV = g.declareVectorValueSetupAndMember("incoming", inFieldId);
        TypedFieldId outFieldId = new TypedFieldId.Builder().finalType(vv.getField().getType()).hyper(false).addId(fieldId).build();
        JVar outVV = g.declareVectorValueSetupAndMember("outgoing", outFieldId);
        if (hyper) {
            g.getEvalBlock().add(outVV.invoke(copyMethod).arg(inIndex.band(JExpr.lit((int) Character.MAX_VALUE))).arg(outIndex).arg(inVV.component(inIndex.shrz(JExpr.lit(16)))));
        } else {
            g.getEvalBlock().add(outVV.invoke(copyMethod).arg(inIndex).arg(outIndex).arg(inVV));
        }
        g.rotateBlock();
        fieldId++;
    }
}
Also used : TypedFieldId(org.apache.drill.exec.record.TypedFieldId) JExpression(com.sun.codemodel.JExpression) JVar(com.sun.codemodel.JVar)

Example 33 with JExpression

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

the class NestedLoopJoinBatch method setupWorker.

/**
 * Method generates the runtime code needed for NLJ. Other than the setup method to set the input and output value
 * vector references we implement three more methods
 * 1. doEval() -> Evaluates if record from left side matches record from the right side
 * 2. emitLeft() -> Project record from the left side
 * 3. emitRight() -> Project record from the right side (which is a hyper container)
 * @return the runtime generated class that implements the NestedLoopJoin interface
 */
private NestedLoopJoin setupWorker() {
    final CodeGenerator<NestedLoopJoin> nLJCodeGenerator = CodeGenerator.get(SETUP_LEFT_MAPPING, NestedLoopJoin.TEMPLATE_DEFINITION, context.getOptions());
    nLJCodeGenerator.plainJavaCapable(true);
    // Uncomment out this line to debug the generated code.
    // nLJCodeGenerator.saveCodeForDebugging(true);
    final ClassGenerator<NestedLoopJoin> nLJClassGenerator = nLJCodeGenerator.getRoot();
    // generate doEval
    final ErrorCollector collector = new ErrorCollectorImpl();
    /*
        Logical expression may contain fields from left and right batches. During code generation (materialization)
        we need to indicate from which input field should be taken.

        Non-equality joins can belong to one of below categories. For example:
        1. Join on non-equality join predicates:
        select * from t1 inner join t2 on (t1.c1 between t2.c1 AND t2.c2) AND (...)
        2. Join with an OR predicate:
        select * from t1 inner join t2 on on t1.c1 = t2.c1 OR t1.c2 = t2.c2
     */
    Map<VectorAccessible, BatchReference> batches = ImmutableMap.<VectorAccessible, BatchReference>builder().put(left, new BatchReference("leftBatch", "leftIndex")).put(rightContainer, new BatchReference("rightContainer", "rightBatchIndex", "rightRecordIndexWithinBatch")).build();
    LogicalExpression materialize = ExpressionTreeMaterializer.materialize(popConfig.getCondition(), batches, collector, context.getFunctionRegistry(), false, false);
    collector.reportErrors(logger);
    nLJClassGenerator.addExpr(new ReturnValueExpression(materialize), ClassGenerator.BlkCreateMode.FALSE);
    // generate emitLeft
    nLJClassGenerator.setMappingSet(emitLeftMapping);
    JExpression outIndex = JExpr.direct("outIndex");
    JExpression leftIndex = JExpr.direct("leftIndex");
    int fieldId = 0;
    int outputFieldId = 0;
    if (leftSchema != null) {
        // Set the input and output value vector references corresponding to the left batch
        for (MaterializedField field : leftSchema) {
            final TypeProtos.MajorType fieldType = field.getType();
            // Add the vector to the output container
            container.addOrGet(field);
            TypedFieldId inFieldId = new TypedFieldId.Builder().finalType(fieldType).hyper(false).addId(fieldId).build();
            JVar inVV = nLJClassGenerator.declareVectorValueSetupAndMember("leftBatch", inFieldId);
            TypedFieldId outFieldId = new TypedFieldId.Builder().finalType(fieldType).hyper(false).addId(outputFieldId).build();
            JVar outVV = nLJClassGenerator.declareVectorValueSetupAndMember("outgoing", outFieldId);
            nLJClassGenerator.getEvalBlock().add(outVV.invoke("copyFromSafe").arg(leftIndex).arg(outIndex).arg(inVV));
            nLJClassGenerator.rotateBlock();
            fieldId++;
            outputFieldId++;
        }
    }
    // generate emitRight
    fieldId = 0;
    nLJClassGenerator.setMappingSet(emitRightMapping);
    JExpression batchIndex = JExpr.direct("batchIndex");
    JExpression recordIndexWithinBatch = JExpr.direct("recordIndexWithinBatch");
    if (rightSchema != null) {
        // Set the input and output value vector references corresponding to the right batch
        for (MaterializedField field : rightSchema) {
            final TypeProtos.MajorType inputType = field.getType();
            TypeProtos.MajorType outputType;
            // if join type is LEFT, make sure right batch output fields data mode is optional
            if (popConfig.getJoinType() == JoinRelType.LEFT && inputType.getMode() == TypeProtos.DataMode.REQUIRED) {
                outputType = Types.overrideMode(inputType, TypeProtos.DataMode.OPTIONAL);
            } else {
                outputType = inputType;
            }
            MaterializedField newField = MaterializedField.create(field.getName(), outputType);
            container.addOrGet(newField);
            TypedFieldId inFieldId = new TypedFieldId.Builder().finalType(inputType).hyper(true).addId(fieldId).build();
            JVar inVV = nLJClassGenerator.declareVectorValueSetupAndMember("rightContainer", inFieldId);
            TypedFieldId outFieldId = new TypedFieldId.Builder().finalType(outputType).hyper(false).addId(outputFieldId).build();
            JVar outVV = nLJClassGenerator.declareVectorValueSetupAndMember("outgoing", outFieldId);
            nLJClassGenerator.getEvalBlock().add(outVV.invoke("copyFromSafe").arg(recordIndexWithinBatch).arg(outIndex).arg(inVV.component(batchIndex)));
            nLJClassGenerator.rotateBlock();
            fieldId++;
            outputFieldId++;
        }
    }
    return context.getImplementationClass(nLJCodeGenerator);
}
Also used : VectorAccessible(org.apache.drill.exec.record.VectorAccessible) ErrorCollector(org.apache.drill.common.expression.ErrorCollector) MaterializedField(org.apache.drill.exec.record.MaterializedField) JExpression(com.sun.codemodel.JExpression) TypeProtos(org.apache.drill.common.types.TypeProtos) ErrorCollectorImpl(org.apache.drill.common.expression.ErrorCollectorImpl) ReturnValueExpression(org.apache.drill.exec.physical.impl.filter.ReturnValueExpression) LogicalExpression(org.apache.drill.common.expression.LogicalExpression) BatchReference(org.apache.drill.exec.expr.BatchReference) TypedFieldId(org.apache.drill.exec.record.TypedFieldId) JVar(com.sun.codemodel.JVar)

Example 34 with JExpression

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

the class DrillSimpleFuncHolder method generateEvalBody.

/**
 * Generate the eval block for a simple function, including the null-handling wrapper,
 * if requested.
 *
 * @see {@link #generateBody()}
 */
protected HoldingContainer generateEvalBody(ClassGenerator<?> g, HoldingContainer[] inputVariables, String body, JVar[] workspaceJVars, FunctionHolderExpression holderExpr) {
    g.getEvalBlock().directStatement(String.format("//---- start of eval portion of %s function. ----//", getRegisteredNames()[0]));
    JBlock sub = new JBlock(true, true);
    JBlock topSub = sub;
    HoldingContainer out = null;
    MajorType returnValueType = getReturnType();
    // null checks.
    if (getNullHandling() == NullHandling.NULL_IF_NULL) {
        JExpression e = null;
        for (HoldingContainer v : inputVariables) {
            if (v.isOptional()) {
                JExpression isNullExpr;
                if (v.isReader()) {
                    isNullExpr = JOp.cond(v.getHolder().invoke("isSet"), JExpr.lit(1), JExpr.lit(0));
                } else {
                    isNullExpr = v.getIsSet();
                }
                if (e == null) {
                    e = isNullExpr;
                } else {
                    e = e.mul(isNullExpr);
                }
            }
        }
        if (e != null) {
            // if at least one expression must be checked, set up the conditional.
            returnValueType = getReturnType().toBuilder().setMode(DataMode.OPTIONAL).build();
            out = g.declare(returnValueType);
            e = e.eq(JExpr.lit(0));
            JConditional jc = sub._if(e);
            jc._then().assign(out.getIsSet(), JExpr.lit(0));
            sub = jc._else();
        } else if (holderExpr.getMajorType().getMode() == DataMode.OPTIONAL) {
            returnValueType = getReturnType().toBuilder().setMode(DataMode.OPTIONAL).build();
        }
    }
    if (out == null) {
        out = g.declare(returnValueType);
    }
    // add the subblock after the out declaration.
    g.getEvalBlock().add(topSub);
    JVar internalOutput = sub.decl(JMod.FINAL, g.getHolderType(returnValueType), getReturnValue().getName(), JExpr._new(g.getHolderType(returnValueType)));
    // Generate function body from source, including fields rendered as
    // block local vars.
    addProtectedBlock(g, sub, body, inputVariables, workspaceJVars, false);
    // Copy the output from the internal output parameter to the
    // one known in the outer scope.
    List<String> holderFields = ValueHolderHelper.getHolderParams(returnValueType);
    for (String holderField : holderFields) {
        sub.assign(out.f(holderField), internalOutput.ref(holderField));
    }
    if (sub != topSub) {
        // Assign null if NULL_IF_NULL mode
        sub.assign(out.f("isSet"), JExpr.lit(1));
    }
    g.getEvalBlock().directStatement(String.format("//---- end of eval portion of %s function. ----//", getRegisteredNames()[0]));
    return out;
}
Also used : HoldingContainer(org.apache.drill.exec.expr.ClassGenerator.HoldingContainer) MajorType(org.apache.drill.common.types.TypeProtos.MajorType) JBlock(com.sun.codemodel.JBlock) JConditional(com.sun.codemodel.JConditional) JExpression(com.sun.codemodel.JExpression) JVar(com.sun.codemodel.JVar)

Example 35 with JExpression

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

the class DrillComplexWriterFuncHolder method generateEvalBody.

@Override
protected HoldingContainer generateEvalBody(ClassGenerator<?> classGenerator, HoldingContainer[] inputVariables, String body, JVar[] workspaceJVars, FunctionHolderExpression holderExpr) {
    FieldReference fieldReference = holderExpr.getFieldReference();
    classGenerator.getEvalBlock().directStatement(String.format("//---- start of eval portion of %s function. ----//", getRegisteredNames()[0]));
    JBlock sub = new JBlock(true, true);
    JBlock topSub = sub;
    JVar complexWriter = classGenerator.declareClassField("complexWriter", classGenerator.getModel()._ref(ComplexWriter.class));
    JInvocation container = classGenerator.getMappingSet().getOutgoing().invoke("getOutgoingContainer");
    // Default name is "col", if not passed in a reference name for the output vector.
    String refName = fieldReference == null ? "col" : fieldReference.getRootSegment().getPath();
    JClass cwClass = classGenerator.getModel().ref(VectorAccessibleComplexWriter.class);
    classGenerator.getSetupBlock().assign(complexWriter, cwClass.staticInvoke("getWriter").arg(refName).arg(container));
    JClass projBatchClass = classGenerator.getModel().ref(ProjectRecordBatch.class);
    JExpression projBatch = JExpr.cast(projBatchClass, classGenerator.getMappingSet().getOutgoing());
    classGenerator.getSetupBlock().add(projBatch.invoke("addComplexWriter").arg(complexWriter));
    classGenerator.getEvalBlock().add(complexWriter.invoke("setPosition").arg(classGenerator.getMappingSet().getValueWriteIndex()));
    sub.decl(classGenerator.getModel()._ref(ComplexWriter.class), getReturnValue().getName(), complexWriter);
    // add the subblock after the out declaration.
    classGenerator.getEvalBlock().add(topSub);
    addProtectedBlock(classGenerator, sub, body, inputVariables, workspaceJVars, false);
    // JConditional jc = classGenerator.getEvalBlock()._if(complexWriter.invoke("ok").not());
    // jc._then().add(complexWriter.invoke("reset"));
    // jc._then().directStatement("System.out.println(\"debug : write ok fail!, inIndex = \" + inIndex);");
    // jc._then()._return(JExpr.FALSE);
    // jc._else().directStatement("System.out.println(\"debug : write successful, inIndex = \" + inIndex);");
    classGenerator.getEvalBlock().directStatement(String.format("//---- end of eval portion of %s function. ----//", getRegisteredNames()[0]));
    return null;
}
Also used : ComplexWriter(org.apache.drill.exec.vector.complex.writer.BaseWriter.ComplexWriter) VectorAccessibleComplexWriter(org.apache.drill.exec.record.VectorAccessibleComplexWriter) FieldReference(org.apache.drill.common.expression.FieldReference) JClass(com.sun.codemodel.JClass) JBlock(com.sun.codemodel.JBlock) JInvocation(com.sun.codemodel.JInvocation) JExpression(com.sun.codemodel.JExpression) JVar(com.sun.codemodel.JVar)

Aggregations

JExpression (com.sun.codemodel.JExpression)39 JVar (com.sun.codemodel.JVar)28 JBlock (com.sun.codemodel.JBlock)22 JMethod (com.sun.codemodel.JMethod)16 JClass (com.sun.codemodel.JClass)13 JConditional (com.sun.codemodel.JConditional)13 JInvocation (com.sun.codemodel.JInvocation)10 JFieldVar (com.sun.codemodel.JFieldVar)9 JDefinedClass (com.sun.codemodel.JDefinedClass)8 TypedFieldId (org.apache.drill.exec.record.TypedFieldId)6 JType (com.sun.codemodel.JType)5 Map (java.util.Map)5 LogicalExpression (org.apache.drill.common.expression.LogicalExpression)5 JFieldRef (com.sun.codemodel.JFieldRef)4 IOException (java.io.IOException)4 MajorType (org.apache.drill.common.types.TypeProtos.MajorType)4 JPackage (com.sun.codemodel.JPackage)3 ErrorCollector (org.apache.drill.common.expression.ErrorCollector)3 ErrorCollectorImpl (org.apache.drill.common.expression.ErrorCollectorImpl)3 HoldingContainer (org.apache.drill.exec.expr.ClassGenerator.HoldingContainer)3