Search in sources :

Example 86 with SqlType

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);
    }
}
Also used : JsonCastException(io.trino.util.JsonCastException) TrinoException(io.trino.spi.TrinoException) IOException(java.io.IOException) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(io.trino.spi.function.ScalarOperator) SqlNullable(io.trino.spi.function.SqlNullable) SqlType(io.trino.spi.function.SqlType)

Example 87 with SqlType

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);
    }
}
Also used : JsonCastException(io.trino.util.JsonCastException) TrinoException(io.trino.spi.TrinoException) IOException(java.io.IOException) JsonUtil.currentTokenAsBoolean(io.trino.util.JsonUtil.currentTokenAsBoolean) JsonUtil.createJsonParser(io.trino.util.JsonUtil.createJsonParser) JsonParser(com.fasterxml.jackson.core.JsonParser) ScalarOperator(io.trino.spi.function.ScalarOperator) SqlNullable(io.trino.spi.function.SqlNullable) SqlType(io.trino.spi.function.SqlType)

Example 88 with SqlType

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));
    }
}
Also used : BigInteger(java.math.BigInteger) TrinoException(io.trino.spi.TrinoException) BigDecimal(java.math.BigDecimal) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) LiteralParameters(io.trino.spi.function.LiteralParameters) SqlType(io.trino.spi.function.SqlType)

Example 89 with SqlType

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();
}
Also used : ISOChronology(org.joda.time.chrono.ISOChronology) LocalDate(org.joda.time.LocalDate) ZonedDateTime(java.time.ZonedDateTime) LocalDateTime(java.time.LocalDateTime) DateTime(org.joda.time.DateTime) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Example 90 with SqlType

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);
}
Also used : Inet4Address(java.net.Inet4Address) UnknownHostException(java.net.UnknownHostException) TrinoException(io.trino.spi.TrinoException) BigInteger(java.math.BigInteger) InetAddress(java.net.InetAddress) ScalarFunction(io.trino.spi.function.ScalarFunction) Description(io.trino.spi.function.Description) SqlType(io.trino.spi.function.SqlType)

Aggregations

SqlType (io.trino.spi.function.SqlType)176 ScalarFunction (io.trino.spi.function.ScalarFunction)107 Description (io.trino.spi.function.Description)90 LiteralParameters (io.trino.spi.function.LiteralParameters)60 SqlNullable (io.trino.spi.function.SqlNullable)56 BlockBuilder (io.trino.spi.block.BlockBuilder)52 OGCGeometry (com.esri.core.geometry.ogc.OGCGeometry)46 TrinoException (io.trino.spi.TrinoException)37 Constraint (io.trino.type.Constraint)23 Slice (io.airlift.slice.Slice)22 TypeParameter (io.trino.spi.function.TypeParameter)21 Point (com.esri.core.geometry.Point)17 OGCPoint (com.esri.core.geometry.ogc.OGCPoint)17 MultiPoint (com.esri.core.geometry.MultiPoint)15 JsonParser (com.fasterxml.jackson.core.JsonParser)14 JsonUtil.createJsonParser (io.trino.util.JsonUtil.createJsonParser)14 IOException (java.io.IOException)14 ScalarOperator (io.trino.spi.function.ScalarOperator)13 SliceUtf8.lengthOfCodePoint (io.airlift.slice.SliceUtf8.lengthOfCodePoint)11 Matcher (io.airlift.joni.Matcher)9