use of io.trino.spi.function.SqlType in project trino by trinodb.
the class JsonOperators method castToSmallint.
@ScalarOperator(CAST)
@SqlNullable
@SqlType(SMALLINT)
public static Long castToSmallint(@SqlType(JSON) Slice json) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Long result = currentTokenAsSmallint(parser);
// check no trailing token
checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to SMALLINT");
return result;
} catch (TrinoException e) {
if (e.getErrorCode().equals(NUMERIC_VALUE_OUT_OF_RANGE.toErrorCode())) {
throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), INTEGER), e.getCause());
}
throw e;
} catch (IllegalArgumentException | IOException | JsonCastException e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), SMALLINT), e);
}
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class JsonOperators method castToBoolean.
@ScalarOperator(CAST)
@SqlNullable
@SqlType(BOOLEAN)
public static Boolean castToBoolean(@SqlType(JSON) Slice json) {
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
parser.nextToken();
Boolean result = currentTokenAsBoolean(parser);
// check no trailing token
checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to BOOLEAN");
return result;
} catch (IOException | JsonCastException e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), BOOLEAN), e);
}
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class DataSizeFunctions method parsePrestoDataSize.
@Description("Converts data size string to bytes")
@ScalarFunction(value = "parse_data_size", alias = "parse_presto_data_size")
@LiteralParameters("x")
@SqlType("decimal(38,0)")
public static Int128 parsePrestoDataSize(@SqlType("varchar(x)") Slice input) {
String dataSize = input.toStringUtf8();
int valueLength = 0;
for (int i = 0; i < dataSize.length(); i++) {
char c = dataSize.charAt(i);
if (isDigit(c) || c == '.') {
valueLength++;
} else {
break;
}
}
if (valueLength == 0) {
throw invalidDataSize(dataSize);
}
BigDecimal value = parseValue(dataSize.substring(0, valueLength), dataSize);
Unit unit = Unit.parse(dataSize.substring(valueLength), dataSize);
BigInteger bytes = value.multiply(unit.getFactor()).toBigInteger();
try {
return Decimals.valueOf(bytes);
} catch (ArithmeticException e) {
throw new TrinoException(NUMERIC_VALUE_OUT_OF_RANGE, format("Value out of range: '%s' ('%sB')", dataSize, bytes));
}
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class DateTimeFunctions method currentDate.
@Description("Current date")
@ScalarFunction
@SqlType(StandardTypes.DATE)
public static long currentDate(ConnectorSession session) {
ISOChronology chronology = getChronology(session.getTimeZoneKey());
// It is ok for this method to use the Object interfaces because it is constant folded during
// plan optimization
LocalDate currentDate = new DateTime(session.getStart().toEpochMilli(), chronology).toLocalDate();
return Days.daysBetween(new LocalDate(1970, 1, 1), currentDate).getDays();
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class IpAddressFunctions method contains.
@Description("Determines whether given IP address exists in the CIDR")
@ScalarFunction
@SqlType(StandardTypes.BOOLEAN)
public static boolean contains(@SqlType(StandardTypes.VARCHAR) Slice network, @SqlType(StandardTypes.IPADDRESS) Slice address) {
String cidr = network.toStringUtf8();
int separator = cidr.indexOf("/");
if (separator == -1) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid CIDR");
}
byte[] base;
boolean isIpv4;
try {
InetAddress inetAddress = InetAddresses.forString(cidr.substring(0, separator));
base = inetAddress.getAddress();
isIpv4 = inetAddress instanceof Inet4Address;
} catch (IllegalArgumentException e) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid network IP address");
}
int prefixLength = Integer.parseInt(cidr.substring(separator + 1));
if (prefixLength < 0) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid prefix length");
}
int baseLength = base.length * Byte.SIZE;
if (prefixLength > baseLength) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Prefix length exceeds address length");
}
if (isIpv4 && !isValidIpV4Cidr(base, prefixLength)) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid CIDR");
}
if (!isIpv4 && !isValidIpV6Cidr(prefixLength)) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid CIDR");
}
byte[] ipAddress;
try {
ipAddress = InetAddress.getByAddress(address.getBytes()).getAddress();
} catch (UnknownHostException e) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "Invalid IP address");
}
if (base.length != ipAddress.length) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, "IP address version should be the same");
}
if (prefixLength == 0) {
return true;
}
BigInteger cidrPrefix = new BigInteger(base).shiftRight(baseLength - prefixLength);
BigInteger addressPrefix = new BigInteger(ipAddress).shiftRight(baseLength - prefixLength);
return cidrPrefix.equals(addressPrefix);
}
Aggregations