-
Notifications
You must be signed in to change notification settings - Fork 736
datalake: compile-time schema descriptors for table_definition #30170
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
Merged
andrwng
merged 1 commit into
redpanda-data:dev
from
andrwng:compile-time-iceberg-structs
Apr 21, 2026
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,201 @@ | ||
| /* | ||
| * Copyright 2026 Redpanda Data, Inc. | ||
| * | ||
| * Licensed as a Redpanda Enterprise file under the Redpanda Community | ||
| * License (the "License"); you may not use this file except in compliance with | ||
| * the License. You may obtain a copy of the License at | ||
| * | ||
| * https://github.com/redpanda-data/redpanda/blob/master/licenses/rcl.md | ||
| */ | ||
| #pragma once | ||
|
|
||
| #include "iceberg/datatypes.h" | ||
| #include "iceberg/values.h" | ||
|
|
||
| #include <seastar/core/sstring.hh> | ||
|
|
||
| #include <algorithm> | ||
| #include <array> | ||
| #include <compare> | ||
| #include <concepts> | ||
| #include <cstddef> | ||
| #include <optional> | ||
| #include <string_view> | ||
| #include <type_traits> | ||
| #include <utility> | ||
|
|
||
| namespace datalake { | ||
|
|
||
| /// A compile-time string usable as a template parameter (C++20 NTTP). | ||
| template<size_t N> | ||
| struct ct_string { | ||
| char data[N]{}; | ||
| constexpr ct_string(const char (&s)[N]) { std::copy_n(s, N, data); } | ||
| constexpr operator std::string_view() const { return {data, N - 1}; } | ||
| constexpr auto operator<=>(const ct_string&) const = default; | ||
| }; | ||
|
|
||
| /// A field descriptor: compile-time name + iceberg type. | ||
| template<ct_string Name, typename T> | ||
| struct field_desc { | ||
| static constexpr std::string_view name{Name}; | ||
| using type = T; | ||
| }; | ||
|
|
||
| /// Forward declarations. | ||
| template<typename... Fields> | ||
| struct struct_desc; | ||
|
|
||
| template<typename ElementDesc> | ||
| struct list_desc; | ||
|
|
||
| /// A nested schema descriptor exposes build_impl(int&) to contribute its | ||
| /// subtree to the enclosing iceberg::struct_type. Leaf types (e.g. | ||
| /// iceberg::int_type) do not satisfy this and are default-constructed | ||
| /// as field types instead. | ||
| template<typename T> | ||
| concept nested_schema_desc = requires(int& next_id) { | ||
| { T::build_impl(next_id) }; | ||
| }; | ||
|
|
||
| /// Find the index of a field by name. Fails to compile if not found. | ||
| template<ct_string Name, typename... Fields> | ||
| consteval size_t index_of_fn() { | ||
| constexpr std::array names = {Fields::name...}; | ||
| for (size_t i = 0; i < names.size(); ++i) { | ||
| if (names[i] == std::string_view{Name}) { | ||
| return i; | ||
| } | ||
| } | ||
| // If we reach here, the name was not found. This is consteval, | ||
| // so the compiler will reject it with an error pointing here. | ||
| throw "field name not found in descriptor"; | ||
| } | ||
|
|
||
| /// Check that given names match field names in order. | ||
| /// Uses an array of field names passed directly to avoid partial | ||
| /// specialization. | ||
| template<ct_string... Names> | ||
| consteval bool | ||
| names_match_fn(std::array<std::string_view, sizeof...(Names)> field_names) { | ||
| constexpr std::array given = {std::string_view{Names}...}; | ||
| for (size_t i = 0; i < field_names.size(); ++i) { | ||
| if (field_names[i] != given[i]) { | ||
| return false; | ||
| } | ||
| } | ||
| return true; | ||
| } | ||
|
|
||
| /// A named value for use with struct_desc::build_value. The name is | ||
| /// checked at compile time to match the corresponding field descriptor. | ||
| template<ct_string Name> | ||
| struct val { | ||
| std::optional<iceberg::value> v; | ||
|
|
||
| template<typename U> | ||
| requires std::constructible_from<std::optional<iceberg::value>, U> | ||
| val(U&& u) // NOLINT(google-explicit-constructor) | ||
| : v(std::forward<U>(u)) {} | ||
| }; | ||
|
|
||
| template<typename... Fields> | ||
| struct struct_desc { | ||
| static constexpr size_t count = sizeof...(Fields); | ||
|
|
||
| /// Compile-time index of a field by name. | ||
| template<ct_string Name> | ||
| static constexpr size_t index_of = index_of_fn<Name, Fields...>(); | ||
|
|
||
| /// Total number of fields in the tree (used for ID assignment). | ||
| static int total_fields() { | ||
| int next_id = 1; | ||
| build_impl(next_id); | ||
| return next_id; | ||
| } | ||
|
|
||
| /// Build the runtime iceberg::struct_type. | ||
| static iceberg::struct_type build() { | ||
| int next_id = 1; | ||
| return build_impl(next_id); | ||
| } | ||
|
|
||
| static iceberg::struct_type build_impl(int& next_id) { | ||
| iceberg::struct_type st; | ||
| auto add = [&]<typename F>() { | ||
| int my_id = next_id++; | ||
| st.fields.push_back( | ||
| iceberg::nested_field::create( | ||
| my_id, | ||
| ss::sstring{F::name}, | ||
| iceberg::field_required::no, | ||
| build_iceberg_type<F>(next_id))); | ||
| }; | ||
| (add.template operator()<Fields>(), ...); | ||
| return st; | ||
| } | ||
|
|
||
| /// Build a struct_value with compile-time name and arity checking. | ||
| /// Each argument is a val<"field_name"> matching the descriptor. | ||
| /// | ||
| /// Wrong arity, wrong name, or wrong order = compile error. | ||
| template<ct_string... Names> | ||
| static std::unique_ptr<iceberg::struct_value> | ||
| build_value(val<Names>... args) { | ||
| static_assert(sizeof...(Names) == count, "wrong number of fields"); | ||
| static_assert( | ||
| names_match_fn<Names...>({Fields::name...}), | ||
| "field names don't match descriptor or are in wrong order"); | ||
| auto sv = std::make_unique<iceberg::struct_value>(); | ||
| sv->fields.reserve(count); | ||
| (sv->fields.emplace_back(std::move(args.v)), ...); | ||
| return sv; | ||
| } | ||
|
|
||
| private: | ||
| template<typename F> | ||
| static auto build_iceberg_type(int& next_id) { | ||
| if constexpr (nested_schema_desc<typename F::type>) { | ||
| return F::type::build_impl(next_id); | ||
| } else { | ||
| return typename F::type{}; | ||
| } | ||
| } | ||
| }; | ||
|
|
||
| template<typename ElementDesc> | ||
| struct list_desc; | ||
|
|
||
| template<typename... Fields> | ||
| struct list_desc<struct_desc<Fields...>> { | ||
| static iceberg::list_type build_impl(int& next_id) { | ||
| int element_id = next_id++; | ||
| return iceberg::list_type::create( | ||
| element_id, | ||
| iceberg::field_required::no, | ||
| struct_desc<Fields...>::build_impl(next_id)); | ||
| } | ||
| }; | ||
|
|
||
| template<typename Desc, ct_string Name> | ||
| iceberg::nested_field& type_field(iceberg::struct_type& st) { | ||
| return *st.fields[Desc::template index_of<Name>]; | ||
| } | ||
|
|
||
| template<typename Desc, ct_string Name> | ||
| const iceberg::nested_field& type_field(const iceberg::struct_type& st) { | ||
| return *st.fields[Desc::template index_of<Name>]; | ||
| } | ||
|
|
||
| template<typename Desc, ct_string Name> | ||
| std::optional<iceberg::value>& value_field(iceberg::struct_value& sv) { | ||
| return sv.fields[Desc::template index_of<Name>]; | ||
| } | ||
|
|
||
| template<typename Desc, ct_string Name> | ||
| const std::optional<iceberg::value>& | ||
| value_field(const iceberg::struct_value& sv) { | ||
| return sv.fields[Desc::template index_of<Name>]; | ||
| } | ||
|
|
||
| } // namespace datalake | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.