Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 2 additions & 13 deletions src/adapter/src/explain/insights.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,7 @@ use std::fmt::Debug;
use std::sync::Arc;

use mz_compute_types::dataflows::{BuildDesc, DataflowDescription};
use mz_expr::{
AccessStrategy, AggregateExpr, AggregateFunc, Id, MirRelationExpr, OptimizedMirRelationExpr,
RowSetFinishing,
};
use mz_expr::{AccessStrategy, Id, MirRelationExpr, OptimizedMirRelationExpr, RowSetFinishing};
use mz_ore::num::NonNeg;
use mz_repr::explain::ExprHumanizer;
use mz_repr::{GlobalId, Timestamp};
Expand Down Expand Up @@ -266,15 +263,7 @@ fn global_insights(
let [aggregate] = aggregates.as_slice() else {
return;
};
let AggregateExpr {
func: AggregateFunc::Count,
distinct: false,
expr,
} = aggregate
else {
return;
};
if !expr.is_literal_true() {
if !aggregate.is_count_asterisk() {
return;
}
let name = structured_name(humanizer, *id);
Expand Down
13 changes: 3 additions & 10 deletions src/expr/src/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3340,16 +3340,9 @@ impl AggregateExpr {
match self {
AggregateExpr {
func: AggregateFunc::Count,
expr:
MirScalarExpr::Literal(
Ok(row),
mz_repr::ColumnType {
scalar_type: mz_repr::ScalarType::Bool,
nullable: false,
},
),
..
} => row.unpack_first() == mz_repr::Datum::True,
expr,
distinct: false,
} => expr.is_literal_true(),
_ => false,
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/sql/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3046,6 +3046,8 @@ pub static PG_CATALOG_BUILTINS: LazyLock<BTreeMap<&'static str, Func>> = LazyLoc
"count" => Aggregate {
params!() => Operation::nullary(|_ecx| {
// COUNT(*) is equivalent to COUNT(true).
// This is mirrored in `AggregateExpr::is_count_asterisk`, so if you modify this,
// then attend to that code also.
Ok((HirScalarExpr::literal_true(), AggregateFunc::Count))
}) => Int64, 2803;
params!(Any) => AggregateFunc::Count => Int64, 2147;
Expand Down
93 changes: 93 additions & 0 deletions test/sqllogictest/explain/optimized_plan_as_text.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1623,3 +1623,96 @@ Source materialize.public.t6
Target cluster: no_replicas

EOF

# `count(*)` is planned as `count(true)`. We take care in EXPLAIN to show `count(true)` as `count(*)` to avoid confusing
# users.
query T multiline
EXPLAIN OPTIMIZED PLAN AS TEXT FOR
SELECT count(*)
FROM t5;
----
Explained Query:
With
cte l0 =
Reduce aggregates=[count(*)]
Project ()
ReadStorage materialize.public.t5
Return
Union
Get l0
Map (0)
Union
Negate
Project ()
Get l0
Constant
- ()

Source materialize.public.t5

Target cluster: no_replicas

EOF

query error DISTINCT \* not supported as function args
EXPLAIN OPTIMIZED PLAN AS TEXT FOR
SELECT count(distinct *)
FROM t5;

# `count(true)` is currently also printed as `count(*)` in EXPLAIN, which I'd say is fine.
query T multiline
EXPLAIN OPTIMIZED PLAN AS TEXT FOR
SELECT count(true)
FROM t5;
----
Explained Query:
With
cte l0 =
Reduce aggregates=[count(*)]
Project ()
ReadStorage materialize.public.t5
Return
Union
Get l0
Map (0)
Union
Negate
Project ()
Get l0
Constant
- ()

Source materialize.public.t5

Target cluster: no_replicas

EOF

# But `count(DISTINCT true)` means an entirely different thing, so EXPLAIN shouldn't conflate it with `count(*)`.
query T multiline
EXPLAIN OPTIMIZED PLAN AS TEXT FOR
SELECT count(DISTINCT true)
FROM t5;
----
Explained Query:
With
cte l0 =
Reduce aggregates=[count(distinct true)]
Project ()
ReadStorage materialize.public.t5
Return
Union
Get l0
Map (0)
Union
Negate
Project ()
Get l0
Constant
- ()

Source materialize.public.t5

Target cluster: no_replicas

EOF