prqlc 0.13.11

PRQL is a modern language for transforming data — a simple, powerful, pipelined SQL replacement.
Documentation
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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use anyhow::Result;
use connector_arrow::arrow::record_batch::RecordBatch;
use glob::glob;
use prqlc::sql::Dialect;

use super::protocol::DbProtocol;

static DATA_FILE_ROOT_KEYWORD: &str = "data_file_root";

pub(crate) trait DbTestRunner: Send {
    fn dialect(&self) -> Dialect;
    fn data_file_root(&self) -> &str;
    fn import_csv(&mut self, csv_path: &str, table_name: &str);
    fn modify_ddl(&self, sql: String) -> String;
    fn query(&mut self, prql: &str) -> Result<RecordBatch> {
        let prql = prql.replace(DATA_FILE_ROOT_KEYWORD, self.data_file_root());
        let options =
            prqlc::Options::default().with_target(prqlc::Target::Sql(Some(self.dialect())));
        let sql = prqlc::compile(&prql, &options)?;

        self.protocol().query(&sql)
    }
    fn protocol(&mut self) -> &mut dyn DbProtocol;
    fn setup(&mut self) {
        let schema = include_str!("../data/chinook/schema.sql");
        let statements: Vec<String> = schema
            .split(';')
            .map(str::trim)
            .filter(|s| !s.is_empty())
            .map(|s| self.modify_ddl(s.to_string()))
            .collect();

        for statement in statements {
            self.protocol().execute(&statement).unwrap();
        }

        for file_path in glob("tests/integration/data/chinook/*.csv").unwrap() {
            let file_path = file_path.unwrap();
            let stem = file_path.file_stem().unwrap().to_str().unwrap();
            let path = format!("{}/{}.csv", self.data_file_root(), stem);
            self.import_csv(&path, stem);
        }
    }
}

pub(crate) struct DuckDbTestRunner {
    protocol: connector_arrow::duckdb::DuckDBConnection,
    data_file_root: String,
}

impl DuckDbTestRunner {
    pub(crate) fn new(data_file_root: String) -> Self {
        let conn = ::duckdb::Connection::open_in_memory().unwrap();
        let conn_ar = connector_arrow::duckdb::DuckDBConnection::new(conn);
        Self {
            protocol: conn_ar,
            data_file_root,
        }
    }
}

impl DbTestRunner for DuckDbTestRunner {
    fn dialect(&self) -> Dialect {
        Dialect::DuckDb
    }

    fn protocol(&mut self) -> &mut dyn DbProtocol {
        &mut self.protocol
    }

    fn data_file_root(&self) -> &str {
        &self.data_file_root
    }

    fn import_csv(&mut self, csv_path: &str, table_name: &str) {
        self.protocol
            .execute(&format!(
                "COPY {table_name} FROM '{csv_path}' (AUTO_DETECT TRUE);"
            ))
            .unwrap();
    }

    fn modify_ddl(&self, sql: String) -> String {
        sql.replace("FLOAT", "DOUBLE")
    }
}

pub(crate) struct SQLiteTestRunner {
    protocol: connector_arrow::rusqlite::SQLiteConnection,
    data_file_root: String,
}

impl SQLiteTestRunner {
    pub(crate) fn new(data_file_root: String) -> Self {
        let conn = rusqlite::Connection::open_in_memory().unwrap();
        let conn_ar = connector_arrow::rusqlite::SQLiteConnection::new(conn);
        Self {
            protocol: conn_ar,
            data_file_root,
        }
    }
}

impl DbTestRunner for SQLiteTestRunner {
    fn dialect(&self) -> Dialect {
        Dialect::SQLite
    }

    fn protocol(&mut self) -> &mut dyn DbProtocol {
        &mut self.protocol
    }

    fn data_file_root(&self) -> &str {
        &self.data_file_root
    }

    fn import_csv(&mut self, csv_path: &str, table_name: &str) {
        let mut reader = csv::ReaderBuilder::new()
            .has_headers(true)
            .from_path(csv_path)
            .unwrap();
        let headers = reader
            .headers()
            .unwrap()
            .iter()
            .map(|s| s.to_string())
            .collect::<Vec<String>>();
        for result in reader.records() {
            let r = result.unwrap();
            let q = format!(
                "INSERT INTO {table_name} ({}) VALUES ({})",
                headers.join(","),
                r.iter()
                    .map(|s| if s.is_empty() {
                        "null".to_string()
                    } else {
                        format!("\"{}\"", s.replace('"', "\"\""))
                    })
                    .collect::<Vec<_>>()
                    .join(",")
            );
            self.protocol.execute(&q).unwrap();
        }
    }

    fn modify_ddl(&self, sql: String) -> String {
        sql.replace("TIMESTAMP", "TEXT") // timestamps in chinook are stored as ISO8601
    }
}

#[cfg(feature = "test-dbs-external")]
pub(crate) use self::external::*;

#[cfg(feature = "test-dbs-external")]
pub(crate) mod external {
    use super::*;
    use std::fs;

    pub(crate) struct PostgresTestRunner {
        protocol: connector_arrow::postgres::PostgresConnection,
        data_file_root: String,
    }

    impl PostgresTestRunner {
        pub(crate) fn new(url: &str, data_file_root: String) -> Self {
            use connector_arrow::postgres::PostgresConnection;
            let client = ::postgres::Client::connect(url, ::postgres::NoTls).unwrap();
            Self {
                protocol: PostgresConnection::new(client),
                data_file_root,
            }
        }
    }

    impl DbTestRunner for PostgresTestRunner {
        fn dialect(&self) -> Dialect {
            Dialect::Postgres
        }

        fn protocol(&mut self) -> &mut dyn DbProtocol {
            &mut self.protocol
        }

        fn data_file_root(&self) -> &str {
            &self.data_file_root
        }

        fn import_csv(&mut self, csv_path: &str, table_name: &str) {
            self.protocol
                .execute(&format!(
                    "COPY {table_name} FROM '{csv_path}' DELIMITER ',' CSV HEADER;"
                ))
                .unwrap();
        }

        fn modify_ddl(&self, sql: String) -> String {
            sql.replace("FLOAT", "DOUBLE PRECISION")
        }
    }

    pub(crate) struct MySqlTestRunner {
        protocol: connector_arrow::mysql::MySQLConnection<::mysql::Conn>,
        data_file_root: String,
    }

    impl MySqlTestRunner {
        pub(crate) fn new(url: &str, data_file_root: String) -> Self {
            let conn = ::mysql::Conn::new(url)
                .unwrap_or_else(|e| panic!("Failed to connect to {url}:\n{e}"));
            Self {
                protocol: connector_arrow::mysql::MySQLConnection::<::mysql::Conn>::new(conn),
                data_file_root,
            }
        }
    }

    impl DbTestRunner for MySqlTestRunner {
        fn dialect(&self) -> Dialect {
            Dialect::MySql
        }

        fn protocol(&mut self) -> &mut dyn DbProtocol {
            &mut self.protocol
        }

        fn data_file_root(&self) -> &str {
            &self.data_file_root
        }

        fn import_csv(&mut self, csv_path: &str, table_name: &str) {
            // MySQL-specific CSV import logic
            let csv_path_binding = std::path::PathBuf::from(csv_path);
            let local_csv_path = format!(
                "tests/integration/data/chinook/{}",
                csv_path_binding.file_name().unwrap().to_str().unwrap()
            );
            let local_old_path = std::path::PathBuf::from(local_csv_path);
            let mut local_new_path = local_old_path.clone();
            local_new_path.pop();
            local_new_path.push(format!("{table_name}.my.csv").as_str());
            let file_content = fs::read_to_string(local_old_path)
                .unwrap()
                .replace(",,", ",\\N,")
                .replace(",\n", ",\\N\n");
            fs::write(&local_new_path, file_content).unwrap();
            self.protocol.execute(
                &format!(
                    "LOAD DATA INFILE '{}' INTO TABLE {table_name} FIELDS TERMINATED BY ',' OPTIONALLY ENCLOSED BY '\"' LINES TERMINATED BY '\n' IGNORE 1 ROWS;",
                    &csv_path_binding.parent().unwrap().join(local_new_path.file_name().unwrap()).to_str().unwrap()
                )
            ).unwrap();
            fs::remove_file(&local_new_path).unwrap();
        }

        fn modify_ddl(&self, sql: String) -> String {
            sql.replace("TIMESTAMP", "DATETIME")
        }
    }

    pub(crate) struct ClickHouseTestRunner {
        protocol: connector_arrow::mysql::MySQLConnection<::mysql::Conn>,
        data_file_root: String,
    }

    impl ClickHouseTestRunner {
        #[allow(dead_code)]
        pub(crate) fn new(url: &str, data_file_root: String) -> Self {
            Self {
                protocol: connector_arrow::mysql::MySQLConnection::<::mysql::Conn>::new(
                    ::mysql::Conn::new(url)
                        .unwrap_or_else(|e| panic!("Failed to connect to {url}:\n{e}")),
                ),
                data_file_root,
            }
        }
    }

    impl DbTestRunner for ClickHouseTestRunner {
        fn dialect(&self) -> Dialect {
            Dialect::ClickHouse
        }

        fn protocol(&mut self) -> &mut dyn DbProtocol {
            &mut self.protocol
        }

        fn data_file_root(&self) -> &str {
            &self.data_file_root
        }

        fn import_csv(&mut self, csv_path: &str, table_name: &str) {
            self.protocol
                .execute(&format!(
                    "INSERT INTO {table_name} SELECT * FROM file('{csv_path}')"
                ))
                .unwrap();
        }

        fn modify_ddl(&self, sql: String) -> String {
            use regex::Regex;
            let re = Regex::new(r"(?s)\)$").unwrap();
            re.replace(&sql, r") ENGINE = Memory")
                .replace("TIMESTAMP", "DATETIME64")
                .replace("FLOAT", "DOUBLE")
                .replace("VARCHAR(255)", "Nullable(String)")
                .to_string()
        }
    }

    pub(crate) struct MsSqlTestRunner {
        protocol: connector_arrow::tiberius::TiberiusConnection<
            tokio_util::compat::Compat<tokio::net::TcpStream>,
        >,
        data_file_root: String,
    }

    impl MsSqlTestRunner {
        pub(crate) fn new(data_file_root: String) -> Self {
            use std::sync::Arc;
            use tokio_util::compat::TokioAsyncWriteCompatExt;

            let mut config = tiberius::Config::new();
            config.host("localhost");
            config.port(1433);
            config.trust_cert();
            config.authentication(tiberius::AuthMethod::sql_server("sa", "Wordpass123##"));

            let rt = Arc::new(
                tokio::runtime::Builder::new_current_thread()
                    .enable_all()
                    .build()
                    .unwrap(),
            );

            let client = rt
                .block_on(async {
                    let tcp = tokio::net::TcpStream::connect(config.get_addr()).await?;
                    tcp.set_nodelay(true).unwrap();
                    tiberius::Client::connect(config, tcp.compat_write()).await
                })
                .unwrap();

            Self {
                protocol: connector_arrow::tiberius::TiberiusConnection::new(rt, client),
                data_file_root,
            }
        }
    }

    impl DbTestRunner for MsSqlTestRunner {
        fn dialect(&self) -> Dialect {
            Dialect::MsSql
        }

        fn protocol(&mut self) -> &mut dyn DbProtocol {
            &mut self.protocol
        }

        fn data_file_root(&self) -> &str {
            &self.data_file_root
        }

        fn import_csv(&mut self, csv_path: &str, table_name: &str) {
            self.protocol.execute(&format!(
                "BULK INSERT {table_name} FROM '{csv_path}' WITH (FIRSTROW = 2, FIELDTERMINATOR = ',', ROWTERMINATOR = '\n', TABLOCK, FORMAT = 'CSV', CODEPAGE = 'RAW');"
            )).unwrap();
        }

        fn modify_ddl(&self, sql: String) -> String {
            sql.replace("TIMESTAMP", "DATETIME")
                .replace("FLOAT", "FLOAT(53)")
                .replace(" AS TEXT", " AS VARCHAR")
        }
    }

    #[allow(dead_code)]
    pub(crate) struct GlareDbTestRunner {
        protocol: connector_arrow::postgres::PostgresConnection,
        data_file_root: String,
    }

    impl GlareDbTestRunner {
        #[allow(dead_code)]
        pub(crate) fn new(url: &str, data_file_root: String) -> Self {
            use connector_arrow::postgres::PostgresConnection;
            let client = ::postgres::Client::connect(url, ::postgres::NoTls).unwrap();
            Self {
                protocol: PostgresConnection::new(client),
                data_file_root,
            }
        }
    }

    impl DbTestRunner for GlareDbTestRunner {
        fn dialect(&self) -> Dialect {
            Dialect::GlareDb
        }

        fn protocol(&mut self) -> &mut dyn DbProtocol {
            &mut self.protocol
        }

        fn data_file_root(&self) -> &str {
            &self.data_file_root
        }

        fn import_csv(&mut self, csv_path: &str, table_name: &str) {
            self.protocol
                .execute(&format!(
                    "INSERT INTO {table_name} SELECT * FROM '{csv_path}'"
                ))
                .unwrap();
        }

        fn modify_ddl(&self, sql: String) -> String {
            sql.replace("FLOAT", "DOUBLE PRECISION")
        }
    }
}