You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@avro.apache.org by rs...@apache.org on 2021/10/12 15:25:43 UTC

[avro] branch branch-1.10 updated: AVRO-2903: Ruby: accept BigDecimal datum for float and double (#1364)

This is an automated email from the ASF dual-hosted git repository.

rskraba pushed a commit to branch branch-1.10
in repository https://gitbox.apache.org/repos/asf/avro.git


The following commit(s) were added to refs/heads/branch-1.10 by this push:
     new a6f13b2  AVRO-2903: Ruby: accept BigDecimal datum for float and double (#1364)
a6f13b2 is described below

commit a6f13b269a359d3839e55a75e0662d834d76992c
Author: Tim Perkins <tj...@users.noreply.github.com>
AuthorDate: Tue Oct 12 08:29:07 2021 -0400

    AVRO-2903: Ruby: accept BigDecimal datum for float and double (#1364)
    
    Co-authored-by: jjlee <jj...@navercorp.com>
---
 lang/ruby/lib/avro/schema_validator.rb  |  2 +-
 lang/ruby/test/test_io.rb               | 14 ++++++++++++++
 lang/ruby/test/test_schema_validator.rb |  4 ++--
 3 files changed, 17 insertions(+), 3 deletions(-)

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 @@ module Avro
           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 @@ EOS
     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 @@ class TestSchemaValidator < Test::Unit::TestCase
   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