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);
}
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);
}
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);
}
}
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());
}
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);
}
}
Aggregations