Java Code Examples for io.prestosql.spi.type.RealType

The following examples show how to use io.prestosql.spi.type.RealType. These examples are extracted from open source projects. 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 Project: presto   Source File: DoubleStatisticsBuilder.java    License: Apache License 2.0 6 votes vote down vote up
@Override
public void addBlock(Type type, Block block)
{
    for (int position = 0; position < block.getPositionCount(); position++) {
        if (!block.isNull(position)) {
            double value;
            if (type == RealType.REAL) {
                value = Float.intBitsToFloat((int) type.getLong(block, position));
            }
            else {
                value = type.getDouble(block, position);
            }
            addValue(value);
        }
    }
}
 
Example 2
Source Project: presto   Source File: ShowStatsRewrite.java    License: Apache License 2.0 6 votes vote down vote up
private static Expression toStringLiteral(Type type, double value)
{
    if (type.equals(BigintType.BIGINT) || type.equals(IntegerType.INTEGER) || type.equals(SmallintType.SMALLINT) || type.equals(TinyintType.TINYINT)) {
        return new StringLiteral(Long.toString(round(value)));
    }
    if (type.equals(DOUBLE) || type instanceof DecimalType) {
        return new StringLiteral(Double.toString(value));
    }
    if (type.equals(RealType.REAL)) {
        return new StringLiteral(Float.toString((float) value));
    }
    if (type.equals(DATE)) {
        return new StringLiteral(LocalDate.ofEpochDay(round(value)).toString());
    }
    throw new IllegalArgumentException("Unexpected type: " + type);
}
 
Example 3
Source Project: presto   Source File: JsonUtil.java    License: 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 4
Source Project: presto   Source File: TestRawDecoder.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSupportedDataTypeValidation()
{
    // supported types
    singleColumnDecoder(BigintType.BIGINT, "0", "LONG");
    singleColumnDecoder(IntegerType.INTEGER, "0", "INT");
    singleColumnDecoder(SmallintType.SMALLINT, "0", "SHORT");
    singleColumnDecoder(TinyintType.TINYINT, "0", "BYTE");
    singleColumnDecoder(BooleanType.BOOLEAN, "0", "LONG");
    singleColumnDecoder(DoubleType.DOUBLE, "0", "DOUBLE");
    singleColumnDecoder(createUnboundedVarcharType(), "0", "BYTE");
    singleColumnDecoder(createVarcharType(100), "0", "BYTE");

    // some unsupported types
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(RealType.REAL, "0", "BYTE"));
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(DecimalType.createDecimalType(10, 4), "0", "BYTE"));
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(VarbinaryType.VARBINARY, "0", "BYTE"));
}
 
Example 5
Source Project: presto   Source File: TestCsvDecoder.java    License: Apache License 2.0 6 votes vote down vote up
@Test
public void testSupportedDataTypeValidation()
{
    // supported types
    singleColumnDecoder(BigintType.BIGINT);
    singleColumnDecoder(IntegerType.INTEGER);
    singleColumnDecoder(SmallintType.SMALLINT);
    singleColumnDecoder(TinyintType.TINYINT);
    singleColumnDecoder(BooleanType.BOOLEAN);
    singleColumnDecoder(DoubleType.DOUBLE);
    singleColumnDecoder(createUnboundedVarcharType());
    singleColumnDecoder(createVarcharType(100));

    // some unsupported types
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(RealType.REAL));
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(DecimalType.createDecimalType(10, 4)));
    assertUnsupportedColumnTypeException(() -> singleColumnDecoder(VarbinaryType.VARBINARY));
}
 
Example 6
Source Project: presto   Source File: BlackHolePageSourceProvider.java    License: Apache License 2.0 5 votes vote down vote up
public static boolean isNumericType(Type type)
{
    return type instanceof TinyintType ||
            type instanceof SmallintType ||
            type instanceof IntegerType ||
            type instanceof BigintType ||
            type instanceof RealType ||
            type instanceof DoubleType ||
            type instanceof DecimalType;
}
 
Example 7
Source Project: presto   Source File: TestPostgreSqlTypeMapping.java    License: Apache License 2.0 5 votes vote down vote up
private static DataType<Float> postgreSqlRealDataType()
{
    return dataType("real", RealType.REAL,
            value -> {
                if (Float.isFinite(value)) {
                    return value.toString();
                }
                if (Float.isNaN(value)) {
                    return "'NaN'::real";
                }
                return format("'%sInfinity'::real", value > 0 ? "+" : "-");
            },
            realDataType()::toPrestoLiteral,
            Function.identity());
}
 
Example 8
Source Project: presto   Source File: FloatColumnReader.java    License: Apache License 2.0 5 votes vote down vote up
public FloatColumnReader(Type type, OrcColumn column, LocalMemoryContext systemMemoryContext)
        throws OrcCorruptionException
{
    requireNonNull(type, "type is null");
    verifyStreamType(column, type, RealType.class::isInstance);

    this.column = requireNonNull(column, "column is null");
    this.systemMemoryContext = requireNonNull(systemMemoryContext, "systemMemoryContext is null");
}
 
Example 9
Source Project: presto   Source File: DataType.java    License: Apache License 2.0 5 votes vote down vote up
public static DataType<Float> realDataType()
{
    return dataType("real", RealType.REAL,
            value -> {
                if (Float.isFinite(value)) {
                    return value.toString();
                }
                if (Float.isNaN(value)) {
                    return "nan()";
                }
                return format("%sinfinity()", value > 0 ? "+" : "-");
            });
}
 
Example 10
Source Project: presto   Source File: StatsUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean convertibleToDoubleWithCast(Type type)
{
    return type instanceof DecimalType
            || type instanceof DoubleType
            || type instanceof RealType
            || type instanceof BigintType
            || type instanceof IntegerType
            || type instanceof SmallintType
            || type instanceof TinyintType
            || type instanceof BooleanType;
}
 
Example 11
Source Project: presto   Source File: JsonUtil.java    License: 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 12
Source Project: presto   Source File: JsonUtil.java    License: Apache License 2.0 5 votes vote down vote up
private static boolean isValidJsonObjectKeyType(Type type)
{
    return 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;
}
 
Example 13
Source Project: presto   Source File: JsonUtil.java    License: Apache License 2.0 5 votes vote down vote up
static ObjectKeyProvider createObjectKeyProvider(Type type)
{
    if (type instanceof UnknownType) {
        return (block, position) -> null;
    }
    if (type instanceof BooleanType) {
        return (block, position) -> type.getBoolean(block, position) ? "true" : "false";
    }
    if (type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType || type instanceof BigintType) {
        return (block, position) -> String.valueOf(type.getLong(block, position));
    }
    if (type instanceof RealType) {
        return (block, position) -> String.valueOf(intBitsToFloat(toIntExact(type.getLong(block, position))));
    }
    if (type instanceof DoubleType) {
        return (block, position) -> String.valueOf(type.getDouble(block, position));
    }
    if (type instanceof DecimalType) {
        DecimalType decimalType = (DecimalType) type;
        if (isShortDecimal(decimalType)) {
            return (block, position) -> Decimals.toString(decimalType.getLong(block, position), decimalType.getScale());
        }
        return (block, position) -> Decimals.toString(
                decodeUnscaledValue(type.getSlice(block, position)),
                decimalType.getScale());
    }
    if (type instanceof VarcharType) {
        return (block, position) -> type.getSlice(block, position).toStringUtf8();
    }

    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Unsupported type: %s", type));
}
 
Example 14
Source Project: presto   Source File: Marker.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * LOWER UNBOUNDED is specified with an empty value and a ABOVE bound
 * UPPER UNBOUNDED is specified with an empty value and a BELOW bound
 */
@JsonCreator
public Marker(
        @JsonProperty("type") Type type,
        @JsonProperty("valueBlock") Optional<Block> valueBlock,
        @JsonProperty("bound") Bound bound)
{
    requireNonNull(type, "type is null");
    requireNonNull(valueBlock, "valueBlock is null");
    requireNonNull(bound, "bound is null");

    if (!type.isOrderable()) {
        throw new IllegalArgumentException("type must be orderable");
    }
    if (valueBlock.isEmpty() && bound == Bound.EXACTLY) {
        throw new IllegalArgumentException("Cannot be equal to unbounded");
    }
    if (valueBlock.isPresent() && valueBlock.get().getPositionCount() != 1) {
        throw new IllegalArgumentException("value block should only have one position");
    }
    if (type instanceof RealType && valueBlock.isPresent() && Float.isNaN(intBitsToFloat(toIntExact((long) blockToNativeValue(type, valueBlock.get()))))) {
        throw new IllegalArgumentException("cannot use Real NaN as range bound");
    }
    if (type instanceof DoubleType && valueBlock.isPresent() && Double.isNaN((double) blockToNativeValue(type, valueBlock.get()))) {
        throw new IllegalArgumentException("cannot use Double NaN as range bound");
    }
    this.type = type;
    this.valueBlock = valueBlock;
    this.bound = bound;
}
 
Example 15
Source Project: presto   Source File: ExpressionConverter.java    License: Apache License 2.0 5 votes vote down vote up
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 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();
}
 
Example 16
Source Project: presto   Source File: IcebergPageSink.java    License: Apache License 2.0 5 votes vote down vote up
public static Object getIcebergValue(Block block, int position, Type type)
{
    if (block.isNull(position)) {
        return null;
    }
    if (type instanceof BigintType) {
        return type.getLong(block, position);
    }
    if (type instanceof IntegerType || type instanceof SmallintType || type instanceof TinyintType || type instanceof DateType) {
        return toIntExact(type.getLong(block, position));
    }
    if (type instanceof BooleanType) {
        return type.getBoolean(block, position);
    }
    if (type instanceof DecimalType) {
        return readBigDecimal((DecimalType) type, block, position);
    }
    if (type instanceof RealType) {
        return intBitsToFloat(toIntExact(type.getLong(block, position)));
    }
    if (type instanceof DoubleType) {
        return type.getDouble(block, position);
    }
    if (type instanceof TimestampType) {
        return MILLISECONDS.toMicros(type.getLong(block, position));
    }
    if (type instanceof TimestampWithTimeZoneType) {
        return MILLISECONDS.toMicros(unpackMillisUtc(type.getLong(block, position)));
    }
    if (type instanceof VarbinaryType) {
        return type.getSlice(block, position).getBytes();
    }
    if (type instanceof VarcharType) {
        return type.getSlice(block, position).toStringUtf8();
    }
    throw new UnsupportedOperationException("Type not supported as partition column: " + type.getDisplayName());
}
 
Example 17
Source Project: presto   Source File: CassandraType.java    License: Apache License 2.0 5 votes vote down vote up
public static CassandraType toCassandraType(Type type, ProtocolVersion protocolVersion)
{
    if (type.equals(BooleanType.BOOLEAN)) {
        return BOOLEAN;
    }
    if (type.equals(BigintType.BIGINT)) {
        return BIGINT;
    }
    if (type.equals(IntegerType.INTEGER)) {
        return INT;
    }
    if (type.equals(SmallintType.SMALLINT)) {
        return SMALLINT;
    }
    if (type.equals(TinyintType.TINYINT)) {
        return TINYINT;
    }
    if (type.equals(DoubleType.DOUBLE)) {
        return DOUBLE;
    }
    if (type.equals(RealType.REAL)) {
        return FLOAT;
    }
    if (isVarcharType(type)) {
        return TEXT;
    }
    if (type.equals(DateType.DATE)) {
        return protocolVersion.toInt() <= ProtocolVersion.V3.toInt() ? TEXT : DATE;
    }
    if (type.equals(VarbinaryType.VARBINARY)) {
        return BLOB;
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        return TIMESTAMP;
    }
    throw new IllegalArgumentException("unsupported type: " + type);
}
 
Example 18
Source Project: presto   Source File: TypeHelper.java    License: Apache License 2.0 5 votes vote down vote up
private static Type fromKuduClientType(org.apache.kudu.Type ktype, ColumnTypeAttributes attributes)
{
    switch (ktype) {
        case STRING:
            return VarcharType.VARCHAR;
        case UNIXTIME_MICROS:
            return TimestampType.TIMESTAMP;
        case INT64:
            return BigintType.BIGINT;
        case INT32:
            return IntegerType.INTEGER;
        case INT16:
            return SmallintType.SMALLINT;
        case INT8:
            return TinyintType.TINYINT;
        case FLOAT:
            return RealType.REAL;
        case DOUBLE:
            return DoubleType.DOUBLE;
        case BOOL:
            return BooleanType.BOOLEAN;
        case BINARY:
            return VarbinaryType.VARBINARY;
        case DECIMAL:
            return DecimalType.createDecimalType(attributes.getPrecision(), attributes.getScale());
        default:
            throw new IllegalStateException("Kudu type not implemented for " + ktype);
    }
}
 
Example 19
Source Project: presto   Source File: TypeHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static Object getObject(Type type, RowResult row, int field)
{
    if (row.isNull(field)) {
        return null;
    }
    if (type instanceof VarcharType) {
        return row.getString(field);
    }
    if (type.equals(TimestampType.TIMESTAMP)) {
        return row.getLong(field) / 1000;
    }
    if (type == BigintType.BIGINT) {
        return row.getLong(field);
    }
    if (type == IntegerType.INTEGER) {
        return row.getInt(field);
    }
    if (type == SmallintType.SMALLINT) {
        return row.getShort(field);
    }
    if (type == TinyintType.TINYINT) {
        return row.getByte(field);
    }
    if (type == DoubleType.DOUBLE) {
        return row.getDouble(field);
    }
    if (type == RealType.REAL) {
        return row.getFloat(field);
    }
    if (type == BooleanType.BOOLEAN) {
        return row.getBoolean(field);
    }
    if (type instanceof VarbinaryType) {
        return Slices.wrappedBuffer(row.getBinary(field));
    }
    if (type instanceof DecimalType) {
        return row.getDecimal(field);
    }
    throw new IllegalStateException("getObject not implemented for " + type);
}
 
Example 20
Source Project: presto   Source File: TypeHelper.java    License: Apache License 2.0 5 votes vote down vote up
public static long getLong(Type type, RowResult row, int field)
{
    if (type.equals(TimestampType.TIMESTAMP)) {
        return row.getLong(field) / 1000;
    }
    if (type == BigintType.BIGINT) {
        return row.getLong(field);
    }
    if (type == IntegerType.INTEGER) {
        return row.getInt(field);
    }
    if (type == SmallintType.SMALLINT) {
        return row.getShort(field);
    }
    if (type == TinyintType.TINYINT) {
        return row.getByte(field);
    }
    if (type == RealType.REAL) {
        return floatToRawIntBits(row.getFloat(field));
    }
    if (type instanceof DecimalType) {
        DecimalType dtype = (DecimalType) type;
        if (dtype.isShort()) {
            return row.getDecimal(field).unscaledValue().longValue();
        }
        throw new IllegalStateException("getLong not supported for long decimal: " + type);
    }
    throw new IllegalStateException("getLong not implemented for " + type);
}
 
Example 21
Source Project: presto   Source File: OracleDataTypes.java    License: Apache License 2.0 5 votes vote down vote up
public static DataType<Float> binaryFloatDataType()
{
    return dataType("binary_float", RealType.REAL,
            value -> {
                if (Float.isFinite(value)) {
                    return value.toString();
                }
                if (Float.isNaN(value)) {
                    return "binary_float_nan";
                }
                return format("%sbinary_float_infinity", value > 0 ? "+" : "-");
            });
}
 
Example 22
Source Project: presto   Source File: OracleDataTypes.java    License: Apache License 2.0 5 votes vote down vote up
public static DataType<Float> realDataType()
{
    return dataType("real", RealType.REAL,
            value -> {
                if (Float.isFinite(value)) {
                    return value.toString();
                }
                if (Float.isNaN(value)) {
                    return "nan()";
                }
                return format("%sinfinity()", value > 0 ? "+" : "-");
            });
}
 
Example 23
Source Project: presto   Source File: ParquetWriters.java    License: 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 24
Source Project: presto   Source File: AvroColumnDecoder.java    License: Apache License 2.0 5 votes vote down vote up
private static void serializePrimitive(BlockBuilder blockBuilder, Object value, Type type, String columnName)
{
    requireNonNull(blockBuilder, "parent blockBuilder is null");

    if (value == null) {
        blockBuilder.appendNull();
        return;
    }

    if (type instanceof BooleanType) {
        type.writeBoolean(blockBuilder, (Boolean) value);
        return;
    }

    if ((value instanceof Integer || value instanceof Long) && (type instanceof BigintType || type instanceof IntegerType || type instanceof SmallintType || type instanceof TinyintType)) {
        type.writeLong(blockBuilder, ((Number) value).longValue());
        return;
    }

    if (type instanceof DoubleType) {
        type.writeDouble(blockBuilder, (Double) value);
        return;
    }

    if (type instanceof RealType) {
        type.writeLong(blockBuilder, floatToIntBits((Float) value));
        return;
    }

    if (type instanceof VarcharType || type instanceof VarbinaryType) {
        type.writeSlice(blockBuilder, getSlice(value, type, columnName));
        return;
    }

    throw new PrestoException(DECODER_CONVERSION_NOT_SUPPORTED, format("cannot decode object of '%s' as '%s' for column '%s'", value.getClass(), type, columnName));
}
 
Example 25
Source Project: pulsar   Source File: PulsarMetadata.java    License: 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 26
Source Project: pulsar   Source File: PulsarMetadata.java    License: Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Type convertType(Schema.Type avroType, LogicalType logicalType) {
    switch (avroType) {
        case BOOLEAN:
            return BooleanType.BOOLEAN;
        case INT:
            if (logicalType == LogicalTypes.timeMillis()) {
                return TIME;
            } else if (logicalType == LogicalTypes.date()) {
                return DATE;
            }
            return IntegerType.INTEGER;
        case LONG:
            if (logicalType == LogicalTypes.timestampMillis()) {
                return TIMESTAMP;
            }
            return BigintType.BIGINT;
        case FLOAT:
            return RealType.REAL;
        case DOUBLE:
            return DoubleType.DOUBLE;
        case BYTES:
            return VarbinaryType.VARBINARY;
        case STRING:
            return VarcharType.VARCHAR;
        case ENUM:
            return VarcharType.VARCHAR;
        default:
            log.error("Cannot convert type: %s", avroType);
            return null;
    }
}
 
Example 27
Source Project: presto   Source File: UnwrapCastInComparison.java    License: Apache License 2.0 4 votes vote down vote up
private boolean typeHasNaN(Type type)
{
    return type instanceof DoubleType || type instanceof RealType;
}
 
Example 28
private boolean typeHasNaN(Type type)
{
    return type instanceof DoubleType || type instanceof RealType;
}
 
Example 29
Source Project: presto   Source File: DomainTranslator.java    License: Apache License 2.0 4 votes vote down vote up
@Override
protected ExtractionResult visitLogicalBinaryExpression(LogicalBinaryExpression node, Boolean complement)
{
    ExtractionResult leftResult = process(node.getLeft(), complement);
    ExtractionResult rightResult = process(node.getRight(), complement);

    TupleDomain<Symbol> leftTupleDomain = leftResult.getTupleDomain();
    TupleDomain<Symbol> rightTupleDomain = rightResult.getTupleDomain();

    LogicalBinaryExpression.Operator operator = complement ? node.getOperator().flip() : node.getOperator();
    switch (operator) {
        case AND:
            return new ExtractionResult(
                    leftTupleDomain.intersect(rightTupleDomain),
                    combineConjuncts(metadata, leftResult.getRemainingExpression(), rightResult.getRemainingExpression()));

        case OR:
            TupleDomain<Symbol> columnUnionedTupleDomain = TupleDomain.columnWiseUnion(leftTupleDomain, rightTupleDomain);

            // In most cases, the columnUnionedTupleDomain is only a superset of the actual strict union
            // and so we can return the current node as the remainingExpression so that all bounds will be double checked again at execution time.
            Expression remainingExpression = complementIfNecessary(node, complement);

            // However, there are a few cases where the column-wise union is actually equivalent to the strict union, so we if can detect
            // some of these cases, we won't have to double check the bounds unnecessarily at execution time.

            // We can only make inferences if the remaining expressions on both side are equal and deterministic
            if (leftResult.getRemainingExpression().equals(rightResult.getRemainingExpression()) &&
                    DeterminismEvaluator.isDeterministic(leftResult.getRemainingExpression(), metadata)) {
                // The column-wise union is equivalent to the strict union if
                // 1) If both TupleDomains consist of the same exact single column (e.g. left TupleDomain => (a > 0), right TupleDomain => (a < 10))
                // 2) If one TupleDomain is a superset of the other (e.g. left TupleDomain => (a > 0, b > 0 && b < 10), right TupleDomain => (a > 5, b = 5))
                boolean matchingSingleSymbolDomains = !leftTupleDomain.isNone()
                        && !rightTupleDomain.isNone()
                        && leftTupleDomain.getDomains().get().size() == 1
                        && rightTupleDomain.getDomains().get().size() == 1
                        && leftTupleDomain.getDomains().get().keySet().equals(rightTupleDomain.getDomains().get().keySet());
                boolean oneSideIsSuperSet = leftTupleDomain.contains(rightTupleDomain) || rightTupleDomain.contains(leftTupleDomain);

                if (oneSideIsSuperSet) {
                    remainingExpression = leftResult.getRemainingExpression();
                }
                else if (matchingSingleSymbolDomains) {
                    // Types REAL and DOUBLE require special handling because they include NaN value. In this case, we cannot rely on the union of domains.
                    // That is because domains covering the value set partially might union up to a domain covering the whole value set.
                    // While the component domains didn't include NaN, the resulting domain could be further translated to predicate "TRUE" or "a IS NOT NULL",
                    // which is satisfied by NaN. So during domain union, NaN might be implicitly added.
                    // Example: Let 'a' be a column of type DOUBLE.
                    //          Let left TupleDomain => (a > 0) /false for NaN/, right TupleDomain => (a < 10) /false for NaN/.
                    //          Unioned TupleDomain => "is not null" /true for NaN/
                    // To guard against wrong results, the current node is returned as the remainingExpression.
                    Domain leftDomain = getOnlyElement(leftTupleDomain.getDomains().get().values());
                    Domain rightDomain = getOnlyElement(rightTupleDomain.getDomains().get().values());
                    Type type = leftDomain.getType();

                    // A Domain of a floating point type contains NaN in the following cases:
                    // 1. When it contains all the values of the type and null.
                    //    In such case the domain is 'all', and if it is the only domain
                    //    in the TupleDomain, the TupleDomain gets normalized to TupleDomain 'all'.
                    // 2. When it contains all the values of the type and doesn't contain null.
                    //    In such case no normalization on the level of TupleDomain takes place,
                    //    and the check for NaN is done by inspecting the Domain's valueSet.
                    //    NaN is included when the valueSet is 'all'.
                    boolean unionedDomainContainsNaN = columnUnionedTupleDomain.isAll() ||
                            (columnUnionedTupleDomain.getDomains().isPresent() &&
                                    getOnlyElement(columnUnionedTupleDomain.getDomains().get().values()).getValues().isAll());
                    boolean implicitlyAddedNaN = (type instanceof RealType || type instanceof DoubleType) &&
                            !leftDomain.getValues().isAll() &&
                            !rightDomain.getValues().isAll() &&
                            unionedDomainContainsNaN;
                    if (!implicitlyAddedNaN) {
                        remainingExpression = leftResult.getRemainingExpression();
                    }
                }
            }

            return new ExtractionResult(columnUnionedTupleDomain, remainingExpression);

        default:
            throw new AssertionError("Unknown operator: " + node.getOperator());
    }
}
 
Example 30
Source Project: presto   Source File: JsonUtil.java    License: Apache License 2.0 4 votes vote down vote up
static JsonGeneratorWriter createJsonGeneratorWriter(Type type)
{
    if (type instanceof UnknownType) {
        return new UnknownJsonGeneratorWriter();
    }
    if (type instanceof BooleanType) {
        return new BooleanJsonGeneratorWriter();
    }
    if (type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType || type instanceof BigintType) {
        return new LongJsonGeneratorWriter(type);
    }
    if (type instanceof RealType) {
        return new RealJsonGeneratorWriter();
    }
    if (type instanceof DoubleType) {
        return new DoubleJsonGeneratorWriter();
    }
    if (type instanceof DecimalType) {
        if (isShortDecimal(type)) {
            return new ShortDecimalJsonGeneratorWriter((DecimalType) type);
        }
        return new LongDeicmalJsonGeneratorWriter((DecimalType) type);
    }
    if (type instanceof VarcharType) {
        return new VarcharJsonGeneratorWriter(type);
    }
    if (type instanceof JsonType) {
        return new JsonJsonGeneratorWriter();
    }
    if (type instanceof TimestampType) {
        return new TimestampJsonGeneratorWriter((TimestampType) type);
    }
    if (type instanceof DateType) {
        return new DateGeneratorWriter();
    }
    if (type instanceof ArrayType) {
        ArrayType arrayType = (ArrayType) type;
        return new ArrayJsonGeneratorWriter(
                arrayType,
                createJsonGeneratorWriter(arrayType.getElementType()));
    }
    if (type instanceof MapType) {
        MapType mapType = (MapType) type;
        return new MapJsonGeneratorWriter(
                mapType,
                createObjectKeyProvider(mapType.getKeyType()),
                createJsonGeneratorWriter(mapType.getValueType()));
    }
    if (type instanceof RowType) {
        List<Type> fieldTypes = type.getTypeParameters();
        List<JsonGeneratorWriter> fieldWriters = new ArrayList<>(fieldTypes.size());
        for (int i = 0; i < fieldTypes.size(); i++) {
            fieldWriters.add(createJsonGeneratorWriter(fieldTypes.get(i)));
        }
        return new RowJsonGeneratorWriter((RowType) type, fieldWriters);
    }

    throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Unsupported type: %s", type));
}