body.append(blockBuilder.set(constantType(binder, toType).invoke("createBlockBuilder", BlockBuilder.class, constantNull(BlockBuilderStatus.class), array.invoke("getPositionCount", int.class)))); Variable element = scope.declareVariable(fromType.getJavaType(), "element_" + NEXT_VARIABLE_ID.getAndIncrement()); Variable newElement = scope.declareVariable(toType.getJavaType(), "newElement_" + NEXT_VARIABLE_ID.getAndIncrement()); SqlTypeBytecodeExpression elementTypeConstant = constantType(binder, fromType); SqlTypeBytecodeExpression newElementTypeConstant = constantType(binder, toType); mapElement = new BytecodeBlock() .append(element.set(elementTypeConstant.getValue(array, position))) .append(newElement.set(mapper.apply(element))) .append(newElementTypeConstant.writeValue(blockBuilder, newElement)); mapperDescription = mapper.apply(element).toString();
public static SqlTypeBytecodeExpression constantType(CallSiteBinder callSiteBinder, Type type) { requireNonNull(callSiteBinder, "callSiteBinder is null"); requireNonNull(type, "type is null"); Binding binding = callSiteBinder.bind(type, Type.class); return new SqlTypeBytecodeExpression(type, binding, BOOTSTRAP_METHOD); }
private InputReferenceNode(CallSiteBinder callSiteBinder, Scope scope, Type type, BytecodeExpression block, BytecodeExpression position) { // Generate body based on block and position Variable wasNullVariable = scope.getVariable("wasNull"); Class<?> javaType = type.getJavaType(); if (!javaType.isPrimitive() && javaType != Slice.class) { javaType = Object.class; } IfStatement ifStatement = new IfStatement(); ifStatement.condition(block.invoke("isNull", boolean.class, position)); ifStatement.ifTrue() .putVariable(wasNullVariable, true) .pushJavaDefault(javaType); String methodName = "get" + Primitives.wrap(javaType).getSimpleName(); ifStatement.ifFalse(constantType(callSiteBinder, type).invoke(methodName, javaType, block, position)); this.body = ifStatement; this.block = block; this.position = position; }
constantType(binder, elementType).invoke("createBlockBuilder", BlockBuilder.class, constantNull(BlockBuilderStatus.class), constantInt(stackTypes.size()))); body.append(createBlockBuilder); .condition(equal(argument, constantNull(stackTypes.get(i)))) .ifTrue(blockBuilderVariable.invoke("appendNull", BlockBuilder.class).pop()) .ifFalse(constantType(binder, elementType).writeValue(blockBuilderVariable, argument.cast(elementType.getJavaType()))); body.append(ifStatement);
private static MethodDefinition generateGetIntermediateType(ClassDefinition definition, CallSiteBinder callSiteBinder, List<Type> type) { MethodDefinition methodDefinition = definition.declareMethod(a(PUBLIC), "getIntermediateType", type(Type.class)); if (type.size() == 1) { methodDefinition.getBody() .append(constantType(callSiteBinder, getOnlyElement(type))) .retObject(); } else { methodDefinition.getBody() .append(constantType(callSiteBinder, RowType.anonymous(type))) .retObject(); } return methodDefinition; }
.condition(block.invoke("isNull", boolean.class, index)) .ifTrue(state.cast(setter.getDeclaringClass()).invoke(setter, constantNull(field.getType()))) .ifFalse(state.cast(setter.getDeclaringClass()).invoke(setter, constantType(binder, field.getSqlType()).getValue(block, index)))); state.cast(setter.getDeclaringClass()).invoke( setter, constantType(binder, field.getSqlType()).getValue(block, index).cast(field.getType()))); .condition(row.invoke("isNull", boolean.class, constantInt(position))) .ifTrue(state.cast(setter.getDeclaringClass()).invoke(setter, constantNull(field.getType()))) .ifFalse(state.cast(setter.getDeclaringClass()).invoke(setter, constantType(binder, field.getSqlType()).getValue(row, constantInt(position))))); state.cast(setter.getDeclaringClass()).invoke( setter, constantType(binder, field.getSqlType()).getValue(row, constantInt(position)).cast(field.getType())));
private void generateOutputMethod(ClassDefinition definition, CallSiteBinder binder, Type valueType, Class<?> stateClass) { Parameter state = arg("state", stateClass); Parameter out = arg("out", BlockBuilder.class); MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "output", type(void.class), state, out); IfStatement ifStatement = new IfStatement() .condition(or(state.invoke("isFirstNull", boolean.class), state.invoke("isSecondNull", boolean.class))) .ifTrue(new BytecodeBlock().append(out.invoke("appendNull", BlockBuilder.class)).pop()); SqlTypeBytecodeExpression valueSqlType = constantType(binder, valueType); BytecodeExpression getValueExpression; if (valueType.getJavaType().isPrimitive()) { getValueExpression = state.invoke("getSecond", valueType.getJavaType()); } else { getValueExpression = valueSqlType.getValue(state.invoke("getSecondBlock", Block.class), state.invoke("getSecondPosition", int.class)); } ifStatement.ifFalse(valueSqlType.writeValue(out, getValueExpression)); method.getBody().append(ifStatement).ret(); } }
body.append(singleMapBlockWriter.set(mapBlockBuilder.invoke("beginBlockEntry", BlockBuilder.class))); SqlTypeBytecodeExpression keySqlType = constantType(binder, keyType); BytecodeNode loadKeyElement; if (!keyType.equals(UNKNOWN)) { loadKeyElement = new BytecodeBlock().append(keyElement.set(keySqlType.getValue(block, position).cast(keyJavaType))); SqlTypeBytecodeExpression valueSqlType = constantType(binder, valueType); BytecodeNode loadValueElement; if (!valueType.equals(UNKNOWN)) { .condition(block.invoke("isNull", boolean.class, add(position, constantInt(1)))) .ifTrue(valueElement.set(constantNull(valueJavaType))) .ifFalse(valueElement.set(valueSqlType.getValue(block, add(position, constantInt(1))).cast(valueJavaType))); .condition(and(notEqual(keep, constantNull(Boolean.class)), keep.cast(boolean.class))) .ifTrue(new BytecodeBlock() .append(keySqlType.invoke("appendTo", void.class, block, position, singleMapBlockWriter)) .append(valueSqlType.invoke("appendTo", void.class, block, add(position, constantInt(1)), singleMapBlockWriter)))))); .pop()); body.append(pageBuilder.invoke("declarePosition", void.class)); body.append(constantType(binder, mapType) .invoke( "getObject", Object.class,
SqlTypeBytecodeExpression sqlType = constantType(binder, getOnlyElement(fields).getSqlType()); Variable fieldValue = scope.declareVariable(getter.getReturnType(), "value"); serializerBody.append(fieldValue.set(state.cast(getter.getDeclaringClass()).invoke(getter))); .condition(equal(fieldValue, constantNull(getter.getReturnType()))) .ifTrue(out.invoke("appendNull", BlockBuilder.class).pop()) .ifFalse(sqlType.writeValue(out, fieldValue))); serializerBody.append(sqlType.writeValue(out, fieldValue.cast(getOnlyElement(fields).getSqlType().getJavaType()))); for (StateField field : fields) { Method getter = getGetter(clazz, field); SqlTypeBytecodeExpression sqlType = constantType(binder, field.getSqlType()); Variable fieldValue = scope.createTempVariable(getter.getReturnType()); serializerBody.append(fieldValue.set(state.cast(getter.getDeclaringClass()).invoke(getter))); serializerBody.append(new IfStatement().condition(equal(fieldValue, constantNull(getter.getReturnType()))) .ifTrue(rowBuilder.invoke("appendNull", BlockBuilder.class).pop()) .ifFalse(sqlType.writeValue(rowBuilder, fieldValue))); serializerBody.append(sqlType.writeValue(rowBuilder, fieldValue.cast(field.getSqlType().getJavaType())));
public BytecodeExpression getValue(BytecodeExpression block, BytecodeExpression position) { Class<?> fromJavaElementType = type.getJavaType(); if (fromJavaElementType == boolean.class) { return invoke("getBoolean", boolean.class, block, position); } if (fromJavaElementType == long.class) { return invoke("getLong", long.class, block, position); } if (fromJavaElementType == double.class) { return invoke("getDouble", double.class, block, position); } if (fromJavaElementType == Slice.class) { return invoke("getSlice", Slice.class, block, position); } return invoke("getObject", Object.class, block, position).cast(fromJavaElementType); }
private static void generateGetSerializedType(ClassDefinition definition, List<StateField> fields, CallSiteBinder callSiteBinder) { BytecodeBlock body = definition.declareMethod(a(PUBLIC), "getSerializedType", type(Type.class)).getBody(); Type type; if (fields.size() > 1) { List<Type> types = fields.stream().map(StateField::getSqlType).collect(toImmutableList()); type = RowType.anonymous(types); } else if (fields.size() == 1) { type = getOnlyElement(fields).getSqlType(); } else { type = UNKNOWN; } body.comment("return %s", type.getTypeSignature()) .append(constantType(callSiteBinder, type)) .retObject(); }
Variable singleRowBlockWriter = scope.createTempVariable(BlockBuilder.class); block.append(blockBuilder.set( constantType(binder, rowType).invoke( "createBlockBuilder", BlockBuilder.class, .condition(context.wasNull()) .ifTrue(singleRowBlockWriter.invoke("appendNull", BlockBuilder.class).pop()) .ifFalse(constantType(binder, fieldType).writeValue(singleRowBlockWriter, field).pop())); block.append(constantType(binder, rowType).invoke("getObject", Object.class, blockBuilder.cast(Block.class), constantInt(0)) .cast(Block.class)); block.append(context.wasNull().set(constantFalse()));
Parameter position = arg("position", int.class); MethodDefinition method = definition.declareMethod(a(PUBLIC, STATIC), "input", type(void.class), state, value, key, position); SqlTypeBytecodeExpression keySqlType = constantType(binder, keyType); .append(state.invoke("setFirst", void.class, keySqlType.getValue(key, position))) .append(state.invoke("setFirstNull", void.class, constantBoolean(false))) .append(state.invoke("setSecondNull", void.class, value.invoke("isNull", boolean.class, position))); BytecodeNode setValueNode; if (valueType.getJavaType().isPrimitive()) { SqlTypeBytecodeExpression valueSqlType = constantType(binder, valueType); setValueNode = state.invoke("setSecond", void.class, valueSqlType.getValue(value, position)); and( not(key.invoke("isNull", boolean.class, position)), loadConstant(binder, compareMethod, MethodHandle.class).invoke("invokeExact", boolean.class, keySqlType.getValue(key, position), state.invoke("getFirst", keyType.getJavaType()))))) .ifTrue(ifBlock)) .ret();
private void generateAppendToMethod( ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> types, List<FieldDefinition> blockFields, FieldDefinition positionField) { Parameter pageBuilder = arg("pageBuilder", PageBuilder.class); MethodDefinition method = classDefinition.declareMethod( a(PUBLIC), "appendTo", type(void.class), pageBuilder); Variable thisVariable = method.getThis(); for (int index = 0; index < blockFields.size(); index++) { Type type = types.get(index); method.getBody() .comment("%s.appendTo(block_%s, position, pageBuilder.getBlockBuilder(%s));", type.getClass(), index, index) .append(constantType(callSiteBinder, type).invoke("appendTo", void.class, thisVariable.getField(blockFields.get(index)), thisVariable.getField(positionField), pageBuilder.invoke("getBlockBuilder", BlockBuilder.class, constantInt(index)))); } method.getBody() .ret(); }
public BytecodeExpression writeValue(BytecodeExpression blockBuilder, BytecodeExpression value) { Class<?> fromJavaElementType = type.getJavaType(); if (fromJavaElementType == boolean.class) { return invoke("writeBoolean", void.class, blockBuilder, value); } if (fromJavaElementType == long.class) { return invoke("writeLong", void.class, blockBuilder, value); } if (fromJavaElementType == double.class) { return invoke("writeDouble", void.class, blockBuilder, value); } if (fromJavaElementType == Slice.class) { return invoke("writeSlice", void.class, blockBuilder, value); } return invoke("writeObject", void.class, blockBuilder, value.cast(Object.class)); } }
constantType(binder, toType).invoke( "createBlockBuilder", BlockBuilder.class, continue; BytecodeExpression fromElement = constantType(binder, currentFromType).getValue(value, constantInt(i)); BytecodeExpression toElement = invokeFunction(scope, cachedInstanceBinder, signature.getName(), function, fromElement); IfStatement ifElementNull = new IfStatement("if the element in the row type is null..."); .ifFalse(constantType(binder, toTypes.get(i)).writeValue(singleRowBlockWriter, toElement)); body.append(constantType(binder, toType) .invoke("getObject", Object.class, blockBuilder.cast(Block.class), constantInt(0)) .cast(Block.class) .ret());
private static MethodDefinition generateGetFinalType(ClassDefinition definition, CallSiteBinder callSiteBinder, Type type) { MethodDefinition methodDefinition = definition.declareMethod(a(PUBLIC), "getFinalType", type(Type.class)); methodDefinition.getBody() .append(constantType(callSiteBinder, type)) .retObject(); return methodDefinition; }
constantType(binder, elementType).invoke("createBlockBuilder", BlockBuilder.class, newInstance(BlockBuilderStatus.class), constantInt(stackTypes.size()))); body.append(createBlockBuilder); .condition(equal(argument, constantNull(stackTypes.get(i)))) .ifTrue(blockBuilderVariable.invoke("appendNull", BlockBuilder.class).pop()) .ifFalse(constantType(binder, elementType).writeValue(blockBuilderVariable, argument.cast(elementType.getJavaType()))); body.append(ifStatement);
.pushJavaDefault(javaType); BytecodeExpression value = constantType(callSiteBinder, returnType).getValue(rowBlock, constantInt(index));
position, page, constantType(callSiteBinder, BigintType.BIGINT).invoke("getLong", long.class, probeHashBlock,