use of io.trino.spi.function.SqlType in project trino by trinodb.
the class IpAddressOperators method castFromVarcharToIpAddress.
@LiteralParameters("x")
@ScalarOperator(CAST)
@SqlType(StandardTypes.IPADDRESS)
public static Slice castFromVarcharToIpAddress(@SqlType("varchar(x)") Slice slice) {
byte[] address;
try {
address = InetAddresses.forString(slice.toStringUtf8()).getAddress();
} catch (IllegalArgumentException e) {
throw new TrinoException(INVALID_CAST_ARGUMENT, "Cannot cast value to IPADDRESS: " + slice.toStringUtf8());
}
byte[] bytes;
if (address.length == 4) {
bytes = new byte[16];
bytes[10] = (byte) 0xff;
bytes[11] = (byte) 0xff;
arraycopy(address, 0, bytes, 12, 4);
} else if (address.length == 16) {
bytes = address;
} else {
throw new TrinoException(GENERIC_INTERNAL_ERROR, "Invalid InetAddress length: " + address.length);
}
return wrappedBuffer(bytes);
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class CreateHll method createHll.
@ScalarFunction
@SqlType(StandardTypes.HYPER_LOG_LOG)
public static Slice createHll(@SqlType(StandardTypes.BIGINT) long value) {
HyperLogLog hll = HyperLogLog.newInstance(4096);
hll.add(value);
return hll.serialize();
}
use of io.trino.spi.function.SqlType in project yauaa by nielsbasjes.
the class ParseUserAgentFunction method parseUserAgent.
@ScalarFunction("parse_user_agent")
@Description("Tries to parse and analyze the provided useragent string and extract as many attributes " + "as possible. Uses Yauaa (Yet Another UserAgent Analyzer) version " + Version.PROJECT_VERSION + ". " + "See https://yauaa.basjes.nl/udf/trino/ for documentation.")
@SqlType("map(varchar, varchar)")
public static Block parseUserAgent(@SqlType(StandardTypes.VARCHAR) Slice userAgentSlice) throws IllegalArgumentException {
String userAgentStringToParse = null;
if (userAgentSlice != null) {
userAgentStringToParse = userAgentSlice.toStringUtf8();
}
UserAgentAnalyzer userAgentAnalyzer = getInstance();
UserAgent userAgent = userAgentAnalyzer.parse(userAgentStringToParse);
Map<String, String> resultMap = userAgent.toMap(userAgent.getAvailableFieldNamesSorted());
MapType mapType = new MapType(VARCHAR, VARCHAR, new TypeOperators());
BlockBuilder blockBuilder = mapType.createBlockBuilder(null, resultMap.size());
BlockBuilder singleMapBlockBuilder = blockBuilder.beginBlockEntry();
for (Map.Entry<String, String> entry : resultMap.entrySet()) {
VARCHAR.writeString(singleMapBlockBuilder, entry.getKey());
VARCHAR.writeString(singleMapBlockBuilder, entry.getValue());
}
blockBuilder.closeEntry();
return mapType.getObject(blockBuilder, blockBuilder.getPositionCount() - 1);
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class ArrayToArrayCast method filterLong.
@TypeParameter("F")
@TypeParameter("T")
@TypeParameterSpecialization(name = "T", nativeContainerType = long.class)
@SqlType("array(T)")
public static Block filterLong(@TypeParameter("T") Type resultType, @CastDependency(fromType = "F", toType = "T", convention = @Convention(arguments = BLOCK_POSITION, result = NULLABLE_RETURN, session = true)) MethodHandle cast, ConnectorSession session, @SqlType("array(F)") Block array) throws Throwable {
int positionCount = array.getPositionCount();
BlockBuilder resultBuilder = resultType.createBlockBuilder(null, positionCount);
for (int position = 0; position < positionCount; position++) {
if (!array.isNull(position)) {
Long value = (Long) cast.invokeExact(session, array, position);
if (value != null) {
resultType.writeLong(resultBuilder, value);
continue;
}
}
resultBuilder.appendNull();
}
return resultBuilder.build();
}
use of io.trino.spi.function.SqlType in project trino by trinodb.
the class ArrayUnionFunction method bigintUnion.
@SqlType("array(bigint)")
public static Block bigintUnion(@SqlType("array(bigint)") Block leftArray, @SqlType("array(bigint)") Block rightArray) {
int leftArrayCount = leftArray.getPositionCount();
int rightArrayCount = rightArray.getPositionCount();
LongSet set = new LongOpenHashSet(leftArrayCount + rightArrayCount);
BlockBuilder distinctElementBlockBuilder = BIGINT.createBlockBuilder(null, leftArrayCount + rightArrayCount);
AtomicBoolean containsNull = new AtomicBoolean(false);
appendBigintArray(leftArray, containsNull, set, distinctElementBlockBuilder);
appendBigintArray(rightArray, containsNull, set, distinctElementBlockBuilder);
return distinctElementBlockBuilder.build();
}
Aggregations