-
Notifications
You must be signed in to change notification settings - Fork 90
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
reader/csv: sniff column name and type #2116
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
name,age:UINT64,height:INT64 | ||
Alice,12,144 | ||
Bob,30,510 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,27 +11,28 @@ | |
namespace kuzu { | ||
namespace processor { | ||
|
||
enum class ParserMode : uint8_t { | ||
PARSING = 0, | ||
PARSING_HEADER = 1, | ||
SNIFFING_DIALECT = 2, | ||
INVALID = 255 | ||
}; | ||
|
||
class BaseCSVReader { | ||
friend class ParsingDriver; | ||
|
||
public: | ||
BaseCSVReader(const std::string& filePath, const common::ReaderConfig& readerConfig); | ||
|
||
virtual ~BaseCSVReader(); | ||
|
||
uint64_t parseBlock(common::block_idx_t blockIdx, common::DataChunk& resultChunk); | ||
virtual uint64_t parseBlock(common::block_idx_t blockIdx, common::DataChunk& resultChunk) = 0; | ||
|
||
uint64_t countRows(); | ||
|
||
protected: | ||
void addValue(common::DataChunk&, std::string_view, common::column_id_t columnIdx, | ||
template<typename Driver> | ||
void addValue(Driver&, uint64_t rowNum, common::column_id_t columnIdx, std::string_view strVal, | ||
std::vector<uint64_t>& escapePositions); | ||
void addRow(common::DataChunk&, common::column_id_t column); | ||
|
||
template<typename Driver> | ||
bool addRow(Driver&, uint64_t rowNum, common::column_id_t column_count); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Add a variable name for Driver& |
||
|
||
//! Read BOM and header. | ||
void handleFirstBlock(); | ||
|
||
//! If this finds a BOM, it advances `position`. | ||
void readBOM(); | ||
|
@@ -49,7 +50,8 @@ class BaseCSVReader { | |
return position < bufferSize || readBuffer(start); | ||
} | ||
|
||
uint64_t parseCSV(common::DataChunk& resultChunk); | ||
template<typename Driver> | ||
uint64_t parseCSV(Driver&); | ||
|
||
inline bool isNewLine(char c) { return c == '\n' || c == '\r'; } | ||
|
||
|
@@ -58,19 +60,8 @@ class BaseCSVReader { | |
uint64_t getLineNumber(); | ||
|
||
protected: | ||
//! Called when starting the parsing of a new block. | ||
virtual void parseBlockHook() = 0; | ||
virtual bool finishedBlockDetail() const = 0; | ||
virtual void handleQuotedNewline() = 0; | ||
|
||
private: | ||
void copyStringToVector(common::ValueVector*, std::string_view); | ||
//! Called after a row is finished to determine if we should keep processing. | ||
inline bool finishedBlock() { | ||
return mode != ParserMode::PARSING || rowToAdd >= common::DEFAULT_VECTOR_CAPACITY || | ||
finishedBlockDetail(); | ||
} | ||
|
||
protected: | ||
std::string filePath; | ||
common::CSVReaderConfig csvReaderConfig; | ||
|
@@ -86,10 +77,6 @@ class BaseCSVReader { | |
uint64_t position; | ||
|
||
bool rowEmpty = false; | ||
|
||
ParserMode mode; | ||
|
||
uint64_t rowToAdd; | ||
}; | ||
|
||
} // namespace processor | ||
|
73 changes: 73 additions & 0 deletions
73
src/include/processor/operator/persistent/reader/csv/driver.h
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
#pragma once | ||
|
||
#include <cstdint> | ||
|
||
#include "common/copier_config/copier_config.h" | ||
#include "common/data_chunk/data_chunk.h" | ||
|
||
namespace kuzu { | ||
namespace processor { | ||
|
||
// TODO(Keenan): Split up this file. | ||
class BaseCSVReader; | ||
class ParsingDriver { | ||
common::DataChunk& chunk; | ||
bool rowEmpty; | ||
|
||
public: | ||
ParsingDriver(common::DataChunk& chunk); | ||
|
||
bool done(uint64_t rowNum); | ||
void addValue(uint64_t rowNum, common::column_id_t columnIdx, std::string_view value); | ||
bool addRow(uint64_t rowNum, common::column_id_t columnCount); | ||
|
||
private: | ||
virtual bool doneEarly() = 0; | ||
virtual BaseCSVReader* getReader() = 0; | ||
}; | ||
|
||
class ParallelCSVReader; | ||
|
||
class ParallelParsingDriver : public ParsingDriver { | ||
ParallelCSVReader* reader; | ||
|
||
public: | ||
ParallelParsingDriver(common::DataChunk& chunk, ParallelCSVReader* reader); | ||
bool doneEarly() override; | ||
|
||
private: | ||
BaseCSVReader* getReader() override; | ||
}; | ||
|
||
class SerialCSVReader; | ||
|
||
class SerialParsingDriver : public ParsingDriver { | ||
SerialCSVReader* reader; | ||
|
||
public: | ||
SerialParsingDriver(common::DataChunk& chunk, SerialCSVReader* reader); | ||
|
||
bool doneEarly() override; | ||
|
||
private: | ||
BaseCSVReader* getReader() override; | ||
}; | ||
|
||
struct SniffCSVNameAndTypeDriver { | ||
std::vector<std::pair<std::string, common::LogicalType>> columns; | ||
|
||
bool done(uint64_t rowNum); | ||
void addValue(uint64_t rowNum, common::column_id_t columnIdx, std::string_view value); | ||
bool addRow(uint64_t rowNum, common::column_id_t columntCount); | ||
}; | ||
|
||
struct SniffCSVColumnCountDriver { | ||
uint64_t numColumns = 0; | ||
|
||
bool done(uint64_t rowNum); | ||
void addValue(uint64_t rowNum, common::column_id_t columnIdx, std::string_view value); | ||
bool addRow(uint64_t rowNum, common::column_id_t columntCount); | ||
}; | ||
|
||
} // namespace processor | ||
} // namespace kuzu |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You don't need to create a variable for columnTypeID since it is only used once. Just inline the call