MCPcopy Create free account
hub / github.com/PRQL/prql / prune_inputs

Function prune_inputs

prqlc/prqlc/src/sql/pq/preprocess.rs:59–94  ·  view source on GitHub ↗

Removes unused relation inputs

(
    mut pipeline: Vec<SqlTransform>,
    ctx: &mut Context,
)

Source from the content-addressed store, hash-verified

57
58/// Removes unused relation inputs
59pub(in crate::sql) fn prune_inputs(
60 mut pipeline: Vec<SqlTransform>,
61 ctx: &mut Context,
62) -> Result<Vec<SqlTransform>> {
63 use SqlTransform::Super;
64
65 let mut used_cids = HashSet::new();
66
67 let mut res = Vec::new();
68 while let Some(mut transform) = pipeline.pop() {
69 // collect cids (special case for Join & From)
70 match transform {
71 SqlTransform::Join { ref filter, .. } => {
72 used_cids.extend(CidCollector::collect(filter.clone()));
73 }
74 SqlTransform::From(_) => {}
75 Super(t) => {
76 let (t, cids) = CidCollector::collect_t(t);
77 used_cids.extend(cids);
78 transform = Super(t);
79 }
80 _ => unreachable!(),
81 }
82
83 // prune unused inputs
84 if let SqlTransform::From(with) | SqlTransform::Join { with, .. } = &mut transform {
85 let relation = ctx.anchor.relation_instances.get_mut(with).unwrap();
86 (relation.table_ref.columns).retain(|(_, cid)| used_cids.contains(cid));
87 }
88
89 res.push(transform);
90 }
91
92 res.reverse();
93 Ok(res)
94}
95
96pub(in crate::sql) fn wrap(pipe: Vec<Transform>, ctx: &mut Context) -> Result<Vec<SqlTransform>> {
97 // We map From and Join into SqlTransforms, because we need to change their RIIds.

Callers 1

preprocessFunction · 0.85

Calls 3

popMethod · 0.80
get_mutMethod · 0.80
pushMethod · 0.80

Tested by

no test coverage detected