Skip to content

Commit

Permalink
store: Fix fulltextsearch space handling
Browse files Browse the repository at this point in the history
  • Loading branch information
mangas committed Dec 16, 2021
1 parent 753cee7 commit ff236aa
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 4 deletions.
4 changes: 4 additions & 0 deletions NEWS.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# NEWS

## Unreleased
- Gracefully handle syntax errors on fulltext search. Specifically provides information about common use case
where whitespace characters were part of the terms.

## 0.25.0

### Api Version 0.0.6
Expand Down
3 changes: 3 additions & 0 deletions graph/src/data/query/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,7 @@ pub enum QueryExecutionError {
Panic(String),
EventStreamError,
FulltextQueryRequiresFilter,
FulltextQueryInvalidSyntax(String),
DeploymentReverted,
SubgraphManifestResolveError(Arc<SubgraphManifestResolveError>),
InvalidSubgraphManifest,
Expand Down Expand Up @@ -113,6 +114,7 @@ impl QueryExecutionError {
| Unimplemented(_)
| CyclicalFragment(_)
| UndefinedFragment(_)
| FulltextQueryInvalidSyntax(_)
| FulltextQueryRequiresFilter => true,
NonNullError(_, _)
| ListValueError(_, _)
Expand Down Expand Up @@ -273,6 +275,7 @@ impl fmt::Display for QueryExecutionError {
Panic(msg) => write!(f, "panic processing query: {}", msg),
EventStreamError => write!(f, "error in the subscription event stream"),
FulltextQueryRequiresFilter => write!(f, "fulltext search queries can only use EntityFilter::Equal"),
FulltextQueryInvalidSyntax(msg) => write!(f, "Invalid fulltext search query syntax. Error: {}. Hint: Search terms with spaces need to be enclosed in single quotes", msg),
TooExpensive => write!(f, "query is too expensive"),
Throttled => write!(f, "service is overloaded and can not run the query right now. Please try again in a few minutes"),
DeploymentReverted => write!(f, "the chain was reorganized while executing the query"),
Expand Down
12 changes: 9 additions & 3 deletions store/postgres/src/relational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -658,12 +658,18 @@ impl Layout {
}
query.load::<EntityData>(conn)
})
.map_err(|e| {
QueryExecutionError::ResolveEntitiesError(format!(
.map_err(|e| match e {
diesel::result::Error::DatabaseError(
diesel::result::DatabaseErrorKind::__Unknown,
ref info,
) if info.message().starts_with("syntax error in tsquery") => {
QueryExecutionError::FulltextQueryInvalidSyntax(info.message().to_string())
}
_ => QueryExecutionError::ResolveEntitiesError(format!(
"{}, query = {:?}",
e,
debug_query(&query_clone).to_string()
))
)),
})?;
log_query_timing(logger, &query_clone, start.elapsed(), values.len());
values
Expand Down
17 changes: 16 additions & 1 deletion store/postgres/tests/relational.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ use graph_store_postgres::layout_for_tests::SqlName;
use hex_literal::hex;
use lazy_static::lazy_static;
use std::borrow::Cow;
use std::panic;
use std::str::FromStr;
use std::sync::Arc;
use std::thread::sleep;
Expand Down Expand Up @@ -889,6 +890,7 @@ impl<'a> QueryChecker<'a> {
}

fn check(self, expected_entity_ids: Vec<&'static str>, query: EntityQuery) -> Self {
let q = query.clone();
let unordered = matches!(query.order, EntityOrder::Unordered);
let entities = self
.layout
Expand Down Expand Up @@ -921,7 +923,7 @@ impl<'a> QueryChecker<'a> {
expected_entity_ids.sort();
}

assert_eq!(entity_ids, expected_entity_ids);
assert_eq!(entity_ids, expected_entity_ids, "{:?}", q);
self
}
}
Expand Down Expand Up @@ -965,6 +967,19 @@ impl EasyOrder for EntityQuery {
}
}

#[test]
#[should_panic(
expected = "layout.query failed to execute query: FulltextQueryInvalidSyntax(\"syntax error in tsquery: \\\"Jono 'a\\\"\")"
)]
fn check_fulltext_search_syntax_error() {
run_test(move |conn, layout| {
QueryChecker::new(conn, layout).check(
vec!["1"],
user_query().filter(EntityFilter::Equal("userSearch".into(), "Jono 'a".into())),
);
});
}

#[test]
fn check_find() {
run_test(move |conn, layout| {
Expand Down

0 comments on commit ff236aa

Please sign in to comment.