Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion src/iceberg/expression/binder.cc
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,10 @@ Result<FieldIdsSetRef> ReferenceVisitor::Predicate(

Result<FieldIdsSetRef> ReferenceVisitor::Aggregate(
const std::shared_ptr<BoundAggregate>& aggregate) {
referenced_field_ids_.insert(aggregate->reference()->field_id());
// Aggregates without a term (e.g. COUNT(*)) reference no field ids.
if (const auto& reference = aggregate->reference()) {
referenced_field_ids_.insert(reference->field_id());
}
return referenced_field_ids_;
}

Expand Down
19 changes: 19 additions & 0 deletions src/iceberg/test/expression_visitor_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -805,6 +805,25 @@ TEST_F(ReferenceVisitorTest, Constants) {
EXPECT_TRUE(refs_false.empty());
}

TEST_F(ReferenceVisitorTest, CountStar) {
// COUNT(*) has no referenced field; the aggregate's reference() is null, so
// GetReferencedFieldIds must return an empty set instead of dereferencing it.
ICEBERG_UNWRAP_OR_FAIL(auto bound_count_star, Bind(Expressions::CountStar()));

ICEBERG_UNWRAP_OR_FAIL(auto refs,
ReferenceVisitor::GetReferencedFieldIds(bound_count_star));
EXPECT_TRUE(refs.empty());
}

TEST_F(ReferenceVisitorTest, AggregateWithTerm) {
// An aggregate with a term (e.g. MAX(age)) still references its field, so the
// null guard must not drop the non-null case. age has field id 3.
ICEBERG_UNWRAP_OR_FAIL(auto bound_max, Bind(Expressions::Max("age")));

ICEBERG_UNWRAP_OR_FAIL(auto refs, ReferenceVisitor::GetReferencedFieldIds(bound_max));
EXPECT_THAT(refs, ::testing::UnorderedElementsAre(3));
}

TEST_F(ReferenceVisitorTest, UnboundPredicate) {
auto unbound_pred = Expressions::Equal("name", Literal::String("Alice"));
auto result = ReferenceVisitor::GetReferencedFieldIds(unbound_pred);
Expand Down
Loading