Coverage Report

Created: 2026-03-12 16:03

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
be/src/util/mysql_global.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
18
#pragma once
19
20
#include <float.h>
21
#include <stdint.h>
22
23
#include <cstring>
24
#include <string>
25
26
namespace doris {
27
28
typedef unsigned char uchar;
29
30
4.71M
#define int1store(T, A) *((uint8_t*)(T)) = (uint8_t)(A)
31
629
#define int2store(T, A) *((uint16_t*)(T)) = (uint16_t)(A)
32
#define int3store(T, A)                           \
33
385
    do {                                          \
34
385
        *(T) = (uchar)((A));                      \
35
385
        *(T + 1) = (uchar)(((uint32_t)(A) >> 8)); \
36
385
        *(T + 2) = (uchar)(((A) >> 16));          \
37
385
    } while (0)
38
574
#define int4store(T, A) *((uint32_t*)(T)) = (uint32_t)(A)
39
50
#define int8store(T, A) *((int64_t*)(T)) = (uint64_t)(A)
40
99
#define float4store(T, A) *((float*)(T)) = (float)(A)
41
28
#define float8store(T, A) *((double*)(T)) = (double)(A)
42
43
#define MY_ALIGN(A, L) (((A) + (L)-1) & ~((L)-1))
44
#define SIZEOF_CHARP 8
45
46
#define MAX_TINYINT_WIDTH 3     /* Max width for a TINY w.o. sign */
47
#define MAX_SMALLINT_WIDTH 5    /* Max width for a SHORT w.o. sign */
48
#define MAX_MEDIUMINT_WIDTH 8   /* Max width for a INT24 w.o. sign */
49
#define MAX_INT_WIDTH 10        /* Max width for a LONG w.o. sign */
50
#define MAX_BIGINT_WIDTH 20     /* Max width for a LONGLONG */
51
#define MAX_LARGEINT_WIDTH 39   /* Max width for a LARGEINT */
52
#define MAX_CHAR_WIDTH 255      /* Max length for a CHAR column */
53
#define MAX_BLOB_WIDTH 16777216 /* Default width for blob */
54
#define MAX_TIME_WIDTH 10       /* Max width for a TIME HH:MM:SS*/
55
#define MAX_DECPT_FOR_F_FORMAT DBL_DIG
56
#define MAX_DATETIME_WIDTH 27 /* YYYY-MM-DD HH:MM:SS.ssssss */
57
0
#define MAX_DECIMAL_WIDTH 29  /* Max width for a DECIMAL */
58
59
/* -[digits].E+## */
60
#define MAX_FLOAT_STR_LENGTH 24 // see gutil/strings/numbers.h kFloatToBufferSize
61
/* -[digits].E+### */
62
#define MAX_DOUBLE_STR_LENGTH 32 // see gutil/strings/numbers.h kDoubleToBufferSize
63
64
/* -[digits].[frac] */
65
#define MAX_DECIMAL_STR_LENGTH 29
66
67
inline void direct_write_to_mysql_result_string(std::string& mysql_rows, const char* str,
68
4.75M
                                                size_t size) {
69
    // MySQL protocol length encoding:
70
    // <= 250: 1 byte for length
71
    // < 65536: 1 byte (252) + 2 bytes for length
72
    // < 16777216: 1 byte (253) + 3 bytes for length
73
    // >= 16777216: 1 byte (254) + 8 bytes for length
74
75
4.75M
    char buf[16];
76
4.75M
    if (size < 251ULL) {
77
4.71M
        int1store(buf, size);
78
4.71M
        mysql_rows.append(buf, 1);
79
4.71M
    } else if (size < 65536ULL) {
80
38.3k
        buf[0] = static_cast<char>(252);
81
38.3k
        uint16_t temp16 = static_cast<uint16_t>(size);
82
38.3k
        memcpy(buf + 1, &temp16, sizeof(temp16));
83
38.3k
        mysql_rows.append(buf, 3);
84
38.3k
    } else if (size < 16777216ULL) {
85
385
        buf[0] = static_cast<char>(253);
86
385
        int3store(buf + 1, size);
87
385
        mysql_rows.append(buf, 4);
88
18.4E
    } else {
89
18.4E
        buf[0] = static_cast<char>(254);
90
18.4E
        uint64_t temp64 = static_cast<uint64_t>(size);
91
18.4E
        memcpy(buf + 1, &temp64, sizeof(temp64));
92
18.4E
        mysql_rows.append(buf, 9);
93
18.4E
    }
94
95
    // Append string content
96
4.75M
    mysql_rows.append(str, size);
97
4.75M
}
98
99
34.3M
inline void direct_write_to_mysql_result_null(std::string& mysql_rows) {
100
    // MySQL protocol for NULL value is a single byte with value 251
101
34.3M
    mysql_rows.push_back(static_cast<char>(251));
102
34.3M
}
103
104
} // namespace doris