Search in sources :

Example 26 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class RowComparisonOperator method getMethodHandles.

protected List<MethodHandle> getMethodHandles(RowType type, FunctionAndTypeManager functionAndTypeManager, OperatorType operatorType) {
    ImmutableList.Builder<MethodHandle> argumentMethods = ImmutableList.builder();
    for (Type parameterType : type.getTypeParameters()) {
        FunctionHandle operatorHandle = functionAndTypeManager.resolveOperator(operatorType, fromTypes(parameterType, parameterType));
        argumentMethods.add(functionAndTypeManager.getJavaScalarFunctionImplementation(operatorHandle).getMethodHandle());
    }
    return argumentMethods.build();
}
Also used : OperatorType(com.facebook.presto.common.function.OperatorType) Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) ImmutableList(com.google.common.collect.ImmutableList) FunctionHandle(com.facebook.presto.spi.function.FunctionHandle) MethodHandle(java.lang.invoke.MethodHandle)

Example 27 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class RowEqualOperator method equals.

public static Boolean equals(RowType rowType, List<MethodHandle> fieldEqualOperators, Block leftRow, Block rightRow) {
    boolean indeterminate = false;
    for (int fieldIndex = 0; fieldIndex < leftRow.getPositionCount(); fieldIndex++) {
        if (leftRow.isNull(fieldIndex) || rightRow.isNull(fieldIndex)) {
            indeterminate = true;
            continue;
        }
        Type fieldType = rowType.getTypeParameters().get(fieldIndex);
        Object leftField = readNativeValue(fieldType, leftRow, fieldIndex);
        Object rightField = readNativeValue(fieldType, rightRow, fieldIndex);
        try {
            MethodHandle equalOperator = fieldEqualOperators.get(fieldIndex);
            Boolean result = (Boolean) equalOperator.invoke(leftField, rightField);
            if (result == null) {
                indeterminate = true;
            } else if (!result) {
                return false;
            }
        } catch (Throwable t) {
            throw internalError(t);
        }
    }
    if (indeterminate) {
        return null;
    }
    return true;
}
Also used : Type(com.facebook.presto.common.type.Type) RowType(com.facebook.presto.common.type.RowType) MethodHandle(java.lang.invoke.MethodHandle)

Example 28 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class ZipFunction method zip.

@UsedByGeneratedCode
public static Block zip(List<Type> types, Block... arrays) {
    int biggestCardinality = 0;
    for (Block array : arrays) {
        biggestCardinality = Math.max(biggestCardinality, array.getPositionCount());
    }
    RowType rowType = RowType.anonymous(types);
    BlockBuilder outputBuilder = rowType.createBlockBuilder(null, biggestCardinality);
    for (int outputPosition = 0; outputPosition < biggestCardinality; outputPosition++) {
        BlockBuilder rowBuilder = outputBuilder.beginBlockEntry();
        for (int fieldIndex = 0; fieldIndex < arrays.length; fieldIndex++) {
            if (arrays[fieldIndex].getPositionCount() <= outputPosition) {
                rowBuilder.appendNull();
            } else {
                types.get(fieldIndex).appendTo(arrays[fieldIndex], outputPosition, rowBuilder);
            }
        }
        outputBuilder.closeEntry();
    }
    return outputBuilder.build();
}
Also used : Block(com.facebook.presto.common.block.Block) RowType(com.facebook.presto.common.type.RowType) BlockBuilder(com.facebook.presto.common.block.BlockBuilder) UsedByGeneratedCode(com.facebook.presto.annotation.UsedByGeneratedCode)

Example 29 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class TypeCoercer method typeCompatibilityForRow.

private TypeCompatibility typeCompatibilityForRow(RowType firstType, RowType secondType) {
    List<RowType.Field> firstFields = firstType.getFields();
    List<RowType.Field> secondFields = secondType.getFields();
    if (firstFields.size() != secondFields.size()) {
        return TypeCompatibility.incompatible();
    }
    ImmutableList.Builder<RowType.Field> fields = ImmutableList.builder();
    boolean coercible = true;
    for (int i = 0; i < firstFields.size(); i++) {
        Type firstFieldType = firstFields.get(i).getType();
        Type secondFieldType = secondFields.get(i).getType();
        TypeCompatibility typeCompatibility = compatibility(firstFieldType, secondFieldType);
        if (!typeCompatibility.isCompatible()) {
            return TypeCompatibility.incompatible();
        }
        Type commonParameterType = typeCompatibility.getCommonSuperType();
        Optional<String> firstParameterName = firstFields.get(i).getName();
        Optional<String> secondParameterName = secondFields.get(i).getName();
        Optional<String> commonName = firstParameterName.equals(secondParameterName) ? firstParameterName : Optional.empty();
        // ignore parameter name for coercible
        coercible &= typeCompatibility.isCoercible();
        fields.add(new RowType.Field(commonName, commonParameterType));
    }
    return TypeCompatibility.compatible(RowType.from(fields.build()), coercible);
}
Also used : CharType.createCharType(com.facebook.presto.common.type.CharType.createCharType) MapType(com.facebook.presto.common.type.MapType) DecimalType(com.facebook.presto.common.type.DecimalType) VarcharType.createUnboundedVarcharType(com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType) KHyperLogLogType(com.facebook.presto.type.khyperloglog.KHyperLogLogType) SetDigestType(com.facebook.presto.type.setdigest.SetDigestType) VarcharType.createVarcharType(com.facebook.presto.common.type.VarcharType.createVarcharType) ArrayType(com.facebook.presto.common.type.ArrayType) CharType(com.facebook.presto.common.type.CharType) UnknownType(com.facebook.presto.common.type.UnknownType) Type(com.facebook.presto.common.type.Type) VarcharType(com.facebook.presto.common.type.VarcharType) RowType(com.facebook.presto.common.type.RowType) DecimalType.createDecimalType(com.facebook.presto.common.type.DecimalType.createDecimalType) ImmutableList(com.google.common.collect.ImmutableList) RowType(com.facebook.presto.common.type.RowType)

Example 30 with RowType

use of com.facebook.presto.common.type.RowType in project presto by prestodb.

the class RowExpressionVerifier method visitDereferenceExpression.

@Override
protected Boolean visitDereferenceExpression(DereferenceExpression expected, RowExpression actual) {
    if (!(actual instanceof SpecialFormExpression) || !(((SpecialFormExpression) actual).getForm().equals(DEREFERENCE))) {
        return false;
    }
    SpecialFormExpression actualDereference = (SpecialFormExpression) actual;
    if (actualDereference.getArguments().size() == 2 && actualDereference.getArguments().get(0).getType() instanceof RowType && actualDereference.getArguments().get(1) instanceof ConstantExpression) {
        RowType rowType = (RowType) actualDereference.getArguments().get(0).getType();
        Object value = LiteralInterpreter.evaluate(TEST_SESSION.toConnectorSession(), (ConstantExpression) actualDereference.getArguments().get(1));
        checkState(value instanceof Long);
        long index = (Long) value;
        checkState(index >= 0 && index < rowType.getFields().size());
        RowType.Field field = rowType.getFields().get(toIntExact(index));
        checkState(field.getName().isPresent());
        return expected.getField().getValue().equals(field.getName().get()) && process(expected.getBase(), actualDereference.getArguments().get(0));
    }
    return false;
}
Also used : ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowType(com.facebook.presto.common.type.RowType) SpecialFormExpression(com.facebook.presto.spi.relation.SpecialFormExpression)

Aggregations

RowType (com.facebook.presto.common.type.RowType)61 ArrayType (com.facebook.presto.common.type.ArrayType)37 Type (com.facebook.presto.common.type.Type)32 MapType (com.facebook.presto.common.type.MapType)28 ImmutableList (com.google.common.collect.ImmutableList)19 ArrayList (java.util.ArrayList)18 DecimalType (com.facebook.presto.common.type.DecimalType)16 BlockBuilder (com.facebook.presto.common.block.BlockBuilder)15 Test (org.testng.annotations.Test)15 List (java.util.List)14 VarcharType (com.facebook.presto.common.type.VarcharType)12 Block (com.facebook.presto.common.block.Block)11 CharType (com.facebook.presto.common.type.CharType)9 ImmutableList.toImmutableList (com.google.common.collect.ImmutableList.toImmutableList)9 PrestoException (com.facebook.presto.spi.PrestoException)8 Map (java.util.Map)8 ImmutableMap (com.google.common.collect.ImmutableMap)7 VarcharType.createUnboundedVarcharType (com.facebook.presto.common.type.VarcharType.createUnboundedVarcharType)6 ColumnMetadata (com.facebook.presto.spi.ColumnMetadata)6 TimestampType (com.facebook.presto.common.type.TimestampType)5