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

Function compile

prqlc/prqlc/src/sql/mod.rs:23–62  ·  view source on GitHub ↗

Translate a PRQL AST into a SQL string.

(query: rq::RelationalQuery, options: &Options)

Source from the content-addressed store, hash-verified

21
22/// Translate a PRQL AST into a SQL string.
23pub fn compile(query: rq::RelationalQuery, options: &Options) -> Result<String> {
24 let crate::Target::Sql(dialect) = options.target;
25 let sql_ast = gen_query::translate_query(query, dialect)?;
26
27 let sql = sql_ast.to_string();
28
29 // formatting
30 let sql = if options.format {
31 let formatted = sqlformat::format(
32 &sql,
33 &sqlformat::QueryParams::default(),
34 &sqlformat::FormatOptions::default(),
35 );
36
37 formatted + "\n"
38 } else {
39 sql
40 };
41
42 debug::log_entry(|| debug::DebugEntryKind::ReprSql(sql.clone()));
43
44 // signature
45 let sql = if options.signature_comment {
46 let pre = if options.format { "\n" } else { " " };
47 let post = if options.format { "\n" } else { "" };
48 let target = dialect
49 .map(|d| format!("target:sql.{d} "))
50 .unwrap_or_default();
51 let signature = format!(
52 "{pre}-- Generated by PRQL compiler version:{} {}(https://prql-lang.org){post}",
53 compiler_version(),
54 target,
55 );
56 sql + &signature
57 } else {
58 sql
59 };
60
61 Ok(sql)
62}
63
64#[derive(Debug)]
65struct Context {

Callers 4

test_end_with_new_lineFunction · 0.70
test_derive_filterFunction · 0.70

Calls 4

translate_queryFunction · 0.85
formatFunction · 0.85
log_entryFunction · 0.85
mapMethod · 0.80

Tested by 4

test_end_with_new_lineFunction · 0.56
test_derive_filterFunction · 0.56