Skip to content

Commit 2f2c08b

Browse files
authored
fix: proper postgres unit quotes (#4537)
1 parent 28fe4a5 commit 2f2c08b

File tree

3 files changed

+38
-15
lines changed

3 files changed

+38
-15
lines changed

prqlc/prqlc/src/sql/dialect.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -167,7 +167,7 @@ pub(super) trait DialectHandler: Any + Debug {
167167
}
168168

169169
/// Whether or not intervals such as `INTERVAL 1 HOUR` require quotes like
170-
/// `INTERVAL '1' HOUR`
170+
/// `INTERVAL '1 HOUR'`
171171
fn requires_quotes_intervals(&self) -> bool {
172172
false
173173
}

prqlc/prqlc/src/sql/gen_expr.rs

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -399,20 +399,29 @@ pub(super) fn translate_literal(l: Literal, ctx: &Context) -> Result<sql_ast::Ex
399399
)))
400400
}
401401
};
402-
let value = if ctx.dialect.requires_quotes_intervals() {
403-
Box::new(sql_ast::Expr::Value(Value::SingleQuotedString(
404-
vau.n.to_string(),
405-
)))
402+
if ctx.dialect.requires_quotes_intervals() {
403+
//postgres requires quotes around number and unit together eg '3 WEEK'
404+
let value = Box::new(sql_ast::Expr::Value(Value::SingleQuotedString(format!(
405+
"{} {}",
406+
vau.n, sql_parser_datetime
407+
))));
408+
sql_ast::Expr::Interval(sqlparser::ast::Interval {
409+
value,
410+
leading_field: None, //set to none since field is now contained in string
411+
leading_precision: None,
412+
last_field: None,
413+
fractional_seconds_precision: None,
414+
})
406415
} else {
407-
Box::new(translate_literal(Literal::Integer(vau.n), ctx)?)
408-
};
409-
sql_ast::Expr::Interval(sqlparser::ast::Interval {
410-
value,
411-
leading_field: Some(sql_parser_datetime),
412-
leading_precision: None,
413-
last_field: None,
414-
fractional_seconds_precision: None,
415-
})
416+
let value = Box::new(translate_literal(Literal::Integer(vau.n), ctx)?);
417+
sql_ast::Expr::Interval(sqlparser::ast::Interval {
418+
value,
419+
leading_field: Some(sql_parser_datetime),
420+
leading_precision: None,
421+
last_field: None,
422+
fractional_seconds_precision: None,
423+
})
424+
}
416425
}
417426
})
418427
}

prqlc/prqlc/tests/integration/sql.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1265,7 +1265,21 @@ fn test_interval() {
12651265
assert_snapshot!((compile(query).unwrap()), @r###"
12661266
SELECT
12671267
*,
1268-
"start" + INTERVAL '10' DAY AS first_check_in
1268+
"start" + INTERVAL '10 DAY' AS first_check_in
1269+
FROM
1270+
projects
1271+
"###);
1272+
1273+
let query = r###"
1274+
prql target:sql.glaredb
1275+
1276+
from projects
1277+
derive first_check_in = start + 10days
1278+
"###;
1279+
assert_snapshot!((compile(query).unwrap()), @r###"
1280+
SELECT
1281+
*,
1282+
"start" + INTERVAL '10 DAY' AS first_check_in
12691283
FROM
12701284
projects
12711285
"###);

0 commit comments

Comments
 (0)