HeartLinked commented on code in PR #206: URL: https://github.com/apache/iceberg-cpp/pull/206#discussion_r2342802498
########## test/literal_test.cc: ########## @@ -227,6 +290,120 @@ TEST(LiteralTest, StringComparison) { EXPECT_EQ(string2 <=> string1, std::partial_ordering::greater); } +TEST(LiteralTest, StringCastToDate) { + // Test standard date format + auto literal1 = Literal::String("2023-05-15"); + auto result1 = literal1.CastTo(iceberg::date()); + ASSERT_THAT(result1, IsOk()); + EXPECT_EQ(result1->type()->type_id(), TypeId::kDate); + EXPECT_EQ(result1->ToString(), "19492"); + + // Test epoch + auto literal_epoch = Literal::String("1970-01-01"); + auto result_epoch = literal_epoch.CastTo(iceberg::date()); + ASSERT_THAT(result_epoch, IsOk()); + EXPECT_EQ(result_epoch->ToString(), "0"); + + // Test pre-epoch + auto literal_pre_epoch = Literal::String("1969-12-31"); + auto result_pre_epoch = literal_pre_epoch.CastTo(iceberg::date()); + ASSERT_THAT(result_pre_epoch, IsOk()); + EXPECT_EQ(result_pre_epoch->ToString(), "-1"); + + // Invalid Formats + auto invalid1 = Literal::String("2023/05/15"); + EXPECT_THAT(invalid1.CastTo(iceberg::date()), IsError(ErrorKind::kNotSupported)); + + auto invalid2 = Literal::String("2023-05-15 extra"); + EXPECT_THAT(invalid2.CastTo(iceberg::date()), IsError(ErrorKind::kNotSupported)); + + auto invalid3 = Literal::String("2023-05"); + EXPECT_THAT(invalid3.CastTo(iceberg::date()), IsError(ErrorKind::kNotSupported)); +} + +TEST(LiteralTest, StringCastToTime) { + // Test without fractional part + auto literal1 = Literal::String("12:00:00"); + auto result1 = literal1.CastTo(iceberg::time()); + ASSERT_THAT(result1, IsOk()); + EXPECT_EQ(result1->type()->type_id(), TypeId::kTime); + EXPECT_EQ(result1->ToString(), "43200000000"); // 12h in microseconds + + // Test with full fractional part + auto literal2 = Literal::String("12:34:56.123456"); + auto result2 = literal2.CastTo(iceberg::time()); + ASSERT_THAT(result2, IsOk()); + EXPECT_EQ(result2->ToString(), "45296123456"); + + // Test with fractional part that needs padding + auto literal3 = Literal::String("01:02:03.123"); + auto result3 = literal3.CastTo(iceberg::time()); + ASSERT_THAT(result3, IsOk()); + EXPECT_EQ(result3->ToString(), "3723123000"); // .123 becomes .123000 + + // Test with fractional part that needs truncation + auto literal4 = Literal::String("23:59:59.987654321"); + auto result4 = literal4.CastTo(iceberg::time()); + ASSERT_THAT(result4, IsOk()); + EXPECT_EQ(result4->ToString(), "86399987654"); + + // Invalid Formats + auto invalid1 = Literal::String("12-00-00"); + EXPECT_THAT(invalid1.CastTo(iceberg::time()), IsError(ErrorKind::kNotSupported)); + + auto invalid2 = Literal::String("12:00:00 extra"); + EXPECT_THAT(invalid2.CastTo(iceberg::time()), IsError(ErrorKind::kNotSupported)); + + auto invalid3 = Literal::String("25:00:00"); + EXPECT_THAT(invalid3.CastTo(iceberg::time()), IsError(ErrorKind::kNotSupported)); +} + +TEST(LiteralTest, StringCastToTimestamp) { + // Test without fractional part + auto literal1 = Literal::String("2023-05-15T12:00:00"); + auto result1 = literal1.CastTo(iceberg::timestamp()); + ASSERT_THAT(result1, IsOk()); + EXPECT_EQ(result1->type()->type_id(), TypeId::kTimestamp); + EXPECT_EQ(result1->ToString(), "1684152000000000"); + + // Test with full fractional part + auto literal2 = Literal::String("2023-05-15T12:34:56.123456"); + auto result2 = literal2.CastTo(iceberg::timestamp()); + ASSERT_THAT(result2, IsOk()); + EXPECT_EQ(result2->ToString(), "1684154096123456"); + + // Invalid Formats + auto invalid1 = Literal::String("2023-05-15 12:00:00"); Review Comment: ISO 8601 stipulates that when a date and time are combined in an expression, the capital letter T should be used as a separator. For example: 2023-05-15T12:00:00 -- 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