io.prestosql.spi.type.VarcharType Java Examples

The following examples show how to use io.prestosql.spi.type.VarcharType. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example #1
Source File: SessionPropertyManager.java    From presto with Apache License 2.0 6 votes vote down vote up
private static Class<?> getMapKeyType(Type type)
{
    if (VarcharType.VARCHAR.equals(type)) {
        return String.class;
    }
    if (BooleanType.BOOLEAN.equals(type)) {
        return Boolean.class;
    }
    if (BigintType.BIGINT.equals(type)) {
        return Long.class;
    }
    if (IntegerType.INTEGER.equals(type)) {
        return Integer.class;
    }
    if (DoubleType.DOUBLE.equals(type)) {
        return Double.class;
    }
    throw new PrestoException(INVALID_SESSION_PROPERTY, format("Session property map key type %s is not supported", type));
}
 
Example #2
Source File: TestRawDecoder.java    From presto with Apache License 2.0 6 votes vote down vote up
private void checkTwice(Map<DecoderColumnHandle, FieldValueProvider> decodedRow, DecoderColumnHandle handle)
{
    FieldValueProvider provider = decodedRow.get(handle);
    assertNotNull(provider);
    Type type = handle.getType();
    if (type == BigintType.BIGINT) {
        assertEquals(provider.getLong(), provider.getLong());
    }
    else if (type == BooleanType.BOOLEAN) {
        assertEquals(provider.getBoolean(), provider.getBoolean());
    }
    else if (type == DoubleType.DOUBLE) {
        assertEquals(provider.getDouble(), provider.getDouble());
    }
    else if (type == VarcharType.VARCHAR) {
        assertEquals(provider.getSlice(), provider.getSlice());
    }
}
 
Example #3
Source File: ColumnIndexStatsUtils.java    From presto with Apache License 2.0 6 votes vote down vote up
public static JDBCType jdbcType(Type type)
{
    if (type.equals(BOOLEAN)) {
        return JDBCType.BOOLEAN;
    }
    if (type.equals(BIGINT) || type.equals(TIMESTAMP)) {
        return JDBCType.BIGINT;
    }
    if (type.equals(INTEGER)) {
        return JDBCType.INTEGER;
    }
    if (type.equals(DOUBLE)) {
        return JDBCType.DOUBLE;
    }
    if (type.equals(DATE)) {
        return JDBCType.INTEGER;
    }
    if (type instanceof VarcharType) {
        return JDBCType.VARBINARY;
    }
    return null;
}
 
Example #4
Source File: TestMapAggFunction.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testMapAgg()
{
    assertWindowQuery("map_agg(orderkey, orderstatus) OVER(PARTITION BY orderdate)",
            resultBuilder(TEST_SESSION, BIGINT, VarcharType.createVarcharType(1), mapType(BIGINT, VarcharType.createVarcharType(1)))
                    .row(1, "O", ImmutableMap.of(1, "O"))
                    .row(2, "O", ImmutableMap.of(2, "O"))
                    .row(3, "F", ImmutableMap.of(3, "F"))
                    .row(4, "O", ImmutableMap.of(4, "O"))
                    .row(5, "F", ImmutableMap.of(5, "F"))
                    .row(6, "F", ImmutableMap.of(6, "F"))
                    .row(7, "O", ImmutableMap.of(7, "O"))
                    .row(32, "O", ImmutableMap.of(32, "O"))
                    .row(33, "F", ImmutableMap.of(33, "F"))
                    .row(34, "O", ImmutableMap.of(34, "O"))
                    .build());
}
 
Example #5
Source File: JsonUtil.java    From presto with Apache License 2.0 6 votes vote down vote up
public static boolean canCastFromJson(Type type)
{
    if (type instanceof BooleanType ||
            type instanceof TinyintType ||
            type instanceof SmallintType ||
            type instanceof IntegerType ||
            type instanceof BigintType ||
            type instanceof RealType ||
            type instanceof DoubleType ||
            type instanceof DecimalType ||
            type instanceof VarcharType ||
            type instanceof JsonType) {
        return true;
    }
    if (type instanceof ArrayType) {
        return canCastFromJson(((ArrayType) type).getElementType());
    }
    if (type instanceof MapType) {
        return isValidJsonObjectKeyType(((MapType) type).getKeyType()) && canCastFromJson(((MapType) type).getValueType());
    }
    if (type instanceof RowType) {
        return type.getTypeParameters().stream().allMatch(JsonUtil::canCastFromJson);
    }
    return false;
}
 
Example #6
Source File: TestVariableWidthBlock.java    From presto with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetSizeInBytes()
{
    int numEntries = 1000;
    VarcharType unboundedVarcharType = createUnboundedVarcharType();
    VariableWidthBlockBuilder blockBuilder = new VariableWidthBlockBuilder(null, numEntries, 20 * numEntries);
    for (int i = 0; i < numEntries; i++) {
        unboundedVarcharType.writeString(blockBuilder, String.valueOf(ThreadLocalRandom.current().nextLong()));
    }
    Block block = blockBuilder.build();

    List<Block> splitQuarter = splitBlock(block, 4);
    long sizeInBytes = block.getSizeInBytes();
    long quarter1size = splitQuarter.get(0).getSizeInBytes();
    long quarter2size = splitQuarter.get(1).getSizeInBytes();
    long quarter3size = splitQuarter.get(2).getSizeInBytes();
    long quarter4size = splitQuarter.get(3).getSizeInBytes();
    double expectedQuarterSizeMin = sizeInBytes * 0.2;
    double expectedQuarterSizeMax = sizeInBytes * 0.3;
    assertTrue(quarter1size > expectedQuarterSizeMin && quarter1size < expectedQuarterSizeMax, format("quarter1size is %s, should be between %s and %s", quarter1size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertTrue(quarter2size > expectedQuarterSizeMin && quarter2size < expectedQuarterSizeMax, format("quarter2size is %s, should be between %s and %s", quarter2size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertTrue(quarter3size > expectedQuarterSizeMin && quarter3size < expectedQuarterSizeMax, format("quarter3size is %s, should be between %s and %s", quarter3size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertTrue(quarter4size > expectedQuarterSizeMin && quarter4size < expectedQuarterSizeMax, format("quarter4size is %s, should be between %s and %s", quarter4size, expectedQuarterSizeMin, expectedQuarterSizeMax));
    assertEquals(quarter1size + quarter2size + quarter3size + quarter4size, sizeInBytes);
}
 
Example #7
Source File: AvroColumnDecoder.java    From presto with Apache License 2.0 6 votes vote down vote up
private static Slice getSlice(Object value, Type type, String columnName)
{
    if (type instanceof VarcharType && (value instanceof CharSequence || value instanceof GenericEnumSymbol)) {
        return truncateToLength(utf8Slice(value.toString()), type);
    }

    if (type instanceof VarbinaryType) {
        if (value instanceof ByteBuffer) {
            return Slices.wrappedBuffer((ByteBuffer) value);
        }
        else if (value instanceof GenericFixed) {
            return Slices.wrappedBuffer(((GenericFixed) value).bytes());
        }
    }

    throw new PrestoException(DECODER_CONVERSION_NOT_SUPPORTED, format("cannot decode object of '%s' as '%s' for column '%s'", value.getClass(), type, columnName));
}
 
Example #8
Source File: ExpressionAnalyzer.java    From presto with Apache License 2.0 6 votes vote down vote up
@Override
protected Type visitLikePredicate(LikePredicate node, StackableAstVisitorContext<Context> context)
{
    Type valueType = process(node.getValue(), context);
    if (!(valueType instanceof CharType) && !(valueType instanceof VarcharType)) {
        coerceType(context, node.getValue(), VARCHAR, "Left side of LIKE expression");
    }

    Type patternType = getVarcharType(node.getPattern(), context);
    coerceType(context, node.getPattern(), patternType, "Pattern for LIKE expression");
    if (node.getEscape().isPresent()) {
        Expression escape = node.getEscape().get();
        Type escapeType = getVarcharType(escape, context);
        coerceType(context, escape, escapeType, "Escape for LIKE expression");
    }

    return setExpressionType(node, BOOLEAN);
}
 
Example #9
Source File: TestTypeValidator.java    From presto with Apache License 2.0 6 votes vote down vote up
@BeforeMethod
public void setUp()
{
    symbolAllocator = new SymbolAllocator();
    columnA = symbolAllocator.newSymbol("a", BIGINT);
    columnB = symbolAllocator.newSymbol("b", INTEGER);
    columnC = symbolAllocator.newSymbol("c", DOUBLE);
    columnD = symbolAllocator.newSymbol("d", DATE);
    columnE = symbolAllocator.newSymbol("e", VarcharType.createVarcharType(3));  // varchar(3), to test type only coercion

    Map<Symbol, ColumnHandle> assignments = ImmutableMap.<Symbol, ColumnHandle>builder()
            .put(columnA, new TestingColumnHandle("a"))
            .put(columnB, new TestingColumnHandle("b"))
            .put(columnC, new TestingColumnHandle("c"))
            .put(columnD, new TestingColumnHandle("d"))
            .put(columnE, new TestingColumnHandle("e"))
            .build();

    baseTableScan = new TableScanNode(
            newId(),
            TEST_TABLE_HANDLE,
            ImmutableList.copyOf(assignments.keySet()),
            assignments,
            TupleDomain.all());
}
 
Example #10
Source File: AccumuloRowSerializer.java    From presto with Apache License 2.0 6 votes vote down vote up
/**
 * Recursive helper function used by {@link AccumuloRowSerializer#getArrayFromBlock} and
 * {@link AccumuloRowSerializer#getMapFromBlock} to decode the Block into a Java type.
 *
 * @param type Presto type
 * @param block Block to decode
 * @param position Position in the block to get
 * @return Java object from the Block
 */
static Object readObject(Type type, Block block, int position)
{
    if (Types.isArrayType(type)) {
        Type elementType = Types.getElementType(type);
        return getArrayFromBlock(elementType, block.getObject(position, Block.class));
    }
    else if (Types.isMapType(type)) {
        return getMapFromBlock(type, block.getObject(position, Block.class));
    }
    else {
        if (type.getJavaType() == Slice.class) {
            Slice slice = (Slice) TypeUtils.readNativeValue(type, block, position);
            return type.equals(VarcharType.VARCHAR) ? slice.toStringUtf8() : slice.getBytes();
        }

        return TypeUtils.readNativeValue(type, block, position);
    }
}
 
Example #11
Source File: VarcharToIntegerNumberCoercer.java    From presto with Apache License 2.0 6 votes vote down vote up
public VarcharToIntegerNumberCoercer(VarcharType fromType, T toType)
{
    super(fromType, toType);

    if (toType.equals(TINYINT)) {
        minValue = Byte.MIN_VALUE;
        maxValue = Byte.MAX_VALUE;
    }
    else if (toType.equals(SMALLINT)) {
        minValue = Short.MIN_VALUE;
        maxValue = Short.MAX_VALUE;
    }
    else if (toType.equals(INTEGER)) {
        minValue = Integer.MIN_VALUE;
        maxValue = Integer.MAX_VALUE;
    }
    else if (toType.equals(BIGINT)) {
        minValue = Long.MIN_VALUE;
        maxValue = Long.MAX_VALUE;
    }
    else {
        throw new PrestoException(NOT_SUPPORTED, format("Could not create Coercer from from varchar to %s", toType));
    }
}
 
Example #12
Source File: LiteralFunction.java    From presto with Apache License 2.0 6 votes vote down vote up
public static Type typeForMagicLiteral(Type type)
{
    Class<?> clazz = type.getJavaType();
    clazz = Primitives.unwrap(clazz);

    if (clazz == long.class) {
        return BIGINT;
    }
    if (clazz == double.class) {
        return DOUBLE;
    }
    if (!clazz.isPrimitive()) {
        if (type instanceof VarcharType) {
            return type;
        }
        else {
            return VARBINARY;
        }
    }
    if (clazz == boolean.class) {
        return BOOLEAN;
    }
    throw new IllegalArgumentException("Unhandled Java type: " + clazz.getName());
}
 
Example #13
Source File: SessionPropertyManager.java    From presto with Apache License 2.0 6 votes vote down vote up
public static String serializeSessionProperty(Type type, Object value)
{
    if (value == null) {
        throw new PrestoException(INVALID_SESSION_PROPERTY, "Session property cannot be null");
    }
    if (BooleanType.BOOLEAN.equals(type)) {
        return value.toString();
    }
    if (BigintType.BIGINT.equals(type)) {
        return value.toString();
    }
    if (IntegerType.INTEGER.equals(type)) {
        return value.toString();
    }
    if (DoubleType.DOUBLE.equals(type)) {
        return value.toString();
    }
    if (VarcharType.VARCHAR.equals(type)) {
        return value.toString();
    }
    if (type instanceof ArrayType || type instanceof MapType) {
        return getJsonCodecForType(type).toJson(value);
    }
    throw new PrestoException(INVALID_SESSION_PROPERTY, format("Session property type %s is not supported", type));
}
 
Example #14
Source File: LexicoderRowSerializer.java    From presto with Apache License 2.0 6 votes vote down vote up
public static Lexicoder getLexicoder(Type type)
{
    if (Types.isArrayType(type)) {
        return getListLexicoder(type);
    }
    else if (Types.isMapType(type)) {
        return getMapLexicoder(type);
    }
    else if (type instanceof VarcharType) {
        return LEXICODER_MAP.get(VARCHAR);
    }
    else {
        Lexicoder lexicoder = LEXICODER_MAP.get(type);
        if (lexicoder == null) {
            throw new PrestoException(NOT_SUPPORTED, "No lexicoder for type " + type);
        }
        return lexicoder;
    }
}
 
Example #15
Source File: PulsarMetadata.java    From pulsar with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Type convertPulsarType(SchemaType pulsarType) {
    switch (pulsarType) {
        case BOOLEAN:
            return BooleanType.BOOLEAN;
        case INT8:
            return TinyintType.TINYINT;
        case INT16:
            return SmallintType.SMALLINT;
        case INT32:
            return IntegerType.INTEGER;
        case INT64:
            return BigintType.BIGINT;
        case FLOAT:
            return RealType.REAL;
        case DOUBLE:
            return DoubleType.DOUBLE;
        case NONE:
        case BYTES:
            return VarbinaryType.VARBINARY;
        case STRING:
            return VarcharType.VARCHAR;
        case DATE:
            return DateType.DATE;
        case TIME:
            return TimeType.TIME;
        case TIMESTAMP:
            return TimestampType.TIMESTAMP;
        default:
            log.error("Cannot convert type: %s", pulsarType);
            return null;
    }
}
 
Example #16
Source File: OrcFileWriter.java    From presto with Apache License 2.0 5 votes vote down vote up
private static StorageType toStorageType(Type type)
{
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        return StorageType.decimal(decimalType.getPrecision(), decimalType.getScale());
    }
    Class<?> javaType = type.getJavaType();
    if (javaType == boolean.class) {
        return StorageType.BOOLEAN;
    }
    if (javaType == long.class) {
        return StorageType.LONG;
    }
    if (javaType == double.class) {
        return StorageType.DOUBLE;
    }
    if (javaType == Slice.class) {
        if (type instanceof VarcharType) {
            return StorageType.STRING;
        }
        if (type.equals(VarbinaryType.VARBINARY)) {
            return StorageType.BYTES;
        }
    }
    if (isArrayType(type)) {
        return arrayOf(toStorageType(type.getTypeParameters().get(0)));
    }
    if (isMapType(type)) {
        return mapOf(toStorageType(type.getTypeParameters().get(0)), toStorageType(type.getTypeParameters().get(1)));
    }
    throw new PrestoException(NOT_SUPPORTED, "No storage type for type: " + type);
}
 
Example #17
Source File: TestTypeConversions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertOneLevelRecordColumn()
{
    BigQueryColumnHandle column = new BigQueryColumnHandle("rec", BigQueryType.RECORD, Field.Mode.NULLABLE, ImmutableList.of(
            new BigQueryColumnHandle("sub_s", BigQueryType.STRING, Field.Mode.NULLABLE, ImmutableList.of(), null),
            new BigQueryColumnHandle("sub_i", BigQueryType.INTEGER, Field.Mode.NULLABLE, ImmutableList.of(), null)
    ), null);
    ColumnMetadata metadata = column.getColumnMetadata();
    RowType targetType = RowType.rowType(
            RowType.field("sub_s", VarcharType.VARCHAR),
            RowType.field("sub_i", BigintType.BIGINT));
    assertThat(metadata.getType()).isEqualTo(targetType);
}
 
Example #18
Source File: TestExpressionCompiler.java    From presto with Apache License 2.0 5 votes vote down vote up
private static VarcharType varcharType(List<String> values)
{
    if (values.stream().anyMatch(Objects::isNull)) {
        return VARCHAR;
    }
    return createVarcharType(values.stream().mapToInt(String::length).max().getAsInt());
}
 
Example #19
Source File: TestTypeConversions.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testConvertStringArrayColumn()
{
    BigQueryColumnHandle column = new BigQueryColumnHandle("test", BigQueryType.STRING, Field.Mode.REPEATED, ImmutableList.of(), null);
    ColumnMetadata metadata = column.getColumnMetadata();
    assertThat(metadata.getType()).isEqualTo(new ArrayType(VarcharType.VARCHAR));
}
 
Example #20
Source File: MongoSession.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Optional<Object> translateValue(Object prestoNativeValue, Type type)
{
    requireNonNull(prestoNativeValue, "prestoNativeValue is null");
    requireNonNull(type, "type is null");
    checkArgument(Primitives.wrap(type.getJavaType()).isInstance(prestoNativeValue), "%s (%s) is not a valid representation for %s", prestoNativeValue, prestoNativeValue.getClass(), type);

    if (type == TINYINT) {
        return Optional.of((long) SignedBytes.checkedCast(((Long) prestoNativeValue)));
    }

    if (type == SMALLINT) {
        return Optional.of((long) Shorts.checkedCast(((Long) prestoNativeValue)));
    }

    if (type == IntegerType.INTEGER) {
        return Optional.of((long) toIntExact(((Long) prestoNativeValue)));
    }

    if (type == BIGINT) {
        return Optional.of(prestoNativeValue);
    }

    if (type instanceof ObjectIdType) {
        return Optional.of(new ObjectId(((Slice) prestoNativeValue).getBytes()));
    }

    if (type instanceof VarcharType) {
        return Optional.of(((Slice) prestoNativeValue).toStringUtf8());
    }

    return Optional.empty();
}
 
Example #21
Source File: AvroColumnDecoder.java    From presto with Apache License 2.0 5 votes vote down vote up
private boolean isSupportedType(Type type)
{
    if (isSupportedPrimitive(type)) {
        return true;
    }

    if (type instanceof ArrayType) {
        checkArgument(type.getTypeParameters().size() == 1, "expecting exactly one type parameter for array");
        return isSupportedType(type.getTypeParameters().get(0));
    }

    if (type instanceof MapType) {
        List<Type> typeParameters = type.getTypeParameters();
        checkArgument(typeParameters.size() == 2, "expecting exactly two type parameters for map");
        checkArgument(typeParameters.get(0) instanceof VarcharType, "Unsupported column type '%s' for map key", typeParameters.get(0));
        return isSupportedType(type.getTypeParameters().get(1));
    }

    if (type instanceof RowType) {
        for (Type fieldType : type.getTypeParameters()) {
            if (!isSupportedType(fieldType)) {
                return false;
            }
        }
        return true;
    }
    return false;
}
 
Example #22
Source File: TpchMetadata.java    From presto with Apache License 2.0 5 votes vote down vote up
private static Optional<DoubleRange> toRange(Optional<Object> min, Optional<Object> max, Type columnType)
{
    if (columnType instanceof VarcharType) {
        return Optional.empty();
    }
    if (min.isEmpty() || max.isEmpty()) {
        return Optional.empty();
    }
    return Optional.of(new DoubleRange(toDouble(min.get(), columnType), toDouble(max.get(), columnType)));
}
 
Example #23
Source File: PrometheusRecordCursor.java    From presto with Apache License 2.0 5 votes vote down vote up
static void writeObject(BlockBuilder builder, Type type, Object obj)
{
    if (Types.isArrayType(type)) {
        BlockBuilder arrayBldr = builder.beginBlockEntry();
        Type elementType = Types.getElementType(type);
        for (Object item : (List<?>) obj) {
            writeObject(arrayBldr, elementType, item);
        }
        builder.closeEntry();
    }
    else if (Types.isMapType(type)) {
        BlockBuilder mapBlockBuilder = builder.beginBlockEntry();
        for (Map.Entry<?, ?> entry : ((Map<?, ?>) obj).entrySet()) {
            writeObject(mapBlockBuilder, Types.getKeyType(type), entry.getKey());
            writeObject(mapBlockBuilder, Types.getValueType(type), entry.getValue());
        }
        builder.closeEntry();
    }
    else {
        if (BOOLEAN.equals(type)
                || TINYINT.equals(type)
                || SMALLINT.equals(type)
                || INTEGER.equals(type)
                || BIGINT.equals(type)
                || DOUBLE.equals(type)
                || type instanceof VarcharType) {
            TypeUtils.writeNativeValue(type, builder, obj);
        }
    }
}
 
Example #24
Source File: TestColumnMask.java    From presto with Apache License 2.0 5 votes vote down vote up
@BeforeClass
public void init()
{
    LocalQueryRunner runner = LocalQueryRunner.builder(SESSION).build();

    runner.createCatalog(CATALOG, new TpchConnectorFactory(1), ImmutableMap.of());

    ConnectorViewDefinition view = new ConnectorViewDefinition(
            "SELECT nationkey, name FROM local.tiny.nation",
            Optional.empty(),
            Optional.empty(),
            ImmutableList.of(new ConnectorViewDefinition.ViewColumn("nationkey", BigintType.BIGINT.getTypeId()), new ConnectorViewDefinition.ViewColumn("name", VarcharType.createVarcharType(25).getTypeId())),
            Optional.empty(),
            Optional.of(VIEW_OWNER),
            false);

    MockConnectorFactory mock = MockConnectorFactory.builder()
            .withGetViews((s, prefix) -> ImmutableMap.<SchemaTableName, ConnectorViewDefinition>builder()
                    .put(new SchemaTableName("default", "nation_view"), view)
                    .build())
            .build();

    runner.createCatalog(MOCK_CATALOG, mock, ImmutableMap.of());

    assertions = new QueryAssertions(runner);
    accessControl = assertions.getQueryRunner().getAccessControl();
}
 
Example #25
Source File: TestMySqlTypeMapping.java    From presto with Apache License 2.0 5 votes vote down vote up
@Test
public void testPrestoCreatedParameterizedVarchar()
{
    DataTypeTest.create()
            .addRoundTrip(stringDataType("varchar(10)", createVarcharType(255)), "text_a")
            .addRoundTrip(stringDataType("varchar(255)", createVarcharType(255)), "text_b")
            .addRoundTrip(stringDataType("varchar(256)", createVarcharType(65535)), "text_c")
            .addRoundTrip(stringDataType("varchar(65535)", createVarcharType(65535)), "text_d")
            .addRoundTrip(stringDataType("varchar(65536)", createVarcharType(16777215)), "text_e")
            .addRoundTrip(stringDataType("varchar(16777215)", createVarcharType(16777215)), "text_f")
            .addRoundTrip(stringDataType("varchar(16777216)", createUnboundedVarcharType()), "text_g")
            .addRoundTrip(stringDataType("varchar(" + VarcharType.MAX_LENGTH + ")", createUnboundedVarcharType()), "text_h")
            .addRoundTrip(stringDataType("varchar", createUnboundedVarcharType()), "unbounded")
            .execute(getQueryRunner(), prestoCreateAsSelect("presto_test_parameterized_varchar"));
}
 
Example #26
Source File: ShardStats.java    From presto with Apache License 2.0 5 votes vote down vote up
private static ColumnStats doComputeColumnStats(OrcReader orcReader, long columnId, Type type, TypeManager typeManager)
        throws IOException
{
    OrcColumn column = getColumn(orcReader.getRootColumn().getNestedColumns(), columnId);
    Type columnType = toOrcFileType(type, typeManager);
    OrcRecordReader reader = orcReader.createRecordReader(
            ImmutableList.of(column),
            ImmutableList.of(columnType),
            OrcPredicate.TRUE,
            UTC,
            newSimpleAggregatedMemoryContext(),
            INITIAL_BATCH_SIZE,
            exception -> new PrestoException(RAPTOR_ERROR, "Error reading column: " + columnId, exception));

    if (type.equals(BooleanType.BOOLEAN)) {
        return indexBoolean(reader, columnId);
    }
    if (type.equals(BigintType.BIGINT) ||
            type.equals(DateType.DATE) ||
            type.equals(TimestampType.TIMESTAMP)) {
        return indexLong(type, reader, columnId);
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return indexDouble(reader, columnId);
    }
    if (type instanceof VarcharType) {
        return indexString(type, reader, columnId);
    }
    return null;
}
 
Example #27
Source File: JsonUtil.java    From presto with Apache License 2.0 5 votes vote down vote up
public static boolean canCastToJson(Type type)
{
    if (type instanceof UnknownType ||
            type instanceof BooleanType ||
            type instanceof TinyintType ||
            type instanceof SmallintType ||
            type instanceof IntegerType ||
            type instanceof BigintType ||
            type instanceof RealType ||
            type instanceof DoubleType ||
            type instanceof DecimalType ||
            type instanceof VarcharType ||
            type instanceof JsonType ||
            type instanceof TimestampType ||
            type instanceof DateType) {
        return true;
    }
    if (type instanceof ArrayType) {
        return canCastToJson(((ArrayType) type).getElementType());
    }
    if (type instanceof MapType) {
        MapType mapType = (MapType) type;
        return (mapType.getKeyType() instanceof UnknownType ||
                isValidJsonObjectKeyType(mapType.getKeyType())) &&
                canCastToJson(mapType.getValueType());
    }
    if (type instanceof RowType) {
        return type.getTypeParameters().stream().allMatch(JsonUtil::canCastToJson);
    }
    return false;
}
 
Example #28
Source File: ParquetWriters.java    From presto with Apache License 2.0 5 votes vote down vote up
private static PrimitiveValueWriter getValueWriter(ValuesWriter valuesWriter, io.prestosql.spi.type.Type type, PrimitiveType parquetType)
{
    if (BOOLEAN.equals(type)) {
        return new BooleanValueWriter(valuesWriter, parquetType);
    }
    if (INTEGER.equals(type) || SMALLINT.equals(type) || TINYINT.equals(type)) {
        return new IntegerValueWriter(valuesWriter, type, parquetType);
    }
    if (type instanceof DecimalType) {
        return new DecimalValueWriter(valuesWriter, type, parquetType);
    }
    if (DATE.equals(type)) {
        return new DateValueWriter(valuesWriter, parquetType);
    }
    if (BIGINT.equals(type) || TIMESTAMP.equals(type)) {
        return new BigintValueWriter(valuesWriter, type, parquetType);
    }
    if (DOUBLE.equals(type)) {
        return new DoubleValueWriter(valuesWriter, parquetType);
    }
    if (RealType.REAL.equals(type)) {
        return new RealValueWriter(valuesWriter, parquetType);
    }
    if (type instanceof VarcharType || type instanceof CharType || type instanceof VarbinaryType) {
        return new CharValueWriter(valuesWriter, type, parquetType);
    }
    throw new PrestoException(NOT_SUPPORTED, format("Unsupported type in parquet writer: %s", type));
}
 
Example #29
Source File: LogicalPlanner.java    From presto with Apache License 2.0 5 votes vote down vote up
private Expression noTruncationCast(Expression expression, Type fromType, Type toType)
{
    if (fromType instanceof UnknownType || (!(toType instanceof VarcharType) && !(toType instanceof CharType))) {
        return new Cast(expression, toSqlType(toType));
    }
    int targetLength;
    if (toType instanceof VarcharType) {
        if (((VarcharType) toType).isUnbounded()) {
            return new Cast(expression, toSqlType(toType));
        }
        targetLength = ((VarcharType) toType).getBoundedLength();
    }
    else {
        targetLength = ((CharType) toType).getLength();
    }

    checkState(fromType instanceof VarcharType || fromType instanceof CharType, "inserting non-character value to column of character type");
    ResolvedFunction spaceTrimmedLength = metadata.resolveFunction(QualifiedName.of("$space_trimmed_length"), fromTypes(VARCHAR));
    ResolvedFunction fail = metadata.resolveFunction(QualifiedName.of("fail"), fromTypes(VARCHAR));

    return new IfExpression(
            // check if the trimmed value fits in the target type
            new ComparisonExpression(
                    GREATER_THAN_OR_EQUAL,
                    new GenericLiteral("BIGINT", Integer.toString(targetLength)),
                    new CoalesceExpression(
                            new FunctionCall(
                                    spaceTrimmedLength.toQualifiedName(),
                                    ImmutableList.of(new Cast(expression, toSqlType(VARCHAR)))),
                            new GenericLiteral("BIGINT", "0"))),
            new Cast(expression, toSqlType(toType)),
            new Cast(
                    new FunctionCall(
                            fail.toQualifiedName(),
                            ImmutableList.of(new Cast(new StringLiteral("Cannot truncate non-space characters on INSERT"), toSqlType(VARCHAR)))),
                    toSqlType(toType)));
}
 
Example #30
Source File: TestTupleDomainParquetPredicate.java    From presto with Apache License 2.0 5 votes vote down vote up
private TupleDomain<ColumnDescriptor> getEffectivePredicate(RichColumnDescriptor column, VarcharType type, Slice value)
{
    ColumnDescriptor predicateColumn = new ColumnDescriptor(column.getPath(), column.getPrimitiveType().getPrimitiveTypeName(), 0, 0);
    Domain predicateDomain = singleValue(type, value);
    Map<ColumnDescriptor, Domain> predicateColumns = singletonMap(predicateColumn, predicateDomain);
    return withColumnDomains(predicateColumns);
}