| 1 | #pragma once |
| 2 | |
| 3 | #include <Core/Block.h> |
| 4 | #include <Processors/Formats/RowInputFormatWithDiagnosticInfo.h> |
| 5 | #include <Formats/FormatSettings.h> |
| 6 | #include <Formats/ParsedTemplateFormatString.h> |
| 7 | #include <IO/ReadHelpers.h> |
| 8 | #include <IO/PeekableReadBuffer.h> |
| 9 | |
| 10 | |
| 11 | namespace DB |
| 12 | { |
| 13 | |
| 14 | class TemplateRowInputFormat : public RowInputFormatWithDiagnosticInfo |
| 15 | { |
| 16 | using ColumnFormat = ParsedTemplateFormatString::ColumnFormat; |
| 17 | public: |
| 18 | TemplateRowInputFormat(const Block & , ReadBuffer & in_, const Params & params_, |
| 19 | FormatSettings settings_, bool ignore_spaces_, |
| 20 | ParsedTemplateFormatString format_, ParsedTemplateFormatString row_format_, |
| 21 | std::string row_between_delimiter); |
| 22 | |
| 23 | String getName() const override { return "TemplateRowInputFormat" ; } |
| 24 | |
| 25 | bool readRow(MutableColumns & columns, RowReadExtension & ) override; |
| 26 | |
| 27 | void readPrefix() override; |
| 28 | |
| 29 | bool allowSyncAfterError() const override; |
| 30 | void syncAfterError() override; |
| 31 | |
| 32 | void resetParser() override; |
| 33 | |
| 34 | private: |
| 35 | bool deserializeField(const DataTypePtr & type, IColumn & column, size_t file_column); |
| 36 | void skipField(ColumnFormat col_format); |
| 37 | inline void skipSpaces() { if (ignore_spaces) skipWhitespaceIfAny(buf); } |
| 38 | |
| 39 | template <typename ReturnType = void> |
| 40 | ReturnType tryReadPrefixOrSuffix(size_t & input_part_beg, size_t input_part_end); |
| 41 | bool checkForSuffix(); |
| 42 | [[noreturn]] void throwUnexpectedEof(); |
| 43 | |
| 44 | bool parseRowAndPrintDiagnosticInfo(MutableColumns & columns, WriteBuffer & out) override; |
| 45 | void tryDeserializeFiled(const DataTypePtr & type, IColumn & column, size_t file_column, ReadBuffer::Position & prev_pos, |
| 46 | ReadBuffer::Position & curr_pos) override; |
| 47 | bool isGarbageAfterField(size_t after_col_idx, ReadBuffer::Position pos) override; |
| 48 | void writeErrorStringForWrongDelimiter(WriteBuffer & out, const String & description, const String & delim); |
| 49 | |
| 50 | void skipToNextDelimiterOrEof(const String & delimiter); |
| 51 | |
| 52 | private: |
| 53 | PeekableReadBuffer buf; |
| 54 | DataTypes data_types; |
| 55 | |
| 56 | FormatSettings settings; |
| 57 | const bool ignore_spaces; |
| 58 | ParsedTemplateFormatString format; |
| 59 | ParsedTemplateFormatString row_format; |
| 60 | |
| 61 | size_t format_data_idx; |
| 62 | bool end_of_stream = false; |
| 63 | std::vector<size_t> always_default_columns; |
| 64 | char default_csv_delimiter; |
| 65 | |
| 66 | std::string row_between_delimiter; |
| 67 | }; |
| 68 | |
| 69 | } |
| 70 | |