/** * Creates a default value for a set property by: * <ol> * <li>Creating a new {@link LinkedHashSet} with the correct generic type * <li>Using {@link Arrays#asList(Object...)} to initialize the set with the * correct default values * </ol> * * @param fieldType * the java type that applies for this field ({@link Set} with * some generic type argument) * @param node * the node containing default values for this set * @return an expression that creates a default value that can be assigned * to this field */ private JExpression getDefaultSet(JType fieldType, JsonNode node) { JClass setGenericType = ((JClass) fieldType).getTypeParameters().get(0); JClass setImplClass = fieldType.owner().ref(LinkedHashSet.class); setImplClass = setImplClass.narrow(setGenericType); JInvocation newSetImpl = JExpr._new(setImplClass); if (node instanceof ArrayNode && node.size() > 0) { JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList"); for (JsonNode defaultValue : node) { invokeAsList.arg(getDefaultValue(setGenericType, defaultValue)); } newSetImpl.arg(invokeAsList); } else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) { return JExpr._null(); } return newSetImpl; }
private void addFactoryMethod(JDefinedClass _enum, JType backingType) { JFieldVar quickLookupMap = addQuickLookupMap(_enum, backingType); JMethod fromValue = _enum.method(JMod.PUBLIC | JMod.STATIC, _enum, "fromValue"); JVar valueParam = fromValue.param(backingType, "value"); JBlock body = fromValue.body(); JVar constant = body.decl(_enum, "constant"); constant.init(quickLookupMap.invoke("get").arg(valueParam)); JConditional _if = body._if(constant.eq(JExpr._null())); JInvocation illegalArgumentException = JExpr._new(_enum.owner().ref(IllegalArgumentException.class)); JExpression expr = valueParam; // if string no need to add "" if(!isString(backingType)){ expr = expr.plus(JExpr.lit("")); } illegalArgumentException.arg(expr); _if._then()._throw(illegalArgumentException); _if._else()._return(constant); ruleFactory.getAnnotator().enumCreatorMethod(fromValue); }
/** * Creates a default value for a list property by: * <ol> * <li>Creating a new {@link ArrayList} with the correct generic type * <li>Using {@link Arrays#asList(Object...)} to initialize the list with * the correct default values * </ol> * * @param fieldType * the java type that applies for this field ({@link List} with * some generic type argument) * @param node * the node containing default values for this list * @return an expression that creates a default value that can be assigned * to this field */ private JExpression getDefaultList(JType fieldType, JsonNode node) { JClass listGenericType = ((JClass) fieldType).getTypeParameters().get(0); JClass listImplClass = fieldType.owner().ref(ArrayList.class); listImplClass = listImplClass.narrow(listGenericType); JInvocation newListImpl = JExpr._new(listImplClass); if (node instanceof ArrayNode && node.size() > 0) { JInvocation invokeAsList = fieldType.owner().ref(Arrays.class).staticInvoke("asList"); for (JsonNode defaultValue : node) { invokeAsList.arg(getDefaultValue(listGenericType, defaultValue)); } newListImpl.arg(invokeAsList); } else if (!ruleFactory.getGenerationConfig().isInitializeCollections()) { return JExpr._null(); } return newListImpl; }
@Override void generateCode(ClassGenerator<WindowFramer> cg) { final GeneratorMapping mapping = GeneratorMapping.create("setupPartition", "outputRow", "resetValues", "cleanup"); final MappingSet mappingSet = new MappingSet(null, "outIndex", mapping, mapping); cg.setMappingSet(mappingSet); final JVar vv = cg.declareVectorValueSetupAndMember(cg.getMappingSet().getOutgoing(), fieldId); final JExpression outIndex = cg.getMappingSet().getValueWriteIndex(); JInvocation setMethod = vv.invoke("getMutator").invoke("setSafe").arg(outIndex) .arg(JExpr.direct("partition.ntile(" + numTiles + ")")); cg.getEvalBlock().add(setMethod); }
@Override public HoldingContainer visitDecimal9Constant(Decimal9Expression e, ClassGenerator<?> generator) throws RuntimeException { MajorType majorType = e.getMajorType(); JBlock setup = generator.getBlock(BlockType.SETUP); JType holderType = generator.getHolderType(majorType); JVar var = generator.declareClassField("dec9", holderType); JExpression valueLiteral = JExpr.lit(e.getIntFromDecimal()); JExpression scaleLiteral = JExpr.lit(e.getScale()); JExpression precisionLiteral = JExpr.lit(e.getPrecision()); setup.assign( var, generator.getModel().ref(ValueHolderHelper.class).staticInvoke("getDecimal9Holder").arg(valueLiteral) .arg(scaleLiteral).arg(precisionLiteral)); return new HoldingContainer(majorType, var, null, null); }
@Override public HoldingContainer visitDecimal18Constant(Decimal18Expression e, ClassGenerator<?> generator) throws RuntimeException { MajorType majorType = e.getMajorType(); JBlock setup = generator.getBlock(BlockType.SETUP); JType holderType = generator.getHolderType(majorType); JVar var = generator.declareClassField("dec18", holderType); JExpression valueLiteral = JExpr.lit(e.getLongFromDecimal()); JExpression scaleLiteral = JExpr.lit(e.getScale()); JExpression precisionLiteral = JExpr.lit(e.getPrecision()); setup.assign( var, generator.getModel().ref(ValueHolderHelper.class).staticInvoke("getDecimal18Holder").arg(valueLiteral) .arg(scaleLiteral).arg(precisionLiteral)); return new HoldingContainer(majorType, var, null, null); }
@Override public HoldingContainer visitDecimalConstant(DecimalExpression e, ClassGenerator<?> generator) throws RuntimeException { CompleteType majorType= e.getCompleteType(); JBlock setup = generator.getBlock(BlockType.SETUP); JType holderType = majorType.getHolderType(generator.getModel()); JVar var = generator.declareClassField("dec", holderType); JExpression valueLiteral = JExpr.lit(e.getIntFromDecimal()); JExpression scaleLiteral = JExpr.lit(e.getScale()); JExpression precisionLiteral = JExpr.lit(e.getPrecision()); setup.assign( var, generator.getModel().ref(ValueHolderHelper.class).staticInvoke("getNullableDecimalHolder").arg(valueLiteral) .arg(scaleLiteral).arg(precisionLiteral)); return new HoldingContainer(majorType, var, var.ref("value"), var.ref("isSet")); }
public JVar declareVectorValueSetupAndMember(DirectExpression batchName, TypedFieldId fieldId) { final ValueVectorSetup setup = new ValueVectorSetup(batchName, fieldId); final Class<?> valueVectorClass = fieldId.getIntermediateClass(); final JClass vvClass = model.ref(valueVectorClass); final JClass retClass = fieldId.isHyperReader() ? vvClass.array() : vvClass; final JVar vv = declareClassField("vv", retClass); final JBlock b = getSetupBlock(); int[] fieldIndices = fieldId.getFieldIds(); JInvocation invoke = model.ref(VectorResolver.class).staticInvoke(fieldId.isHyperReader() ? "hyper" : "simple") .arg(batchName) .arg(vvClass.dotclass()); for(int i = 0; i < fieldIndices.length; i++){ invoke.arg(JExpr.lit(fieldIndices[i])); } // we have to cast here since Janino doesn't handle generic inference well. JExpression casted = JExpr.cast(retClass, invoke); b.assign(vv, casted); vvDeclaration.put(setup, vv); return vv; }
private List<Partitioner> createClassInstances(int actualPartitions) throws SchemaChangeException { // set up partitioning function final LogicalExpression expr = config.getExpr(); final ClassGenerator<Partitioner> cg = context.getClassProducer().createGenerator(Partitioner.TEMPLATE_DEFINITION).getRoot(); ClassGenerator<Partitioner> cgInner = cg.getInnerGenerator("OutgoingRecordBatch"); final LogicalExpression materializedExpr = context.getClassProducer().materialize(expr, incoming); // generate code to copy from an incoming value vector to the destination partition's outgoing value vector JExpression bucket = JExpr.direct("bucket"); // generate evaluate expression to determine the hash ClassGenerator.HoldingContainer exprHolder = cg.addExpr(materializedExpr); cg.getEvalBlock().decl(JType.parse(cg.getModel(), "int"), "bucket", exprHolder.getValue().mod(JExpr.lit(outGoingBatchCount))); cg.getEvalBlock()._return(cg.getModel().ref(Math.class).staticInvoke("abs").arg(bucket)); CopyUtil.generateCopies(cgInner, incoming, incoming.getSchema().getSelectionVectorMode() == SelectionVectorMode.FOUR_BYTE); // compile and setup generated code List<Partitioner> subPartitioners = cg.getCodeGenerator().getImplementationClass(actualPartitions); return subPartitioners; }
private JVar addParameter( final JMethod method, final JExpression returnValue, final JFieldVar field, final boolean isImutable, final boolean isNullable, final boolean isArrayNullable, final boolean isCollectionNullable) { if (isImutable) { return SourceFactoryUtilities.addParameter(method, field); } if (isInstanceOfMap(field.type())) { return mapSetter(method, returnValue, field, isNullable); } if (isInstanceOfList(field.type())) { return listSetter(method, returnValue, field, isNullable, isCollectionNullable); } return objectSetter(method, returnValue, field, isNullable, isArrayNullable); }
private JVar listSetter( final JMethod method, final JExpression returnValue, final JFieldVar field, final boolean isNullable, final boolean isCollectionNullable) { if (isNullable) { if (!isCollectionNullable) { return addListParameter(method, field, true, createAddIfNullClearListAndReturnClosure(method, returnValue)); } return addListParameter(method, field, true, createAddIfNullReturnClosure(method, returnValue)); } return addListParameter( method, field, true, createEnsureArgumentNotNullClosure(this.ensurePredicateFactory, method)); }
private JVar objectSetter( final JMethod method, final JExpression returnValue, final JFieldVar field, final boolean isNullable, final boolean isArrayNullable) { if (isNullable) { if (!isArrayNullable && field.type().isArray()) { return addObjectParameter( method, field, createAddIfNullSetEmptyArrayAndReturnClosure(this.codeModel, method, returnValue)); } return addObjectParameter(method, field); } return addObjectParameter(method, field, createEnsureArgumentNotNullClosure(this.ensurePredicateFactory, method)); }
public void createEquals(final JDefinedClass bean, final Iterable<JFieldVar> fields) { final JMethod method = bean.method(JMod.PUBLIC, this.codeModel.BOOLEAN, "equals"); method.annotate(java.lang.Override.class); final JVar object = method.param(_type(java.lang.Object.class.getName()), "object"); final JBlock block = method.body(); block._if(JExpr._this().eq(object))._then()._return(JExpr.TRUE); block._if(object._instanceof(bean).not())._then()._return(JExpr.FALSE); JExpression result = JExpr.TRUE; final JExpression other = block.decl(bean, "other", JExpr.cast(bean, object)); final JClass objectUtilities = _classByNames(net.anwiba.commons.lang.object.ObjectUtilities.class.getName()); for (final JFieldVar field : fields) { result = result.cand(objectUtilities.staticInvoke("equals").arg(JExpr.refthis(field.name())).arg(other.ref(field))); } block._return(result); }
public static IProcedure<JVar, RuntimeException> createAddIfNullSetEmptyArrayAndReturnClosure( final JCodeModel codeModel, final JMethod method, final JExpression returnValue) { ensureThatArgument(method, notNull()); return new IProcedure<JVar, RuntimeException>() { @Override public void execute(final JVar param) throws RuntimeException { ensureThatArgument(param, notNull()); final ValueConverter valueConverter = new ValueConverter(codeModel); final JInvocation invocation = JExpr._new(param.type()); method .body() ._if(param.eq(JExpr._null())) ._then() .block() .assign(JExpr.refthis(param.name()), valueConverter.convert(invocation)) ._return(returnValue); } }; }
public static IProcedure<JVar, RuntimeException> createAddIfNullClearMapAndReturnClosure( final JMethod method, final JExpression returnValue) { ensureThatArgument(method, notNull()); return new IProcedure<JVar, RuntimeException>() { @Override public void execute(final JVar param) throws RuntimeException { ensureThatArgument(param, notNull()); method .body() ._if(param.eq(JExpr._null())) ._then() .block() .add(JExpr.refthis(param.name()).invoke("clear")) ._return(returnValue); } }; }
public static IProcedure<JVar, RuntimeException> createAddIfNullClearListAndReturnClosure( final JMethod method, final JExpression returnValue) { ensureThatArgument(method, notNull()); return new IProcedure<JVar, RuntimeException>() { @Override public void execute(final JVar param) throws RuntimeException { ensureThatArgument(param, notNull()); method .body() ._if(param.eq(JExpr._null())) ._then() .block() .add(JExpr.refthis(param.name()).invoke("clear")) ._return(returnValue); } }; }
@SuppressWarnings("unchecked") @Test public void shouldFindEnumValues() throws IllegalAccessException { klass = modelPackage._getClass("Colour"); assertThat(klass.getClassType()).isEqualTo(ClassType.ENUM); Map<String,JEnumConstant> enums = (Map<String, JEnumConstant>) FieldUtils.readField(klass, "enumConstantsByName", true); assertThat(enums.keySet()).contains("LIGHT_BLUE", "RED"); JEnumConstant lightBlue = enums.get("LIGHT_BLUE"); assertThat(lightBlue.javadoc().get(0)).isEqualTo("Colour of the sky"); List<JExpression> args = (List<JExpression>) FieldUtils.readField(lightBlue, "args", true); assertThat(args).hasSize(1); assertThat(args.get(0)).isInstanceOf(JStringLiteral.class); String literal = (String) FieldUtils.readField(args.get(0), "str", true); assertThat(literal).isEqualTo("lightBlue"); }
private void addParameter(JBlock methodBody, JVar queryParams, String valueName, Boolean encode, Boolean simple, boolean isList) { JBlock b = methodBody; if (!simple) { JConditional _if = methodBody._if(JExpr.ref(valueName).ne(JExpr._null())); b = _if._then(); } b.invoke(queryParams, "append").arg(JExpr.lit(valueName + "=")); if (encode) { JExpression expr = jCodeModel.ref(java.net.URLEncoder.class).staticInvoke("encode").arg(JExpr.ref(valueName)).arg("UTF-8"); b.invoke(queryParams, "append").arg(expr); } else { if(isList){ b.directStatement("if("+valueName+".getClass().isArray())" +"{queryParams.append(String.join(\"&"+valueName+"=\"," +valueName+"));}"); } else{ b.invoke(queryParams, "append").arg(JExpr.ref(valueName)); } } b.invoke(queryParams, "append").arg(JExpr.lit("&")); }
@Override public void caseAPrimaryMethodInvocation(APrimaryMethodInvocation node) { ExpressionAdapter ea = new ExpressionAdapter(new JExprParent(), context); node.getExpressionNoName().apply(ea); JExpression lhs = ea.expr; JInvocation inv = parent.invoke(lhs, node.getIdentifier().getText()); if (!node.getNonWildTypeArguments().isEmpty()) { Logger.error(context.getFile(), node.getIdentifier(), "CodeModel does not support type arguments for methods, yet. Ignoring..."); } Argumentable ia = new InvocationArgumentable(inv); for (PArgument arg : node.getArgument()) { ArgumentAdapter aa = new ArgumentAdapter(ia, context); arg.apply(aa); } expr = inv; }
@Override public void caseAClassMethodInvocation(AClassMethodInvocation node) { AClassName acname = (AClassName) node.getClassName(); String name = nameToString(acname.getName()); JClass jc = context.resolveType(acname.getName()); JExpression jcs = JExpr.dotsuper(jc); JInvocation inv = jcs.invoke(node.getIdentifier().getText()); Argumentable ia = new InvocationArgumentable(inv); for (PArgument arg : node.getArgument()) { ArgumentAdapter aa = new ArgumentAdapter(ia, context); arg.apply(aa); } expr = inv; parent.addInvocation(inv); }
@Override public void generete(Const definition) throws Exception { JDefinedClass jExceptionClass = this.getJavaPackage()._class(JMod.PUBLIC, definition.getName(), ClassType.CLASS); jExceptionClass._extends(org.apache.thrift.TException.class); // this.context.getTypeRegistry().add(jExceptionClass); int[] count = { 0 }; // for (Const constDef : subprogram.constants) { JType fieldType = this.resolvType(definition.getType()); JExpression exp = this.buildStaticExpression(jConstantsClass, fieldType, definition.getName(), definition.getValue() , count); jConstantsClass.field(JMod.PUBLIC | JMod.FINAL | JMod.STATIC, fieldType, definition.getName(), exp); // } }
public EqualsArguments element(JBlock subBlock, JType elementType) { final JVar leftElementValue = subBlock.decl(JMod.FINAL, elementType, leftValue().name() + "Element", leftValue().invoke("next")); final JVar rightElementValue = subBlock.decl(JMod.FINAL, elementType, rightValue().name() + "Element", rightValue().invoke("next")); // if (!(o1==null ? o2==null : o1.equals(o2))) // return false; final boolean isElementAlwaysSet = elementType.isPrimitive(); final JExpression leftElementHasSetValue = isElementAlwaysSet ? JExpr.TRUE : leftElementValue.ne(JExpr._null()); final JExpression rightElementHasSetValue = isElementAlwaysSet ? JExpr.TRUE : rightElementValue.ne(JExpr._null()); return spawn(leftElementValue, leftElementHasSetValue, rightElementValue, rightElementHasSetValue); }
@Override public JExpression visit(Expression.Const val) { final JExpression repr; if (val.value instanceof TypeInt) { // TODO: check if representation is not outside range repr = JExpr.lit(((TypeInt)val.value).safeIntValue()); // XXX } else if (val.value instanceof TypeFloat) { repr = JExpr.lit(((TypeFloat)val.value).getValue()); } else if (val.value instanceof TypeString) { repr = JExpr.lit(((TypeString)val.value).getValue()); } else if (val.value instanceof TypeBool) { repr = JExpr.lit(val.value.isTrue()); } else if (val.value instanceof TypeBytes) { repr = TypeBytes.ByteSequence.lit(((TypeBytes)val.value).getValue()); } else { throw new ExpressionFault.Unsupported(val.getType().getClass(), "primitive evaluation. Type was not specified correctly, evaluation cannot be used" +" for unspecified or wrong types. Please check content of the constans: " + val + "."); } return repr; }
protected JMethod createGetter() { final MethodWriter writer = outline.createMethodWriter(); final JMethod getter = writer.declareMethod(exposedType, getGetterName()); JExpression source = getCore(); final JExpression unwrapCondition = unwrapCondifiton(source); if (unwrapCondition == null) { getter.body()._return(unwrap(source)); } else { final JConditional _if = getter.body()._if(unwrapCondition); _if._then()._return(unwrap(source)); _if._else()._return(JExpr._null()); } return getter; }
private void appendTerminal(Terminal t){ //add to pattern array patternArray.add(model.ref(Pattern.class) .staticInvoke("compile").arg(JExpr.lit(t.getRegex()))); //add to build by index method String symbol = t.getSymbol(); JExpression build; if(symbol == null){ build = JExpr._null(); }else{ build = JExpr._new(nodes.getTokenNode(symbol)); } buildIndexSw._case(JExpr.lit(index)).body()._return(build); if(symbol != null){ build = JExpr._new(nodes.getTokenNode(symbol)).arg(text) .arg(line).arg(pos); } buildIndexTextLinePosSw._case(JExpr.lit(index)).body()._return(build); ++index; }
@Override protected JExpression unwrap(JExpression source) { final JType declaredType = getDeclaredType(); final JClass elementClass = codeModel.ref(JAXBElement.class).narrow( declaredType.boxify().wildcard()); // TODO remove if cast is not necessary final JExpression value = JExpr.cast(elementClass, source); if (xmlAdapterClass == null) { return XmlAdapterXjcUtils.unmarshallJAXBElement(codeModel, value); } else { return XmlAdapterXjcUtils.unmarshallJAXBElement(codeModel, xmlAdapterClass, value); } }
private JMethod addInternalGetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) { JMethod method = jclass.method(PROTECTED, jclass.owner()._ref(Object.class), DEFINED_GETTER_NAME); JVar nameParam = method.param(String.class, "name"); JVar notFoundParam = method.param(jclass.owner()._ref(Object.class), "notFoundValue"); JBlock body = method.body(); JConditional propertyConditional = null; if (propertiesNode != null) { for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) { Map.Entry<String, JsonNode> property = properties.next(); String propertyName = property.getKey(); JsonNode node = property.getValue(); String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node); JType propertyType = jclass.fields().get(fieldName).type(); JExpression condition = lit(propertyName).invoke("equals").arg(nameParam); if (propertyConditional == null) { propertyConditional = body._if(condition); } else { propertyConditional = propertyConditional._elseif(condition); } JMethod propertyGetter = jclass.getMethod(getGetterName(propertyName, propertyType, node), new JType[] {}); propertyConditional._then()._return(invoke(propertyGetter)); } } JClass extendsType = jclass._extends(); JBlock lastBlock = propertyConditional == null ? body : propertyConditional._else(); if (extendsType != null && extendsType instanceof JDefinedClass) { JDefinedClass parentClass = (JDefinedClass) extendsType; JMethod parentMethod = parentClass.getMethod(DEFINED_GETTER_NAME, new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) }); lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(notFoundParam)); } else { lastBlock._return(notFoundParam); } return method; }
private JMethod addInternalSetMethodJava6(JDefinedClass jclass, JsonNode propertiesNode) { JMethod method = jclass.method(PROTECTED, jclass.owner().BOOLEAN, DEFINED_SETTER_NAME); JVar nameParam = method.param(String.class, "name"); JVar valueParam = method.param(Object.class, "value"); JBlock body = method.body(); JConditional propertyConditional = null; if (propertiesNode != null) { for (Iterator<Map.Entry<String, JsonNode>> properties = propertiesNode.fields(); properties.hasNext();) { Map.Entry<String, JsonNode> property = properties.next(); String propertyName = property.getKey(); JsonNode node = property.getValue(); String fieldName = ruleFactory.getNameHelper().getPropertyName(propertyName, node); JType propertyType = jclass.fields().get(fieldName).type(); JExpression condition = lit(propertyName).invoke("equals").arg(nameParam); propertyConditional = propertyConditional == null ? propertyConditional = body._if(condition) : propertyConditional._elseif(condition); JBlock callSite = propertyConditional._then(); addSetProperty(jclass, callSite, propertyName, propertyType, valueParam, node); callSite._return(TRUE); } } JClass extendsType = jclass._extends(); JBlock lastBlock = propertyConditional == null ? body : propertyConditional._else(); if (extendsType != null && extendsType instanceof JDefinedClass) { JDefinedClass parentClass = (JDefinedClass) extendsType; JMethod parentMethod = parentClass.getMethod(DEFINED_SETTER_NAME, new JType[] { parentClass.owner()._ref(String.class), parentClass.owner()._ref(Object.class) }); lastBlock._return(_super().invoke(parentMethod).arg(nameParam).arg(valueParam)); } else { lastBlock._return(FALSE); } return method; }
private void addToString(JDefinedClass _enum, JFieldVar valueField) { JMethod toString = _enum.method(JMod.PUBLIC, String.class, "toString"); JBlock body = toString.body(); JExpression toReturn = JExpr._this().ref(valueField); if(!isString(valueField.type())){ toReturn = toReturn.plus(JExpr.lit("")); } body._return(toReturn); toString.annotate(Override.class); }
/** * @see EnumRule */ private static JExpression getDefaultEnum(JType fieldType, JsonNode node) { JDefinedClass enumClass = (JDefinedClass) fieldType; JType backingType = enumClass.fields().get("value").type(); JInvocation invokeFromValue = enumClass.staticInvoke("fromValue"); invokeFromValue.arg(getDefaultValue(backingType, node)); return invokeFromValue; }
/** * Calls the correct {@link JExpr} <code>lit</code> method. * * @param value * The literal value that must be output in an expresion. * @return The matching expression for the real type of value. */ public static JExpression literalExpr(Object value) { if (value == null) { return JExpr._null(); } else if (value instanceof String) { return JExpr.lit((String) value); } else if (value instanceof Integer) { return JExpr.lit((Integer) value); } throw new RuntimeException("Impossible to construct initial value for: " + value); }
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(); JVar inVV = g.declareVectorValueSetupAndMember("incoming", new TypedFieldId(vv.getField().getType(), vv.isHyper(), fieldId)); JVar outVV = g.declareVectorValueSetupAndMember("outgoing", new TypedFieldId(vv.getField().getType(), false, fieldId)); 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++; } }
private List<Partitioner> createClassInstances(int actualPartitions) throws SchemaChangeException { // set up partitioning function final LogicalExpression expr = operator.getExpr(); final ErrorCollector collector = new ErrorCollectorImpl(); final ClassGenerator<Partitioner> cg ; cg = CodeGenerator.getRoot(Partitioner.TEMPLATE_DEFINITION, context.getFunctionRegistry()); ClassGenerator<Partitioner> cgInner = cg.getInnerGenerator("OutgoingRecordBatch"); final LogicalExpression materializedExpr = ExpressionTreeMaterializer.materialize(expr, incoming, collector, context.getFunctionRegistry()); if (collector.hasErrors()) { throw new SchemaChangeException(String.format( "Failure while trying to materialize incoming schema. Errors:\n %s.", collector.toErrorString())); } // generate code to copy from an incoming value vector to the destination partition's outgoing value vector JExpression bucket = JExpr.direct("bucket"); // generate evaluate expression to determine the hash ClassGenerator.HoldingContainer exprHolder = cg.addExpr(materializedExpr); cg.getEvalBlock().decl(JType.parse(cg.getModel(), "int"), "bucket", exprHolder.getValue().mod(JExpr.lit(outGoingBatchCount))); cg.getEvalBlock()._return(cg.getModel().ref(Math.class).staticInvoke("abs").arg(bucket)); CopyUtil.generateCopies(cgInner, incoming, incoming.getSchema().getSelectionVectorMode() == SelectionVectorMode.FOUR_BYTE); try { // compile and setup generated code List<Partitioner> subPartitioners = context.getImplementationClass(cg, actualPartitions); return subPartitioners; } catch (ClassTransformationException | IOException e) { throw new SchemaChangeException("Failure while attempting to load generated class", e); } }
@Override void generateCode(ClassGenerator<WindowFramer> cg) { final GeneratorMapping mapping = GeneratorMapping.create("setupPartition", "outputRow", "resetValues", "cleanup"); final MappingSet mappingSet = new MappingSet(null, "outIndex", mapping, mapping); cg.setMappingSet(mappingSet); final JVar vv = cg.declareVectorValueSetupAndMember(cg.getMappingSet().getOutgoing(), fieldId); final JExpression outIndex = cg.getMappingSet().getValueWriteIndex(); JInvocation setMethod = vv.invoke("getMutator").invoke("setSafe").arg(outIndex).arg(JExpr.direct("partition." + getName())); cg.getEvalBlock().add(setMethod); }
private HoldingContainer visitValueVectorWriteExpression(ValueVectorWriteExpression e, ClassGenerator<?> generator) { final LogicalExpression child = e.getChild(); final HoldingContainer inputContainer = child.accept(this, generator); JBlock block = generator.getEvalBlock(); JExpression outIndex = generator.getMappingSet().getValueWriteIndex(); JVar vv = generator.declareVectorValueSetupAndMember(generator.getMappingSet().getOutgoing(), e.getFieldId()); // Only when the input is a reader, use writer interface to copy value. // Otherwise, input is a holder and we use vv mutator to set value. if (inputContainer.isReader()) { JType writerImpl = generator.getModel()._ref( TypeHelper.getWriterImpl(inputContainer.getMinorType(), inputContainer.getMajorType().getMode())); JType writerIFace = generator.getModel()._ref( TypeHelper.getWriterInterface(inputContainer.getMinorType(), inputContainer.getMajorType().getMode())); JVar writer = generator.declareClassField("writer", writerIFace); generator.getSetupBlock().assign(writer, JExpr._new(writerImpl).arg(vv).arg(JExpr._null())); generator.getEvalBlock().add(writer.invoke("setPosition").arg(outIndex)); String copyMethod = inputContainer.isSingularRepeated() ? "copyAsValueSingle" : "copyAsValue"; generator.getEvalBlock().add(inputContainer.getHolder().invoke(copyMethod).arg(writer)); if (e.isSafe()) { HoldingContainer outputContainer = generator.declare(Types.REQUIRED_BIT); generator.getEvalBlock().assign(outputContainer.getValue(), JExpr.lit(1)); return outputContainer; } } else { final JInvocation setMeth = GetSetVectorHelper.write(e.getChild().getMajorType(), vv, inputContainer, outIndex, e.isSafe() ? "setSafe" : "set"); if (inputContainer.isOptional()) { JConditional jc = block._if(inputContainer.getIsSet().eq(JExpr.lit(0)).not()); block = jc._then(); } block.add(setMeth); } return null; }
@Override public HoldingContainer visitQuotedStringConstant(QuotedString e, ClassGenerator<?> generator) throws RuntimeException { MajorType majorType = Types.required(MinorType.VARCHAR); JBlock setup = generator.getBlock(BlockType.SETUP); JType holderType = generator.getHolderType(majorType); JVar var = generator.declareClassField("string", holderType); JExpression stringLiteral = JExpr.lit(e.value); JExpression buffer = generator.getMappingSet().getIncoming().invoke("getContext").invoke("getManagedBuffer"); setup.assign(var, generator.getModel().ref(ValueHolderHelper.class).staticInvoke("getVarCharHolder").arg(buffer).arg(stringLiteral)); return new HoldingContainer(majorType, var, null, null); }
@Override public HoldingContainer visitIntervalDayConstant(IntervalDayExpression e, ClassGenerator<?> generator) throws RuntimeException { MajorType majorType = Types.required(MinorType.INTERVALDAY); JBlock setup = generator.getBlock(BlockType.SETUP); JType holderType = generator.getHolderType(majorType); JVar var = generator.declareClassField("intervalday", holderType); JExpression dayLiteral = JExpr.lit(e.getIntervalDay()); JExpression millisLiteral = JExpr.lit(e.getIntervalMillis()); setup.assign( var, generator.getModel().ref(ValueHolderHelper.class).staticInvoke("getIntervalDayHolder").arg(dayLiteral) .arg(millisLiteral)); return new HoldingContainer(majorType, var, null, null); }
@Override public HoldingContainer visitDecimal28Constant(Decimal28Expression e, ClassGenerator<?> generator) throws RuntimeException { MajorType majorType = e.getMajorType(); JBlock setup = generator.getBlock(BlockType.SETUP); JType holderType = generator.getHolderType(majorType); JVar var = generator.declareClassField("dec28", holderType); JExpression stringLiteral = JExpr.lit(e.getBigDecimal().toString()); setup.assign(var, generator.getModel().ref(ValueHolderHelper.class).staticInvoke("getDecimal28Holder").arg(stringLiteral)); return new HoldingContainer(majorType, var, null, null); }