Skip to content

Commit

Permalink
Addressing some PR comments
Browse files Browse the repository at this point in the history
  • Loading branch information
mamcx committed Jan 15, 2025
1 parent 3839516 commit f8700bc
Show file tree
Hide file tree
Showing 2 changed files with 129 additions and 83 deletions.
84 changes: 43 additions & 41 deletions crates/physical-plan/src/plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1255,18 +1255,18 @@ Seq Scan on t
&db,
sql,
expect![[r#"
Index Join: Rhs
-> Index Join: Rhs
-> Index Join: Rhs
-> Index Scan using Index id 0: (identity) on u
-> Index Cond: (u.identity = U64(5))
-> Inner Unique: true
-> Index Cond: (u.entity_id = p.entity_id)
-> Inner Unique: false
-> Index Cond: (p.chunk = q.chunk)
Inner Unique: true
Index Cond: (q.entity_id = b.entity_id)
Output: b.entity_id, b.misc"#]],
Index Join: Rhs on b
-> Index Join: Rhs on q
-> Index Join: Rhs on p
-> Index Scan using Index id 0 on u
-> Index Cond: (u.identity = U64(5))
-> Inner Unique: true
-> Index Cond: (u.entity_id = p.entity_id)
-> Inner Unique: false
-> Index Cond: (p.chunk = q.chunk)
Inner Unique: true
Index Cond: (q.entity_id = b.entity_id)
Output: b.entity_id, b.misc"#]],
);

let lp = parse_and_type_sub(sql, &db).unwrap();
Expand Down Expand Up @@ -1464,10 +1464,10 @@ Seq Scan on t
&db,
sql,
expect![[r#"
Hash Join: All
-> Hash Join: All
-> Hash Join: All
-> Hash Join: All
Hash Join
-> Hash Join
-> Hash Join
-> Hash Join
-> Seq Scan on m
-> Seq Scan on n
-> Inner Unique: false
Expand Down Expand Up @@ -1661,7 +1661,7 @@ Hash Join: All
sql,
expect![
r#"
Index Scan using Index id 2: (x, y, z) on t
Index Scan using Index id 2 on t
Index Cond: (t.z = U8(5), t.x = U8(3), t.y = U8(4))
Output: t.w, t.x, t.y, t.z"#
],
Expand Down Expand Up @@ -1810,21 +1810,23 @@ Index Scan using Index id 2: (x, y, z) on t
"SELECT m.* FROM m CROSS JOIN p WHERE m.employee = 1",
expect![
r#"
Query: SELECT m.* FROM m CROSS JOIN p WHERE m.employee = 1
Nested Loop
-> Index Scan using Index id 0: (employee) on m:1
-> Index Cond: (m.employee = U64(1))
-> Seq Scan on p:2
Output: m.employee, m.manager
-------
Schema:
Label m: 1
Columns: employee, manager
Indexes: Unique(m.employee)
Label p: 2
Columns: id, name
Indexes: Unique(p.id)"#
Query: SELECT m.* FROM m CROSS JOIN p WHERE m.employee = 1
Nested Loop
-> Index Scan using Index id 0 on m
-> Index Cond: (m.employee = U64(1))
-> Seq Scan on p:2
Output: m.employee, m.manager
-------
Schema:
Label: m, TableId:1
Columns: employee, manager
Indexes: Index id 0: (m.employee), Index id 1: (m.manager)
Constraints: Constraint id 0: Unique(m.employee)
Label: p, TableId:3
Columns: id, name
Indexes: Index id 0: (p.id)
Constraints: Constraint id 0: Unique(p.id)"#
],
);
}
Expand Down Expand Up @@ -1918,9 +1920,9 @@ Index Scan using Index id 2: (x, y, z) on t
&db,
"SELECT m.* FROM m WHERE employee = 1",
expect![[r#"
Index Scan using Index id 0: (employee) on m
Index Cond: (m.employee = U64(1))
Output: m.employee, m.manager"#]],
Index Scan using Index id 0 on m
Index Cond: (m.employee = U64(1))
Output: m.employee, m.manager"#]],
);
}

Expand All @@ -1947,7 +1949,7 @@ Index Scan using Index id 2: (x, y, z) on t
&db,
"SELECT p.* FROM m JOIN p ON m.employee = p.id where m.employee = 1",
expect![[r#"
Hash Join: All
Hash Join
-> Seq Scan on m
-> Seq Scan on p
Inner Unique: false
Expand All @@ -1965,11 +1967,11 @@ Index Scan using Index id 2: (x, y, z) on t
&db,
"SELECT p.* FROM m JOIN p ON m.employee = p.id",
expect![[r#"
Index Join: Rhs
-> Seq Scan on m
Inner Unique: true
Index Cond: (m.employee = p.id)
Output: p.id, p.name"#]],
Index Join: Rhs on p
-> Seq Scan on m
Inner Unique: true
Index Cond: (m.employee = p.id)
Output: p.id, p.name"#]],
);
}
}
128 changes: 86 additions & 42 deletions crates/physical-plan/src/printer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@ use crate::plan::{
};
use spacetimedb_expr::StatementSource;
use spacetimedb_lib::AlgebraicValue;
use spacetimedb_primitives::{ColId, IndexId};
use spacetimedb_primitives::{ColId, ConstraintId, IndexId};
use spacetimedb_schema::def::ConstraintData;
use spacetimedb_schema::schema::{ColumnSchema, IndexSchema, TableSchema};
use spacetimedb_schema::schema::{IndexSchema, TableSchema};
use spacetimedb_sql_parser::ast::BinOp;

fn range_to_op(lower: &Bound<AlgebraicValue>, upper: &Bound<AlgebraicValue>) -> BinOp {
Expand Down Expand Up @@ -149,10 +149,34 @@ struct PrintSarg<'a> {
prefix: &'a [(ColId, AlgebraicValue)],
}

/// A pretty printer for objects that could have a empty name
pub enum PrintName<'a> {
Named { object: &'a str, name: &'a str },
Id { object: &'a str, id: usize },
}

impl<'a> PrintName<'a> {
fn new(object: &'a str, id: usize, name: &'a str) -> Self {
if name.is_empty() {
Self::Id { object, id }
} else {
Self::Named { object, name }
}
}

fn index(index_id: IndexId, index_name: &'a str) -> Self {
Self::new("Index", index_id.idx(), index_name)
}

fn constraint(constraint_id: ConstraintId, constraint_name: &'a str) -> Self {
Self::new("Constraint", constraint_id.idx(), constraint_name)
}
}

/// A pretty printer for indexes
pub enum PrintIndex<'a> {
Named(&'a str, Vec<&'a ColumnSchema>),
Id(IndexId, Vec<&'a ColumnSchema>),
pub struct PrintIndex<'a> {
name: PrintName<'a>,
cols: Vec<Field<'a>>,
}

impl<'a> PrintIndex<'a> {
Expand All @@ -161,13 +185,15 @@ impl<'a> PrintIndex<'a> {
.index_algorithm
.columns()
.iter()
.map(|x| table.get_column(x.idx()).unwrap())
.map(|x| Field {
table: table.table_name.as_ref(),
field: table.get_column(x.idx()).unwrap().col_name.as_ref(),
})
.collect_vec();

if idx.index_name.is_empty() {
Self::Id(idx.index_id, cols)
} else {
Self::Named(&idx.index_name, cols)
Self {
name: PrintName::index(idx.index_id, &idx.index_name),
cols,
}
}
}
Expand Down Expand Up @@ -196,12 +222,13 @@ pub enum Line<'a> {
},
IxScan {
table_name: &'a str,
index: PrintIndex<'a>,
label: Label,
index: PrintName<'a>,
ident: u16,
},
IxJoin {
semi: &'a Semi,

rhs: String,
ident: u16,
},
HashJoin {
Expand Down Expand Up @@ -351,8 +378,7 @@ fn eval_plan<'a>(lines: &mut Lines<'a>, plan: &'a PhysicalPlan, ident: u16) {

lines.add(Line::IxScan {
table_name: &idx.schema.table_name,
index: PrintIndex::new(index, &idx.schema),
label: *label,
index: PrintName::index(idx.index_id, &index.index_name),
ident,
});

Expand All @@ -364,8 +390,13 @@ fn eval_plan<'a>(lines: &mut Lines<'a>, plan: &'a PhysicalPlan, ident: u16) {
}
PhysicalPlan::IxJoin(idx, semi) => {
lines.add_table(idx.rhs_label, &idx.rhs);

lines.add(Line::IxJoin { semi, ident });
//let lhs = lines.labels.table_by_label(&idx.lhs_field.label).unwrap();
let rhs = lines.labels.table_by_label(&idx.rhs_label).unwrap();
lines.add(Line::IxJoin {
semi,
ident,
rhs: rhs.name.to_string(),
});

eval_plan(lines, &idx.lhs, ident + 4);

Expand Down Expand Up @@ -548,19 +579,20 @@ impl<'a> fmt::Display for Field<'a> {
write!(f, "{}.{}", self.table, self.field)
}
}

impl<'a> fmt::Display for PrintName<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
PrintName::Named { object, name } => write!(f, "{} {}", object, name),
PrintName::Id { object, id } => write!(f, "{} id {}", object, id),
}
}
}

impl<'a> fmt::Display for PrintIndex<'a> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let cols = match self {
PrintIndex::Named(name, cols) => {
write!(f, "Index {name}: ")?;
cols
}
PrintIndex::Id(id, cols) => {
write!(f, "Index id {id}: ")?;
cols
}
};
write!(f, "({})", cols.iter().map(|x| &x.col_name).join(", "))
write!(f, "{}: ", self.name)?;
write!(f, "({})", self.cols.iter().join(", "))
}
}

Expand Down Expand Up @@ -592,14 +624,9 @@ impl<'a> fmt::Display for Explain<'a> {
Line::IxScan {
table_name,
index,
label,
ident: _,
} => {
if self.options.show_schema {
write!(f, "Index Scan using {index} on {table_name}:{}", label.0)?;
} else {
write!(f, "Index Scan using {index} on {table_name}")?;
}
write!(f, "Index Scan using {index} on {table_name}")?;
}
Line::Filter { expr, ident: _ } => {
write!(
Expand All @@ -624,12 +651,17 @@ impl<'a> fmt::Display for Explain<'a> {
)?;
}

Line::IxJoin { semi, ident: _ } => {
write!(f, "Index Join: {semi:?}")?;
}
Line::HashJoin { semi, ident: _ } => {
write!(f, "Hash Join: {semi:?}")?;
Line::IxJoin { semi, rhs, ident: _ } => {
write!(f, "Index Join: {semi:?} on {rhs}")?;
}
Line::HashJoin { semi, ident: _ } => match semi {
Semi::All => {
write!(f, "Hash Join")?;
}
semi => {
write!(f, "Hash Join: {semi:?}")?;
}
},
Line::NlJoin { ident: _ } => {
write!(f, "Nested Loop")?;
}
Expand Down Expand Up @@ -683,22 +715,34 @@ impl<'a> fmt::Display for Explain<'a> {
writeln!(f, "-------")?;
writeln!(f, "Schema:")?;
writeln!(f)?;
for (pos, (label, schema)) in self.labels.labels.iter().enumerate() {
writeln!(f, "Label {}: {}", schema.name, label)?;
for (pos, (_label, schema)) in self.labels.labels.iter().enumerate() {
writeln!(f, "Label: {}, TableId:{}", schema.name, schema.table.table_id)?;
let columns = schema.table.columns().iter().map(|x| &x.col_name).join(", ");
writeln!(f, " Columns: {columns}")?;

write!(
writeln!(
f,
" Indexes: {}",
schema
.table
.indexes
.iter()
.map(|x| PrintIndex::new(x, schema.table))
.join(", ")
)?;

write!(
f,
" Constraints: {}",
schema
.table
.constraints
.iter()
.map(|x| {
match &x.data {
ConstraintData::Unique(idx) => format!(
"Unique({})",
"{}: Unique({})",
PrintName::constraint(x.constraint_id, &x.constraint_name),
idx.columns
.iter()
.map(|x| {
Expand Down

0 comments on commit f8700bc

Please sign in to comment.