Skip to content

feat(view): add metadata JSON serialization - #1196

Open
kamcheungting-db wants to merge 4 commits into
apache:mainfrom
kamcheungting-db:feat/view-metadata-json
Open

kamcheungting-db wants to merge 4 commits into
apache:mainfrom
kamcheungting-db:feat/view-metadata-json

Conversation

@kamcheungting-db

@kamcheungting-db kamcheungting-db commented Nov 30, 2026 •

Copy link
Copy Markdown
Contributor

Adds Iceberg view format v1 JSON parsing and serialization, building on #1069.

  • Preserves unknown representation payloads and properties through round trips and property edits; adds typed ViewProperties.
  • Separates validation: reads check structure; writes also validate every schema field and default; ViewMetadata::Validate() additionally checks UUID, location, references, and SQL dialects.
  • Uses one SQL discriminator policy in metadata validation and serde: ASCII case variants of sql are reserved; other tags, including ſql, remain opaque.
  • Rejects overflowing integers and malformed struct fields; accepts empty default namespaces and absent/null default catalogs.

For example, metadata with a missing historical reference can still round trip after a property edit. An integer field with a string default ("oops") now fails validation and serialization, preventing JSON that cannot reload. NaN defaults are rejected before they can become null.

Validation: 85 view tests, 175 JSON serde tests, 583 schema tests, and the C++20 header check pass under Clang 18 with ASan/UBSan (leak detection and alloc/dealloc mismatch checks disabled). Six new regression tests fail before the fixes and pass afterward. Pre-commit passes for the changed files.

- Share one IsSqlRepresentationType policy between metadata validation and
  JSON serde. "sql" in any case is reserved everywhere, while "ſql"
  (U+017F LATIN SMALL LETTER LONG S) does not case-fold to "sql" and is
  now an opaque type that parses, validates, and round trips unchanged.
- Add ValidateViewMetadataForWrite, used by ViewMetadata::Validate and
  by serialization: the read checks plus SchemaField::Validate on every
  schema field, including nested struct fields. A NaN default or a default
  with the wrong type now fails fast instead of being serialized as null
  or producing metadata that cannot be reloaded. Parsing stays lenient so
  existing metadata still round trips.
bool number_integer(number_integer_t) override { return true; }
bool number_unsigned(number_unsigned_t) override { return true; }
bool number_float(number_float_t, const string_t& token) override {
return token.find_first_of(".eE") != std::string::npos;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A valid opaque payload number like 9007199254740993.0 passes this check after the DOM has rounded it. Can we preserve its value, or reject precision loss before a property-only update writes back a different number?

if (IsSqlRepresentationType(unknown.type)) {
return ValidationFailed("Unknown view representation must have a non-SQL type");
}
if (unknown.extra_fields.contains("type")) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A nested non-finite number in the public extra_fields passes here and is written as null. Please validate that all opaque values can be serialized without changing them.

/// The representation type identifier from the metadata, such as "cypher".
std::string type;
/// Representation-specific JSON fields, excluding the type discriminator.
nlohmann::json::object_t extra_fields;

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The bundled JSON comparator treats UINT64_MAX and -1 as equal through its signed cast. Please compare opaque numeric values without narrowing and add a regression for these two payloads. Comparator

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

btw, we shouldn't export nlohmann::json::object_t in a public header.

GetJsonInteger<int32_t>(json, kCurrentVersionId));
ICEBERG_ASSIGN_OR_RAISE(auto schemas, RequiredArray(json, kSchemas));
for (const auto& schema : schemas) {
ICEBERG_RETURN_UNEXPECTED(GetJsonInteger<int32_t>(schema, kSchemaId));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

With an omitted schema ID and a version referencing 0, Java and our shared parser both default to schema 0. Please remove this precheck and keep the duplicate check on the resolved IDs. Java parser

IdToFieldVisitor id_visitor(fields);
ICEBERG_RETURN_UNEXPECTED(VisitTypeInline(schema, &id_visitor));
for (const auto& [id, field_ref] : fields) {
if (auto status = field_ref.get().Validate(); !status.has_value()) {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

A time default of 86,400,000,000 microseconds passes this check and is written as "24:00". Our reader rejects hour 24. Please validate the time range on write and test the last valid microsecond of the day.

for (const auto* reserved : {"sql", "SQL", "SqL"}) {
SCOPED_TRACE(reserved);
ICEBERG_UNWRAP_OR_FAIL(auto metadata, ViewMetadataFromJson(ValidMetadataJson()));
metadata->versions[0]->representations.push_back(

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

The linter flags this call and the one at line 467. Please use emplace_back in both loops.

#include <variant>
#include <vector>

#include <nlohmann/json.hpp>

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

As a convention, we shouldn't include nlohmann header here.

This branch has not been deployed

No deployments
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.

2 participants