Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/iceberg/test/truncate_util_test.cc
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
Expand Down Expand Up @@ -51,6 +51,26 @@
Literal::Binary(std::vector<uint8_t>(expected.begin(), expected.end())));
}

TEST(TruncateUtilTest, TruncateLiteralRejectsInvalidWidth) {
std::vector<uint8_t> data{1, 2, 3};

auto expect_invalid_width = [](const auto& result) {
EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument));
EXPECT_THAT(result, HasErrorMessage("Width must be positive"));
};

for (int32_t width : {0, -1}) {
SCOPED_TRACE(width);
expect_invalid_width(TruncateUtils::TruncateLiteral(Literal::Int(1), width));
expect_invalid_width(TruncateUtils::TruncateLiteral(Literal::Long(1), width));
expect_invalid_width(
TruncateUtils::TruncateLiteral(Literal::Decimal(1065, 4, 2), width));
expect_invalid_width(
TruncateUtils::TruncateLiteral(Literal::String("iceberg"), width));
expect_invalid_width(TruncateUtils::TruncateLiteral(Literal::Binary(data), width));
}
}

TEST(TruncateUtilTest, TruncateBinaryMax) {
std::vector<uint8_t> test1{1, 1, 2};
std::vector<uint8_t> test2{1, 1, 0xFF, 2};
Expand Down Expand Up @@ -190,4 +210,27 @@
EXPECT_EQ(result9_2, Literal::String(test9_2_expected));
}

TEST(TruncateUtilTest, TruncateLiteralMaxRejectsInvalidWidth) {
std::vector<uint8_t> data{1, 2, 3};

auto expect_invalid_width = [](const auto& result) {
EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument));
EXPECT_THAT(result, HasErrorMessage("Width must be positive"));
};

for (int32_t width : {0, -1}) {
SCOPED_TRACE(width);
expect_invalid_width(
TruncateUtils::TruncateLiteralMax(Literal::String("iceberg"), width));
expect_invalid_width(
TruncateUtils::TruncateLiteralMax(Literal::Binary(data), width));
}
}

TEST(TruncateUtilTest, TruncateUTF8MaxRejectsZeroWidth) {
auto result = TruncateUtils::TruncateUTF8Max("iceberg", 0);
EXPECT_THAT(result, IsError(ErrorKind::kInvalidArgument));
EXPECT_THAT(result, HasErrorMessage("Width must be positive"));
}

} // namespace iceberg
21 changes: 21 additions & 0 deletions src/iceberg/util/truncate_util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
#include "iceberg/expression/literal.h"
#include "iceberg/type.h"
#include "iceberg/util/checked_cast.h"
#include "iceberg/util/macros.h"

namespace iceberg {

Expand All @@ -34,6 +35,20 @@ constexpr uint32_t kUtf8MaxCodePoint = 0x10FFFF;
constexpr uint32_t kUtf8MinSurrogate = 0xD800;
constexpr uint32_t kUtf8MaxSurrogate = 0xDFFF;

Status ValidateTruncateWidth(int32_t width) {
if (width <= 0) {
return InvalidArgument("Width must be positive, got {}", width);
}
return {};
}

Status ValidateTruncateWidth(size_t width) {
if (width == 0) {
return InvalidArgument("Width must be positive, got 0");
}
return {};
}

std::optional<uint32_t> DecodeUtf8CodePoint(std::string_view source) {
if (source.empty()) {
return std::nullopt;
Expand Down Expand Up @@ -205,6 +220,8 @@ Result<std::optional<Literal>> TruncateLiteralMaxImpl<TypeId::kBinary>(

Result<std::optional<std::string>> TruncateUtils::TruncateUTF8Max(
const std::string& source, size_t L) {
ICEBERG_RETURN_UNEXPECTED(ValidateTruncateWidth(L));

std::string truncated = TruncateUTF8(source, L);
if (truncated == source) {
return truncated;
Expand Down Expand Up @@ -253,6 +270,8 @@ Decimal TruncateUtils::TruncateDecimal(const Decimal& decimal, int32_t width) {
return TruncateLiteralImpl<TYPE_ID>(literal, width);

Result<Literal> TruncateUtils::TruncateLiteral(const Literal& literal, int32_t width) {
ICEBERG_RETURN_UNEXPECTED(ValidateTruncateWidth(width));

if (literal.IsNull()) [[unlikely]] {
// Return null as is
return literal;
Expand Down Expand Up @@ -280,6 +299,8 @@ Result<Literal> TruncateUtils::TruncateLiteral(const Literal& literal, int32_t w

Result<std::optional<Literal>> TruncateUtils::TruncateLiteralMax(const Literal& literal,
int32_t width) {
ICEBERG_RETURN_UNEXPECTED(ValidateTruncateWidth(width));

if (literal.IsNull()) [[unlikely]] {
// Return null as is
return literal;
Expand Down
Loading