Search in sources :

Example 1 with TinyintType

use of io.prestosql.spi.type.TinyintType in project hetu-core by openlookeng.

the class TypeUtils method getActualValue.

public static Object getActualValue(Type type, Object value) {
    if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
        return value;
    } else if (type instanceof BooleanType) {
        return value;
    } else if (type instanceof DoubleType) {
        return value;
    } else if (type instanceof DateType) {
        // keep the `long` representation of date
        return value;
    } else if (type instanceof RealType) {
        Long number = (Long) value;
        return intBitsToFloat(number.intValue());
    } else if (type instanceof VarcharType || type instanceof CharType) {
        if (value instanceof Slice) {
            return ((Slice) value).toStringUtf8();
        }
        return value;
    } else if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            checkState(value instanceof Long);
            return new BigDecimal(BigInteger.valueOf((Long) value), decimalType.getScale(), new MathContext(decimalType.getPrecision()));
        }
        Slice slice;
        if (value instanceof String) {
            slice = Slices.utf8Slice((String) value);
        } else {
            checkState(value instanceof Slice);
            slice = (Slice) value;
        }
        return new BigDecimal(decodeUnscaledValue(slice), decimalType.getScale(), new MathContext(decimalType.getPrecision()));
    } else if (type instanceof TimestampType) {
        Long time = (Long) value;
        return new Timestamp(time);
    }
    throw new UnsupportedOperationException("Not Implemented Exception: " + value + "->" + type);
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) TinyintType(io.prestosql.spi.type.TinyintType) BooleanType(io.prestosql.spi.type.BooleanType) RealType(io.prestosql.spi.type.RealType) Timestamp(java.sql.Timestamp) BigintType(io.prestosql.spi.type.BigintType) BigDecimal(java.math.BigDecimal) MathContext(java.math.MathContext) IntegerType(io.prestosql.spi.type.IntegerType) DoubleType(io.prestosql.spi.type.DoubleType) Slice(io.airlift.slice.Slice) DecimalType(io.prestosql.spi.type.DecimalType) TimestampType(io.prestosql.spi.type.TimestampType) SmallintType(io.prestosql.spi.type.SmallintType) CharType(io.prestosql.spi.type.CharType) DateType(io.prestosql.spi.type.DateType)

Example 2 with TinyintType

use of io.prestosql.spi.type.TinyintType in project hetu-core by openlookeng.

the class LiteralInterpreter method evaluate.

public static Object evaluate(ConstantExpression node) {
    Type type = node.getType();
    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 (node.getValue() instanceof String) ? node.getValue() : ((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) {
        return new SqlTime((long) node.getValue());
    }
    if (type instanceof TimestampType) {
        try {
            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(io.prestosql.type.IntervalYearMonthType) VarcharType(io.prestosql.spi.type.VarcharType) TinyintType(io.prestosql.spi.type.TinyintType) SqlVarbinary(io.prestosql.spi.type.SqlVarbinary) SqlTime(io.prestosql.spi.type.SqlTime) SqlIntervalYearMonth(io.prestosql.type.SqlIntervalYearMonth) SqlTimestamp(io.prestosql.spi.type.SqlTimestamp) PrestoException(io.prestosql.spi.PrestoException) RealType(io.prestosql.spi.type.RealType) IntervalDayTimeType(io.prestosql.type.IntervalDayTimeType) TimeType(io.prestosql.spi.type.TimeType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) TimestampType(io.prestosql.spi.type.TimestampType) SmallintType(io.prestosql.spi.type.SmallintType) DateType(io.prestosql.spi.type.DateType) BooleanType(io.prestosql.spi.type.BooleanType) BigintType(io.prestosql.spi.type.BigintType) IntegerType(io.prestosql.spi.type.IntegerType) DecimalType(io.prestosql.spi.type.DecimalType) IntervalDayTimeType(io.prestosql.type.IntervalDayTimeType) Type(io.prestosql.spi.type.Type) RealType(io.prestosql.spi.type.RealType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) TimestampType(io.prestosql.spi.type.TimestampType) IntervalYearMonthType(io.prestosql.type.IntervalYearMonthType) BigintType(io.prestosql.spi.type.BigintType) CharType(io.prestosql.spi.type.CharType) DoubleType(io.prestosql.spi.type.DoubleType) SmallintType(io.prestosql.spi.type.SmallintType) IntegerType(io.prestosql.spi.type.IntegerType) TimeType(io.prestosql.spi.type.TimeType) TinyintType(io.prestosql.spi.type.TinyintType) DateType(io.prestosql.spi.type.DateType) BooleanType(io.prestosql.spi.type.BooleanType) VarcharType(io.prestosql.spi.type.VarcharType) DoubleType(io.prestosql.spi.type.DoubleType) Slices.utf8Slice(io.airlift.slice.Slices.utf8Slice) Slice(io.airlift.slice.Slice) SqlDate(io.prestosql.spi.type.SqlDate) SqlIntervalDayTime(io.prestosql.type.SqlIntervalDayTime) IntervalDayTimeType(io.prestosql.type.IntervalDayTimeType) DecimalType(io.prestosql.spi.type.DecimalType) CharType(io.prestosql.spi.type.CharType)

Example 3 with TinyintType

use of io.prestosql.spi.type.TinyintType in project hetu-core by openlookeng.

the class BaseJdbcRowExpressionConverter method visitConstant.

@Override
public String visitConstant(ConstantExpression literal, JdbcConverterContext context) {
    Type type = literal.getType();
    if (literal.getValue() == null) {
        return "null";
    }
    if (type instanceof BooleanType) {
        return String.valueOf(((Boolean) literal.getValue()).booleanValue());
    }
    if (type instanceof BigintType || type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType) {
        Number number = (Number) literal.getValue();
        return format("%d", number.longValue());
    }
    if (type instanceof DoubleType) {
        return literal.getValue().toString();
    }
    if (type instanceof RealType) {
        Long number = (Long) literal.getValue();
        return format("%f", intBitsToFloat(number.intValue()));
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (decimalType.isShort()) {
            checkState(literal.getValue() instanceof Long);
            return decodeDecimal(BigInteger.valueOf((long) literal.getValue()), decimalType).toString();
        }
        checkState(literal.getValue() instanceof Slice);
        Slice value = (Slice) literal.getValue();
        return decodeDecimal(decodeUnscaledValue(value), decimalType).toString();
    }
    if (type instanceof VarcharType || type instanceof CharType) {
        return "'" + ((Slice) literal.getValue()).toStringUtf8() + "'";
    }
    if (type.equals(DATE)) {
        String date = printDate(Integer.parseInt(String.valueOf(literal.getValue())));
        return StandardTypes.DATE + " " + ExpressionFormatter.formatStringLiteral(date);
    }
    if (type instanceof TimestampType) {
        DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss.SSS");
        Long time = (Long) literal.getValue();
        return format("TIMESTAMP '%s'", formatter.format(Instant.ofEpochMilli(time).atZone(UTC).toLocalDateTime()));
    }
    throw new PrestoException(NOT_SUPPORTED, String.format("Cannot handle the constant expression %s with value of type %s", literal.getValue(), type));
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) TinyintType(io.prestosql.spi.type.TinyintType) BooleanType(io.prestosql.spi.type.BooleanType) PrestoException(io.prestosql.spi.PrestoException) RealType(io.prestosql.spi.type.RealType) BigintType(io.prestosql.spi.type.BigintType) IntegerType(io.prestosql.spi.type.IntegerType) DecimalType(io.prestosql.spi.type.DecimalType) OperatorType(io.prestosql.spi.function.OperatorType) Type(io.prestosql.spi.type.Type) RealType(io.prestosql.spi.type.RealType) TimestampType(io.prestosql.spi.type.TimestampType) BigintType(io.prestosql.spi.type.BigintType) CharType(io.prestosql.spi.type.CharType) DoubleType(io.prestosql.spi.type.DoubleType) SmallintType(io.prestosql.spi.type.SmallintType) IntegerType(io.prestosql.spi.type.IntegerType) TinyintType(io.prestosql.spi.type.TinyintType) BooleanType(io.prestosql.spi.type.BooleanType) VarcharType(io.prestosql.spi.type.VarcharType) DoubleType(io.prestosql.spi.type.DoubleType) Slice(io.airlift.slice.Slice) DecimalType(io.prestosql.spi.type.DecimalType) TimestampType(io.prestosql.spi.type.TimestampType) SmallintType(io.prestosql.spi.type.SmallintType) CharType(io.prestosql.spi.type.CharType) DateTimeFormatter(java.time.format.DateTimeFormatter)

Example 4 with TinyintType

use of io.prestosql.spi.type.TinyintType in project pulsar by apache.

the class PulsarPrimitiveRowDecoder method decodeRow.

@Override
public Optional<Map<DecoderColumnHandle, FieldValueProvider>> decodeRow(ByteBuf byteBuf) {
    if (columnHandle == null) {
        return Optional.empty();
    }
    Object value = schema.decode(byteBuf);
    Map<DecoderColumnHandle, FieldValueProvider> primitiveColumn = new HashMap<>();
    if (value == null) {
        primitiveColumn.put(columnHandle, FieldValueProviders.nullValueProvider());
    } else {
        Type type = columnHandle.getType();
        if (type instanceof BooleanType) {
            primitiveColumn.put(columnHandle, booleanValueProvider(Boolean.valueOf((Boolean) value)));
        } else if (type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType || type instanceof BigintType) {
            primitiveColumn.put(columnHandle, longValueProvider(Long.parseLong(value.toString())));
        } else if (type instanceof DoubleType) {
            primitiveColumn.put(columnHandle, doubleValueProvider(Double.parseDouble(value.toString())));
        } else if (type instanceof RealType) {
            primitiveColumn.put(columnHandle, longValueProvider(Float.floatToIntBits((Float.parseFloat(value.toString())))));
        } else if (type instanceof VarbinaryType) {
            primitiveColumn.put(columnHandle, bytesValueProvider((byte[]) value));
        } else if (type instanceof VarcharType) {
            primitiveColumn.put(columnHandle, bytesValueProvider(value.toString().getBytes()));
        } else if (type instanceof DateType) {
            primitiveColumn.put(columnHandle, longValueProvider(((Date) value).getTime()));
        } else if (type instanceof TimeType) {
            primitiveColumn.put(columnHandle, longValueProvider(((Time) value).getTime()));
        } else if (type instanceof TimestampType) {
            primitiveColumn.put(columnHandle, longValueProvider(((Timestamp) value).getTime()));
        } else {
            primitiveColumn.put(columnHandle, bytesValueProvider(value.toString().getBytes()));
        }
    }
    return Optional.of(primitiveColumn);
}
Also used : HashMap(java.util.HashMap) VarcharType(io.prestosql.spi.type.VarcharType) FieldValueProvider(io.prestosql.decoder.FieldValueProvider) TinyintType(io.prestosql.spi.type.TinyintType) BooleanType(io.prestosql.spi.type.BooleanType) DecoderColumnHandle(io.prestosql.decoder.DecoderColumnHandle) RealType(io.prestosql.spi.type.RealType) Timestamp(java.sql.Timestamp) BigintType(io.prestosql.spi.type.BigintType) Date(java.util.Date) TimeType(io.prestosql.spi.type.TimeType) IntegerType(io.prestosql.spi.type.IntegerType) BigintType(io.prestosql.spi.type.BigintType) DoubleType(io.prestosql.spi.type.DoubleType) Type(io.prestosql.spi.type.Type) SmallintType(io.prestosql.spi.type.SmallintType) IntegerType(io.prestosql.spi.type.IntegerType) TimeType(io.prestosql.spi.type.TimeType) RealType(io.prestosql.spi.type.RealType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) TinyintType(io.prestosql.spi.type.TinyintType) TimestampType(io.prestosql.spi.type.TimestampType) DateType(io.prestosql.spi.type.DateType) BooleanType(io.prestosql.spi.type.BooleanType) VarcharType(io.prestosql.spi.type.VarcharType) VarbinaryType(io.prestosql.spi.type.VarbinaryType) DoubleType(io.prestosql.spi.type.DoubleType) TimestampType(io.prestosql.spi.type.TimestampType) SmallintType(io.prestosql.spi.type.SmallintType) DateType(io.prestosql.spi.type.DateType)

Example 5 with TinyintType

use of io.prestosql.spi.type.TinyintType in project hetu-core by openlookeng.

the class HivePartitionManager method getFilteredPartitionNames.

private List<String> getFilteredPartitionNames(SemiTransactionalHiveMetastore metastore, HiveIdentity identity, SchemaTableName tableName, List<HiveColumnHandle> partitionKeys, TupleDomain<ColumnHandle> effectivePredicate, Table table) {
    checkArgument(effectivePredicate.getDomains().isPresent());
    List<String> filter = new ArrayList<>();
    for (HiveColumnHandle partitionKey : partitionKeys) {
        Domain domain = effectivePredicate.getDomains().get().get(partitionKey);
        if (domain != null && domain.isNullableSingleValue()) {
            Object value = domain.getNullableSingleValue();
            Type type = domain.getType();
            if (value == null) {
                filter.add(HivePartitionKey.HIVE_DEFAULT_DYNAMIC_PARTITION);
            } else if (type instanceof CharType) {
                Slice slice = (Slice) value;
                filter.add(padSpaces(slice, (CharType) type).toStringUtf8());
            } else if (type instanceof VarcharType) {
                Slice slice = (Slice) value;
                filter.add(slice.toStringUtf8());
            } else // unless we know that all partition values use the canonical Java representation.
            if (!assumeCanonicalPartitionKeys) {
                filter.add(PARTITION_VALUE_WILDCARD);
            } else if (type instanceof DecimalType && !((DecimalType) type).isShort()) {
                Slice slice = (Slice) value;
                filter.add(Decimals.toString(slice, ((DecimalType) type).getScale()));
            } else if (type instanceof DecimalType && ((DecimalType) type).isShort()) {
                filter.add(Decimals.toString((long) value, ((DecimalType) type).getScale()));
            } else if (type instanceof DateType) {
                DateTimeFormatter dateTimeFormatter = ISODateTimeFormat.date().withZoneUTC();
                filter.add(dateTimeFormatter.print(TimeUnit.DAYS.toMillis((long) value)));
            } else if (type instanceof TimestampType) {
                // we don't have time zone info, so just add a wildcard
                filter.add(PARTITION_VALUE_WILDCARD);
            } else if (type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType || type instanceof BigintType || type instanceof DoubleType || type instanceof RealType || type instanceof BooleanType) {
                filter.add(value.toString());
            } else {
                throw new PrestoException(NOT_SUPPORTED, format("Unsupported partition key type: %s", type.getDisplayName()));
            }
        } else {
            filter.add(PARTITION_VALUE_WILDCARD);
        }
    }
    // fetch the partition names
    return metastore.getPartitionNamesByParts(identity, tableName.getSchemaName(), tableName.getTableName(), filter, table).orElseThrow(() -> new TableNotFoundException(tableName));
}
Also used : VarcharType(io.prestosql.spi.type.VarcharType) TinyintType(io.prestosql.spi.type.TinyintType) ArrayList(java.util.ArrayList) BooleanType(io.prestosql.spi.type.BooleanType) PrestoException(io.prestosql.spi.PrestoException) RealType(io.prestosql.spi.type.RealType) BigintType(io.prestosql.spi.type.BigintType) IntegerType(io.prestosql.spi.type.IntegerType) TableNotFoundException(io.prestosql.spi.connector.TableNotFoundException) DecimalType(io.prestosql.spi.type.DecimalType) Type(io.prestosql.spi.type.Type) RealType(io.prestosql.spi.type.RealType) TimestampType(io.prestosql.spi.type.TimestampType) BigintType(io.prestosql.spi.type.BigintType) CharType(io.prestosql.spi.type.CharType) DoubleType(io.prestosql.spi.type.DoubleType) SmallintType(io.prestosql.spi.type.SmallintType) IntegerType(io.prestosql.spi.type.IntegerType) TinyintType(io.prestosql.spi.type.TinyintType) DateType(io.prestosql.spi.type.DateType) BooleanType(io.prestosql.spi.type.BooleanType) VarcharType(io.prestosql.spi.type.VarcharType) Slice(io.airlift.slice.Slice) DoubleType(io.prestosql.spi.type.DoubleType) DecimalType(io.prestosql.spi.type.DecimalType) TimestampType(io.prestosql.spi.type.TimestampType) SmallintType(io.prestosql.spi.type.SmallintType) CharType(io.prestosql.spi.type.CharType) Domain(io.prestosql.spi.predicate.Domain) TupleDomain(io.prestosql.spi.predicate.TupleDomain) DateType(io.prestosql.spi.type.DateType) DateTimeFormatter(org.joda.time.format.DateTimeFormatter)

Aggregations

BigintType (io.prestosql.spi.type.BigintType)5 BooleanType (io.prestosql.spi.type.BooleanType)5 DoubleType (io.prestosql.spi.type.DoubleType)5 IntegerType (io.prestosql.spi.type.IntegerType)5 RealType (io.prestosql.spi.type.RealType)5 SmallintType (io.prestosql.spi.type.SmallintType)5 TimestampType (io.prestosql.spi.type.TimestampType)5 TinyintType (io.prestosql.spi.type.TinyintType)5 VarcharType (io.prestosql.spi.type.VarcharType)5 Slice (io.airlift.slice.Slice)4 CharType (io.prestosql.spi.type.CharType)4 DateType (io.prestosql.spi.type.DateType)4 DecimalType (io.prestosql.spi.type.DecimalType)4 Type (io.prestosql.spi.type.Type)4 PrestoException (io.prestosql.spi.PrestoException)3 TimeType (io.prestosql.spi.type.TimeType)2 VarbinaryType (io.prestosql.spi.type.VarbinaryType)2 Timestamp (java.sql.Timestamp)2 Slices.utf8Slice (io.airlift.slice.Slices.utf8Slice)1 DecoderColumnHandle (io.prestosql.decoder.DecoderColumnHandle)1