Search in sources :

Example 1 with IntegerType

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

the class InCodeGenerator method checkSwitchGenerationCase.

@VisibleForTesting
static SwitchGenerationCase checkSwitchGenerationCase(Type type, List<RowExpression> values) {
    if (values.size() > 32) {
        // * Benchmark shows performance of SET_CONTAINS is better at 50, but similar at 25.
        return SwitchGenerationCase.SET_CONTAINS;
    }
    if (!(type instanceof IntegerType || type instanceof BigintType || type instanceof DateType)) {
        return SwitchGenerationCase.HASH_SWITCH;
    }
    for (RowExpression expression : values) {
        // Same argument applies for nulls.
        if (!(expression instanceof ConstantExpression)) {
            continue;
        }
        Object constant = ((ConstantExpression) expression).getValue();
        if (constant == null) {
            continue;
        }
        long longConstant = ((Number) constant).longValue();
        if (longConstant < Integer.MIN_VALUE || longConstant > Integer.MAX_VALUE) {
            return SwitchGenerationCase.HASH_SWITCH;
        }
    }
    return SwitchGenerationCase.DIRECT_SWITCH;
}
Also used : IntegerType(com.facebook.presto.common.type.IntegerType) ConstantExpression(com.facebook.presto.spi.relation.ConstantExpression) RowExpression(com.facebook.presto.spi.relation.RowExpression) DateType(com.facebook.presto.common.type.DateType) BigintType(com.facebook.presto.common.type.BigintType) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 2 with IntegerType

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

the class TestPinotSegmentPageSource method testMultivaluedType.

@Test
public void testMultivaluedType() throws IOException {
    String[] columnNames = { "col1", "col2" };
    DataSchema.ColumnDataType[] columnDataTypes = { DataSchema.ColumnDataType.INT_ARRAY, DataSchema.ColumnDataType.STRING_ARRAY };
    DataSchema dataSchema = new DataSchema(columnNames, columnDataTypes);
    String[] stringArray = { "stringVal1", "stringVal2" };
    int[] intArray = { 10, 34, 67 };
    DataTableBuilder dataTableBuilder = new DataTableBuilder(dataSchema);
    dataTableBuilder.startRow();
    dataTableBuilder.setColumn(0, intArray);
    dataTableBuilder.setColumn(1, stringArray);
    dataTableBuilder.finishRow();
    DataTable dataTable = dataTableBuilder.build();
    PinotSessionProperties pinotSessionProperties = new PinotSessionProperties(pinotConfig);
    ConnectorSession session = new TestingConnectorSession(pinotSessionProperties.getSessionProperties());
    List<PinotColumnHandle> pinotColumnHandles = ImmutableList.of(new PinotColumnHandle(columnNames[0], PinotColumnUtils.getPrestoTypeFromPinotType(getFieldSpec(columnNames[0], columnDataTypes[0]), false, false), PinotColumnHandle.PinotColumnType.REGULAR), new PinotColumnHandle(columnNames[1], PinotColumnUtils.getPrestoTypeFromPinotType(getFieldSpec(columnNames[1], columnDataTypes[1]), false, false), PinotColumnHandle.PinotColumnType.REGULAR));
    PinotSplit mockPinotSplit = new PinotSplit(pinotConnectorId.toString(), PinotSplit.SplitType.SEGMENT, pinotColumnHandles, Optional.empty(), Optional.of("blah"), ImmutableList.of("seg"), Optional.of("host"), getGrpcPort());
    PinotSegmentPageSource pinotSegmentPageSource = getPinotSegmentPageSource(session, ImmutableList.of(dataTable), mockPinotSplit, pinotColumnHandles);
    Page page = requireNonNull(pinotSegmentPageSource.getNextPage(), "Expected a valid page");
    for (int i = 0; i < columnDataTypes.length; i++) {
        Block block = page.getBlock(i);
        Type type = PinotColumnUtils.getPrestoTypeFromPinotType(getFieldSpec(columnNames[i], columnDataTypes[i]), false, false);
        Assert.assertTrue(type instanceof ArrayType, "presto type should be array");
        if (((ArrayType) type).getElementType() instanceof IntegerType) {
            Assert.assertTrue(block.getBlock(0).getInt(0) == 10, "Array element not matching");
            Assert.assertTrue(block.getBlock(0).getInt(1) == 34, "Array element not matching");
            Assert.assertTrue(block.getBlock(0).getInt(2) == 67, "Array element not matching");
        } else if (((ArrayType) type).getElementType() instanceof VariableWidthType) {
            Type type1 = ((ArrayType) type).getElementType();
            Assert.assertTrue(block.getBlock(0) instanceof VariableWidthBlock);
            VariableWidthBlock variableWidthBlock = (VariableWidthBlock) block.getBlock(0);
            Assert.assertTrue("stringVal1".equals(new String(variableWidthBlock.getSlice(0, 0, variableWidthBlock.getSliceLength(0)).getBytes())), "Array element not matching");
            Assert.assertTrue("stringVal2".equals(new String(variableWidthBlock.getSlice(1, 0, variableWidthBlock.getSliceLength(1)).getBytes())), "Array element not matching");
        }
    }
}
Also used : DataTable(org.apache.pinot.common.utils.DataTable) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) Page(com.facebook.presto.common.Page) VariableWidthBlock(com.facebook.presto.common.block.VariableWidthBlock) DataSchema(org.apache.pinot.common.utils.DataSchema) ArrayType(com.facebook.presto.common.type.ArrayType) IntegerType(com.facebook.presto.common.type.IntegerType) VariableWidthType(com.facebook.presto.common.type.VariableWidthType) IntegerType(com.facebook.presto.common.type.IntegerType) ArrayType(com.facebook.presto.common.type.ArrayType) Type(com.facebook.presto.common.type.Type) VariableWidthBlock(com.facebook.presto.common.block.VariableWidthBlock) Block(com.facebook.presto.common.block.Block) TestingConnectorSession(com.facebook.presto.testing.TestingConnectorSession) ConnectorSession(com.facebook.presto.spi.ConnectorSession) VariableWidthType(com.facebook.presto.common.type.VariableWidthType) DataTableBuilder(org.apache.pinot.core.common.datatable.DataTableBuilder) Test(org.testng.annotations.Test)

Example 3 with IntegerType

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

the class PinotBrokerPageSourceBase method setValue.

protected void setValue(Type type, BlockBuilder blockBuilder, String value) {
    if (blockBuilder == null) {
        return;
    }
    if (value == null) {
        blockBuilder.appendNull();
        return;
    }
    if (!(type instanceof FixedWidthType) && !(type instanceof VarcharType) && !(type instanceof JsonType)) {
        throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "type '" + type + "' not supported");
    }
    if (type instanceof FixedWidthType) {
        completedBytes += ((FixedWidthType) type).getFixedSize();
        if (type instanceof BigintType) {
            type.writeLong(blockBuilder, parseDouble(value).longValue());
        } else if (type instanceof IntegerType) {
            blockBuilder.writeInt(parseDouble(value).intValue());
        } else if (type instanceof TinyintType) {
            blockBuilder.writeByte(parseDouble(value).byteValue());
        } else if (type instanceof SmallintType) {
            blockBuilder.writeShort(parseDouble(value).shortValue());
        } else if (type instanceof BooleanType) {
            type.writeBoolean(blockBuilder, parseBoolean(value));
        } else if (type instanceof DecimalType || type instanceof DoubleType) {
            type.writeDouble(blockBuilder, parseDouble(value));
        } else if (type instanceof TimestampType) {
            type.writeLong(blockBuilder, parseTimestamp(value));
        } else if (type instanceof DateType) {
            type.writeLong(blockBuilder, parseLong(value));
        } else {
            throw new PinotException(PINOT_UNSUPPORTED_COLUMN_TYPE, Optional.empty(), "type '" + type + "' not supported");
        }
    } else {
        Slice slice = Slices.utf8Slice(value);
        blockBuilder.writeBytes(slice, 0, slice.length()).closeEntry();
        completedBytes += slice.length();
    }
}
Also used : JsonType(com.facebook.presto.common.type.JsonType) VarcharType(com.facebook.presto.common.type.VarcharType) TinyintType(com.facebook.presto.common.type.TinyintType) BooleanType(com.facebook.presto.common.type.BooleanType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) DoubleType(com.facebook.presto.common.type.DoubleType) Slice(io.airlift.slice.Slice) DecimalType(com.facebook.presto.common.type.DecimalType) TimestampType(com.facebook.presto.common.type.TimestampType) SmallintType(com.facebook.presto.common.type.SmallintType) DateType(com.facebook.presto.common.type.DateType) FixedWidthType(com.facebook.presto.common.type.FixedWidthType)

Example 4 with IntegerType

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

the class LiteralInterpreter method evaluate.

public static Object evaluate(ConnectorSession session, ConstantExpression node) {
    Type type = node.getType();
    SqlFunctionProperties properties = session.getSqlFunctionProperties();
    if (node.getValue() == null) {
        return null;
    }
    if (type instanceof BooleanType) {
        return node.getValue();
    }
    if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
        return node.getValue();
    }
    if (type instanceof DoubleType) {
        return node.getValue();
    }
    if (type instanceof RealType) {
        Long number = (Long) node.getValue();
        return intBitsToFloat(number.intValue());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            checkState(node.getValue() instanceof Long);
            return decodeDecimal(BigInteger.valueOf((long) node.getValue()), decimalType);
        }
        checkState(node.getValue() instanceof Slice);
        Slice value = (Slice) node.getValue();
        return decodeDecimal(decodeUnscaledValue(value), decimalType);
    }
    if (type instanceof VarcharType || type instanceof CharType) {
        return ((Slice) node.getValue()).toStringUtf8();
    }
    if (type instanceof VarbinaryType) {
        return new SqlVarbinary(((Slice) node.getValue()).getBytes());
    }
    if (type instanceof DateType) {
        return new SqlDate(((Long) node.getValue()).intValue());
    }
    if (type instanceof TimeType) {
        if (properties.isLegacyTimestamp()) {
            return new SqlTime((long) node.getValue(), properties.getTimeZoneKey());
        }
        return new SqlTime((long) node.getValue());
    }
    if (type instanceof TimestampType) {
        try {
            if (properties.isLegacyTimestamp()) {
                return new SqlTimestamp((long) node.getValue(), properties.getTimeZoneKey());
            }
            return new SqlTimestamp((long) node.getValue());
        } catch (RuntimeException e) {
            throw new PrestoException(GENERIC_USER_ERROR, format("'%s' is not a valid timestamp literal", (String) node.getValue()));
        }
    }
    if (type instanceof IntervalDayTimeType) {
        return new SqlIntervalDayTime((long) node.getValue());
    }
    if (type instanceof IntervalYearMonthType) {
        return new SqlIntervalYearMonth(((Long) node.getValue()).intValue());
    }
    if (type.getJavaType().equals(Slice.class)) {
        // DO NOT ever remove toBase64. Calling toString directly on Slice whose base is not byte[] will cause JVM to crash.
        return "'" + VarbinaryFunctions.toBase64((Slice) node.getValue()).toStringUtf8() + "'";
    }
    // We should not fail at the moment; just return the raw value (block, regex, etc) to the user
    return node.getValue();
}
Also used : IntervalYearMonthType(com.facebook.presto.type.IntervalYearMonthType) VarcharType(com.facebook.presto.common.type.VarcharType) TinyintType(com.facebook.presto.common.type.TinyintType) SqlVarbinary(com.facebook.presto.common.type.SqlVarbinary) SqlTime(com.facebook.presto.common.type.SqlTime) SqlIntervalYearMonth(com.facebook.presto.type.SqlIntervalYearMonth) SqlTimestamp(com.facebook.presto.common.type.SqlTimestamp) PrestoException(com.facebook.presto.spi.PrestoException) RealType(com.facebook.presto.common.type.RealType) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) TimeType(com.facebook.presto.common.type.TimeType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) SmallintType(com.facebook.presto.common.type.SmallintType) DateType(com.facebook.presto.common.type.DateType) SqlFunctionProperties(com.facebook.presto.common.function.SqlFunctionProperties) BooleanType(com.facebook.presto.common.type.BooleanType) BigintType(com.facebook.presto.common.type.BigintType) IntegerType(com.facebook.presto.common.type.IntegerType) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) TinyintType(com.facebook.presto.common.type.TinyintType) BigintType(com.facebook.presto.common.type.BigintType) IntervalYearMonthType(com.facebook.presto.type.IntervalYearMonthType) VarcharType(com.facebook.presto.common.type.VarcharType) RealType(com.facebook.presto.common.type.RealType) SmallintType(com.facebook.presto.common.type.SmallintType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) BooleanType(com.facebook.presto.common.type.BooleanType) IntegerType(com.facebook.presto.common.type.IntegerType) CharType(com.facebook.presto.common.type.CharType) Type(com.facebook.presto.common.type.Type) TimeType(com.facebook.presto.common.type.TimeType) DateType(com.facebook.presto.common.type.DateType) DoubleType(com.facebook.presto.common.type.DoubleType) DoubleType(com.facebook.presto.common.type.DoubleType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) SqlDate(com.facebook.presto.common.type.SqlDate) SqlIntervalDayTime(com.facebook.presto.type.SqlIntervalDayTime) IntervalDayTimeType(com.facebook.presto.type.IntervalDayTimeType) DecimalType(com.facebook.presto.common.type.DecimalType) CharType(com.facebook.presto.common.type.CharType)

Example 5 with IntegerType

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

the class ExpressionConverter method getIcebergLiteralValue.

private static Object getIcebergLiteralValue(Type type, Marker marker) {
    if (type instanceof IntegerType) {
        return toIntExact((long) marker.getValue());
    }
    if (type instanceof RealType) {
        return intBitsToFloat(toIntExact((long) marker.getValue()));
    }
    // TODO: Remove this conversion once we move to next iceberg version
    if (type instanceof DateType) {
        return toIntExact(((Long) marker.getValue()));
    }
    if (type instanceof TimestampType || type instanceof TimeType) {
        return MILLISECONDS.toMicros((Long) marker.getValue());
    }
    if (type instanceof VarcharType) {
        return ((Slice) marker.getValue()).toStringUtf8();
    }
    if (type instanceof VarbinaryType) {
        return ByteBuffer.wrap(((Slice) marker.getValue()).getBytes());
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        Object value = requireNonNull(marker.getValue(), "The value of the marker must be non-null");
        if (Decimals.isShortDecimal(decimalType)) {
            checkArgument(value instanceof Long, "A short decimal should be represented by a Long value but was %s", value.getClass().getName());
            return BigDecimal.valueOf((long) value).movePointLeft(decimalType.getScale());
        }
        checkArgument(value instanceof Slice, "A long decimal should be represented by a Slice value but was %s", value.getClass().getName());
        return new BigDecimal(Decimals.decodeUnscaledValue((Slice) value), decimalType.getScale());
    }
    return marker.getValue();
}
Also used : IntegerType(com.facebook.presto.common.type.IntegerType) VarbinaryType(com.facebook.presto.common.type.VarbinaryType) VarcharType(com.facebook.presto.common.type.VarcharType) Slice(io.airlift.slice.Slice) TimestampType(com.facebook.presto.common.type.TimestampType) DecimalType(com.facebook.presto.common.type.DecimalType) RealType(com.facebook.presto.common.type.RealType) DateType(com.facebook.presto.common.type.DateType) BigDecimal(java.math.BigDecimal) TimeType(com.facebook.presto.common.type.TimeType)

Aggregations

IntegerType (com.facebook.presto.common.type.IntegerType)9 BigintType (com.facebook.presto.common.type.BigintType)7 DateType (com.facebook.presto.common.type.DateType)7 DecimalType (com.facebook.presto.common.type.DecimalType)7 TimestampType (com.facebook.presto.common.type.TimestampType)7 VarcharType (com.facebook.presto.common.type.VarcharType)7 BooleanType (com.facebook.presto.common.type.BooleanType)6 DoubleType (com.facebook.presto.common.type.DoubleType)6 RealType (com.facebook.presto.common.type.RealType)6 SmallintType (com.facebook.presto.common.type.SmallintType)6 TinyintType (com.facebook.presto.common.type.TinyintType)6 Slice (io.airlift.slice.Slice)6 CharType (com.facebook.presto.common.type.CharType)5 Type (com.facebook.presto.common.type.Type)5 VarbinaryType (com.facebook.presto.common.type.VarbinaryType)3 PrestoException (com.facebook.presto.spi.PrestoException)3 ArrayType (com.facebook.presto.common.type.ArrayType)2 TimeType (com.facebook.presto.common.type.TimeType)2 TimestampWithTimeZoneType (com.facebook.presto.common.type.TimestampWithTimeZoneType)2 Page (com.facebook.presto.common.Page)1