use of io.trino.spi.function.ScalarFunction 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 = threadLocalUserAgentAnalyzer.get();
UserAgent userAgent = userAgentAnalyzer.parse(userAgentStringToParse);
Map<String, String> resultMap = userAgent.toMap(userAgentAnalyzer.getAllPossibleFieldNamesSorted());
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.ScalarFunction in project trino by trinodb.
the class DateTimeFunctions method fromISO8601Date.
@ScalarFunction("from_iso8601_date")
@LiteralParameters("x")
@SqlType(StandardTypes.DATE)
public static long fromISO8601Date(@SqlType("varchar(x)") Slice iso8601DateTime) {
DateTimeFormatter formatter = ISODateTimeFormat.dateElementParser().withChronology(UTC_CHRONOLOGY);
DateTime dateTime = parseDateTimeHelper(formatter, iso8601DateTime.toStringUtf8());
return MILLISECONDS.toDays(dateTime.getMillis());
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class DateTimeFunctions method fromIso8601TimestampNanos.
@ScalarFunction("from_iso8601_timestamp_nanos")
@LiteralParameters("x")
@SqlType("timestamp(9) with time zone")
public static LongTimestampWithTimeZone fromIso8601TimestampNanos(ConnectorSession session, @SqlType("varchar(x)") Slice iso8601DateTime) {
java.time.format.DateTimeFormatter formatter = java.time.format.DateTimeFormatter.ISO_DATE_TIME;
String datetimeString = iso8601DateTime.toStringUtf8();
TemporalAccessor parsedDatetime;
try {
parsedDatetime = formatter.parseBest(datetimeString, ZonedDateTime::from, LocalDateTime::from);
} catch (DateTimeParseException e) {
throw new TrinoException(INVALID_FUNCTION_ARGUMENT, e);
}
ZonedDateTime zonedDatetime;
if (parsedDatetime instanceof ZonedDateTime) {
zonedDatetime = (ZonedDateTime) parsedDatetime;
} else {
zonedDatetime = ((LocalDateTime) parsedDatetime).atZone(session.getTimeZoneKey().getZoneId());
}
long picosOfSecond = zonedDatetime.getNano() * ((long) PICOSECONDS_PER_NANOSECOND);
TimeZoneKey zone = TimeZoneKey.getTimeZoneKey(zonedDatetime.getZone().getId());
return LongTimestampWithTimeZone.fromEpochSecondsAndFraction(zonedDatetime.toEpochSecond(), picosOfSecond, zone);
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class JoniRegexpFunctions method regexpSplit.
@ScalarFunction
@LiteralParameters("x")
@Description("Returns array of strings split by pattern")
@SqlType("array(varchar(x))")
public static Block regexpSplit(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern) {
Matcher matcher = pattern.matcher(source.getBytes());
BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(null, 32);
int lastEnd = 0;
int nextStart = 0;
while (true) {
int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
if (offset == -1) {
break;
}
nextStart = getNextStart(source, matcher);
Slice slice = source.slice(lastEnd, matcher.getBegin() - lastEnd);
lastEnd = matcher.getEnd();
VARCHAR.writeSlice(blockBuilder, slice);
}
VARCHAR.writeSlice(blockBuilder, source.slice(lastEnd, source.length() - lastEnd));
return blockBuilder.build();
}
use of io.trino.spi.function.ScalarFunction in project trino by trinodb.
the class JoniRegexpFunctions method regexpCount.
@ScalarFunction
@Description("Returns the number of times that a pattern occurs in a string")
@LiteralParameters("x")
@SqlType(StandardTypes.BIGINT)
public static long regexpCount(@SqlType("varchar(x)") Slice source, @SqlType(JoniRegexpType.NAME) JoniRegexp pattern) {
Matcher matcher = pattern.matcher(source.getBytes());
int count = 0;
// Start from zero, implies the first byte
int nextStart = 0;
while (true) {
// mather.search returns `source.length` if `nextStart` equals `source.length - 1`.
// It should return -1 if `nextStart` is greater than `source.length - 1`.
int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
if (offset < 0) {
break;
}
nextStart = getNextStart(source, matcher);
count++;
}
return count;
}
Aggregations