Skip to content

Commit

Permalink
Add support for generic parameters in make_set_digest function.
Browse files Browse the repository at this point in the history
Cherry-pick of trinodb/trino@b41d388 (trinodb/trino#8295)

Co-authored-by: Marius Grama <[email protected]>
  • Loading branch information
2 people authored and tdcmeehan committed Sep 25, 2023
1 parent 0dde0a2 commit 081f215
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,14 @@
package com.facebook.presto.type.setdigest;

import com.facebook.presto.common.block.BlockBuilder;
import com.facebook.presto.common.type.StandardTypes;
import com.facebook.presto.spi.function.AggregationFunction;
import com.facebook.presto.spi.function.AggregationState;
import com.facebook.presto.spi.function.CombineFunction;
import com.facebook.presto.spi.function.InputFunction;
import com.facebook.presto.spi.function.OutputFunction;
import com.facebook.presto.spi.function.SqlType;
import com.facebook.presto.spi.function.TypeParameter;
import io.airlift.slice.Slice;

@AggregationFunction("make_set_digest")
public final class BuildSetDigestAggregation
Expand All @@ -30,7 +32,18 @@ public final class BuildSetDigestAggregation
private BuildSetDigestAggregation() {}

@InputFunction
public static void input(SetDigestState state, @SqlType(StandardTypes.BIGINT) long value)
@TypeParameter("T")
public static void input(@AggregationState SetDigestState state, @SqlType("T") long value)
{
if (state.getDigest() == null) {
state.setDigest(new SetDigest());
}
state.getDigest().add(value);
}

@InputFunction
@TypeParameter("T")
public static void input(@AggregationState SetDigestState state, @SqlType("T") Slice value)
{
if (state.getDigest() == null) {
state.setDigest(new SetDigest());
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.facebook.presto.type.setdigest;

import com.facebook.presto.sql.query.QueryAssertions;
import org.testng.annotations.AfterClass;
import org.testng.annotations.BeforeClass;
import org.testng.annotations.Test;

public class TestMakeSetDigest
{
private QueryAssertions assertions;

@BeforeClass
public void init()
{
assertions = new QueryAssertions();
}

@AfterClass(alwaysRun = true)
public void teardown()
{
assertions.close();
assertions = null;
}

@Test
public void testMakeSetDigestInteger()
{
assert assertions.getQueryRunner().execute(
"SELECT make_set_digest(value) " +
"FROM (VALUES 1, 1, 1, 2, 2) T(value)").getMaterializedRows().get(0).getField(0).toString().replaceAll("\n", " ")
.equals("01 0c 00 00 00 02 0b 02 00 80 03 44 00 80 20 08 de 00 20 00 00 02 00 00 00 a8 c0 76 6c a0 20 08 de 4a c4 05 fb b7 03 44 00 02 00 03 00");
}

@Test
public void testMakeSetDigestLong()
{
assert assertions.getQueryRunner().execute(
"SELECT make_set_digest(value) " +
"FROM (VALUES -398741129664271, 9000000125356546) T(value)").getMaterializedRows().get(0).getField(0).toString().replaceAll("\n", " ")
.equals("01 0c 00 00 00 02 0b 02 00 02 5f e3 93 40 15 34 fa 00 20 00 00 02 00 00 00 e0 7f 7d fb 0d 5f e3 93 67 9b 35 b1 60 15 34 fa 01 00 01 00");
}

@Test
public void testMakeSetDigestFloat()
{
assert assertions.getQueryRunner().execute(
"SELECT make_set_digest(value) " +
"FROM (VALUES 1.2, 2.3) T(value)").getMaterializedRows().get(0).getField(0).toString().replaceAll("\n", " ")
.equals("01 0c 00 00 00 02 0b 02 00 00 fd db 53 80 bb 20 9d 00 20 00 00 02 00 00 00 f6 96 17 64 b0 bb 20 9d 19 15 c6 ec 29 fd db 53 01 00 01 00");
}

@Test
public void testMakeSetDigestChar()
{
assert assertions.getQueryRunner().execute(
"SELECT make_set_digest(value) " +
"FROM (VALUES 'c') T(value)").getMaterializedRows().get(0).getField(0).toString().replaceAll("\n", " ")
.equals("01 08 00 00 00 02 0b 01 00 40 df 38 8e 00 20 00 00 01 00 00 00 d7 74 1f 4a 6c df 38 8e 01 00");
}

@Test
public void testMakeSetDigestVarchar()
{
assert assertions.getQueryRunner().execute(
"SELECT make_set_digest(value) " +
"FROM (VALUES 'abc', 'def') T(value)").getMaterializedRows().get(0).getField(0).toString().replaceAll("\n", " ")
.equals("01 0c 00 00 00 02 0b 02 00 40 a5 e1 87 00 3f 96 b4 00 20 00 00 02 00 00 00 9b db 75 37 71 a5 e1 87 67 78 ad 3f 3f 3f 96 b4 01 00 01 00");
}

@Test
public void testMakeSetDigestDate()
{
assert assertions.getQueryRunner().execute(
"SELECT make_set_digest(value) " +
"FROM (VALUES 2009-09-15, 1998-11-30) T(value)").getMaterializedRows().get(0).getField(0).toString().replaceAll("\n", " ")
.equals("01 0c 00 00 00 02 0b 02 00 41 50 11 6d 82 6f 8c 78 00 20 00 00 02 00 00 00 36 63 98 5e 5e 50 11 6d c2 d0 81 a4 8d 6f 8c 78 01 00 01 00");
}
}

0 comments on commit 081f215

Please sign in to comment.