Skip to content

Commit 9df1312

Browse files
committed
WIP: Add headers for type/field/schema
1 parent f17fd2f commit 9df1312

File tree

3 files changed

+266
-0
lines changed

3 files changed

+266
-0
lines changed

src/iceberg/schema.h

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
22+
/// \file iceberg/schema.h
23+
/// Schemas for Iceberg tables.
24+
25+
#include <cstdint>
26+
#include <string>
27+
#include <vector>
28+
29+
#include "iceberg/iceberg_export.h"
30+
#include "iceberg/type.h"
31+
32+
namespace iceberg {
33+
34+
/// \brief A schema for a Table.
35+
///
36+
/// A schema is a list of typed columns, along with a unique integer ID. A
37+
/// Table may have different schemas over its lifetime due to schema
38+
/// evolution.
39+
class ICEBERG_EXPORT Schema : public StructType {
40+
public:
41+
Schema(int32_t schema_id, std::vector<Field> fields);
42+
43+
/// \brief Get the schema ID.
44+
///
45+
/// Schemas are identified by a unique ID for the purposes of schema
46+
/// evolution.
47+
[[nodiscard]] int32_t schema_id() const;
48+
49+
/// \brief Get a user-readable string representation of the schema.
50+
[[nodiscard]] std::string ToString() const;
51+
52+
/// \brief Compare two schemas for equality.
53+
[[nodiscard]] bool Equals(const Schema& other) const;
54+
55+
friend bool operator==(const Schema& lhs, const Schema& rhs) { return lhs.Equals(rhs); }
56+
57+
friend bool operator!=(const Schema& lhs, const Schema& rhs) { return !(lhs == rhs); }
58+
};
59+
60+
} // namespace iceberg

src/iceberg/type.h

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,149 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
22+
/// \file iceberg/type.h
23+
/// Data types for Iceberg.
24+
25+
#include <memory>
26+
#include <span>
27+
#include <string>
28+
#include <vector>
29+
30+
#include "iceberg/iceberg_export.h"
31+
#include "iceberg/type_fwd.h"
32+
33+
namespace iceberg {
34+
35+
/// \brief A type combined with a name.
36+
class ICEBERG_EXPORT Field {
37+
public:
38+
/// \brief Construct a field.
39+
/// \param[in] field_id The field ID.
40+
/// \param[in] name The field name.
41+
/// \param[in] type The field type.
42+
/// \param[in] optional Whether values of this field are required or nullable.
43+
Field(int32_t field_id, std::string name, std::shared_ptr<Type> type, bool optional);
44+
45+
/// \brief Construct an optional (nullable) field.
46+
static Field MakeOptional(int32_t field_id, std::string name,
47+
std::shared_ptr<Type> type);
48+
/// \brief Construct a required (non-null) field.
49+
static Field MakeRequired(int32_t field_id, std::string name,
50+
std::shared_ptr<Type> type);
51+
52+
/// \brief Get the field ID.
53+
[[nodiscard]] int32_t field_id() const;
54+
55+
/// \brief Get the field name.
56+
[[nodiscard]] std::string_view name() const;
57+
58+
/// \brief Get the field type.
59+
[[nodiscard]] const std::shared_ptr<Type>& type() const;
60+
61+
/// \brief Get whether the field is optional.
62+
[[nodiscard]] bool optional() const;
63+
64+
/// \brief Get a user-readable string representation of the field.
65+
[[nodiscard]] std::string ToString() const;
66+
67+
/// \brief Compare two fields for equality.
68+
[[nodiscard]] bool Equals(const Field& other) const;
69+
70+
friend bool operator==(const Field& lhs, const Field& rhs) { return lhs.Equals(rhs); }
71+
72+
friend bool operator!=(const Field& lhs, const Field& rhs) { return !(lhs == rhs); }
73+
};
74+
75+
/// \brief Interface for a data type for a field.
76+
class ICEBERG_EXPORT Type {
77+
public:
78+
virtual ~Type() = default;
79+
80+
/// \brief Get the type ID.
81+
[[nodiscard]] virtual TypeId type_id() const = 0;
82+
83+
/// \brief Get a user-readable string representation of the type.
84+
[[nodiscard]] virtual std::string ToString() const = 0;
85+
86+
/// \brief Compare two types for equality.
87+
[[nodiscard]] virtual bool Equals(const Type& other) const = 0;
88+
89+
/// \brief Is this a primitive type (may not have child fields)?
90+
[[nodiscard]] virtual bool is_primitive() const = 0;
91+
92+
/// \brief Is this a nested type (may have child fields)?
93+
[[nodiscard]] virtual bool is_nested() const = 0;
94+
95+
friend bool operator==(const Type& lhs, const Type& rhs) { return lhs.Equals(rhs); }
96+
97+
friend bool operator!=(const Type& lhs, const Type& rhs) { return !(lhs == rhs); }
98+
};
99+
100+
/// \brief A data type that may not have child fields.
101+
class ICEBERG_EXPORT PrimitiveType : public Type {
102+
public:
103+
bool is_primitive() const override { return true; }
104+
bool is_nested() const override { return false; }
105+
};
106+
107+
/// \brief A data type that may have child fields.
108+
class ICEBERG_EXPORT NestedType : public Type {
109+
public:
110+
bool is_primitive() const override { return false; }
111+
bool is_nested() const override { return true; }
112+
};
113+
114+
/// \brief A data type representing a boolean.
115+
class ICEBERG_EXPORT BooleanType : public PrimitiveType {
116+
public:
117+
BooleanType() = default;
118+
~BooleanType() = default;
119+
120+
TypeId type_id() const override;
121+
122+
std::string ToString() const override;
123+
124+
bool Equals(const Type& other) const override;
125+
};
126+
127+
/// \brief A data type representing a struct with nested fields.
128+
class ICEBERG_EXPORT StructType : public NestedType {
129+
public:
130+
explicit StructType(std::vector<Field> fields);
131+
132+
StructType() = default;
133+
~StructType() = default;
134+
135+
/// \brief Get a view of the child fields.
136+
[[nodiscard]] std::span<Field> fields() const;
137+
/// \brief Get a field by index.
138+
const Field& GetFieldByIndex(int i) const;
139+
/// \brief Get a field by name.
140+
const Field& GetFieldByName(std::string_view name) const;
141+
142+
TypeId type_id() const override;
143+
144+
std::string ToString() const override;
145+
146+
bool Equals(const Type& other) const override;
147+
};
148+
149+
} // namespace iceberg

src/iceberg/type_fwd.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
/*
2+
* Licensed to the Apache Software Foundation (ASF) under one
3+
* or more contributor license agreements. See the NOTICE file
4+
* distributed with this work for additional information
5+
* regarding copyright ownership. The ASF licenses this file
6+
* to you under the Apache License, Version 2.0 (the
7+
* "License"); you may not use this file except in compliance
8+
* with the License. You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
#pragma once
21+
22+
/// \file iceberg/type_fwd.h
23+
/// Forward declarations and enum definitions.
24+
25+
namespace iceberg {
26+
27+
/// \brief A data type.
28+
///
29+
/// This is not a complete data type by itself because some types are nested
30+
/// and/or parameterized.
31+
enum class TypeId {
32+
kBoolean,
33+
kInt32,
34+
kInt64,
35+
kFloat32,
36+
kFloat64,
37+
kDecimal,
38+
kDate,
39+
kTime,
40+
kTimestamp,
41+
kTimestampTz,
42+
kString,
43+
kFixed,
44+
kStruct,
45+
kList,
46+
kMap,
47+
};
48+
49+
class BooleanType;
50+
class Field;
51+
class NestedType;
52+
class PrimitiveType;
53+
class Schema;
54+
class StructType;
55+
class Type;
56+
57+
} // namespace iceberg

0 commit comments

Comments
 (0)