2021-10-03 18:07:04 +08:00
|
|
|
// Copyright 2021 Memgraph Ltd.
|
|
|
|
//
|
|
|
|
// Use of this software is governed by the Business Source License
|
|
|
|
// included in the file licenses/BSL.txt; by using this file, you agree to be bound by the terms of the Business Source
|
|
|
|
// License, and you may not use this file except in compliance with the Business Source License.
|
|
|
|
//
|
|
|
|
// As of the Change Date specified in that file, in accordance with
|
|
|
|
// the Business Source License, use of this software will be governed
|
|
|
|
// by the Apache License, Version 2.0, included in the file
|
|
|
|
// licenses/APL.txt.
|
|
|
|
|
2021-03-02 02:47:41 +08:00
|
|
|
#include "utils/csv_parsing.hpp"
|
|
|
|
|
|
|
|
#include <string_view>
|
|
|
|
|
|
|
|
#include "utils/file.hpp"
|
|
|
|
#include "utils/string.hpp"
|
|
|
|
|
|
|
|
namespace csv {
|
|
|
|
|
|
|
|
using ParseError = Reader::ParseError;
|
|
|
|
|
|
|
|
void Reader::InitializeStream() {
|
|
|
|
if (!std::filesystem::exists(path_)) {
|
|
|
|
throw CsvReadException("CSV file not found: {}", path_.string());
|
|
|
|
}
|
|
|
|
csv_stream_.open(path_);
|
|
|
|
if (!csv_stream_.good()) {
|
|
|
|
throw CsvReadException("CSV file {} couldn't be opened!", path_.string());
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-24 18:02:55 +08:00
|
|
|
std::optional<utils::pmr::string> Reader::GetNextLine(utils::MemoryResource *mem) {
|
|
|
|
utils::pmr::string line(mem);
|
2021-03-02 02:47:41 +08:00
|
|
|
if (!std::getline(csv_stream_, line)) {
|
|
|
|
// reached end of file or an I/0 error occurred
|
|
|
|
if (!csv_stream_.good()) {
|
|
|
|
csv_stream_.close();
|
|
|
|
}
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
|
|
|
++line_count_;
|
|
|
|
return line;
|
|
|
|
}
|
|
|
|
|
2021-03-19 00:24:25 +08:00
|
|
|
Reader::ParsingResult Reader::ParseHeader() {
|
2021-03-02 02:47:41 +08:00
|
|
|
// header must be the very first line in the file
|
2021-12-07 18:05:33 +08:00
|
|
|
MG_ASSERT(line_count_ == 1, "Invalid use of {}", __func__);
|
2021-03-24 18:02:55 +08:00
|
|
|
return ParseRow(memory_);
|
2021-03-19 00:24:25 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void Reader::TryInitializeHeader() {
|
|
|
|
if (!HasHeader()) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
auto header = ParseHeader();
|
|
|
|
if (header.HasError()) {
|
|
|
|
throw CsvReadException("CSV reading : {}", header.GetError().message);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (header->empty()) {
|
2021-03-02 02:47:41 +08:00
|
|
|
throw CsvReadException("CSV file {} empty!", path_);
|
|
|
|
}
|
2021-03-19 00:24:25 +08:00
|
|
|
|
|
|
|
number_of_columns_ = header->size();
|
2021-03-24 18:02:55 +08:00
|
|
|
header_ = std::move(*header);
|
2021-03-02 02:47:41 +08:00
|
|
|
}
|
|
|
|
|
2021-03-19 00:24:25 +08:00
|
|
|
[[nodiscard]] bool Reader::HasHeader() const { return read_config_.with_header; }
|
|
|
|
|
2021-03-24 18:02:55 +08:00
|
|
|
const Reader::Header &Reader::GetHeader() const { return header_; }
|
2021-03-19 00:24:25 +08:00
|
|
|
|
2021-03-02 02:47:41 +08:00
|
|
|
namespace {
|
2021-03-27 16:47:41 +08:00
|
|
|
enum class CsvParserState : uint8_t { INITIAL_FIELD, NEXT_FIELD, QUOTING, EXPECT_DELIMITER, DONE };
|
2021-03-02 02:47:41 +08:00
|
|
|
|
2021-03-19 21:40:09 +08:00
|
|
|
} // namespace
|
2021-03-02 02:47:41 +08:00
|
|
|
|
2021-03-24 18:02:55 +08:00
|
|
|
Reader::ParsingResult Reader::ParseRow(utils::MemoryResource *mem) {
|
|
|
|
utils::pmr::vector<utils::pmr::string> row(mem);
|
2021-03-19 21:40:09 +08:00
|
|
|
if (number_of_columns_ != 0) {
|
|
|
|
row.reserve(number_of_columns_);
|
|
|
|
}
|
|
|
|
|
2021-03-19 00:24:25 +08:00
|
|
|
utils::pmr::string column(memory_);
|
2021-03-02 02:47:41 +08:00
|
|
|
|
|
|
|
auto state = CsvParserState::INITIAL_FIELD;
|
|
|
|
|
|
|
|
do {
|
2021-03-24 18:02:55 +08:00
|
|
|
const auto maybe_line = GetNextLine(mem);
|
2021-03-02 02:47:41 +08:00
|
|
|
if (!maybe_line) {
|
|
|
|
// The whole file was processed.
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2021-03-19 21:40:09 +08:00
|
|
|
std::string_view line_string_view = *maybe_line;
|
|
|
|
|
2021-05-18 19:44:29 +08:00
|
|
|
// remove '\r' from the end in case we have dos file format
|
|
|
|
if (line_string_view.back() == '\r') {
|
|
|
|
line_string_view.remove_suffix(1);
|
|
|
|
}
|
|
|
|
|
2021-03-27 16:47:41 +08:00
|
|
|
while (state != CsvParserState::DONE && !line_string_view.empty()) {
|
2021-03-19 21:40:09 +08:00
|
|
|
const auto c = line_string_view[0];
|
2021-03-02 02:47:41 +08:00
|
|
|
|
|
|
|
// Line feeds and carriage returns are ignored in CSVs.
|
2021-03-19 21:40:09 +08:00
|
|
|
if (c == '\n' || c == '\r') {
|
|
|
|
line_string_view.remove_prefix(1);
|
|
|
|
continue;
|
|
|
|
}
|
2021-03-02 02:47:41 +08:00
|
|
|
// Null bytes aren't allowed in CSVs.
|
|
|
|
if (c == '\0') {
|
|
|
|
return ParseError(ParseError::ErrorCode::NULL_BYTE,
|
2021-03-19 00:24:25 +08:00
|
|
|
fmt::format("CSV: Line {:d} contains NULL byte", line_count_ - 1));
|
2021-03-02 02:47:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
switch (state) {
|
|
|
|
case CsvParserState::INITIAL_FIELD:
|
|
|
|
case CsvParserState::NEXT_FIELD: {
|
2021-03-19 21:40:09 +08:00
|
|
|
if (utils::StartsWith(line_string_view, *read_config_.quote)) {
|
2021-03-02 02:47:41 +08:00
|
|
|
// The current field is a quoted field.
|
|
|
|
state = CsvParserState::QUOTING;
|
2021-03-19 21:40:09 +08:00
|
|
|
line_string_view.remove_prefix(read_config_.quote->size());
|
|
|
|
} else if (utils::StartsWith(line_string_view, *read_config_.delimiter)) {
|
2021-03-02 02:47:41 +08:00
|
|
|
// The current field has an empty value.
|
|
|
|
row.emplace_back("");
|
|
|
|
state = CsvParserState::NEXT_FIELD;
|
2021-03-19 21:40:09 +08:00
|
|
|
line_string_view.remove_prefix(read_config_.delimiter->size());
|
2021-03-02 02:47:41 +08:00
|
|
|
} else {
|
|
|
|
// The current field is a regular field.
|
2021-03-19 21:40:09 +08:00
|
|
|
const auto delimiter_idx = line_string_view.find(*read_config_.delimiter);
|
|
|
|
row.emplace_back(line_string_view.substr(0, delimiter_idx));
|
|
|
|
if (delimiter_idx == std::string_view::npos) {
|
2021-03-27 16:47:41 +08:00
|
|
|
state = CsvParserState::DONE;
|
2021-03-19 21:40:09 +08:00
|
|
|
} else {
|
|
|
|
line_string_view.remove_prefix(delimiter_idx + read_config_.delimiter->size());
|
2021-03-27 16:47:41 +08:00
|
|
|
state = CsvParserState::NEXT_FIELD;
|
2021-03-19 21:40:09 +08:00
|
|
|
}
|
2021-03-02 02:47:41 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CsvParserState::QUOTING: {
|
2021-03-19 21:40:09 +08:00
|
|
|
const auto quote_now = utils::StartsWith(line_string_view, *read_config_.quote);
|
|
|
|
const auto quote_next =
|
|
|
|
utils::StartsWith(line_string_view.substr(read_config_.quote->size()), *read_config_.quote);
|
2021-03-02 02:47:41 +08:00
|
|
|
if (quote_now && quote_next) {
|
|
|
|
// This is an escaped quote character.
|
2021-03-19 00:24:25 +08:00
|
|
|
column += *read_config_.quote;
|
2021-03-19 21:40:09 +08:00
|
|
|
line_string_view.remove_prefix(read_config_.quote->size() * 2);
|
|
|
|
} else if (quote_now) {
|
2021-03-02 02:47:41 +08:00
|
|
|
// This is the end of the quoted field.
|
|
|
|
row.emplace_back(std::move(column));
|
2021-03-19 21:40:09 +08:00
|
|
|
column.clear();
|
2021-03-02 02:47:41 +08:00
|
|
|
state = CsvParserState::EXPECT_DELIMITER;
|
2021-03-19 21:40:09 +08:00
|
|
|
line_string_view.remove_prefix(read_config_.quote->size());
|
2021-03-02 02:47:41 +08:00
|
|
|
} else {
|
|
|
|
column.push_back(c);
|
2021-03-19 21:40:09 +08:00
|
|
|
line_string_view.remove_prefix(1);
|
2021-03-02 02:47:41 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case CsvParserState::EXPECT_DELIMITER: {
|
2021-03-19 21:40:09 +08:00
|
|
|
if (utils::StartsWith(line_string_view, *read_config_.delimiter)) {
|
2021-03-02 02:47:41 +08:00
|
|
|
state = CsvParserState::NEXT_FIELD;
|
2021-03-19 21:40:09 +08:00
|
|
|
line_string_view.remove_prefix(read_config_.delimiter->size());
|
2021-03-02 02:47:41 +08:00
|
|
|
} else {
|
|
|
|
return ParseError(ParseError::ErrorCode::UNEXPECTED_TOKEN,
|
2021-03-19 00:24:25 +08:00
|
|
|
fmt::format("CSV Reader: Expected '{}' after '{}', but got '{}' at line {:d}",
|
|
|
|
*read_config_.delimiter, *read_config_.quote, c, line_count_ - 1));
|
2021-03-02 02:47:41 +08:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2021-03-27 16:47:41 +08:00
|
|
|
case CsvParserState::DONE: {
|
|
|
|
LOG_FATAL("Invalid state of the CSV parser!");
|
|
|
|
}
|
2021-03-02 02:47:41 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} while (state == CsvParserState::QUOTING);
|
|
|
|
|
|
|
|
switch (state) {
|
2021-03-19 21:40:09 +08:00
|
|
|
case CsvParserState::INITIAL_FIELD:
|
2021-03-27 16:47:41 +08:00
|
|
|
case CsvParserState::DONE:
|
2021-03-19 21:40:09 +08:00
|
|
|
case CsvParserState::EXPECT_DELIMITER:
|
2021-03-02 02:47:41 +08:00
|
|
|
break;
|
2021-03-27 16:47:41 +08:00
|
|
|
case CsvParserState::NEXT_FIELD:
|
|
|
|
row.emplace_back("");
|
|
|
|
break;
|
2021-03-02 02:47:41 +08:00
|
|
|
case CsvParserState::QUOTING: {
|
|
|
|
return ParseError(ParseError::ErrorCode::NO_CLOSING_QUOTE,
|
|
|
|
"There is no more data left to load while inside a quoted string. "
|
|
|
|
"Did you forget to close the quote?");
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// reached the end of file - return empty row
|
|
|
|
if (row.empty()) {
|
2021-03-19 00:24:25 +08:00
|
|
|
return row;
|
2021-03-02 02:47:41 +08:00
|
|
|
}
|
|
|
|
|
2021-03-19 00:24:25 +08:00
|
|
|
// Has header, but the header has already been read and the number_of_columns_
|
|
|
|
// is already set. Otherwise, we would get an error every time we'd try to
|
|
|
|
// parse the header.
|
|
|
|
// Also, if we don't have a header, the 'number_of_columns_' will be 0, so no
|
|
|
|
// need to check the number of columns.
|
2021-11-12 16:09:15 +08:00
|
|
|
if (number_of_columns_ != 0 && row.size() != number_of_columns_) [[unlikely]] {
|
2021-03-19 00:24:25 +08:00
|
|
|
return ParseError(ParseError::ErrorCode::BAD_NUM_OF_COLUMNS,
|
|
|
|
// ToDo(the-joksim):
|
|
|
|
// - 'line_count_ - 1' is the last line of a row (as a
|
|
|
|
// row may span several lines) ==> should have a row
|
|
|
|
// counter
|
|
|
|
fmt::format("Expected {:d} columns in row {:d}, but got {:d}", number_of_columns_,
|
|
|
|
line_count_ - 1, row.size()));
|
2021-03-02 02:47:41 +08:00
|
|
|
}
|
|
|
|
|
2021-03-19 21:40:09 +08:00
|
|
|
return std::move(row);
|
2021-03-02 02:47:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
// Returns Reader::Row if the read row if valid;
|
|
|
|
// Returns std::nullopt if end of file is reached or an error occurred
|
|
|
|
// making it unreadable;
|
2021-03-19 00:24:25 +08:00
|
|
|
// @throws CsvReadException if a bad row is encountered, and the ignore_bad is set
|
2021-03-02 02:47:41 +08:00
|
|
|
// to 'true' in the Reader::Config.
|
2021-03-24 18:02:55 +08:00
|
|
|
std::optional<Reader::Row> Reader::GetNextRow(utils::MemoryResource *mem) {
|
|
|
|
auto row = ParseRow(mem);
|
2021-03-02 02:47:41 +08:00
|
|
|
|
|
|
|
if (row.HasError()) {
|
2021-03-19 00:24:25 +08:00
|
|
|
if (!read_config_.ignore_bad) {
|
|
|
|
throw CsvReadException("CSV Reader: Bad row at line {:d}: {}", line_count_ - 1, row.GetError().message);
|
2021-03-02 02:47:41 +08:00
|
|
|
}
|
|
|
|
// try to parse as many times as necessary to reach a valid row
|
|
|
|
do {
|
2021-03-19 00:24:25 +08:00
|
|
|
spdlog::debug("CSV Reader: Bad row at line {:d}: {}", line_count_ - 1, row.GetError().message);
|
2021-03-02 02:47:41 +08:00
|
|
|
if (!csv_stream_.good()) {
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2021-03-24 18:02:55 +08:00
|
|
|
row = ParseRow(mem);
|
2021-03-02 02:47:41 +08:00
|
|
|
} while (row.HasError());
|
|
|
|
}
|
|
|
|
|
2021-03-19 00:24:25 +08:00
|
|
|
if (row->empty()) {
|
2021-03-02 02:47:41 +08:00
|
|
|
// reached end of file
|
|
|
|
return std::nullopt;
|
|
|
|
}
|
2021-03-19 21:40:09 +08:00
|
|
|
return std::move(*row);
|
2021-03-02 02:47:41 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace csv
|