Skip to content

Commit 37a8c30

Browse files
committed
use result for schema/spec/order
1 parent 156c90e commit 37a8c30

File tree

13 files changed

+80
-85
lines changed

13 files changed

+80
-85
lines changed

src/iceberg/constants.h

Lines changed: 0 additions & 38 deletions
This file was deleted.

src/iceberg/json_internal.cc

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,9 @@
2727
#include <type_traits>
2828
#include <unordered_set>
2929

30+
#include <iceberg/table.h>
3031
#include <nlohmann/json.hpp>
3132

32-
#include "iceberg/constants.h"
3333
#include "iceberg/partition_field.h"
3434
#include "iceberg/partition_spec.h"
3535
#include "iceberg/result.h"
@@ -499,7 +499,7 @@ nlohmann::json ToJson(const Snapshot& snapshot) {
499499
nlohmann::json json;
500500
json[kSnapshotId] = snapshot.snapshot_id;
501501
SetOptionalField(json, kParentSnapshotId, snapshot.parent_snapshot_id);
502-
if (snapshot.sequence_number > kInitialSequenceNumber) {
502+
if (snapshot.sequence_number > TableMetadata::kInitialSequenceNumber) {
503503
json[kSequenceNumber] = snapshot.sequence_number;
504504
}
505505
json[kTimestampMs] = snapshot.timestamp_ms;
@@ -666,8 +666,8 @@ Result<std::unique_ptr<PartitionField>> PartitionFieldFromJson(
666666
int32_t field_id;
667667
if (allow_field_id_missing) {
668668
// Partition field id in v1 is not tracked, so we use -1 to indicate that.
669-
ICEBERG_ASSIGN_OR_RAISE(
670-
field_id, GetJsonValueOrDefault<int32_t>(json, kFieldId, kInvalidFieldId));
669+
ICEBERG_ASSIGN_OR_RAISE(field_id, GetJsonValueOrDefault<int32_t>(
670+
json, kFieldId, SchemaField::kInvalidFieldId));
671671
} else {
672672
ICEBERG_ASSIGN_OR_RAISE(field_id, GetJsonValue<int32_t>(json, kFieldId));
673673
}
@@ -765,8 +765,9 @@ Result<std::unique_ptr<Snapshot>> SnapshotFromJson(const nlohmann::json& json) {
765765
ICEBERG_ASSIGN_OR_RAISE(auto schema_id, GetJsonValueOptional<int32_t>(json, kSchemaId));
766766

767767
return std::make_unique<Snapshot>(
768-
snapshot_id, parent_snapshot_id, sequence_number.value_or(kInitialSequenceNumber),
769-
timestamp_ms, manifest_list, std::move(summary), schema_id);
768+
snapshot_id, parent_snapshot_id,
769+
sequence_number.value_or(TableMetadata::kInitialSequenceNumber), timestamp_ms,
770+
manifest_list, std::move(summary), schema_id);
770771
}
771772

772773
nlohmann::json ToJson(const BlobMetadata& blob_metadata) {
@@ -1054,16 +1055,16 @@ Status ParsePartitionSpecs(const nlohmann::json& json, int8_t format_version,
10541055
for (const auto& entry_json : partition_spec_json) {
10551056
ICEBERG_ASSIGN_OR_RAISE(auto field, PartitionFieldFromJson(entry_json));
10561057
int32_t field_id = field->field_id();
1057-
if (field_id == kInvalidFieldId) {
1058+
if (field_id == SchemaField::kInvalidFieldId) {
10581059
// If the field ID is not set, we need to assign a new one
10591060
field_id = next_partition_field_id++;
10601061
}
10611062
fields.emplace_back(field->source_id(), field_id, std::string(field->name()),
10621063
std::move(field->transform()));
10631064
}
10641065

1065-
auto spec = std::make_unique<PartitionSpec>(current_schema, kInitialSpecId,
1066-
std::move(fields));
1066+
auto spec = std::make_unique<PartitionSpec>(
1067+
current_schema, PartitionSpec::kInitialSpecId, std::move(fields));
10671068
default_spec_id = spec->spec_id();
10681069
partition_specs.push_back(std::move(spec));
10691070
}
@@ -1112,7 +1113,7 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataFromJson(const nlohmann::jso
11121113
ICEBERG_ASSIGN_OR_RAISE(table_metadata->format_version,
11131114
GetJsonValue<int8_t>(json, kFormatVersion));
11141115
if (table_metadata->format_version < 1 ||
1115-
table_metadata->format_version > kSupportedTableFormatVersion) {
1116+
table_metadata->format_version > TableMetadata::kSupportedTableFormatVersion) {
11161117
return JsonParseError("Cannot read unsupported version: {}",
11171118
table_metadata->format_version);
11181119
}
@@ -1126,7 +1127,7 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataFromJson(const nlohmann::jso
11261127
ICEBERG_ASSIGN_OR_RAISE(table_metadata->last_sequence_number,
11271128
GetJsonValue<int64_t>(json, kLastSequenceNumber));
11281129
} else {
1129-
table_metadata->last_sequence_number = kInitialSequenceNumber;
1130+
table_metadata->last_sequence_number = TableMetadata::kInitialSequenceNumber;
11301131
}
11311132
ICEBERG_ASSIGN_OR_RAISE(table_metadata->last_column_id,
11321133
GetJsonValue<int32_t>(json, kLastColumnId));
@@ -1169,15 +1170,15 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataFromJson(const nlohmann::jso
11691170
}
11701171

11711172
// This field is optional, but internally we set this to -1 when not set
1172-
ICEBERG_ASSIGN_OR_RAISE(
1173-
table_metadata->current_snapshot_id,
1174-
GetJsonValueOrDefault<int64_t>(json, kCurrentSnapshotId, kInvalidSnapshotId));
1173+
ICEBERG_ASSIGN_OR_RAISE(table_metadata->current_snapshot_id,
1174+
GetJsonValueOrDefault<int64_t>(json, kCurrentSnapshotId,
1175+
Snapshot::kInvalidSnapshotId));
11751176

11761177
if (table_metadata->format_version >= 3) {
11771178
ICEBERG_ASSIGN_OR_RAISE(table_metadata->next_row_id,
11781179
GetJsonValue<int64_t>(json, kNextRowId));
11791180
} else {
1180-
table_metadata->next_row_id = kInitialRowId;
1181+
table_metadata->next_row_id = TableMetadata::kInitialRowId;
11811182
}
11821183

11831184
ICEBERG_ASSIGN_OR_RAISE(auto last_updated_ms,
@@ -1189,7 +1190,7 @@ Result<std::unique_ptr<TableMetadata>> TableMetadataFromJson(const nlohmann::jso
11891190
ICEBERG_ASSIGN_OR_RAISE(
11901191
table_metadata->refs,
11911192
FromJsonMap<std::shared_ptr<SnapshotRef>>(json, kRefs, SnapshotRefFromJson));
1192-
} else if (table_metadata->current_snapshot_id != kInvalidSnapshotId) {
1193+
} else if (table_metadata->current_snapshot_id != Snapshot::kInvalidSnapshotId) {
11931194
table_metadata->refs["main"] = std::make_unique<SnapshotRef>(SnapshotRef{
11941195
.snapshot_id = table_metadata->current_snapshot_id,
11951196
.retention = SnapshotRef::Branch{},

src/iceberg/partition_spec.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,6 @@
2323
#include <format>
2424
#include <ranges>
2525

26-
#include "iceberg/constants.h"
2726
#include "iceberg/schema.h"
2827
#include "iceberg/util/formatter.h" // IWYU pragma: keep
2928

src/iceberg/partition_spec.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,9 +41,10 @@ namespace iceberg {
4141
/// evolution.
4242
class ICEBERG_EXPORT PartitionSpec : public util::Formattable {
4343
public:
44+
static constexpr int32_t kInitialSpecId = 0;
4445
/// \brief The start ID for partition field. It is only used to generate
4546
/// partition field id for v1 metadata where it is tracked.
46-
constexpr static int32_t kLegacyPartitionDataIdStart = 1000;
47+
static constexpr int32_t kLegacyPartitionDataIdStart = 1000;
4748

4849
/// \brief Create a new partition spec.
4950
///

src/iceberg/result.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ enum class ErrorKind {
4242
kNotSupported,
4343
kInvalidExpression,
4444
kJsonParseError,
45+
kNotFound,
4546
};
4647

4748
/// \brief Error with a kind and a message.

src/iceberg/schema.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,8 @@ namespace iceberg {
4141
/// evolution.
4242
class ICEBERG_EXPORT Schema : public StructType {
4343
public:
44+
static constexpr int32_t kInitialSchemaId = 0;
45+
4446
Schema(std::vector<SchemaField> fields, std::optional<int32_t> schema_id);
4547

4648
/// \brief Get the schema ID.

src/iceberg/schema_field.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,6 +37,8 @@ namespace iceberg {
3737
/// \brief A type combined with a name.
3838
class ICEBERG_EXPORT SchemaField : public iceberg::util::Formattable {
3939
public:
40+
static constexpr int32_t kInvalidFieldId = -1;
41+
4042
/// \brief Construct a field.
4143
/// \param[in] field_id The field ID.
4244
/// \param[in] name The field name.

src/iceberg/snapshot.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,8 @@ struct ICEBERG_EXPORT DataOperation {
233233
///
234234
/// Snapshots are created by table operations.
235235
struct ICEBERG_EXPORT Snapshot {
236+
static constexpr int64_t kInvalidSnapshotId = -1;
237+
236238
/// A unqiue long ID.
237239
int64_t snapshot_id;
238240
/// The snapshot ID of the snapshot's parent. Omitted for any snapshot with no parent.

src/iceberg/sort_order.cc

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121

2222
#include <format>
2323

24-
#include "iceberg/constants.h"
2524
#include "iceberg/util/formatter.h" // IWYU pragma: keep
2625

2726
namespace iceberg {

src/iceberg/sort_order.h

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ namespace iceberg {
3636
/// applied to the data.
3737
class ICEBERG_EXPORT SortOrder : public util::Formattable {
3838
public:
39+
static constexpr int32_t kInitialSortOrderId = 1;
40+
3941
SortOrder(int32_t order_id, std::vector<SortField> fields);
4042

4143
/// \brief Get an unsorted sort order singleton.

0 commit comments

Comments
 (0)