|
1 | 1 | use std::{collections::HashMap, sync::Arc};
|
2 | 2 |
|
3 |
| -use crate::plan_nodes::{BinOpType, ColumnRefExpr, LogOpType, OptRelNode, UnOpType}; |
| 3 | +use crate::plan_nodes::{ |
| 4 | + BinOpType, ColumnRefExpr, ConstantExpr, ConstantType, LogOpType, OptRelNode, UnOpType, |
| 5 | +}; |
4 | 6 | use crate::properties::column_ref::{ColumnRefPropertyBuilder, GroupColumnRefs};
|
5 | 7 | use crate::{
|
6 | 8 | plan_nodes::{OptRelNodeRef, OptRelNodeTyp},
|
@@ -392,8 +394,39 @@ impl<M: MostCommonValues, D: Distribution> CostModel<OptRelNodeTyp> for OptCostM
|
392 | 394 | OptRelNodeTyp::PhysicalEmptyRelation => Self::cost(0.5, 0.01, 0.0),
|
393 | 395 | OptRelNodeTyp::PhysicalLimit => {
|
394 | 396 | let (row_cnt, compute_cost, _) = Self::cost_tuple(&children[0]);
|
395 |
| - let selectivity = 0.001; |
396 |
| - Self::cost((row_cnt * selectivity).max(1.0), compute_cost, 0.0) |
| 397 | + let row_cnt = if let Some(context) = context { |
| 398 | + if let Some(optimizer) = optimizer { |
| 399 | + let mut fetch_expr = |
| 400 | + optimizer.get_all_group_bindings(context.children_group_ids[2], false); |
| 401 | + assert!( |
| 402 | + fetch_expr.len() == 1, |
| 403 | + "fetch expression should be the only expr in the group" |
| 404 | + ); |
| 405 | + let fetch_expr = fetch_expr.pop().unwrap(); |
| 406 | + assert!( |
| 407 | + matches!( |
| 408 | + fetch_expr.typ, |
| 409 | + OptRelNodeTyp::Constant(ConstantType::UInt64) |
| 410 | + ), |
| 411 | + "fetch type can only be UInt64" |
| 412 | + ); |
| 413 | + let fetch = ConstantExpr::from_rel_node(fetch_expr) |
| 414 | + .unwrap() |
| 415 | + .value() |
| 416 | + .as_u64(); |
| 417 | + // u64::MAX represents None |
| 418 | + if fetch == u64::MAX { |
| 419 | + row_cnt |
| 420 | + } else { |
| 421 | + row_cnt.min(fetch as f64) |
| 422 | + } |
| 423 | + } else { |
| 424 | + (row_cnt * INVALID_SELECTIVITY).max(1.0) |
| 425 | + } |
| 426 | + } else { |
| 427 | + (row_cnt * INVALID_SELECTIVITY).max(1.0) |
| 428 | + }; |
| 429 | + Self::cost(row_cnt, compute_cost, 0.0) |
397 | 430 | }
|
398 | 431 | OptRelNodeTyp::PhysicalFilter => {
|
399 | 432 | let (row_cnt, _, _) = Self::cost_tuple(&children[0]);
|
|
0 commit comments