-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Fix: disable index scan for NOT_EQUAL operator (Fixes #620) #631
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -128,11 +128,12 @@ RC PhysicalPlanGenerator::create_plan(TableGetLogicalOperator &table_get_oper, u | |||||
|
|
||||||
| Index *index = nullptr; | ||||||
| ValueExpr *value_expr = nullptr; | ||||||
| CompOp target_op = NO_OP; | ||||||
| for (auto &expr : predicates) { | ||||||
| if (expr->type() == ExprType::COMPARISON) { | ||||||
| auto comparison_expr = static_cast<ComparisonExpr *>(expr.get()); | ||||||
| // 简单处理,就找等值查询 | ||||||
| if (comparison_expr->comp() != EQUAL_TO && comparison_expr->comp() != NOT_EQUAL) { | ||||||
| if (comparison_expr->comp() != EQUAL_TO ) { | ||||||
| continue; | ||||||
| } | ||||||
|
|
||||||
|
|
@@ -161,12 +162,17 @@ RC PhysicalPlanGenerator::create_plan(TableGetLogicalOperator &table_get_oper, u | |||||
| const Field &field = field_expr->field(); | ||||||
| index = table->find_index_by_field(field.field_name()); | ||||||
| if (nullptr != index) { | ||||||
| target_op = comparison_expr->comp(); | ||||||
| // 找到索引把操作符记录下来 | ||||||
| break; | ||||||
| } | ||||||
| } | ||||||
| } | ||||||
|
|
||||||
| if (index != nullptr) { | ||||||
|
|
||||||
| if (index != nullptr && target_op != NOT_EQUAL) | ||||||
| { | ||||||
| //修改条件:不等于操作不使用索引 | ||||||
| ASSERT(value_expr != nullptr, "got an index but value expr is null ?"); | ||||||
|
|
||||||
| const Value &value = value_expr->get_value(); | ||||||
|
|
@@ -181,16 +187,27 @@ RC PhysicalPlanGenerator::create_plan(TableGetLogicalOperator &table_get_oper, u | |||||
| index_scan_oper->set_predicates(std::move(predicates)); | ||||||
| oper = unique_ptr<PhysicalOperator>(index_scan_oper); | ||||||
| LOG_TRACE("use index scan"); | ||||||
| } else { | ||||||
| } else | ||||||
| { | ||||||
| //对于不等于操作或者没有索引的情况,使用表扫描 | ||||||
| auto table_scan_oper = new TableScanPhysicalOperator(table, table_get_oper.read_write_mode()); | ||||||
| table_scan_oper->set_predicates(std::move(predicates)); | ||||||
| oper = unique_ptr<PhysicalOperator>(table_scan_oper); | ||||||
| LOG_TRACE("use table scan"); | ||||||
| if(target_op == NOT_EQUAL) | ||||||
| { | ||||||
| LOG_TRACE("not equal operation,use table scan instead of index scan"); | ||||||
|
||||||
| LOG_TRACE("not equal operation,use table scan instead of index scan"); | |
| LOG_TRACE("not equal operation, use table scan instead of index scan"); |
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Duplicated and redundant logging logic. This code logs "use table scan" unconditionally at line 196, then has a conditional block (lines 197-204) that logs either a specific message for NOT_EQUAL or "use table scan" again. This results in duplicate "use table scan" messages being logged for most cases.
Recommend removing the LOG_TRACE at line 196 and keeping only the conditional logging block, or simplifying to just use the conditional logging.
Copilot
AI
Dec 8, 2025
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[nitpick] Unnecessary blank line added. This extra blank line is inconsistent with the rest of the file's formatting and should be removed.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The logic here is problematic. The code now only allows EQUAL_TO comparisons to proceed, but then later checks if
target_op == NOT_EQUAL(line 165). This creates a logical inconsistency: if NOT_EQUAL comparisons are filtered out here withcontinue, thentarget_opcan never be set to NOT_EQUAL at line 165.The original code allowed both EQUAL_TO and NOT_EQUAL to proceed, which made sense if the intent is to detect NOT_EQUAL operations and disable index usage for them. This change breaks that detection logic.