forked from oceanbase/oceanbase
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathob_expr_relation_analyzer.cpp
More file actions
175 lines (167 loc) · 6.95 KB
/
ob_expr_relation_analyzer.cpp
File metadata and controls
175 lines (167 loc) · 6.95 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
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL_RESV
#include "sql/resolver/expr/ob_expr_relation_analyzer.h"
#include "lib/oblog/ob_log_module.h"
#include "sql/resolver/dml/ob_select_stmt.h"
#include "common/ob_smart_call.h"
namespace oceanbase {
using namespace common;
namespace sql {
ObExprRelationAnalyzer::ObExprRelationAnalyzer() : query_exprs_()
{}
/**
* @brief ObExprRelationAnalyzer::pull_expr_relation_id_and_levels
* Assume that the following expression is the expression of the i-th level
* ObQueryRefRawExpr:
expr levels: the expr level of all columns (include aggr, set, column) referenced by this subquery
relation ids: all i-th level columns referenced by the subquery
* ObColumnRefRawExpr:
expr levels: contains only i
relation ids: the relation bit index of the corresponding table
* ObSetOpRawExpr
expr levels: contains only i
relation ids: empty
* ObAggRawExpr
expr levels: contains only i
relation ids: the set of relation id of all i-th level columns referenced by this expression
* Others
expr levels: the expr level of all columns referenced by the expression
relation ids: the set of relation id of all i-th level columns referenced by this expression
* @return
*/
int ObExprRelationAnalyzer::pull_expr_relation_id_and_levels(ObRawExpr* expr, int32_t cur_stmt_level)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("expr is null", K(ret), K(expr));
} else if (OB_FAIL(visit_expr(*expr, cur_stmt_level))) {
LOG_WARN("failed to pull expr relation id and levels", K(ret));
}
return ret;
}
int ObExprRelationAnalyzer::visit_expr(ObRawExpr& expr, int32_t stmt_level)
{
int ret = OB_SUCCESS;
// corner case: select min(t1.c1) from t1 group by c2 order by (select avg(t1.c1) from t2);
// avg(t1.1) exists in the subquery, but belongs to the main query
int64_t expr_level = expr.is_aggr_expr() ? expr.get_expr_level() : stmt_level;
int64_t param_count = expr.get_param_count();
if (OB_FAIL(init_expr_info(expr))) {
LOG_WARN("failed into init expr level and relation ids", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < param_count; ++i) {
ObRawExpr* param = NULL;
if (OB_ISNULL(param = expr.get_param_expr(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("param expr is null", K(ret));
} else if (OB_FAIL(SMART_CALL(visit_expr(*param, stmt_level)))) {
LOG_WARN("failed to visit param", K(ret));
} else if (OB_FAIL(expr.get_expr_levels().add_members(param->get_expr_levels()))) {
LOG_WARN("failed to add expr levels", K(ret));
} else if (!param->get_expr_levels().has_member(expr_level)) {
// skip
} else if (OB_FAIL(expr.get_relation_ids().add_members(param->get_relation_ids()))) {
LOG_WARN("failed to add relation ids", K(ret));
}
}
return ret;
}
int ObExprRelationAnalyzer::init_expr_info(ObRawExpr& expr)
{
int ret = OB_SUCCESS;
expr.get_expr_levels().reuse();
if (!expr.is_column_ref_expr() && T_ORA_ROWSCN != expr.get_expr_type()) {
expr.get_relation_ids().reuse();
}
if (expr.is_column_ref_expr() || expr.is_aggr_expr() || expr.is_set_op_expr() || expr.is_win_func_expr() ||
T_ORA_ROWSCN == expr.get_expr_type()) {
int32_t expr_level = expr.get_expr_level();
if (OB_UNLIKELY(expr_level >= OB_MAX_SUBQUERY_LAYER_NUM || expr_level < 0)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("the subquery nested layers is out of range", K(ret), K(expr_level));
} else if (OB_FAIL(expr.get_expr_levels().add_member(expr_level))) {
LOG_WARN("failed to add expr level", K(ret));
}
for (int64_t i = 0; OB_SUCC(ret) && i < query_exprs_.count(); ++i) {
if (OB_ISNULL(query_exprs_.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("query expr is null", K(ret));
} else if (OB_FAIL(query_exprs_.at(i)->get_expr_levels().add_member(expr_level))) {
LOG_WARN("failed to add expr level", K(ret));
} else if (expr_level != query_exprs_.at(i)->get_expr_level() || !expr.is_column_ref_expr()) {
// do nothing
} else if (OB_FAIL(query_exprs_.at(i)->add_relation_ids(expr.get_relation_ids()))) {
// the column and the query ref comes from the same stmt
LOG_WARN("failed to add relation ids", K(ret));
}
}
} else if (OB_FAIL(expr.is_query_ref_expr())) {
ObQueryRefRawExpr& query = static_cast<ObQueryRefRawExpr&>(expr);
if (OB_FAIL(query_exprs_.push_back(&query))) {
LOG_WARN("failed to push back query ref", K(ret));
} else if (OB_UNLIKELY(!query.is_ref_stmt())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("query is expected to use stmt", K(ret));
} else if (OB_FAIL(visit_stmt(query.get_ref_stmt()))) {
LOG_WARN("failed to visit subquery stmt", K(ret));
} else if (OB_UNLIKELY(query_exprs_.empty())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("query expr stack is invalid", K(ret), K(query));
} else {
query_exprs_.pop_back();
}
}
return ret;
}
int ObExprRelationAnalyzer::visit_stmt(ObDMLStmt* stmt)
{
int ret = OB_SUCCESS;
ObSEArray<ObRawExpr*, 8> relation_exprs;
if (OB_ISNULL(stmt)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("stmt is null", K(ret), K(stmt));
} else if (OB_FAIL(stmt->get_relation_exprs(relation_exprs))) {
LOG_WARN("failed to get relation exprs", K(ret));
} else {
for (int64_t i = 0; OB_SUCC(ret) && i < relation_exprs.count(); ++i) {
ObRawExpr* expr = NULL;
if (OB_ISNULL(expr = relation_exprs.at(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("relation expr is null", K(ret), K(expr));
} else if (OB_FAIL(visit_expr(*expr, stmt->get_current_level()))) {
LOG_WARN("failed to visit expr", K(ret));
}
}
if (OB_SUCC(ret) && stmt->is_select_stmt()) {
ObIArray<ObSelectStmt*>& child_stmts = static_cast<ObSelectStmt*>(stmt)->get_set_query();
for (int64_t i = 0; OB_SUCC(ret) && i < child_stmts.count(); ++i) {
ret = SMART_CALL(visit_stmt(child_stmts.at(i)));
}
}
for (int64_t i = 0; OB_SUCC(ret) && i < stmt->get_table_size(); ++i) {
TableItem* table = NULL;
if (OB_ISNULL(table = stmt->get_table_item(i))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("table item is null", K(ret), K(table));
} else if (!table->is_generated_table()) {
// skip
} else if (OB_FAIL(SMART_CALL(visit_stmt(table->ref_query_)))) {
LOG_WARN("failed to visit generated table", K(ret));
}
}
}
return ret;
}
} // namespace sql
} // namespace oceanbase