@ScalarFunction(value = "construct_tdigest", visibility = EXPERIMENTAL)
    @Description("Create a TDigest by passing in its internal state.")
    @SqlType("tdigest(double)")
    public static Slice constructTDigest(
            @SqlType("array(double)") Block centroidMeansBlock,
            @SqlType("array(double)") Block centroidWeightsBlock,
            @SqlType(StandardTypes.DOUBLE) double compression,
            @SqlType(StandardTypes.DOUBLE) double min,
            @SqlType(StandardTypes.DOUBLE) double max,
            @SqlType(StandardTypes.DOUBLE) double sum,
            @SqlType(StandardTypes.BIGINT) long count)
    {
        double[] centroidMeans = new double[centroidMeansBlock.getPositionCount()];
        for (int i = 0; i < centroidMeansBlock.getPositionCount(); i++) {
            centroidMeans[i] = DOUBLE.getDouble(centroidMeansBlock, i);
        }
        double[] centroidWeights = new double[centroidWeightsBlock.getPositionCount()];
        for (int i = 0; i < centroidWeightsBlock.getPositionCount(); i++) {
            centroidWeights[i] = DOUBLE.getDouble(centroidWeightsBlock, i);
        }

        TDigest tDigest = createTDigest(
                centroidMeans,
                centroidWeights,
                compression,
                min,
                max,
                sum,
                toIntExact(count));

        return tDigest.serialize();
    }
@Test
    public void testConstructTDigest()
    {
        TDigest tDigest = createTDigest(STANDARD_COMPRESSION_FACTOR);
        ImmutableList<Double> values = ImmutableList.of(0.0d, 1.0d, 2.0d, 3.0d, 4.0d, 5.0d, 6.0d, 7.0d, 8.0d, 9.0d);
        values.stream().forEach(tDigest::add);

        List<Double> weights = Collections.nCopies(values.size(), 1.0);
        double compression = Double.valueOf(STANDARD_COMPRESSION_FACTOR);
        double min = values.stream().reduce(Double.POSITIVE_INFINITY, Double::min);
        double max = values.stream().reduce(Double.NEGATIVE_INFINITY, Double::max);
        double sum = values.stream().reduce(0.0d, Double::sum);
        int count = values.size();

        String sql = format("construct_tdigest(ARRAY%s, ARRAY%s, %s, %s, %s, %s, %s)",
                values,
                weights,
                compression,
                min,
                max,
                sum,
                count);

        functionAssertions.selectSingleValue(
                sql,
                TDIGEST_DOUBLE,
                SqlVarbinary.class);
    }