Skip to content

feat: add visit type support #94

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open

Conversation

zhjwpku
Copy link
Collaborator

@zhjwpku zhjwpku commented May 9, 2025

Add a TypeId field to each concrete type to enable macro-based type visitor support.

zhjwpku added 2 commits May 10, 2025 00:05
Signed-off-by: Junwang Zhao <[email protected]>
Signed-off-by: Junwang Zhao <[email protected]>
@zhjwpku zhjwpku mentioned this pull request May 10, 2025
Comment on lines +78 to +91
/// Unlike VisitTypeInline which calls `visitor.Visit`, here `visitor`
/// itself is called.
/// `visitor` must support a `const Type&` argument as a fallback,
/// in addition to concrete type classes.
///
/// The intent is for this to be called on a generic lambda
/// that may internally use `if constexpr` or similar constructs.
template <typename VISITOR, typename... ARGS>
inline auto VisitType(const Type& type, VISITOR&& visitor, ARGS&&... args)
-> decltype(std::forward<VISITOR>(visitor)(type, args...)) {
switch (type.type_id()) {
ICEBERG_GENERATE_FOR_ALL_TYPES(TYPE_VISIT_INLINE);
default:
internal::Unreachable("Type not implemented");
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The docstring says a fallback is called but the body aborts in the default case?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

TBH I have no idea what that sentence actually means, I don't think these words does any help to understand the template, so I'd rather delete that, or do you have any suggestions?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this was copied from Arrow? It may be worth noting that in the header just for reference.

Looking at it again, I think what it means is that because the visitor here does not inherit from a base class that has all cases defined, you must make sure to handle all cases. So I guess the implementation is OK. But the wording could be improved.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So this was copied from Arrow? It may be worth noting that in the header just for reference.

Yeah, it's adapted from arrow, I'll add the note.

Looking at it again, I think what it means is that because the visitor here does not inherit from a base class that has all cases defined, you must make sure to handle all cases. So I guess the implementation is OK. But the wording could be improved.

How about we keep this as is for now, and revisit it later, either improve the wording ourselves or sync with Arrow if they update their version.

The visit type infrastructure is adapted from apache arrow.

Add a reference note per @lidavidm's suggestion.

Signed-off-by: Junwang Zhao <[email protected]>
@zhjwpku zhjwpku requested a review from wgtmac May 10, 2025 10:22
Copy link
Member

@wgtmac wgtmac left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for adding this! I was thinking of the same thing.


namespace iceberg::internal {

[[noreturn]] ICEBERG_EXPORT void Unreachable(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why not directly use std::unreachable?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I did a quick search and found that std::unreachable was introduced in C++23 [1]. And it doesn't support passing a custom message, which I think might be useful for logging purposes.

[1] https://en.cppreference.com/w/cpp/utility/unreachable

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah my bad. Please ignore this comment.

* under the License.
*/

#pragma once
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: add the \file docstring and mention that it is adapted from Apache Arrow.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will add later today, thanks for the suggestion.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we take big pieces from other projects, then we also want to add that to the LICENSE:

For example in PyIceberg: https://github.com/apache/iceberg-python/blob/260ef54e3920d435ae3b2ccda090e66f9c1ac015/LICENSE#L206-L213

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants