1#pragma once
2
3#include <DataTypes/IDataType.h>
4
5namespace DB
6{
7
8/// A nullable data type is an ordinary data type provided with a tag
9/// indicating that it also contains the NULL value. The following class
10/// embodies this concept.
11class DataTypeNullable final : public IDataType
12{
13public:
14 static constexpr bool is_parametric = true;
15
16 explicit DataTypeNullable(const DataTypePtr & nested_data_type_);
17 std::string doGetName() const override { return "Nullable(" + nested_data_type->getName() + ")"; }
18 const char * getFamilyName() const override { return "Nullable"; }
19 TypeIndex getTypeId() const override { return TypeIndex::Nullable; }
20
21 void enumerateStreams(const StreamCallback & callback, SubstreamPath & path) const override;
22
23 void serializeBinaryBulkStatePrefix(
24 SerializeBinaryBulkSettings & settings,
25 SerializeBinaryBulkStatePtr & state) const override;
26
27 void serializeBinaryBulkStateSuffix(
28 SerializeBinaryBulkSettings & settings,
29 SerializeBinaryBulkStatePtr & state) const override;
30
31 void deserializeBinaryBulkStatePrefix(
32 DeserializeBinaryBulkSettings & settings,
33 DeserializeBinaryBulkStatePtr & state) const override;
34
35 void serializeBinaryBulkWithMultipleStreams(
36 const IColumn & column,
37 size_t offset,
38 size_t limit,
39 SerializeBinaryBulkSettings & settings,
40 SerializeBinaryBulkStatePtr & state) const override;
41
42 void deserializeBinaryBulkWithMultipleStreams(
43 IColumn & column,
44 size_t limit,
45 DeserializeBinaryBulkSettings & settings,
46 DeserializeBinaryBulkStatePtr & state) const override;
47
48 void serializeBinary(const Field & field, WriteBuffer & ostr) const override;
49 void deserializeBinary(Field & field, ReadBuffer & istr) const override;
50 void serializeBinary(const IColumn & column, size_t row_num, WriteBuffer & ostr) const override;
51 void deserializeBinary(IColumn & column, ReadBuffer & istr) const override;
52 void serializeTextEscaped(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
53 void deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override;
54 void serializeTextQuoted(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
55 void deserializeTextQuoted(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override;
56 void deserializeWholeText(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override;
57
58 void serializeTextCSV(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
59
60 /** It is questionable, how NULL values could be represented in CSV. There are three variants:
61 * 1. \N
62 * 2. empty string (without quotes)
63 * 3. NULL
64 * We support all of them (however, second variant is supported by CSVRowInputStream, not by deserializeTextCSV).
65 * (see also input_format_defaults_for_omitted_fields and input_format_csv_unquoted_null_literal_as_null settings)
66 * In CSV, non-NULL string value, starting with \N characters, must be placed in quotes, to avoid ambiguity.
67 */
68 void deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings & settings) const override;
69
70 void serializeTextJSON(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
71 void deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings &) const override;
72 void serializeText(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
73 void serializeTextXML(const IColumn & column, size_t row_num, WriteBuffer & ostr, const FormatSettings &) const override;
74
75 void serializeProtobuf(const IColumn & column, size_t row_num, ProtobufWriter & protobuf, size_t & value_index) const override;
76 void deserializeProtobuf(IColumn & column, ProtobufReader & protobuf, bool allow_add_row, bool & row_added) const override;
77
78 MutableColumnPtr createColumn() const override;
79
80 Field getDefault() const override;
81
82 bool equals(const IDataType & rhs) const override;
83
84 bool isParametric() const override { return true; }
85 bool haveSubtypes() const override { return true; }
86 bool cannotBeStoredInTables() const override { return nested_data_type->cannotBeStoredInTables(); }
87 bool shouldAlignRightInPrettyFormats() const override { return nested_data_type->shouldAlignRightInPrettyFormats(); }
88 bool textCanContainOnlyValidUTF8() const override { return nested_data_type->textCanContainOnlyValidUTF8(); }
89 bool isComparable() const override { return nested_data_type->isComparable(); }
90 bool canBeComparedWithCollation() const override { return nested_data_type->canBeComparedWithCollation(); }
91 bool canBeUsedAsVersion() const override { return false; }
92 bool isSummable() const override { return nested_data_type->isSummable(); }
93 bool canBeUsedInBooleanContext() const override { return nested_data_type->canBeUsedInBooleanContext(); }
94 bool haveMaximumSizeOfValue() const override { return nested_data_type->haveMaximumSizeOfValue(); }
95 size_t getMaximumSizeOfValueInMemory() const override { return 1 + nested_data_type->getMaximumSizeOfValueInMemory(); }
96 bool isNullable() const override { return true; }
97 size_t getSizeOfValueInMemory() const override;
98 bool onlyNull() const override;
99 bool canBeInsideLowCardinality() const override { return nested_data_type->canBeInsideLowCardinality(); }
100
101 const DataTypePtr & getNestedType() const { return nested_data_type; }
102
103 /// If ReturnType is bool, check for NULL and deserialize value into non-nullable column (and return true) or insert default value of nested type (and return false)
104 /// If ReturnType is void, deserialize Nullable(T)
105 template <typename ReturnType = bool>
106 static ReturnType deserializeTextEscaped(IColumn & column, ReadBuffer & istr, const FormatSettings & settings, const DataTypePtr & nested);
107 template <typename ReturnType = bool>
108 static ReturnType deserializeTextQuoted(IColumn & column, ReadBuffer & istr, const FormatSettings &, const DataTypePtr & nested);
109 template <typename ReturnType = bool>
110 static ReturnType deserializeTextCSV(IColumn & column, ReadBuffer & istr, const FormatSettings & settings, const DataTypePtr & nested);
111 template <typename ReturnType = bool>
112 static ReturnType deserializeTextJSON(IColumn & column, ReadBuffer & istr, const FormatSettings &, const DataTypePtr & nested);
113
114private:
115 DataTypePtr nested_data_type;
116};
117
118
119DataTypePtr makeNullable(const DataTypePtr & type);
120DataTypePtr removeNullable(const DataTypePtr & type);
121
122}
123