datapress-datafusion 0.5.3

Apache Arrow + DataFusion-backed implementation of the datapress Parquet/Delta dataset HTTP server.
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
//! End-to-end tests for the PostgreSQL wire-protocol server.
//!
//! These exercise the real `serve_pgwire` entry point over a loopback TCP
//! socket using the `tokio-postgres` client — the same driver Power BI /
//! psql / JDBC clients speak underneath. Two scenarios are covered:
//!
//!   1. Cleartext password auth (no TLS): simple queries, typed column
//!      round-trips, an extended-protocol prepared statement with `$1`,
//!      session-maintenance statements (`DISCARD ALL` & friends) that pooling
//!      drivers issue on connection reset, and a proof that the *wrong*
//!      password is rejected.
//!   2. Password auth over TLS with a self-signed cert generated at runtime.
//!
//! The whole file is gated on the `pgwire` feature so the default build (and
//! the crates that don't opt in) never pull in the server or its test-only
//! client dependencies.
#![cfg(feature = "pgwire")]

use std::net::{IpAddr, TcpListener};
use std::sync::Arc;
use std::time::Duration;

use arrow::array::{Int64Array, StringArray};
use arrow::datatypes::{DataType, Field, Schema};
use arrow::record_batch::RecordBatch;
use parquet::arrow::ArrowWriter;
use tempfile::TempDir;

use datapress_core::config::{
    AppConfig, DatasetConfig, IndexConfig, PgwireConfig, ServerConfig, SourceConfig, SourceKind,
};
use datapress_datafusion::pgwire::{serve_pgwire, spawn_pgwire};
use datapress_datafusion::store::Store;

// ---------------------------------------------------------------------------
// Fixtures
// ---------------------------------------------------------------------------

const USER: &str = "datapress";
const PASSWORD: &str = "s3cr3t-pw";

/// Write a small `id|name` parquet file: (1,"Anna"), (3,"Cara"), (4,"Dan").
fn write_people_parquet(path: &std::path::Path) {
    let schema = Arc::new(Schema::new(vec![
        Field::new("id", DataType::Int64, false),
        Field::new("name", DataType::Utf8, false),
    ]));
    let batch = RecordBatch::try_new(
        schema.clone(),
        vec![
            Arc::new(Int64Array::from(vec![1_i64, 3, 4])),
            Arc::new(StringArray::from(vec!["Anna", "Cara", "Dan"])),
        ],
    )
    .unwrap();
    let file = std::fs::File::create(path).unwrap();
    let mut writer = ArrowWriter::try_new(file, schema, None).unwrap();
    writer.write(&batch).unwrap();
    writer.close().unwrap();
}

/// Build a `Store` over a single-file `people` parquet dataset.
async fn make_people_store(location: &str) -> Store {
    let cfg = AppConfig {
        server: ServerConfig::default(),
        docs: datapress_core::config::DocsConfig::default(),
        swagger: datapress_core::config::SwaggerConfig::default(),
        auth: datapress_core::config::AuthConfig::default(),
        metrics: datapress_core::config::MetricsConfig::default(),
        explorer: datapress_core::config::ExplorerConfig::default(),
        sql: datapress_core::config::SqlConfig::default(),
        datafusion: datapress_core::config::DataFusionConfig::default(),
        datasets: vec![DatasetConfig {
            name: "people".into(),
            source: SourceConfig {
                kind: SourceKind::Parquet,
                location: location.to_string(),
            },
            s3: None,
            index: IndexConfig::default(),
            columns: vec![],
            dict_encode: true,
            lazy: false,
            predicate_filter: Default::default(),
            projection_filter: Default::default(),
        }],
    };
    Store::load(&cfg).await.expect("Store::load")
}

/// Grab an ephemeral loopback port by binding then immediately releasing it.
/// There's an inherent (tiny) race between release and the pgwire server
/// re-binding, but it's more than good enough for a local test.
fn free_port() -> u16 {
    TcpListener::bind("127.0.0.1:0")
        .unwrap()
        .local_addr()
        .unwrap()
        .port()
}

fn loopback() -> IpAddr {
    IpAddr::from([127, 0, 0, 1])
}

// ---------------------------------------------------------------------------
// Test 1: cleartext password auth (no TLS)
// ---------------------------------------------------------------------------

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn pgwire_password_auth_queries() {
    let dir = TempDir::new().unwrap();
    let parquet = dir.path().join("people.parquet");
    write_people_parquet(&parquet);
    let store = make_people_store(parquet.to_str().unwrap()).await;

    let port = free_port();
    let cfg = PgwireConfig {
        enabled: true,
        listen: loopback(),
        port,
        username: USER.into(),
        password: Some(PASSWORD.into()),
        tls_cert: None,
        tls_key: None,
    };
    let ctx = store.session_context().clone();
    let server = tokio::spawn(async move {
        let _ = serve_pgwire(ctx, cfg).await;
    });

    // Connect with the correct password, retrying until the listener is up.
    let good = format!("host=127.0.0.1 port={port} user={USER} password={PASSWORD} dbname=datapress");
    let client = {
        let mut attempt = None;
        for _ in 0..50 {
            match tokio_postgres::connect(&good, tokio_postgres::NoTls).await {
                Ok((client, connection)) => {
                    tokio::spawn(async move {
                        let _ = connection.await;
                    });
                    attempt = Some(client);
                    break;
                }
                Err(_) => tokio::time::sleep(Duration::from_millis(100)).await,
            }
        }
        attempt.expect("pgwire server did not become reachable")
    };

    // (a) aggregate — count(*) comes back as pg int8 / i64.
    let row = client
        .query_one("SELECT count(*) AS c FROM people", &[])
        .await
        .expect("count query");
    let c: i64 = row.get("c");
    assert_eq!(c, 3);

    // (b) typed column round-trip (int8 + text).
    let rows = client
        .query("SELECT id, name FROM people ORDER BY id", &[])
        .await
        .expect("select query");
    assert_eq!(rows.len(), 3);
    let id0: i64 = rows[0].get("id");
    let name0: &str = rows[0].get("name");
    assert_eq!(id0, 1);
    assert_eq!(name0, "Anna");

    // (c) extended protocol — prepared statement with a bound `$1` param.
    let rows = client
        .query("SELECT name FROM people WHERE id = $1", &[&3_i64])
        .await
        .expect("prepared query");
    assert_eq!(rows.len(), 1);
    let name: &str = rows[0].get("name");
    assert_eq!(name, "Cara");

    // (d) introspection canaries — cheap proof that `setup_pg_catalog` ran and
    // both `pg_catalog` and `information_schema` are queryable, which is what
    // psql `\dt`/`\d` and BI navigators (DBeaver, Power BI/Npgsql) rely on.
    //
    // pg_catalog.pg_type must exist and be non-empty (the builtin type rows).
    let row = client
        .query_one("SELECT count(*) AS c FROM pg_catalog.pg_type", &[])
        .await
        .expect("pg_catalog.pg_type query");
    let pg_type_rows: i64 = row.get("c");
    assert!(
        pg_type_rows > 0,
        "pg_catalog.pg_type should be populated, got {pg_type_rows}"
    );

    // information_schema.tables must list the registered `people` dataset.
    let rows = client
        .query("SELECT table_name FROM information_schema.tables", &[])
        .await
        .expect("information_schema.tables query");
    let table_names: Vec<String> = rows.iter().map(|r| r.get::<_, String>("table_name")).collect();
    assert!(
        table_names.iter().any(|t| t == "people"),
        "information_schema.tables should include 'people', got {table_names:?}"
    );

    // current_schema() must resolve (library UDF wins on the pgwire path) and
    // return the default schema name.
    let row = client
        .query_one("SELECT current_schema() AS s", &[])
        .await
        .expect("current_schema query");
    let schema: &str = row.get("s");
    assert_eq!(schema, "public");

    // (e) session-maintenance statements — pooling drivers (Npgsql/Power BI)
    // issue `DISCARD ALL` (and friends) when resetting a pooled connection.
    // DataFusion can't plan them, so without our interception hook they'd fail
    // with `XX000: Unsupported SQL statement`. Each must succeed over the simple
    // protocol (`batch_execute`), and the connection must stay usable after.
    for stmt in ["DISCARD ALL", "RESET ALL", "DEALLOCATE ALL", "UNLISTEN *"] {
        client
            .batch_execute(stmt)
            .await
            .unwrap_or_else(|e| panic!("session-maintenance statement `{stmt}` failed: {e}"));
    }

    // The connection still works after the resets.
    let row = client
        .query_one("SELECT count(*) AS c FROM people", &[])
        .await
        .expect("query after session reset");
    let c: i64 = row.get("c");
    assert_eq!(c, 3);

    // Negative: a statement that merely *contains* DISCARD ALL as a string
    // literal must NOT be swallowed — it must run and return the literal,
    // proving the hook matches on the parsed statement, not a substring.
    let row = client
        .query_one("SELECT 'DISCARD ALL' AS s", &[])
        .await
        .expect("string-literal query");
    let literal: &str = row.get("s");
    assert_eq!(literal, "DISCARD ALL");

    // The wrong password must be rejected outright.
    let bad = format!("host=127.0.0.1 port={port} user={USER} password=wrong-pw dbname=datapress");
    let denied = tokio_postgres::connect(&bad, tokio_postgres::NoTls).await;
    assert!(
        denied.is_err(),
        "connection with an incorrect password must be rejected"
    );

    server.abort();
    drop(store);
}

// ---------------------------------------------------------------------------
// Test 2: password auth over TLS (self-signed cert)
// ---------------------------------------------------------------------------

/// A `ServerCertVerifier` that accepts anything — the test uses a throwaway
/// self-signed cert, so we deliberately skip validation client-side.
#[derive(Debug)]
struct NoCertVerify;

impl rustls::client::danger::ServerCertVerifier for NoCertVerify {
    fn verify_server_cert(
        &self,
        _end_entity: &rustls::pki_types::CertificateDer<'_>,
        _intermediates: &[rustls::pki_types::CertificateDer<'_>],
        _server_name: &rustls::pki_types::ServerName<'_>,
        _ocsp_response: &[u8],
        _now: rustls::pki_types::UnixTime,
    ) -> Result<rustls::client::danger::ServerCertVerified, rustls::Error> {
        Ok(rustls::client::danger::ServerCertVerified::assertion())
    }

    fn verify_tls12_signature(
        &self,
        _message: &[u8],
        _cert: &rustls::pki_types::CertificateDer<'_>,
        _dss: &rustls::DigitallySignedStruct,
    ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
        Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
    }

    fn verify_tls13_signature(
        &self,
        _message: &[u8],
        _cert: &rustls::pki_types::CertificateDer<'_>,
        _dss: &rustls::DigitallySignedStruct,
    ) -> Result<rustls::client::danger::HandshakeSignatureValid, rustls::Error> {
        Ok(rustls::client::danger::HandshakeSignatureValid::assertion())
    }

    fn supported_verify_schemes(&self) -> Vec<rustls::SignatureScheme> {
        rustls::crypto::ring::default_provider()
            .signature_verification_algorithms
            .supported_schemes()
    }
}

#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn pgwire_password_auth_over_tls() {
    let dir = TempDir::new().unwrap();
    let parquet = dir.path().join("people.parquet");
    write_people_parquet(&parquet);
    let store = make_people_store(parquet.to_str().unwrap()).await;

    // Self-signed cert + PKCS8 key written to disk for the server to load.
    let issued =
        rcgen::generate_simple_self_signed(vec!["localhost".into(), "127.0.0.1".into()]).unwrap();
    let cert_path = dir.path().join("server.crt");
    let key_path = dir.path().join("server.key");
    std::fs::write(&cert_path, issued.cert.pem()).unwrap();
    std::fs::write(&key_path, issued.key_pair.serialize_pem()).unwrap();

    let port = free_port();
    let cfg = PgwireConfig {
        enabled: true,
        listen: loopback(),
        port,
        username: USER.into(),
        password: Some(PASSWORD.into()),
        tls_cert: Some(cert_path),
        tls_key: Some(key_path),
    };
    let ctx = store.session_context().clone();
    let server = tokio::spawn(async move {
        let _ = serve_pgwire(ctx, cfg).await;
    });

    // Client TLS config that trusts the throwaway cert.
    let tls_config =
        rustls::ClientConfig::builder_with_provider(Arc::new(rustls::crypto::ring::default_provider()))
            .with_safe_default_protocol_versions()
            .unwrap()
            .dangerous()
            .with_custom_certificate_verifier(Arc::new(NoCertVerify))
            .with_no_client_auth();
    let tls = tokio_postgres_rustls::MakeRustlsConnect::new(tls_config);

    let conn_str =
        format!("host=127.0.0.1 port={port} user={USER} password={PASSWORD} dbname=datapress sslmode=require");
    let client = {
        let mut attempt = None;
        for _ in 0..50 {
            match tokio_postgres::connect(&conn_str, tls.clone()).await {
                Ok((client, connection)) => {
                    tokio::spawn(async move {
                        let _ = connection.await;
                    });
                    attempt = Some(client);
                    break;
                }
                Err(_) => tokio::time::sleep(Duration::from_millis(100)).await,
            }
        }
        attempt.expect("pgwire TLS server did not become reachable")
    };

    let row = client
        .query_one("SELECT count(*) AS c FROM people", &[])
        .await
        .expect("count query over TLS");
    let c: i64 = row.get("c");
    assert_eq!(c, 3);

    server.abort();
    drop(store);
}

// ---------------------------------------------------------------------------
// Test 3: dedicated large-stack runtime (`spawn_pgwire`)
// ---------------------------------------------------------------------------

/// The production entry point hosts pgwire on its own OS thread + multi-thread
/// runtime with large worker stacks so DataFusion's recursive SQL planner can
/// absorb the deeply nested `pg_catalog`/`information_schema` introspection
/// queries BI clients (DBeaver, …) fire on connect without overflowing the
/// stack and aborting the process. This exercises that path end to end:
/// start via [`spawn_pgwire`], serve a query, then prove the handle's `Drop`
/// signals shutdown and joins the thread without hanging.
#[tokio::test(flavor = "multi_thread", worker_threads = 2)]
async fn pgwire_dedicated_runtime_serves_and_stops() {
    let dir = TempDir::new().unwrap();
    let parquet = dir.path().join("people.parquet");
    write_people_parquet(&parquet);
    let store = make_people_store(parquet.to_str().unwrap()).await;

    let port = free_port();
    let cfg = PgwireConfig {
        enabled: true,
        listen: loopback(),
        port,
        username: USER.into(),
        // Loopback with no password is permitted by config validation and keeps
        // the test focused on the runtime plumbing rather than auth.
        password: None,
        tls_cert: None,
        tls_key: None,
    };
    let ctx = store.session_context().clone();

    // Start on the dedicated large-stack runtime — the same path `lib::serve`
    // uses in production.
    let server = spawn_pgwire(ctx, cfg).expect("spawn pgwire runtime");

    let conn = format!("host=127.0.0.1 port={port} user={USER} dbname=datapress");
    let client = {
        let mut attempt = None;
        for _ in 0..50 {
            match tokio_postgres::connect(&conn, tokio_postgres::NoTls).await {
                Ok((client, connection)) => {
                    tokio::spawn(async move {
                        let _ = connection.await;
                    });
                    attempt = Some(client);
                    break;
                }
                Err(_) => tokio::time::sleep(Duration::from_millis(100)).await,
            }
        }
        attempt.expect("pgwire dedicated-runtime server did not become reachable")
    };

    // A data query and a pg_catalog introspection query both plan and run on
    // the large-stack workers.
    let row = client
        .query_one("SELECT count(*) AS c FROM people WHERE id > 0", &[])
        .await
        .expect("count query on dedicated runtime");
    let c: i64 = row.get("c");
    assert_eq!(c, 3);

    let row = client
        .query_one("SELECT count(*) AS c FROM pg_catalog.pg_type", &[])
        .await
        .expect("pg_catalog.pg_type query on dedicated runtime");
    let pg_type_rows: i64 = row.get("c");
    assert!(pg_type_rows > 0, "pg_catalog.pg_type should be populated");

    // Drop the client connection first, then tear the server down. `Drop`
    // signals the listener and joins the pgwire thread; the test hanging here
    // would mean shutdown never completes.
    drop(client);
    drop(server);
    drop(store);
}