Skip to content

Commit

Permalink
ARROW-4632: [Ruby] Add BigDecimal#to_arrow
Browse files Browse the repository at this point in the history
Author: Kenta Murata <[email protected]>

Closes apache#3709 from mrkn/ruby_bigdecimal_to_arrow and squashes the following commits:

28e7f89 <Kenta Murata> Fix test code following the review comments
0cab272 <Kenta Murata> Add BigDecimal#to_arrow
  • Loading branch information
mrkn authored and kou committed Feb 20, 2019
1 parent ef28f20 commit 24f83be
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 2 deletions.
24 changes: 24 additions & 0 deletions ruby/red-arrow/lib/arrow/bigdecimal-extension.rb
Original file line number Diff line number Diff line change
@@ -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
4 changes: 2 additions & 2 deletions ruby/red-arrow/lib/arrow/decimal128-array-builder.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
# specific language governing permissions and limitations
# under the License.

require "bigdecimal"
require "arrow/bigdecimal-extension"

module Arrow
class Decimal128ArrayBuilder
Expand All @@ -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
Expand Down
24 changes: 24 additions & 0 deletions ruby/red-arrow/test/test-bigdecimal.rb
Original file line number Diff line number Diff line change
@@ -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

0 comments on commit 24f83be

Please sign in to comment.