@SqlType(StandardTypes.VARCHAR) @TypeParameterContainer({@TypeParameter("decimal(lat_precision, lat_scale)") , @TypeParameter("decimal(lng_precision, lng_scale)")} ) @Nullable public static Slice geohash_encode_dec( @TypeParameter("decimal(lat_precision, lat_scale)") DecimalType latParameter, @TypeParameter("decimal(lng_precision, lng_scale)") DecimalType lngParameter, @SqlType("decimal(lat_precision, lat_scale)") Slice lat, @SqlType("decimal(lng_precision, lng_scale)") Slice lng, @SqlType(StandardTypes.INTEGER) long precision) { BigDecimal biglat = new BigDecimal(Decimals.decodeUnscaledValue(lat), latParameter.getScale()); BigDecimal bigLng = new BigDecimal(Decimals.decodeUnscaledValue(lng), lngParameter.getScale()); return GeohashEncode.geohash_encode(biglat.doubleValue(), bigLng.doubleValue(), precision); }
@InputFunction public static void doubleInput(ApproximateAverageState state, @SqlType(StandardTypes.DOUBLE) double value, @SampleWeight long sampleWeight) { long currentCount = state.getCount(); double currentMean = state.getMean(); // Use numerically stable variant for (int i = 0; i < sampleWeight; i++) { currentCount++; double delta = value - currentMean; currentMean += (delta / currentCount); // update m2 inline state.setM2(state.getM2() + (delta * (value - currentMean))); } // write values back out state.setCount(currentCount); state.setMean(currentMean); state.setSamples(state.getSamples() + 1); }
@InputFunction public static void input(DigestAndPercentileState state, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.DOUBLE) double percentile) { QuantileDigest digest = state.getDigest(); if (digest == null) { digest = new QuantileDigest(0.01); state.setDigest(digest); state.addMemoryUsage(digest.estimatedInMemorySizeInBytes()); } state.addMemoryUsage(-digest.estimatedInMemorySizeInBytes()); digest.add(value); state.addMemoryUsage(digest.estimatedInMemorySizeInBytes()); // use last percentile state.setPercentile(percentile); }
@SqlType(StandardTypes.VARCHAR) public static Slice shuffle_string(@SqlType(StandardTypes.VARCHAR) Slice string) { String id = string.toStringUtf8(); Random rnd = new Random(id.charAt(0)); byte[] bytes = id.getBytes(); for (int i = bytes.length; i > 1; i--) { swap(bytes, i - 1, rnd.nextInt(i)); } return Slices.wrappedBuffer(bytes); }
@TypeParameter(StandardTypes.DOUBLE) @SqlType(StandardTypes.BOOLEAN) @Nullable public static Boolean contains( @TypeParameter(StandardTypes.DOUBLE) Type elementType, @SqlType("array(double)") Block arrayBlock, @SqlType(StandardTypes.DOUBLE) double lng, @SqlType(StandardTypes.DOUBLE) double lat) { double[] array= new double[arrayBlock.getPositionCount()] ; Polygon poly = new Polygon(); for (int i = 0; i < arrayBlock.getPositionCount(); i++) { if (arrayBlock.isNull(i)) { continue; } array[i]=elementType.getDouble(arrayBlock, i); } poly.startPath(array[0], array[1]); for (int i = 2; i < array.length; i += 2) { poly.lineTo(array[i], array[i + 1]); } return OperatorContains.local().execute(poly, new Point(lng,lat), null, null); }
@SqlType(StandardTypes.DOUBLE) public static double haversine(@SqlType(StandardTypes.DOUBLE) double lat1, @SqlType(StandardTypes.DOUBLE) double lng1, @SqlType(StandardTypes.DOUBLE) double lat2, @SqlType(StandardTypes.DOUBLE) double lng2) { double dLat = Math.toRadians(lat2 - lat1); double dLng = Math.toRadians(lng2 - lng1); double a = Math.sin(dLat / 2.0D) * Math.sin(dLat / 2.0D) + Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) * Math.sin(dLng / 2.0D) * Math.sin(dLng / 2.0D); double c = 2.0D * Math.atan2(Math.sqrt(a), Math.sqrt(1.0D - a)); return 6371000.0D * c; }
@SqlType("array(double)") public static Block geohash_decode(@SqlType(StandardTypes.VARCHAR) Slice geohash) { BlockBuilder blockBuilder = DOUBLE.createBlockBuilder(new BlockBuilderStatus(), 2); LatLong coordinates = GeoHash.decodeHash(geohash.toStringUtf8()); DOUBLE.writeDouble(blockBuilder, coordinates.getLat()); DOUBLE.writeDouble(blockBuilder, coordinates.getLon()); return blockBuilder.build(); }
@SqlType(StandardTypes.VARCHAR) public static Slice to_cuebiq_week_format(@SqlType(StandardTypes.DATE) Slice date, @SqlType(StandardTypes.VARCHAR) Slice dateFormat) throws ParseException { SimpleDateFormat formatter = new SimpleDateFormat(dateFormat.toStringUtf8()); Calendar calendar = Calendar.getInstance(); calendar.setMinimalDaysInFirstWeek(4); calendar.setFirstDayOfWeek(Calendar.MONDAY); calendar.setTime(formatter.parse(date.toStringUtf8())); int week = calendar.get(Calendar.WEEK_OF_YEAR); int year = calendar.getWeekYear(); return Slices.utf8Slice(year + "-W" + (week < 10 ? "0" + week : week) + "-1"); }
@Description("hour of the day of the given time") @ScalarFunction("hour") @SqlType(StandardTypes.BIGINT) public static long hourFromTime(ConnectorSession session, @SqlType(StandardTypes.TIME) long time) { return getChronology(session.getTimeZoneKey()).hourOfDay().get(time); }
@InputFunction public static void input( LearnState state, @SqlType(BIGINT) long label, @SqlType("map<bigint,double>") Block features) { input(state, (double) label, features); }
@ScalarFunction @Nullable @SqlType(StandardTypes.VARCHAR) public static Slice jsonExtractScalar(@SqlType(StandardTypes.JSON) Slice json, @SqlType(JsonPathType.NAME) JsonPath jsonPath) { return JsonExtract.extract(json, jsonPath.getScalarExtractor()); }
@Description("round up to nearest integer") @ScalarFunction(alias = "ceil") @SqlType(StandardTypes.BIGINT) public static long ceiling(@SqlType(StandardTypes.BIGINT) long num) { return num; }
@InputFunction public static void input(DigestAndPercentileArrayState state, @SqlType(StandardTypes.BIGINT) long value, @SqlType("array<double>") Block percentilesArrayBlock) { initializePercentilesArray(state, percentilesArrayBlock); initializeDigest(state); QuantileDigest digest = state.getDigest(); state.addMemoryUsage(-digest.estimatedInMemorySizeInBytes()); digest.add(value); state.addMemoryUsage(digest.estimatedInMemorySizeInBytes()); }
@InputFunction public static void weightedInput(DigestAndPercentileArrayState state, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.BIGINT) long weight, @SqlType("array<double>") Block percentilesArrayBlock) { initializePercentilesArray(state, percentilesArrayBlock); initializeDigest(state); QuantileDigest digest = state.getDigest(); state.addMemoryUsage(-digest.estimatedInMemorySizeInBytes()); digest.add(value, weight); state.addMemoryUsage(digest.estimatedInMemorySizeInBytes()); }
@Description("value raised to the power of exponent") @ScalarFunction(alias = "pow") @SqlType(StandardTypes.DOUBLE) public static double power(@SqlType(StandardTypes.DOUBLE) double num, @SqlType(StandardTypes.DOUBLE) double exponent) { return Math.pow(num, exponent); }
@ScalarFunction @SqlType(StandardTypes.VARCHAR) public static Slice bar( @SqlType(StandardTypes.DOUBLE) double percent, @SqlType(StandardTypes.BIGINT) long width, @SqlType(ColorType.NAME) long lowColor, @SqlType(ColorType.NAME) long highColor) { long count = (int) (percent * width); count = Math.min(width, count); count = Math.max(0, count); StringBuilder builder = new StringBuilder(); for (int i = 0; i < count; i++) { float fraction = (float) (i * 1.0 / (width - 1)); int color = interpolate(fraction, lowColor, highColor); builder.append(ansiColorEscape(color)) .append('\u2588'); } // reset builder.append(ANSI_RESET); // pad to force column to be the requested width for (long i = count; i < width; ++i) { builder.append(' '); } return utf8Slice(builder.toString()); }
@Nullable @ScalarFunction("json_array_get") @SqlType(StandardTypes.JSON) public static Slice varcharJsonArrayGet(@SqlType(StandardTypes.VARCHAR) Slice json, @SqlType(StandardTypes.BIGINT) long index) { return jsonArrayGet(json, index); }
@InputFunction public static void input(ApproximateDoubleSumState state, @SqlType(StandardTypes.DOUBLE) double value, @SampleWeight long sampleWeight) { state.setWeightedCount(state.getWeightedCount() + sampleWeight); state.setSum(state.getSum() + value * sampleWeight); updateVarianceState(state, value); }
@InputFunction public static void input(ApproximateLongSumState state, @SqlType(StandardTypes.BIGINT) long value, @SampleWeight long sampleWeight) { state.setWeightedCount(state.getWeightedCount() + sampleWeight); state.setSum(state.getSum() + value * sampleWeight); updateVarianceState(state, value); }
@InputFunction @IntermediateInputFunction public static void sum(NullableLongState state, @SqlType(StandardTypes.BIGINT) long value) { state.setNull(false); state.setLong(state.getLong() + value); }
@Description("year of the ISO week of the given timestamp") @ScalarFunction(value = "year_of_week", alias = "yow") @SqlType(StandardTypes.BIGINT) public static long yearOfWeekFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { return getChronology(session.getTimeZoneKey()).weekyear().get(timestamp); }
@Description("natural logarithm") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double ln(@SqlType(StandardTypes.DOUBLE) double num) { return Math.log(num); }
@Nullable @Description("extract query from url") @ScalarFunction @SqlType(StandardTypes.VARCHAR) public static Slice urlExtractQuery(@SqlType(StandardTypes.VARCHAR) Slice url) { URI uri = parseUrl(url); return (uri == null) ? null : slice(uri.getQuery()); }
@Description("transforms the string to normalized form") @ScalarFunction @SqlType(StandardTypes.VARCHAR) public static Slice normalize(@SqlType(StandardTypes.VARCHAR) Slice slice, @SqlType(StandardTypes.VARCHAR) Slice form) { Normalizer.Form targetForm; try { targetForm = Normalizer.Form.valueOf(form.toStringUtf8()); } catch (IllegalArgumentException e) { throw new PrestoException(INVALID_FUNCTION_ARGUMENT, "Normalization form must be one of [NFD, NFC, NFKD, NFKC]"); } return utf8Slice(Normalizer.normalize(slice.toStringUtf8(), targetForm)); }
@Description("returns substrings matching a regular expression") @ScalarFunction @SqlType(StandardTypes.BOOLEAN) public static boolean regexpLike(@SqlType(StandardTypes.VARCHAR) Slice source, @SqlType(RegexpType.NAME) Regex pattern) { Matcher m = pattern.matcher(source.getBytes()); int offset = m.search(0, source.length(), Option.DEFAULT); return offset != -1; }
@InputFunction public static void input(HyperLogLogState state, @SqlType(StandardTypes.BIGINT) long value, @SqlType(StandardTypes.DOUBLE) double maxStandardError) { HyperLogLog hll = getOrCreateHyperLogLog(state, maxStandardError); state.addMemoryUsage(-hll.estimatedInMemorySize()); hll.add(value); state.addMemoryUsage(hll.estimatedInMemorySize()); }
@Description("decode URL safe base64 encoded binary data") @ScalarFunction("from_base64url") @SqlType(StandardTypes.VARBINARY) public static Slice fromBase64UrlVarbinary(@SqlType(StandardTypes.VARBINARY) Slice slice) { return Slices.wrappedBuffer(Base64.getUrlDecoder().decode(slice.getBytes())); }
@ScalarFunction("test_row") @SqlType("row<double,array<row<bigint,double>('col0','col1')>,row<bigint,double>('col0','col1')>('col0','col1','col2')") public static Block testNestedRowWithArray( @Nullable @SqlType(StandardTypes.DOUBLE) Double arg1, @Nullable @SqlType("array<row<bigint,double>('col0','col1')>") Block arg2, @Nullable @SqlType("row<bigint,double>('col0','col1')") Block arg3) { List<Type> parameterTypes = ImmutableList.of( DOUBLE, new ArrayType(new RowType(ImmutableList.of(BIGINT, DOUBLE), Optional.of(ImmutableList.of("col0", "col1")))), new RowType(ImmutableList.of(BIGINT, DOUBLE), Optional.of(ImmutableList.of("col0", "col1")))); return toStackRepresentation(parameterTypes, arg1, arg2, arg3); }
private List<Type> getInputTypes(Method inputFunction) { ImmutableList.Builder<Type> builder = ImmutableList.builder(); Annotation[][] parameterAnnotations = inputFunction.getParameterAnnotations(); for (Annotation[] annotations : parameterAnnotations) { for (Annotation annotation : annotations) { if (annotation instanceof SqlType) { String typeName = ((SqlType) annotation).value(); builder.add(typeManager.getType(parseTypeSignature(typeName))); } } } return builder.build(); }
@Description("Euler's number") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double e() { return Math.E; }
@InputFunction public static void input( LearnState state, @SqlType(BIGINT) long label, @SqlType("map<bigint,double>") Block features, @SqlType(VARCHAR) Slice parameters) { input(state, (double) label, features, parameters); }
@Description("day of the week of the given timestamp") @ScalarFunction(value = "day_of_week", alias = "dow") @SqlType(StandardTypes.BIGINT) public static long dayOfWeekFromTimestamp(ConnectorSession session, @SqlType(StandardTypes.TIMESTAMP) long timestamp) { return getChronology(session.getTimeZoneKey()).dayOfWeek().get(timestamp); }
@Description("reverse all code points in a given string") @ScalarFunction @SqlType(StandardTypes.VARCHAR) public static Slice reverse(@SqlType(StandardTypes.VARCHAR) Slice slice) { return SliceUtf8.reverse(slice); }
@Nullable @ScalarFunction("json_array_contains") @SqlType(StandardTypes.BOOLEAN) public static Boolean varcharJsonArrayContains(@SqlType(StandardTypes.VARCHAR) Slice json, @SqlType(StandardTypes.VARCHAR) Slice value) { return jsonArrayContains(json, value); }
@Nullable @ScalarFunction("json_array_contains") @SqlType(StandardTypes.BOOLEAN) public static Boolean varcharJsonArrayContains(@SqlType(StandardTypes.VARCHAR) Slice json, @SqlType(StandardTypes.BOOLEAN) boolean value) { return jsonArrayContains(json, value); }
@Description("hour of the day of the given interval") @ScalarFunction("hour") @SqlType(StandardTypes.BIGINT) public static long hourFromInterval(@SqlType(StandardTypes.INTERVAL_DAY_TO_SECOND) long milliseconds) { return (milliseconds % MILLISECONDS_IN_DAY) / MILLISECONDS_IN_HOUR; }
@Nullable @ScalarFunction @SqlType(StandardTypes.BOOLEAN) public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.BIGINT) long value) { try (JsonParser parser = JSON_FACTORY.createParser(json.getInput())) { 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; } }
@Description("the constant Pi") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double pi() { return Math.PI; }
@Description("arc sine") @ScalarFunction @SqlType(StandardTypes.DOUBLE) public static double asin(@SqlType(StandardTypes.DOUBLE) double num) { return Math.asin(num); }
@Nullable @ScalarFunction @SqlType(StandardTypes.BOOLEAN) public static Boolean jsonArrayContains(@SqlType(StandardTypes.JSON) Slice json, @SqlType(StandardTypes.VARCHAR) Slice value) { String valueString = value.toStringUtf8(); try (JsonParser parser = JSON_FACTORY.createParser(json.getInput())) { 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; } }