Search in sources :

Example 11 with ScalarOperator

use of com.facebook.presto.spi.function.ScalarOperator in project presto by prestodb.

the class IpPrefixOperators method castFromIpAddressToIpPrefix.

@ScalarOperator(CAST)
@SqlType(StandardTypes.IPPREFIX)
public static Slice castFromIpAddressToIpPrefix(@SqlType(StandardTypes.IPADDRESS) Slice slice) {
    byte[] address;
    try {
        address = InetAddress.getByAddress(slice.getBytes()).getAddress();
    } catch (UnknownHostException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, "Invalid IP address binary: " + slice.toStringUtf8(), e);
    }
    byte[] bytes = new byte[IPPREFIX.getFixedSize()];
    arraycopy(slice.getBytes(), 0, bytes, 0, IPPREFIX.getFixedSize() - 1);
    if (address.length == 4) {
        bytes[IPPREFIX.getFixedSize() - 1] = (byte) 32;
    } else if (address.length == 16) {
        bytes[IPPREFIX.getFixedSize() - 1] = (byte) 128;
    } else {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Invalid InetAddress length: " + address.length);
    }
    return wrappedBuffer(bytes);
}
Also used : UnknownHostException(java.net.UnknownHostException) PrestoException(com.facebook.presto.spi.PrestoException) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlType(com.facebook.presto.spi.function.SqlType)

Example 12 with ScalarOperator

use of com.facebook.presto.spi.function.ScalarOperator in project presto by prestodb.

the class IpPrefixOperators method castFromVarcharToIpPrefix.

@LiteralParameters("x")
@ScalarOperator(CAST)
@SqlType(StandardTypes.IPPREFIX)
public static Slice castFromVarcharToIpPrefix(@SqlType("varchar(x)") Slice slice) {
    byte[] address;
    int subnetSize;
    String string = slice.toStringUtf8();
    if (!string.contains("/")) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPPREFIX: " + slice.toStringUtf8());
    }
    String[] parts = string.split("/");
    try {
        address = InetAddresses.forString(parts[0]).getAddress();
    } catch (IllegalArgumentException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPPREFIX: " + slice.toStringUtf8());
    }
    subnetSize = Integer.parseInt(parts[1]);
    byte[] bytes = new byte[IPPREFIX.getFixedSize()];
    if (address.length == 4) {
        if (subnetSize < 0 || 32 < subnetSize) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPPREFIX: " + slice.toStringUtf8());
        }
        for (int i = 0; i < 4; i++) {
            address[3 - i] &= -0x1 << min(max((32 - subnetSize) - 8 * i, 0), 8);
        }
        bytes[10] = (byte) 0xff;
        bytes[11] = (byte) 0xff;
        arraycopy(address, 0, bytes, 12, 4);
        bytes[IPPREFIX.getFixedSize() - 1] = (byte) subnetSize;
    } else if (address.length == 16) {
        if (subnetSize < 0 || 128 < subnetSize) {
            throw new PrestoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPPREFIX: " + slice.toStringUtf8());
        }
        for (int i = 0; i < 16; i++) {
            address[15 - i] &= (byte) -0x1 << min(max((128 - subnetSize) - 8 * i, 0), 8);
        }
        arraycopy(address, 0, bytes, 0, 16);
        bytes[IPPREFIX.getFixedSize() - 1] = (byte) subnetSize;
    } else {
        throw new PrestoException(GENERIC_INTERNAL_ERROR, "Invalid InetAddress length: " + address.length);
    }
    return wrappedBuffer(bytes);
}
Also used : PrestoException(com.facebook.presto.spi.PrestoException) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) LiteralParameters(com.facebook.presto.spi.function.LiteralParameters) SqlType(com.facebook.presto.spi.function.SqlType)

Example 13 with ScalarOperator

use of com.facebook.presto.spi.function.ScalarOperator in project presto by prestodb.

the class IpPrefixOperators method castFromIpPrefixToVarchar.

@ScalarOperator(CAST)
@SqlType(StandardTypes.VARCHAR)
public static Slice castFromIpPrefixToVarchar(@SqlType(StandardTypes.IPPREFIX) Slice slice) {
    try {
        String addrString = InetAddresses.toAddrString(InetAddress.getByAddress(slice.getBytes(0, 2 * Long.BYTES)));
        String prefixString = Integer.toString(slice.getByte(2 * Long.BYTES) & 0xff);
        return utf8Slice(addrString + "/" + prefixString);
    } catch (UnknownHostException e) {
        throw new PrestoException(INVALID_CAST_ARGUMENT, "Invalid IP address binary length: " + slice.length(), e);
    }
}
Also used : UnknownHostException(java.net.UnknownHostException) PrestoException(com.facebook.presto.spi.PrestoException) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlType(com.facebook.presto.spi.function.SqlType)

Example 14 with ScalarOperator

use of com.facebook.presto.spi.function.ScalarOperator in project presto by prestodb.

the class DateOperators method castToTimestampWithTimeZone.

@ScalarOperator(CAST)
@SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE)
public static long castToTimestampWithTimeZone(SqlFunctionProperties properties, @SqlType(StandardTypes.DATE) long value) {
    long utcMillis = TimeUnit.DAYS.toMillis(value);
    // date is encoded as milliseconds at midnight in UTC
    // convert to midnight in the session timezone
    ISOChronology chronology = getChronology(properties.getTimeZoneKey());
    long millis = utcMillis - chronology.getZone().getOffset(utcMillis);
    return packDateTimeWithZone(millis, properties.getTimeZoneKey());
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlType(com.facebook.presto.spi.function.SqlType)

Example 15 with ScalarOperator

use of com.facebook.presto.spi.function.ScalarOperator in project presto by prestodb.

the class DateOperators method castToTimestamp.

@ScalarOperator(CAST)
@SqlType(StandardTypes.TIMESTAMP)
public static long castToTimestamp(SqlFunctionProperties properties, @SqlType(StandardTypes.DATE) long value) {
    if (properties.isLegacyTimestamp()) {
        long utcMillis = TimeUnit.DAYS.toMillis(value);
        // date is encoded as milliseconds at midnight in UTC
        // convert to midnight in the session timezone
        ISOChronology chronology = getChronology(properties.getTimeZoneKey());
        return utcMillis - chronology.getZone().getOffset(utcMillis);
    } else {
        return TimeUnit.DAYS.toMillis(value);
    }
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) ScalarOperator(com.facebook.presto.spi.function.ScalarOperator) SqlType(com.facebook.presto.spi.function.SqlType)

Aggregations

ScalarOperator (com.facebook.presto.spi.function.ScalarOperator)26 SqlType (com.facebook.presto.spi.function.SqlType)24 PrestoException (com.facebook.presto.spi.PrestoException)13 ISOChronology (org.joda.time.chrono.ISOChronology)9 SqlNullable (com.facebook.presto.spi.function.SqlNullable)8 JsonCastException (com.facebook.presto.util.JsonCastException)8 JsonUtil.createJsonParser (com.facebook.presto.util.JsonUtil.createJsonParser)8 JsonParser (com.fasterxml.jackson.core.JsonParser)8 IOException (java.io.IOException)8 ScalarFunction (com.facebook.presto.spi.function.ScalarFunction)5 LiteralParameters (com.facebook.presto.spi.function.LiteralParameters)4 ImmutableList (com.google.common.collect.ImmutableList)2 Slice (io.airlift.slice.Slice)2 UnknownHostException (java.net.UnknownHostException)2 HyperLogLog (com.facebook.airlift.stats.cardinality.HyperLogLog)1 ScalarHeader (com.facebook.presto.operator.scalar.ScalarHeader)1 JsonUtil.currentTokenAsBoolean (com.facebook.presto.util.JsonUtil.currentTokenAsBoolean)1 JsonUtil.currentTokenAsDouble (com.facebook.presto.util.JsonUtil.currentTokenAsDouble)1 SliceUtf8.offsetOfCodePoint (io.airlift.slice.SliceUtf8.offsetOfCodePoint)1 Method (java.lang.reflect.Method)1