Skip to content

Commit

Permalink
Test nits
Browse files Browse the repository at this point in the history
  • Loading branch information
pitrou committed Oct 14, 2022
1 parent 12ed517 commit eef9f58
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 17 deletions.
6 changes: 3 additions & 3 deletions cpp/src/arrow/json/converter.cc
Original file line number Diff line number Diff line change
Expand Up @@ -176,16 +176,16 @@ class DecimalConverter : public PrimitiveConverter {
RETURN_NOT_OK(TypeTraits<T>::BuilderType::ValueType::FromString(
repr, &value, &precision, &scale));
if (precision > out_precision) {
return GenericConversionError(*out_type_, ". ", repr, " requires precision ",
return GenericConversionError(*out_type_, ": ", repr, " requires precision ",
precision);
}
if (scale != out_scale) {
auto result = value.Rescale(scale, out_scale);
if (ARROW_PREDICT_FALSE(!result.ok())) {
return GenericConversionError(*out_type_, ". ", repr, " requires scale ",
return GenericConversionError(*out_type_, ": ", repr, " requires scale ",
scale);
} else {
value = result.ValueUnsafe();
value = result.MoveValueUnsafe();
}
}
builder.UnsafeAppend(value);
Expand Down
30 changes: 16 additions & 14 deletions cpp/src/arrow/json/converter_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -223,10 +223,9 @@ TEST(ConverterTest, Decimal128And256) {
}

TEST(ConverterTest, Decimal128And256ScaleError) {
std::vector<std::shared_ptr<DataType>> types = {decimal128(38, 10), decimal256(38, 10)};
for (int i = 0; i < 2; ++i) {
for (auto decimal_type : {decimal128(38, 10), decimal256(38, 10)}) {
ParseOptions options;
options.explicit_schema = schema({field("", types[i])});
options.explicit_schema = schema({field("", decimal_type)});

std::string json_source = R"(
{"" : "30.0123456789001"}
Expand All @@ -235,18 +234,19 @@ TEST(ConverterTest, Decimal128And256ScaleError) {
std::shared_ptr<StructArray> parse_array;
ASSERT_OK(ParseFromString(options, json_source, &parse_array));

std::string error_msg = "Failed of conversion of JSON to " + types[i]->ToString() +
". 30.0123456789001 requires scale 13";
EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, ::testing::HasSubstr(error_msg),
Convert(types[i], parse_array->GetFieldByName("")));
std::string error_msg = "Failed of conversion of JSON to " +
decimal_type->ToString() +
": 30.0123456789001 requires scale 13";
EXPECT_RAISES_WITH_MESSAGE_THAT(
Invalid, ::testing::HasSubstr(error_msg),
Convert(decimal_type, parse_array->GetFieldByName("")));
}
}

TEST(ConverterTest, Decimal128And256PrecisionError) {
std::vector<std::shared_ptr<DataType>> types = {decimal128(38, 10), decimal256(38, 10)};
for (int i = 0; i < 2; ++i) {
for (auto decimal_type : {decimal128(38, 10), decimal256(38, 10)}) {
ParseOptions options;
options.explicit_schema = schema({field("", types[i])});
options.explicit_schema = schema({field("", decimal_type)});

std::string json_source = R"(
{"" : "123456789012345678901234567890.0123456789"}
Expand All @@ -256,11 +256,13 @@ TEST(ConverterTest, Decimal128And256PrecisionError) {
ASSERT_OK(ParseFromString(options, json_source, &parse_array));

std::string error_msg =
"Invalid: Failed of conversion of JSON to " + types[i]->ToString() +
". 123456789012345678901234567890.0123456789 requires precision 40";
EXPECT_RAISES_WITH_MESSAGE_THAT(Invalid, ::testing::HasSubstr(error_msg),
Convert(types[i], parse_array->GetFieldByName("")));
"Invalid: Failed of conversion of JSON to " + decimal_type->ToString() +
": 123456789012345678901234567890.0123456789 requires precision 40";
EXPECT_RAISES_WITH_MESSAGE_THAT(
Invalid, ::testing::HasSubstr(error_msg),
Convert(decimal_type, parse_array->GetFieldByName("")));
}
}

} // namespace json
} // namespace arrow
16 changes: 16 additions & 0 deletions python/pyarrow/tests/test_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
# under the License.

from collections import OrderedDict
from decimal import Decimal
import io
import itertools
import json
Expand Down Expand Up @@ -243,6 +244,21 @@ def test_reconcile_accross_blocks(self):
# Check that the issue was exercised
assert table.column("a").num_chunks > 1

def test_explicit_schema_decimal(self):
rows = (b'{"a": 1}\n'
b'{"a": 1.45}\n'
b'{"a": -23.456}\n'
b'{}\n')
expected = {
'a': [Decimal("1"), Decimal("1.45"), Decimal("-23.456"), None],
}
for type_factory in (pa.decimal128, pa.decimal256):
schema = pa.schema([('a', type_factory(9, 4))])
opts = ParseOptions(explicit_schema=schema)
table = self.read_bytes(rows, parse_options=opts)
assert table.schema == schema
assert table.to_pydict() == expected

def test_explicit_schema_with_unexpected_behaviour(self):
# infer by default
rows = (b'{"foo": "bar", "num": 0}\n'
Expand Down

0 comments on commit eef9f58

Please sign in to comment.