-
Notifications
You must be signed in to change notification settings - Fork 7k
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
Format ODBCDriver2 with NULL support #2834
Merged
Merged
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
c9ca505
Format ODBCDriver2 with NULL support
proller 2da8140
Fix comment
proller 89729c3
Merge remote-tracking branch 'upstream/master' into fix19
proller e4a6c43
Update ODBCDriver2BlockOutputStream.cpp
alexey-milovidov fa2ceb7
clean
proller 24a4bdd
Merge remote-tracking branch 'origin/fix19' into fix19
proller 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
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 |
---|---|---|
@@ -0,0 +1,95 @@ | ||
#include <Core/Block.h> | ||
#include <Formats/FormatFactory.h> | ||
#include <Formats/ODBCDriver2BlockOutputStream.h> | ||
#include <IO/WriteBuffer.h> | ||
#include <IO/WriteHelpers.h> | ||
|
||
|
||
#include <Core/iostream_debug_helpers.h> | ||
|
||
|
||
namespace DB | ||
{ | ||
ODBCDriver2BlockOutputStream::ODBCDriver2BlockOutputStream( | ||
WriteBuffer & out_, const Block & header_, const FormatSettings & format_settings) | ||
: out(out_), header(header_), format_settings(format_settings) | ||
{ | ||
} | ||
|
||
void ODBCDriver2BlockOutputStream::flush() | ||
{ | ||
out.next(); | ||
} | ||
|
||
void writeODBCString(WriteBuffer & out, const std::string & str) | ||
{ | ||
writeIntBinary(Int32(str.size()), out); | ||
out.write(str.data(), str.size()); | ||
} | ||
|
||
void ODBCDriver2BlockOutputStream::write(const Block & block) | ||
{ | ||
const size_t rows = block.rows(); | ||
const size_t columns = block.columns(); | ||
String text_value; | ||
|
||
for (size_t i = 0; i < rows; ++i) | ||
{ | ||
for (size_t j = 0; j < columns; ++j) | ||
{ | ||
text_value.resize(0); | ||
const ColumnWithTypeAndName & col = block.getByPosition(j); | ||
|
||
if (col.column->isNullAt(i)) | ||
{ | ||
writeIntBinary(Int32(-1), out); | ||
} | ||
else | ||
{ | ||
{ | ||
WriteBufferFromString text_out(text_value); | ||
col.type->serializeText(*col.column, i, text_out, format_settings); | ||
} | ||
writeODBCString(out, text_value); | ||
} | ||
} | ||
} | ||
} | ||
|
||
void ODBCDriver2BlockOutputStream::writePrefix() | ||
{ | ||
const size_t columns = header.columns(); | ||
|
||
/// Number of header rows. | ||
writeIntBinary(Int32(2), out); | ||
|
||
/// Names of columns. | ||
/// Number of columns + 1 for first name column. | ||
writeIntBinary(Int32(columns + 1), out); | ||
writeODBCString(out, "name"); | ||
for (size_t i = 0; i < columns; ++i) | ||
{ | ||
const ColumnWithTypeAndName & col = header.getByPosition(i); | ||
writeODBCString(out, col.name); | ||
} | ||
|
||
/// Types of columns. | ||
writeIntBinary(Int32(columns + 1), out); | ||
writeODBCString(out, "type"); | ||
for (size_t i = 0; i < columns; ++i) | ||
{ | ||
const ColumnWithTypeAndName & col = header.getByPosition(i); | ||
writeODBCString(out, col.type->getName()); | ||
} | ||
} | ||
|
||
|
||
void registerOutputFormatODBCDriver2(FormatFactory & factory) | ||
{ | ||
factory.registerOutputFormat( | ||
"ODBCDriver2", [](WriteBuffer & buf, const Block & sample, const Context &, const FormatSettings & format_settings) { | ||
return std::make_shared<ODBCDriver2BlockOutputStream>(buf, sample, format_settings); | ||
}); | ||
} | ||
|
||
} |
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,44 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <Core/Block.h> | ||
#include <DataStreams/IBlockOutputStream.h> | ||
#include <Formats/FormatSettings.h> | ||
|
||
|
||
namespace DB | ||
{ | ||
class WriteBuffer; | ||
|
||
|
||
/** A data format designed to simplify the implementation of the ODBC driver. | ||
* ODBC driver is designed to be build for different platforms without dependencies from the main code, | ||
* so the format is made that way so that it can be as easy as possible to parse it. | ||
* A header is displayed with the required information. | ||
* The data is then output in the order of the rows. Each value is displayed as follows: length in Int32 format (-1 for NULL), then data in text form. | ||
*/ | ||
class ODBCDriver2BlockOutputStream : public IBlockOutputStream | ||
{ | ||
public: | ||
ODBCDriver2BlockOutputStream(WriteBuffer & out_, const Block & header_, const FormatSettings & format_settings); | ||
|
||
Block getHeader() const override | ||
{ | ||
return header; | ||
} | ||
void write(const Block & block) override; | ||
void writePrefix() override; | ||
|
||
void flush() override; | ||
std::string getContentType() const override | ||
{ | ||
return "application/octet-stream"; | ||
} | ||
|
||
private: | ||
WriteBuffer & out; | ||
const Block header; | ||
const FormatSettings format_settings; | ||
}; | ||
|
||
} |
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.
I cannot understand the motivation behind this format.
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.
expandable header without breaking format - we can send any column flags, for example length for display_size or something else