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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
//! SQL support (Polars-backed).
//!
//! This module is implemented as a thin wrapper around Polars SQL: it compiles SQL into a Polars
//! logical plan (a `LazyFrame`) and returns a [`crate::pipeline::DataFrame`].
//!
//! Design goals:
//! - Keep public signatures in crate types (no Polars types in signatures)
//! - Preserve underlying engine errors via `IngestionError::Engine { source, .. }`
use crate;
use crateDataFrame;
use SQLContext;
/// Default single-table name used by [`query`].
pub const DEFAULT_TABLE: &str = "df";
/// Execute a SQL query against a single [`DataFrame`].
///
/// The input is registered as the table [`DEFAULT_TABLE`] (i.e. `df`), so callers should write
/// queries like: `SELECT ... FROM df WHERE ...`.
///
/// # Example
///
/// ```no_run
/// use rust_data_processing::pipeline::DataFrame;
/// use rust_data_processing::sql;
/// use rust_data_processing::types::{DataSet, DataType, Field, Schema, Value};
///
/// # fn main() -> Result<(), rust_data_processing::IngestionError> {
/// let ds = DataSet::new(
/// Schema::new(vec![
/// Field::new("id", DataType::Int64),
/// Field::new("active", DataType::Bool),
/// ]),
/// vec![
/// vec![Value::Int64(1), Value::Bool(true)],
/// vec![Value::Int64(2), Value::Bool(false)],
/// ],
/// );
///
/// let out = sql::query(
/// &DataFrame::from_dataset(&ds)?,
/// "SELECT id FROM df WHERE active = TRUE ORDER BY id",
/// )?
/// .collect()?;
///
/// assert_eq!(out.row_count(), 1);
/// # Ok(())
/// # }
/// ```
/// A SQL execution context that can register multiple tables and execute queries.
///
/// This is the preferred entrypoint for JOINs across multiple [`DataFrame`]s.
///
/// # Example (JOIN)
///
/// ```no_run
/// use rust_data_processing::pipeline::DataFrame;
/// use rust_data_processing::sql;
/// use rust_data_processing::types::{DataSet, DataType, Field, Schema, Value};
///
/// # fn main() -> Result<(), rust_data_processing::IngestionError> {
/// let people = DataSet::new(
/// Schema::new(vec![
/// Field::new("id", DataType::Int64),
/// Field::new("name", DataType::Utf8),
/// ]),
/// vec![
/// vec![Value::Int64(1), Value::Utf8("Ada".to_string())],
/// vec![Value::Int64(2), Value::Utf8("Grace".to_string())],
/// ],
/// );
/// let scores = DataSet::new(
/// Schema::new(vec![
/// Field::new("id", DataType::Int64),
/// Field::new("score", DataType::Float64),
/// ]),
/// vec![vec![Value::Int64(1), Value::Float64(98.5)]],
/// );
///
/// let mut ctx = sql::Context::new();
/// ctx.register("people", &DataFrame::from_dataset(&people)?)?;
/// ctx.register("scores", &DataFrame::from_dataset(&scores)?)?;
///
/// let out = ctx
/// .execute("SELECT p.id, p.name, s.score FROM people p JOIN scores s ON p.id = s.id")?
/// .collect()?;
///
/// assert_eq!(out.row_count(), 1);
/// # Ok(())
/// # }
/// ```