HeartLinked commented on code in PR #206:
URL: https://github.com/apache/iceberg-cpp/pull/206#discussion_r2342785617


##########
src/iceberg/expression/literal.cc:
##########
@@ -109,17 +172,230 @@ Result<Literal> LiteralCaster::CastFromLong(
 Result<Literal> LiteralCaster::CastFromFloat(
     const Literal& literal, const std::shared_ptr<PrimitiveType>& target_type) 
{
   auto float_val = std::get<float>(literal.value_);
-  auto target_type_id = target_type->type_id();
 
-  switch (target_type_id) {
+  switch (target_type->type_id()) {
     case TypeId::kDouble:
       return Literal::Double(static_cast<double>(float_val));
+    // TODO(Li Feiyang): Implement cast from Float to decimal
     default:
       return NotSupported("Cast from Float to {} is not supported",
                           target_type->ToString());
   }
 }
 
+Result<Literal> LiteralCaster::CastFromDouble(
+    const Literal& literal, const std::shared_ptr<PrimitiveType>& target_type) 
{
+  auto double_val = std::get<double>(literal.value_);
+
+  switch (target_type->type_id()) {
+    case TypeId::kFloat: {
+      if (double_val > static_cast<double>(std::numeric_limits<float>::max())) 
{
+        return AboveMaxLiteral(target_type);
+      }
+      if (double_val < 
static_cast<double>(std::numeric_limits<float>::lowest())) {
+        return BelowMinLiteral(target_type);
+      }
+      return Literal::Float(static_cast<float>(double_val));
+    }
+    default:
+      return NotSupported("Cast from Double to {} is not supported",
+                          target_type->ToString());
+  }
+}
+
+Result<Literal> LiteralCaster::CastFromString(
+    const Literal& literal, const std::shared_ptr<PrimitiveType>& target_type) 
{
+  const auto& str_val = std::get<std::string>(literal.value_);
+  std::istringstream in{str_val};
+  std::tm tm = {};
+
+  switch (target_type->type_id()) {
+    case TypeId::kDate: {
+      // Parse "YYYY-MM-DD" into days since 1970-01-01 epoch.
+      in >> std::get_time(&tm, "%Y-%m-%d");
+
+      if (in.fail() || tm.tm_mday == 0 || in.peek() != EOF) {

Review Comment:
   Checking tm_mday ensures the entire format was parsed because it is the last 
required field, a necessary check since in.fail() often won't report an error 
for incomplete input, only for an explicit format mismatch.
   



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

Reply via email to