Skip to content

Commit

Permalink
rename generic "query parser" to "ZQL".
Browse files Browse the repository at this point in the history
Also fix up ureq error handling with its -rc4 release.
  • Loading branch information
eeeebbbbrrrr committed Dec 20, 2020
1 parent 8abd096 commit 35b3591
Show file tree
Hide file tree
Showing 27 changed files with 165 additions and 174 deletions.
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,4 @@ foo.*
/test/sql/load-data.sql
/regression.diffs
/regression.out
/src/query_parser/parser.rs
/src/zql/parser.rs
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ serde = { version = "1.0.118", features = [ "derive" ] }
serde_json = { version = "1.0.60", features = [ "preserve_order" ] }
serde_cbor = "0.11.1"
sqlformat = "0.1.5"
ureq = { version = "2.0.0-rc3", features = [ "json" ] }
ureq = { version = "2.0.0-rc4", features = [ "json" ] }
url = "2.2.0"

[build-dependencies]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ ZomboDB allows you to use the power and scalability of Elasticsearch directly fr
- Works with current Elasticsearch releases (no plugins required)
- Query using
- Elasticsearch's [Query String Syntax](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-query-string-query.html#query-string-syntax) via `dsl.query_string()`
- ZomboDB's [custom query language](QUERY-SYNTAX.md)
- ZQL -- [ZomboDB's custom query language](ZQL.md)
- Raw Elasticsearch QueryDSL JSON
- ZomboDB's type-safe [query builder SQL syntax](QUERY-DSL.md)
- Any combination of the above, even in combination with standard SQL
Expand Down
2 changes: 1 addition & 1 deletion TUTORIAL.md
Original file line number Diff line number Diff line change
Expand Up @@ -178,7 +178,7 @@ tutorial=#
tutorial=#
```

From here, it's just a matter of coming up with a full-text query to answer your question. See the [Query Syntax documentation](QUERY-SYNTAX.md) or the [DSL Query Builder documenation](QUERY-DSL.md) for details on what the full-text query syntax can do.
From here, it's just a matter of coming up with a full-text query to answer your question. See the [Query Syntax documentation](ZQL.md) or the [DSL Query Builder documenation](QUERY-DSL.md) for details on what the full-text query syntax can do.


## Summary
Expand Down
7 changes: 4 additions & 3 deletions QUERY-SYNTAX.md → ZQL.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# Query Syntax

The ZomboDB query syntax is designed with many conveniences for text-search operations.
ZQL is designed with many conveniences for text-search operations.

An example query might look like:

Expand Down Expand Up @@ -160,11 +160,12 @@ Symbol | Description
`:~` | field contains terms matching a [regular expression](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-regexp-query.html#regexp-syntax). Note that regular expression searches are always **case sensitive**.
`:@` | ["more like this"](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-mlt-query.html)
`:@~` | ["fuzzy like this"](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-flt-field-query.html)
`==>` | uses the ["match query"](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html)
`==>` | field "matches" the term, using Elasticsearch's ["match query"](https://www.elastic.co/guide/en/elasticsearch/reference/current/query-dsl-match-query.html)

### Keywords

The list of keywords is very short: `with`, `and`, `or`, `not`, and `null`. To use one of these as a search term, simply quote it.
The list of keywords is very short: `with`, `and`, `or`, `not`, `null`, `true`, and `false`.
To use one of these as a search term, simply quote it.


## Value Lists (`[]` and `[[]]`)
Expand Down
6 changes: 3 additions & 3 deletions src/access_method/options.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use crate::elasticsearch::Elasticsearch;
use crate::gucs::{ZDB_DEFAULT_ELASTICSEARCH_URL, ZDB_DEFAULT_REPLICAS};
use crate::query_parser::ast::{IndexLink, QualifiedField};
use crate::query_parser::transformations::field_finder::find_link_for_field;
use crate::query_parser::{parse_field_lists, INDEX_LINK_PARSER};
use crate::utils::find_zdb_index;
use crate::zql::ast::{IndexLink, QualifiedField};
use crate::zql::transformations::field_finder::find_link_for_field;
use crate::zql::{parse_field_lists, INDEX_LINK_PARSER};
use lazy_static::*;
use memoffset::*;
use pgx::pg_sys::AsPgCStr;
Expand Down
2 changes: 1 addition & 1 deletion src/elasticsearch/cat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ impl ElasticsearchCatRequest {
url.push_str(&self.endpoint);
url.push_str("?h=*&format=json&time=ms&bytes=b&size=k");

Elasticsearch::execute_json_request(Elasticsearch::client().get(&url), None, |mut body| {
Elasticsearch::execute_json_request(Elasticsearch::client().get(&url), None, |body| {
let mut response = Vec::new();
body.read_to_end(&mut response)
.expect("failed to read response stream");
Expand Down
42 changes: 17 additions & 25 deletions src/elasticsearch/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ impl Elasticsearch {
ArbitraryRequestType::DELETE => Elasticsearch::client().delete(&url),
};

Elasticsearch::execute_json_request(request, post_data, |mut body| {
Elasticsearch::execute_json_request(request, post_data, |body| {
let mut response = Vec::new();
body.read_to_end(&mut response)
.expect("failed to read response stream");
Expand Down Expand Up @@ -385,12 +385,9 @@ impl Elasticsearch {
response_parser: F,
) -> std::result::Result<R, ElasticsearchError>
where
F: FnOnce(Box<dyn std::io::Read + Send>) -> std::result::Result<R, ElasticsearchError>,
F: FnOnce(&mut (dyn std::io::Read + Send)) -> std::result::Result<R, ElasticsearchError>,
{
Elasticsearch::handle_response(
response_parser,
request.error_on_non_2xx(false).send(post_data),
)
Elasticsearch::handle_response(response_parser, request.send(post_data))
}

pub fn execute_json_request<F, R>(
Expand All @@ -399,14 +396,12 @@ impl Elasticsearch {
response_parser: F,
) -> std::result::Result<R, ElasticsearchError>
where
F: FnOnce(Box<dyn std::io::Read + Send>) -> std::result::Result<R, ElasticsearchError>,
F: FnOnce(&mut (dyn std::io::Read + Send)) -> std::result::Result<R, ElasticsearchError>,
{
let response = if post_data.is_some() {
request
.error_on_non_2xx(false)
.send_json(post_data.unwrap())
request.send_json(post_data.unwrap())
} else {
request.error_on_non_2xx(false).call()
request.call()
};

Elasticsearch::handle_response(response_parser, response)
Expand All @@ -417,26 +412,23 @@ impl Elasticsearch {
response: Result<ureq::Response, ureq::Error>,
) -> Result<R, ElasticsearchError>
where
F: FnOnce(Box<dyn std::io::Read + Send>) -> std::result::Result<R, ElasticsearchError>,
F: FnOnce(&mut (dyn Read + Send)) -> std::result::Result<R, ElasticsearchError>,
{
match response {
// the request was processed by ES, but maybe not successfully
Ok(response) => {
let code = response.status();

if code != 200 {
// it wasn't a valid response code
Err(ElasticsearchError(
Some(code),
response
.into_string()
.expect("failed to convert response to a string"),
))
} else {
response_parser(Box::new(std::io::BufReader::new(response.into_reader())))
}
let mut reader = std::io::BufReader::new(response.into_reader());
response_parser(&mut reader)
}

// it wasn't a valid HTTP response code
Err(ureq::Error::Status(code, response)) => Err(ElasticsearchError(
Some(code),
response
.into_string()
.expect("failed to convert response to a string"),
)),

// the request didn't reach ES
Err(e) => Err(ElasticsearchError(None, e.to_string())),
}
Expand Down
6 changes: 3 additions & 3 deletions src/highlighting/document_highlighter.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::elasticsearch::Elasticsearch;
use crate::query_parser::ast::{IndexLink, ProximityPart, QualifiedField};
use crate::query_parser::ast::{ProximityTerm, Term};
use crate::zql::ast::{IndexLink, ProximityPart, QualifiedField};
use crate::zql::ast::{ProximityTerm, Term};
use levenshtein::*;
use pgx::PgRelation;
use pgx::*;
Expand Down Expand Up @@ -698,7 +698,7 @@ fn highlight_proximity(
#[cfg(any(test, feature = "pg_test"))]
mod tests {
use crate::highlighting::document_highlighter::{DocumentHighlighter, TokenEntry};
use crate::query_parser::ast::Term;
use crate::zql::ast::Term;
use pgx::*;
use regex::Regex;
use serde_json::*;
Expand Down
4 changes: 2 additions & 2 deletions src/highlighting/query_highlighter.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::highlighting::document_highlighter::*;
use crate::query_parser::ast::{Expr, QualifiedField, Term};
use crate::zql::ast::{Expr, QualifiedField, Term};
use pgx::*;
use pgx::{JsonB, PgRelation};
use serde_json::Value;
Expand Down Expand Up @@ -353,7 +353,7 @@ fn highlight_document(
#[cfg(any(test, feature = "pg_test"))]
mod tests {
use crate::highlighting::query_highlighter::QueryHighligther;
use crate::query_parser::ast::Expr;
use crate::zql::ast::Expr;
use pgx::*;
use serde_json::*;
use std::collections::HashSet;
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,18 @@ mod json;
mod mapping;
mod misc;
pub mod query_dsl;
pub mod query_parser;
pub mod scoring;
mod utils;
mod walker;
mod zdbquery;
pub mod zql;

pg_module_magic!();

#[allow(non_snake_case)]
#[pg_guard]
pub unsafe extern "C" fn _PG_init() {
query_parser::init();
zql::init();
gucs::init();
executor_manager::hooks::init_hooks();
access_method::options::init();
Expand Down
8 changes: 4 additions & 4 deletions src/query_dsl/zdb.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
mod dsl {
use crate::query_parser::ast::IndexLink;
use crate::query_parser::dsl::expr_to_dsl;
use crate::query_parser::INDEX_LINK_PARSER;
use crate::zdbquery::ZDBQuery;
use crate::zql::ast::IndexLink;
use crate::zql::dsl::expr_to_dsl;
use crate::zql::INDEX_LINK_PARSER;
use pgx::*;
use std::collections::HashSet;

#[pg_extern(immutable, parallel_safe)]
fn zdb(index: PgRelation, input: &str) -> ZDBQuery {
let mut used_fields = HashSet::new();
let index_links = IndexLink::from_zdb(&index);
let query = crate::query_parser::ast::Expr::from_str(
let query = crate::zql::ast::Expr::from_str(
&index,
"zdb_all",
input,
Expand Down
8 changes: 4 additions & 4 deletions src/zdbquery/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ mod opclass;

use crate::gucs::ZDB_DEFAULT_ROW_ESTIMATE;
use crate::query_dsl::nested::pg_catalog::ScoreMode;
use crate::query_parser::ast::{Expr, IndexLink, QualifiedField};
use crate::query_parser::dsl::expr_to_dsl;
use crate::query_parser::transformations::field_finder::find_link_for_field;
use crate::zql::ast::{Expr, IndexLink, QualifiedField};
use crate::zql::dsl::expr_to_dsl;
use crate::zql::transformations::field_finder::find_link_for_field;
pub use pg_catalog::*;
use serde::*;
use std::collections::{HashMap, HashSet};
Expand Down Expand Up @@ -107,8 +107,8 @@ impl PartialEq<Value> for ZDBQueryClause {

mod pg_catalog {
#![allow(non_camel_case_types)]
use crate::query_parser::ast::IndexLink;
use crate::zdbquery::ZDBQueryClause;
use crate::zql::ast::IndexLink;
use pgx::*;
use serde::*;
use serde_json::Value;
Expand Down
34 changes: 16 additions & 18 deletions src/query_parser/ast.rs → src/zql/ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,20 +11,18 @@ pub use pg_catalog::ProximityTerm;

use crate::access_method::options::ZDBIndexOptions;
use crate::elasticsearch::Elasticsearch;
use crate::query_parser::parser::Token;
use crate::query_parser::relationship_manager::RelationshipManager;
use crate::query_parser::transformations::expand::expand;
use crate::query_parser::transformations::expand_index_links::{
expand_index_links, merge_adjacent_links,
};
use crate::query_parser::transformations::field_finder::{find_fields, find_link_for_field};
use crate::query_parser::transformations::field_lists::expand_field_lists;
use crate::query_parser::transformations::index_links::assign_links;
use crate::query_parser::transformations::nested_groups::group_nested;
use crate::query_parser::transformations::prox_rewriter::rewrite_proximity_chains;
use crate::query_parser::transformations::retarget::retarget_expr;
use crate::query_parser::{INDEX_LINK_PARSER, ZDB_QUERY_PARSER};
use crate::utils::{find_zdb_index, get_null_copy_to_fields, get_search_analyzer};
use crate::zql::parser::Token;
use crate::zql::relationship_manager::RelationshipManager;
use crate::zql::transformations::expand::expand;
use crate::zql::transformations::expand_index_links::{expand_index_links, merge_adjacent_links};
use crate::zql::transformations::field_finder::{find_fields, find_link_for_field};
use crate::zql::transformations::field_lists::expand_field_lists;
use crate::zql::transformations::index_links::assign_links;
use crate::zql::transformations::nested_groups::group_nested;
use crate::zql::transformations::prox_rewriter::rewrite_proximity_chains;
use crate::zql::transformations::retarget::retarget_expr;
use crate::zql::{INDEX_LINK_PARSER, ZDB_QUERY_PARSER};

#[derive(Debug, Copy, Clone, PartialEq, Serialize, Deserialize)]
pub struct ProximityDistance {
Expand All @@ -45,7 +43,7 @@ pub mod pg_catalog {
use pgx::*;
use serde::{Deserialize, Serialize};

use crate::query_parser::ast::ProximityDistance;
use crate::zql::ast::ProximityDistance;

#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum ProximityTerm {
Expand Down Expand Up @@ -179,7 +177,7 @@ pub enum Expr<'input> {
}

impl<'input> Term<'input> {
pub(in crate::query_parser) fn maybe_make_wildcard_or_regex(
pub(in crate::zql) fn maybe_make_wildcard_or_regex(
opcode: Option<&ComparisonOpcode>,
s: &'input str,
b: Option<f32>,
Expand Down Expand Up @@ -542,7 +540,7 @@ impl<'input> Expr<'input> {
Ok(expr)
}

pub(in crate::query_parser) fn from_opcode(
pub(in crate::zql) fn from_opcode(
field: &'input str,
opcode: ComparisonOpcode,
right: Term<'input>,
Expand All @@ -568,7 +566,7 @@ impl<'input> Expr<'input> {
}
}

pub(in crate::query_parser) fn range_from_opcode(
pub(in crate::zql) fn range_from_opcode(
field: &'input str,
opcode: ComparisonOpcode,
start: &'input str,
Expand All @@ -590,7 +588,7 @@ impl<'input> Expr<'input> {
}
}

pub(in crate::query_parser) fn extract_prox_terms(
pub(in crate::zql) fn extract_prox_terms(
&self,
index: Option<&PgRelation>,
) -> Vec<ProximityTerm> {
Expand Down
6 changes: 3 additions & 3 deletions src/query_parser/dsl/mod.rs → src/zql/dsl/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,11 @@ use serde_json::json;
use crate::access_method::options::ZDBIndexOptions;
use crate::elasticsearch::aggregates::terms::terms_array_agg;
use crate::gucs::{ZDB_ACCELERATOR, ZDB_IGNORE_VISIBILITY};
use crate::query_parser::ast::{
ComparisonOpcode, Expr, IndexLink, ProximityPart, ProximityTerm, QualifiedField, Term,
};
use crate::zdbquery::mvcc::build_visibility_clause;
use crate::zdbquery::ZDBQuery;
use crate::zql::ast::{
ComparisonOpcode, Expr, IndexLink, ProximityPart, ProximityTerm, QualifiedField, Term,
};

#[pg_extern(immutable, parallel_safe)]
fn dump_query(index: PgRelation, query: ZDBQuery) -> String {
Expand Down
Loading

0 comments on commit 35b3591

Please sign in to comment.