|
| 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