Skip to content

Commit 8804747

Browse files
committed
minor polish
1 parent 282b708 commit 8804747

File tree

6 files changed

+39
-40
lines changed

6 files changed

+39
-40
lines changed

src/iceberg/expression/aggregate.cc

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,19 @@ std::shared_ptr<PrimitiveType> GetPrimitiveType(const BoundTerm& term) {
4242
return internal::checked_pointer_cast<PrimitiveType>(term.type());
4343
}
4444

45+
/// \brief A single-field StructLike that wraps a Literal
46+
class SingleValueStructLike : public StructLike {
47+
public:
48+
explicit SingleValueStructLike(Literal literal) : literal_(std::move(literal)) {}
49+
50+
Result<Scalar> GetField(size_t) const override { return LiteralToScalar(literal_); }
51+
52+
size_t num_fields() const override { return 1; }
53+
54+
private:
55+
Literal literal_;
56+
};
57+
4558
Result<Literal> EvaluateBoundTerm(const BoundTerm& term,
4659
const std::optional<std::vector<uint8_t>>& bound) {
4760
auto ptype = GetPrimitiveType(term);
@@ -111,6 +124,7 @@ class MaxAggregator : public BoundAggregate::Aggregator {
111124

112125
if (auto ordering = value <=> current_;
113126
ordering == std::partial_ordering::unordered) {
127+
valid_ = false;
114128
return InvalidArgument("Cannot compare literal {} with current value {}",
115129
value.ToString(), current_.ToString());
116130
} else if (ordering == std::partial_ordering::greater) {
@@ -140,6 +154,7 @@ class MaxAggregator : public BoundAggregate::Aggregator {
140154

141155
if (auto ordering = value <=> current_;
142156
ordering == std::partial_ordering::unordered) {
157+
valid_ = false;
143158
return InvalidArgument("Cannot compare literal {} with current value {}",
144159
value.ToString(), current_.ToString());
145160
} else if (ordering == std::partial_ordering::greater) {
@@ -181,6 +196,7 @@ class MinAggregator : public BoundAggregate::Aggregator {
181196

182197
if (auto ordering = value <=> current_;
183198
ordering == std::partial_ordering::unordered) {
199+
valid_ = false;
184200
return InvalidArgument("Cannot compare literal {} with current value {}",
185201
value.ToString(), current_.ToString());
186202
} else if (ordering == std::partial_ordering::less) {
@@ -209,6 +225,7 @@ class MinAggregator : public BoundAggregate::Aggregator {
209225

210226
if (auto ordering = value <=> current_;
211227
ordering == std::partial_ordering::unordered) {
228+
valid_ = false;
212229
return InvalidArgument("Cannot compare literal {} with current value {}",
213230
value.ToString(), current_.ToString());
214231
} else if (ordering == std::partial_ordering::less) {

src/iceberg/expression/aggregate.h

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,13 @@ class ICEBERG_EXPORT BoundAggregate : public Aggregate<BoundTerm>, public Bound
111111

112112
virtual Status Update(const DataFile& file) = 0;
113113

114-
virtual bool IsValid() const { return true; }
114+
/// \brief Whether the aggregator is still valid.
115+
virtual bool IsValid() const = 0;
115116

116117
/// \brief Get the result of the aggregation.
117118
/// \return The result of the aggregation.
118119
/// \note It is an undefined behavior to call this method if any previous Update call
119-
/// has returned an error.
120+
/// has returned an error or if IsValid() returns false.
120121
virtual Literal GetResult() const = 0;
121122
};
122123

@@ -127,6 +128,7 @@ class ICEBERG_EXPORT BoundAggregate : public Aggregate<BoundTerm>, public Bound
127128
}
128129

129130
Result<Literal> Evaluate(const StructLike& data) const override = 0;
131+
130132
virtual Result<Literal> Evaluate(const DataFile& file) const = 0;
131133

132134
/// \brief Whether metrics in the data file are sufficient to evaluate.
@@ -146,7 +148,7 @@ class ICEBERG_EXPORT BoundAggregate : public Aggregate<BoundTerm>, public Bound
146148
/// \brief Base class for COUNT aggregates.
147149
class ICEBERG_EXPORT CountAggregate : public BoundAggregate {
148150
public:
149-
Result<Literal> Evaluate(const StructLike& data) const final;
151+
Result<Literal> Evaluate(const StructLike& data) const override;
150152
Result<Literal> Evaluate(const DataFile& file) const override;
151153

152154
std::unique_ptr<Aggregator> NewAggregator() const override;
@@ -156,8 +158,6 @@ class ICEBERG_EXPORT CountAggregate : public BoundAggregate {
156158
/// \brief Count using metrics from a data file.
157159
virtual Result<int64_t> CountFor(const DataFile& file) const = 0;
158160

159-
bool HasValue(const DataFile& file) const override = 0;
160-
161161
protected:
162162
CountAggregate(Expression::Operation op, std::shared_ptr<BoundTerm> term)
163163
: BoundAggregate(op, std::move(term)) {}
@@ -171,7 +171,6 @@ class ICEBERG_EXPORT CountNonNullAggregate : public CountAggregate {
171171

172172
Result<int64_t> CountFor(const StructLike& data) const override;
173173
Result<int64_t> CountFor(const DataFile& file) const override;
174-
175174
bool HasValue(const DataFile& file) const override;
176175

177176
private:
@@ -186,7 +185,6 @@ class ICEBERG_EXPORT CountNullAggregate : public CountAggregate {
186185

187186
Result<int64_t> CountFor(const StructLike& data) const override;
188187
Result<int64_t> CountFor(const DataFile& file) const override;
189-
190188
bool HasValue(const DataFile& file) const override;
191189

192190
private:
@@ -200,7 +198,6 @@ class ICEBERG_EXPORT CountStarAggregate : public CountAggregate {
200198

201199
Result<int64_t> CountFor(const StructLike& data) const override;
202200
Result<int64_t> CountFor(const DataFile& file) const override;
203-
204201
bool HasValue(const DataFile& file) const override;
205202

206203
private:
@@ -213,12 +210,11 @@ class ICEBERG_EXPORT MaxAggregate : public BoundAggregate {
213210
static std::shared_ptr<MaxAggregate> Make(std::shared_ptr<BoundTerm> term);
214211

215212
Result<Literal> Evaluate(const StructLike& data) const override;
216-
Result<Literal> Evaluate(const DataFile& file) const final;
213+
Result<Literal> Evaluate(const DataFile& file) const override;
214+
bool HasValue(const DataFile& file) const override;
217215

218216
std::unique_ptr<Aggregator> NewAggregator() const override;
219217

220-
bool HasValue(const DataFile& file) const override;
221-
222218
private:
223219
explicit MaxAggregate(std::shared_ptr<BoundTerm> term);
224220
};
@@ -229,12 +225,11 @@ class ICEBERG_EXPORT MinAggregate : public BoundAggregate {
229225
static std::shared_ptr<MinAggregate> Make(std::shared_ptr<BoundTerm> term);
230226

231227
Result<Literal> Evaluate(const StructLike& data) const override;
232-
Result<Literal> Evaluate(const DataFile& file) const final;
228+
Result<Literal> Evaluate(const DataFile& file) const override;
229+
bool HasValue(const DataFile& file) const override;
233230

234231
std::unique_ptr<Aggregator> NewAggregator() const override;
235232

236-
bool HasValue(const DataFile& file) const override;
237-
238233
private:
239234
explicit MinAggregate(std::shared_ptr<BoundTerm> term);
240235
};

src/iceberg/expression/term.h

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,13 @@ namespace iceberg {
3737
/// \brief A term is an expression node that produces a typed value when evaluated.
3838
class ICEBERG_EXPORT Term : public util::Formattable {
3939
public:
40-
enum class Kind : uint8_t { kReference = 0, kTransform, kExtract };
40+
enum class Kind : uint8_t { kReference, kTransform, kExtract };
4141

4242
/// \brief Returns the kind of this term.
4343
virtual Kind kind() const = 0;
44+
45+
/// \brief Returns whether this term is unbound.
46+
virtual bool is_unbound() const = 0;
4447
};
4548

4649
template <typename T>
@@ -53,6 +56,8 @@ template <typename B>
5356
class ICEBERG_EXPORT UnboundTerm : public Unbound<B>, public Term {
5457
public:
5558
using BoundType = B;
59+
60+
bool is_unbound() const override { return true; }
5661
};
5762

5863
/// \brief Base class for bound terms.
@@ -66,8 +71,6 @@ class ICEBERG_EXPORT BoundTerm : public Bound, public Term {
6671
/// \brief Returns whether this term may produce null values.
6772
virtual bool MayProduceNull() const = 0;
6873

69-
// TODO(gangwu): add a comparator function to Literal and BoundTerm.
70-
7174
/// \brief Returns whether this term is equivalent to another.
7275
///
7376
/// Two terms are equivalent if they produce the same values when evaluated.
@@ -79,6 +82,8 @@ class ICEBERG_EXPORT BoundTerm : public Bound, public Term {
7982
friend bool operator==(const BoundTerm& lhs, const BoundTerm& rhs) {
8083
return lhs.Equals(rhs);
8184
}
85+
86+
bool is_unbound() const override { return false; }
8287
};
8388

8489
/// \brief A reference represents a named field in an expression.
@@ -114,6 +119,8 @@ class ICEBERG_EXPORT NamedReference
114119

115120
Kind kind() const override { return Kind::kReference; }
116121

122+
bool is_unbound() const override { return true; }
123+
117124
private:
118125
explicit NamedReference(std::string field_name);
119126

@@ -152,6 +159,8 @@ class ICEBERG_EXPORT BoundReference
152159

153160
Kind kind() const override { return Kind::kReference; }
154161

162+
bool is_unbound() const override { return false; }
163+
155164
private:
156165
BoundReference(SchemaField field, std::unique_ptr<StructLikeAccessor> accessor);
157166

src/iceberg/row/struct_like.cc

Lines changed: 0 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -68,15 +68,6 @@ Result<Scalar> LiteralToScalar(const Literal& literal) {
6868
}
6969
}
7070

71-
SingleValueStructLike::SingleValueStructLike(Literal literal)
72-
: literal_(std::move(literal)) {}
73-
74-
Result<Scalar> SingleValueStructLike::GetField(size_t /*pos*/) const {
75-
return LiteralToScalar(literal_);
76-
}
77-
78-
size_t SingleValueStructLike::num_fields() const { return 1; }
79-
8071
StructLikeAccessor::StructLikeAccessor(std::shared_ptr<Type> type,
8172
std::span<const size_t> position_path)
8273
: type_(std::move(type)) {

src/iceberg/row/struct_like.h

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,19 +71,6 @@ class ICEBERG_EXPORT StructLike {
7171
virtual size_t num_fields() const = 0;
7272
};
7373

74-
/// \brief A single-field StructLike that wraps a Literal
75-
class ICEBERG_EXPORT SingleValueStructLike : public StructLike {
76-
public:
77-
explicit SingleValueStructLike(Literal literal);
78-
79-
Result<Scalar> GetField(size_t pos) const override;
80-
81-
size_t num_fields() const override;
82-
83-
private:
84-
Literal literal_;
85-
};
86-
8774
/// \brief An immutable array-like wrapper.
8875
class ICEBERG_EXPORT ArrayLike {
8976
public:

src/iceberg/test/aggregate_test.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -400,7 +400,7 @@ TEST(AggregateTest, DataFileAggregatorParity) {
400400
for (const auto& f : files) {
401401
ASSERT_TRUE(evaluator->Update(f).has_value());
402402
}
403-
EXPECT_EQ(evaluator->AllAggregatorsValid(), expect_all_valid);
403+
ASSERT_EQ(evaluator->AllAggregatorsValid(), expect_all_valid);
404404
ICEBERG_UNWRAP_OR_FAIL(auto results, evaluator->GetResults());
405405
ASSERT_EQ(results.size(), expected.size());
406406
for (size_t i = 0; i < expected.size(); ++i) {

0 commit comments

Comments
 (0)