AVRO-2903: Ruby: accept BigDecimal datum for float and double (#1364)
Co-authored-by: jjlee <jj76.lee@navercorp.com>
diff --git a/lang/ruby/lib/avro/schema_validator.rb b/lang/ruby/lib/avro/schema_validator.rb
index 71f5963..3b602d4 100644
--- a/lang/ruby/lib/avro/schema_validator.rb
+++ b/lang/ruby/lib/avro/schema_validator.rb
@@ -132,7 +132,7 @@
fail TypeMismatchError unless datum.is_a?(Integer)
result.add_error(path, "out of bound value #{datum}") unless LONG_RANGE.cover?(datum)
when :float, :double
- fail TypeMismatchError unless datum.is_a?(Float) || datum.is_a?(Integer)
+ fail TypeMismatchError unless datum.is_a?(Float) || datum.is_a?(Integer) || datum.is_a?(BigDecimal)
when :fixed
if datum.is_a? String
result.add_error(path, fixed_string_message(expected_schema.size, datum)) unless datum.bytesize == expected_schema.size
diff --git a/lang/ruby/test/test_io.rb b/lang/ruby/test/test_io.rb
index fcc3f97..81e0607 100644
--- a/lang/ruby/test/test_io.rb
+++ b/lang/ruby/test/test_io.rb
@@ -489,6 +489,20 @@
assert_equal(datum_read, { 'field2' => 1 })
end
+ def test_big_decimal_datum_for_float
+ writers_schema = Avro::Schema.parse('"float"')
+ writer, * = write_datum(BigDecimal('1.2'), writers_schema)
+ datum_read = read_datum(writer, writers_schema)
+ assert_in_delta(1.2, datum_read)
+ end
+
+ def test_big_decimal_datum_for_double
+ writers_schema = Avro::Schema.parse('"double"')
+ writer, * = write_datum(BigDecimal("1.2"), writers_schema)
+ datum_read = read_datum(writer, writers_schema)
+ assert_in_delta(1.2, datum_read)
+ end
+
def test_snappy_backward_compat
# a snappy-compressed block payload without the checksum
# this has no back-references, just one literal so the last 9
diff --git a/lang/ruby/test/test_schema_validator.rb b/lang/ruby/test/test_schema_validator.rb
index e15dbdf..5d8471b 100644
--- a/lang/ruby/test/test_schema_validator.rb
+++ b/lang/ruby/test/test_schema_validator.rb
@@ -169,13 +169,13 @@
def test_validate_float
schema = hash_to_schema(type: 'float', name: 'name')
- assert_valid_schema(schema, [1.1, 1, Avro::Schema::LONG_MAX_VALUE], ['string'], true)
+ assert_valid_schema(schema, [1.1, 1, BigDecimal('1.1'), Avro::Schema::LONG_MAX_VALUE], ['string'], true)
end
def test_validate_double
schema = hash_to_schema(type: 'double', name: 'name')
- assert_valid_schema(schema, [1.1, 1, Avro::Schema::LONG_MAX_VALUE], ['string'], true)
+ assert_valid_schema(schema, [1.1, 1, BigDecimal('1.1'), Avro::Schema::LONG_MAX_VALUE], ['string'], true)
end
def test_validate_fixed