Skip to content

Commit 580d26b

Browse files
committed
store: Rename and simplify children_type_d
For queries of type D, there can only be one child per parent, not many. Rename the function to `child_type_d` to reflect that and simplify the SQL query.
1 parent 068e24b commit 580d26b

File tree

1 file changed

+10
-18
lines changed

1 file changed

+10
-18
lines changed

store/postgres/src/relational_queries.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -1575,40 +1575,32 @@ impl<'a> FilterWindow<'a> {
15751575
Ok(())
15761576
}
15771577

1578-
fn children_type_d(
1578+
fn child_type_d(
15791579
&self,
15801580
child_ids: &Vec<String>,
15811581
limit: ParentLimit<'_>,
15821582
block: BlockNumber,
15831583
out: &mut AstPass<Pg>,
15841584
) -> QueryResult<()> {
15851585
// Generate
1586-
// select c.*, p.id as parent_id
1587-
// from rows from (unnest({parent_ids}), unnest({child_ids})) as p(id, child_id)
1588-
// cross join lateral
1589-
// (select *
1590-
// from children c
1591-
// where c.id = p.child_id
1592-
// and .. other conditions on c ..
1593-
// order by c.{sort_key}
1594-
// limit {first} offset {skip}) c
1595-
// order by c.{sort_key}
1586+
// from rows from (unnest({parent_ids}), unnest({child_ids})) as p(id, child_id),
1587+
// children c
1588+
// where c.id = p.child_id
1589+
// and .. other conditions on c ..
15961590

1597-
out.push_sql("\n from unnest(");
1591+
out.push_sql("\n from rows from (unnest(");
15981592
out.push_bind_param::<Array<Text>, _>(&self.ids)?;
1599-
out.push_sql(",");
1593+
out.push_sql("), unnest(");
16001594
self.table.primary_key().bind_ids(&child_ids, out)?;
1601-
out.push_sql(") as p(id, child_id)");
1602-
out.push_sql(" cross join lateral (select * from ");
1595+
out.push_sql(")) as p(id, child_id), ");
16031596
out.push_sql(self.table.qualified_name.as_str());
16041597
out.push_sql(" c where ");
16051598
BlockRangeContainsClause::new("c.", block).walk_ast(out.reborrow())?;
16061599
limit.filter(out);
16071600
out.push_sql(" and ");
16081601
out.push_sql("c.id = p.child_id");
16091602
self.and_filter(out.reborrow())?;
1610-
limit.restrict(out)?;
1611-
out.push_sql(") c");
1603+
limit.single_limit(self.ids.len(), out);
16121604
Ok(())
16131605
}
16141606

@@ -1630,7 +1622,7 @@ impl<'a> FilterWindow<'a> {
16301622
self.children_type_c(child_ids, limit, block, &mut out)
16311623
}
16321624
TableLink::Parent(ParentIds::Scalar(child_ids)) => {
1633-
self.children_type_d(child_ids, limit, block, &mut out)
1625+
self.child_type_d(child_ids, limit, block, &mut out)
16341626
}
16351627
}
16361628
}

0 commit comments

Comments
 (0)