Skip to content

Commit 4e51f98

Browse files
koushiro615
authored andcommitted
ARROW-6240: [Ruby] Arrow::Decimal128Array#get_value returns BigDecimal
Instead of Arrow::Decimal128. This is a backward incompatible change but it's acceptable. Because BigDecimal is easy to use than Arrow::Decimal128 in Ruby. BigDecimal is a self-contained object. It means that we can get expected result by #to_s without Arrow::Decimal128DataType. Users can still get value as Arrow::Deciaml128 by Arrow::Decimal128Array#get_raw_value. Closes apache#5089 from kou/ruby-use-bigdecimal and squashes the following commits: e7e4853 <Sutou Kouhei> Arrow::Decimal128Array#get_value returns BigDecimal Authored-by: Sutou Kouhei <kou@clear-code.com> Signed-off-by: Yosuke Shiro <yosuke.shiro615@gmail.com>
1 parent 3cc12ab commit 4e51f98

4 files changed

Lines changed: 44 additions & 16 deletions

File tree

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Licensed to the Apache Software Foundation (ASF) under one
2+
# or more contributor license agreements. See the NOTICE file
3+
# distributed with this work for additional information
4+
# regarding copyright ownership. The ASF licenses this file
5+
# to you under the Apache License, Version 2.0 (the
6+
# "License"); you may not use this file except in compliance
7+
# with the License. You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing,
12+
# software distributed under the License is distributed on an
13+
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14+
# KIND, either express or implied. See the License for the
15+
# specific language governing permissions and limitations
16+
# under the License.
17+
18+
module Arrow
19+
class Decimal128Array
20+
def get_value(i)
21+
BigDecimal(format_value(i))
22+
end
23+
end
24+
end

ruby/red-arrow/lib/arrow/loader.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ def require_libraries
4646
require "arrow/date64-array"
4747
require "arrow/date64-array-builder"
4848
require "arrow/decimal128"
49+
require "arrow/decimal128-array"
4950
require "arrow/decimal128-array-builder"
5051
require "arrow/decimal128-data-type"
5152
require "arrow/dense-union-data-type"
@@ -115,7 +116,10 @@ def load_method_info(info, klass, method_name)
115116
method_name = "get_value"
116117
end
117118
super(info, klass, method_name)
118-
when "Arrow::TimestampArray", "Arrow::Date32Array", "Arrow::Date64Array"
119+
when "Arrow::Date32Array",
120+
"Arrow::Date64Array",
121+
"Arrow::Decimal128Array",
122+
"Arrow::TimestampArray"
119123
case method_name
120124
when "get_value"
121125
method_name = "get_raw_value"

ruby/red-arrow/test/test-decimal128-array-builder.rb

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@
1717

1818
class Decimal128ArrayBuilderTest < Test::Unit::TestCase
1919
def setup
20-
@data_type = Arrow::Decimal128DataType.new(8, 2)
20+
@data_type = Arrow::Decimal128DataType.new(3, 1)
2121
@builder = Arrow::Decimal128ArrayBuilder.new(@data_type)
2222
end
2323

@@ -31,28 +31,28 @@ def setup
3131
test("Arrow::Decimal128") do
3232
@builder.append_value(Arrow::Decimal128.new("10.1"))
3333
array = @builder.finish
34-
assert_equal(Arrow::Decimal128.new("10.1"),
34+
assert_equal(BigDecimal("10.1"),
3535
array[0])
3636
end
3737

3838
test("String") do
3939
@builder.append_value("10.1")
4040
array = @builder.finish
41-
assert_equal(Arrow::Decimal128.new("10.1"),
41+
assert_equal(BigDecimal("10.1"),
4242
array[0])
4343
end
4444

4545
test("Float") do
4646
@builder.append_value(10.1)
4747
array = @builder.finish
48-
assert_equal(Arrow::Decimal128.new("10.1"),
48+
assert_equal(BigDecimal("10.1"),
4949
array[0])
5050
end
5151

5252
test("BigDecimal") do
5353
@builder.append_value(BigDecimal("10.1"))
5454
array = @builder.finish
55-
assert_equal(Arrow::Decimal128.new("10.1"),
55+
assert_equal(BigDecimal("10.1"),
5656
array[0])
5757
end
5858
end
@@ -68,11 +68,11 @@ def setup
6868
])
6969
array = @builder.finish
7070
assert_equal([
71-
Arrow::Decimal128.new("10.1"),
71+
BigDecimal("10.1"),
7272
nil,
73-
Arrow::Decimal128.new("10.1"),
74-
Arrow::Decimal128.new("10.1"),
75-
Arrow::Decimal128.new("10.1"),
73+
BigDecimal("10.1"),
74+
BigDecimal("10.1"),
75+
BigDecimal("10.1"),
7676
],
7777
array.to_a)
7878
end
@@ -85,9 +85,9 @@ def setup
8585
])
8686
array = @builder.finish
8787
assert_equal([
88-
Arrow::Decimal128.new("10.1"),
88+
BigDecimal("10.1"),
8989
nil,
90-
Arrow::Decimal128.new("10.1"),
90+
BigDecimal("10.1"),
9191
],
9292
array.to_a)
9393
end

ruby/red-arrow/test/test-decimal128-array.rb

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
class Decimal128ArrayTest < Test::Unit::TestCase
1919
sub_test_case(".new") do
2020
test("build") do
21-
data_type = Arrow::Decimal128DataType.new(8, 2)
21+
data_type = Arrow::Decimal128DataType.new(3, 1)
2222
values = [
2323
10.1,
2424
nil,
@@ -27,10 +27,10 @@ class Decimal128ArrayTest < Test::Unit::TestCase
2727
]
2828
array = Arrow::Decimal128Array.new(data_type, values)
2929
assert_equal([
30-
Arrow::Decimal128.new("10.1"),
30+
BigDecimal("10.1"),
3131
nil,
32-
Arrow::Decimal128.new("10.1"),
33-
Arrow::Decimal128.new("10.1"),
32+
BigDecimal("10.1"),
33+
BigDecimal("10.1"),
3434
],
3535
array.to_a)
3636
end

0 commit comments

Comments
 (0)