Search in sources :

Example 1 with BigintType

use of io.trino.spi.type.BigintType in project trino by trinodb.

the class JoinCompiler method generateHashPositionMethod.

private void generateHashPositionMethod(ClassDefinition classDefinition, CallSiteBinder callSiteBinder, List<Type> joinChannelTypes, List<FieldDefinition> joinChannelFields, FieldDefinition hashChannelField) {
    Parameter blockIndex = arg("blockIndex", int.class);
    Parameter blockPosition = arg("blockPosition", int.class);
    MethodDefinition hashPositionMethod = classDefinition.declareMethod(a(PUBLIC), "hashPosition", type(long.class), blockIndex, blockPosition);
    Variable thisVariable = hashPositionMethod.getThis();
    BytecodeExpression hashChannel = thisVariable.getField(hashChannelField);
    BytecodeExpression bigintType = constantType(callSiteBinder, BigintType.BIGINT);
    IfStatement ifStatement = new IfStatement();
    ifStatement.condition(notEqual(hashChannel, constantNull(hashChannelField.getType())));
    ifStatement.ifTrue(bigintType.invoke("getLong", long.class, hashChannel.invoke("get", Object.class, blockIndex).cast(Block.class), blockPosition).ret());
    hashPositionMethod.getBody().append(ifStatement);
    Variable resultVariable = hashPositionMethod.getScope().declareVariable(long.class, "result");
    hashPositionMethod.getBody().push(0L).putVariable(resultVariable);
    for (int index = 0; index < joinChannelTypes.size(); index++) {
        Type type = joinChannelTypes.get(index);
        BytecodeExpression block = hashPositionMethod.getThis().getField(joinChannelFields.get(index)).invoke("get", Object.class, blockIndex).cast(Block.class);
        hashPositionMethod.getBody().getVariable(resultVariable).push(31L).append(OpCode.LMUL).append(typeHashCode(callSiteBinder, type, block, blockPosition)).append(OpCode.LADD).putVariable(resultVariable);
    }
    hashPositionMethod.getBody().getVariable(resultVariable).retLong();
}
Also used : IfStatement(io.airlift.bytecode.control.IfStatement) SqlTypeBytecodeExpression.constantType(io.trino.sql.gen.SqlTypeBytecodeExpression.constantType) Type(io.trino.spi.type.Type) BigintType(io.trino.spi.type.BigintType) Variable(io.airlift.bytecode.Variable) MethodDefinition(io.airlift.bytecode.MethodDefinition) Parameter(io.airlift.bytecode.Parameter) BytecodeBlock(io.airlift.bytecode.BytecodeBlock) Block(io.trino.spi.block.Block) BytecodeExpression(io.airlift.bytecode.expression.BytecodeExpression)

Example 2 with BigintType

use of io.trino.spi.type.BigintType in project trino by trinodb.

the class TestHiveBucketing method toNativeContainerValue.

private static Object toNativeContainerValue(Type type, Object hiveValue) {
    if (hiveValue == null) {
        return null;
    }
    if (type instanceof ArrayType) {
        BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
        BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
        for (Object subElement : (Iterable<?>) hiveValue) {
            appendToBlockBuilder(type.getTypeParameters().get(0), subElement, subBlockBuilder);
        }
        blockBuilder.closeEntry();
        return type.getObject(blockBuilder, 0);
    }
    if (type instanceof RowType) {
        BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
        BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
        int field = 0;
        for (Object subElement : (Iterable<?>) hiveValue) {
            appendToBlockBuilder(type.getTypeParameters().get(field), subElement, subBlockBuilder);
            field++;
        }
        blockBuilder.closeEntry();
        return type.getObject(blockBuilder, 0);
    }
    if (type instanceof MapType) {
        BlockBuilder blockBuilder = type.createBlockBuilder(null, 1);
        BlockBuilder subBlockBuilder = blockBuilder.beginBlockEntry();
        for (Entry<?, ?> entry : ((Map<?, ?>) hiveValue).entrySet()) {
            appendToBlockBuilder(type.getTypeParameters().get(0), entry.getKey(), subBlockBuilder);
            appendToBlockBuilder(type.getTypeParameters().get(1), entry.getValue(), subBlockBuilder);
        }
        blockBuilder.closeEntry();
        return type.getObject(blockBuilder, 0);
    }
    if (type instanceof BooleanType) {
        return hiveValue;
    }
    if (type instanceof TinyintType) {
        return (long) (byte) hiveValue;
    }
    if (type instanceof SmallintType) {
        return (long) (short) hiveValue;
    }
    if (type instanceof IntegerType) {
        return (long) (int) hiveValue;
    }
    if (type instanceof BigintType) {
        return hiveValue;
    }
    if (type instanceof RealType) {
        return (long) Float.floatToRawIntBits((float) hiveValue);
    }
    if (type instanceof DoubleType) {
        return hiveValue;
    }
    if (type instanceof VarcharType) {
        return Slices.utf8Slice(hiveValue.toString());
    }
    if (type instanceof DateType) {
        return (long) ((Date) hiveValue).toEpochDay();
    }
    throw new IllegalArgumentException("Unsupported bucketing type: " + type);
}
Also used : VarcharType(io.trino.spi.type.VarcharType) TinyintType(io.trino.spi.type.TinyintType) BooleanType(io.trino.spi.type.BooleanType) RowType(io.trino.spi.type.RowType) RealType(io.trino.spi.type.RealType) MapType(io.trino.spi.type.MapType) BigintType(io.trino.spi.type.BigintType) ArrayType(io.trino.spi.type.ArrayType) IntegerType(io.trino.spi.type.IntegerType) DoubleType(io.trino.spi.type.DoubleType) SmallintType(io.trino.spi.type.SmallintType) Map(java.util.Map) ImmutableMap(com.google.common.collect.ImmutableMap) DateType(io.trino.spi.type.DateType) BlockBuilder(io.trino.spi.block.BlockBuilder)

Aggregations

BigintType (io.trino.spi.type.BigintType)2 ImmutableMap (com.google.common.collect.ImmutableMap)1 BytecodeBlock (io.airlift.bytecode.BytecodeBlock)1 MethodDefinition (io.airlift.bytecode.MethodDefinition)1 Parameter (io.airlift.bytecode.Parameter)1 Variable (io.airlift.bytecode.Variable)1 IfStatement (io.airlift.bytecode.control.IfStatement)1 BytecodeExpression (io.airlift.bytecode.expression.BytecodeExpression)1 Block (io.trino.spi.block.Block)1 BlockBuilder (io.trino.spi.block.BlockBuilder)1 ArrayType (io.trino.spi.type.ArrayType)1 BooleanType (io.trino.spi.type.BooleanType)1 DateType (io.trino.spi.type.DateType)1 DoubleType (io.trino.spi.type.DoubleType)1 IntegerType (io.trino.spi.type.IntegerType)1 MapType (io.trino.spi.type.MapType)1 RealType (io.trino.spi.type.RealType)1 RowType (io.trino.spi.type.RowType)1 SmallintType (io.trino.spi.type.SmallintType)1 TinyintType (io.trino.spi.type.TinyintType)1