use of com.facebook.presto.spi.function.ScalarFunction in project presto by prestodb.
the class JsonFunctions method jsonArrayContains.
@SqlNullable
@ScalarFunction
@SqlType(StandardTypes.BOOLEAN)
public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.BIGINT) long value) {
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_NUMBER_INT) && ((parser.getNumberType() == NumberType.INT) || (parser.getNumberType() == NumberType.LONG)) && (parser.getLongValue() == value)) {
return true;
}
}
} catch (IOException e) {
return null;
}
}
use of com.facebook.presto.spi.function.ScalarFunction 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.ScalarFunction 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.ScalarFunction 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.ScalarFunction in project presto by prestodb.
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
DateTime currentDateTime = new DateTime(session.getStartTime(), chronology).withTimeAtStartOfDay();
DateTime baseDateTime = new DateTime(1970, 1, 1, 0, 0, chronology).withTimeAtStartOfDay();
return Days.daysBetween(baseDateTime, currentDateTime).getDays();
}
Aggregations