MCPcopy Create free account
hub / github.com/apache/arrow / GetOrderedColumnNames

Function GetOrderedColumnNames

cpp/src/arrow/dataset/file_csv.cc:150–211  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

148};
149
150Result<std::vector<std::string>> GetOrderedColumnNames(
151 const csv::ReadOptions& read_options, const csv::ParseOptions& parse_options,
152 std::string_view first_block, MemoryPool* pool) {
153 // Skip BOM when reading column names (ARROW-14644, ARROW-17382)
154 auto size = first_block.length();
155 const uint8_t* data = reinterpret_cast<const uint8_t*>(first_block.data());
156 ARROW_ASSIGN_OR_RAISE(auto data_no_bom, util::SkipUTF8BOM(data, size));
157 size = size - static_cast<uint32_t>(data_no_bom - data);
158 first_block = std::string_view(reinterpret_cast<const char*>(data_no_bom), size);
159 if (!read_options.column_names.empty()) {
160 return read_options.column_names;
161 }
162
163 uint32_t parsed_size = 0;
164 int32_t max_num_rows = read_options.skip_rows + 1;
165 std::optional<csv::ParseOptions> inspection_parse_options;
166 const auto* parser_options = &parse_options;
167 if (parse_options.pad_short_rows || parse_options.ignore_extra_columns) {
168 // Do not adjust row widths while determining column names: padding cannot
169 // synthesize missing names, and ignoring extra columns may discard columns.
170 inspection_parse_options.emplace(parse_options);
171 inspection_parse_options->pad_short_rows = false;
172 inspection_parse_options->ignore_extra_columns = false;
173 parser_options = &*inspection_parse_options;
174 }
175 csv::BlockParser parser(pool, *parser_options, /*num_cols=*/-1, /*first_row=*/1,
176 max_num_rows);
177
178 RETURN_NOT_OK(parser.Parse(std::string_view{first_block}, &parsed_size));
179
180 if (parser.num_rows() != max_num_rows) {
181 return Status::Invalid("Could not read first ", max_num_rows,
182 " rows from CSV file, either file is truncated or"
183 " header is larger than block size");
184 }
185
186 if (parser.num_cols() == 0) {
187 return Status::Invalid("No columns in CSV file");
188 }
189
190 std::vector<std::string> column_names;
191
192 if (read_options.autogenerate_column_names) {
193 column_names.reserve(parser.num_cols());
194 for (int32_t i = 0; i < parser.num_cols(); ++i) {
195 std::stringstream ss;
196 ss << "f" << i;
197 column_names.emplace_back(ss.str());
198 }
199 return column_names;
200 }
201
202 RETURN_NOT_OK(parser.VisitLastRow(
203 [&](const uint8_t* data, uint32_t size, bool quoted, bool missing) -> Status {
204 DCHECK(!missing);
205 std::string_view view{reinterpret_cast<const char*>(data), size};
206 column_names.emplace_back(view);
207 return Status::OK();

Callers 2

GetColumnNamesFunction · 0.85
DoInspectFragmentFunction · 0.85

Calls 11

num_colsMethod · 0.80
emplace_backMethod · 0.80
strMethod · 0.80
InvalidFunction · 0.50
OKFunction · 0.50
lengthMethod · 0.45
dataMethod · 0.45
emptyMethod · 0.45
ParseMethod · 0.45
num_rowsMethod · 0.45
reserveMethod · 0.45

Tested by

no test coverage detected