Line data Source code
1 : #include "backend/engine/semantic/functions/hll_funcs.h"
2 :
3 : #include <vector>
4 :
5 : #include "backend/engine/semantic/value.h"
6 : #include "gtest/gtest.h"
7 :
8 : namespace bigquery_emulator {
9 : namespace backend {
10 : namespace engine {
11 : namespace semantic {
12 : namespace functions {
13 : namespace {
14 :
15 2 : std::string BrSketch() {
16 2 : return std::string("\x12\xef\x7f\x3f\xad\x55\xad\x18\x24\x06\xb9", 11);
17 2 : }
18 :
19 2 : std::string CzSketch() {
20 2 : return std::string("\x12\xef\x7f\x4e\x58\x83\x8d\xb9\xa1\x54\x40", 11);
21 2 : }
22 :
23 2 : std::string UaSketch() {
24 2 : return std::string(
25 2 : "\x12\xef\x7f\x3a\x5f\x10\xe7\xef\xa3\x56\x33\x4e\x58\x83\x8d\xb9\xa1"
26 2 : "\x54\x40",
27 2 : 19);
28 2 : }
29 :
30 1 : std::string MergedSketch() {
31 1 : return std::string(
32 1 : "\x12\xef\x7f\x3a\x5f\x10\xe7\xef\xa3\x56\x33\x3f\xad\x55\xad\x18\x24"
33 1 : "\x06\xb9\x4e\x58\x83\x8d\xb9\xa1\x54\x40",
34 1 : 27);
35 1 : }
36 :
37 1 : TEST(HllFuncsTest, InitMatchesExpectedSketchVectors) {
38 1 : Value customer_id_3 = Value::String("customer_id_3");
39 1 : auto br = HllCountInitValues({customer_id_3}, /*precision=*/10);
40 2 : ASSERT_TRUE(br.ok()) << br.status();
41 1 : EXPECT_EQ(br->bytes_value(), BrSketch());
42 :
43 1 : Value customer_id_2 = Value::String("customer_id_2");
44 1 : auto cz = HllCountInitValues({customer_id_2}, /*precision=*/10);
45 2 : ASSERT_TRUE(cz.ok()) << cz.status();
46 1 : EXPECT_EQ(cz->bytes_value(), CzSketch());
47 :
48 1 : Value customer_id_1 = Value::String("customer_id_1");
49 1 : auto ua = HllCountInitValues({customer_id_1, customer_id_2},
50 1 : /*precision=*/10);
51 2 : ASSERT_TRUE(ua.ok()) << ua.status();
52 1 : EXPECT_EQ(ua->bytes_value(), UaSketch());
53 1 : }
54 :
55 1 : TEST(HllFuncsTest, MergeAndExtractMatchExpectedVectors) {
56 1 : Value ua_sketch = Value::Bytes(UaSketch());
57 1 : Value br_sketch = Value::Bytes(BrSketch());
58 1 : Value cz_sketch = Value::Bytes(CzSketch());
59 1 : auto merge = HllCountMergeAggregate({{ua_sketch, br_sketch, cz_sketch}});
60 2 : ASSERT_TRUE(merge.ok()) << merge.status();
61 1 : EXPECT_EQ(merge->int64_value(), 3);
62 :
63 1 : auto merge_partial =
64 1 : HllCountMergePartialAggregate({{ua_sketch, br_sketch, cz_sketch}});
65 2 : ASSERT_TRUE(merge_partial.ok()) << merge_partial.status();
66 1 : EXPECT_EQ(merge_partial->bytes_value(), MergedSketch());
67 :
68 1 : auto extract_br = HllCountExtractScalar({br_sketch});
69 2 : ASSERT_TRUE(extract_br.ok()) << extract_br.status();
70 1 : EXPECT_EQ(extract_br->int64_value(), 1);
71 :
72 1 : auto extract_cz = HllCountExtractScalar({cz_sketch});
73 2 : ASSERT_TRUE(extract_cz.ok()) << extract_cz.status();
74 1 : EXPECT_EQ(extract_cz->int64_value(), 1);
75 :
76 1 : auto extract_ua = HllCountExtractScalar({ua_sketch});
77 2 : ASSERT_TRUE(extract_ua.ok()) << extract_ua.status();
78 1 : EXPECT_EQ(extract_ua->int64_value(), 2);
79 1 : }
80 :
81 1 : TEST(HllFuncsTest, ExtractNullReturnsZero) {
82 1 : Value null_bytes = Value::NullBytes();
83 1 : auto got = HllCountExtractScalar({null_bytes});
84 2 : ASSERT_TRUE(got.ok()) << got.status();
85 1 : EXPECT_EQ(got->int64_value(), 0);
86 1 : }
87 :
88 : } // namespace
89 : } // namespace functions
90 : } // namespace semantic
91 : } // namespace engine
92 : } // namespace backend
93 : } // namespace bigquery_emulator
|