@LiteralParameters("x") @ScalarOperator(CAST) @SqlType(StandardTypes.BIGINT) public static long castToBigint(@SqlType("varchar(x)") Slice slice) { try { return Long.parseLong(slice.toStringUtf8()); } catch (Exception e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to BIGINT", slice.toStringUtf8())); } }
@SuppressWarnings("NumericCastThatLosesPrecision") private static char getEscapeChar(Slice escape) { String escapeString = escape.toStringUtf8(); if (escapeString.isEmpty()) { // escaping disabled return (char) -1; // invalid character } if (escapeString.length() == 1) { return escapeString.charAt(0); } throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Escape string must be a single character"); } }
@ScalarFunction("from_unixtime") @LiteralParameters("x") @SqlType(StandardTypes.TIMESTAMP_WITH_TIME_ZONE) public static long fromUnixTime(@SqlType(StandardTypes.DOUBLE) double unixTime, @SqlType("varchar(x)") Slice zoneId) { return packDateTimeWithZone(Math.round(unixTime * 1000), zoneId.toStringUtf8()); }
@Description("Throws an exception with a given message") @ScalarFunction(value = "fail", hidden = true) @SqlType("unknown") public static boolean fail(@SqlType(StandardTypes.VARCHAR) Slice message) { throw new PrestoException(StandardErrorCode.GENERIC_USER_ERROR, message.toStringUtf8()); }
@Description("Creates a Bing tile from a QuadKey") @ScalarFunction("bing_tile") @SqlType(BingTileType.NAME) public static long toBingTile(@SqlType(StandardTypes.VARCHAR) Slice quadKey) { checkQuadKey(quadKey); return BingTile.fromQuadKey(quadKey.toStringUtf8()).encode(); }
@LiteralParameters("x") @ScalarOperator(CAST) @SqlType(StandardTypes.REAL) public static long castToFloat(@SqlType("varchar(x)") Slice slice) { try { return Float.floatToIntBits(Float.parseFloat(slice.toStringUtf8())); } catch (Exception e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to REAL", slice.toStringUtf8())); } }
private static OGCGeometry geometryFromText(Slice input) { OGCGeometry geometry; try { geometry = OGCGeometry.fromText(input.toStringUtf8()); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Invalid WKT: " + input.toStringUtf8(), e); } geometry.setSpatialReference(null); return geometry; }
@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); checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to BOOLEAN"); // check no trailing token return result; } catch (IOException | JsonCastException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), BOOLEAN), e); } }
@SqlNullable @Description("Returns TRUE if this Geometry is spatially related to another Geometry") @ScalarFunction("ST_Relate") @SqlType(BOOLEAN) public static Boolean stRelate(@SqlType(GEOMETRY_TYPE_NAME) Slice left, @SqlType(GEOMETRY_TYPE_NAME) Slice right, @SqlType(VARCHAR) Slice relation) { OGCGeometry leftGeometry = deserialize(left); OGCGeometry rightGeometry = deserialize(right); verifySameSpatialReference(leftGeometry, rightGeometry); return leftGeometry.relate(rightGeometry, relation.toStringUtf8()); }
@ScalarOperator(CAST) @LiteralParameters("x") @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long castFromSlice(@SqlType("varchar(x)") Slice value) { return parseTimeWithTimeZone(value.toStringUtf8()); }
@LiteralParameters("x") @ScalarOperator(CAST) @SqlType(StandardTypes.INTEGER) public static long castToInteger(@SqlType("varchar(x)") Slice slice) { try { return Integer.parseInt(slice.toStringUtf8()); } catch (Exception e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to INT", slice.toStringUtf8())); } }
private String getVarcharValue(Type type, Object value) { if (type instanceof VarcharType) { return ((Slice) value).toStringUtf8(); } if (type instanceof TinyintType || type instanceof SmallintType || type instanceof IntegerType || type instanceof BigintType) { return ((Long) value).toString(); } if (type instanceof BooleanType) { return ((Boolean) value).toString(); } throw new PrestoException(NOT_SUPPORTED, format("Unsupported data type in EXPLAIN (TYPE IO): %s", type.getDisplayName())); }
@ScalarOperator(CAST) @SqlNullable @SqlType(BIGINT) public static Long castToBigint(@SqlType(JSON) Slice json) { try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) { parser.nextToken(); Long result = currentTokenAsBigint(parser); checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to BIGINT"); // check no trailing token return result; } catch (IOException | JsonCastException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), BIGINT), e); } }
@InputFunction public static void input( @AggregationState LearnState state, @SqlType(VARCHAR) Slice label, @SqlType("map(bigint,double)") Block features, @SqlType(VARCHAR) Slice parameters) { state.getLabels().add((double) state.enumerateLabel(label.toStringUtf8())); FeatureVector featureVector = ModelUtils.toFeatures(features); state.addMemoryUsage(featureVector.getEstimatedSize()); state.getFeatureVectors().add(featureVector); state.setParameters(parameters); }
@ScalarOperator(OperatorType.CAST) @SqlType(JsonPathType.NAME) @LiteralParameters("x") public static JsonPath castVarcharToJsonPath(@SqlType("varchar(x)") Slice pattern) { return new JsonPath(pattern.toStringUtf8()); }
@LiteralParameters("x") @ScalarOperator(CAST) @SqlType(StandardTypes.DOUBLE) public static double castToDouble(@SqlType("varchar(x)") Slice slice) { try { return Double.parseDouble(slice.toStringUtf8()); } catch (Exception e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to DOUBLE", slice.toStringUtf8())); } }
private static RuntimeException throwMissingKeyException(Type type, InterpretedFunctionInvoker functionInvoker, Object value, ConnectorSession session) { String stringValue; try { stringValue = ((Slice) functionInvoker.invoke(castSignature(VarcharType.VARCHAR, type), session, value)).toStringUtf8(); } catch (RuntimeException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Key not present in map"); } throw new PrestoException(INVALID_FUNCTION_ARGUMENT, format("Key not present in map: %s", stringValue)); } }
@ScalarOperator(CAST) @SqlNullable @SqlType(REAL) public static Long castToReal(@SqlType(JSON) Slice json) { try (JsonParser parser = createJsonParser(JSON_FACTORY, json)) { parser.nextToken(); Long result = currentTokenAsReal(parser); checkCondition(parser.nextToken() == null, INVALID_CAST_ARGUMENT, "Cannot cast input json to REAL"); // check no trailing token return result; } catch (IOException | JsonCastException e) { throw new PrestoException(INVALID_CAST_ARGUMENT, format("Cannot cast '%s' to %s", json.toStringUtf8(), REAL), e); } }
@Description("mongodb ObjectId from the given string") @ScalarFunction("objectid") @SqlType("ObjectId") public static Slice ObjectId(@SqlType(StandardTypes.VARCHAR) Slice value) { return Slices.wrappedBuffer(new ObjectId(CharMatcher.is(' ').removeFrom(value.toStringUtf8())).toByteArray()); }
@ScalarFunction(value = "at_timezone", hidden = true) @LiteralParameters("x") @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) public static long timeAtTimeZone(ConnectorSession session, @SqlType(StandardTypes.TIME_WITH_TIME_ZONE) long timeWithTimeZone, @SqlType("varchar(x)") Slice zoneId) { return timeAtTimeZone(session, timeWithTimeZone, getTimeZoneKey(zoneId.toStringUtf8())); }