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
6 changes: 1 addition & 5 deletions crates/lance-csr/src/csr_expand.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1535,11 +1535,7 @@ impl EdgeLookup {
.iter()
.map(|&c| {
let f = rows.schema.field(c);
Field::new(
qualify_column(variable, f.name()),
f.data_type().clone(),
f.is_nullable(),
)
f.clone().with_name(qualify_column(variable, f.name()))
})
.collect();
Some(Self {
Expand Down
8 changes: 7 additions & 1 deletion crates/lance-csr/src/csr_expand/node_lookup.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,13 @@ impl NodeLookupNode {
columns.sort_unstable();
let fields = columns
.iter()
.map(|(c, name)| Field::new(name, rows.schema.field(*c).data_type().clone(), true))
.map(|(c, name)| {
rows.schema
.field(*c)
.clone()
.with_name(name)
.with_nullable(true)
})
.collect();
Self::with_fields(
input,
Expand Down
16 changes: 6 additions & 10 deletions crates/lance-csr/src/datafusion_planner/builder/expand_ops.rs
Original file line number Diff line number Diff line change
Expand Up @@ -403,11 +403,9 @@ impl DataFusionPlanner {
};

let output = |f: &arrow_schema::Field| {
arrow_schema::Field::new(
qualify_column(rel_alias, f.name()),
f.data_type().clone(),
true,
)
f.clone()
.with_name(qualify_column(rel_alias, f.name()))
.with_nullable(true)
};
let node = CsrExpandNode::try_new(
left_plan.clone(),
Expand Down Expand Up @@ -534,11 +532,9 @@ impl DataFusionPlanner {
.fields()
.iter()
.map(|f| {
arrow_schema::Field::new(
qualify_column(target_variable, f.name()),
f.data_type().clone(),
f.is_nullable(),
)
f.as_ref()
.clone()
.with_name(qualify_column(target_variable, f.name()))
})
.collect();
Some(crate::csr_expand::TargetLookup::new(
Expand Down
17 changes: 11 additions & 6 deletions crates/lance-csr/src/path_match.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ impl PathMatchNode {
.iter()
.position(|f| f.name().eq_ignore_ascii_case(field.name()))
.expect("unified property");
fields.push(Field::new(name, field.data_type().clone(), true));
fields.push(field.as_ref().clone().with_name(name).with_nullable(true));
outputs.push(Output::Node { boundary, property });
}
}
Expand All @@ -161,11 +161,16 @@ impl PathMatchNode {
fields.push(Field::new(variable.to_lowercase(), edge_type.clone(), true));
outputs.push(Output::RelationshipValue { step });
for field in &edge_fields {
fields.push(Field::new(
crate::case_insensitive::qualify_column(variable, field.name()),
field.data_type().clone(),
true,
));
fields.push(
field
.as_ref()
.clone()
.with_name(crate::case_insensitive::qualify_column(
variable,
field.name(),
))
.with_nullable(true),
);
outputs.push(Output::Relationship {
step,
property: edge_fields
Expand Down
10 changes: 2 additions & 8 deletions crates/lance-csr/src/query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::logical_plan::LogicalPlanner;
use crate::parser::parse_cypher_query;
use crate::spark_dialect::build_spark_dialect;
use arrow_array::RecordBatch;
use arrow_schema::{Field, Schema, SchemaRef};
use arrow_schema::{Schema, SchemaRef};
use datafusion_sql::unparser::dialect::{
CustomDialect, DefaultDialect, MySqlDialect, PostgreSqlDialect, SqliteDialect,
};
Expand Down Expand Up @@ -82,13 +82,7 @@ pub(crate) fn normalize_schema(schema: SchemaRef) -> Result<SchemaRef> {
let fields: Vec<_> = schema
.fields()
.iter()
.map(|f| {
Arc::new(Field::new(
f.name().to_lowercase(),
f.data_type().clone(),
f.is_nullable(),
))
})
.map(|f| Arc::new(f.as_ref().clone().with_name(f.name().to_lowercase())))
.collect();
Ok(Arc::new(Schema::new(fields)))
}
Expand Down
60 changes: 60 additions & 0 deletions crates/lance-csr/tests/test_engine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -728,3 +728,63 @@ async fn parameterized_key_map_with_declared_property_projection() {
["Alice"]
);
}

/// Returned properties keep their source field's metadata (here an Arrow extension
/// name) across hops, on the CSR and on the join plan.
#[tokio::test]
async fn returned_properties_keep_field_metadata() {
let tag = |batch: RecordBatch, column: &str, name: &str| {
let schema = batch.schema();
let fields = schema.fields().iter().map(|f| {
let f = f.as_ref().clone();
if f.name() == column {
f.with_metadata(HashMap::from([(
"ARROW:extension:name".into(),
name.into(),
)]))
} else {
f
}
});
batch
.with_schema(Arc::new(Schema::new(fields.collect::<Vec<_>>())))
.unwrap()
};
let people = tag(people(), "name", "test.name");
let knows = tag(knows(&[(1, 2), (2, 3)]), "dst_id", "test.dst");
let queries = [
("MATCH (p:Person) RETURN p.name AS name", "test.name"),
(
"MATCH (:Person)-[:KNOWS]->(b:Person) RETURN b.name AS name",
"test.name",
),
(
"MATCH (:Person)-[:KNOWS]->(b:Person) RETURN b.name",
"test.name",
),
(
"MATCH (:Person)-[:KNOWS*1..2]->(b:Person) RETURN b.name AS name",
"test.name",
),
(
"MATCH (:Person)-[k:KNOWS]->(:Person) RETURN k.dst_id AS dst",
"test.dst",
),
];
for use_csr in [false, true] {
let sources = HashMap::from([
("Person".to_string(), EngineSource::Arrow(people.clone())),
("KNOWS".to_string(), EngineSource::Arrow(knows.clone())),
]);
let engine = CypherEngine::new(config(), sources, use_csr).await.unwrap();
for (query, expected) in queries {
let batch = engine.execute(query, HashMap::new()).await.unwrap();
let metadata = batch.schema().field(0).metadata().clone();
assert_eq!(
metadata.get("ARROW:extension:name").map(String::as_str),
Some(expected),
"use_csr={use_csr}: {query}"
);
}
}
}
81 changes: 81 additions & 0 deletions python/python/tests/test_field_metadata.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# SPDX-License-Identifier: Apache-2.0
# SPDX-FileCopyrightText: Copyright The Lance Authors

"""Returned properties keep their source field's Arrow metadata, such as a GeoArrow
extension name, whatever the number of hops and whether CSR is on."""

import lance
import pyarrow as pa
import pytest
from lance_csr import CypherEngine, GraphConfig

POINT = {b"ARROW:extension:name": b"geoarrow.point"}
WKB = {b"ARROW:extension:name": b"geoarrow.wkb"}
CONFIG = (
GraphConfig.builder()
.node_table("Place", "place_id")
.node_table("Visit", "visit_id")
.rel_table("AT", "Visit", "Place", "src", "dst")
.build()
)
POINTS = [("here", POINT), ("xy", POINT), ("wkb", WKB)]
QUERIES = [
("MATCH (p:Place) RETURN p.location AS here, p.xy AS xy, p.wkb AS wkb", POINTS),
(
"MATCH (v:Visit)-[:AT]->(p:Place) "
"RETURN v.day AS day, p.location AS here, p.xy AS xy, p.wkb AS wkb",
POINTS,
),
("MATCH (v:Visit)-[:AT]->(p:Place) RETURN p.location", [("p.location", POINT)]),
("MATCH (v:Visit)-[:AT*1..2]->(p:Place) RETURN p.location AS here", POINTS[:1]),
("MATCH (v:Visit)-[a:AT]->(p:Place) RETURN a.route AS route", [("route", WKB)]),
]


@pytest.fixture(scope="module")
def datasets(tmp_path_factory):
path = tmp_path_factory.mktemp("graph")
xy = pa.struct([("x", pa.float64()), ("y", pa.float64())])
place = pa.table(
[
pa.array([1, 2], pa.int64()),
pa.array([[0.0, 1.0], [2.0, 3.0]], pa.list_(pa.float64(), 2)),
pa.array([{"x": 0.0, "y": 1.0}, {"x": 2.0, "y": 3.0}], xy),
pa.array([b"\x01", b"\x02"], pa.binary()),
],
schema=pa.schema(
[
pa.field("place_id", pa.int64()),
pa.field("location", pa.list_(pa.float64(), 2), metadata=POINT),
pa.field("xy", xy, metadata=POINT),
pa.field("wkb", pa.binary(), metadata=WKB),
]
),
)
visit = pa.table(
{"visit_id": pa.array([10, 11], pa.int64()), "day": ["mon", "tue"]}
)
at = pa.table(
[
pa.array([10, 11], pa.int64()),
pa.array([1, 2], pa.int64()),
pa.array([b"\x03", b"\x04"], pa.binary()),
],
schema=pa.schema(
[
pa.field("src", pa.int64()),
pa.field("dst", pa.int64()),
pa.field("route", pa.binary(), metadata=WKB),
]
),
)
tables = {"Place": place, "Visit": visit, "AT": at}
return {name: lance.write_dataset(t, path / name) for name, t in tables.items()}


@pytest.mark.parametrize("use_csr", [False, True])
@pytest.mark.parametrize(("query", "expected"), QUERIES)
def test_returned_properties_keep_field_metadata(datasets, use_csr, query, expected):
schema = CypherEngine(CONFIG, datasets, use_csr=use_csr).execute(query).schema
for name, metadata in expected:
assert schema.field(name).metadata == metadata, name
Loading