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
mod distinct;
mod materializer;
mod translator;
mod un_group;

pub use materializer::{materialize, MaterializedFrame};
pub use translator::translate;

use anyhow::Result;

use crate::ast::{Node, Query};
use crate::semantic;

/// Resolve all variable and function calls using SQL stdlib and then translate AST into SQL.
pub fn resolve_and_translate(mut query: Query) -> Result<String> {
    let std_lib = load_std_lib()?;
    let (_, context) = semantic::resolve_names(std_lib, None)?;

    let (nodes, context) = semantic::resolve_names(query.nodes, Some(context))?;

    query.nodes = nodes;
    translate(query, context)
}

pub fn load_std_lib() -> Result<Vec<Node>> {
    use crate::parse;
    let std_lib = include_str!("./stdlib.prql");
    Ok(parse(std_lib)?.nodes)
}