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

Function translate_expr

prqlc/prqlc/src/sql/gen_expr.rs:24–141  ·  view source on GitHub ↗
(expr: rq::Expr, ctx: &mut Context)

Source from the content-addressed store, hash-verified

22use crate::{Error, Result, Span, WithErrorInfo};
23
24pub(super) fn translate_expr(expr: rq::Expr, ctx: &mut Context) -> Result<ExprOrSource> {
25 Ok(match expr.kind {
26 rq::ExprKind::ColumnRef(cid) => translate_cid(cid, ctx)?,
27
28 // Fairly hacky — convert everything to a string, then concat it,
29 // then convert to sql_ast::Expr. We can't use the `Item::sql_ast::Expr` code above
30 // since we don't want to intersperse with spaces.
31 rq::ExprKind::SString(s_string_items) => {
32 let text = translate_sstring(s_string_items, ctx)?;
33
34 ExprOrSource::Source(SourceExpr {
35 text,
36 binding_strength: 100,
37 window_frame: false,
38 })
39 }
40 rq::ExprKind::Param(id) => ExprOrSource::Source(SourceExpr {
41 text: format!("${id}"),
42 binding_strength: 100,
43 window_frame: false,
44 }),
45 rq::ExprKind::Literal(l) => translate_literal(l, ctx)?.into(),
46 rq::ExprKind::Case(mut cases) => {
47 let default = cases
48 .last()
49 .filter(|last| {
50 matches!(
51 last.condition.kind,
52 rq::ExprKind::Literal(Literal::Boolean(true))
53 )
54 })
55 .map(|def| translate_expr(def.value.clone(), ctx))
56 .transpose()?
57 .map(|x| x.into_ast());
58
59 if default.is_some() {
60 cases.pop();
61 }
62
63 let else_result = default
64 .or(Some(sql_ast::Expr::Value(Value::Null.into())))
65 .map(Box::new);
66
67 let conditions = cases
68 .into_iter()
69 .map(|case| -> Result<_> {
70 let condition = translate_expr(case.condition, ctx)?.into_ast();
71 let result = translate_expr(case.value, ctx)?.into_ast();
72 Ok(sql_ast::CaseWhen { condition, result })
73 })
74 .try_collect()?;
75
76 sql_ast::Expr::Case {
77 case_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
78 end_token: sqlparser::ast::helpers::attached_token::AttachedToken::empty(),
79 operand: None,
80 conditions,
81 else_result,

Callers 10

process_array_inFunction · 0.85
process_date_truncFunction · 0.85
process_concatFunction · 0.85
translate_cidFunction · 0.85
translate_sstringFunction · 0.85
fetch_of_i64Function · 0.85
translate_operandFunction · 0.85
translate_joinFunction · 0.85
filter_of_conditionsFunction · 0.85

Calls 15

translate_cidFunction · 0.85
translate_sstringFunction · 0.85
translate_literalFunction · 0.85
LiteralEnum · 0.85
process_nullFunction · 0.85
operator_from_nameFunction · 0.85
process_concatFunction · 0.85
process_array_inFunction · 0.85
process_date_to_textFunction · 0.85
process_date_truncFunction · 0.85
try_into_betweenFunction · 0.85

Tested by

no test coverage detected