1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
use super::super::items;
use crate::statements::{
SqlBoundInputMode, SqlBoundItem, SqlBoundItemKind, SqlBoundQuery, SqlPinSource,
};
use std::collections::BTreeMap;
pub(super) const MAX_BOUND_ITEMS: usize = 2048;
/// The number of items in a bound, nested queries and subquery pins included.
pub(super) fn size(query: &SqlBoundQuery) -> usize {
query
.items
.iter()
.map(|item| {
let inner = match &item.kind {
SqlBoundItemKind::Query(inner) => size(inner),
_ => 0,
};
let pins: usize = item
.pins
.iter()
.map(|pin| match &pin.source {
SqlPinSource::Query(inner)
| SqlPinSource::ReadQuery(inner)
| SqlPinSource::ArrayQuery(inner) => size(inner),
_ => 0,
})
.sum();
1 + inner + pins
})
.sum()
}
/// Keep a fail-closed summary of oversized CTEs. Capped subqueries contribute no
/// unbounded reads; an opaque item prevents this summary from sizing a later join.
pub(super) fn compact(bound: &SqlBoundQuery, at: (usize, usize)) -> SqlBoundQuery {
fn tables(
query: &SqlBoundQuery,
mut mandatory: bool,
mut blocking_only: bool,
found: &mut BTreeMap<String, (SqlBoundItem, bool)>,
) {
if query.input_mode == SqlBoundInputMode::Skipped {
return;
}
if query.capped {
mandatory = false;
blocking_only = true;
} else if query.input_mode == SqlBoundInputMode::Blocking {
mandatory = true;
blocking_only = false;
}
for item in &query.items {
// Discard pin eligibility separately from reads the pin expression executes.
for pin in &item.pins {
if let SqlPinSource::Query(inner)
| SqlPinSource::ReadQuery(inner)
| SqlPinSource::ArrayQuery(inner) = &pin.source
{
tables(inner, mandatory, blocking_only, found);
}
}
match &item.kind {
SqlBoundItemKind::Table(name) if !blocking_only || mandatory => {
// Only caller-sized values remain valid after the surrounding items are
// removed. Positional aliases cannot keep catalog-key credit once their
// identity is discarded. Repeated reads retain only their common pins.
let pins: Vec<_> = item
.pins
.iter()
.filter(|pin| {
item.column_aliases.is_empty()
&& pin.alternative.is_none()
&& super::super::compact_pins::independent(&pin.source)
})
.cloned()
.collect();
if let Some((previous, blocking)) = found.get_mut(name) {
previous.pins.retain(|pin| pins.contains(pin));
*blocking |= mandatory;
} else {
let mut table = SqlBoundItem::new(
SqlBoundItemKind::Table(name.clone()),
None,
(item.line, item.column),
);
table.pins = pins;
found.insert(name.clone(), (table, mandatory));
}
}
SqlBoundItemKind::Query(inner) => tables(inner, mandatory, blocking_only, found),
_ => {}
}
}
}
let mut found = BTreeMap::new();
tables(bound, false, false, &mut found);
let mut items: Vec<_> = found
.into_values()
.map(|(item, mandatory)| {
if !mandatory {
return item;
}
let at = (item.line, item.column);
SqlBoundItem::new(
SqlBoundItemKind::Query(SqlBoundQuery {
input_mode: SqlBoundInputMode::Blocking,
capped: false,
items: vec![item],
outputs: Vec::new(),
group_constant: false,
group_keys: Vec::new(),
catalog_limit: None,
}),
None,
at,
)
})
.collect();
items.push(items::opaque(at));
SqlBoundQuery {
input_mode: match bound.input_mode {
// The summary drops both the modified-target projection and the insert-source identity.
SqlBoundInputMode::Returning | SqlBoundInputMode::ReturningSource => {
SqlBoundInputMode::Streaming
}
mode => mode,
},
outputs: bound
.outputs
.iter()
.cloned()
.map(|mut output| {
// Compaction changes FROM ordinals; syntactic scalar values survive, column lineage does not.
output.source_column = None;
output.recursive_domain = false;
output.array_aggregate_column = None;
output
})
.collect(),
capped: bound.capped,
// Compaction renumbers FROM items, so item indexes in group keys no longer apply.
group_constant: false,
group_keys: Vec::new(),
items,
catalog_limit: bound.catalog_limit.clone(),
}
}