Compare commits
18 commits
Author | SHA1 | Date | |
---|---|---|---|
|
3d10df0fcb | ||
|
18eb778720 | ||
|
eff4936dc3 | ||
|
8cca86028b | ||
|
3b21249e99 | ||
|
ebec244b01 | ||
|
a3b6b2c5db | ||
|
957ff739bb | ||
|
491b549f1d | ||
|
99db7f2a82 | ||
|
5e990d569b | ||
|
127b443f30 | ||
|
86f39ff253 | ||
|
bb8d4b41ab | ||
|
19dc7a3ac7 | ||
|
e0f357c18a | ||
|
52aef7ccc4 | ||
|
5867ce95d9 |
50 changed files with 953 additions and 553 deletions
2
.github/workflows/automoderator.yaml
vendored
2
.github/workflows/automoderator.yaml
vendored
|
@ -28,7 +28,7 @@ jobs:
|
|||
const regexReplacements = [
|
||||
// File Sharing
|
||||
{
|
||||
pattern: /(www\.)?(box|dropbox|mediafire|sugarsync|tresorit|hightail|opentext|sharefile|citrixsharefile|icloud|onedrive|1drv)\.com\/[^\s\)]+/g,
|
||||
pattern: /(www\.)?(box|dropbox|mediafire|sugarsync|tresorit|hightail|opentext|sharefile|citrixsharefile|icloud|onedrive|1drv|mega)(\.com|\.co\.nz)\/[^\s\)]+/g,
|
||||
replacement: '[REDACTED]'
|
||||
},
|
||||
// Google Drive
|
||||
|
|
|
@ -684,7 +684,7 @@ jobs:
|
|||
# Update the version to a nightly one
|
||||
sed -i "s#^version = \".*\"#version = \"${version}\"#" sdk/Cargo.toml
|
||||
sed -i "s#^version = \".*\"#version = \"${version}\"#" core/Cargo.toml
|
||||
sed -i "s#surrealdb-core = { version = \"${major}\"#surrealdb-core = { version = \"=${version}\"#" sdk/Cargo.toml
|
||||
sed -i "s#surrealdb-core = { version = \"=${currentVersion}\"#surrealdb-core = { version = \"=${version}\"#" sdk/Cargo.toml
|
||||
|
||||
- name: Patch crate name and description
|
||||
if: ${{ inputs.environment != 'stable' }}
|
||||
|
|
19
Cargo.lock
generated
19
Cargo.lock
generated
|
@ -3503,24 +3503,17 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "nanoservices-utils"
|
||||
version = "0.1.4"
|
||||
version = "0.1.5"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "c6a95cdb2ba5dc6584099b1aac8da0a6f80e12ad5f9381a19d232c1fee714793"
|
||||
checksum = "63a4117f6ebfcfe84722ef283fd6e3bfcaa141718f3ff092343bb6cb98482e03"
|
||||
dependencies = [
|
||||
"actix-web",
|
||||
"bincode",
|
||||
"bitcode",
|
||||
"bytes",
|
||||
"chrono",
|
||||
"futures",
|
||||
"jsonwebtoken",
|
||||
"paste",
|
||||
"revision 0.7.1",
|
||||
"serde",
|
||||
"serde_json",
|
||||
"thiserror",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
|
@ -5990,9 +5983,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "surrealcs"
|
||||
version = "0.3.1"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "457eb2adc59f9a9ba337e1fa3a6af63e09524e4adf01921b9d7079917cd7f694"
|
||||
checksum = "b28a50184a9a743ac180985d8b94ac4c93e4afaa0140f1057d3b11d4d36b9e8b"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"bytes",
|
||||
|
@ -6011,9 +6004,9 @@ dependencies = [
|
|||
|
||||
[[package]]
|
||||
name = "surrealcs-kernel"
|
||||
version = "0.3.1"
|
||||
version = "0.3.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f4a79da58bfc886b93a431a463f956184d4b0a9d3672833544a1b718ff86b0df"
|
||||
checksum = "ab2a5ba4af373b48fa241a81676d0172fc92e563df217282c472252b90beafee"
|
||||
dependencies = [
|
||||
"bincode",
|
||||
"chrono",
|
||||
|
|
|
@ -147,7 +147,7 @@ sha2 = "0.10.8"
|
|||
snap = "1.1.0"
|
||||
storekey = "0.5.0"
|
||||
subtle = "2.6"
|
||||
surrealcs = { version = "0.3.1", optional = true }
|
||||
surrealcs = { version = "0.3.2", optional = true }
|
||||
surrealkv = { version = "0.3.6", optional = true }
|
||||
surrealml = { version = "0.1.1", optional = true, package = "surrealml-core" }
|
||||
tempfile = { version = "3.10.1", optional = true }
|
||||
|
|
|
@ -387,8 +387,12 @@ impl<'a> Executor<'a> {
|
|||
.await;
|
||||
ctx = MutableContext::unfreeze(c)?;
|
||||
// Check if this is a RETURN statement
|
||||
let can_return =
|
||||
matches!(stm, Statement::Output(_) | Statement::Value(_));
|
||||
let can_return = matches!(
|
||||
stm,
|
||||
Statement::Output(_)
|
||||
| Statement::Value(_) | Statement::Ifelse(_)
|
||||
| Statement::Foreach(_)
|
||||
);
|
||||
// Catch global timeout
|
||||
let res = match ctx.is_timedout() {
|
||||
true => Err(Error::QueryTimedout),
|
||||
|
|
|
@ -188,7 +188,7 @@ impl Document {
|
|||
// The out field is a match, so don't error
|
||||
Value::Thing(v) if v.eq(r) => (),
|
||||
// The out is a match, so don't error
|
||||
v if l.id.is(&v) => (),
|
||||
v if r.id.is(&v) => (),
|
||||
// The out field does not match
|
||||
v => {
|
||||
return Err(Error::OutMismatch {
|
||||
|
|
|
@ -983,9 +983,11 @@ pub enum Error {
|
|||
index: String,
|
||||
},
|
||||
|
||||
/// The session has expired either because the token used
|
||||
/// to establish it has expired or because an expiration
|
||||
/// was explicitly defined when establishing it
|
||||
/// The token has expired
|
||||
#[error("The token has expired")]
|
||||
ExpiredToken,
|
||||
|
||||
/// The session has expired
|
||||
#[error("The session has expired")]
|
||||
ExpiredSession,
|
||||
|
||||
|
@ -1149,6 +1151,10 @@ pub enum Error {
|
|||
|
||||
#[error("Found a non-computed value where they are not allowed")]
|
||||
NonComputed,
|
||||
|
||||
/// Represents a failure in timestamp arithmetic related to database internals
|
||||
#[error("Failed to compute: \"{0}\", as the operation results in an overflow.")]
|
||||
ArithmeticOverflow(String),
|
||||
}
|
||||
|
||||
impl From<Error> for String {
|
||||
|
|
|
@ -153,7 +153,19 @@ pub fn time((range,): (Option<(i64, i64)>,)) -> Result<Value, Error> {
|
|||
|
||||
pub fn ulid((timestamp,): (Option<Datetime>,)) -> Result<Value, Error> {
|
||||
let ulid = match timestamp {
|
||||
Some(timestamp) => Ulid::from_datetime(timestamp.0.into()),
|
||||
Some(timestamp) => {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
if timestamp.0 < chrono::DateTime::UNIX_EPOCH {
|
||||
return Err(Error::InvalidArguments {
|
||||
name: String::from("rand::ulid"),
|
||||
message: format!(
|
||||
"To generate a ULID from a datetime, it must be a time beyond UNIX epoch."
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
Ulid::from_datetime(timestamp.0.into())
|
||||
}
|
||||
None => Ulid::new(),
|
||||
};
|
||||
|
||||
|
@ -162,7 +174,19 @@ pub fn ulid((timestamp,): (Option<Datetime>,)) -> Result<Value, Error> {
|
|||
|
||||
pub fn uuid((timestamp,): (Option<Datetime>,)) -> Result<Value, Error> {
|
||||
let uuid = match timestamp {
|
||||
Some(timestamp) => Uuid::new_v7_from_datetime(timestamp),
|
||||
Some(timestamp) => {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
if timestamp.0 < chrono::DateTime::UNIX_EPOCH {
|
||||
return Err(Error::InvalidArguments {
|
||||
name: String::from("rand::ulid"),
|
||||
message: format!(
|
||||
"To generate a ULID from a datetime, it must be a time beyond UNIX epoch."
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
Uuid::new_v7_from_datetime(timestamp)
|
||||
}
|
||||
None => Uuid::new(),
|
||||
};
|
||||
Ok(uuid.into())
|
||||
|
@ -181,7 +205,19 @@ pub mod uuid {
|
|||
|
||||
pub fn v7((timestamp,): (Option<Datetime>,)) -> Result<Value, Error> {
|
||||
let uuid = match timestamp {
|
||||
Some(timestamp) => Uuid::new_v7_from_datetime(timestamp),
|
||||
Some(timestamp) => {
|
||||
#[cfg(target_arch = "wasm32")]
|
||||
if timestamp.0 < chrono::DateTime::UNIX_EPOCH {
|
||||
return Err(Error::InvalidArguments {
|
||||
name: String::from("rand::ulid"),
|
||||
message: format!(
|
||||
"To generate a ULID from a datetime, it must be a time beyond UNIX epoch."
|
||||
),
|
||||
});
|
||||
}
|
||||
|
||||
Uuid::new_v7_from_datetime(timestamp)
|
||||
}
|
||||
None => Uuid::new(),
|
||||
};
|
||||
Ok(uuid.into())
|
||||
|
|
|
@ -1,3 +1,5 @@
|
|||
use std::ops::Deref;
|
||||
|
||||
use crate::ctx::Context;
|
||||
use crate::dbs::Options;
|
||||
use crate::doc::CursorDoc;
|
||||
|
@ -146,6 +148,7 @@ pub fn thing((arg1, arg2): (Value, Option<Value>)) -> Result<Value, Error> {
|
|||
Value::Array(v) => v.into(),
|
||||
Value::Object(v) => v.into(),
|
||||
Value::Number(v) => v.into(),
|
||||
Value::Range(v) => v.deref().to_owned().try_into()?,
|
||||
v => v.as_string().into(),
|
||||
},
|
||||
})),
|
||||
|
|
|
@ -185,7 +185,7 @@ pub async fn generate_schema(
|
|||
let desc = current.get("desc");
|
||||
match (asc, desc) {
|
||||
(Some(_), Some(_)) => {
|
||||
return Err("Found both asc and desc in order".into());
|
||||
return Err("Found both ASC and DESC in order".into());
|
||||
}
|
||||
(Some(GqlValue::Enum(a)), None) => {
|
||||
orders.push(order!(asc, a.as_str()))
|
||||
|
@ -281,7 +281,7 @@ pub async fn generate_schema(
|
|||
})
|
||||
},
|
||||
)
|
||||
.description(format!("Generated from table `{}`{}\nallows querying a table with filters", tb.name, if let Some(ref c) = &tb.comment {format!("\n{c}")} else {"".to_string()}))
|
||||
.description(format!("{}", if let Some(ref c) = &tb.comment { format!("{c}") } else { format!("Generated from table `{}`\nallows querying a table with filters", tb.name) }))
|
||||
.argument(limit_input!())
|
||||
.argument(start_input!())
|
||||
.argument(InputValue::new("order", TypeRef::named(&table_order_name)))
|
||||
|
@ -329,12 +329,11 @@ pub async fn generate_schema(
|
|||
},
|
||||
)
|
||||
.description(format!(
|
||||
"Generated from table `{}`{}\nallows querying a single record in a table by id",
|
||||
tb.name,
|
||||
"{}",
|
||||
if let Some(ref c) = &tb.comment {
|
||||
format!("\n{c}")
|
||||
format!("{c}")
|
||||
} else {
|
||||
"".to_string()
|
||||
format!("Generated from table `{}`\nallows querying a single record in a table by ID", tb.name)
|
||||
}
|
||||
))
|
||||
.argument(id_input!()),
|
||||
|
@ -371,11 +370,20 @@ pub async fn generate_schema(
|
|||
table_filter = table_filter
|
||||
.field(InputValue::new(fd.name.to_string(), TypeRef::named(type_filter_name)));
|
||||
|
||||
table_ty_obj = table_ty_obj.field(Field::new(
|
||||
fd.name.to_string(),
|
||||
fd_type,
|
||||
make_table_field_resolver(fd_name.as_str(), fd.kind.clone()),
|
||||
));
|
||||
table_ty_obj = table_ty_obj
|
||||
.field(Field::new(
|
||||
fd.name.to_string(),
|
||||
fd_type,
|
||||
make_table_field_resolver(fd_name.as_str(), fd.kind.clone()),
|
||||
))
|
||||
.description(format!(
|
||||
"{}",
|
||||
if let Some(ref c) = fd.comment {
|
||||
format!("{c}")
|
||||
} else {
|
||||
"".to_string()
|
||||
}
|
||||
));
|
||||
}
|
||||
|
||||
types.push(Type::Object(table_ty_obj));
|
||||
|
@ -421,7 +429,7 @@ pub async fn generate_schema(
|
|||
}
|
||||
})
|
||||
})
|
||||
.description("allows fetching arbitrary records".to_string())
|
||||
.description("Allows fetching arbitrary records".to_string())
|
||||
.argument(id_input!()),
|
||||
);
|
||||
|
||||
|
@ -476,7 +484,7 @@ pub async fn generate_schema(
|
|||
schema,
|
||||
"uuid",
|
||||
Kind::Uuid,
|
||||
"a string encoded uuid",
|
||||
"String encoded UUID",
|
||||
"https://datatracker.ietf.org/doc/html/rfc4122"
|
||||
);
|
||||
|
||||
|
|
|
@ -209,8 +209,14 @@ pub async fn db_access(
|
|||
}
|
||||
Err(e) => match e {
|
||||
Error::Thrown(_) => Err(e),
|
||||
e if *INSECURE_FORWARD_ACCESS_ERRORS => Err(e),
|
||||
_ => Err(Error::AccessRecordSigninQueryFailed),
|
||||
e => {
|
||||
debug!("Record user signin query failed: {e}");
|
||||
if *INSECURE_FORWARD_ACCESS_ERRORS {
|
||||
Err(e)
|
||||
} else {
|
||||
Err(Error::AccessRecordSigninQueryFailed)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
@ -221,6 +227,9 @@ pub async fn db_access(
|
|||
// TODO(gguillemas): Remove this once bearer access is no longer experimental.
|
||||
if !*EXPERIMENTAL_BEARER_ACCESS {
|
||||
// Return opaque error to avoid leaking the existence of the feature.
|
||||
debug!(
|
||||
"Error attempting to authenticate with disabled bearer access feature"
|
||||
);
|
||||
return Err(Error::InvalidAuth);
|
||||
}
|
||||
// Check if the bearer access method supports issuing tokens.
|
||||
|
@ -236,7 +245,10 @@ pub async fn db_access(
|
|||
let gr = match tx.get_db_access_grant(&ns, &db, &ac, &kid).await {
|
||||
Ok(gr) => gr,
|
||||
// Return opaque error to avoid leaking existence of the grant.
|
||||
_ => return Err(Error::InvalidAuth),
|
||||
Err(e) => {
|
||||
debug!("Error retrieving bearer access grant: {e}");
|
||||
return Err(Error::InvalidAuth);
|
||||
}
|
||||
};
|
||||
// Ensure that the transaction is cancelled.
|
||||
tx.cancel().await?;
|
||||
|
@ -248,7 +260,7 @@ pub async fn db_access(
|
|||
let tx = kvs.transaction(Read, Optimistic).await?;
|
||||
// Fetch the specified user from storage.
|
||||
let user = tx.get_db_user(&ns, &db, user).await.map_err(|e| {
|
||||
trace!("Error while authenticating to database `{ns}/{db}`: {e}");
|
||||
debug!("Error retrieving user for bearer access to database `{ns}/{db}`: {e}");
|
||||
// Return opaque error to avoid leaking grant subject existence.
|
||||
Error::InvalidAuth
|
||||
})?;
|
||||
|
@ -363,7 +375,7 @@ pub async fn db_user(
|
|||
..Claims::default()
|
||||
};
|
||||
// Log the authenticated database info
|
||||
trace!("Signing in to database `{}`", db);
|
||||
trace!("Signing in to database `{ns}/{db}`");
|
||||
// Create the authentication token
|
||||
let enc = encode(&HEADER, &val, &key);
|
||||
// Set the authentication on the session
|
||||
|
@ -379,7 +391,11 @@ pub async fn db_user(
|
|||
_ => Err(Error::TokenMakingFailed),
|
||||
}
|
||||
}
|
||||
_ => Err(Error::InvalidAuth),
|
||||
// The password did not verify
|
||||
Err(e) => {
|
||||
debug!("Failed to verify signin credentials for user `{user}` in database `{ns}/{db}`: {e}");
|
||||
Err(Error::InvalidAuth)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -405,6 +421,9 @@ pub async fn ns_access(
|
|||
// TODO(gguillemas): Remove this once bearer access is no longer experimental.
|
||||
if !*EXPERIMENTAL_BEARER_ACCESS {
|
||||
// Return opaque error to avoid leaking the existence of the feature.
|
||||
debug!(
|
||||
"Error attempting to authenticate with disabled bearer access feature"
|
||||
);
|
||||
return Err(Error::InvalidAuth);
|
||||
}
|
||||
// Check if the bearer access method supports issuing tokens.
|
||||
|
@ -420,7 +439,10 @@ pub async fn ns_access(
|
|||
let gr = match tx.get_ns_access_grant(&ns, &ac, &kid).await {
|
||||
Ok(gr) => gr,
|
||||
// Return opaque error to avoid leaking existence of the grant.
|
||||
_ => return Err(Error::InvalidAuth),
|
||||
Err(e) => {
|
||||
debug!("Error retrieving bearer access grant: {e}");
|
||||
return Err(Error::InvalidAuth);
|
||||
}
|
||||
};
|
||||
// Ensure that the transaction is cancelled.
|
||||
tx.cancel().await?;
|
||||
|
@ -431,11 +453,12 @@ pub async fn ns_access(
|
|||
// Create a new readonly transaction.
|
||||
let tx = kvs.transaction(Read, Optimistic).await?;
|
||||
// Fetch the specified user from storage.
|
||||
let user = tx.get_ns_user(&ns, user).await.map_err(|e| {
|
||||
trace!("Error while authenticating to namespace `{ns}`: {e}");
|
||||
// Return opaque error to avoid leaking grant subject existence.
|
||||
Error::InvalidAuth
|
||||
})?;
|
||||
let user =
|
||||
tx.get_ns_user(&ns, user).await.map_err(|e| {
|
||||
debug!("Error retrieving user for bearer access to namespace `{ns}`: {e}");
|
||||
// Return opaque error to avoid leaking grant subject existence.
|
||||
Error::InvalidAuth
|
||||
})?;
|
||||
// Ensure that the transaction is cancelled.
|
||||
tx.cancel().await?;
|
||||
user.roles.clone()
|
||||
|
@ -533,7 +556,7 @@ pub async fn ns_user(
|
|||
..Claims::default()
|
||||
};
|
||||
// Log the authenticated namespace info
|
||||
trace!("Signing in to namespace `{}`", ns);
|
||||
trace!("Signing in to namespace `{ns}`");
|
||||
// Create the authentication token
|
||||
let enc = encode(&HEADER, &val, &key);
|
||||
// Set the authentication on the session
|
||||
|
@ -549,7 +572,12 @@ pub async fn ns_user(
|
|||
}
|
||||
}
|
||||
// The password did not verify
|
||||
_ => Err(Error::InvalidAuth),
|
||||
Err(e) => {
|
||||
debug!(
|
||||
"Failed to verify signin credentials for user `{user}` in namespace `{ns}`: {e}"
|
||||
);
|
||||
Err(Error::InvalidAuth)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -589,7 +617,10 @@ pub async fn root_user(
|
|||
}
|
||||
}
|
||||
// The password did not verify
|
||||
_ => Err(Error::InvalidAuth),
|
||||
Err(e) => {
|
||||
debug!("Failed to verify signin credentials for user `{user}` in root: {e}");
|
||||
Err(Error::InvalidAuth)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -614,6 +645,9 @@ pub async fn root_access(
|
|||
// TODO(gguillemas): Remove this once bearer access is no longer experimental.
|
||||
if !*EXPERIMENTAL_BEARER_ACCESS {
|
||||
// Return opaque error to avoid leaking the existence of the feature.
|
||||
debug!(
|
||||
"Error attempting to authenticate with disabled bearer access feature"
|
||||
);
|
||||
return Err(Error::InvalidAuth);
|
||||
}
|
||||
// Check if the bearer access method supports issuing tokens.
|
||||
|
@ -629,7 +663,10 @@ pub async fn root_access(
|
|||
let gr = match tx.get_root_access_grant(&ac, &kid).await {
|
||||
Ok(gr) => gr,
|
||||
// Return opaque error to avoid leaking existence of the grant.
|
||||
_ => return Err(Error::InvalidAuth),
|
||||
Err(e) => {
|
||||
debug!("Error retrieving bearer access grant: {e}");
|
||||
return Err(Error::InvalidAuth);
|
||||
}
|
||||
};
|
||||
// Ensure that the transaction is cancelled.
|
||||
tx.cancel().await?;
|
||||
|
@ -641,7 +678,7 @@ pub async fn root_access(
|
|||
let tx = kvs.transaction(Read, Optimistic).await?;
|
||||
// Fetch the specified user from storage.
|
||||
let user = tx.get_root_user(user).await.map_err(|e| {
|
||||
trace!("Error while authenticating to root: {e}");
|
||||
debug!("Error retrieving user for bearer access to root: {e}");
|
||||
// Return opaque error to avoid leaking grant subject existence.
|
||||
Error::InvalidAuth
|
||||
})?;
|
||||
|
@ -753,10 +790,14 @@ pub fn verify_grant_bearer(gr: &Arc<AccessGrant>, key: String) -> Result<(), Err
|
|||
(Some(exp), None) => {
|
||||
if exp < &Datetime::default() {
|
||||
// Return opaque error to avoid leaking revocation status.
|
||||
debug!("Bearer access grant `{}` for method `{}` is expired", gr.id, gr.ac);
|
||||
return Err(Error::InvalidAuth);
|
||||
}
|
||||
}
|
||||
_ => return Err(Error::InvalidAuth),
|
||||
(_, Some(_)) => {
|
||||
debug!("Bearer access grant `{}` for method `{}` is revoked", gr.id, gr.ac);
|
||||
return Err(Error::InvalidAuth);
|
||||
}
|
||||
}
|
||||
// Check if the provided key matches the bearer key in the grant.
|
||||
// We use time-constant comparison to prevent timing attacks.
|
||||
|
@ -765,6 +806,7 @@ pub fn verify_grant_bearer(gr: &Arc<AccessGrant>, key: String) -> Result<(), Err
|
|||
let signin_key_bytes: &[u8] = key.as_bytes();
|
||||
let ok: bool = grant_key_bytes.ct_eq(signin_key_bytes).into();
|
||||
if !ok {
|
||||
debug!("Bearer access grant `{}` for method `{}` is invalid", gr.id, gr.ac);
|
||||
return Err(Error::InvalidAuth);
|
||||
}
|
||||
};
|
||||
|
|
|
@ -77,7 +77,7 @@ pub async fn db_access(
|
|||
sess.or.clone_from(&session.or);
|
||||
// Compute the value with the params
|
||||
match kvs.evaluate(val, &sess, vars).await {
|
||||
// The signin value succeeded
|
||||
// The signup value succeeded
|
||||
Ok(val) => {
|
||||
match val.record() {
|
||||
// There is a record returned
|
||||
|
@ -113,7 +113,10 @@ pub async fn db_access(
|
|||
// Update rid with result from AUTHENTICATE clause
|
||||
rid = id;
|
||||
}
|
||||
_ => return Err(Error::InvalidAuth),
|
||||
_ => {
|
||||
debug!("Authentication attempt as record user rejected by AUTHENTICATE clause");
|
||||
return Err(Error::InvalidAuth);
|
||||
}
|
||||
},
|
||||
Err(e) => return match e {
|
||||
Error::Thrown(_) => Err(e),
|
||||
|
@ -153,8 +156,14 @@ pub async fn db_access(
|
|||
}
|
||||
Err(e) => match e {
|
||||
Error::Thrown(_) => Err(e),
|
||||
e if *INSECURE_FORWARD_ACCESS_ERRORS => Err(e),
|
||||
_ => Err(Error::AccessRecordSignupQueryFailed),
|
||||
e => {
|
||||
debug!("Record user signup query failed: {e}");
|
||||
if *INSECURE_FORWARD_ACCESS_ERRORS {
|
||||
Err(e)
|
||||
} else {
|
||||
Err(Error::AccessRecordSignupQueryFailed)
|
||||
}
|
||||
}
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
|
@ -120,7 +120,12 @@ pub async fn basic(
|
|||
}
|
||||
Err(err) => Err(err),
|
||||
},
|
||||
(None, Some(_)) => Err(Error::InvalidAuth),
|
||||
(None, Some(db)) => {
|
||||
debug!(
|
||||
"Attempted basic authentication in database '{db}' without specifying a namespace"
|
||||
);
|
||||
Err(Error::InvalidAuth)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -134,15 +139,15 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
// Check if the auth token can be used
|
||||
if let Some(nbf) = token_data.claims.nbf {
|
||||
if nbf > Utc::now().timestamp() {
|
||||
trace!("The 'nbf' field in the authentication token was invalid");
|
||||
debug!("Token verification failed due to the 'nbf' claim containing a future time");
|
||||
return Err(Error::InvalidAuth);
|
||||
}
|
||||
}
|
||||
// Check if the auth token has expired
|
||||
if let Some(exp) = token_data.claims.exp {
|
||||
if exp < Utc::now().timestamp() {
|
||||
trace!("The 'exp' field in the authentication token was invalid");
|
||||
return Err(Error::InvalidAuth);
|
||||
debug!("Token verification failed due to the 'exp' claim containing a past time");
|
||||
return Err(Error::ExpiredToken);
|
||||
}
|
||||
}
|
||||
// Check the token authentication claims
|
||||
|
@ -183,7 +188,7 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
_ => return Err(Error::AccessMethodMismatch),
|
||||
};
|
||||
// Verify the token
|
||||
decode::<Claims>(token, &cf.0, &cf.1)?;
|
||||
verify_token(token, &cf.0, &cf.1)?;
|
||||
// AUTHENTICATE clause
|
||||
if let Some(au) = &de.authenticate {
|
||||
// Setup the system session for finding the signin record
|
||||
|
@ -244,7 +249,7 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
_ => return Err(Error::AccessMethodMismatch),
|
||||
}?;
|
||||
// Verify the token
|
||||
decode::<Claims>(token, &cf.0, &cf.1)?;
|
||||
verify_token(token, &cf.0, &cf.1)?;
|
||||
// AUTHENTICATE clause
|
||||
if let Some(au) = &de.authenticate {
|
||||
// Setup the system session for executing the clause
|
||||
|
@ -285,7 +290,7 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
// The clause can make up for the missing "id" claim by resolving other claims to a specific record
|
||||
AccessType::Record(at) => match &de.authenticate {
|
||||
Some(au) => {
|
||||
trace!("Access method `{}` is record access with authenticate clause", ac);
|
||||
trace!("Access method `{}` is record access with AUTHENTICATE clause", ac);
|
||||
let cf = match &at.jwt.verify {
|
||||
JwtAccessVerify::Key(key) => config(key.alg, key.key.as_bytes()),
|
||||
#[cfg(feature = "jwks")]
|
||||
|
@ -301,7 +306,7 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
}?;
|
||||
|
||||
// Verify the token
|
||||
decode::<Claims>(token, &cf.0, &cf.1)?;
|
||||
verify_token(token, &cf.0, &cf.1)?;
|
||||
// AUTHENTICATE clause
|
||||
// Setup the system session for finding the signin record
|
||||
let mut sess = Session::editor().with_ns(ns).with_db(db);
|
||||
|
@ -342,7 +347,7 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
let tx = kvs.transaction(Read, Optimistic).await?;
|
||||
// Get the database user
|
||||
let de = tx.get_db_user(ns, db, id).await.map_err(|e| {
|
||||
trace!("Error while authenticating to database `{db}`: {e}");
|
||||
debug!("Error while authenticating to database `{db}`: {e}");
|
||||
Error::InvalidAuth
|
||||
})?;
|
||||
// Ensure that the transaction is cancelled
|
||||
|
@ -350,9 +355,9 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
// Check the algorithm
|
||||
let cf = config(Algorithm::Hs512, de.code.as_bytes())?;
|
||||
// Verify the token
|
||||
decode::<Claims>(token, &cf.0, &cf.1)?;
|
||||
verify_token(token, &cf.0, &cf.1)?;
|
||||
// Log the success
|
||||
debug!("Authenticated to database `{}` with user `{}`", db, id);
|
||||
debug!("Authenticated to database `{}` with user `{}` using token", db, id);
|
||||
// Set the session
|
||||
session.tk = Some(value);
|
||||
session.ns = Some(ns.to_owned());
|
||||
|
@ -397,7 +402,7 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
_ => return Err(Error::AccessMethodMismatch),
|
||||
}?;
|
||||
// Verify the token
|
||||
decode::<Claims>(token, &cf.0, &cf.1)?;
|
||||
verify_token(token, &cf.0, &cf.1)?;
|
||||
// AUTHENTICATE clause
|
||||
if let Some(au) = &de.authenticate {
|
||||
// Setup the system session for executing the clause
|
||||
|
@ -420,7 +425,7 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
.collect::<Result<Vec<_>, _>>()?,
|
||||
};
|
||||
// Log the success
|
||||
trace!("Authenticated to namespace `{}` with access method `{}`", ns, ac);
|
||||
debug!("Authenticated to namespace `{}` with access method `{}`", ns, ac);
|
||||
// Set the session
|
||||
session.tk = Some(value);
|
||||
session.ns = Some(ns.to_owned());
|
||||
|
@ -445,7 +450,7 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
let tx = kvs.transaction(Read, Optimistic).await?;
|
||||
// Get the namespace user
|
||||
let de = tx.get_ns_user(ns, id).await.map_err(|e| {
|
||||
trace!("Error while authenticating to namespace `{ns}`: {e}");
|
||||
debug!("Error while authenticating to namespace `{ns}`: {e}");
|
||||
Error::InvalidAuth
|
||||
})?;
|
||||
// Ensure that the transaction is cancelled
|
||||
|
@ -453,9 +458,9 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
// Check the algorithm
|
||||
let cf = config(Algorithm::Hs512, de.code.as_bytes())?;
|
||||
// Verify the token
|
||||
decode::<Claims>(token, &cf.0, &cf.1)?;
|
||||
verify_token(token, &cf.0, &cf.1)?;
|
||||
// Log the success
|
||||
trace!("Authenticated to namespace `{}` with user `{}`", ns, id);
|
||||
debug!("Authenticated to namespace `{}` with user `{}` using token", ns, id);
|
||||
// Set the session
|
||||
session.tk = Some(value);
|
||||
session.ns = Some(ns.to_owned());
|
||||
|
@ -498,7 +503,7 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
_ => return Err(Error::AccessMethodMismatch),
|
||||
}?;
|
||||
// Verify the token
|
||||
decode::<Claims>(token, &cf.0, &cf.1)?;
|
||||
verify_token(token, &cf.0, &cf.1)?;
|
||||
// AUTHENTICATE clause
|
||||
if let Some(au) = &de.authenticate {
|
||||
// Setup the system session for executing the clause
|
||||
|
@ -521,7 +526,7 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
.collect::<Result<Vec<_>, _>>()?,
|
||||
};
|
||||
// Log the success
|
||||
trace!("Authenticated to root with access method `{}`", ac);
|
||||
debug!("Authenticated to root with access method `{}`", ac);
|
||||
// Set the session
|
||||
session.tk = Some(value);
|
||||
session.ac = Some(ac.to_owned());
|
||||
|
@ -540,7 +545,7 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
let tx = kvs.transaction(Read, Optimistic).await?;
|
||||
// Get the namespace user
|
||||
let de = tx.get_root_user(id).await.map_err(|e| {
|
||||
trace!("Error while authenticating to root: {e}");
|
||||
debug!("Error while authenticating to root: {e}");
|
||||
Error::InvalidAuth
|
||||
})?;
|
||||
// Ensure that the transaction is cancelled
|
||||
|
@ -548,9 +553,9 @@ pub async fn token(kvs: &Datastore, session: &mut Session, token: &str) -> Resul
|
|||
// Check the algorithm
|
||||
let cf = config(Algorithm::Hs512, de.code.as_bytes())?;
|
||||
// Verify the token
|
||||
decode::<Claims>(token, &cf.0, &cf.1)?;
|
||||
verify_token(token, &cf.0, &cf.1)?;
|
||||
// Log the success
|
||||
trace!("Authenticated to root level with user `{}`", id);
|
||||
debug!("Authenticated to root level with user `{}` using token", id);
|
||||
// Set the session
|
||||
session.tk = Some(value);
|
||||
session.exp = expiration(de.duration.session)?;
|
||||
|
@ -575,7 +580,7 @@ pub async fn verify_root_creds(
|
|||
let tx = ds.transaction(Read, Optimistic).await?;
|
||||
// Fetch the specified user from storage
|
||||
let user = tx.get_root_user(user).await.map_err(|e| {
|
||||
trace!("Error while authenticating to root: {e}");
|
||||
debug!("Error retrieving user for authentication to root: {e}");
|
||||
Error::InvalidAuth
|
||||
})?;
|
||||
// Ensure that the transaction is cancelled
|
||||
|
@ -598,7 +603,7 @@ pub async fn verify_ns_creds(
|
|||
let tx = ds.transaction(Read, Optimistic).await?;
|
||||
// Fetch the specified user from storage
|
||||
let user = tx.get_ns_user(ns, user).await.map_err(|e| {
|
||||
trace!("Error while authenticating to namespace `{ns}`: {e}");
|
||||
debug!("Error retrieving user for authentication to namespace `{ns}`: {e}");
|
||||
Error::InvalidAuth
|
||||
})?;
|
||||
// Ensure that the transaction is cancelled
|
||||
|
@ -622,7 +627,7 @@ pub async fn verify_db_creds(
|
|||
let tx = ds.transaction(Read, Optimistic).await?;
|
||||
// Fetch the specified user from storage
|
||||
let user = tx.get_db_user(ns, db, user).await.map_err(|e| {
|
||||
trace!("Error while authenticating to database `{ns}/{db}`: {e}");
|
||||
debug!("Error retrieving user for authentication to database `{ns}/{db}`: {e}");
|
||||
Error::InvalidAuth
|
||||
})?;
|
||||
// Ensure that the transaction is cancelled
|
||||
|
@ -656,7 +661,10 @@ pub async fn authenticate_record(
|
|||
// If the AUTHENTICATE clause returns a record, authentication continues with that record
|
||||
Some(id) => Ok(id),
|
||||
// If the AUTHENTICATE clause returns anything else, authentication fails generically
|
||||
_ => Err(Error::InvalidAuth),
|
||||
_ => {
|
||||
debug!("Authentication attempt as record user rejected by AUTHENTICATE clause");
|
||||
Err(Error::InvalidAuth)
|
||||
}
|
||||
},
|
||||
Err(e) => match e {
|
||||
// If the AUTHENTICATE clause throws a specific error, authentication fails with that error
|
||||
|
@ -679,7 +687,10 @@ pub async fn authenticate_generic(
|
|||
// If the AUTHENTICATE clause returns nothing, authentication continues
|
||||
Value::None => Ok(()),
|
||||
// If the AUTHENTICATE clause returns anything else, authentication fails generically
|
||||
_ => Err(Error::InvalidAuth),
|
||||
_ => {
|
||||
debug!("Authentication attempt as system user rejected by AUTHENTICATE clause");
|
||||
Err(Error::InvalidAuth)
|
||||
}
|
||||
}
|
||||
}
|
||||
Err(e) => match e {
|
||||
|
@ -691,6 +702,22 @@ pub async fn authenticate_generic(
|
|||
}
|
||||
}
|
||||
|
||||
fn verify_token(token: &str, key: &DecodingKey, validation: &Validation) -> Result<(), Error> {
|
||||
match decode::<Claims>(token, key, validation) {
|
||||
Ok(_) => Ok(()),
|
||||
Err(err) => {
|
||||
// Only transparently return certain token verification errors
|
||||
match err.kind() {
|
||||
jsonwebtoken::errors::ErrorKind::ExpiredSignature => Err(Error::ExpiredToken),
|
||||
_ => {
|
||||
debug!("Error verifying authentication token: {err}");
|
||||
Err(Error::InvalidAuth)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
@ -1512,7 +1539,11 @@ mod tests {
|
|||
let mut sess = Session::default();
|
||||
let res = token(&ds, &mut sess, &enc).await;
|
||||
|
||||
assert!(res.is_err(), "Unexpected success signing in with expired token: {:?}", res);
|
||||
match res {
|
||||
Err(Error::ExpiredToken) => {} // ok
|
||||
Err(err) => panic!("Unexpected error signing in with expired token: {:?}", err),
|
||||
res => panic!("Unexpected success signing in with expired token: {:?}", res),
|
||||
}
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
|
|
|
@ -116,7 +116,7 @@ impl Filter {
|
|||
|
||||
#[inline]
|
||||
fn uppercase(c: &str) -> FilterResult {
|
||||
Self::check_term(c, c.to_lowercase())
|
||||
Self::check_term(c, c.to_uppercase())
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
@ -842,4 +842,32 @@ mod tests {
|
|||
)
|
||||
.await;
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn test_uppercase_tokens() {
|
||||
test_analyzer_tokens(
|
||||
"ANALYZER test TOKENIZERS blank,class FILTERS uppercase",
|
||||
"Ālea IactA!",
|
||||
&[
|
||||
Token::String {
|
||||
chars: (0, 0, 4),
|
||||
bytes: (0, 5),
|
||||
term: "ĀLEA".to_string(),
|
||||
len: 4,
|
||||
},
|
||||
Token::String {
|
||||
chars: (5, 5, 10),
|
||||
bytes: (6, 11),
|
||||
term: "IACTA".to_string(),
|
||||
len: 5,
|
||||
},
|
||||
Token::Ref {
|
||||
chars: (10, 10, 11),
|
||||
bytes: (11, 12),
|
||||
len: 1,
|
||||
},
|
||||
],
|
||||
)
|
||||
.await;
|
||||
}
|
||||
}
|
||||
|
|
|
@ -112,7 +112,15 @@ impl QueryPlanner {
|
|||
tree.knn_condition,
|
||||
)
|
||||
.await?;
|
||||
match PlanBuilder::build(tree.root, params, tree.with_indexes, order)? {
|
||||
match PlanBuilder::build(
|
||||
tree.root,
|
||||
params,
|
||||
tree.with_indexes,
|
||||
order,
|
||||
tree.all_and_groups,
|
||||
tree.all_and,
|
||||
tree.all_expressions_with_index,
|
||||
)? {
|
||||
Plan::SingleIndex(exp, io) => {
|
||||
if io.require_distinct() {
|
||||
self.requires_distinct = true;
|
||||
|
|
|
@ -17,33 +17,27 @@ pub(super) struct PlanBuilder {
|
|||
has_indexes: bool,
|
||||
/// List of expressions that are not ranges, backed by an index
|
||||
non_range_indexes: Vec<(Arc<Expression>, IndexOption)>,
|
||||
/// List of indexes involved in this plan
|
||||
with_indexes: Vec<IndexRef>,
|
||||
/// List of indexes allowed in this plan
|
||||
with_indexes: Option<Vec<IndexRef>>,
|
||||
/// Group each possible optimisations local to a SubQuery
|
||||
groups: BTreeMap<GroupRef, Group>, // The order matters because we want the plan to be consistent across repeated queries.
|
||||
/// Does a group contains only AND relations?
|
||||
all_and_groups: HashMap<GroupRef, bool>,
|
||||
/// Does the whole query contains only AND relations?
|
||||
all_and: bool,
|
||||
/// Is every expression backed by an index?
|
||||
all_exp_with_index: bool,
|
||||
}
|
||||
|
||||
impl PlanBuilder {
|
||||
pub(super) fn build(
|
||||
root: Option<Node>,
|
||||
params: &QueryPlannerParams,
|
||||
with_indexes: Vec<IndexRef>,
|
||||
with_indexes: Option<Vec<IndexRef>>,
|
||||
order: Option<IndexOption>,
|
||||
all_and_groups: HashMap<GroupRef, bool>,
|
||||
all_and: bool,
|
||||
all_expressions_with_index: bool,
|
||||
) -> Result<Plan, Error> {
|
||||
let mut b = PlanBuilder {
|
||||
has_indexes: false,
|
||||
non_range_indexes: Default::default(),
|
||||
groups: Default::default(),
|
||||
with_indexes,
|
||||
all_and_groups: Default::default(),
|
||||
all_and: true,
|
||||
all_exp_with_index: true,
|
||||
};
|
||||
|
||||
// If we only count and there are no conditions and no aggregations, then we can only scan keys
|
||||
|
@ -61,7 +55,7 @@ impl PlanBuilder {
|
|||
}
|
||||
|
||||
// If every boolean operator are AND then we can use the single index plan
|
||||
if b.all_and {
|
||||
if all_and {
|
||||
// TODO: This is currently pretty arbitrary
|
||||
// We take the "first" range query if one is available
|
||||
if let Some((_, group)) = b.groups.into_iter().next() {
|
||||
|
@ -79,10 +73,10 @@ impl PlanBuilder {
|
|||
}
|
||||
}
|
||||
// If every expression is backed by an index with can use the MultiIndex plan
|
||||
else if b.all_exp_with_index {
|
||||
else if all_expressions_with_index {
|
||||
let mut ranges = Vec::with_capacity(b.groups.len());
|
||||
for (depth, group) in b.groups {
|
||||
if b.all_and_groups.get(&depth) == Some(&true) {
|
||||
for (gr, group) in b.groups {
|
||||
if all_and_groups.get(&gr) == Some(&true) {
|
||||
group.take_union_ranges(&mut ranges);
|
||||
} else {
|
||||
group.take_intersect_ranges(&mut ranges);
|
||||
|
@ -100,9 +94,11 @@ impl PlanBuilder {
|
|||
|
||||
// Check if we have an explicit list of index we can use
|
||||
fn filter_index_option(&self, io: Option<&IndexOption>) -> Option<IndexOption> {
|
||||
if let Some(io) = &io {
|
||||
if !self.with_indexes.is_empty() && !self.with_indexes.contains(&io.ix_ref()) {
|
||||
return None;
|
||||
if let Some(io) = io {
|
||||
if let Some(wi) = &self.with_indexes {
|
||||
if !wi.contains(&io.ix_ref()) {
|
||||
return None;
|
||||
}
|
||||
}
|
||||
}
|
||||
io.cloned()
|
||||
|
@ -117,11 +113,8 @@ impl PlanBuilder {
|
|||
right,
|
||||
exp,
|
||||
} => {
|
||||
let is_bool = self.check_boolean_operator(*group, exp.operator());
|
||||
if let Some(io) = self.filter_index_option(io.as_ref()) {
|
||||
self.add_index_option(*group, exp.clone(), io);
|
||||
} else if self.all_exp_with_index && !is_bool {
|
||||
self.all_exp_with_index = false;
|
||||
}
|
||||
self.eval_node(left)?;
|
||||
self.eval_node(right)?;
|
||||
|
@ -132,26 +125,6 @@ impl PlanBuilder {
|
|||
}
|
||||
}
|
||||
|
||||
fn check_boolean_operator(&mut self, gr: GroupRef, op: &Operator) -> bool {
|
||||
match op {
|
||||
Operator::Neg | Operator::Or => {
|
||||
if self.all_and {
|
||||
self.all_and = false;
|
||||
}
|
||||
self.all_and_groups.entry(gr).and_modify(|b| *b = false).or_insert(false);
|
||||
true
|
||||
}
|
||||
Operator::And => {
|
||||
self.all_and_groups.entry(gr).or_insert(true);
|
||||
true
|
||||
}
|
||||
_ => {
|
||||
self.all_and_groups.entry(gr).or_insert(true);
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn add_index_option(&mut self, group_ref: GroupRef, exp: Arc<Expression>, io: IndexOption) {
|
||||
if let IndexOperator::RangePart(_, _) = io.op() {
|
||||
let level = self.groups.entry(group_ref).or_default();
|
||||
|
|
|
@ -20,10 +20,16 @@ use std::sync::Arc;
|
|||
pub(super) struct Tree {
|
||||
pub(super) root: Option<Node>,
|
||||
pub(super) index_map: IndexesMap,
|
||||
pub(super) with_indexes: Vec<IndexRef>,
|
||||
pub(super) with_indexes: Option<Vec<IndexRef>>,
|
||||
pub(super) knn_expressions: KnnExpressions,
|
||||
pub(super) knn_brute_force_expressions: KnnBruteForceExpressions,
|
||||
pub(super) knn_condition: Option<Cond>,
|
||||
/// Is every expression backed by an index?
|
||||
pub(super) all_expressions_with_index: bool,
|
||||
/// Does the whole query contains only AND relations?
|
||||
pub(super) all_and: bool,
|
||||
/// Does a group contains only AND relations?
|
||||
pub(super) all_and_groups: HashMap<GroupRef, bool>,
|
||||
}
|
||||
|
||||
impl Tree {
|
||||
|
@ -50,6 +56,10 @@ impl Tree {
|
|||
knn_expressions: b.knn_expressions,
|
||||
knn_brute_force_expressions: b.knn_brute_force_expressions,
|
||||
knn_condition: b.knn_condition,
|
||||
all_expressions_with_index: b.leaf_nodes_count > 0
|
||||
&& b.leaf_nodes_with_index_count == b.leaf_nodes_count,
|
||||
all_and: b.all_and.unwrap_or(true),
|
||||
all_and_groups: b.all_and_groups,
|
||||
})
|
||||
}
|
||||
}
|
||||
|
@ -65,13 +75,17 @@ struct TreeBuilder<'a> {
|
|||
resolved_expressions: HashMap<Arc<Expression>, ResolvedExpression>,
|
||||
resolved_idioms: HashMap<Arc<Idiom>, Node>,
|
||||
index_map: IndexesMap,
|
||||
with_indexes: Vec<IndexRef>,
|
||||
with_indexes: Option<Vec<IndexRef>>,
|
||||
knn_brute_force_expressions: HashMap<Arc<Expression>, KnnBruteForceExpression>,
|
||||
knn_expressions: KnnExpressions,
|
||||
idioms_record_options: HashMap<Arc<Idiom>, RecordOptions>,
|
||||
group_sequence: GroupRef,
|
||||
root: Option<Node>,
|
||||
knn_condition: Option<Cond>,
|
||||
leaf_nodes_count: usize,
|
||||
leaf_nodes_with_index_count: usize,
|
||||
all_and: Option<bool>,
|
||||
all_and_groups: HashMap<GroupRef, bool>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Eq, PartialEq, Hash)]
|
||||
|
@ -93,8 +107,8 @@ impl<'a> TreeBuilder<'a> {
|
|||
orders: Option<&'a Orders>,
|
||||
) -> Self {
|
||||
let with_indexes = match with {
|
||||
Some(With::Index(ixs)) => Vec::with_capacity(ixs.len()),
|
||||
_ => vec![],
|
||||
Some(With::Index(ixs)) => Some(Vec::with_capacity(ixs.len())),
|
||||
_ => None,
|
||||
};
|
||||
let first_order = if let Some(o) = orders {
|
||||
o.0.first()
|
||||
|
@ -119,6 +133,10 @@ impl<'a> TreeBuilder<'a> {
|
|||
group_sequence: 0,
|
||||
root: None,
|
||||
knn_condition: None,
|
||||
all_and: None,
|
||||
all_and_groups: Default::default(),
|
||||
leaf_nodes_count: 0,
|
||||
leaf_nodes_with_index_count: 0,
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -188,7 +206,10 @@ impl<'a> TreeBuilder<'a> {
|
|||
| Value::Param(_)
|
||||
| Value::Null
|
||||
| Value::None
|
||||
| Value::Function(_) => Ok(Node::Computable),
|
||||
| Value::Function(_) => {
|
||||
self.leaf_nodes_count += 1;
|
||||
Ok(Node::Computable)
|
||||
}
|
||||
Value::Array(a) => self.eval_array(stk, a).await,
|
||||
Value::Subquery(s) => self.eval_subquery(stk, s).await,
|
||||
_ => Ok(Node::Unsupported(format!("Unsupported value: {}", v))),
|
||||
|
@ -207,6 +228,7 @@ impl<'a> TreeBuilder<'a> {
|
|||
}
|
||||
|
||||
async fn eval_array(&mut self, stk: &mut Stk, a: &Array) -> Result<Node, Error> {
|
||||
self.leaf_nodes_count += 1;
|
||||
let mut values = Vec::with_capacity(a.len());
|
||||
for v in &a.0 {
|
||||
values.push(stk.run(|stk| v.compute(stk, self.ctx, self.opt, None)).await?);
|
||||
|
@ -220,6 +242,7 @@ impl<'a> TreeBuilder<'a> {
|
|||
group: GroupRef,
|
||||
i: &Idiom,
|
||||
) -> Result<Node, Error> {
|
||||
self.leaf_nodes_count += 1;
|
||||
// Check if the idiom has already been resolved
|
||||
if let Some(node) = self.resolved_idioms.get(i).cloned() {
|
||||
return Ok(node);
|
||||
|
@ -273,7 +296,11 @@ impl<'a> TreeBuilder<'a> {
|
|||
let ixr = self.index_map.definitions.len() as IndexRef;
|
||||
if let Some(With::Index(ixs)) = &self.with {
|
||||
if ixs.contains(&ix.name.0) {
|
||||
self.with_indexes.push(ixr);
|
||||
if let Some(wi) = &mut self.with_indexes {
|
||||
wi.push(ixr);
|
||||
} else {
|
||||
self.with_indexes = Some(vec![ixr]);
|
||||
}
|
||||
}
|
||||
}
|
||||
self.index_map.definitions.push(ix.clone().into());
|
||||
|
@ -343,7 +370,10 @@ impl<'a> TreeBuilder<'a> {
|
|||
match e {
|
||||
Expression::Unary {
|
||||
..
|
||||
} => Ok(Node::Unsupported("unary expressions not supported".to_string())),
|
||||
} => {
|
||||
self.leaf_nodes_count += 1;
|
||||
Ok(Node::Unsupported("unary expressions not supported".to_string()))
|
||||
}
|
||||
Expression::Binary {
|
||||
l,
|
||||
o,
|
||||
|
@ -353,6 +383,7 @@ impl<'a> TreeBuilder<'a> {
|
|||
if let Some(re) = self.resolved_expressions.get(e).cloned() {
|
||||
return Ok(re.into());
|
||||
}
|
||||
self.check_boolean_operator(group, o);
|
||||
let left = stk.run(|stk| self.eval_value(stk, group, l)).await?;
|
||||
let right = stk.run(|stk| self.eval_value(stk, group, r)).await?;
|
||||
// If both values are computable, then we can delegate the computation to the parent
|
||||
|
@ -362,9 +393,8 @@ impl<'a> TreeBuilder<'a> {
|
|||
let exp = Arc::new(e.clone());
|
||||
let left = Arc::new(self.compute(stk, l, left).await?);
|
||||
let right = Arc::new(self.compute(stk, r, right).await?);
|
||||
let mut io = None;
|
||||
if let Some((id, local_irs, remote_irs)) = left.is_indexed_field() {
|
||||
io = self.lookup_index_options(
|
||||
let io = if let Some((id, local_irs, remote_irs)) = left.is_indexed_field() {
|
||||
self.lookup_index_options(
|
||||
o,
|
||||
id,
|
||||
&right,
|
||||
|
@ -372,9 +402,9 @@ impl<'a> TreeBuilder<'a> {
|
|||
IdiomPosition::Left,
|
||||
local_irs,
|
||||
remote_irs,
|
||||
)?;
|
||||
)?
|
||||
} else if let Some((id, local_irs, remote_irs)) = right.is_indexed_field() {
|
||||
io = self.lookup_index_options(
|
||||
self.lookup_index_options(
|
||||
o,
|
||||
id,
|
||||
&left,
|
||||
|
@ -382,13 +412,16 @@ impl<'a> TreeBuilder<'a> {
|
|||
IdiomPosition::Right,
|
||||
local_irs,
|
||||
remote_irs,
|
||||
)?;
|
||||
}
|
||||
)?
|
||||
} else {
|
||||
None
|
||||
};
|
||||
if let Some(id) = left.is_field() {
|
||||
self.eval_bruteforce_knn(id, &right, &exp)?;
|
||||
} else if let Some(id) = right.is_field() {
|
||||
self.eval_bruteforce_knn(id, &left, &exp)?;
|
||||
}
|
||||
self.check_leaf_node_with_index(io.as_ref());
|
||||
let re = ResolvedExpression {
|
||||
group,
|
||||
exp: exp.clone(),
|
||||
|
@ -402,6 +435,37 @@ impl<'a> TreeBuilder<'a> {
|
|||
}
|
||||
}
|
||||
|
||||
fn check_boolean_operator(&mut self, gr: GroupRef, op: &Operator) {
|
||||
match op {
|
||||
Operator::Neg | Operator::Or => {
|
||||
if self.all_and != Some(false) {
|
||||
self.all_and = Some(false);
|
||||
}
|
||||
self.all_and_groups.entry(gr).and_modify(|b| *b = false).or_insert(false);
|
||||
}
|
||||
Operator::And => {
|
||||
if self.all_and.is_none() {
|
||||
self.all_and = Some(true);
|
||||
}
|
||||
self.all_and_groups.entry(gr).or_insert(true);
|
||||
}
|
||||
_ => {
|
||||
self.all_and_groups.entry(gr).or_insert(true);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn check_leaf_node_with_index(&mut self, io: Option<&IndexOption>) {
|
||||
if let Some(io) = io {
|
||||
if let Some(wi) = &self.with_indexes {
|
||||
if !wi.contains(&io.ix_ref()) {
|
||||
return;
|
||||
}
|
||||
}
|
||||
self.leaf_nodes_with_index_count += 2;
|
||||
}
|
||||
}
|
||||
|
||||
#[allow(clippy::too_many_arguments)]
|
||||
fn lookup_index_options(
|
||||
&mut self,
|
||||
|
|
4
core/src/kvs/surrealcs/cnf.rs
Normal file
4
core/src/kvs/surrealcs/cnf.rs
Normal file
|
@ -0,0 +1,4 @@
|
|||
use std::sync::LazyLock;
|
||||
|
||||
pub static SURREALCS_CONNECTION_POOL_SIZE: LazyLock<usize> =
|
||||
lazy_env_parse_or_else!("SURREAL_SURREALCS_CONNECTION_POOL_SIZE", usize, |_| num_cpus::get());
|
|
@ -1,5 +1,7 @@
|
|||
#![cfg(feature = "kv-surrealcs")]
|
||||
|
||||
mod cnf;
|
||||
|
||||
use crate::err::Error;
|
||||
use crate::key::debug::Sprintable;
|
||||
use crate::kvs::savepoint::{SaveOperation, SavePointImpl, SavePoints};
|
||||
|
@ -72,7 +74,7 @@ impl Drop for Transaction {
|
|||
impl Datastore {
|
||||
/// Open a new database
|
||||
pub(crate) async fn new(path: &str) -> Result<Datastore, Error> {
|
||||
match create_connection_pool(path, None).await {
|
||||
match create_connection_pool(path, Some(*cnf::SURREALCS_CONNECTION_POOL_SIZE)).await {
|
||||
Ok(_) => Ok(Datastore {}),
|
||||
Err(_) => {
|
||||
Err(Error::Ds("Cannot connect to the `surrealcs` storage engine".to_string()))
|
||||
|
|
|
@ -172,9 +172,12 @@ pub trait RpcContext {
|
|||
return Err(RpcError::InvalidParams);
|
||||
};
|
||||
let mut tmp_session = mem::take(self.session_mut());
|
||||
crate::iam::verify::token(self.kvs(), &mut tmp_session, &token.0).await?;
|
||||
let out: Result<(), RpcError> =
|
||||
crate::iam::verify::token(self.kvs(), &mut tmp_session, &token.0)
|
||||
.await
|
||||
.map_err(Into::into);
|
||||
*self.session_mut() = tmp_session;
|
||||
Ok(Value::None.into())
|
||||
out.map(|_| Value::None.into())
|
||||
}
|
||||
|
||||
// ------------------------------
|
||||
|
@ -299,8 +302,6 @@ pub trait RpcContext {
|
|||
let Ok((what, data)) = params.needs_two() else {
|
||||
return Err(RpcError::InvalidParams);
|
||||
};
|
||||
// Return a single result?
|
||||
let one = what.is_thing_single();
|
||||
// Specify the SQL query string
|
||||
|
||||
let mut res = match what {
|
||||
|
@ -323,11 +324,7 @@ pub trait RpcContext {
|
|||
}
|
||||
};
|
||||
|
||||
// Extract the first query result
|
||||
let res = match one {
|
||||
true => res.remove(0).result?.first(),
|
||||
false => res.remove(0).result?,
|
||||
};
|
||||
let res = res.remove(0).result?;
|
||||
// Return the result to the client
|
||||
Ok(res.into())
|
||||
}
|
||||
|
@ -337,8 +334,6 @@ pub trait RpcContext {
|
|||
return Err(RpcError::InvalidParams);
|
||||
};
|
||||
|
||||
let one = what.is_thing_single();
|
||||
|
||||
let mut res = match what {
|
||||
Value::None | Value::Null => {
|
||||
let sql = "INSERT RELATION $data RETURN AFTER";
|
||||
|
@ -360,10 +355,7 @@ pub trait RpcContext {
|
|||
_ => return Err(RpcError::InvalidParams),
|
||||
};
|
||||
|
||||
let res = match one {
|
||||
true => res.remove(0).result?.first(),
|
||||
false => res.remove(0).result?,
|
||||
};
|
||||
let res = res.remove(0).result?;
|
||||
Ok(res)
|
||||
}
|
||||
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::err::Error;
|
||||
use crate::sql::duration::Duration;
|
||||
use crate::sql::strand::Strand;
|
||||
use crate::syn;
|
||||
|
@ -11,6 +12,7 @@ use std::str;
|
|||
use std::str::FromStr;
|
||||
|
||||
use super::escape::quote_str;
|
||||
use super::value::TrySub;
|
||||
|
||||
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Datetime";
|
||||
|
||||
|
@ -108,3 +110,13 @@ impl ops::Sub<Self> for Datetime {
|
|||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TrySub for Datetime {
|
||||
type Output = Duration;
|
||||
fn try_sub(self, other: Self) -> Result<Duration, Error> {
|
||||
(self.0 - other.0)
|
||||
.to_std()
|
||||
.map_err(|_| Error::ArithmeticOverflow(format!("{self} - {other}")))
|
||||
.map(Duration::from)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::err::Error;
|
||||
use crate::sql::datetime::Datetime;
|
||||
use crate::sql::statements::info::InfoStructure;
|
||||
use crate::sql::strand::Strand;
|
||||
|
@ -12,6 +13,8 @@ use std::ops::Deref;
|
|||
use std::str::FromStr;
|
||||
use std::time;
|
||||
|
||||
use super::value::{TryAdd, TrySub};
|
||||
|
||||
pub(crate) static SECONDS_PER_YEAR: u64 = 365 * SECONDS_PER_DAY;
|
||||
pub(crate) static SECONDS_PER_WEEK: u64 = 7 * SECONDS_PER_DAY;
|
||||
pub(crate) static SECONDS_PER_DAY: u64 = 24 * SECONDS_PER_HOUR;
|
||||
|
@ -235,6 +238,16 @@ impl ops::Add for Duration {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryAdd for Duration {
|
||||
type Output = Self;
|
||||
fn try_add(self, other: Self) -> Result<Self, Error> {
|
||||
self.0
|
||||
.checked_add(other.0)
|
||||
.ok_or_else(|| Error::ArithmeticOverflow(format!("{self} + {other}")))
|
||||
.map(Duration::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> ops::Add<&'b Duration> for &'a Duration {
|
||||
type Output = Duration;
|
||||
fn add(self, other: &'b Duration) -> Duration {
|
||||
|
@ -245,6 +258,16 @@ impl<'a, 'b> ops::Add<&'b Duration> for &'a Duration {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> TryAdd<&'b Duration> for &'a Duration {
|
||||
type Output = Duration;
|
||||
fn try_add(self, other: &'b Duration) -> Result<Duration, Error> {
|
||||
self.0
|
||||
.checked_add(other.0)
|
||||
.ok_or_else(|| Error::ArithmeticOverflow(format!("{self} + {other}")))
|
||||
.map(Duration::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Sub for Duration {
|
||||
type Output = Self;
|
||||
fn sub(self, other: Self) -> Self {
|
||||
|
@ -255,6 +278,16 @@ impl ops::Sub for Duration {
|
|||
}
|
||||
}
|
||||
|
||||
impl TrySub for Duration {
|
||||
type Output = Self;
|
||||
fn try_sub(self, other: Self) -> Result<Self, Error> {
|
||||
self.0
|
||||
.checked_sub(other.0)
|
||||
.ok_or_else(|| Error::ArithmeticOverflow(format!("{self} - {other}")))
|
||||
.map(Duration::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> ops::Sub<&'b Duration> for &'a Duration {
|
||||
type Output = Duration;
|
||||
fn sub(self, other: &'b Duration) -> Duration {
|
||||
|
@ -265,26 +298,68 @@ impl<'a, 'b> ops::Sub<&'b Duration> for &'a Duration {
|
|||
}
|
||||
}
|
||||
|
||||
impl<'a, 'b> TrySub<&'b Duration> for &'a Duration {
|
||||
type Output = Duration;
|
||||
fn try_sub(self, other: &'b Duration) -> Result<Duration, Error> {
|
||||
self.0
|
||||
.checked_sub(other.0)
|
||||
.ok_or_else(|| Error::ArithmeticOverflow(format!("{self} - {other}")))
|
||||
.map(Duration::from)
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Add<Datetime> for Duration {
|
||||
type Output = Datetime;
|
||||
fn add(self, other: Datetime) -> Datetime {
|
||||
match chrono::Duration::from_std(self.0) {
|
||||
Ok(d) => Datetime::from(other.0 + d),
|
||||
Ok(d) => match other.0.checked_add_signed(d) {
|
||||
Some(v) => Datetime::from(v),
|
||||
None => Datetime::default(),
|
||||
},
|
||||
Err(_) => Datetime::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TryAdd<Datetime> for Duration {
|
||||
type Output = Datetime;
|
||||
fn try_add(self, other: Datetime) -> Result<Datetime, Error> {
|
||||
match chrono::Duration::from_std(self.0) {
|
||||
Ok(d) => match other.0.checked_add_signed(d) {
|
||||
Some(v) => Ok(Datetime::from(v)),
|
||||
None => Err(Error::ArithmeticOverflow(format!("{self} + {other}"))),
|
||||
},
|
||||
Err(_) => Err(Error::ArithmeticOverflow(format!("{self} + {other}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ops::Sub<Datetime> for Duration {
|
||||
type Output = Datetime;
|
||||
fn sub(self, other: Datetime) -> Datetime {
|
||||
match chrono::Duration::from_std(self.0) {
|
||||
Ok(d) => Datetime::from(other.0 - d),
|
||||
Ok(d) => match other.0.checked_sub_signed(d) {
|
||||
Some(v) => Datetime::from(v),
|
||||
None => Datetime::default(),
|
||||
},
|
||||
Err(_) => Datetime::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl TrySub<Datetime> for Duration {
|
||||
type Output = Datetime;
|
||||
fn try_sub(self, other: Datetime) -> Result<Datetime, Error> {
|
||||
match chrono::Duration::from_std(self.0) {
|
||||
Ok(d) => match other.0.checked_sub_signed(d) {
|
||||
Some(v) => Ok(Datetime::from(v)),
|
||||
None => Err(Error::ArithmeticOverflow(format!("{self} - {other}"))),
|
||||
},
|
||||
Err(_) => Err(Error::ArithmeticOverflow(format!("{self} - {other}"))),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Sum<Self> for Duration {
|
||||
fn sum<I>(iter: I) -> Duration
|
||||
where
|
||||
|
|
|
@ -79,15 +79,12 @@ pub fn escape_ident(s: &str) -> Cow<'_, str> {
|
|||
|
||||
#[inline]
|
||||
pub fn escape_normal<'a>(s: &'a str, l: char, r: char, e: &str) -> Cow<'a, str> {
|
||||
// Loop over each character
|
||||
for x in s.bytes() {
|
||||
// Check if character is allowed
|
||||
if !(x.is_ascii_alphanumeric() || x == b'_') {
|
||||
return Cow::Owned(format!("{l}{}{r}", s.replace(r, e)));
|
||||
}
|
||||
// Is there no need to escape the value?
|
||||
if s.bytes().all(|x| x.is_ascii_alphanumeric() || x == b'_') {
|
||||
return Cow::Borrowed(s);
|
||||
}
|
||||
// Output the value
|
||||
Cow::Borrowed(s)
|
||||
Cow::Owned(format!("{l}{}{r}", s.replace(r, e)))
|
||||
}
|
||||
|
||||
pub fn escape_reserved_keyword(s: &str) -> Option<String> {
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
use crate::err::Error;
|
||||
use crate::sql::escape::quote_plain_str;
|
||||
use revision::revisioned;
|
||||
use serde::{Deserialize, Serialize};
|
||||
|
@ -6,6 +7,8 @@ use std::ops::Deref;
|
|||
use std::ops::{self};
|
||||
use std::str;
|
||||
|
||||
use super::value::TryAdd;
|
||||
|
||||
pub(crate) const TOKEN: &str = "$surrealdb::private::sql::Strand";
|
||||
|
||||
/// A string that doesn't contain NUL bytes.
|
||||
|
@ -72,6 +75,18 @@ impl ops::Add for Strand {
|
|||
}
|
||||
}
|
||||
|
||||
impl TryAdd for Strand {
|
||||
type Output = Self;
|
||||
fn try_add(mut self, other: Self) -> Result<Self, Error> {
|
||||
if self.0.try_reserve(other.len()).is_ok() {
|
||||
self.0.push_str(other.as_str());
|
||||
Ok(self)
|
||||
} else {
|
||||
Err(Error::ArithmeticOverflow(format!("{self} + {other}")))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// serde(with = no_nul_bytes) will (de)serialize with no NUL bytes.
|
||||
pub(crate) mod no_nul_bytes {
|
||||
use serde::{
|
||||
|
|
|
@ -64,7 +64,7 @@ mod tests {
|
|||
fn changed_remove() {
|
||||
let old = Value::parse("{ test: true, other: 'test' }");
|
||||
let now = Value::parse("{ test: true }");
|
||||
let res = Value::parse("{ other: NONE }]");
|
||||
let res = Value::parse("{ other: NONE }");
|
||||
assert_eq!(res, old.changed(&now));
|
||||
}
|
||||
|
||||
|
|
|
@ -2981,10 +2981,10 @@ impl TryAdd for Value {
|
|||
fn try_add(self, other: Self) -> Result<Self, Error> {
|
||||
Ok(match (self, other) {
|
||||
(Self::Number(v), Self::Number(w)) => Self::Number(v.try_add(w)?),
|
||||
(Self::Strand(v), Self::Strand(w)) => Self::Strand(v + w),
|
||||
(Self::Datetime(v), Self::Duration(w)) => Self::Datetime(w + v),
|
||||
(Self::Duration(v), Self::Datetime(w)) => Self::Datetime(v + w),
|
||||
(Self::Duration(v), Self::Duration(w)) => Self::Duration(v + w),
|
||||
(Self::Strand(v), Self::Strand(w)) => Self::Strand(v.try_add(w)?),
|
||||
(Self::Datetime(v), Self::Duration(w)) => Self::Datetime(w.try_add(v)?),
|
||||
(Self::Duration(v), Self::Datetime(w)) => Self::Datetime(v.try_add(w)?),
|
||||
(Self::Duration(v), Self::Duration(w)) => Self::Duration(v.try_add(w)?),
|
||||
(v, w) => return Err(Error::TryAdd(v.to_raw_string(), w.to_raw_string())),
|
||||
})
|
||||
}
|
||||
|
@ -2994,7 +2994,7 @@ impl TryAdd for Value {
|
|||
|
||||
pub(crate) trait TrySub<Rhs = Self> {
|
||||
type Output;
|
||||
fn try_sub(self, v: Self) -> Result<Self::Output, Error>;
|
||||
fn try_sub(self, v: Rhs) -> Result<Self::Output, Error>;
|
||||
}
|
||||
|
||||
impl TrySub for Value {
|
||||
|
@ -3002,10 +3002,10 @@ impl TrySub for Value {
|
|||
fn try_sub(self, other: Self) -> Result<Self, Error> {
|
||||
Ok(match (self, other) {
|
||||
(Self::Number(v), Self::Number(w)) => Self::Number(v.try_sub(w)?),
|
||||
(Self::Datetime(v), Self::Datetime(w)) => Self::Duration(v - w),
|
||||
(Self::Datetime(v), Self::Duration(w)) => Self::Datetime(w - v),
|
||||
(Self::Duration(v), Self::Datetime(w)) => Self::Datetime(v - w),
|
||||
(Self::Duration(v), Self::Duration(w)) => Self::Duration(v - w),
|
||||
(Self::Datetime(v), Self::Datetime(w)) => Self::Duration(v.try_sub(w)?),
|
||||
(Self::Datetime(v), Self::Duration(w)) => Self::Datetime(w.try_sub(v)?),
|
||||
(Self::Duration(v), Self::Datetime(w)) => Self::Datetime(v.try_sub(w)?),
|
||||
(Self::Duration(v), Self::Duration(w)) => Self::Duration(v.try_sub(w)?),
|
||||
(v, w) => return Err(Error::TrySub(v.to_raw_string(), w.to_raw_string())),
|
||||
})
|
||||
}
|
||||
|
|
|
@ -12,281 +12,120 @@ pub struct Location {
|
|||
pub column: usize,
|
||||
}
|
||||
|
||||
/// Safety: b must be a substring of a.
|
||||
unsafe fn str_offset(a: &str, b: &str) -> usize {
|
||||
b.as_ptr().offset_from(a.as_ptr()) as usize
|
||||
}
|
||||
|
||||
impl Location {
|
||||
/// Returns the location of the start of substring in the larger input string.
|
||||
///
|
||||
/// Assumption: substr must be a subslice of input.
|
||||
pub fn of_in(substr: &str, input: &str) -> Self {
|
||||
// Bytes of input before substr.
|
||||
let offset = (substr.as_ptr() as usize)
|
||||
.checked_sub(input.as_ptr() as usize)
|
||||
.expect("tried to find location of substring in unrelated string");
|
||||
assert!(offset <= input.len(), "tried to find location of substring in unrelated string");
|
||||
// Bytes of input prior to line being iterated.
|
||||
let mut bytes_prior = 0;
|
||||
for (line_idx, (line, seperator_len)) in LineIterator::new(input).enumerate() {
|
||||
let bytes_so_far = bytes_prior + line.len() + seperator_len.unwrap_or(0) as usize;
|
||||
if bytes_so_far >= offset {
|
||||
// found line.
|
||||
let line_offset = offset - bytes_prior;
|
||||
fn range_of_source_end(source: &str) -> Range<Self> {
|
||||
let (line, column) = source
|
||||
.lines()
|
||||
.enumerate()
|
||||
.last()
|
||||
.map(|(idx, line)| {
|
||||
let idx = idx + 1;
|
||||
let line_idx = line.chars().count().max(1);
|
||||
(idx, line_idx)
|
||||
})
|
||||
.unwrap_or((0, 0));
|
||||
|
||||
let column = if line_offset > line.len() {
|
||||
// error is inside line terminator.
|
||||
line.chars().count() + 1
|
||||
} else {
|
||||
line[..line_offset].chars().count()
|
||||
};
|
||||
// +1 because line and column are 1 index.
|
||||
return Self {
|
||||
line: line_idx + 1,
|
||||
column: column + 1,
|
||||
};
|
||||
}
|
||||
bytes_prior = bytes_so_far;
|
||||
Self {
|
||||
line,
|
||||
column,
|
||||
}..Self {
|
||||
line,
|
||||
column: column + 1,
|
||||
}
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
pub fn of_offset(source: &str, offset: usize) -> Self {
|
||||
assert!(offset <= source.len(), "tried to find location of substring in unrelated string");
|
||||
|
||||
if offset == source.len() {
|
||||
// Eof character
|
||||
|
||||
let (last_line, column) = LineIterator::new(source)
|
||||
.enumerate()
|
||||
.last()
|
||||
.map(|(idx, (l, _))| (idx, l.len()))
|
||||
.unwrap_or((0, 0));
|
||||
return Self {
|
||||
line: last_line + 1,
|
||||
column: column + 1,
|
||||
};
|
||||
}
|
||||
|
||||
// Bytes of input prior to line being iterated.
|
||||
let mut bytes_prior = 0;
|
||||
for (line_idx, (line, seperator_len)) in LineIterator::new(source).enumerate() {
|
||||
let bytes_so_far = bytes_prior + line.len() + seperator_len.unwrap_or(0) as usize;
|
||||
if bytes_so_far >= offset {
|
||||
// found line.
|
||||
let line_offset = offset - bytes_prior;
|
||||
|
||||
let column = if line_offset > line.len() {
|
||||
// error is inside line terminator.
|
||||
line.chars().count() + 1
|
||||
} else {
|
||||
line[..line_offset].chars().count()
|
||||
};
|
||||
// +1 because line and column are 1 index.
|
||||
return Self {
|
||||
line: line_idx + 1,
|
||||
column: column + 1,
|
||||
};
|
||||
}
|
||||
bytes_prior = bytes_so_far;
|
||||
}
|
||||
unreachable!()
|
||||
}
|
||||
|
||||
pub fn of_span_start(source: &str, span: Span) -> Self {
|
||||
// Bytes of input before substr.
|
||||
|
||||
let offset = span.offset as usize;
|
||||
Self::of_offset(source, offset)
|
||||
}
|
||||
|
||||
pub fn of_span_end(source: &str, span: Span) -> Self {
|
||||
// Bytes of input before substr.
|
||||
let offset = span.offset as usize + span.len as usize;
|
||||
Self::of_offset(source, offset)
|
||||
}
|
||||
|
||||
pub fn range_of_span(source: &str, span: Span) -> Range<Self> {
|
||||
if source.len() == span.offset as usize {
|
||||
// EOF span
|
||||
let (line_idx, column) = LineIterator::new(source)
|
||||
.map(|(l, _)| l.len())
|
||||
.enumerate()
|
||||
.last()
|
||||
.unwrap_or((0, 0));
|
||||
|
||||
return Self {
|
||||
line: line_idx + 1,
|
||||
column: column + 1,
|
||||
}..Self {
|
||||
line: line_idx + 1,
|
||||
column: column + 2,
|
||||
};
|
||||
}
|
||||
|
||||
// Bytes of input before substr.
|
||||
let offset = span.offset as usize;
|
||||
let end = offset + span.len as usize;
|
||||
|
||||
if span.len == 0 && source.len() == span.offset as usize {
|
||||
// EOF span
|
||||
let (last_line, column) = LineIterator::new(source)
|
||||
.enumerate()
|
||||
.last()
|
||||
.map(|(idx, (l, _))| (idx, l.len()))
|
||||
.unwrap_or((0, 0));
|
||||
return Self {
|
||||
line: last_line + 1,
|
||||
column,
|
||||
}..Self {
|
||||
line: last_line + 1,
|
||||
column: column + 1,
|
||||
};
|
||||
if source.len() <= span.offset as usize {
|
||||
return Self::range_of_source_end(source);
|
||||
}
|
||||
|
||||
let mut prev_line = "";
|
||||
let mut lines = source.lines().enumerate().peekable();
|
||||
// Bytes of input prior to line being iteratated.
|
||||
let mut bytes_prior = 0;
|
||||
let mut iterator = LineIterator::new(source).enumerate().peekable();
|
||||
let start_offset = span.offset as usize;
|
||||
let start = loop {
|
||||
let Some((line_idx, (line, seperator_offset))) = iterator.peek() else {
|
||||
panic!("tried to find location of span not belonging to string");
|
||||
let Some((line_idx, line)) = lines.peek().copied() else {
|
||||
// Couldn't find the line, give up and return the last
|
||||
return Self::range_of_source_end(source);
|
||||
};
|
||||
let bytes_so_far = bytes_prior + line.len() + seperator_offset.unwrap_or(0) as usize;
|
||||
if bytes_so_far > offset {
|
||||
// found line.
|
||||
let line_offset = offset - bytes_prior;
|
||||
let column = if line_offset > line.len() {
|
||||
line.chars().count() + 1
|
||||
} else {
|
||||
line[..line_offset.min(line.len())].chars().count()
|
||||
// Safety: line originates from source so it is a substring so calling str_offset is
|
||||
// valid.
|
||||
let line_offset = unsafe { str_offset(source, line) };
|
||||
|
||||
if start_offset < line_offset {
|
||||
// Span is inside the previous line terminator, point to the end of the line.
|
||||
let len = prev_line.chars().count();
|
||||
break Self {
|
||||
line: line_idx,
|
||||
column: len + 1,
|
||||
};
|
||||
// +1 because line and column are 1 index.
|
||||
if bytes_so_far >= end {
|
||||
// end is on the same line, finish immediatly.
|
||||
let line_offset = end - bytes_prior;
|
||||
let end_column = line[..line_offset].chars().count();
|
||||
return Self {
|
||||
line: line_idx + 1,
|
||||
column: column + 1,
|
||||
}..Self {
|
||||
line: line_idx + 1,
|
||||
column: end_column + 1,
|
||||
};
|
||||
} else {
|
||||
break Self {
|
||||
line: line_idx + 1,
|
||||
column: column + 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
bytes_prior = bytes_so_far;
|
||||
iterator.next();
|
||||
|
||||
if (line_offset..(line_offset + line.len())).contains(&start_offset) {
|
||||
let column_offset = start_offset - line_offset;
|
||||
let column = line
|
||||
.char_indices()
|
||||
.enumerate()
|
||||
.find(|(_, (char_idx, _))| *char_idx >= column_offset)
|
||||
.map(|(l, _)| l)
|
||||
.unwrap_or_else(|| {
|
||||
// give up, just point to the end.
|
||||
line.chars().count()
|
||||
});
|
||||
break Self {
|
||||
line: line_idx + 1,
|
||||
column: column + 1,
|
||||
};
|
||||
}
|
||||
|
||||
lines.next();
|
||||
prev_line = line;
|
||||
};
|
||||
|
||||
loop {
|
||||
let Some((line_idx, (line, seperator_offset))) = iterator.next() else {
|
||||
panic!("tried to find location of span not belonging to string");
|
||||
let end_offset = span.offset as usize + span.len as usize;
|
||||
let end = loop {
|
||||
let Some((line_idx, line)) = lines.peek().copied() else {
|
||||
// Couldn't find the line, give up and return the last
|
||||
break Self::range_of_source_end(source).end;
|
||||
};
|
||||
let bytes_so_far = bytes_prior + line.len() + seperator_offset.unwrap_or(0) as usize;
|
||||
if bytes_so_far >= end {
|
||||
let line_offset = end - bytes_prior;
|
||||
let column = if line_offset > line.len() {
|
||||
line.chars().count() + 1
|
||||
} else {
|
||||
line[..line_offset.min(line.len())].chars().count()
|
||||
// Safety: line originates from source so it is a substring so calling str_offset is
|
||||
// valid.
|
||||
let line_offset = unsafe { str_offset(source, line) };
|
||||
|
||||
if end_offset < line_offset {
|
||||
// Span is inside the previous line terminator, point to the end of the line.
|
||||
let len = prev_line.chars().count();
|
||||
break Self {
|
||||
line: line_idx,
|
||||
column: len + 1,
|
||||
};
|
||||
return start..Self {
|
||||
}
|
||||
|
||||
if (line_offset..(line_offset + line.len())).contains(&end_offset) {
|
||||
let column_offset = end_offset - line_offset;
|
||||
let column = line
|
||||
.char_indices()
|
||||
.enumerate()
|
||||
.find(|(_, (char_idx, _))| *char_idx >= column_offset)
|
||||
.map(|(l, _)| l)
|
||||
.unwrap_or_else(|| {
|
||||
// give up, just point to the end.
|
||||
line.chars().count()
|
||||
});
|
||||
break Self {
|
||||
line: line_idx + 1,
|
||||
column: column + 1,
|
||||
};
|
||||
}
|
||||
bytes_prior = bytes_so_far;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
struct LineIterator<'a> {
|
||||
current: &'a str,
|
||||
}
|
||||
|
||||
impl<'a> LineIterator<'a> {
|
||||
pub fn new(s: &'a str) -> Self {
|
||||
LineIterator {
|
||||
current: s,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Iterator for LineIterator<'a> {
|
||||
type Item = (&'a str, Option<u8>);
|
||||
|
||||
fn next(&mut self) -> Option<Self::Item> {
|
||||
if self.current.is_empty() {
|
||||
return None;
|
||||
}
|
||||
let bytes = self.current.as_bytes();
|
||||
for i in 0..bytes.len() {
|
||||
match bytes[i] {
|
||||
b'\r' => {
|
||||
if let Some(b'\n') = bytes.get(i + 1) {
|
||||
let res = &self.current[..i];
|
||||
self.current = &self.current[i + 2..];
|
||||
return Some((res, Some(2)));
|
||||
}
|
||||
let res = &self.current[..i];
|
||||
self.current = &self.current[i + 1..];
|
||||
return Some((res, Some(1)));
|
||||
}
|
||||
0xb | 0xC | b'\n' => {
|
||||
// vertical tab VT and form feed FF.
|
||||
let res = &self.current[..i];
|
||||
self.current = &self.current[i + 1..];
|
||||
return Some((res, Some(1)));
|
||||
}
|
||||
0xc2 => {
|
||||
// next line NEL
|
||||
if bytes.get(i + 1).copied() != Some(0x85) {
|
||||
continue;
|
||||
}
|
||||
let res = &self.current[..i];
|
||||
self.current = &self.current[i + 2..];
|
||||
return Some((res, Some(2)));
|
||||
}
|
||||
0xe2 => {
|
||||
// line separator and paragraph seperator.
|
||||
if bytes.get(i + 1).copied() != Some(0x80) {
|
||||
continue;
|
||||
}
|
||||
let next_byte = bytes.get(i + 2).copied();
|
||||
if next_byte != Some(0xA8) && next_byte != Some(0xA9) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// vertical tab VT, next line NEL and form feed FF.
|
||||
let res = &self.current[..i];
|
||||
self.current = &self.current[i + 3..];
|
||||
return Some((res, Some(3)));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
}
|
||||
Some((std::mem::take(&mut self.current), None))
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::LineIterator;
|
||||
|
||||
#[test]
|
||||
fn test_line_iterator() {
|
||||
let lines = "foo\nbar\r\nfoo\rbar\u{000B}foo\u{000C}bar\u{0085}foo\u{2028}bar\u{2029}\n";
|
||||
let mut iterator = LineIterator::new(lines);
|
||||
assert_eq!(iterator.next(), Some(("foo", Some(1))));
|
||||
assert_eq!(iterator.next(), Some(("bar", Some(2))));
|
||||
assert_eq!(iterator.next(), Some(("foo", Some(1))));
|
||||
assert_eq!(iterator.next(), Some(("bar", Some(1))));
|
||||
assert_eq!(iterator.next(), Some(("foo", Some(1))));
|
||||
assert_eq!(iterator.next(), Some(("bar", Some(2))));
|
||||
assert_eq!(iterator.next(), Some(("foo", Some(3))));
|
||||
assert_eq!(iterator.next(), Some(("bar", Some(3))));
|
||||
assert_eq!(iterator.next(), Some(("", Some(1))));
|
||||
assert_eq!(iterator.next(), None);
|
||||
|
||||
lines.next();
|
||||
prev_line = line;
|
||||
};
|
||||
|
||||
start..end
|
||||
}
|
||||
}
|
||||
|
|
|
@ -222,17 +222,26 @@ impl fmt::Display for Snippet {
|
|||
#[cfg(test)]
|
||||
mod test {
|
||||
use super::{RenderedError, Snippet, Truncation};
|
||||
use crate::syn::error::{Location, MessageKind};
|
||||
use crate::syn::{
|
||||
error::{Location, MessageKind},
|
||||
token::Span,
|
||||
};
|
||||
|
||||
#[test]
|
||||
fn truncate_whitespace() {
|
||||
let source = "\n\n\n\t $ \t";
|
||||
let offset = source.char_indices().find(|(_, c)| *c == '$').unwrap().0;
|
||||
let error = &source[offset..];
|
||||
|
||||
let location = Location::of_in(error, source);
|
||||
let location = Location::range_of_span(
|
||||
source,
|
||||
Span {
|
||||
offset: offset as u32,
|
||||
len: 1,
|
||||
},
|
||||
);
|
||||
|
||||
let snippet = Snippet::from_source_location(source, location, None, MessageKind::Error);
|
||||
let snippet =
|
||||
Snippet::from_source_location(source, location.start, None, MessageKind::Error);
|
||||
assert_eq!(snippet.truncation, Truncation::None);
|
||||
assert_eq!(snippet.offset, 0);
|
||||
assert_eq!(snippet.source.as_str(), "$");
|
||||
|
@ -242,11 +251,17 @@ mod test {
|
|||
fn truncate_start() {
|
||||
let source = " aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa $ \t";
|
||||
let offset = source.char_indices().find(|(_, c)| *c == '$').unwrap().0;
|
||||
let error = &source[offset..];
|
||||
|
||||
let location = Location::of_in(error, source);
|
||||
let location = Location::range_of_span(
|
||||
source,
|
||||
Span {
|
||||
offset: offset as u32,
|
||||
len: 1,
|
||||
},
|
||||
);
|
||||
|
||||
let snippet = Snippet::from_source_location(source, location, None, MessageKind::Error);
|
||||
let snippet =
|
||||
Snippet::from_source_location(source, location.start, None, MessageKind::Error);
|
||||
assert_eq!(snippet.truncation, Truncation::Start);
|
||||
assert_eq!(snippet.offset, 10);
|
||||
assert_eq!(snippet.source.as_str(), "aaaaaaaaa $");
|
||||
|
@ -256,11 +271,17 @@ mod test {
|
|||
fn truncate_end() {
|
||||
let source = "\n\n a $ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \t";
|
||||
let offset = source.char_indices().find(|(_, c)| *c == '$').unwrap().0;
|
||||
let error = &source[offset..];
|
||||
|
||||
let location = Location::of_in(error, source);
|
||||
let location = Location::range_of_span(
|
||||
source,
|
||||
Span {
|
||||
offset: offset as u32,
|
||||
len: 1,
|
||||
},
|
||||
);
|
||||
|
||||
let snippet = Snippet::from_source_location(source, location, None, MessageKind::Error);
|
||||
let snippet =
|
||||
Snippet::from_source_location(source, location.start, None, MessageKind::Error);
|
||||
assert_eq!(snippet.truncation, Truncation::End);
|
||||
assert_eq!(snippet.offset, 2);
|
||||
assert_eq!(
|
||||
|
@ -273,11 +294,17 @@ mod test {
|
|||
fn truncate_both() {
|
||||
let source = "\n\n\n\n aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa $ aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa \t";
|
||||
let offset = source.char_indices().find(|(_, c)| *c == '$').unwrap().0;
|
||||
let error = &source[offset..];
|
||||
|
||||
let location = Location::of_in(error, source);
|
||||
let location = Location::range_of_span(
|
||||
source,
|
||||
Span {
|
||||
offset: offset as u32,
|
||||
len: 1,
|
||||
},
|
||||
);
|
||||
|
||||
let snippet = Snippet::from_source_location(source, location, None, MessageKind::Error);
|
||||
let snippet =
|
||||
Snippet::from_source_location(source, location.start, None, MessageKind::Error);
|
||||
assert_eq!(snippet.truncation, Truncation::Both);
|
||||
assert_eq!(snippet.offset, 10);
|
||||
assert_eq!(
|
||||
|
|
|
@ -82,9 +82,16 @@ pub fn numeric(lexer: &mut Lexer, start: Token) -> Result<Numeric, SyntaxError>
|
|||
match start.kind {
|
||||
t!("-") | t!("+") => number(lexer, start).map(Numeric::Number),
|
||||
TokenKind::Digits => match lexer.reader.peek() {
|
||||
Some(b'n' | b'm' | b's' | b'h' | b'y' | b'd' | b'w') => {
|
||||
Some(b'n' | b'm' | b's' | b'h' | b'y' | b'w') => {
|
||||
duration(lexer, start).map(Numeric::Duration)
|
||||
}
|
||||
Some(b'd') => {
|
||||
if lexer.reader.peek1() == Some(b'e') {
|
||||
number(lexer, start).map(Numeric::Number)
|
||||
} else {
|
||||
duration(lexer, start).map(Numeric::Duration)
|
||||
}
|
||||
}
|
||||
Some(x) if !x.is_ascii() => duration(lexer, start).map(Numeric::Duration),
|
||||
_ => number(lexer, start).map(Numeric::Number),
|
||||
},
|
||||
|
|
|
@ -64,7 +64,7 @@ pub fn value(input: &str) -> Result<Value, Error> {
|
|||
.with_query_recursion_limit(*MAX_QUERY_PARSING_DEPTH as usize);
|
||||
let mut stack = Stack::new();
|
||||
stack
|
||||
.enter(|stk| parser.parse_value_table(stk))
|
||||
.enter(|stk| parser.parse_value_field(stk))
|
||||
.finish()
|
||||
.and_then(|e| parser.assert_finished().map(|_| e))
|
||||
.map_err(|e| e.render_on(input))
|
||||
|
@ -211,7 +211,7 @@ pub fn value_legacy_strand(input: &str) -> Result<Value, Error> {
|
|||
let mut stack = Stack::new();
|
||||
parser.allow_legacy_strand(true);
|
||||
stack
|
||||
.enter(|stk| parser.parse_value_table(stk))
|
||||
.enter(|stk| parser.parse_value_field(stk))
|
||||
.finish()
|
||||
.and_then(|e| parser.assert_finished().map(|_| e))
|
||||
.map_err(|e| e.render_on(input))
|
||||
|
|
|
@ -188,6 +188,7 @@ impl Parser<'_> {
|
|||
|
||||
#[cfg(test)]
|
||||
mod test {
|
||||
use crate::sql::{Ident, Part};
|
||||
|
||||
#[test]
|
||||
fn identifiers() {
|
||||
|
@ -206,8 +207,8 @@ mod test {
|
|||
|
||||
assert_eq!(
|
||||
r,
|
||||
sql::Query(sql::Statements(vec![sql::Statement::Value(sql::Value::Table(
|
||||
sql::Table(ident.to_string())
|
||||
sql::Query(sql::Statements(vec![sql::Statement::Value(sql::Value::Idiom(
|
||||
sql::Idiom(vec![Part::Field(Ident(ident.to_string()))])
|
||||
))]))
|
||||
)
|
||||
}
|
||||
|
|
|
@ -134,7 +134,7 @@ impl<'a> Parser<'a> {
|
|||
last_span: Span::empty(),
|
||||
token_buffer: TokenBuffer::new(),
|
||||
glued_value: GluedValue::None,
|
||||
table_as_field: false,
|
||||
table_as_field: true,
|
||||
legacy_strands: false,
|
||||
flexible_record_id: true,
|
||||
object_recursion: 100,
|
||||
|
@ -180,7 +180,7 @@ impl<'a> Parser<'a> {
|
|||
pub fn reset(&mut self) {
|
||||
self.last_span = Span::empty();
|
||||
self.token_buffer.clear();
|
||||
self.table_as_field = false;
|
||||
self.table_as_field = true;
|
||||
self.lexer.reset();
|
||||
}
|
||||
|
||||
|
@ -319,8 +319,12 @@ impl<'a> Parser<'a> {
|
|||
self.last_span
|
||||
}
|
||||
|
||||
pub fn assert_finished(&self) -> ParseResult<()> {
|
||||
self.lexer.assert_finished()
|
||||
pub fn assert_finished(&mut self) -> ParseResult<()> {
|
||||
let p = self.peek();
|
||||
if self.peek().kind != TokenKind::Eof {
|
||||
bail!("Unexpected token `{}`, expected no more tokens",p.kind, @p.span);
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Eat the next token if it is of the given kind.
|
||||
|
|
|
@ -3,9 +3,10 @@ use std::collections::BTreeMap;
|
|||
use reblessive::Stk;
|
||||
|
||||
use crate::{
|
||||
sql::{Block, Geometry, Number, Object, Strand, Value},
|
||||
sql::{Block, Geometry, Object, Strand, Value},
|
||||
syn::{
|
||||
error::bail,
|
||||
lexer::compound,
|
||||
parser::{enter_object_recursion, mac::expected, ParseResult, Parser},
|
||||
token::{t, Glued, Span, TokenKind},
|
||||
},
|
||||
|
@ -602,10 +603,8 @@ impl Parser<'_> {
|
|||
match token.kind {
|
||||
x if Self::kind_is_keyword_like(x) => {
|
||||
self.pop_peek();
|
||||
let str = self.lexer.reader.span(token.span);
|
||||
// Lexer should ensure that the token is valid utf-8
|
||||
let str = std::str::from_utf8(str).unwrap().to_owned();
|
||||
Ok(str)
|
||||
let str = self.lexer.span_str(token.span);
|
||||
Ok(str.to_string())
|
||||
}
|
||||
TokenKind::Identifier => {
|
||||
self.pop_peek();
|
||||
|
@ -616,9 +615,16 @@ impl Parser<'_> {
|
|||
let str = self.next_token_value::<Strand>()?.0;
|
||||
Ok(str)
|
||||
}
|
||||
TokenKind::Digits | TokenKind::Glued(Glued::Number) => {
|
||||
let number = self.next_token_value::<Number>()?.to_string();
|
||||
Ok(number)
|
||||
TokenKind::Digits => {
|
||||
self.pop_peek();
|
||||
let span = self.lexer.lex_compound(token, compound::number)?.span;
|
||||
let str = self.lexer.span_str(span);
|
||||
Ok(str.to_string())
|
||||
}
|
||||
TokenKind::Glued(Glued::Number) => {
|
||||
self.pop_peek();
|
||||
let str = self.lexer.span_str(token.span);
|
||||
Ok(str.to_string())
|
||||
}
|
||||
_ => unexpected!(self, token, "an object key"),
|
||||
}
|
||||
|
|
|
@ -288,6 +288,7 @@ impl Parser<'_> {
|
|||
t!("RETURN")
|
||||
| t!("SELECT")
|
||||
| t!("CREATE")
|
||||
| t!("INSERT")
|
||||
| t!("UPSERT")
|
||||
| t!("UPDATE")
|
||||
| t!("DELETE")
|
||||
|
|
|
@ -335,12 +335,12 @@ impl Parser<'_> {
|
|||
t!("SIGNUP") => {
|
||||
self.pop_peek();
|
||||
ac.signup =
|
||||
Some(stk.run(|stk| self.parse_value_table(stk)).await?);
|
||||
Some(stk.run(|stk| self.parse_value_field(stk)).await?);
|
||||
}
|
||||
t!("SIGNIN") => {
|
||||
self.pop_peek();
|
||||
ac.signin =
|
||||
Some(stk.run(|stk| self.parse_value_table(stk)).await?);
|
||||
Some(stk.run(|stk| self.parse_value_field(stk)).await?);
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
|
@ -375,7 +375,7 @@ impl Parser<'_> {
|
|||
}
|
||||
t!("AUTHENTICATE") => {
|
||||
self.pop_peek();
|
||||
res.authenticate = Some(stk.run(|stk| self.parse_value_table(stk)).await?);
|
||||
res.authenticate = Some(stk.run(|stk| self.parse_value_field(stk)).await?);
|
||||
}
|
||||
t!("DURATION") => {
|
||||
self.pop_peek();
|
||||
|
@ -581,11 +581,11 @@ impl Parser<'_> {
|
|||
}
|
||||
t!("SIGNUP") => {
|
||||
self.pop_peek();
|
||||
ac.signup = Some(stk.run(|stk| self.parse_value_table(stk)).await?);
|
||||
ac.signup = Some(stk.run(|stk| self.parse_value_field(stk)).await?);
|
||||
}
|
||||
t!("SIGNIN") => {
|
||||
self.pop_peek();
|
||||
ac.signin = Some(stk.run(|stk| self.parse_value_table(stk)).await?);
|
||||
ac.signin = Some(stk.run(|stk| self.parse_value_field(stk)).await?);
|
||||
}
|
||||
_ => break,
|
||||
}
|
||||
|
@ -619,7 +619,7 @@ impl Parser<'_> {
|
|||
match self.peek_kind() {
|
||||
t!("VALUE") => {
|
||||
self.pop_peek();
|
||||
res.value = ctx.run(|ctx| self.parse_value_table(ctx)).await?;
|
||||
res.value = ctx.run(|ctx| self.parse_value_field(ctx)).await?;
|
||||
}
|
||||
t!("COMMENT") => {
|
||||
self.pop_peek();
|
||||
|
@ -758,13 +758,13 @@ impl Parser<'_> {
|
|||
match self.peek_kind() {
|
||||
t!("WHEN") => {
|
||||
self.pop_peek();
|
||||
res.when = ctx.run(|ctx| self.parse_value_table(ctx)).await?;
|
||||
res.when = ctx.run(|ctx| self.parse_value_field(ctx)).await?;
|
||||
}
|
||||
t!("THEN") => {
|
||||
self.pop_peek();
|
||||
res.then = Values(vec![ctx.run(|ctx| self.parse_value_table(ctx)).await?]);
|
||||
res.then = Values(vec![ctx.run(|ctx| self.parse_value_field(ctx)).await?]);
|
||||
while self.eat(t!(",")) {
|
||||
res.then.0.push(ctx.run(|ctx| self.parse_value_table(ctx)).await?)
|
||||
res.then.0.push(ctx.run(|ctx| self.parse_value_field(ctx)).await?)
|
||||
}
|
||||
}
|
||||
t!("COMMENT") => {
|
||||
|
|
|
@ -129,7 +129,7 @@ impl Parser<'_> {
|
|||
let mut values = Vec::new();
|
||||
let start = expected!(self, t!("(")).span;
|
||||
loop {
|
||||
values.push(self.parse_value_table(ctx).await?);
|
||||
values.push(self.parse_value_field(ctx).await?);
|
||||
|
||||
if !self.eat(t!(",")) {
|
||||
break;
|
||||
|
|
|
@ -214,7 +214,7 @@ impl Parser<'_> {
|
|||
}
|
||||
_ => {
|
||||
// TODO: Provide information about keywords.
|
||||
let value = ctx.run(|ctx| self.parse_value_table(ctx)).await?;
|
||||
let value = ctx.run(|ctx| self.parse_value_field(ctx)).await?;
|
||||
Ok(Self::refine_stmt_value(value))
|
||||
}
|
||||
}
|
||||
|
|
|
@ -75,6 +75,14 @@ impl Parser<'_> {
|
|||
}
|
||||
}
|
||||
pub async fn parse_relate_value(&mut self, ctx: &mut Stk) -> ParseResult<Value> {
|
||||
let old = self.table_as_field;
|
||||
self.table_as_field = true;
|
||||
let r = self.parse_relate_value_inner(ctx).await;
|
||||
self.table_as_field = old;
|
||||
r
|
||||
}
|
||||
|
||||
async fn parse_relate_value_inner(&mut self, ctx: &mut Stk) -> ParseResult<Value> {
|
||||
match self.peek_kind() {
|
||||
t!("[") => {
|
||||
let start = self.pop_peek().span;
|
||||
|
|
|
@ -5,6 +5,11 @@ fn object_with_negative() {
|
|||
test_parse!(parse_json, r#"{"foo": -1 }"#).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn object_with_trailing_whitespace() {
|
||||
test_parse!(parse_json, r#"{"foo": -1 }\n"#).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn array_with_negative() {
|
||||
test_parse!(parse_json, r#"[-1]"#).unwrap();
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use crate::{
|
||||
sql::{self, Id, Statement, Thing, Value},
|
||||
sql::{self, Id, Ident, Idiom, Part, Query, Statement, Statements, Thing, Value},
|
||||
syn::parser::mac::test_parse,
|
||||
};
|
||||
|
||||
|
@ -9,6 +9,11 @@ mod stmt;
|
|||
mod streaming;
|
||||
mod value;
|
||||
|
||||
#[test]
|
||||
fn parse_large_test_file() {
|
||||
test_parse!(parse_query, include_str!("../../../../test.surql")).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn multiple_semicolons() {
|
||||
let res = test_parse!(parse_query, r#";;"#).unwrap();
|
||||
|
@ -83,6 +88,20 @@ fn query_object() {
|
|||
test_parse!(parse_query, src).inspect_err(|e| eprintln!("{}", e.render_on(src))).unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn ident_is_field() {
|
||||
let src = r#"foo"#;
|
||||
|
||||
let field =
|
||||
test_parse!(parse_query, src).inspect_err(|e| eprintln!("{}", e.render_on(src))).unwrap();
|
||||
assert_eq!(
|
||||
field,
|
||||
Query(Statements(vec![Statement::Value(Value::Idiom(Idiom(vec![Part::Field(Ident(
|
||||
"foo".to_string()
|
||||
))])))]))
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn escaped_params_backtick() {
|
||||
test_parse!(
|
||||
|
@ -92,3 +111,8 @@ fn escaped_params_backtick() {
|
|||
)
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_immediate_insert_subquery() {
|
||||
test_parse!(parse_query, r#"LET $insert = INSERT INTO t (SELECT true FROM 1);"#).unwrap();
|
||||
}
|
||||
|
|
|
@ -41,6 +41,10 @@ use crate::{
|
|||
};
|
||||
use chrono::{offset::TimeZone, NaiveDate, Offset, Utc};
|
||||
|
||||
fn ident_field(name: &str) -> Value {
|
||||
Value::Idiom(Idiom(vec![Part::Field(Ident(name.to_string()))]))
|
||||
}
|
||||
|
||||
#[test]
|
||||
pub fn parse_analyze() {
|
||||
let res = test_parse!(parse_stmt, r#"ANALYZE INDEX b on a"#).unwrap();
|
||||
|
@ -205,7 +209,7 @@ fn parse_define_function() {
|
|||
(Ident("b".to_string()), Kind::Array(Box::new(Kind::Bool), Some(3)))
|
||||
],
|
||||
block: Block(vec![Entry::Output(OutputStatement {
|
||||
what: Value::Table(Table("a".to_string())),
|
||||
what: ident_field("a"),
|
||||
fetch: None,
|
||||
})]),
|
||||
comment: Some(Strand("test".to_string())),
|
||||
|
@ -1709,10 +1713,10 @@ fn parse_if() {
|
|||
res,
|
||||
Statement::Ifelse(IfelseStatement {
|
||||
exprs: vec![
|
||||
(Value::Table(Table("foo".to_owned())), Value::Table(Table("bar".to_owned()))),
|
||||
(Value::Table(Table("faz".to_owned())), Value::Table(Table("baz".to_owned())))
|
||||
(ident_field("foo"), ident_field("bar")),
|
||||
(ident_field("faz"), ident_field("baz")),
|
||||
],
|
||||
close: Some(Value::Table(Table("baq".to_owned())))
|
||||
close: Some(ident_field("baq"))
|
||||
})
|
||||
)
|
||||
}
|
||||
|
@ -1726,21 +1730,15 @@ fn parse_if_block() {
|
|||
Statement::Ifelse(IfelseStatement {
|
||||
exprs: vec![
|
||||
(
|
||||
Value::Table(Table("foo".to_owned())),
|
||||
Value::Block(Box::new(Block(vec![Entry::Value(Value::Table(Table(
|
||||
"bar".to_owned()
|
||||
)),)]))),
|
||||
ident_field("foo"),
|
||||
Value::Block(Box::new(Block(vec![Entry::Value(ident_field("bar"))]))),
|
||||
),
|
||||
(
|
||||
Value::Table(Table("faz".to_owned())),
|
||||
Value::Block(Box::new(Block(vec![Entry::Value(Value::Table(Table(
|
||||
"baz".to_owned()
|
||||
)),)]))),
|
||||
ident_field("faz"),
|
||||
Value::Block(Box::new(Block(vec![Entry::Value(ident_field("baz"))]))),
|
||||
)
|
||||
],
|
||||
close: Some(Value::Block(Box::new(Block(vec![Entry::Value(Value::Table(Table(
|
||||
"baq".to_owned()
|
||||
)))])))),
|
||||
close: Some(Value::Block(Box::new(Block(vec![Entry::Value(ident_field("baq"))])))),
|
||||
})
|
||||
)
|
||||
}
|
||||
|
@ -2193,10 +2191,8 @@ fn parse_return() {
|
|||
assert_eq!(
|
||||
res,
|
||||
Statement::Output(OutputStatement {
|
||||
what: Value::Table(Table("RETRUN".to_owned())),
|
||||
fetch: Some(Fetchs(vec![Fetch(Value::Idiom(Idiom(vec![Part::Field(
|
||||
Ident("RETURN".to_owned()).to_owned()
|
||||
)])))])),
|
||||
what: ident_field("RETRUN"),
|
||||
fetch: Some(Fetchs(vec![Fetch(ident_field("RETURN"))]))
|
||||
}),
|
||||
)
|
||||
}
|
||||
|
|
|
@ -31,6 +31,10 @@ use crate::{
|
|||
use chrono::{offset::TimeZone, NaiveDate, Offset, Utc};
|
||||
use reblessive::Stack;
|
||||
|
||||
fn ident_field(name: &str) -> Value {
|
||||
Value::Idiom(Idiom(vec![Part::Field(Ident(name.to_string()))]))
|
||||
}
|
||||
|
||||
static SOURCE: &str = r#"
|
||||
ANALYZE INDEX b on a;
|
||||
BEGIN;
|
||||
|
@ -192,7 +196,7 @@ fn statements() -> Vec<Statement> {
|
|||
(Ident("b".to_string()), Kind::Array(Box::new(Kind::Bool), Some(3))),
|
||||
],
|
||||
block: Block(vec![Entry::Output(OutputStatement {
|
||||
what: Value::Table(Table("a".to_string())),
|
||||
what: ident_field("a"),
|
||||
fetch: None,
|
||||
})]),
|
||||
comment: Some(Strand("test".to_string())),
|
||||
|
@ -440,29 +444,23 @@ fn statements() -> Vec<Statement> {
|
|||
}),
|
||||
Statement::Ifelse(IfelseStatement {
|
||||
exprs: vec![
|
||||
(Value::Table(Table("foo".to_owned())), Value::Table(Table("bar".to_owned()))),
|
||||
(Value::Table(Table("faz".to_owned())), Value::Table(Table("baz".to_owned()))),
|
||||
(ident_field("foo"), ident_field("bar")),
|
||||
(ident_field("faz"), ident_field("baz")),
|
||||
],
|
||||
close: Some(Value::Table(Table("baq".to_owned()))),
|
||||
close: Some(ident_field("baq")),
|
||||
}),
|
||||
Statement::Ifelse(IfelseStatement {
|
||||
exprs: vec![
|
||||
(
|
||||
Value::Table(Table("foo".to_owned())),
|
||||
Value::Block(Box::new(Block(vec![Entry::Value(Value::Table(Table(
|
||||
"bar".to_owned(),
|
||||
)))]))),
|
||||
ident_field("foo"),
|
||||
Value::Block(Box::new(Block(vec![Entry::Value(ident_field("bar"))]))),
|
||||
),
|
||||
(
|
||||
Value::Table(Table("faz".to_owned())),
|
||||
Value::Block(Box::new(Block(vec![Entry::Value(Value::Table(Table(
|
||||
"baz".to_owned(),
|
||||
)))]))),
|
||||
ident_field("faz"),
|
||||
Value::Block(Box::new(Block(vec![Entry::Value(ident_field("baz"))]))),
|
||||
),
|
||||
],
|
||||
close: Some(Value::Block(Box::new(Block(vec![Entry::Value(Value::Table(Table(
|
||||
"baq".to_owned(),
|
||||
)))])))),
|
||||
close: Some(Value::Block(Box::new(Block(vec![Entry::Value(ident_field("baq"))])))),
|
||||
}),
|
||||
Statement::Info(InfoStatement::Root(false)),
|
||||
Statement::Info(InfoStatement::Ns(false)),
|
||||
|
@ -606,10 +604,8 @@ fn statements() -> Vec<Statement> {
|
|||
id: Value::Uuid(Uuid(uuid::uuid!("e72bee20-f49b-11ec-b939-0242ac120002"))),
|
||||
}),
|
||||
Statement::Output(OutputStatement {
|
||||
what: Value::Table(Table("RETRUN".to_owned())),
|
||||
fetch: Some(Fetchs(vec![Fetch(Value::Idiom(Idiom(vec![Part::Field(
|
||||
Ident("RETURN".to_owned()).to_owned(),
|
||||
)])))])),
|
||||
what: ident_field("RETRUN"),
|
||||
fetch: Some(Fetchs(vec![Fetch(ident_field("RETURN"))])),
|
||||
}),
|
||||
Statement::Relate(RelateStatement {
|
||||
only: true,
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
use std::collections::BTreeMap;
|
||||
|
||||
use reblessive::Stack;
|
||||
use rust_decimal::Decimal;
|
||||
|
||||
use crate::{
|
||||
sql::{
|
||||
|
@ -12,7 +13,7 @@ use crate::{
|
|||
|
||||
#[test]
|
||||
fn parse_index_expression() {
|
||||
let value = test_parse!(parse_value_table, "a[1 + 1]").unwrap();
|
||||
let value = test_parse!(parse_value_field, "a[1 + 1]").unwrap();
|
||||
let Value::Idiom(x) = value else {
|
||||
panic!("not the right value type");
|
||||
};
|
||||
|
@ -29,7 +30,7 @@ fn parse_index_expression() {
|
|||
|
||||
#[test]
|
||||
fn parse_coordinate() {
|
||||
let coord = test_parse!(parse_value_table, "(1.88, -18.0)").unwrap();
|
||||
let coord = test_parse!(parse_value_field, "(1.88, -18.0)").unwrap();
|
||||
let Value::Geometry(Geometry::Point(x)) = coord else {
|
||||
panic!("not the right value");
|
||||
};
|
||||
|
@ -37,14 +38,24 @@ fn parse_coordinate() {
|
|||
assert_eq!(x.y(), -18.0);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_numeric_object_key() {
|
||||
let v = test_parse!(parse_value_table, "{ 00: 0 }").unwrap();
|
||||
let Value::Object(object) = v else {
|
||||
panic!("not an object");
|
||||
};
|
||||
assert!(object.len() == 1);
|
||||
assert_eq!(object.get("00").cloned(), Some(Value::Number(Number::Int(0))));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_like_operator() {
|
||||
test_parse!(parse_value_table, "a ~ b").unwrap();
|
||||
test_parse!(parse_value_field, "a ~ b").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_range_operator() {
|
||||
test_parse!(parse_value_table, "1..2").unwrap();
|
||||
test_parse!(parse_value_field, "1..2").unwrap();
|
||||
}
|
||||
|
||||
#[test]
|
||||
|
@ -116,7 +127,7 @@ fn parse_large_depth_record_id() {
|
|||
|
||||
#[test]
|
||||
fn parse_recursive_record_string() {
|
||||
let res = test_parse!(parse_value_table, r#" r"a:[r"b:{c: r"d:1"}"]" "#).unwrap();
|
||||
let res = test_parse!(parse_value_field, r#" r"a:[r"b:{c: r"d:1"}"]" "#).unwrap();
|
||||
assert_eq!(
|
||||
res,
|
||||
Value::Thing(Thing {
|
||||
|
@ -137,7 +148,7 @@ fn parse_recursive_record_string() {
|
|||
|
||||
#[test]
|
||||
fn parse_record_string_2() {
|
||||
let res = test_parse!(parse_value_table, r#" r'a:["foo"]' "#).unwrap();
|
||||
let res = test_parse!(parse_value_field, r#" r'a:["foo"]' "#).unwrap();
|
||||
assert_eq!(
|
||||
res,
|
||||
Value::Thing(Thing {
|
||||
|
@ -149,82 +160,88 @@ fn parse_record_string_2() {
|
|||
|
||||
#[test]
|
||||
fn parse_i64() {
|
||||
let res = test_parse!(parse_value_table, r#" -9223372036854775808 "#).unwrap();
|
||||
let res = test_parse!(parse_value_field, r#" -9223372036854775808 "#).unwrap();
|
||||
assert_eq!(res, Value::Number(Number::Int(i64::MIN)));
|
||||
|
||||
let res = test_parse!(parse_value_table, r#" 9223372036854775807 "#).unwrap();
|
||||
let res = test_parse!(parse_value_field, r#" 9223372036854775807 "#).unwrap();
|
||||
assert_eq!(res, Value::Number(Number::Int(i64::MAX)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn parse_decimal() {
|
||||
let res = test_parse!(parse_value_field, r#" 0dec "#).unwrap();
|
||||
assert_eq!(res, Value::Number(Number::Decimal(Decimal::ZERO)));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn constant_lowercase() {
|
||||
let out = test_parse!(parse_value_table, r#" math::pi "#).unwrap();
|
||||
let out = test_parse!(parse_value_field, r#" math::pi "#).unwrap();
|
||||
assert_eq!(out, Value::Constant(Constant::MathPi));
|
||||
|
||||
let out = test_parse!(parse_value_table, r#" math::inf "#).unwrap();
|
||||
let out = test_parse!(parse_value_field, r#" math::inf "#).unwrap();
|
||||
assert_eq!(out, Value::Constant(Constant::MathInf));
|
||||
|
||||
let out = test_parse!(parse_value_table, r#" math::neg_inf "#).unwrap();
|
||||
let out = test_parse!(parse_value_field, r#" math::neg_inf "#).unwrap();
|
||||
assert_eq!(out, Value::Constant(Constant::MathNegInf));
|
||||
|
||||
let out = test_parse!(parse_value_table, r#" time::epoch "#).unwrap();
|
||||
let out = test_parse!(parse_value_field, r#" time::epoch "#).unwrap();
|
||||
assert_eq!(out, Value::Constant(Constant::TimeEpoch));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn constant_uppercase() {
|
||||
let out = test_parse!(parse_value_table, r#" MATH::PI "#).unwrap();
|
||||
let out = test_parse!(parse_value_field, r#" MATH::PI "#).unwrap();
|
||||
assert_eq!(out, Value::Constant(Constant::MathPi));
|
||||
|
||||
let out = test_parse!(parse_value_table, r#" MATH::INF "#).unwrap();
|
||||
let out = test_parse!(parse_value_field, r#" MATH::INF "#).unwrap();
|
||||
assert_eq!(out, Value::Constant(Constant::MathInf));
|
||||
|
||||
let out = test_parse!(parse_value_table, r#" MATH::NEG_INF "#).unwrap();
|
||||
let out = test_parse!(parse_value_field, r#" MATH::NEG_INF "#).unwrap();
|
||||
assert_eq!(out, Value::Constant(Constant::MathNegInf));
|
||||
|
||||
let out = test_parse!(parse_value_table, r#" TIME::EPOCH "#).unwrap();
|
||||
let out = test_parse!(parse_value_field, r#" TIME::EPOCH "#).unwrap();
|
||||
assert_eq!(out, Value::Constant(Constant::TimeEpoch));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn constant_mixedcase() {
|
||||
let out = test_parse!(parse_value_table, r#" MaTh::Pi "#).unwrap();
|
||||
let out = test_parse!(parse_value_field, r#" MaTh::Pi "#).unwrap();
|
||||
assert_eq!(out, Value::Constant(Constant::MathPi));
|
||||
|
||||
let out = test_parse!(parse_value_table, r#" MaTh::Inf "#).unwrap();
|
||||
let out = test_parse!(parse_value_field, r#" MaTh::Inf "#).unwrap();
|
||||
assert_eq!(out, Value::Constant(Constant::MathInf));
|
||||
|
||||
let out = test_parse!(parse_value_table, r#" MaTh::Neg_Inf "#).unwrap();
|
||||
let out = test_parse!(parse_value_field, r#" MaTh::Neg_Inf "#).unwrap();
|
||||
assert_eq!(out, Value::Constant(Constant::MathNegInf));
|
||||
|
||||
let out = test_parse!(parse_value_table, r#" TiME::ePoCH "#).unwrap();
|
||||
let out = test_parse!(parse_value_field, r#" TiME::ePoCH "#).unwrap();
|
||||
assert_eq!(out, Value::Constant(Constant::TimeEpoch));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scientific_decimal() {
|
||||
let res = test_parse!(parse_value_table, r#" 9.7e-7dec "#).unwrap();
|
||||
let res = test_parse!(parse_value_field, r#" 9.7e-7dec "#).unwrap();
|
||||
assert!(matches!(res, Value::Number(Number::Decimal(_))));
|
||||
assert_eq!(res.to_string(), "0.00000097dec")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn scientific_number() {
|
||||
let res = test_parse!(parse_value_table, r#" 9.7e-5"#).unwrap();
|
||||
let res = test_parse!(parse_value_field, r#" 9.7e-5"#).unwrap();
|
||||
assert!(matches!(res, Value::Number(Number::Float(_))));
|
||||
assert_eq!(res.to_string(), "0.000097f")
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn number_method() {
|
||||
let res = test_parse!(parse_value_table, r#" 9.7e-5.sin()"#).unwrap();
|
||||
let res = test_parse!(parse_value_field, r#" 9.7e-5.sin()"#).unwrap();
|
||||
let expected = Value::Idiom(Idiom(vec![
|
||||
Part::Start(Value::Number(Number::Float(9.7e-5))),
|
||||
Part::Method("sin".to_string(), vec![]),
|
||||
]));
|
||||
assert_eq!(res, expected);
|
||||
|
||||
let res = test_parse!(parse_value_table, r#" 1.sin()"#).unwrap();
|
||||
let res = test_parse!(parse_value_field, r#" 1.sin()"#).unwrap();
|
||||
let expected = Value::Idiom(Idiom(vec![
|
||||
Part::Start(Value::Number(Number::Int(1))),
|
||||
Part::Method("sin".to_string(), vec![]),
|
||||
|
@ -234,10 +251,10 @@ fn number_method() {
|
|||
|
||||
#[test]
|
||||
fn datetime_error() {
|
||||
test_parse!(parse_value_table, r#" d"2001-01-01T01:01:01.9999999999" "#).unwrap_err();
|
||||
test_parse!(parse_value_field, r#" d"2001-01-01T01:01:01.9999999999" "#).unwrap_err();
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn empty_string() {
|
||||
test_parse!(parse_value_table, "").unwrap_err();
|
||||
test_parse!(parse_value_field, "").unwrap_err();
|
||||
}
|
||||
|
|
|
@ -125,7 +125,7 @@ impl Parser<'_> {
|
|||
pub(crate) async fn parse_range(&mut self, ctx: &mut Stk) -> ParseResult<Range> {
|
||||
// Check for beginning id
|
||||
let beg = if Self::kind_is_identifier(self.peek_whitespace().kind) {
|
||||
let v = ctx.run(|ctx| self.parse_value_table(ctx)).await?;
|
||||
let v = ctx.run(|ctx| self.parse_value_inherit(ctx)).await?;
|
||||
|
||||
if self.eat_whitespace(t!(">")) {
|
||||
Bound::Excluded(v)
|
||||
|
@ -142,7 +142,7 @@ impl Parser<'_> {
|
|||
|
||||
// parse ending id.
|
||||
let end = if Self::kind_is_identifier(self.peek_whitespace().kind) {
|
||||
let v = ctx.run(|ctx| self.parse_value_table(ctx)).await?;
|
||||
let v = ctx.run(|ctx| self.parse_value_inherit(ctx)).await?;
|
||||
if inclusive {
|
||||
Bound::Included(v)
|
||||
} else {
|
||||
|
|
|
@ -69,7 +69,7 @@ impl Parse<Self> for Expression {
|
|||
let mut parser = Parser::new(val.as_bytes());
|
||||
let mut stack = Stack::new();
|
||||
let value = stack
|
||||
.enter(|ctx| parser.parse_value_table(ctx))
|
||||
.enter(|ctx| parser.parse_value_field(ctx))
|
||||
.finish()
|
||||
.map_err(|e| e.render_on(val))
|
||||
.unwrap();
|
||||
|
|
|
@ -54,7 +54,7 @@ mod api_integration {
|
|||
name: String,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize, PartialEq, PartialOrd)]
|
||||
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, PartialOrd)]
|
||||
struct RecordBuf {
|
||||
id: RecordId,
|
||||
name: String,
|
||||
|
|
|
@ -502,6 +502,74 @@ async fn create_record_with_id_with_content() {
|
|||
);
|
||||
}
|
||||
|
||||
#[test_log::test(tokio::test)]
|
||||
async fn create_record_with_id_in_content() {
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Person {
|
||||
pub id: u32,
|
||||
pub name: &'static str,
|
||||
pub job: &'static str,
|
||||
}
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Record {
|
||||
#[allow(dead_code)]
|
||||
pub id: RecordId,
|
||||
}
|
||||
|
||||
let (permit, db) = new_db().await;
|
||||
db.use_ns(NS).use_db(Ulid::new().to_string()).await.unwrap();
|
||||
drop(permit);
|
||||
|
||||
let record: Option<RecordBuf> = db
|
||||
.create(("user", "john"))
|
||||
.content(RecordBuf {
|
||||
id: RecordId::from_table_key("user", "john"),
|
||||
name: "John Doe".to_owned(),
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
assert_eq!(record.unwrap().id, "user:john".parse::<RecordId>().unwrap());
|
||||
|
||||
let error = db
|
||||
.create::<Option<RecordBuf>>(("user", "john"))
|
||||
.content(RecordBuf {
|
||||
id: RecordId::from_table_key("user", "jane"),
|
||||
name: "John Doe".to_owned(),
|
||||
})
|
||||
.await
|
||||
.unwrap_err();
|
||||
match error {
|
||||
surrealdb::Error::Db(DbError::IdMismatch {
|
||||
..
|
||||
}) => {}
|
||||
surrealdb::Error::Api(ApiError::Query {
|
||||
..
|
||||
}) => {}
|
||||
error => panic!("unexpected error; {error:?}"),
|
||||
}
|
||||
|
||||
let _: Option<Record> = db
|
||||
.create("person")
|
||||
.content(Person {
|
||||
id: 1010,
|
||||
name: "Max Mustermann",
|
||||
job: "chef",
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
|
||||
let _: Option<Record> = db
|
||||
.update(("person", 1010))
|
||||
.content(Person {
|
||||
id: 1010,
|
||||
name: "Max Mustermann",
|
||||
job: "IT Tech",
|
||||
})
|
||||
.await
|
||||
.unwrap();
|
||||
}
|
||||
|
||||
#[test_log::test(tokio::test)]
|
||||
async fn insert_table() {
|
||||
let (permit, db) = new_db().await;
|
||||
|
|
|
@ -1,5 +1,5 @@
|
|||
use std::collections::HashMap;
|
||||
use std::fmt::{Debug, Formatter};
|
||||
use std::fmt::{Debug, Display, Formatter};
|
||||
use std::future::Future;
|
||||
use std::sync::Arc;
|
||||
use std::thread::Builder;
|
||||
|
@ -313,7 +313,11 @@ impl Test {
|
|||
/// Panics if the expected value is not equal to the actual value.
|
||||
/// Compliant with NaN and Constants.
|
||||
#[allow(dead_code)]
|
||||
pub fn expect_value(&mut self, val: Value) -> Result<&mut Self, Error> {
|
||||
pub fn expect_value_info<I: Display>(
|
||||
&mut self,
|
||||
val: Value,
|
||||
info: I,
|
||||
) -> Result<&mut Self, Error> {
|
||||
let tmp = self.next_value()?;
|
||||
// Then check they are indeed the same values
|
||||
//
|
||||
|
@ -324,14 +328,19 @@ impl Test {
|
|||
val
|
||||
};
|
||||
if val.is_nan() {
|
||||
assert!(tmp.is_nan(), "Expected NaN but got: {tmp}");
|
||||
assert!(tmp.is_nan(), "Expected NaN but got {info}: {tmp}");
|
||||
} else {
|
||||
assert_eq!(tmp, val, "{tmp:#}");
|
||||
assert_eq!(tmp, val, "{info} {tmp:#}");
|
||||
}
|
||||
//
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn expect_value(&mut self, val: Value) -> Result<&mut Self, Error> {
|
||||
self.expect_value_info(val, "")
|
||||
}
|
||||
|
||||
/// Expect values in the given slice to be present in the responses, following the same order.
|
||||
#[allow(dead_code)]
|
||||
pub fn expect_values(&mut self, values: &[Value]) -> Result<&mut Self, Error> {
|
||||
|
@ -344,7 +353,15 @@ impl Test {
|
|||
/// Expect the given value to be equals to the next response.
|
||||
#[allow(dead_code)]
|
||||
pub fn expect_val(&mut self, val: &str) -> Result<&mut Self, Error> {
|
||||
self.expect_value(value(val).unwrap_or_else(|_| panic!("INVALID VALUE:\n{val}")))
|
||||
self.expect_val_info(val, "")
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn expect_val_info<I: Display>(&mut self, val: &str, info: I) -> Result<&mut Self, Error> {
|
||||
self.expect_value_info(
|
||||
value(val).unwrap_or_else(|_| panic!("INVALID VALUE {info}:\n{val}")),
|
||||
info,
|
||||
)
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
|
|
|
@ -188,7 +188,7 @@ fn three_multi_index_query(with: &str, parallel: &str) -> String {
|
|||
DEFINE INDEX uniq_name ON TABLE person COLUMNS name UNIQUE;
|
||||
DEFINE INDEX idx_genre ON TABLE person COLUMNS genre;
|
||||
SELECT name FROM person {with} WHERE name = 'Jaime' OR genre = 'm' OR company @@ 'surrealdb' {parallel};
|
||||
SELECT name FROM person {with} WHERE name = 'Jaime' OR genre = 'm' OR company @@ 'surrealdb' {parallel} EXPLAIN FULL;
|
||||
SELECT name FROM person {with} WHERE name = 'Jaime' OR genre = 'm' OR company @@ 'surrealdb' {parallel} EXPLAIN FULL;
|
||||
SELECT name FROM person {with} WHERE name = 'Jaime' AND genre = 'm' AND company @@ 'surrealdb' {parallel};
|
||||
SELECT name FROM person {with} WHERE name = 'Jaime' AND genre = 'm' AND company @@ 'surrealdb' {parallel} EXPLAIN FULL;")
|
||||
}
|
||||
|
@ -3063,3 +3063,35 @@ async fn select_composite_standard_index() -> Result<(), Error> {
|
|||
async fn select_composite_unique_index() -> Result<(), Error> {
|
||||
select_composite_index(true).await
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn select_where_index_boolean_behaviour() -> Result<(), Error> {
|
||||
let sql = r"
|
||||
DEFINE INDEX flagIndex ON TABLE test COLUMNS flag;
|
||||
CREATE test:t CONTENT { flag:true };
|
||||
CREATE test:f CONTENT { flag:false };
|
||||
SELECT * FROM test;
|
||||
SELECT * FROM test WITH NOINDEX WHERE (true OR flag=true);
|
||||
SELECT * FROM test WITH NOINDEX WHERE (true OR flag==true);
|
||||
SELECT * FROM test WHERE (true OR flag=true);
|
||||
SELECT * FROM test WHERE (true OR flag==true);";
|
||||
let mut t = Test::new(sql).await?;
|
||||
t.expect_size(8)?;
|
||||
t.skip_ok(3)?;
|
||||
for i in 0..5 {
|
||||
t.expect_val_info(
|
||||
"[
|
||||
{
|
||||
flag: false,
|
||||
id: test:f
|
||||
},
|
||||
{
|
||||
flag: true,
|
||||
id: test:t
|
||||
}
|
||||
]",
|
||||
i,
|
||||
)?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
|
|
@ -348,13 +348,44 @@ async fn insert() -> Result<(), Box<dyn std::error::Error>> {
|
|||
assert_eq!(res.len(), 1, "result: {res:?}");
|
||||
assert_eq!(res[0]["name"], "foo", "result: {res:?}");
|
||||
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
|
||||
// Send INSERT command trying to create multiple records
|
||||
let res = socket
|
||||
.send_request(
|
||||
"insert",
|
||||
json!([
|
||||
"tester",
|
||||
[
|
||||
{
|
||||
"name": "foo",
|
||||
"value": "bar",
|
||||
},
|
||||
{
|
||||
"name": "foo",
|
||||
"value": "bar",
|
||||
}
|
||||
]
|
||||
]),
|
||||
)
|
||||
.await?;
|
||||
assert!(res.is_object(), "result: {res:?}");
|
||||
assert!(res["result"].is_array(), "result: {res:?}");
|
||||
let res = res["result"].as_array().unwrap();
|
||||
assert_eq!(res.len(), 2, "result: {res:?}");
|
||||
assert_eq!(res[0]["name"], "foo", "result: {res:?}");
|
||||
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
|
||||
assert_eq!(res[1]["name"], "foo", "result: {res:?}");
|
||||
assert_eq!(res[1]["value"], "bar", "result: {res:?}");
|
||||
// Verify the data was inserted and can be queried
|
||||
let res = socket.send_message_query("SELECT * FROM tester").await?;
|
||||
assert!(res[0]["result"].is_array(), "result: {res:?}");
|
||||
let res = res[0]["result"].as_array().unwrap();
|
||||
assert_eq!(res.len(), 1, "result: {res:?}");
|
||||
assert_eq!(res.len(), 3, "result: {res:?}");
|
||||
assert_eq!(res[0]["name"], "foo", "result: {res:?}");
|
||||
assert_eq!(res[0]["value"], "bar", "result: {res:?}");
|
||||
assert_eq!(res[1]["name"], "foo", "result: {res:?}");
|
||||
assert_eq!(res[1]["value"], "bar", "result: {res:?}");
|
||||
assert_eq!(res[2]["name"], "foo", "result: {res:?}");
|
||||
assert_eq!(res[2]["value"], "bar", "result: {res:?}");
|
||||
// Test passed
|
||||
server.finish().unwrap();
|
||||
Ok(())
|
||||
|
@ -1474,6 +1505,25 @@ async fn session_reauthentication_expired() {
|
|||
server.finish().unwrap();
|
||||
}
|
||||
|
||||
#[test(tokio::test)]
|
||||
async fn session_failed_reauthentication() {
|
||||
// Setup database server without authentication
|
||||
let (addr, mut server) = common::start_server_without_auth().await.unwrap();
|
||||
// Connect to WebSocket
|
||||
let mut socket = Socket::connect(&addr, SERVER, FORMAT).await.unwrap();
|
||||
// Specify a namespace and database to use
|
||||
socket.send_message_use(Some(NS), Some(DB)).await.unwrap();
|
||||
// Check that we have are have a database and namespace selected
|
||||
socket.send_message_query("INFO FOR DB").await.unwrap();
|
||||
// Authenticate using an invalid token
|
||||
socket.send_request("authenticate", json!(["invalid",])).await.unwrap();
|
||||
// Check to see if we still have a namespace and database selected
|
||||
let res = socket.send_message_query("INFO FOR DB").await.unwrap();
|
||||
assert_eq!(res[0]["status"], "OK", "result: {res:?}");
|
||||
// Test passed
|
||||
server.finish().unwrap();
|
||||
}
|
||||
|
||||
#[test(tokio::test)]
|
||||
async fn session_use_change_database() {
|
||||
// Setup database server
|
||||
|
|
Loading…
Reference in a new issue