grafeo icon indicating copy to clipboard operation
grafeo copied to clipboard

text_match and text_score are refused per row on a store of its own, although the store answers has_text_index and score_text

Open 1pxone opened this issue 2 weeks ago • 1 comments

Summary

On a database built on a store of its own (GrafeoDB::with_read_store or with_store), text_score in a projection and text_match anywhere it is not pushed into a text scan fail with GRAFEO-Q004: unsupported: ... needs a text index, which only the built-in store has. The store implements GraphStore::has_text_index and score_text and reports an index on the property, but the planner refuses before asking it (check_text_index, crates/grafeo-engine/src/query/planner/lpg/expression.rs). The pushed-down form (WHERE text_match(d.body, 'graph') alone) works, through text_search, so the same store serves one form and is refused for the other.

Reproduce

[dependencies]
grafeo-core = { git = "https://github.com/GrafeoDB/grafeo", branch = "release/0.6.4", default-features = false, features = ["lpg", "text-index"] }
grafeo-engine = { git = "https://github.com/GrafeoDB/grafeo", branch = "release/0.6.4", default-features = false, features = ["lpg", "cypher", "text-index"] }
grafeo-common = { git = "https://github.com/GrafeoDB/grafeo", branch = "release/0.6.4" }
//! A store of its own that keeps a text index on :Doc(body) and answers
//! `has_text_index`, `score_text` and `text_search` for it.
use std::sync::Arc;

use grafeo_common::types::{ArcStr, EdgeId, EpochId, NodeId, ReadView};
use grafeo_common::utils::error::Result;
use grafeo_core::graph::lpg::{Edge, Node};
use grafeo_core::graph::{Direction, GraphStore};
use grafeo_core::statistics::Statistics;
use grafeo_engine::{Config, GrafeoDB};

struct OneDoc;

impl GraphStore for OneDoc {
    fn node(&self, id: NodeId, _: ReadView) -> Result<Option<Node>> {
        Ok((id == NodeId::new(1)).then(|| {
            let mut node = Node::with_labels(id, ["Doc"]);
            node.set_property("body", "graph databases");
            node
        }))
    }
    fn edge(&self, _: EdgeId, _: ReadView) -> Result<Option<Edge>> {
        Ok(None)
    }
    fn edges(&self, _: NodeId, _: Direction, _: &[ArcStr], _: ReadView) -> Result<Vec<(NodeId, EdgeId)>> {
        Ok(Vec::new())
    }
    fn has_backward_adjacency(&self) -> bool {
        true
    }
    fn node_ids(&self, _: ReadView) -> Result<Vec<NodeId>> {
        Ok(vec![NodeId::new(1)])
    }
    fn all_labels(&self) -> Result<Vec<String>> {
        Ok(vec!["Doc".into()])
    }
    fn all_edge_types(&self) -> Result<Vec<String>> {
        Ok(Vec::new())
    }
    fn all_property_keys(&self) -> Result<Vec<String>> {
        Ok(vec!["body".into()])
    }
    fn statistics(&self) -> Arc<Statistics> {
        Arc::new(Statistics::default())
    }
    fn committed_epoch(&self) -> EpochId {
        EpochId::new(1)
    }
    // The store's own text index on :Doc(body).
    fn has_text_index(&self, label: &str, property: &str) -> bool {
        (label, property) == ("Doc", "body")
    }
    fn score_text(&self, id: NodeId, _: &str, _: &str, query: &str, _: ReadView) -> Result<Option<f64>> {
        Ok(Some(if id == NodeId::new(1) && query == "graph" { 1.0 } else { 0.0 }))
    }
    fn text_search(&self, _: &str, _: &str, query: &str, _: usize, _: ReadView) -> Result<Vec<(NodeId, f64)>> {
        Ok(if query == "graph" { vec![(NodeId::new(1), 1.0)] } else { Vec::new() })
    }
    fn text_search_with_threshold(&self, _: &str, _: &str, query: &str, _: f64, _: ReadView) -> Result<Vec<(NodeId, f64)>> {
        Ok(if query == "graph" { vec![(NodeId::new(1), 1.0)] } else { Vec::new() })
    }
}

fn main() {
    let db = GrafeoDB::with_read_store(Arc::new(OneDoc), Config::in_memory()).unwrap();
    for statement in [
        "MATCH (d:Doc) WHERE text_match(d.body, 'graph') OR d.body = 'x' RETURN d.body",
        "MATCH (d:Doc) WHERE text_match(d.body, 'graph') RETURN d.body",
        "MATCH (d:Doc) RETURN text_score(d.body, 'graph') AS score",
    ] {
        match db.session().execute_cypher(statement) {
            Ok(result) => println!("{statement}\n  rows: {:?}", result.rows()),
            Err(error) => println!("{statement}\n  error: {error}"),
        }
    }
}

Output (Rust 1.100.0):

MATCH (d:Doc) WHERE text_match(d.body, 'graph') OR d.body = 'x' RETURN d.body
  error: GRAFEO-Q004: unsupported: text_match needs a text index, which only the built-in store has: this database is on a store of its own
MATCH (d:Doc) WHERE text_match(d.body, 'graph') RETURN d.body
  rows: [[String("graph databases")]]
MATCH (d:Doc) RETURN text_score(d.body, 'graph') AS score
  error: GRAFEO-Q004: unsupported: text_score needs a text index, which only the built-in store has: this database is on a store of its own

Expected

All three statements run: the first returns the row, the third returns score = 1.0 from score_text. A store of its own that reports a text index (has_text_index) serves text_match and text_score per row through score_text, as it already serves the pushed-down form through text_search. Only a store without a text index on the property should be refused.

Impact

Blocks text search for embedders on a store of their own: the per-row forms (text_score in RETURN or ORDER BY, text_match under OR or NOT, or with a second predicate the planner does not push) cannot be used, although the GraphStore contract has the methods for them. It worked on 0.5.x (GraphStoreSearch::score_text). We embed Grafeo over our own storage (the RushDB engine) and hit this when moving from 0.5.44 to 0.6.

Surface

Rust API (GrafeoDB::with_read_store, GrafeoDB::with_store).

Language

Cypher (GQL is the same: the check is in the shared LPG planner).

Storage

A store of its own (GraphStore / GraphBackend implemented outside Grafeo).

Version

0.6.3, and release/0.6.4 at 532e8e03.

Anything else

Proposed fix: ask the store first, and refuse a store of its own only when it has no text index on the property. In check_text_index (crates/grafeo-engine/src/query/planner/lpg/expression.rs), move the StoreKind::External check after the has_text_index loop:

-        if self.store_kind == super::StoreKind::External {
-            return Err(Error::from(QueryError::unsupported(format!(
-                "{name} needs a text index, which only the built-in store has: this database is on a store of its own"
-            ))));
-        }
         let labels = self.store.all_labels()?;
         if labels
             .iter()
             .any(|label| self.store.has_text_index(label, property))
         {
             return Ok(());
         }
+        if self.store_kind == super::StoreKind::External {
+            return Err(Error::from(QueryError::unsupported(format!(
+                "{name}({variable}.{property}, ...) needs a text index on '{property}', and the store has none"
+            ))));
+        }

With it, the reproduction prints the row for all three statements and score = 1.0 for the third. Happy to open the PR with a regression test (a .gtest cannot build a store of its own, so a Rust test in crates/grafeo-engine/tests/ along the lines of the program above) once the issue is planned.

1pxone avatar Nov 20 '26 17:11 1pxone

@1pxone, this one should be relatively easy. I'll look at it tonight or tomorrow. Part of 0.6.4

StevenBtw avatar Nov 21 '26 18:11 StevenBtw