Merge pull request #29123 from depressed-pho/csv-custom-null

This commit is contained in:
Vladimir C 2021-09-21 11:35:21 +03:00 committed by GitHub
commit 9abd8aea6d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 62 additions and 2 deletions

View File

@ -2833,6 +2833,43 @@ Possible values:
Default value: `1`.
## output_format_csv_null_representation {#output_format_csv_null_representation}
Defines the representation of `NULL` for [CSV](../../interfaces/formats.md#csv) output format. User can set any string as a value, for example, `My NULL`.
Default value: `\N`.
**Examples**
Query
```sql
SELECT * from csv_custom_null FORMAT CSV;
```
Result
```text
788
\N
\N
```
Query
```sql
SET output_format_csv_null_representation = 'My NULL';
SELECT * FROM csv_custom_null FORMAT CSV;
```
Result
```text
788
My NULL
My NULL
```
## output_format_tsv_null_representation {#output_format_tsv_null_representation}
Defines the representation of `NULL` for [TSV](../../interfaces/formats.md#tabseparated) output format. User can set any string as a value, for example, `My NULL`.

View File

@ -577,6 +577,7 @@ class IColumn;
M(String, output_format_avro_codec, "", "Compression codec used for output. Possible values: 'null', 'deflate', 'snappy'.", 0) \
M(UInt64, output_format_avro_sync_interval, 16 * 1024, "Sync interval in bytes.", 0) \
M(Bool, output_format_tsv_crlf_end_of_line, false, "If it is set true, end of line in TSV format will be \\r\\n instead of \\n.", 0) \
M(String, output_format_csv_null_representation, "\\N", "Custom NULL representation in CSV format", 0) \
M(String, output_format_tsv_null_representation, "\\N", "Custom NULL representation in TSV format", 0) \
M(Bool, output_format_decimal_trailing_zeros, false, "Output trailing zeros when printing Decimal values. E.g. 1.230000 instead of 1.23.", 0) \
\

View File

@ -327,7 +327,7 @@ void SerializationNullable::serializeTextCSV(const IColumn & column, size_t row_
const ColumnNullable & col = assert_cast<const ColumnNullable &>(column);
if (col.isNullAt(row_num))
writeCString("\\N", ostr);
writeString(settings.csv.null_representation, ostr);
else
nested->serializeTextCSV(col.getNestedColumn(), row_num, ostr, settings);
}

View File

@ -60,6 +60,7 @@ FormatSettings getFormatSettings(ContextPtr context, const Settings & settings)
format_settings.csv.delimiter = settings.format_csv_delimiter;
format_settings.csv.empty_as_default = settings.input_format_defaults_for_omitted_fields;
format_settings.csv.input_format_enum_as_number = settings.input_format_csv_enum_as_number;
format_settings.csv.null_representation = settings.output_format_csv_null_representation;
format_settings.csv.unquoted_null_literal_as_null = settings.input_format_csv_unquoted_null_literal_as_null;
format_settings.csv.input_format_arrays_as_nested_csv = settings.input_format_csv_arrays_as_nested_csv;
format_settings.custom.escaping_rule = settings.format_custom_escaping_rule;

View File

@ -76,6 +76,7 @@ struct FormatSettings
bool crlf_end_of_line = false;
bool input_format_enum_as_number = false;
bool input_format_arrays_as_nested_csv = false;
String null_representation = "\\N";
} csv;
struct Custom

View File

@ -436,7 +436,7 @@ void MaterializedPostgreSQLConsumer::processReplicationMessage(const char * repl
if (new_relation_definition)
{
current_schema_data.column_identifiers.emplace_back(std::make_tuple(data_type_id, type_modifier));
current_schema_data.column_identifiers.emplace_back(std::make_pair(data_type_id, type_modifier));
}
else
{

View File

@ -0,0 +1,4 @@
# output_format_csv_null_representation should initially be \\N
"val1",\N,"val3"
# Changing output_format_csv_null_representation
"val1",∅,"val3"

View File

@ -0,0 +1,16 @@
DROP TABLE IF EXISTS test_data;
CREATE TABLE test_data (
col1 Nullable(String),
col2 Nullable(String),
col3 Nullable(String)
) ENGINE = Memory;
INSERT INTO test_data VALUES ('val1', NULL, 'val3');
SELECT '# output_format_csv_null_representation should initially be \\N';
SELECT * FROM test_data FORMAT CSV;
SELECT '# Changing output_format_csv_null_representation';
SET output_format_csv_null_representation = '';
SELECT * FROM test_data FORMAT CSV;
SET output_format_csv_null_representation = '\\N';