|
37 | 37 | #include "exprs/vectorized_fn_call.h" |
38 | 38 | #include "exprs/vexpr.h" |
39 | 39 | #include "exprs/vexpr_context.h" |
| 40 | +#include "exprs/virtual_slot_ref.h" |
40 | 41 | #include "exprs/vslot_ref.h" |
41 | 42 | #include "io/cache/block_file_cache_profile.h" |
42 | 43 | #include "runtime/query_cache/query_cache.h" |
@@ -407,12 +408,62 @@ Status OlapScanLocalState::_init_profile() { |
407 | 408 | return Status::OK(); |
408 | 409 | } |
409 | 410 |
|
| 411 | +static bool contains_expr_node_type(const VExprSPtr& expr, TExprNodeType::type node_type) { |
| 412 | + DORIS_CHECK(expr != nullptr); |
| 413 | + if (expr->node_type() == node_type) { |
| 414 | + return true; |
| 415 | + } |
| 416 | + if (expr->is_rf_wrapper() && contains_expr_node_type(expr->get_impl(), node_type)) { |
| 417 | + return true; |
| 418 | + } |
| 419 | + return std::ranges::any_of(expr->children(), [node_type](const auto& child) { |
| 420 | + return contains_expr_node_type(child, node_type); |
| 421 | + }); |
| 422 | +} |
| 423 | + |
| 424 | +static Status validate_residual_scan_conjuncts(RuntimeState* state, |
| 425 | + TPushAggOp::type push_down_agg_type, |
| 426 | + const VExprContextSPtrs& conjuncts) { |
| 427 | + for (const auto& conjunct : conjuncts) { |
| 428 | + const auto& root = conjunct->root(); |
| 429 | + if (contains_expr_node_type(root, TExprNodeType::SEARCH_EXPR)) { |
| 430 | + return Status::InvalidArgument( |
| 431 | + "SEARCH expression remains as a residual scan predicate. A valid search() " |
| 432 | + "must bind at least one indexed field and be evaluated in SegmentIterator. " |
| 433 | + "enable_segment_limit_pushdown only controls SegmentIterator LIMIT pushdown " |
| 434 | + "and cannot make residual SEARCH executable."); |
| 435 | + } |
| 436 | + if (!state->query_options().enable_match_without_inverted_index && |
| 437 | + contains_expr_node_type(root, TExprNodeType::MATCH_PRED)) { |
| 438 | + return Status::InvalidArgument( |
| 439 | + "MATCH expression remains as a residual scan predicate and would fall back to " |
| 440 | + "a disabled slow path because enable_match_without_inverted_index is false. " |
| 441 | + "enable_segment_limit_pushdown only controls SegmentIterator LIMIT pushdown " |
| 442 | + "and cannot make residual MATCH executable. Set " |
| 443 | + "enable_match_without_inverted_index=true to allow slow MATCH execution."); |
| 444 | + } |
| 445 | + } |
| 446 | + |
| 447 | + if (push_down_agg_type == TPushAggOp::COUNT_ON_INDEX && !conjuncts.empty()) { |
| 448 | + return Status::InvalidArgument( |
| 449 | + "COUNT_ON_INDEX pushdown cannot be used with residual scan predicates. " |
| 450 | + "Residual predicates must be evaluated before COUNT_ON_INDEX counts rows; " |
| 451 | + "otherwise the query may return incorrect results. " |
| 452 | + "enable_segment_limit_pushdown only controls SegmentIterator LIMIT pushdown and " |
| 453 | + "does not make COUNT_ON_INDEX safe with residual predicates. Set " |
| 454 | + "enable_count_on_index_pushdown=false to disable COUNT_ON_INDEX pushdown."); |
| 455 | + } |
| 456 | + return Status::OK(); |
| 457 | +} |
| 458 | + |
410 | 459 | Status OlapScanLocalState::_process_conjuncts(RuntimeState* state) { |
411 | 460 | SCOPED_TIMER(_process_conjunct_timer); |
412 | 461 | RETURN_IF_ERROR(ScanLocalState::_process_conjuncts(state)); |
413 | 462 | if (ScanLocalState::_eos) { |
414 | 463 | return Status::OK(); |
415 | 464 | } |
| 465 | + auto& p = _parent->cast<OlapScanOperatorX>(); |
| 466 | + RETURN_IF_ERROR(validate_residual_scan_conjuncts(state, p._push_down_agg_type, _conjuncts)); |
416 | 467 | RETURN_IF_ERROR(_build_key_ranges_and_filters()); |
417 | 468 | return Status::OK(); |
418 | 469 | } |
@@ -472,8 +523,38 @@ Status OlapScanLocalState::_should_push_down_function_filter(VectorizedFnCall* f |
472 | 523 | return Status::OK(); |
473 | 524 | } |
474 | 525 |
|
475 | | -bool OlapScanLocalState::_should_push_down_common_expr() { |
476 | | - return state()->enable_common_expr_pushdown() && _storage_no_merge(); |
| 526 | +bool OlapScanLocalState::_should_push_down_common_expr(const VExprSPtr& expr) { |
| 527 | + // SegmentIterator common exprs must eventually act on at least one scan slot. |
| 528 | + if (!_check_expr_storage_filter(expr, ExprStorageFilterCheckMode::HAS_SEGMENT_EVALUABLE_EXPR)) { |
| 529 | + return false; |
| 530 | + } |
| 531 | + |
| 532 | + // DUP and UNIQUE-MOW/MOR-as-DUP do not need storage aggregation/merge, so any slot-based common |
| 533 | + // expression can be evaluated together with SegmentIterator lazy materialization. |
| 534 | + if (_storage_no_merge()) { |
| 535 | + return true; |
| 536 | + } |
| 537 | + |
| 538 | + // AGG and UNIQUE-MOR may still merge value columns above SegmentIterator. Push only key-column |
| 539 | + // expressions so filtering does not observe pre-merge values. |
| 540 | + return !_check_expr_storage_filter(expr, ExprStorageFilterCheckMode::HAS_NON_KEY_SLOT); |
| 541 | +} |
| 542 | + |
| 543 | +bool OlapScanLocalState::_check_expr_storage_filter(const VExprSPtr& expr, |
| 544 | + ExprStorageFilterCheckMode mode) { |
| 545 | + if (expr->is_slot_ref()) { |
| 546 | + const auto* slot_ref = assert_cast<const VSlotRef*>(expr.get()); |
| 547 | + return mode == ExprStorageFilterCheckMode::HAS_SEGMENT_EVALUABLE_EXPR || |
| 548 | + !_is_key_column(slot_ref->expr_name()); |
| 549 | + } |
| 550 | + if (expr->is_virtual_slot_ref()) { |
| 551 | + // Treat virtual slot ref as non-key because it may depend on non-key source columns. |
| 552 | + return true; |
| 553 | + } |
| 554 | + |
| 555 | + return std::ranges::any_of(expr->children(), [this, mode](const auto& child) { |
| 556 | + return _check_expr_storage_filter(child, mode); |
| 557 | + }); |
477 | 558 | } |
478 | 559 |
|
479 | 560 | bool OlapScanLocalState::_storage_no_merge() { |
@@ -1081,6 +1162,8 @@ OlapScanOperatorX::OlapScanOperatorX(ObjectPool* pool, const TPlanNode& tnode, i |
1081 | 1162 | _cache_param(param) { |
1082 | 1163 | _output_tuple_id = tnode.olap_scan_node.tuple_id; |
1083 | 1164 | if (_olap_scan_node.__isset.sort_info && _olap_scan_node.__isset.sort_limit) { |
| 1165 | + DORIS_CHECK(_limit < 0); |
| 1166 | + DORIS_CHECK(_olap_scan_node.sort_limit > 0); |
1084 | 1167 | _limit_per_scanner = _olap_scan_node.sort_limit; |
1085 | 1168 | } |
1086 | 1169 | DBUG_EXECUTE_IF("segment_iterator.topn_opt_1", { |
|
0 commit comments