aboutsummaryrefslogtreecommitdiffstats
path: root/contrib/clickhouse/src/QueryPipeline/SizeLimits.cpp
blob: 76832b1f95123b428d18e6ff97d5ac41d92c1cf4 (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <QueryPipeline/SizeLimits.h>
#include <Common/formatReadable.h>
#include <Common/Exception.h>
#include <Common/ProfileEvents.h>
#include <string>


namespace ProfileEvents
{
    extern const Event OverflowThrow;
    extern const Event OverflowBreak;
}


namespace DB
{

bool SizeLimits::check(UInt64 rows, UInt64 bytes, const char * what, int too_many_rows_exception_code, int too_many_bytes_exception_code) const
{
    if (overflow_mode == OverflowMode::THROW)
    {
        if (max_rows && rows > max_rows)
        {
            ProfileEvents::increment(ProfileEvents::OverflowThrow);
            throw Exception(
                too_many_rows_exception_code,
                "Limit for {} exceeded, max rows: {}, current rows: {}",
                what,
                formatReadableQuantity(max_rows),
                formatReadableQuantity(rows));
        }

        if (max_bytes && bytes > max_bytes)
        {
            ProfileEvents::increment(ProfileEvents::OverflowThrow);
            throw Exception(
                too_many_bytes_exception_code,
                "Limit for {} exceeded, max bytes: {}, current bytes: {}",
                what,
                ReadableSize(max_bytes),
                ReadableSize(bytes));
        }

        return true;
    }

    return softCheck(rows, bytes);
}

bool SizeLimits::softCheck(UInt64 rows, UInt64 bytes) const
{
    /// For result_overflow_mode = 'break', we check for >= to tell that no more data is needed.
    /// Last chunk will be processed.
    if ((max_rows && rows >= max_rows)
        || (max_bytes && bytes >= max_bytes))
    {
        ProfileEvents::increment(ProfileEvents::OverflowBreak);
        return false;
    }
    return true;
}

bool SizeLimits::check(UInt64 rows, UInt64 bytes, const char * what, int exception_code) const
{
    return check(rows, bytes, what, exception_code, exception_code);
}

}