Skip to content

Commit

Permalink
apacheGH-39433: [Ruby] Add support for Table.load(format: json) optio…
Browse files Browse the repository at this point in the history
…ns (apache#39464)

### Rationale for this change

Other `format:` such as `format: :csv` accepts custom options. `format: :json` should also accept them.

### What changes are included in this PR?

Use `Arrow::JSONReadOptions` for `Table::Load(format: :json)`.

### Are these changes tested?

Yes.

### Are there any user-facing changes?

Yes.
* Closes: apache#39433

Authored-by: Sutou Kouhei <kou@clear-code.com>
Signed-off-by: Sutou Kouhei <kou@clear-code.com>
  • Loading branch information
kou authored Jan 5, 2024
1 parent bec0385 commit 04d7984
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 1 deletion.
8 changes: 7 additions & 1 deletion ruby/red-arrow/lib/arrow/table-loader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -252,7 +252,13 @@ def load_as_feather

def load_as_json
open_input_stream do |input|
reader = JSONReader.new(input)
options = JSONReadOptions.new
@options.each do |key, value|
next if value.nil?
setter = :"#{key}="
options.__send__(setter, value) if options.respond_to?(setter)
end
reader = JSONReader.new(input, options)
table = reader.read
table.refer_input(input)
table
Expand Down
1 change: 1 addition & 0 deletions ruby/red-arrow/test/helper.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
require "arrow"

require "fiddle"
require "json"
require "pathname"
require "tempfile"
require "timeout"
Expand Down
25 changes: 25 additions & 0 deletions ruby/red-arrow/test/test-table.rb
Original file line number Diff line number Diff line change
Expand Up @@ -677,6 +677,31 @@ def test_tsv
format: :tsv,
schema: @table.schema))
end

def test_json
output = create_output(".json")
# TODO: Implement this.
# @table.save(output, format: :json)
columns = ""
@table.each_record.each do |record|
column = {
"count" => record.count,
"visible" => record.visible,
}
columns << column.to_json
columns << "\n"
end
if output.is_a?(String)
File.write(output, columns)
else
output.resize(columns.bytesize)
output.set_data(0, columns)
end
assert_equal(@table,
Arrow::Table.load(output,
format: :json,
schema: @table.schema))
end
end

sub_test_case("path") do
Expand Down

0 comments on commit 04d7984

Please sign in to comment.