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

Function translate_select_items

prqlc/prqlc/src/sql/gen_projection.rs:136–195  ·  view source on GitHub ↗
(
    cols: Vec<CId>,
    mut excluded: Excluded,
    ctx: &mut Context,
)

Source from the content-addressed store, hash-verified

134}
135
136pub(super) fn translate_select_items(
137 cols: Vec<CId>,
138 mut excluded: Excluded,
139 ctx: &mut Context,
140) -> Result<Vec<SelectItem>> {
141 let mut res: Vec<_> = cols
142 .into_iter()
143 .map(|cid| {
144 let decl = ctx.anchor.column_decls.get(&cid).unwrap();
145
146 let ColumnDecl::RelationColumn(riid, _, RelationColumn::Wildcard) = decl else {
147 // general case
148 return translate_select_item(cid, ctx);
149 };
150
151 // wildcard case
152 let t = &ctx.anchor.relation_instances[riid];
153 let table_name = t.table_ref.name.clone().map(Ident::from_name);
154
155 let ident = translate_ident(table_name, Some("*".to_string()), ctx);
156
157 // excluded columns
158 let opts = (excluded.remove(&cid))
159 .and_then(|excluded| translate_exclude(ctx, excluded))
160 .unwrap_or_default();
161
162 Ok(if ident.len() > 1 {
163 let mut object_name = ident;
164 object_name.pop();
165 SelectItem::QualifiedWildcard(
166 sqlparser::ast::SelectItemQualifiedWildcardKind::ObjectName(ObjectName(
167 object_name
168 .into_iter()
169 .map(sqlparser::ast::ObjectNamePart::Identifier)
170 .collect(),
171 )),
172 opts,
173 )
174 } else {
175 SelectItem::Wildcard(opts)
176 })
177 })
178 .try_collect()?;
179
180 deduplicate_select_items(&mut res);
181
182 if res.is_empty() && !ctx.dialect.supports_zero_columns() {
183 // In some cases, no columns will appear in the projection
184 // for SQL to parse correctly, we inject a `NULL`.
185 // This is not strictly correct and should probably generate an error
186 // instead.
187 // Example: `from x | take 10 | aggregate { count this }`.
188 // Here, first SELECT does not need to emit any columns as we don't need
189 // any since we just count the number of rows.
190 res.push(SelectItem::UnnamedExpr(sql_ast::Expr::Value(
191 sql_ast::Value::Null.into(),
192 )));
193 }

Callers 1

Calls 13

translate_select_itemFunction · 0.85
translate_identFunction · 0.85
translate_excludeFunction · 0.85
deduplicate_select_itemsFunction · 0.85
mapMethod · 0.80
into_iterMethod · 0.80
lenMethod · 0.80
popMethod · 0.80
is_emptyMethod · 0.80
pushMethod · 0.80
getMethod · 0.45
collectMethod · 0.45

Tested by

no test coverage detected