datafusion-opensearch 0.2.0

A DataFusion TableProvider over OpenSearch/Elasticsearch with filter, projection and limit pushdown.
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
//! End-to-end proof against a REAL OpenSearch started by testcontainers: schema inference from
//! `_mapping`, filter/projection/limit pushdown, the always-on base filter, and (with the `udf`
//! feature) the native `match` and geo pushdowns. Self-contained — it creates and drops its own
//! index. Needs Docker, so it is ignored by default:
//!
//!   cargo test -p datafusion-opensearch --all-features --test testcontainers -- --ignored --nocapture
//!
//! The container mirrors a typical local single-node setup (security plugin disabled).

use std::sync::Arc;
use std::time::Duration;

use datafusion::arrow::array::{Array, StringArray, StringViewArray};
use datafusion::arrow::datatypes::{DataType, TimeUnit};
use datafusion::catalog::SchemaProvider;
use datafusion::prelude::{SessionConfig, SessionContext};
use datafusion_opensearch::{OpenSearchClient, OpenSearchTableFactory};
use datafusion_opensearch::{OpenSearchSchemaProvider, OpenSearchTableProviderFactory};
use serde_json::json;
use testcontainers::core::IntoContainerPort;
use testcontainers::runners::AsyncRunner;
use testcontainers::{GenericImage, ImageExt};

const IMAGE: &str = "opensearchproject/opensearch";
const TAG: &str = "2.17.1";
const INDEX: &str = "datafusion-opensearch-it";
/// OpenSearch takes a while to come up; poll the health endpoint rather than rely on log lines.
const STARTUP: Duration = Duration::from_secs(180);

/// Wait until `/_cluster/health` answers 200 (yellow or green), or give up after [`STARTUP`].
async fn wait_healthy(http: &reqwest::Client, url: &str) {
    let deadline = std::time::Instant::now() + STARTUP;
    loop {
        if let Ok(r) = http.get(format!("{url}/_cluster/health")).send().await {
            if r.status().is_success() {
                return;
            }
        }
        assert!(
            std::time::Instant::now() < deadline,
            "OpenSearch did not become healthy within {STARTUP:?}"
        );
        tokio::time::sleep(Duration::from_secs(2)).await;
    }
}

/// Collect one string column (Utf8 or Utf8View — SQL `VARCHAR` plans as the latter) across all
/// batches, sorted.
fn strings(batches: &[datafusion::arrow::record_batch::RecordBatch], col: usize) -> Vec<String> {
    let mut out = vec![];
    for b in batches {
        let c = b.column(col);
        if let Some(a) = c.as_any().downcast_ref::<StringArray>() {
            out.extend((0..a.len()).filter(|&i| !a.is_null(i)).map(|i| a.value(i).to_string()));
        } else if let Some(a) = c.as_any().downcast_ref::<StringViewArray>() {
            out.extend((0..a.len()).filter(|&i| !a.is_null(i)).map(|i| a.value(i).to_string()));
        } else {
            panic!("column {col} is not a string column: {:?}", c.data_type());
        }
    }
    out.sort();
    out
}

#[tokio::test]
#[ignore = "needs Docker (testcontainers)"]
async fn end_to_end_against_a_real_opensearch() {
    let container = GenericImage::new(IMAGE, TAG)
        .with_exposed_port(9200.tcp())
        .with_env_var("discovery.type", "single-node")
        .with_env_var("DISABLE_SECURITY_PLUGIN", "true")
        .with_env_var("DISABLE_INSTALL_DEMO_CONFIG", "true")
        .with_env_var("cluster.routing.allocation.disk.threshold_enabled", "false")
        .with_env_var("OPENSEARCH_JAVA_OPTS", "-Xms512m -Xmx512m")
        .start()
        .await
        .expect("start opensearch container");
    let port = container.get_host_port_ipv4(9200).await.unwrap();
    let url = format!("http://127.0.0.1:{port}");
    let http = reqwest::Client::new();
    wait_healthy(&http, &url).await;

    // ── seed: an index with an explicit mapping covering every type family we read ──
    http.put(format!("{url}/{INDEX}"))
        .json(&json!({ "mappings": { "properties": {
            "id": { "type": "keyword" },
            "status": { "type": "keyword" },
            "speed": { "type": "double" },
            "count": { "type": "long" },
            "active": { "type": "boolean" },
            "title": { "type": "text" },
            "pos": { "type": "geo_point" },
            "when": { "type": "date" },
        } } }))
        .send()
        .await
        .unwrap()
        .error_for_status()
        .unwrap();
    let docs = [
        json!({ "id": "a", "status": "OK",   "speed": 55.0, "count": 1, "active": true,  "title": "engine fault cleared",  "pos": { "lat": 60.17, "lon": 24.94 }, "when": "2024-01-01T10:00:00Z" }), // Helsinki
        json!({ "id": "b", "status": "OK",   "speed": 30.0, "count": 2, "active": false, "title": "routine service",        "pos": { "lat": 59.33, "lon": 18.07 }, "when": "2024-01-02T10:00:00Z" }), // Stockholm
        json!({ "id": "c", "status": "DOWN", "speed": 70.0, "count": 3, "active": true,  "title": "engine overheating",     "pos": { "lat": 55.68, "lon": 12.57 }, "when": "2024-01-03T10:00:00Z" }), // Copenhagen
        json!({ "id": "d", "status": "OK",   "speed": 45.0, "count": 4, "active": true,  "title": "brakes worn",            "pos": { "lat": 59.91, "lon": 10.75 }, "when": 1_704_362_400_000_i64 }), // Oslo, epoch millis = 2024-01-04T10:00:00Z
    ];
    for d in &docs {
        http.put(format!("{url}/{INDEX}/_doc/{}?refresh=true", d["id"].as_str().unwrap()))
            .json(d)
            .send()
            .await
            .unwrap()
            .error_for_status()
            .unwrap();
    }

    // ── 1) schema inferred from _mapping ──
    let factory = OpenSearchTableFactory::new(OpenSearchClient::new(&url));
    let table = factory.table_provider(INDEX).await.expect("schema from _mapping");
    let schema = table.schema();
    let typed: Vec<(String, DataType)> = schema
        .fields()
        .iter()
        .map(|f| (f.name().clone(), f.data_type().clone()))
        .collect();
    assert_eq!(
        typed,
        vec![
            ("active".into(), DataType::Boolean),
            ("count".into(), DataType::Int64),
            ("id".into(), DataType::Utf8),
            ("pos".into(), DataType::Utf8), // geo_point → JSON text
            ("speed".into(), DataType::Float64),
            ("status".into(), DataType::Utf8),
            ("title".into(), DataType::Utf8),
            (
                "when".into(),
                DataType::Timestamp(TimeUnit::Millisecond, Some("UTC".into()))
            ),
        ]
    );
    let ctx = SessionContext::new();
    ctx.register_table("t", table).unwrap();

    // ── 2) filter + projection + limit pushdown ──
    let batches = ctx
        .sql("SELECT id FROM t WHERE status = 'OK' AND speed > 40")
        .await
        .unwrap()
        .collect()
        .await
        .unwrap();
    assert_eq!(strings(&batches, 0), vec!["a", "d"]);
    assert_eq!(batches[0].num_columns(), 1, "projection pushed to one column");
    let n: usize = ctx
        .sql("SELECT id FROM t LIMIT 2")
        .await
        .unwrap()
        .collect()
        .await
        .unwrap()
        .iter()
        .map(|b| b.num_rows())
        .sum();
    assert_eq!(n, 2, "LIMIT pushed into size");
    let batches = ctx
        .sql("SELECT id FROM t WHERE count BETWEEN 2 AND 3 AND active")
        .await
        .unwrap()
        .collect()
        .await
        .unwrap();
    assert_eq!(strings(&batches, 0), vec!["c"]);

    // ── 3) the always-on base filter composes with (never replaced by) the user's WHERE ──
    let gated = factory
        .provider_with_schema(INDEX, schema.clone())
        .with_base_filter(vec![json!({ "term": { "status": "OK" } })]);
    let ctx2 = SessionContext::new();
    ctx2.register_table("g", Arc::new(gated)).unwrap();
    let batches = ctx2.sql("SELECT id FROM g").await.unwrap().collect().await.unwrap();
    assert_eq!(strings(&batches, 0), vec!["a", "b", "d"], "DOWN row never visible");
    let batches = ctx2
        .sql("SELECT id FROM g WHERE status = 'DOWN'")
        .await
        .unwrap()
        .collect()
        .await
        .unwrap();
    assert!(
        strings(&batches, 0).is_empty(),
        "base filter ANDs with the user predicate"
    );

    // ── 4) timestamps: read from ISO strings and epoch millis; range pushed as epoch millis ──
    let batches = ctx
        .sql("SELECT id FROM t WHERE \"when\" >= '2024-01-03T00:00:00Z'")
        .await
        .unwrap()
        .collect()
        .await
        .unwrap();
    assert_eq!(
        strings(&batches, 0),
        vec!["c", "d"],
        "date range pushed as epoch millis"
    );
    let batches = ctx
        .sql("SELECT id FROM t WHERE \"when\" BETWEEN '2024-01-02' AND '2024-01-02T23:59:59Z'")
        .await
        .unwrap()
        .collect()
        .await
        .unwrap();
    assert_eq!(strings(&batches, 0), vec!["b"]);

    // ── 5) streaming: a result larger than one page comes back complete, LIMIT stops early ──
    let big = format!("{INDEX}-paging");
    http.put(format!("{url}/{big}"))
        .json(&json!({ "mappings": { "properties": { "n": { "type": "integer" } } } }))
        .send()
        .await
        .unwrap()
        .error_for_status()
        .unwrap();
    let mut bulk = String::new();
    for n in 0..23 {
        bulk.push_str(&format!("{{\"index\":{{\"_id\":\"{n}\"}}}}\n{{\"n\":{n}}}\n"));
    }
    http.post(format!("{url}/{big}/_bulk?refresh=true"))
        .header("content-type", "application/x-ndjson")
        .body(bulk)
        .send()
        .await
        .unwrap()
        .error_for_status()
        .unwrap();
    let paged = factory.provider(&big).await.unwrap().with_page_size(5);
    let ctx3 = SessionContext::new();
    ctx3.register_table("p", Arc::new(paged)).unwrap();
    let batches = ctx3.sql("SELECT n FROM p").await.unwrap().collect().await.unwrap();
    let rows: usize = batches.iter().map(|b| b.num_rows()).sum();
    assert_eq!(rows, 23, "every page of a 23-row result with page_size 5");
    assert!(batches.len() >= 5, "streamed as pages, got {} batches", batches.len());
    let n: usize = ctx3
        .sql("SELECT n FROM p LIMIT 7")
        .await
        .unwrap()
        .collect()
        .await
        .unwrap()
        .iter()
        .map(|b| b.num_rows())
        .sum();
    assert_eq!(n, 7, "LIMIT stops the scroll after two pages");
    let capped = factory
        .provider(&big)
        .await
        .unwrap()
        .with_page_size(5)
        .with_max_rows(Some(12));
    ctx3.register_table("capped", Arc::new(capped)).unwrap();
    let n: usize = ctx3
        .sql("SELECT n FROM capped")
        .await
        .unwrap()
        .collect()
        .await
        .unwrap()
        .iter()
        .map(|b| b.num_rows())
        .sum();
    assert_eq!(n, 12, "max_rows caps a LIMIT-less scan");
    // Partitioned: three sliced scrolls in parallel still return every row exactly once.
    let sliced = factory
        .provider(&big)
        .await
        .unwrap()
        .with_page_size(4)
        .with_partitions(3);
    ctx3.register_table("sliced", Arc::new(sliced)).unwrap();
    let batches = ctx3.sql("SELECT n FROM sliced").await.unwrap().collect().await.unwrap();
    let mut seen: Vec<i64> = batches
        .iter()
        .flat_map(|b| {
            let a = b
                .column(0)
                .as_any()
                .downcast_ref::<datafusion::arrow::array::Int64Array>()
                .unwrap();
            (0..a.len()).map(|i| a.value(i)).collect::<Vec<_>>()
        })
        .collect();
    seen.sort();
    assert_eq!(
        seen,
        (0..23).collect::<Vec<i64>>(),
        "sliced scroll covers every row once"
    );
    let n: usize = ctx3
        .sql("SELECT n FROM sliced LIMIT 5")
        .await
        .unwrap()
        .collect()
        .await
        .unwrap()
        .iter()
        .map(|b| b.num_rows())
        .sum();
    assert_eq!(n, 5, "a LIMIT on a partitioned table is exact (single partition)");
    let plan = ctx3
        .sql("EXPLAIN SELECT n FROM sliced")
        .await
        .unwrap()
        .collect()
        .await
        .unwrap();
    assert!(
        strings(&plan, 1).iter().any(|l| l.contains("partitions=3")),
        "EXPLAIN shows the operator"
    );

    let total: i64 = {
        let b = ctx3.sql("SELECT sum(n) FROM p").await.unwrap().collect().await.unwrap();
        b[0].column(0)
            .as_any()
            .downcast_ref::<datafusion::arrow::array::Int64Array>()
            .unwrap()
            .value(0)
    };
    assert_eq!(total, (0..23).sum::<i64>(), "aggregation over a streamed scan");

    // ── 6) CREATE EXTERNAL TABLE … STORED AS OPENSEARCH ──
    // information_schema on, so SHOW TABLES works in step 7.
    let ctx4 = SessionContext::new_with_config(SessionConfig::new().with_information_schema(true));
    OpenSearchTableProviderFactory::new().register(&ctx4);
    ctx4.sql(&format!(
        "CREATE EXTERNAL TABLE docs STORED AS OPENSEARCH LOCATION '{url}/{INDEX}' \
         OPTIONS ('sort' 'speed:desc', 'base_filter' '[{{\"term\":{{\"status\":\"OK\"}}}}]')"
    ))
    .await
    .unwrap()
    .collect()
    .await
    .unwrap();
    let batches = ctx4
        .sql("SELECT id FROM docs LIMIT 2")
        .await
        .unwrap()
        .collect()
        .await
        .unwrap();
    assert_eq!(
        strings(&batches, 0),
        vec!["a", "d"],
        "top-2 by speed among OK rows: a (55), d (45)"
    );
    ctx4.sql(&format!(
        "CREATE EXTERNAL TABLE typed (id VARCHAR, speed DOUBLE) STORED AS OPENSEARCH LOCATION '{url}/{INDEX}'"
    ))
    .await
    .unwrap()
    .collect()
    .await
    .unwrap();
    let batches = ctx4
        .sql("SELECT id FROM typed WHERE speed < 40")
        .await
        .unwrap()
        .collect()
        .await
        .unwrap();
    assert_eq!(strings(&batches, 0), vec!["b"]);

    // ── 7) the cluster as a schema: SHOW TABLES lists the indices, tables build lazily ──
    let os = OpenSearchSchemaProvider::connect(OpenSearchClient::new(&url))
        .await
        .unwrap();
    assert!(os.table_exist(INDEX) && os.table_exist(&big));
    ctx4.catalog("datafusion")
        .unwrap()
        .register_schema("os", Arc::new(os))
        .unwrap();
    let batches = ctx4
        .sql(&format!("SELECT id FROM os.\"{INDEX}\" WHERE status = 'DOWN'"))
        .await
        .unwrap()
        .collect()
        .await
        .unwrap();
    assert_eq!(strings(&batches, 0), vec!["c"]);
    let names = strings(&ctx4.sql("SHOW TABLES").await.unwrap().collect().await.unwrap(), 2);
    assert!(names.contains(&INDEX.to_string()) && names.contains(&big), "{names:?}");
    http.delete(format!("{url}/{big}")).send().await.unwrap();

    // ── 8) native match + geo pushdown (feature `udf`) ──
    #[cfg(feature = "udf")]
    {
        for f in datafusion_opensearch::udf::all_udfs() {
            ctx.register_udf(f.as_ref().clone());
        }
        let batches = ctx
            .sql("SELECT id FROM t WHERE os_match(title, 'engine')")
            .await
            .unwrap()
            .collect()
            .await
            .unwrap();
        assert_eq!(strings(&batches, 0), vec!["a", "c"], "analyzed match on a text field");
        // 5 km around Helsinki → only 'a'.
        let batches = ctx
            .sql("SELECT id FROM t WHERE os_geo_distance(pos, 24.94, 60.17, 5000)")
            .await
            .unwrap()
            .collect()
            .await
            .unwrap();
        assert_eq!(strings(&batches, 0), vec!["a"], "geo_distance pushed natively");
        // A bbox over the Nordics, west of Helsinki → Stockholm/Copenhagen/Oslo.
        let batches = ctx
            .sql("SELECT id FROM t WHERE os_geo_bbox(pos, 5.0, 65.0, 20.0, 50.0)")
            .await
            .unwrap()
            .collect()
            .await
            .unwrap();
        assert_eq!(
            strings(&batches, 0),
            vec!["b", "c", "d"],
            "geo_bounding_box pushed natively"
        );
    }

    http.delete(format!("{url}/{INDEX}")).send().await.unwrap();
}