@@ -1422,4 +1422,176 @@ nlohmann::json ToJson(const TableRequirement& requirement) {
14221422 return json;
14231423}
14241424
1425+ Result<std::unique_ptr<TableUpdate>> TableUpdateFromJson (
1426+ const nlohmann::json& json, const std::shared_ptr<Schema>& schema) {
1427+ ICEBERG_ASSIGN_OR_RAISE (auto action, GetJsonValue<std::string>(json, kAction ));
1428+
1429+ if (action == kActionAssignUUID ) {
1430+ ICEBERG_ASSIGN_OR_RAISE (auto uuid, GetJsonValue<std::string>(json, kUUID ));
1431+ return std::make_unique<table::AssignUUID>(std::move (uuid));
1432+ }
1433+ if (action == kActionUpgradeFormatVersion ) {
1434+ ICEBERG_ASSIGN_OR_RAISE (auto format_version,
1435+ GetJsonValue<int8_t >(json, kFormatVersion ));
1436+ return std::make_unique<table::UpgradeFormatVersion>(format_version);
1437+ }
1438+ if (action == kActionAddSchema ) {
1439+ ICEBERG_ASSIGN_OR_RAISE (auto schema_json,
1440+ GetJsonValue<nlohmann::json>(json, kSchema ));
1441+ ICEBERG_ASSIGN_OR_RAISE (auto parsed_schema, SchemaFromJson (schema_json));
1442+ ICEBERG_ASSIGN_OR_RAISE (auto last_column_id,
1443+ GetJsonValue<int32_t >(json, kLastColumnId ));
1444+ return std::make_unique<table::AddSchema>(std::move (parsed_schema), last_column_id);
1445+ }
1446+ if (action == kActionSetCurrentSchema ) {
1447+ ICEBERG_ASSIGN_OR_RAISE (auto schema_id, GetJsonValue<int32_t >(json, kSchemaId ));
1448+ return std::make_unique<table::SetCurrentSchema>(schema_id);
1449+ }
1450+ if (action == kActionAddPartitionSpec ) {
1451+ if (!schema) {
1452+ return NotSupported (
1453+ " Cannot parse AddPartitionSpec without schema context. Use the overload "
1454+ " that takes a schema parameter." );
1455+ }
1456+ ICEBERG_ASSIGN_OR_RAISE (auto spec_json, GetJsonValue<nlohmann::json>(json, kSpec ));
1457+ ICEBERG_ASSIGN_OR_RAISE (auto spec_id_opt,
1458+ GetJsonValueOptional<int32_t >(spec_json, kSpecId ));
1459+ ICEBERG_ASSIGN_OR_RAISE (
1460+ auto spec,
1461+ PartitionSpecFromJson (schema, spec_json,
1462+ spec_id_opt.value_or (PartitionSpec::kInitialSpecId )));
1463+ return std::make_unique<table::AddPartitionSpec>(std::move (spec));
1464+ }
1465+ if (action == kActionSetDefaultPartitionSpec ) {
1466+ ICEBERG_ASSIGN_OR_RAISE (auto spec_id, GetJsonValue<int32_t >(json, kSpecId ));
1467+ return std::make_unique<table::SetDefaultPartitionSpec>(spec_id);
1468+ }
1469+ if (action == kActionRemovePartitionSpecs ) {
1470+ ICEBERG_ASSIGN_OR_RAISE (auto spec_ids,
1471+ GetJsonValue<std::vector<int32_t >>(json, kSpecIds ));
1472+ return std::make_unique<table::RemovePartitionSpecs>(std::move (spec_ids));
1473+ }
1474+ if (action == kActionRemoveSchemas ) {
1475+ ICEBERG_ASSIGN_OR_RAISE (auto schema_ids_vec,
1476+ GetJsonValue<std::vector<int32_t >>(json, kSchemaIds ));
1477+ std::unordered_set<int32_t > schema_ids (schema_ids_vec.begin (), schema_ids_vec.end ());
1478+ return std::make_unique<table::RemoveSchemas>(std::move (schema_ids));
1479+ }
1480+ if (action == kActionAddSortOrder ) {
1481+ if (!schema) {
1482+ return NotSupported (
1483+ " Cannot parse AddSortOrder without schema context. Use the overload "
1484+ " that takes a schema parameter." );
1485+ }
1486+ ICEBERG_ASSIGN_OR_RAISE (auto sort_order_json,
1487+ GetJsonValue<nlohmann::json>(json, kSortOrder ));
1488+ ICEBERG_ASSIGN_OR_RAISE (auto sort_order, SortOrderFromJson (sort_order_json, schema));
1489+ return std::make_unique<table::AddSortOrder>(std::move (sort_order));
1490+ }
1491+ if (action == kActionSetDefaultSortOrder ) {
1492+ ICEBERG_ASSIGN_OR_RAISE (auto sort_order_id,
1493+ GetJsonValue<int32_t >(json, kSortOrderId ));
1494+ return std::make_unique<table::SetDefaultSortOrder>(sort_order_id);
1495+ }
1496+ if (action == kActionAddSnapshot ) {
1497+ ICEBERG_ASSIGN_OR_RAISE (auto snapshot_json,
1498+ GetJsonValue<nlohmann::json>(json, kSnapshot ));
1499+ ICEBERG_ASSIGN_OR_RAISE (auto snapshot, SnapshotFromJson (snapshot_json));
1500+ return std::make_unique<table::AddSnapshot>(std::move (snapshot));
1501+ }
1502+ if (action == kActionRemoveSnapshots ) {
1503+ ICEBERG_ASSIGN_OR_RAISE (auto snapshot_ids,
1504+ GetJsonValue<std::vector<int64_t >>(json, kSnapshotIds ));
1505+ return std::make_unique<table::RemoveSnapshots>(std::move (snapshot_ids));
1506+ }
1507+ if (action == kActionRemoveSnapshotRef ) {
1508+ ICEBERG_ASSIGN_OR_RAISE (auto ref_name, GetJsonValue<std::string>(json, kRefName ));
1509+ return std::make_unique<table::RemoveSnapshotRef>(std::move (ref_name));
1510+ }
1511+ if (action == kActionSetSnapshotRef ) {
1512+ ICEBERG_ASSIGN_OR_RAISE (auto ref_name, GetJsonValue<std::string>(json, kRefName ));
1513+ ICEBERG_ASSIGN_OR_RAISE (auto snapshot_id, GetJsonValue<int64_t >(json, kSnapshotId ));
1514+ ICEBERG_ASSIGN_OR_RAISE (
1515+ auto type,
1516+ GetJsonValue<std::string>(json, kType ).and_then (SnapshotRefTypeFromString));
1517+ ICEBERG_ASSIGN_OR_RAISE (auto min_snapshots,
1518+ GetJsonValueOptional<int32_t >(json, kMinSnapshotsToKeep ));
1519+ ICEBERG_ASSIGN_OR_RAISE (auto max_snapshot_age,
1520+ GetJsonValueOptional<int64_t >(json, kMaxSnapshotAgeMs ));
1521+ ICEBERG_ASSIGN_OR_RAISE (auto max_ref_age,
1522+ GetJsonValueOptional<int64_t >(json, kMaxRefAgeMs ));
1523+ return std::make_unique<table::SetSnapshotRef>(std::move (ref_name), snapshot_id, type,
1524+ min_snapshots, max_snapshot_age,
1525+ max_ref_age);
1526+ }
1527+ if (action == kActionSetProperties ) {
1528+ using StringMap = std::unordered_map<std::string, std::string>;
1529+ ICEBERG_ASSIGN_OR_RAISE (auto updates, GetJsonValue<StringMap>(json, kUpdates ));
1530+ return std::make_unique<table::SetProperties>(std::move (updates));
1531+ }
1532+ if (action == kActionRemoveProperties ) {
1533+ ICEBERG_ASSIGN_OR_RAISE (auto removals_vec,
1534+ GetJsonValue<std::vector<std::string>>(json, kRemovals ));
1535+ std::unordered_set<std::string> removals (removals_vec.begin (), removals_vec.end ());
1536+ return std::make_unique<table::RemoveProperties>(std::move (removals));
1537+ }
1538+ if (action == kActionSetLocation ) {
1539+ ICEBERG_ASSIGN_OR_RAISE (auto location, GetJsonValue<std::string>(json, kLocation ));
1540+ return std::make_unique<table::SetLocation>(std::move (location));
1541+ }
1542+
1543+ return JsonParseError (" Unknown table update action: {}" , action);
1544+ }
1545+
1546+ Result<std::unique_ptr<TableUpdate>> TableUpdateFromJson (const nlohmann::json& json) {
1547+ return TableUpdateFromJson (json, nullptr );
1548+ }
1549+
1550+ Result<std::unique_ptr<TableRequirement>> TableRequirementFromJson (
1551+ const nlohmann::json& json) {
1552+ ICEBERG_ASSIGN_OR_RAISE (auto type, GetJsonValue<std::string>(json, kType ));
1553+
1554+ if (type == kRequirementAssertDoesNotExist ) {
1555+ return std::make_unique<table::AssertDoesNotExist>();
1556+ }
1557+ if (type == kRequirementAssertUUID ) {
1558+ ICEBERG_ASSIGN_OR_RAISE (auto uuid, GetJsonValue<std::string>(json, kUUID ));
1559+ return std::make_unique<table::AssertUUID>(std::move (uuid));
1560+ }
1561+ if (type == kRequirementAssertRefSnapshotID ) {
1562+ ICEBERG_ASSIGN_OR_RAISE (auto ref_name, GetJsonValue<std::string>(json, kRefName ));
1563+ ICEBERG_ASSIGN_OR_RAISE (auto snapshot_id_opt,
1564+ GetJsonValueOptional<int64_t >(json, kSnapshotId ));
1565+ return std::make_unique<table::AssertRefSnapshotID>(std::move (ref_name),
1566+ snapshot_id_opt);
1567+ }
1568+ if (type == kRequirementAssertLastAssignedFieldId ) {
1569+ ICEBERG_ASSIGN_OR_RAISE (auto last_assigned_field_id,
1570+ GetJsonValue<int32_t >(json, kLastAssignedFieldId ));
1571+ return std::make_unique<table::AssertLastAssignedFieldId>(last_assigned_field_id);
1572+ }
1573+ if (type == kRequirementAssertCurrentSchemaID ) {
1574+ ICEBERG_ASSIGN_OR_RAISE (auto schema_id,
1575+ GetJsonValue<int32_t >(json, kCurrentSchemaId ));
1576+ return std::make_unique<table::AssertCurrentSchemaID>(schema_id);
1577+ }
1578+ if (type == kRequirementAssertLastAssignedPartitionId ) {
1579+ ICEBERG_ASSIGN_OR_RAISE (auto last_assigned_partition_id,
1580+ GetJsonValue<int32_t >(json, kLastAssignedPartitionId ));
1581+ return std::make_unique<table::AssertLastAssignedPartitionId>(
1582+ last_assigned_partition_id);
1583+ }
1584+ if (type == kRequirementAssertDefaultSpecID ) {
1585+ ICEBERG_ASSIGN_OR_RAISE (auto spec_id, GetJsonValue<int32_t >(json, kDefaultSpecId ));
1586+ return std::make_unique<table::AssertDefaultSpecID>(spec_id);
1587+ }
1588+ if (type == kRequirementAssertDefaultSortOrderID ) {
1589+ ICEBERG_ASSIGN_OR_RAISE (auto sort_order_id,
1590+ GetJsonValue<int32_t >(json, kDefaultSortOrderId ));
1591+ return std::make_unique<table::AssertDefaultSortOrderID>(sort_order_id);
1592+ }
1593+
1594+ return JsonParseError (" Unknown table requirement type: {}" , type);
1595+ }
1596+
14251597} // namespace iceberg
0 commit comments