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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
use hamelin_lib::{
antlr::hamelinparser::{ExplodeCommandContext, ExplodeCommandContextAttrs},
err::{TranslationError, TranslationErrors},
sql::{
expression::{
identifier::{CompoundIdentifier, Identifier, SimpleIdentifier},
literal::{BooleanLiteral, ColumnReference},
Leaf, SQLExpression,
},
projection_builder::ProjectionBuilder,
query::{
Join, JoinClause, JoinType, SQLQuery, SubQuery, TableAlias, TableExpression,
TableFunctionApply,
},
},
types::Type,
};
use crate::ast::{assignment_clause::HamelinAssignmentClause, pipeline::HamelinPipeline};
use crate::env::Environment;
use crate::translation::PendingQuery;
pub fn translate(
ctx: &ExplodeCommandContext<'static>,
pipeline: &HamelinPipeline,
previous_command: &PendingQuery,
) -> Result<PendingQuery, TranslationErrors> {
let assignment_ctx = TranslationErrors::expect(ctx, ctx.assignmentClause())?;
let (identifier, translation) = HamelinAssignmentClause::new(
assignment_ctx.clone(),
pipeline
.context
.default_expression_translation_context(&previous_command.env),
)
.to_sql()?;
let left_alias = SimpleIdentifier::new("_left");
let right_alias = SimpleIdentifier::new("_right");
let right_nested_alias = SimpleIdentifier::new("_nested");
let left_sql = previous_command.query.clone();
let left_query = SubQuery::new(left_sql.clone().into()).alias(left_alias.clone().into());
match translation.typ {
Type::Array(array) => {
/*
This is the alias for the right hand side of the join. We have to be careful in how
we construct this. If the element type is struct, the database will automatically
"lift" each subfield into a named field in the result set. If the element type is
not a struct, we have to give that inner field a name, so that we can then refer to
it later. We just pick "_nested" so we can find it later, when building the projections.
*/
let table_alias = match array.element_type.as_ref() {
Type::Struct(_) => TableAlias::new(right_alias.clone().into()),
_ => TableAlias::new(right_alias.clone().into())
.with_field_alias(right_nested_alias.clone()),
};
let unnest = TableFunctionApply::new("unnest".to_string())
.with_argument(translation.sql.clone())
.with_alias(table_alias.into());
let from: TableExpression = if left_sql == SQLQuery::default() {
unnest.into()
} else {
Join::new(left_query.into())
.with_clause(JoinClause {
table: unnest.into(),
join_type: JoinType::LEFT,
condition: Some(BooleanLiteral::new(true).into()),
})
.into()
};
let mut pb = ProjectionBuilder::default();
// Check if this is a canonical explode (identifier already exists in schema)
// Use lookup_local to properly handle compound identifiers like data.arr
let is_canonical = previous_command.env.lookup_local(&identifier).is_some();
// Build the exploded column expression
let (exploded_expr, exploded_type): (SQLExpression, Type) =
match array.element_type.as_ref() {
Type::Struct(srct) => {
// Generate the cast expression that represents the inner struct type.
let cast: SQLExpression =
ProjectionBuilder::deep_initialize_from_struct(None, srct.clone())
.build_cast()
.map_err(|e| TranslationError::wrap_box(ctx, e.into()))?
.into();
// Prefix any inner references with the "_right" alias.
let cast_with_prefix = cast.map_leaves(|leaf| match leaf {
Leaf::ColumnReference(column_reference) => ColumnReference::new(
column_reference
.identifier
.prefix_from_simples(&[right_alias.clone()])
.into(),
)
.into(),
l => l.into(),
});
(cast_with_prefix, srct.clone().into())
}
t => {
// No fancy business needed for the non-struct case. Just column ref _right.
let right_nested_identifier: Identifier = right_nested_alias.clone().into();
let cr = ColumnReference::new(
right_nested_identifier
.prefix_from_simples(&[right_alias])
.into(),
);
(cr.into(), t.clone())
}
};
// For compound identifiers (like data.arr or str.exploded), we need to handle
// the parent struct specially: initialize it as a Node with column references
// so we can modify a field inside while preserving other fields.
let target_root: Option<SimpleIdentifier> = match &identifier {
Identifier::Compound(c) => Some(c.first().clone()),
Identifier::Simple(_) => None,
};
// Helper to bind a field from the environment, handling the case where
// it's the parent struct of our compound target identifier
let bind_env_field = |pb: &mut ProjectionBuilder, k: &SimpleIdentifier, v: &Type| {
let ident: Identifier = k.clone().into();
if target_root.as_ref() == Some(k) {
// This is the parent struct of our compound target.
// Initialize it as a Node with _left-prefixed column references
// so we can later bind into it without losing existing fields.
if let Type::Struct(struct_type) = v {
let prefixed_name: Identifier = CompoundIdentifier::from_idents(&[
left_alias.clone().into(),
k.clone().into(),
])
.into();
let initialized = ProjectionBuilder::deep_initialize_from_struct(
Some(prefixed_name),
struct_type.clone(),
);
for (nested_k, nested_v) in initialized.build_hamelin_type().fields.iter() {
let nested_ident: Identifier = CompoundIdentifier::from_idents(&[
k.clone().into(),
nested_k.clone().into(),
])
.into();
let nested_cr = ColumnReference::new(
CompoundIdentifier::from_idents(&[
left_alias.clone().into(),
k.clone().into(),
nested_k.clone().into(),
])
.into(),
);
pb.bind(nested_ident, nested_cr.into(), nested_v.clone());
}
}
} else {
// Regular field: bind with _left prefix
let cr = ColumnReference::new(
ident.prefix_from_simples(&[left_alias.clone()]).into(),
);
pb.bind(ident, cr.into(), v.clone());
}
};
if is_canonical {
// Canonical: bind old fields first (preserving order), then overwrite in place
for (k, v) in previous_command.env.fields.fields.iter() {
bind_env_field(&mut pb, k, v);
}
pb.bind(identifier, exploded_expr, exploded_type);
} else {
// Non-canonical: new fields are prepended in Hamelin
// First bind the new exploded field
pb.bind(identifier.clone(), exploded_expr, exploded_type);
// For compound identifiers, bind remaining fields from the parent struct
// AFTER the new field so they come after in the struct
if let Some(ref root) = target_root {
if let Some(Type::Struct(struct_type)) =
previous_command.env.fields.fields.get(root)
{
for (nested_k, nested_v) in struct_type.fields.iter() {
let nested_ident: Identifier = CompoundIdentifier::from_idents(&[
root.clone().into(),
nested_k.clone().into(),
])
.into();
// Skip if this is the field we just bound (canonical-like case within struct)
if nested_ident == identifier {
continue;
}
let nested_cr = ColumnReference::new(
CompoundIdentifier::from_idents(&[
left_alias.clone().into(),
root.clone().into(),
nested_k.clone().into(),
])
.into(),
);
pb.bind(nested_ident, nested_cr.into(), nested_v.clone());
}
}
}
// Then bind the rest of the environment fields
for (k, v) in previous_command.env.fields.fields.iter() {
// Skip the target root - we already handled its fields above
if target_root.as_ref() == Some(k) {
continue;
}
bind_env_field(&mut pb, k, v);
}
}
let projections = pb
.clone()
.build_projections()
.map_err(|e| TranslationError::wrap_box(ctx, e.into()))?;
let query = SQLQuery::default().from(from).select(projections);
let merged_env = Environment::new(pb.build_hamelin_type());
Ok(PendingQuery::new(query, merged_env))
}
t => {
return Err(TranslationError::msg(
assignment_ctx.as_ref(),
&format!("Unsupported type {} for explode command. Must be array.", t),
)
.single())
}
}
}