aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/Formats/insertNullAsDefaultIfNeeded.cpp
blob: 767892718c575f84122a20f387f2f7baf3dc63a9 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#include <Formats/insertNullAsDefaultIfNeeded.h>
#include <Columns/ColumnNullable.h>
#include <Columns/ColumnLowCardinality.h>
#include <DataTypes/DataTypeNullable.h>
#include <DataTypes/DataTypeLowCardinality.h>

namespace DB
{

void insertNullAsDefaultIfNeeded(ColumnWithTypeAndName & input_column, const ColumnWithTypeAndName & header_column, size_t column_i, BlockMissingValues * block_missing_values)
{
    if (!isNullableOrLowCardinalityNullable(input_column.type) || isNullableOrLowCardinalityNullable(header_column.type))
        return;

    if (block_missing_values)
    {
        for (size_t i = 0; i < input_column.column->size(); ++i)
        {
            if (input_column.column->isNullAt(i))
                block_missing_values->setBit(column_i, i);
        }
    }

    if (input_column.type->isNullable())
    {
        input_column.column = assert_cast<const ColumnNullable *>(input_column.column.get())->getNestedColumnWithDefaultOnNull();
        input_column.type = removeNullable(input_column.type);
    }
    else
    {
        input_column.column = assert_cast<const ColumnLowCardinality *>(input_column.column.get())->cloneWithDefaultOnNull();
        const auto * lc_type = assert_cast<const DataTypeLowCardinality *>(input_column.type.get());
        input_column.type = std::make_shared<DataTypeLowCardinality>(removeNullable(lc_type->getDictionaryType()));
    }
}

}