Skip to content

Commit a2d1a9e

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

File tree

3 files changed

+235
-0
lines changed

3 files changed

+235
-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: 120 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,120 @@
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 Interface for a data type for a field.
36+
class ICEBERG_EXPORT Type {
37+
public:
38+
virtual ~Type() = default;
39+
40+
/// \brief Get the type ID.
41+
[[nodiscard]] virtual TypeId type_id() const = 0;
42+
43+
/// \brief Get a user-readable string representation of the type.
44+
[[nodiscard]] virtual std::string ToString() const = 0;
45+
46+
/// \brief Compare two types for equality.
47+
[[nodiscard]] virtual bool Equals(const Type& other) const = 0;
48+
49+
friend bool operator==(const Type& lhs, const Type& rhs) { return lhs.Equals(rhs); }
50+
51+
friend bool operator!=(const Type& lhs, const Type& rhs) { return !(lhs == rhs); }
52+
};
53+
54+
/// \brief A type combined with a name.
55+
class ICEBERG_EXPORT Field {
56+
public:
57+
Field(int32_t field_id, std::string name, std::shared_ptr<Type> type, bool optional);
58+
59+
static Field MakeOptional(int32_t field_id, std::string name,
60+
std::shared_ptr<Type> type);
61+
static Field MakeRequired(int32_t field_id, std::string name,
62+
std::shared_ptr<Type> type);
63+
64+
/// \brief Get the field ID.
65+
[[nodiscard]] int32_t field_id() const;
66+
67+
/// \brief Get the field name.
68+
[[nodiscard]] std::string_view name() const;
69+
70+
/// \brief Get the field type.
71+
[[nodiscard]] const std::shared_ptr<Type>& type() const;
72+
73+
/// \brief Get whether the field is optional.
74+
[[nodiscard]] bool optional() const;
75+
76+
/// \brief Get a user-readable string representation of the field.
77+
[[nodiscard]] std::string ToString() const;
78+
79+
/// \brief Compare two fields for equality.
80+
[[nodiscard]] bool Equals(const Field& other) const;
81+
82+
friend bool operator==(const Field& lhs, const Field& rhs) { return lhs.Equals(rhs); }
83+
84+
friend bool operator!=(const Field& lhs, const Field& rhs) { return !(lhs == rhs); }
85+
};
86+
87+
/// \brief A data type representing a boolean.
88+
class ICEBERG_EXPORT BooleanType : public Type {
89+
public:
90+
BooleanType() = default;
91+
~BooleanType() = default;
92+
93+
TypeId type_id() const override;
94+
95+
std::string ToString() const override;
96+
97+
bool Equals(const Type& other) const override;
98+
};
99+
100+
/// \brief A data type representing a struct with nested fields.
101+
class ICEBERG_EXPORT StructType : public Type {
102+
public:
103+
explicit StructType(std::vector<Field> fields);
104+
105+
StructType() = default;
106+
~StructType() = default;
107+
108+
[[nodiscard]] std::span<Field> fields() const;
109+
110+
const Field& field(int i) const;
111+
const Field& field(std::string_view name) const;
112+
113+
TypeId type_id() const override;
114+
115+
std::string ToString() const override;
116+
117+
bool Equals(const Type& other) const override;
118+
};
119+
120+
} // namespace iceberg

src/iceberg/type_fwd.h

Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
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 Schema;
52+
class StructType;
53+
class Type;
54+
55+
} // namespace iceberg

0 commit comments

Comments
 (0)