Skip to content

Commit

Permalink
Merge branch 'string_to_int' into uhugeint
Browse files Browse the repository at this point in the history
  • Loading branch information
nickgerrets committed Dec 1, 2023
2 parents b28fc43 + 6885560 commit 25c4649
Show file tree
Hide file tree
Showing 3 changed files with 181 additions and 8 deletions.
7 changes: 6 additions & 1 deletion src/common/operator/cast_operators.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2178,11 +2178,16 @@ struct HugeIntegerCastOperation {
return false;
}

if (state.decimal_total_digits == 0) {
if (state.decimal == 0 || state.decimal_total_digits == 0) {
return true;
}

// Get the first (left-most) digit of the decimals
while (state.decimal_total_digits > 39) {
state.decimal /= Hugeint::POWERS_OF_TEN[39];
state.decimal_total_digits -= 39;
}
D_ASSERT((state.decimal_total_digits - 1) >= 0 && (state.decimal_total_digits - 1) <= 39);
state.decimal /= Hugeint::POWERS_OF_TEN[state.decimal_total_digits - 1];

if (state.decimal >= 5 || state.decimal <= -5) {
Expand Down
80 changes: 76 additions & 4 deletions test/sql/cast/string_to_integer_decimal_cast.test
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,7 @@ select '-1.50004'::${type};

endloop


# Check limits (Signed)
# Check limits (signed)

query II
select '127.1'::TINYINT, '-128.1'::TINYINT;
Expand All @@ -84,8 +83,7 @@ select '170141183460469231731687303715884105727.1'::HUGEINT, '-17014118346046923
----
170141183460469231731687303715884105727 -170141183460469231731687303715884105728


# Check limits (Unsigned)
# Check limits (unsigned)

query I
select '255.1'::UTINYINT;
Expand All @@ -111,3 +109,77 @@ query I
select '340282366920938463463374607431768211455.1'::UHUGEINT;
----
340282366920938463463374607431768211455

# going over limit should error (signed)

statement error
select '127.5'::TINYINT;
----
Conversion Error

statement error
select '32767.5'::SMALLINT;
----
Conversion Error

statement error
select '2147483647.5'::INTEGER;
----
Conversion Error

statement error
select '9223372036854775807.5'::BIGINT;
----
Conversion Error

statement error
select '170141183460469231731687303715884105727.5'::HUGEINT;
----
Conversion Error

statement error
select '-128.5'::TINYINT;
----
Conversion Error

statement error
select '-32768.5'::SMALLINT;
----
Conversion Error

statement error
select '-2147483648.5'::INTEGER;
----
Conversion Error

statement error
select '-9223372036854775808.5'::BIGINT;
----
Conversion Error

statement error
select '-170141183460469231731687303715884105728.5'::HUGEINT;
----
Conversion Error

# going over limit should error (unsigned)

statement error
select '255.5'::UTINYINT;
----
Conversion Error

statement error
select '65535.5'::USMALLINT;
----
Conversion Error

statement error
select '4294967295.5'::UINTEGER;
----
Conversion Error

statement error
select '18446744073709551615.5'::UBIGINT;
----
Conversion Error
102 changes: 99 additions & 3 deletions test/sql/cast/string_to_integer_exponent_cast.test
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ Conversion Error

endloop

# Check limits (Signed)
# Check limits (signed)

query II
select '12.7e1'::TINYINT, '-12.8e1'::TINYINT;
Expand All @@ -142,8 +142,7 @@ select '17014118346046923173168730371588410572.7e1'::HUGEINT, '-1701411834604692
----
170141183460469231731687303715884105727 -170141183460469231731687303715884105728


# Check limits (Unsigned)
# Check limits (unsigned)

query I
select '25.5e1'::UTINYINT;
Expand All @@ -169,3 +168,100 @@ query I
select '34028236692093846346337460743176821145.5e1'::UHUGEINT;
----
340282366920938463463374607431768211455

# going over limit should error (signed)

statement error
select '12.8e1'::TINYINT;
----
Conversion Error

statement error
select '3276.8e1'::SMALLINT;
----
Conversion Error

statement error
select '214748364.8e1'::INTEGER;
----
Conversion Error

statement error
select '922337203685477580.8e1'::BIGINT;
----
Conversion Error

statement error
select '17014118346046923173168730371588410572.8e1'::HUGEINT;
----
Conversion Error

statement error
select '-12.9e1'::TINYINT;
----
Conversion Error

statement error
select '-3276.9e1'::SMALLINT;
----
Conversion Error

statement error
select '-214748364.9e1'::INTEGER;
----
Conversion Error

statement error
select '-922337203685477580.9e1'::BIGINT;
----
Conversion Error

statement error
select '-17014118346046923173168730371588410572.9e1'::HUGEINT;
----
Conversion Error

# going over limit should error (unsigned)

statement error
select '25.6e1'::UTINYINT;
----
Conversion Error

statement error
select '6553.6e1'::USMALLINT;
----
Conversion Error

statement error
select '429496729.6e1'::UINTEGER;
----
Conversion Error

statement error
select '1844674407370955161.6e1'::UBIGINT;
----
Conversion Error

# some more "extreme" cases

query I
select '0.00000000000000000000000000000009223372036854775807e50'::BIGINT;
----
9223372036854775807

query I
select '-0.00000000000000000000000000000009223372036854775807e50'::BIGINT;
----
-9223372036854775807

query I
select '0.00000000000170141183460469231731687303715884105727e50'::HUGEINT;
----
170141183460469231731687303715884105727

query I
select '-0.00000000000170141183460469231731687303715884105727e50'::HUGEINT;
----
-170141183460469231731687303715884105727

0 comments on commit 25c4649

Please sign in to comment.