GH-48680: [GLib][Ruby] Add CSVWriter#48681
Conversation
|
|
c_glib/arrow-glib/writer.cpp
Outdated
| spec = g_param_spec_string("null-string", | ||
| "Null string", | ||
| "The string to write for null values", | ||
| "", |
There was a problem hiding this comment.
| "", | |
| write_options.null_string.c_str(), |
There was a problem hiding this comment.
I always find this difficult in C++, so I wonder, is that safe? The data for the string will be owned by the write_options variable, right? And when that goes out of scope, the string could potentially be gone? I guess it works because it is a constant somewhere.
There was a problem hiding this comment.
Ah. g_param_spec_string()'s documentation https://docs.gtk.org/gobject/func.param_spec_string.html#parameters says "The data is owned by the caller of the function." but it's copied in the function: https://gitlab.gnome.org/GNOME/glib/-/blob/main/gobject/gparamspecs.c?ref_type=heads#L2438
So, we can use c_str() here.
c_glib/arrow-glib/writer.cpp
Outdated
| spec = g_param_spec_string("eol", | ||
| "EOL", | ||
| "The end of line character to use for ending rows", | ||
| "\n", |
There was a problem hiding this comment.
| "\n", | |
| write_options.eol.c_str(), |
|
|
||
| def test_delimiter | ||
| assert_equal(44, @options.delimiter) # 44 is the ASCII code for comma | ||
| @options.delimiter = ";".ord |
There was a problem hiding this comment.
We can use String, right?
| @options.delimiter = ";".ord | |
| @options.delimiter = ";" |
There was a problem hiding this comment.
No, this is implemented in the Ruby wrapper. It can't be used in the tests for the GLib wrapper, right?
| options = Arrow::CSVWriteOptions.new | ||
| options.include_header = false | ||
| options.delimiter = ";".ord | ||
| options.quoting_style = Arrow::CSVQuotingStyle::NONE | ||
| options.null_string = "NULL" |
There was a problem hiding this comment.
We can use
options = {
include_header: false,
delimiter: ";",
quoting_style: :none,
null_string: "NULL,
}by defining Arrow::CSVWriteOptions.try_convert:
module Arrow
class CSVWriteOptions
class << self
def try_convert(value)
case value
when Hash
options = new
value.each do |k, v|
options.public_send("#{k}=", value)
end
options
else
nil
end
end
end
end
endThere was a problem hiding this comment.
This is good idea! I assume you meant to comment on the test for the Ruby wrapper, not GLib though.
There was a problem hiding this comment.
Oh, sorry. You're right. I confused the GLib layer with the Ruby layer...
Rationale for this change
Using Arrow for writing CSVs could potentially give a lot better performance than using Ruby, but the CSV writer is not available.
What changes are included in this PR?
This adds a
CSVWriterand options class to the GLib wrapper.Are these changes tested?
Yes, with Ruby unit tests.
Are there any user-facing changes?
Yes, new classes.