yingcai-cy commented on code in PR #117: URL: https://github.com/apache/iceberg-cpp/pull/117#discussion_r2135431953
########## src/iceberg/literal.h: ########## @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#pragma once + +#include <compare> +#include <memory> +#include <string> +#include <variant> +#include <vector> + +#include "iceberg/result.h" +#include "iceberg/type.h" + +namespace iceberg { + +/// \brief PrimitiveLiteral is a literal value that is associated with a primitive type. +class ICEBERG_EXPORT PrimitiveLiteral { + private: + /// \brief Exception type for values that are below the minimum allowed value for a + /// primitive type. + /// + /// When casting a value to a narrow primitive type, if the value exceeds the maximum of + /// target type, it might be above the maximum allowed value for that type. + struct BelowMin { + bool operator==(const BelowMin&) const = default; + std::strong_ordering operator<=>(const BelowMin&) const = default; + }; + + /// \brief Exception type for values that are above the maximum allowed value for a + /// primitive type. + /// + /// When casting a value to a narrow primitive type, if the value exceeds the maximum of + /// target type, it might be above the maximum allowed value for that type. + struct AboveMax { + bool operator==(const AboveMax&) const = default; + std::strong_ordering operator<=>(const AboveMax&) const = default; + }; + + using PrimitiveLiteralValue = + std::variant<bool, // for boolean + int32_t, // for int, date + int64_t, // for long, timestamp, timestamp_tz, time + float, // for float + double, // for double + std::string, // for string + std::vector<uint8_t>, // for binary, fixed + std::array<uint8_t, 16>, // for uuid and decimal + BelowMin, AboveMax>; + + public: + /// Factory methods for primitive types + static PrimitiveLiteral Boolean(bool value); + static PrimitiveLiteral Int(int32_t value); + static PrimitiveLiteral Long(int64_t value); + static PrimitiveLiteral Float(float value); + static PrimitiveLiteral Double(double value); + static PrimitiveLiteral String(std::string value); + static PrimitiveLiteral Binary(std::vector<uint8_t> value); Review Comment: Can UUIDLiteral and DecimalLiteral also be created via factory methods? ########## src/iceberg/literal.h: ########## @@ -0,0 +1,138 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#pragma once + +#include <compare> +#include <memory> +#include <string> +#include <variant> +#include <vector> + +#include "iceberg/result.h" +#include "iceberg/type.h" + +namespace iceberg { + +/// \brief PrimitiveLiteral is a literal value that is associated with a primitive type. +class ICEBERG_EXPORT PrimitiveLiteral { + private: + /// \brief Exception type for values that are below the minimum allowed value for a + /// primitive type. + /// + /// When casting a value to a narrow primitive type, if the value exceeds the maximum of + /// target type, it might be above the maximum allowed value for that type. + struct BelowMin { + bool operator==(const BelowMin&) const = default; + std::strong_ordering operator<=>(const BelowMin&) const = default; + }; + + /// \brief Exception type for values that are above the maximum allowed value for a + /// primitive type. + /// + /// When casting a value to a narrow primitive type, if the value exceeds the maximum of + /// target type, it might be above the maximum allowed value for that type. + struct AboveMax { + bool operator==(const AboveMax&) const = default; + std::strong_ordering operator<=>(const AboveMax&) const = default; + }; + + using PrimitiveLiteralValue = + std::variant<bool, // for boolean + int32_t, // for int, date + int64_t, // for long, timestamp, timestamp_tz, time + float, // for float + double, // for double + std::string, // for string + std::vector<uint8_t>, // for binary, fixed + std::array<uint8_t, 16>, // for uuid and decimal + BelowMin, AboveMax>; + + public: + /// Factory methods for primitive types + static PrimitiveLiteral Boolean(bool value); + static PrimitiveLiteral Int(int32_t value); Review Comment: Not sure if we should treat Timestamp and Date differently from long/int in expressions or elsewhere. ########## src/iceberg/literal.cc: ########## @@ -0,0 +1,323 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ + +#include "iceberg/literal.h" + +#include <cmath> +#include <concepts> +#include <sstream> + +#include "iceberg/exception.h" + +namespace iceberg { + +// Constructor +PrimitiveLiteral::PrimitiveLiteral(PrimitiveLiteralValue value, + std::shared_ptr<PrimitiveType> type) + : value_(std::move(value)), type_(std::move(type)) {} + +// Factory methods +PrimitiveLiteral PrimitiveLiteral::Boolean(bool value) { + return {PrimitiveLiteralValue{value}, std::make_shared<BooleanType>()}; +} + +PrimitiveLiteral PrimitiveLiteral::Int(int32_t value) { + return {PrimitiveLiteralValue{value}, std::make_shared<IntType>()}; +} + +PrimitiveLiteral PrimitiveLiteral::Long(int64_t value) { + return {PrimitiveLiteralValue{value}, std::make_shared<LongType>()}; +} + +PrimitiveLiteral PrimitiveLiteral::Float(float value) { + return {PrimitiveLiteralValue{value}, std::make_shared<FloatType>()}; +} + +PrimitiveLiteral PrimitiveLiteral::Double(double value) { + return {PrimitiveLiteralValue{value}, std::make_shared<DoubleType>()}; +} + +PrimitiveLiteral PrimitiveLiteral::String(std::string value) { + return {PrimitiveLiteralValue{std::move(value)}, std::make_shared<StringType>()}; +} + +PrimitiveLiteral PrimitiveLiteral::Binary(std::vector<uint8_t> value) { + return {PrimitiveLiteralValue{std::move(value)}, std::make_shared<BinaryType>()}; +} + +PrimitiveLiteral PrimitiveLiteral::BelowMinLiteral(std::shared_ptr<PrimitiveType> type) { + return {PrimitiveLiteralValue{BelowMin{}}, std::move(type)}; +} + +PrimitiveLiteral PrimitiveLiteral::AboveMaxLiteral(std::shared_ptr<PrimitiveType> type) { + return {PrimitiveLiteralValue{AboveMax{}}, std::move(type)}; +} + +Result<PrimitiveLiteral> PrimitiveLiteral::Deserialize(std::span<const uint8_t> data) { + return NotImplemented("Deserialization of PrimitiveLiteral is not implemented yet"); +} + +Result<std::vector<uint8_t>> PrimitiveLiteral::Serialize() const { + return NotImplemented("Serialization of PrimitiveLiteral is not implemented yet"); +} + +// Getters + +const std::shared_ptr<PrimitiveType>& PrimitiveLiteral::type() const { return type_; } + +// Cast method +Result<PrimitiveLiteral> PrimitiveLiteral::CastTo( + const std::shared_ptr<PrimitiveType>& target_type) const { + if (*type_ == *target_type) { + // If types are the same, return a copy of the current literal + return PrimitiveLiteral(value_, target_type); + } + + // Handle special values + if (std::holds_alternative<BelowMin>(value_) || + std::holds_alternative<AboveMax>(value_)) { + // Cannot cast type for special values + return NotSupported("Cannot cast type for {}", ToString()); + } + + auto source_type_id = type_->type_id(); + auto target_type_id = target_type->type_id(); + + // Delegate to specific cast functions based on source type + switch (source_type_id) { + case TypeId::kInt: + return CastFromInt(target_type_id); + case TypeId::kLong: + return CastFromLong(target_type_id); + case TypeId::kFloat: + return CastFromFloat(target_type_id); + case TypeId::kDouble: + case TypeId::kBoolean: + case TypeId::kString: + case TypeId::kBinary: + break; + default: + break; + } + + return NotSupported("Cast from {} to {} is not implemented", type_->ToString(), Review Comment: not implemented -> not supported? -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org --------------------------------------------------------------------- To unsubscribe, e-mail: issues-unsubscr...@iceberg.apache.org For additional commands, e-mail: issues-h...@iceberg.apache.org