Expand logic for static value validation (#3151)

This commit is contained in:
Micha de Vries 2023-12-14 22:23:58 +01:00 committed by GitHub
parent e6a2ef5e94
commit f66b7c8eec
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 43 additions and 3 deletions

View file

@ -149,6 +149,10 @@ impl Array {
pub(crate) fn is_all_none_or_null(&self) -> bool { pub(crate) fn is_all_none_or_null(&self) -> bool {
self.0.iter().all(|v| v.is_none_or_null()) self.0.iter().all(|v| v.is_none_or_null())
} }
pub(crate) fn is_static(&self) -> bool {
self.iter().all(Value::is_static)
}
} }
impl Display for Array { impl Display for Array {

View file

@ -87,6 +87,20 @@ impl Expression {
} }
} }
pub(crate) fn is_static(&self) -> bool {
match self {
Self::Unary {
v,
..
} => v.is_static(),
Self::Binary {
l,
r,
..
} => l.is_static() && r.is_static(),
}
}
/// Returns the operator /// Returns the operator
pub(crate) fn operator(&self) -> &Operator { pub(crate) fn operator(&self) -> &Operator {
match self { match self {

View file

@ -89,6 +89,14 @@ impl Function {
matches!(self, Self::Script(_, _)) matches!(self, Self::Script(_, _))
} }
/// Check if this function has static arguments
pub fn is_static(&self) -> bool {
match self {
Self::Normal(_, a) => a.iter().all(Value::is_static),
_ => false,
}
}
/// Check if this function is a rolling function /// Check if this function is a rolling function
pub fn is_rolling(&self) -> bool { pub fn is_rolling(&self) -> bool {
match self { match self {

View file

@ -222,6 +222,10 @@ impl Object {
} }
Ok(Value::Object(Object(x))) Ok(Value::Object(Object(x)))
} }
pub(crate) fn is_static(&self) -> bool {
self.values().all(Value::is_static)
}
} }
impl Display for Object { impl Display for Object {

View file

@ -2313,8 +2313,10 @@ impl Value {
Value::Duration(_) => true, Value::Duration(_) => true,
Value::Datetime(_) => true, Value::Datetime(_) => true,
Value::Geometry(_) => true, Value::Geometry(_) => true,
Value::Array(v) => v.iter().all(Value::is_static), Value::Array(v) => v.is_static(),
Value::Object(v) => v.values().all(Value::is_static), Value::Object(v) => v.is_static(),
Value::Expression(v) => v.is_static(),
Value::Function(v) => v.is_static(),
Value::Constant(_) => true, Value::Constant(_) => true,
_ => false, _ => false,
} }

View file

@ -447,6 +447,7 @@ async fn field_definition_default_value() -> Result<(), Error> {
DEFINE FIELD primary ON product TYPE number VALUE 123.456; DEFINE FIELD primary ON product TYPE number VALUE 123.456;
DEFINE FIELD secondary ON product TYPE bool DEFAULT true VALUE $value; DEFINE FIELD secondary ON product TYPE bool DEFAULT true VALUE $value;
DEFINE FIELD tertiary ON product TYPE string DEFAULT 'hello' VALUE 'tester'; DEFINE FIELD tertiary ON product TYPE string DEFAULT 'hello' VALUE 'tester';
DEFINE FIELD quaternary ON product TYPE bool VALUE array::all([1, 2]);
-- --
CREATE product:test SET primary = NULL; CREATE product:test SET primary = NULL;
CREATE product:test SET secondary = 'oops'; CREATE product:test SET secondary = 'oops';
@ -460,7 +461,10 @@ async fn field_definition_default_value() -> Result<(), Error> {
let dbs = new_ds().await?; let dbs = new_ds().await?;
let ses = Session::owner().with_ns("test").with_db("test"); let ses = Session::owner().with_ns("test").with_db("test");
let res = &mut dbs.execute(sql, &ses, None).await?; let res = &mut dbs.execute(sql, &ses, None).await?;
assert_eq!(res.len(), 11); assert_eq!(res.len(), 12);
//
let tmp = res.remove(0).result;
assert!(tmp.is_ok());
// //
let tmp = res.remove(0).result; let tmp = res.remove(0).result;
assert!(tmp.is_ok()); assert!(tmp.is_ok());
@ -510,6 +514,7 @@ async fn field_definition_default_value() -> Result<(), Error> {
{ {
id: product:test, id: product:test,
primary: 123.456, primary: 123.456,
quaternary: true,
secondary: true, secondary: true,
tertiary: 'tester', tertiary: 'tester',
} }
@ -523,6 +528,7 @@ async fn field_definition_default_value() -> Result<(), Error> {
{ {
id: product:test, id: product:test,
primary: 123.456, primary: 123.456,
quaternary: true,
secondary: true, secondary: true,
tertiary: 'tester', tertiary: 'tester',
} }
@ -536,6 +542,7 @@ async fn field_definition_default_value() -> Result<(), Error> {
{ {
id: product:test, id: product:test,
primary: 123.456, primary: 123.456,
quaternary: true,
secondary: false, secondary: false,
tertiary: 'tester', tertiary: 'tester',
} }
@ -549,6 +556,7 @@ async fn field_definition_default_value() -> Result<(), Error> {
{ {
id: product:test, id: product:test,
primary: 123.456, primary: 123.456,
quaternary: true,
secondary: false, secondary: false,
tertiary: 'tester', tertiary: 'tester',
} }