forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_insert_stmt_printer.cpp
More file actions
213 lines (193 loc) · 6.69 KB
/
Copy pathob_insert_stmt_printer.cpp
File metadata and controls
213 lines (193 loc) · 6.69 KB
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* Copyright (c) 2025 OceanBase.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#define USING_LOG_PREFIX SQL
#include "sql/printer/ob_insert_stmt_printer.h"
namespace oceanbase
{
using namespace common;
namespace sql
{
int ObInsertStmtPrinter::do_print()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt should not be NULL", K(ret));
} else {
expr_printer_.init(buf_,
buf_len_,
pos_,
schema_guard_,
print_params_,
param_store_);
if (OB_FAIL(print())) {
LOG_WARN("fail to print stmt", K(ret));
}
}
return ret;
}
int ObInsertStmtPrinter::print()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ should not be NULL", K(ret));
} else if (OB_UNLIKELY(!stmt_->is_insert_stmt())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Not a valid insert stmt", K(stmt_->get_stmt_type()),K(ret));
} else if (OB_FAIL(print_basic_stmt())) {
LOG_WARN("fail to print basic stmt", K(ret), K(*stmt_));
} else { /*do nothing*/ }
return ret;
}
int ObInsertStmtPrinter::print_basic_stmt()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ should not be NULL", K(ret));
} else if (OB_FAIL(print_temp_table_as_cte())) {
LOG_WARN("failed to print cte", K(ret));
} else if (OB_FAIL(print_insert())) {
LOG_WARN("fail to print select", K(ret), K(*stmt_));
} else if (OB_FAIL(print_into())) {
LOG_WARN("fail to print into", K(ret), K(*stmt_));
} else if (OB_FAIL(print_values())) {
LOG_WARN("fail to print values", K(ret), K(*stmt_));
} else if (OB_FAIL(print_returning())) {
LOG_WARN("fail to print_returning", K(ret), K(*stmt_));
} else {
// do-nothing
}
return ret;
}
int ObInsertStmtPrinter::print_insert()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_) || OB_ISNULL(buf_) || OB_ISNULL(pos_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ is NULL or buf_ is NULL or pos_ is NULL", K(ret));
} else {
const ObInsertStmt *insert_stmt = static_cast<const ObInsertStmt*>(stmt_);
if (insert_stmt->is_replace()) {
DATA_PRINTF("replace ");
} else {
DATA_PRINTF("insert ");
}
if (OB_SUCC(ret)) {
if (OB_FAIL(print_hint())) { // hint
LOG_WARN("fail to print hint", K(ret), K(*stmt_));
} else {
DATA_PRINTF("into ");
}
}
}
return ret;
}
int ObInsertStmtPrinter::print_into()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_) || OB_ISNULL(buf_) || OB_ISNULL(pos_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ is NULL or buf_ is NULL or pos_ is NULL", K(ret));
} else {
const ObInsertStmt *insert_stmt = static_cast<const ObInsertStmt*>(stmt_);
const TableItem *table_item = NULL;
if (OB_ISNULL(table_item = insert_stmt->get_table_item(0))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Invalid table item", K(stmt_->get_table_size()), K(ret));
} else if (OB_FAIL(print_table(table_item, insert_stmt->get_returning_exprs().count() > 0 ? false : true))) {
LOG_WARN("failed to print table", K(*table_item), K(ret));
} else {
DATA_PRINTF("(");
for (int64_t i = 0; OB_SUCC(ret) && i < insert_stmt->get_values_desc().count(); ++i) {
const ObColumnRefRawExpr* column = insert_stmt->get_values_desc().at(i);
if (OB_ISNULL(column)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("column is NULL", K(ret));
} else {
PRINT_IDENT_WITH_QUOT(column->get_column_name());
DATA_PRINTF(",");
}
}
if (OB_SUCC(ret)) {
--*pos_;
DATA_PRINTF(") ");
}
}
}
return ret;
}
int ObInsertStmtPrinter::print_values()
{
int ret = OB_SUCCESS;
if (OB_ISNULL(stmt_) || OB_ISNULL(buf_) || OB_ISNULL(pos_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt_ is NULL or buf_ is NULL or pos_ is NULL", K(ret));
} else if (!stmt_->is_insert_stmt()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("Not a valid insert stmt", K(stmt_->get_stmt_type()), K(stmt_->get_table_size()), K(ret));
} else {
const ObInsertStmt *insert_stmt = static_cast<const ObInsertStmt*>(stmt_);
if (insert_stmt->get_from_item_size() == 1) {
const TableItem *view = insert_stmt->get_table_item_by_id(
insert_stmt->get_from_item(0).table_id_);
const ObSelectStmt* sub_select_stmt = NULL;
if (OB_ISNULL(view) || OB_ISNULL(sub_select_stmt = view->ref_query_)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("sub select stmt is null", K(ret), K(sub_select_stmt));
} else if (OB_FAIL(print_subquery(sub_select_stmt, PRINT_CTE))) {
LOG_WARN("failed to print subquery", K(ret));
}
} else {
if (insert_stmt->get_values_desc().empty()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("value desc is empty", K(insert_stmt->get_values_desc()), K(ret));
} else {
int64_t column_count = insert_stmt->get_values_desc().count();
int64_t row_count = insert_stmt->get_values_vector().count() / column_count;
DATA_PRINTF("values");
for (int64_t i = 0; OB_SUCC(ret) && i < row_count; ++i) {
DATA_PRINTF("(");
for (int64_t j = 0; OB_SUCC(ret) && j < column_count; ++j) {
const ObColumnRefRawExpr* column = insert_stmt->get_values_desc().at(j);
if (OB_ISNULL(column)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("column is NULL", K(ret));
} else if (i * column_count + j >= insert_stmt->get_values_vector().count()) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("unexpect values vector", K(ret));
} else if (OB_FAIL(expr_printer_.do_print(insert_stmt->get_values_vector().at(i * column_count + j), T_INSERT_SCOPE))) {
LOG_WARN("fail to print where expr", K(ret));
} else {
DATA_PRINTF(",");
}
}
if (OB_SUCC(ret)) {
--*pos_;
DATA_PRINTF("),");
}
}
if (OB_SUCC(ret)) {
--*pos_;
}
}
}
}
return ret;
}
} //end of namespace sql
} //end of namespace oceanbase