Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add insert relation #3813

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 0 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

82 changes: 44 additions & 38 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,13 @@ parser2 = ["surrealdb/parser2"]
performance-profiler = ["dep:pprof"]

[workspace]
members = ["core", "lib", "lib/examples/actix", "lib/examples/axum", "lib/examples/rocket"]
members = [
"core",
"lib",
"lib/examples/actix",
"lib/examples/axum",
"lib/examples/rocket",
]

[profile.release]
lto = true
Expand All @@ -45,10 +51,10 @@ base64 = "0.21.5"
bytes = "1.5.0"
ciborium = "0.2.1"
clap = { version = "4.4.11", features = [
"env",
"derive",
"wrap_help",
"unicode",
"env",
"derive",
"wrap_help",
"unicode",
] }
futures = "0.3.29"
futures-util = "0.3.29"
Expand All @@ -63,13 +69,13 @@ opentelemetry = { version = "0.19", features = ["rt-tokio"] }
opentelemetry-otlp = { version = "0.12.0", features = ["metrics"] }
pin-project-lite = "0.2.13"
pprof = { version = "0.13.0", features = [
"flamegraph",
"prost-codec",
"flamegraph",
"prost-codec",
], optional = true }
rand = "0.8.5"
reqwest = { version = "0.11.22", default-features = false, features = [
"blocking",
"gzip",
"blocking",
"gzip",
] }
revision = "0.5.0"
rmpv = "1.0.1"
Expand All @@ -79,27 +85,27 @@ serde = { version = "1.0.193", features = ["derive"] }
serde_json = "1.0.108"
serde_pack = { version = "1.1.2", package = "rmp-serde" }
surrealdb = { version = "1", path = "lib", features = [
"protocol-http",
"protocol-ws",
"rustls",
"protocol-http",
"protocol-ws",
"rustls",
] }
tempfile = "3.8.1"
thiserror = "1.0.50"
tokio = { version = "1.34.0", features = ["macros", "signal"] }
tokio-util = { version = "0.7.10", features = ["io"] }
tower = "0.4.13"
tower-http = { version = "0.4.4", features = [
"trace",
"sensitive-headers",
"auth",
"request-id",
"util",
"catch-panic",
"cors",
"set-header",
"limit",
"add-extension",
"compression-full",
"trace",
"sensitive-headers",
"auth",
"request-id",
"util",
"catch-panic",
"cors",
"set-header",
"limit",
"add-extension",
"compression-full",
] }
tracing = "0.1"
tracing-opentelemetry = "0.19.0"
Expand All @@ -123,18 +129,18 @@ jemallocator = "0.5.4"
assert_fs = "1.0.13"
env_logger = "0.10.1"
opentelemetry-proto = { version = "0.2.0", features = [
"gen-tonic",
"traces",
"metrics",
"logs",
"gen-tonic",
"traces",
"metrics",
"logs",
] }
rcgen = "0.11.3"
serial_test = "2.0.0"
temp-env = { version = "0.3.6", features = ["async_closure"] }
test-log = { version = "0.2.13", features = ["trace"] }
tokio-stream = { version = "0.1", features = ["net"] }
tokio-tungstenite = { version = "0.20.1" }
tonic = "0.8.3"
# tonic = "0.8.3"
ulid = "1.1.0"
wiremock = "0.5.22"

Expand All @@ -150,16 +156,16 @@ depends = "$auto"
section = "utility"
priority = "optional"
assets = [
[
"target/release/surreal",
"usr/share/surrealdb/surreal",
"755",
],
[
"pkg/deb/README",
"usr/share/surrealdb/README",
"644",
],
[
"target/release/surreal",
"usr/share/surrealdb/surreal",
"755",
],
[
"pkg/deb/README",
"usr/share/surrealdb/README",
"644",
],
]
extended-description = "A scalable, distributed, collaborative, document-graph database, for the realtime web."
license-file = ["LICENSE", "4"]
4 changes: 2 additions & 2 deletions core/src/doc/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,6 @@ impl<'a> Document<'a> {
txn: &Transaction,
stm: &Statement<'_>,
) -> Result<Value, Error> {
// Check if table has correct relation status
self.relation(ctx, opt, txn, stm).await?;
// Merge record data
self.merge(ctx, opt, txn, stm).await?;
// Merge fields data
Expand Down Expand Up @@ -85,6 +83,8 @@ impl<'a> Document<'a> {
txn: &Transaction,
stm: &Statement<'_>,
) -> Result<Value, Error> {
// Check if table has correct relation status
self.relation(ctx, opt, txn, stm).await?;
// Check if allowed
self.allow(ctx, opt, txn, stm).await?;
// Alter record data
Expand Down
1 change: 1 addition & 0 deletions core/src/doc/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ impl<'a> Document<'a> {
Statement::Update(_) => doc.update(ctx, opt, txn, stm).await,
Statement::Relate(_) => doc.relate(ctx, opt, txn, stm).await,
Statement::Delete(_) => doc.delete(ctx, opt, txn, stm).await,
Statement::Insert(i) if i.relation => doc.insert(ctx, opt, txn, stm).await,
Statement::Insert(_) => doc.insert(ctx, opt, txn, stm).await,
_ => unreachable!(),
};
Expand Down
64 changes: 59 additions & 5 deletions core/src/sql/statements/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use std::fmt;

#[derive(Clone, Debug, Default, Eq, PartialEq, PartialOrd, Serialize, Deserialize, Store, Hash)]
#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
#[revisioned(revision = 1)]
#[revisioned(revision = 2)]
#[non_exhaustive]
pub struct InsertStatement {
pub into: Value,
Expand All @@ -20,6 +20,8 @@ pub struct InsertStatement {
pub output: Option<Output>,
pub timeout: Option<Timeout>,
pub parallel: bool,
#[revision(start = 2)]
pub relation: bool,
}

impl InsertStatement {
Expand All @@ -42,8 +44,8 @@ impl InsertStatement {
// Ensure futures are stored
let opt = &opt.new_with_futures(false).with_projections(false);
// Parse the expression
match self.into.compute(ctx, opt, txn, doc).await? {
Value::Table(into) => match &self.data {
match (self.relation, self.into.compute(ctx, opt, txn, doc).await?) {
(false, Value::Table(into)) => match &self.data {
// Check if this is a traditional statement
Data::ValuesExpression(v) => {
for v in v {
Expand Down Expand Up @@ -85,9 +87,61 @@ impl InsertStatement {
}
}
}
_ => unreachable!(),
v => {
return Err(Error::InsertStatement {
value: v.to_string(),
})
}
},
v => {
(true, val) => {
println!("Got to (true, val)");
let into = match val {
Value::None => None,
Value::Table(into) => Some(into),
_ => {
return Err(Error::InsertStatement {
value: val.to_string(),
})
}
};

match &self.data {
Data::SingleExpression(Value::Array(v)) => {
for r in v.iter() {
let Value::Object(o) = r else {
return Err(Error::InsertStatement {
value: r.to_string(),
});
};
let Some(Value::Thing(in_id)) = o.get("in") else {
return Err(Error::Thrown("No in specified".to_string()));
};
let Some(Value::Thing(out_id)) = o.get("out") else {
return Err(Error::Thrown("No out specified".to_string()));
};
let id = match (&into, o.get("id")) {
(_, Some(Value::Thing(id))) => id.clone(),
(Some(tb), _) => tb.generate(),
(_, _) => {
return Err(Error::Thrown(
"No id or table specified".to_string(),
))
}
};
// i.ingest(Iterable::Mergeable(id.clone(), Value::Object(o.to_owned())));
println!("\nIterable::Relate({in_id}, {id}, {out_id})\n");
i.ingest(Iterable::Relatable(in_id.clone(), id, out_id.clone()))
}
}
e => {
return Err(Error::InsertStatement {
value: e.to_string(),
})
}
}
}
// not relation and not table is error
(false, v) => {
return Err(Error::InsertStatement {
value: v.to_string(),
})
Expand Down
12 changes: 10 additions & 2 deletions core/src/sql/statements/relate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,10 @@ impl RelateStatement {
let w = w.clone();
match &self.kind {
// The relation has a specific record id
Value::Thing(id) => i.ingest(Iterable::Relatable(f, id.to_owned(), w)),
Value::Thing(id) => {
println!("\nIterable::Relate({f}, {id}, {w})\n");
i.ingest(Iterable::Relatable(f, id.to_owned(), w))
}
// The relation does not have a specific record id
Value::Table(tb) => match &self.data {
// There is a data clause so check for a record id
Expand All @@ -143,10 +146,15 @@ impl RelateStatement {
Some(id) => id.generate(tb, false)?,
None => tb.generate(),
};
println!("\nIterable::Relate({f}, {id}, {w})\n");
i.ingest(Iterable::Relatable(f, id, w))
}
// There is no data clause so create a record id
None => i.ingest(Iterable::Relatable(f, tb.generate(), w)),
None => {
let id = tb.generate();
println!("\nIterable::Relate({f}, {id}, {w})\n");
i.ingest(Iterable::Relatable(f, id, w))
}
},
// The relation can not be any other type
_ => unreachable!(),
Expand Down
27 changes: 17 additions & 10 deletions core/src/sql/value/serde/ser/statement/insert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ pub struct SerializeInsertStatement {
output: Option<Output>,
timeout: Option<Timeout>,
parallel: Option<bool>,
relation: Option<bool>,
}

impl serde::ser::SerializeStruct for SerializeInsertStatement {
Expand Down Expand Up @@ -79,6 +80,9 @@ impl serde::ser::SerializeStruct for SerializeInsertStatement {
"parallel" => {
self.parallel = Some(value.serialize(ser::primitive::bool::Serializer.wrap())?);
}
"relation" => {
self.parallel = Some(value.serialize(ser::primitive::bool::Serializer.wrap())?);
}
key => {
return Err(Error::custom(format!("unexpected field `InsertStatement::{key}`")));
}
Expand All @@ -87,16 +91,19 @@ impl serde::ser::SerializeStruct for SerializeInsertStatement {
}

fn end(self) -> Result<Self::Ok, Error> {
match (self.into, self.data, self.ignore, self.parallel) {
(Some(into), Some(data), Some(ignore), Some(parallel)) => Ok(InsertStatement {
into,
data,
ignore,
parallel,
update: self.update,
output: self.output,
timeout: self.timeout,
}),
match (self.into, self.data, self.ignore, self.parallel, self.relation) {
(Some(into), Some(data), Some(ignore), Some(parallel), Some(relation)) => {
Ok(InsertStatement {
into,
data,
ignore,
parallel,
update: self.update,
output: self.output,
timeout: self.timeout,
relation,
})
}
_ => Err(Error::custom("`InsertStatement` missing required value(s)")),
}
}
Expand Down
8 changes: 4 additions & 4 deletions core/src/syn/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,10 @@
pub mod common;
pub mod error;

#[cfg(not(feature = "experimental-parser"))]
pub mod v1;
#[cfg(not(feature = "experimental-parser"))]
pub use v1::{datetime_raw, duration, idiom, json, parse, range, subquery, thing, value};
// #[cfg(not(feature = "experimental-parser"))]
// pub mod v1;
// #[cfg(not(feature = "experimental-parser"))]
// pub use v1::{datetime_raw, duration, idiom, json, parse, range, subquery, thing, value};

#[cfg(feature = "experimental-parser")]
pub mod v2;
Expand Down