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/13 10:04:39 UTC

[avro] branch branch-1.11 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.11
in repository https://gitbox.apache.org/repos/asf/avro.git


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

commit 85a49f28dcc292d88c35205a31774c80e8f99875
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 30022bb..8e46394 100644
--- a/lang/ruby/lib/avro/schema_validator.rb
+++ b/lang/ruby/lib/avro/schema_validator.rb
@@ -133,7 +133,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 c851357..afbd81e 100644
--- a/lang/ruby/test/test_io.rb
+++ b/lang/ruby/test/test_io.rb
@@ -490,6 +490,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 8d100ef..8b12092 100644
--- a/lang/ruby/test/test_schema_validator.rb
+++ b/lang/ruby/test/test_schema_validator.rb
@@ -170,13 +170,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