Skip to content

Commit 2beaf96

Browse files
committed
remove decimal
1 parent e83598e commit 2beaf96

File tree

3 files changed

+6
-200
lines changed

3 files changed

+6
-200
lines changed

src/iceberg/expression/literal.cc

Lines changed: 1 addition & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -103,11 +103,6 @@ Result<Literal> LiteralCaster::CastFromInt(
103103
return Literal::Double(static_cast<double>(int_val));
104104
case TypeId::kDate:
105105
return Literal::Date(int_val);
106-
case TypeId::kDecimal: {
107-
auto decimal_type = std::static_pointer_cast<DecimalType>(target_type);
108-
return Literal::Decimal(static_cast<int128_t>(int_val), decimal_type->precision(),
109-
decimal_type->scale());
110-
}
111106
default:
112107
return NotSupported("Cast from Int to {} is not implemented",
113108
target_type->ToString());
@@ -148,11 +143,6 @@ Result<Literal> LiteralCaster::CastFromLong(
148143
return Literal::Timestamp(long_val);
149144
case TypeId::kTimestampTz:
150145
return Literal::TimestampTz(long_val);
151-
case TypeId::kDecimal: {
152-
auto decimal_type = std::static_pointer_cast<DecimalType>(target_type);
153-
return Literal::Decimal(static_cast<int128_t>(long_val), decimal_type->precision(),
154-
decimal_type->scale());
155-
}
156146
default:
157147
return NotSupported("Cast from Long to {} is not supported",
158148
target_type->ToString());
@@ -166,21 +156,6 @@ Result<Literal> LiteralCaster::CastFromFloat(
166156
switch (target_type->type_id()) {
167157
case TypeId::kDouble:
168158
return Literal::Double(static_cast<double>(float_val));
169-
case TypeId::kDecimal: {
170-
auto decimal_type = std::static_pointer_cast<DecimalType>(target_type);
171-
std::string float_str = std::to_string(float_val);
172-
173-
iceberg::Decimal parsed_val;
174-
int32_t parsed_scale = 0;
175-
ICEBERG_ASSIGN_OR_RAISE(
176-
parsed_val, iceberg::Decimal::FromString(float_str, nullptr, &parsed_scale));
177-
iceberg::Decimal rescaled_val;
178-
ICEBERG_ASSIGN_OR_RAISE(rescaled_val,
179-
parsed_val.Rescale(parsed_scale, decimal_type->scale()));
180-
181-
return Literal::Decimal(rescaled_val.value(), decimal_type->precision(),
182-
decimal_type->scale());
183-
}
184159
default:
185160
return NotSupported("Cast from Float to {} is not supported",
186161
target_type->ToString());
@@ -201,21 +176,6 @@ Result<Literal> LiteralCaster::CastFromDouble(
201176
}
202177
return Literal::Float(static_cast<float>(double_val));
203178
}
204-
case TypeId::kDecimal: {
205-
auto decimal_type = std::static_pointer_cast<DecimalType>(target_type);
206-
std::string double_str = std::to_string(double_val);
207-
208-
iceberg::Decimal parsed_val;
209-
int32_t parsed_scale = 0;
210-
ICEBERG_ASSIGN_OR_RAISE(
211-
parsed_val, iceberg::Decimal::FromString(double_str, nullptr, &parsed_scale));
212-
iceberg::Decimal rescaled_val;
213-
ICEBERG_ASSIGN_OR_RAISE(rescaled_val,
214-
parsed_val.Rescale(parsed_scale, decimal_type->scale()));
215-
216-
return Literal::Decimal(rescaled_val.value(), decimal_type->precision(),
217-
decimal_type->scale());
218-
}
219179
default:
220180
return NotSupported("Cast from Double to {} is not supported",
221181
target_type->ToString());
@@ -234,14 +194,6 @@ Result<Literal> LiteralCaster::CastFromString(
234194
case TypeId::kUuid:
235195
return NotImplemented("Cast from String to {} is not implemented yet",
236196
target_type->ToString());
237-
case TypeId::kDecimal: {
238-
auto decimal_type = std::static_pointer_cast<DecimalType>(target_type);
239-
iceberg::Decimal dec_val;
240-
ICEBERG_ASSIGN_OR_RAISE(dec_val, iceberg::Decimal::FromString(str_val));
241-
return Literal::Decimal(dec_val.value(), decimal_type->precision(),
242-
decimal_type->scale());
243-
}
244-
245197
default:
246198
return NotSupported("Cast from String to {} is not supported",
247199
target_type->ToString());
@@ -341,15 +293,6 @@ Literal Literal::Fixed(std::vector<uint8_t> value) {
341293
return {Value{std::move(value)}, fixed(size)};
342294
}
343295

344-
Literal Literal::Decimal(const iceberg::Decimal& value, int32_t precision,
345-
int32_t scale) {
346-
return {Value{value.value()}, decimal(precision, scale)};
347-
}
348-
349-
Literal Literal::Decimal(int128_t value, int32_t precision, int32_t scale) {
350-
return {Value{value}, decimal(precision, scale)};
351-
}
352-
353296
Result<Literal> Literal::Deserialize(std::span<const uint8_t> data,
354297
std::shared_ptr<PrimitiveType> type) {
355298
return NotImplemented("Deserialization of Literal is not implemented yet");
@@ -452,12 +395,6 @@ std::partial_ordering Literal::operator<=>(const Literal& other) const {
452395
return this_val <=> other_val;
453396
}
454397

455-
case TypeId::kDecimal: {
456-
auto this_val = std::get<int128_t>(value_);
457-
auto other_val = std::get<int128_t>(other.value_);
458-
return this_val <=> other_val;
459-
}
460-
461398
default:
462399
// For unsupported types, return unordered
463400
return std::partial_ordering::unordered;
@@ -505,8 +442,7 @@ std::string Literal::ToString() const {
505442
case TypeId::kFixed: {
506443
const auto& binary_data = std::get<std::vector<uint8_t>>(value_);
507444
std::string result = "X'";
508-
result.reserve(2 + binary_data.size() * 2 +
509-
1); // 2 chars per byte and 2 + 1 for prefix and suffix
445+
result.reserve(/*prefix*/ 2 + /*suffix*/ 1 + /*data*/ binary_data.size() * 2);
510446
for (const auto& byte : binary_data) {
511447
std::format_to(std::back_inserter(result), "{:02X}", byte);
512448
}
@@ -521,18 +457,6 @@ std::string Literal::ToString() const {
521457
case TypeId::kDate: {
522458
return std::to_string(std::get<int32_t>(value_));
523459
}
524-
case TypeId::kDecimal: {
525-
const auto unscaled_value = std::get<int128_t>(value_);
526-
auto decimal_type = std::static_pointer_cast<DecimalType>(type_);
527-
int32_t scale = decimal_type->scale();
528-
529-
iceberg::Decimal decimal_val(unscaled_value);
530-
Result<std::string> str_res = decimal_val.ToString(scale);
531-
if (str_res.has_value()) {
532-
return str_res.value();
533-
}
534-
return invalid_argument();
535-
}
536460
default: {
537461
return unsupported_error();
538462
}

src/iceberg/expression/literal.h

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,10 +76,6 @@ class ICEBERG_EXPORT Literal {
7676
static Literal Binary(std::vector<uint8_t> value);
7777
static Literal Fixed(std::vector<uint8_t> value);
7878

79-
/// \brief Factory methods for decimal type
80-
static Literal Decimal(const Decimal& value, int32_t precision, int32_t scale);
81-
static Literal Decimal(int128_t value, int32_t precision, int32_t scale);
82-
8379
/// \brief Create a literal representing a null value.
8480
static Literal Null(std::shared_ptr<PrimitiveType> type) {
8581
return {Value{std::monostate{}}, std::move(type)};

test/literal_test.cc

Lines changed: 5 additions & 119 deletions
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@ void AssertCastSucceeds(const Result<Literal>& result, TypeId expected_type_id,
4040
ASSERT_THAT(result, IsOk());
4141
EXPECT_EQ(result->type()->type_id(), expected_type_id);
4242
ASSERT_NO_THROW(EXPECT_EQ(std::get<T>(result->value()), expected_value))
43-
<< "Value type mismatch in std::get. Expected type for TypeId "
43+
<< "Type mismatch in std::get. Expected type for TypeId "
4444
<< static_cast<int>(expected_type_id);
4545
}
4646

@@ -105,10 +105,6 @@ TEST(LiteralTest, IntCastTo) {
105105

106106
// Cast to Date
107107
AssertCastSucceeds(int_literal.CastTo(iceberg::date()), TypeId::kDate, 42);
108-
109-
// Cast to Decimal
110-
AssertCastSucceeds(int_literal.CastTo(iceberg::decimal(10, 2)), TypeId::kDecimal,
111-
static_cast<int128_t>(42));
112108
}
113109

114110
// Long type tests
@@ -150,19 +146,15 @@ TEST(LiteralTest, LongCastTo) {
150146

151147
// Cast to Time
152148
AssertCastSucceeds(long_literal.CastTo(iceberg::time()), TypeId::kTime,
153-
static_cast<int64_t>(42L));
149+
static_cast<int64_t>(42));
154150

155151
// Cast to Timestamp
156152
AssertCastSucceeds(long_literal.CastTo(iceberg::timestamp()), TypeId::kTimestamp,
157-
static_cast<int64_t>(42L));
153+
static_cast<int64_t>(42));
158154

159155
// Cast to TimestampTz
160156
AssertCastSucceeds(long_literal.CastTo(iceberg::timestamp_tz()), TypeId::kTimestampTz,
161-
static_cast<int64_t>(42L));
162-
163-
// Cast to Decimal
164-
AssertCastSucceeds(long_literal.CastTo(iceberg::decimal(15, 3)), TypeId::kDecimal,
165-
static_cast<int128_t>(42L));
157+
static_cast<int64_t>(42));
166158
}
167159

168160
TEST(LiteralTest, LongCastToOverflow) {
@@ -217,11 +209,6 @@ TEST(LiteralTest, FloatCastTo) {
217209
// Cast to Double
218210
AssertCastSucceeds(float_literal.CastTo(iceberg::float64()), TypeId::kDouble,
219211
static_cast<double>(2.0f));
220-
221-
// Cast to Decimal
222-
auto decimal_result = float_literal.CastTo(iceberg::decimal(10, 2));
223-
ASSERT_THAT(decimal_result, IsOk());
224-
EXPECT_EQ(decimal_result->type()->type_id(), TypeId::kDecimal);
225212
}
226213

227214
// Double type tests
@@ -251,11 +238,6 @@ TEST(LiteralTest, DoubleCastTo) {
251238

252239
// Cast to Float
253240
AssertCastSucceeds(double_literal.CastTo(iceberg::float32()), TypeId::kFloat, 2.0f);
254-
255-
// Cast to Decimal
256-
auto decimal_result = double_literal.CastTo(iceberg::decimal(15, 2));
257-
ASSERT_THAT(decimal_result, IsOk());
258-
EXPECT_EQ(decimal_result->type()->type_id(), TypeId::kDecimal);
259241
}
260242

261243
TEST(LiteralTest, DoubleCastToOverflow) {
@@ -274,73 +256,6 @@ TEST(LiteralTest, DoubleCastToOverflow) {
274256
EXPECT_TRUE(min_result->IsBelowMin());
275257
}
276258

277-
// Decimal type tests
278-
TEST(LiteralTest, DecimalBasics) {
279-
// Test factory methods with different precision and scale
280-
auto decimal1 = Literal::Decimal(iceberg::Decimal(12345), 10, 2);
281-
auto decimal2 = Literal::Decimal(int128_t{-9876543}, 15, 3);
282-
auto decimal_zero = Literal::Decimal(int128_t{0}, 5, 1);
283-
284-
EXPECT_EQ(decimal1.type()->type_id(), TypeId::kDecimal);
285-
EXPECT_EQ(decimal2.type()->type_id(), TypeId::kDecimal);
286-
EXPECT_EQ(decimal_zero.type()->type_id(), TypeId::kDecimal);
287-
288-
// Check type properties
289-
auto decimal1_type = std::static_pointer_cast<iceberg::DecimalType>(decimal1.type());
290-
EXPECT_EQ(decimal1_type->precision(), 10);
291-
EXPECT_EQ(decimal1_type->scale(), 2);
292-
293-
auto decimal2_type = std::static_pointer_cast<iceberg::DecimalType>(decimal2.type());
294-
EXPECT_EQ(decimal2_type->precision(), 15);
295-
EXPECT_EQ(decimal2_type->scale(), 3);
296-
}
297-
298-
TEST(LiteralTest, DecimalToString) {
299-
// Test ToString for different decimal values with specific expected formats
300-
auto decimal_pos = Literal::Decimal(int128_t{12345}, 10, 2); // 123.45
301-
auto decimal_neg = Literal::Decimal(int128_t{-9876}, 8, 3); // -9.876
302-
auto decimal_zero = Literal::Decimal(int128_t{0}, 5, 1); // 0.0
303-
auto decimal_no_scale = Literal::Decimal(int128_t{42}, 5, 0); // 42
304-
305-
// Test expected decimal formatting
306-
EXPECT_EQ(decimal_pos.ToString(), "123.45");
307-
EXPECT_EQ(decimal_neg.ToString(), "-9.876");
308-
EXPECT_EQ(decimal_zero.ToString(), "0.0");
309-
EXPECT_EQ(decimal_no_scale.ToString(), "42");
310-
}
311-
312-
TEST(LiteralTest, DecimalComparison) {
313-
auto decimal1 = Literal::Decimal(int128_t{1000}, 10, 2);
314-
auto decimal2 = Literal::Decimal(int128_t{2000}, 10, 2);
315-
auto decimal3 = Literal::Decimal(int128_t{1000}, 10, 2);
316-
auto decimal_neg = Literal::Decimal(int128_t{-1000}, 10, 2);
317-
318-
EXPECT_EQ(decimal1 <=> decimal3, std::partial_ordering::equivalent);
319-
EXPECT_EQ(decimal1 <=> decimal2, std::partial_ordering::less);
320-
EXPECT_EQ(decimal2 <=> decimal1, std::partial_ordering::greater);
321-
EXPECT_EQ(decimal_neg <=> decimal1, std::partial_ordering::less);
322-
}
323-
324-
TEST(LiteralTest, DecimalMaxPrecisionAndScale) {
325-
// Test with maximum precision and scale values
326-
auto max_decimal = Literal::Decimal(int128_t{1}, 38, 38);
327-
328-
EXPECT_EQ(max_decimal.type()->type_id(), TypeId::kDecimal);
329-
auto decimal_type = std::static_pointer_cast<iceberg::DecimalType>(max_decimal.type());
330-
EXPECT_EQ(decimal_type->precision(), 38);
331-
EXPECT_EQ(decimal_type->scale(), 38);
332-
}
333-
334-
TEST(LiteralTest, DecimalZeroScale) {
335-
// Test with zero scale (integer)
336-
auto int_decimal = Literal::Decimal(int128_t{12345}, 10, 0);
337-
338-
EXPECT_EQ(int_decimal.type()->type_id(), TypeId::kDecimal);
339-
auto decimal_type = std::static_pointer_cast<iceberg::DecimalType>(int_decimal.type());
340-
EXPECT_EQ(decimal_type->precision(), 10);
341-
EXPECT_EQ(decimal_type->scale(), 0);
342-
}
343-
344259
// String type tests
345260
TEST(LiteralTest, StringBasics) {
346261
auto string_literal = Literal::String("hello world");
@@ -363,20 +278,6 @@ TEST(LiteralTest, StringComparison) {
363278
EXPECT_EQ(string2 <=> string1, std::partial_ordering::greater);
364279
}
365280

366-
TEST(LiteralTest, StringCastTo) {
367-
auto string_literal = Literal::String("123.456");
368-
369-
// Cast to Decimal
370-
auto decimal_result = string_literal.CastTo(iceberg::decimal(10, 3));
371-
ASSERT_THAT(decimal_result, IsOk());
372-
EXPECT_EQ(decimal_result->type()->type_id(), TypeId::kDecimal);
373-
374-
// Invalid string should fail
375-
auto invalid_string = Literal::String("not_a_number");
376-
EXPECT_THAT(invalid_string.CastTo(iceberg::decimal(10, 2)),
377-
IsError(ErrorKind::kInvalidArgument));
378-
}
379-
380281
// Binary type tests
381282
TEST(LiteralTest, BinaryBasics) {
382283
std::vector<uint8_t> data = {0x01, 0x02, 0x03, 0xFF};
@@ -551,38 +452,23 @@ TEST(LiteralTest, TimestampTzComparison) {
551452
TEST(LiteralTest, CrossTypeComparison) {
552453
auto int_literal = Literal::Int(42);
553454
auto string_literal = Literal::String("42");
554-
auto decimal_literal = Literal::Decimal(int128_t{42}, 10, 2);
555455

556456
// Different types should return unordered
557457
EXPECT_EQ(int_literal <=> string_literal, std::partial_ordering::unordered);
558-
EXPECT_EQ(decimal_literal <=> int_literal, std::partial_ordering::unordered);
559458
}
560459

561460
// Same type cast tests
562461
TEST(LiteralTest, SameTypeCast) {
563462
auto int_literal = Literal::Int(42);
564-
auto decimal_literal = Literal::Decimal(int128_t{12345}, 10, 2);
565-
566-
auto int_same_type_result = int_literal.CastTo(iceberg::int32());
567-
ASSERT_THAT(int_same_type_result, IsOk());
568-
EXPECT_EQ(int_same_type_result->type()->type_id(), TypeId::kInt);
569-
EXPECT_EQ(int_same_type_result->ToString(), "42");
570463

571-
auto decimal_same_type_result = decimal_literal.CastTo(iceberg::decimal(10, 2));
572-
ASSERT_THAT(decimal_same_type_result, IsOk());
573-
EXPECT_EQ(decimal_same_type_result->type()->type_id(), TypeId::kDecimal);
464+
AssertCastSucceeds(int_literal.CastTo(iceberg::int32()), TypeId::kInt, 42);
574465
}
575466

576467
// Special value tests
577468
TEST(LiteralTest, SpecialValues) {
578469
auto int_literal = Literal::Int(42);
579-
auto decimal_literal = Literal::Decimal(int128_t{12345}, 10, 2);
580-
581470
EXPECT_FALSE(int_literal.IsAboveMax());
582471
EXPECT_FALSE(int_literal.IsBelowMin());
583-
EXPECT_FALSE(decimal_literal.IsAboveMax());
584-
EXPECT_FALSE(decimal_literal.IsBelowMin());
585-
EXPECT_FALSE(decimal_literal.IsNull());
586472
}
587473

588474
// Float special values tests

0 commit comments

Comments
 (0)