From 24f83be6e241f3f8464a49c6c45d486a7c4e4bc9 Mon Sep 17 00:00:00 2001 From: Kenta Murata Date: Wed, 20 Feb 2019 11:05:01 +0900 Subject: [PATCH] ARROW-4632: [Ruby] Add BigDecimal#to_arrow Author: Kenta Murata Closes #3709 from mrkn/ruby_bigdecimal_to_arrow and squashes the following commits: 28e7f89b Fix test code following the review comments 0cab2720 Add BigDecimal#to_arrow --- .../lib/arrow/bigdecimal-extension.rb | 24 +++++++++++++++++++ .../lib/arrow/decimal128-array-builder.rb | 4 ++-- ruby/red-arrow/test/test-bigdecimal.rb | 24 +++++++++++++++++++ 3 files changed, 50 insertions(+), 2 deletions(-) create mode 100644 ruby/red-arrow/lib/arrow/bigdecimal-extension.rb create mode 100644 ruby/red-arrow/test/test-bigdecimal.rb diff --git a/ruby/red-arrow/lib/arrow/bigdecimal-extension.rb b/ruby/red-arrow/lib/arrow/bigdecimal-extension.rb new file mode 100644 index 0000000000000..a663f7032adc6 --- /dev/null +++ b/ruby/red-arrow/lib/arrow/bigdecimal-extension.rb @@ -0,0 +1,24 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +require "bigdecimal" + +class BigDecimal + def to_arrow + Arrow::Decimal128.new(to_s) + end +end diff --git a/ruby/red-arrow/lib/arrow/decimal128-array-builder.rb b/ruby/red-arrow/lib/arrow/decimal128-array-builder.rb index 9a849d487571e..cdf1696070cd0 100644 --- a/ruby/red-arrow/lib/arrow/decimal128-array-builder.rb +++ b/ruby/red-arrow/lib/arrow/decimal128-array-builder.rb @@ -15,7 +15,7 @@ # specific language governing permissions and limitations # under the License. -require "bigdecimal" +require "arrow/bigdecimal-extension" module Arrow class Decimal128ArrayBuilder @@ -36,7 +36,7 @@ def append_value(value) when Float value = Decimal128.new(value.to_s) when BigDecimal - value = Decimal128.new(value.to_s) + value = value.to_arrow end append_value_raw(value) end diff --git a/ruby/red-arrow/test/test-bigdecimal.rb b/ruby/red-arrow/test/test-bigdecimal.rb new file mode 100644 index 0000000000000..3874a38497874 --- /dev/null +++ b/ruby/red-arrow/test/test-bigdecimal.rb @@ -0,0 +1,24 @@ +# Licensed to the Apache Software Foundation (ASF) under one +# or more contributor license agreements. See the NOTICE file +# distributed with this work for additional information +# regarding copyright ownership. The ASF licenses this file +# to you under the Apache License, Version 2.0 (the +# "License"); you may not use this file except in compliance +# with the License. You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, +# software distributed under the License is distributed on an +# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY +# KIND, either express or implied. See the License for the +# specific language governing permissions and limitations +# under the License. + +class BigDecimalTest < Test::Unit::TestCase + test("#to_arrow") do + arrow_decimal = BigDecimal("3.14").to_arrow + assert_equal(Arrow::Decimal128.new("3.14"), + BigDecimal("3.14").to_arrow) + end +end