Skip to content

Commit 59f60da

Browse files
committed
init templates
1 parent 33f2a8a commit 59f60da

File tree

3 files changed

+197
-0
lines changed

3 files changed

+197
-0
lines changed

src/iceberg/CMakeLists.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ set(ICEBERG_SOURCES
2121
arrow_c_data_internal.cc
2222
catalog/in_memory_catalog.cc
2323
demo.cc
24+
datum.cc
2425
expression/expression.cc
2526
file_reader.cc
2627
json_internal.cc

src/iceberg/datum.cc

Lines changed: 101 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,101 @@
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+
#include "iceberg/datum.h"
21+
#include "iceberg/exception.h"
22+
23+
#include <sstream>
24+
25+
namespace iceberg {
26+
27+
// Constructor
28+
PrimitiveLiteral::PrimitiveLiteral(PrimitiveLiteralValue value, std::shared_ptr<PrimitiveType> type)
29+
: value_(std::move(value)), type_(std::move(type)) {}
30+
31+
// Factory methods
32+
PrimitiveLiteral PrimitiveLiteral::Boolean(bool value) {
33+
return PrimitiveLiteral(value, std::make_shared<BooleanType>());
34+
}
35+
36+
PrimitiveLiteral PrimitiveLiteral::Integer(int32_t value) {
37+
return PrimitiveLiteral(value, std::make_shared<IntType>());
38+
}
39+
40+
PrimitiveLiteral PrimitiveLiteral::Long(int64_t value) {
41+
return PrimitiveLiteral(value, std::make_shared<LongType>());
42+
}
43+
44+
PrimitiveLiteral PrimitiveLiteral::Float(float value) {
45+
return PrimitiveLiteral(value, std::make_shared<FloatType>());
46+
}
47+
48+
PrimitiveLiteral PrimitiveLiteral::Double(double value) {
49+
return PrimitiveLiteral(value, std::make_shared<DoubleType>());
50+
}
51+
52+
PrimitiveLiteral PrimitiveLiteral::String(std::string value) {
53+
return PrimitiveLiteral(std::move(value), std::make_shared<StringType>());
54+
}
55+
56+
PrimitiveLiteral PrimitiveLiteral::Binary(std::vector<uint8_t> value) {
57+
return PrimitiveLiteral(std::move(value), std::make_shared<BinaryType>());
58+
}
59+
60+
Result<PrimitiveLiteral> PrimitiveLiteral::Deserialize(std::span<const uint8_t> data) {
61+
return NotImplemented("Deserialization of PrimitiveLiteral is not implemented yet");
62+
}
63+
64+
Result<std::vector<uint8_t>> PrimitiveLiteral::Serialize() const {
65+
return NotImplemented("Serialization of PrimitiveLiteral is not implemented yet");
66+
}
67+
68+
// Getters
69+
const PrimitiveLiteralValue& PrimitiveLiteral::value() const {
70+
return value_;
71+
}
72+
73+
const std::shared_ptr<PrimitiveType>& PrimitiveLiteral::type() const {
74+
return type_;
75+
}
76+
77+
// Cast method
78+
Result<PrimitiveLiteral> PrimitiveLiteral::CastTo(const std::shared_ptr<PrimitiveType>& target_type) const {
79+
if (*type_ == *target_type) {
80+
// If types are the same, return a copy of the current literal
81+
return PrimitiveLiteral(value_, target_type);
82+
}
83+
84+
return NotImplemented("Cast from {} to {} is not implemented",
85+
type_->ToString(), target_type->ToString());
86+
87+
}
88+
89+
// Three-way comparison operator
90+
std::partial_ordering PrimitiveLiteral::operator<=>(const PrimitiveLiteral& other) const {
91+
// If types are different, comparison is unordered
92+
if (type_->type_id() != other.type_->type_id()) {
93+
return std::partial_ordering::unordered;
94+
}
95+
if (value_ == other.value_) {
96+
return std::partial_ordering::equivalent;
97+
}
98+
throw IcebergError("Not implemented: comparison between different primitive types");
99+
}
100+
101+
} // namespace iceberg

src/iceberg/datum.h

Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
#include <compare>
23+
#include <memory>
24+
#include <string>
25+
#include <variant>
26+
#include <vector>
27+
28+
#include "iceberg/type.h"
29+
#include "iceberg/result.h"
30+
31+
namespace iceberg {
32+
33+
/// \brief Exception type for values that are below the minimum allowed value for a primitive type.
34+
///
35+
/// When casting a value to a narrow primitive type, if the value exceeds the maximum of dest type,
36+
/// it might be above the maximum allowed value for that type.
37+
struct BelowMin {
38+
bool operator==(const BelowMin&) const = default;
39+
std::strong_ordering operator<=>(const BelowMin&) const = default;
40+
};
41+
42+
/// \brief Exception type for values that are above the maximum allowed value for a primitive type.
43+
///
44+
/// When casting a value to a narrow primitive type, if the value exceeds the maximum of dest type,
45+
/// it might be above the maximum allowed value for that type.
46+
struct AboveMax {
47+
bool operator==(const AboveMax&) const = default;
48+
std::strong_ordering operator<=>(const AboveMax&) const = default;
49+
};
50+
51+
// TODO(mwish): Supports More types
52+
using PrimitiveLiteralValue =
53+
std::variant<bool, int32_t, int64_t, float, double, std::string, std::vector<uint8_t>, BelowMin, AboveMax>;
54+
55+
/// \brief PrimitiveLiteral is owned literal of a primitive type.
56+
class PrimitiveLiteral {
57+
public:
58+
explicit PrimitiveLiteral(PrimitiveLiteralValue value, std::shared_ptr<PrimitiveType> type);
59+
60+
// Factory methods for primitive types
61+
static PrimitiveLiteral Boolean(bool value);
62+
static PrimitiveLiteral Integer(int32_t value);
63+
static PrimitiveLiteral Long(int64_t value);
64+
static PrimitiveLiteral Float(float value);
65+
static PrimitiveLiteral Double(double value);
66+
static PrimitiveLiteral String(std::string value);
67+
static PrimitiveLiteral Binary(std::vector<uint8_t> value);
68+
69+
/// Create iceberg value from bytes.
70+
///
71+
/// See [this spec](https://iceberg.apache.org/spec/#binary-single-value-serialization) for reference.
72+
static Result<PrimitiveLiteral> Deserialize(std::span<const uint8_t> data);
73+
/// Serialize iceberg value to bytes.
74+
///
75+
/// See [this spec](https://iceberg.apache.org/spec/#binary-single-value-serialization) for reference.
76+
Result<std::vector<uint8_t>> Serialize() const;
77+
78+
// Get the value as a variant
79+
const PrimitiveLiteralValue& value() const;
80+
81+
// Get the Iceberg Type of the literal
82+
const std::shared_ptr<PrimitiveType>& type() const;
83+
84+
// Cast the literal to a specific type
85+
Result<PrimitiveLiteral> CastTo(const std::shared_ptr<PrimitiveType>& target_type) const;
86+
87+
std::partial_ordering operator<=>(const PrimitiveLiteral& other) const;
88+
89+
private:
90+
PrimitiveLiteralValue value_;
91+
std::shared_ptr<PrimitiveType> type_;
92+
};
93+
94+
} // namespace iceberg
95+

0 commit comments

Comments
 (0)