Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 40 additions & 2 deletions crates/lance-csr/examples/embedded_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
//!
//! Writes a small social graph as two Lance datasets in a temporary directory, opens
//! them in a CSR-backed engine, runs a two-hop query, appends an edge and picks it up
//! with `refresh()`, and runs a query under a deadline. The documentation includes this
//! with `refresh()`, runs a query under a deadline, and repeats the home page's in-memory
//! Arrow example. The documentation includes this
//! file section by section (the `--8<--` markers), so keep it compiling and its printed
//! output in step.
//!
Expand All @@ -15,7 +16,7 @@ use std::sync::Arc;
use std::time::Duration;

use arrow::util::pretty::print_batches;
use arrow_array::{Int64Array, RecordBatch, RecordBatchIterator, StringArray};
use arrow_array::{ArrayRef, Int64Array, RecordBatch, RecordBatchIterator, StringArray};
use arrow_schema::{DataType, Field, Schema};
use lance::dataset::{Dataset, WriteMode, WriteParams};
use lance_csr::query::with_execution_deadline;
Expand Down Expand Up @@ -140,5 +141,42 @@ async fn main() -> lance_csr::Result<()> {
// | 16 |
// +-------+
// --8<-- [end:timeout]

// --8<-- [start:quickstart]
let config = GraphConfig::builder()
.node_table("Person", "id")
.rel_table("KNOWS", "Person", "Person", "src", "dst")
.build()?;
let person = RecordBatch::try_from_iter([
("id", Arc::new(Int64Array::from(vec![1, 2, 3])) as ArrayRef),
(
"name",
Arc::new(StringArray::from(vec!["Alice", "Bob", "Carol"])) as _,
),
])?;
let knows = RecordBatch::try_from_iter([
("src", Arc::new(Int64Array::from(vec![1, 2])) as ArrayRef),
("dst", Arc::new(Int64Array::from(vec![2, 3])) as _),
])?;
let sources = HashMap::from([
("Person".to_string(), EngineSource::Arrow(person)),
("KNOWS".to_string(), EngineSource::Arrow(knows)),
]);
let engine = CypherEngine::new(config, sources, true).await?;
let result = engine
.execute(
"MATCH (a:Person {name: 'Alice'})-[:KNOWS*1..2]->(b) \
RETURN b.name AS friend ORDER BY friend",
HashMap::new(),
)
.await?;
print_batches(&[result])?;
// +--------+
// | friend |
// +--------+
// | Bob |
// | Carol |
// +--------+
// --8<-- [end:quickstart]
Ok(())
}
56 changes: 32 additions & 24 deletions docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,38 @@ title: Home
no server to operate. Its distinguishing feature is a **compressed sparse row (CSR) adjacency index** that turns
each relationship hop from a hash join into an array lookup, which is what makes multi-hop traversals fast.

```python
import pyarrow as pa
from lance_csr import CypherEngine, GraphConfig

config = (
GraphConfig.builder()
.node_table("Person", "id")
.rel_table("KNOWS", "Person", "Person", "src", "dst")
.build()
)
engine = CypherEngine(
config,
{
"Person": pa.table({"id": [1, 2, 3], "name": ["Alice", "Bob", "Carol"]}),
"KNOWS": pa.table({"src": [1, 2], "dst": [2, 3]}),
},
use_csr=True,
)
result = engine.execute(
"MATCH (a:Person {name: 'Alice'})-[:KNOWS*1..2]->(b) "
"RETURN b.name AS friend ORDER BY friend"
)
print(result.to_pylist()) # [{'friend': 'Bob'}, {'friend': 'Carol'}]
```
=== "Python"

```python
import pyarrow as pa
from lance_csr import CypherEngine, GraphConfig

config = (
GraphConfig.builder()
.node_table("Person", "id")
.rel_table("KNOWS", "Person", "Person", "src", "dst")
.build()
)
engine = CypherEngine(
config,
{
"Person": pa.table({"id": [1, 2, 3], "name": ["Alice", "Bob", "Carol"]}),
"KNOWS": pa.table({"src": [1, 2], "dst": [2, 3]}),
},
use_csr=True,
)
result = engine.execute(
"MATCH (a:Person {name: 'Alice'})-[:KNOWS*1..2]->(b) "
"RETURN b.name AS friend ORDER BY friend"
)
print(result.to_pylist()) # [{'friend': 'Bob'}, {'friend': 'Carol'}]
```

=== "Rust"

```rust
--8<-- "crates/lance-csr/examples/embedded_query.rs:quickstart"
```

<div class="grid cards" markdown>

Expand Down
Loading