forked from oceanbase/seekdb
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathob_log_topk.cpp
More file actions
163 lines (155 loc) · 6.68 KB
/
Copy pathob_log_topk.cpp
File metadata and controls
163 lines (155 loc) · 6.68 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
/*
* 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_OPT
#include "ob_log_topk.h"
#include "sql/rewrite/ob_transform_utils.h"
using namespace oceanbase::sql;
using namespace oceanbase::common;
using namespace oceanbase::sql::log_op_def;
int ObLogTopk::set_topk_params(ObRawExpr *limit_count, ObRawExpr *limit_offset,
int64_t minimum_row_count, int64_t topk_precision)
{
int ret = OB_SUCCESS;
if (OB_ISNULL(limit_count)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("limit_count is NULL", K(ret));
} else {
topk_limit_count_ = limit_count;
topk_limit_offset_ = limit_offset;
minimum_row_count_ = minimum_row_count;
topk_precision_ = topk_precision;
}
return ret;
}
int ObLogTopk::get_op_exprs(ObIArray<ObRawExpr*> &all_exprs)
{
int ret = OB_SUCCESS;
if (NULL != topk_limit_count_ && OB_FAIL(all_exprs.push_back(topk_limit_count_))) {
LOG_WARN("failed to push back exprs", K(ret));
} else if (NULL != topk_limit_offset_ && OB_FAIL(all_exprs.push_back(topk_limit_offset_))) {
LOG_WARN("failed to push back exprs", K(ret));
} else if (OB_FAIL(ObLogicalOperator::get_op_exprs(all_exprs))) {
LOG_WARN("failed to get exprs", K(ret));
} else { /*do nothing*/ }
return ret;
}
int ObLogTopk::est_width()
{
int ret = OB_SUCCESS;
double width = 0.0;
ObSEArray<ObRawExpr*, 16> output_exprs;
if (OB_ISNULL(get_plan())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid plan", K(ret));
} else if (OB_FAIL(get_topk_output_exprs(output_exprs))) {
LOG_WARN("failed to get topk output exprs", K(ret));
} else if (OB_FAIL(ObOptEstCost::estimate_width_for_exprs(get_plan()->get_basic_table_metas(),
get_plan()->get_selectivity_ctx(),
output_exprs,
width))) {
LOG_WARN("failed to estimate width for output topk exprs", K(ret));
} else {
set_width(width);
LOG_TRACE("est width for topk", K(output_exprs), K(width));
}
return ret;
}
int ObLogTopk::get_topk_output_exprs(ObIArray<ObRawExpr *> &output_exprs)
{
int ret = OB_SUCCESS;
ObLogPlan *plan = NULL;
ObSEArray<ObRawExpr*, 16> candi_exprs;
ObSEArray<ObRawExpr*, 16> extracted_col_aggr_winfunc_exprs;
if (OB_ISNULL(plan = get_plan())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("invalid input", K(ret));
} else if (OB_FAIL(append_array_no_dup(candi_exprs, plan->get_select_item_exprs_for_width_est()))) {
LOG_WARN("failed to add into output exprs", K(ret));
} else if (OB_FAIL(ObRawExprUtils::extract_col_aggr_winfunc_exprs(candi_exprs,
extracted_col_aggr_winfunc_exprs))) {
} else if (OB_FAIL(append_array_no_dup(output_exprs, extracted_col_aggr_winfunc_exprs))) {
LOG_WARN("failed to add into output exprs", K(ret));
} else {/*do nothing*/}
return ret;
}
int ObLogTopk::est_cost()
{
int ret = OB_SUCCESS;
int64_t limit_count = 0;
int64_t offset_count = 0;
bool is_null_value = false;
int64_t parallel = 0.0;
ObLogicalOperator *child = NULL;
if (OB_ISNULL(child = get_child(first_child)) || OB_ISNULL(get_stmt()) ||
OB_ISNULL(get_plan())) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected null", K(child), K(get_stmt()), K(get_plan()),K(ret));
} else if (OB_UNLIKELY((parallel = get_parallel()) < 1)) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("get unexpected parallel degree", K(ret));
} else if (OB_FAIL(ObTransformUtils::get_limit_value(topk_limit_count_,
get_plan()->get_optimizer_context().get_params(),
get_plan()->get_optimizer_context().get_exec_ctx(),
&get_plan()->get_optimizer_context().get_allocator(),
limit_count,
is_null_value))) {
LOG_WARN("get limit count num error", K(ret));
} else if (!is_null_value &&
OB_FAIL(ObTransformUtils::get_limit_value(topk_limit_offset_,
get_plan()->get_optimizer_context().get_params(),
get_plan()->get_optimizer_context().get_exec_ctx(),
&get_plan()->get_optimizer_context().get_allocator(),
offset_count,
is_null_value))) {
LOG_WARN("Get limit offset num error", K(ret));
} else {
limit_count = is_null_value ? 0 : limit_count + offset_count;
double topk_card = static_cast<double>(std::max(minimum_row_count_, limit_count));
double row_count = child->get_card() * static_cast<double>(topk_precision_) / 100.0;
topk_card = std::max(topk_card, row_count);
topk_card = std::min(topk_card, child->get_card());
ObOptimizerContext &opt_ctx = get_plan()->get_optimizer_context();
double op_cost = ObOptEstCost::cost_get_rows(topk_card / parallel,
opt_ctx);
set_card(topk_card);
set_op_cost(op_cost);
set_cost(child->get_cost() + op_cost);
}
return ret;
}
int ObLogTopk::get_plan_item_info(PlanText &plan_text,
ObSqlPlanItem &plan_item)
{
int ret = OB_SUCCESS;
if (OB_FAIL(ObLogicalOperator::get_plan_item_info(plan_text, plan_item))) {
LOG_WARN("failed to get plan item info", K(ret));
}
BEGIN_BUF_PRINT;
if (OB_FAIL(ret)) {
} else if (OB_FAIL(BUF_PRINTF("minimum_row_count:%ld top_precision:%ld ",
minimum_row_count_, topk_precision_))) {
LOG_WARN("BUF_PRINTF fails", K(ret));
} else {
ObRawExpr *limit = topk_limit_count_;
ObRawExpr *offset = topk_limit_offset_;
EXPLAIN_PRINT_EXPR(limit, type);
BUF_PRINTF(", ");
EXPLAIN_PRINT_EXPR(offset, type);
END_BUF_PRINT(plan_item.special_predicates_,
plan_item.special_predicates_len_);
}
return ret;
}