use of com.facebook.presto.spi.function.LiteralParameters in project presto by prestodb.
the class JsonFunctions method jsonParse.
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.JSON)
public static Slice jsonParse(@SqlType("varchar(x)") Slice slice) {
try {
byte[] in = slice.getBytes();
SliceOutput dynamicSliceOutput = new DynamicSliceOutput(in.length);
SORTED_MAPPER.writeValue((OutputStream) dynamicSliceOutput, SORTED_MAPPER.readValue(in, Object.class));
return dynamicSliceOutput.slice();
} catch (Exception e) {
throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Cannot convert '%s' to JSON", slice.toStringUtf8()));
}
}
use of com.facebook.presto.spi.function.LiteralParameters in project presto by prestodb.
the class JsonFunctions method jsonArrayContains.
@SqlNullable
@ScalarFunction
@LiteralParameters("x")
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType("varchar(x)") Slice value) {
String valueString = value.toStringUtf8();
try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) {
if (parser.nextToken() != START_ARRAY) {
return null;
}
while (true) {
JsonToken token = parser.nextToken();
if (token == null) {
return null;
}
if (token == END_ARRAY) {
return false;
}
parser.skipChildren();
if (token == VALUE_STRING && valueString.equals(parser.getValueAsString())) {
return true;
}
}
} catch (IOException e) {
return null;
}
}
use of com.facebook.presto.spi.function.LiteralParameters in project presto by prestodb.
the class DateTimeFunctions method addFieldValueTimeWithTimeZone.
@Description("add the specified amount of time to the given time")
@LiteralParameters("x")
@ScalarFunction("date_add")
@SqlType(StandardTypes.TIME_WITH_TIME_ZONE)
public static long addFieldValueTimeWithTimeZone(@SqlType("varchar(x)") Slice unit, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone) {
ISOChronology chronology = unpackChronology(timeWithTimeZone);
long millis = modulo24Hour(chronology, getTimeField(chronology, unit).add(unpackMillisUtc(timeWithTimeZone), toIntExact(value)));
return updateMillisUtc(millis, timeWithTimeZone);
}
use of com.facebook.presto.spi.function.LiteralParameters in project presto by prestodb.
the class DateTimeFunctions method fromISO8601Date.
@ScalarFunction("from_iso8601_date")
@LiteralParameters("x")
@SqlType(StandardTypes.DATE)
public static long fromISO8601Date(ConnectorSession session, @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 com.facebook.presto.spi.function.LiteralParameters in project presto by prestodb.
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) Regex pattern) {
Matcher matcher = pattern.matcher(source.getBytes());
BlockBuilder blockBuilder = VARCHAR.createBlockBuilder(new BlockBuilderStatus(), 32);
int lastEnd = 0;
int nextStart = 0;
while (true) {
int offset = matcher.search(nextStart, source.length(), Option.DEFAULT);
if (offset == -1) {
break;
}
if (matcher.getEnd() == matcher.getBegin()) {
nextStart = matcher.getEnd() + 1;
} else {
nextStart = matcher.getEnd();
}
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();
}
Aggregations