iceberg-cpp
Loading...
Searching...
No Matches
metrics_test_base.h
1/*
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements. See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership. The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License. You may obtain a copy of the License at
9 *
10 * http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing,
13 * software distributed under the License is distributed on an
14 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15 * KIND, either express or implied. See the License for the
16 * specific language governing permissions and limitations
17 * under the License.
18 */
19
20#pragma once
21
22#include <memory>
23#include <optional>
24#include <string>
25#include <vector>
26
27#include <arrow/array.h>
28
29#include "iceberg/file_io.h"
30#include "iceberg/metrics.h"
32#include "iceberg/result.h"
33#include "iceberg/schema.h"
34
35namespace iceberg::test {
36
42 protected:
43 virtual void SetUp();
44
46 virtual Result<Metrics> GetMetrics(std::shared_ptr<Schema> schema,
47 std::shared_ptr<::arrow::Array> records) = 0;
48
50 virtual Result<Metrics> GetMetrics(std::shared_ptr<Schema> schema,
51 std::shared_ptr<MetricsConfig> config,
52 std::shared_ptr<::arrow::Array> records) = 0;
53
55 virtual std::string CreateOutputFile() = 0;
56
58 virtual Result<int> GetSplitCount() = 0;
59
61 virtual bool SupportsSmallRowGroups() const { return false; }
62
64 virtual bool ReportsNanCounts() const { return false; }
65
66 // Helper methods for assertions
67 void AssertCounts(int field_id, std::optional<int64_t> expected_value_count,
68 std::optional<int64_t> expected_null_count, const Metrics& metrics);
69
70 void AssertCounts(int field_id, std::optional<int64_t> expected_value_count,
71 std::optional<int64_t> expected_null_count,
72 std::optional<int64_t> expected_nan_count, const Metrics& metrics);
73
74 void AssertColumnSizeFields(std::vector<int32_t> expected_field_ids,
75 const Metrics& metrics);
76
77 template <typename T>
78 void AssertBounds(int field_id, std::shared_ptr<PrimitiveType> type,
79 std::optional<T> expected_lower, std::optional<T> expected_upper,
80 const Metrics& metrics);
81
82 // Helper methods for creating test data
83 std::shared_ptr<::arrow::Array> CreateRecordArrays(
84 const std::shared_ptr<::arrow::Schema>& arrow_schema, const std::string& json_data);
85
86 // Common test schemas
87 static std::shared_ptr<Schema> SimpleSchema();
88 static std::shared_ptr<Schema> NestedSchema();
89 static std::shared_ptr<Schema> FloatDoubleSchema();
90
91 // Test case methods - subclasses should call these from TEST_F macros
92 void MetricsForRepeatedValues();
93 void MetricsForTopLevelFields();
94 void MetricsForDecimals();
95 void MetricsForNestedStructFields();
96 void MetricsModeForNestedStructFields();
97 void MetricsForListAndMapElements();
98 void MetricsForNullColumns();
99 void MetricsForNaNColumns();
100 void ColumnBoundsWithNaNValueAtFront();
101 void ColumnBoundsWithNaNValueInMiddle();
102 void ColumnBoundsWithNaNValueAtEnd();
103 void MetricsForTopLevelWithMultipleRowGroup();
104 void MetricsForNestedStructFieldsWithMultipleRowGroup();
105 void NoneMetricsMode();
106 void CountsMetricsMode();
107 void FullMetricsMode();
108 void TruncateStringMetricsMode();
109 void TruncateBinaryMetricsMode();
110
111 private:
112 Result<std::shared_ptr<::arrow::Array>> BuildSimpleRecords(
113 std::shared_ptr<::arrow::Schema> arrow_schema, int32_t count = 1);
114 Result<std::shared_ptr<::arrow::Array>> BuildNestedRecords(int32_t count = 1);
115
116 protected:
117 std::shared_ptr<FileIO> file_io_;
118 std::string temp_dir_;
119 std::string path_;
120};
121
122#define DEFINE_METRICS_TEST_CASE(TestClass, Case) \
123 TEST_F(TestClass, Case) { Case(); }
124
125#define DEFINE_METRICS_TESTS(TestClass) \
126 DEFINE_METRICS_TEST_CASE(TestClass, MetricsForRepeatedValues) \
127 DEFINE_METRICS_TEST_CASE(TestClass, MetricsForTopLevelFields) \
128 DEFINE_METRICS_TEST_CASE(TestClass, MetricsForNestedStructFields) \
129 DEFINE_METRICS_TEST_CASE(TestClass, MetricsForNullColumns) \
130 DEFINE_METRICS_TEST_CASE(TestClass, MetricsForNaNColumns) \
131 DEFINE_METRICS_TEST_CASE(TestClass, ColumnBoundsWithNaNValueAtFront) \
132 DEFINE_METRICS_TEST_CASE(TestClass, ColumnBoundsWithNaNValueInMiddle) \
133 DEFINE_METRICS_TEST_CASE(TestClass, ColumnBoundsWithNaNValueAtEnd) \
134 DEFINE_METRICS_TEST_CASE(TestClass, MetricsForDecimals) \
135 DEFINE_METRICS_TEST_CASE(TestClass, MetricsForListAndMapElements) \
136 DEFINE_METRICS_TEST_CASE(TestClass, MetricsModeForNestedStructFields) \
137 DEFINE_METRICS_TEST_CASE(TestClass, NoneMetricsMode) \
138 DEFINE_METRICS_TEST_CASE(TestClass, CountsMetricsMode) \
139 DEFINE_METRICS_TEST_CASE(TestClass, FullMetricsMode) \
140 DEFINE_METRICS_TEST_CASE(TestClass, TruncateStringMetricsMode) \
141 DEFINE_METRICS_TEST_CASE(TestClass, TruncateBinaryMetricsMode) \
142 DEFINE_METRICS_TEST_CASE(TestClass, MetricsForTopLevelWithMultipleRowGroup) \
143 DEFINE_METRICS_TEST_CASE(TestClass, MetricsForNestedStructFieldsWithMultipleRowGroup)
144
145} // namespace iceberg::test
Base test class for metrics testing, similar to Java's TestMetrics.
Definition metrics_test_base.h:41
virtual Result< Metrics > GetMetrics(std::shared_ptr< Schema > schema, std::shared_ptr< MetricsConfig > config, std::shared_ptr<::arrow::Array > records)=0
Get metrics with custom MetricsConfig.
virtual Result< Metrics > GetMetrics(std::shared_ptr< Schema > schema, std::shared_ptr<::arrow::Array > records)=0
Get metrics for the given schema and records.
virtual Result< int > GetSplitCount()=0
Get the number of row groups/splits in a file.
virtual bool ReportsNanCounts() const
Whether the format reports NaN counts for floating-point fields.
Definition metrics_test_base.h:64
virtual bool SupportsSmallRowGroups() const
Whether the format supports small row groups for testing.
Definition metrics_test_base.h:61
virtual std::string CreateOutputFile()=0
Create an output file for testing.
Metrics configuration for Iceberg tables.
Iceberg file format metrics.
Definition metrics.h:63