Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
29 changes: 28 additions & 1 deletion c_glib/arrow-glib/reader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -904,7 +904,8 @@ enum {
PROP_ALLOW_NEWLINES_IN_VALUES,
PROP_IGNORE_EMPTY_LINES,
PROP_CHECK_UTF8,
PROP_ALLOW_NULL_STRINGS
PROP_ALLOW_NULL_STRINGS,
PROP_GENERATE_COLUMN_NAMES
};

G_DEFINE_TYPE_WITH_PRIVATE(GArrowCSVReadOptions,
Expand Down Expand Up @@ -934,6 +935,9 @@ garrow_csv_read_options_set_property(GObject *object,
case PROP_N_SKIP_ROWS:
priv->read_options.skip_rows = g_value_get_uint(value);
break;
case PROP_GENERATE_COLUMN_NAMES:
priv->read_options.autogenerate_column_names = g_value_get_boolean(value);
break;
case PROP_DELIMITER:
priv->parse_options.delimiter = g_value_get_schar(value);
break;
Expand Down Expand Up @@ -988,6 +992,9 @@ garrow_csv_read_options_get_property(GObject *object,
case PROP_N_SKIP_ROWS:
g_value_set_uint(value, priv->read_options.skip_rows);
break;
case PROP_GENERATE_COLUMN_NAMES:
g_value_set_boolean(value, priv->read_options.autogenerate_column_names);
break;
case PROP_DELIMITER:
g_value_set_schar(value, priv->parse_options.delimiter);
break;
Expand Down Expand Up @@ -1096,6 +1103,26 @@ garrow_csv_read_options_class_init(GArrowCSVReadOptionsClass *klass)
static_cast<GParamFlags>(G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_N_SKIP_ROWS, spec);

/**
* GArrowCSVReadOptions:generate_column_names:
*
* Whether to autogenerate column names if #GArrowCSVReadOptions:column-names is empty.
* If %TRUE, column names will be of the form 'f0', 'f1'...
* If %FALSE, column names will be read from the first CSV row
* after #GArrowCSVReadOptions:n-skip-rows.
*
* Since: 0.15.0
*/
spec = g_param_spec_boolean("generate-column-names",
"Generate column names",
"Whether to autogenerate column names if column-names is empty. "
"If TRUE, column names will be of the form 'f0', 'f1'... "
"If FALSE, column names will be read from the first CSV row "
"after n-skip-rows",
read_options.autogenerate_column_names,
static_cast<GParamFlags>(G_PARAM_READWRITE));
g_object_class_install_property(gobject_class, PROP_GENERATE_COLUMN_NAMES, spec);


auto parse_options = arrow::csv::ParseOptions::Defaults();

Expand Down
17 changes: 17 additions & 0 deletions c_glib/test/test-csv-reader.rb
Original file line number Diff line number Diff line change
Expand Up @@ -220,6 +220,23 @@ def test_add_column_name
options.add_column_name("score")
assert_equal(column_names + ["score"], options.column_names)
end

def test_generate_column_names
options = Arrow::CSVReadOptions.new
options.generate_column_names = true

table = Arrow::CSVReader.new(open_input(<<-CSV), options)
"Start",2
"Shutdown",9
"Reboot",5
CSV
columns = {
"f0" => build_string_array(["Start", "Shutdown", "Reboot"]),
"f1" => build_int64_array([2, 9, 5]),
}
assert_equal(build_table(columns),
table.read)
end
end
end
end