Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion core/crates/core/src/browse/build_export.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ use super::{BrowseSqlError, ExportRequest};
/// unchanged table come out the same. A heap address, or a key the
/// engine would sort the whole table by, orders them only behind the
/// user's sort: on its own it would hold every row before the first
/// could be written.
/// could be written. A table's rows hold only its columns, so SQL reads
/// the listed columns for either `ExportFields`.
pub fn build_export(dialect: &dyn SqlDialect, request: &ExportRequest<'_>) -> Result<Statement, BrowseSqlError> {
if crate::document::speaks_shell(dialect) {
return crate::document::export(request);
Expand Down
12 changes: 12 additions & 0 deletions core/crates/core/src/browse/export_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,4 +13,16 @@ pub struct ExportRequest<'a> {
pub filter: &'a FilterSet,
pub sort: &'a [SortKey],
pub identity: &'a RowIdentity,
pub fields: ExportFields,
}

/// Which fields an export reads. A table's rows hold its columns either
/// way; a collection's columns come from a census of its first documents,
/// so a later document can hold a field they do not list.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExportFields {
/// The columns `columns` lists, in its order.
Listed,
/// Every field the rows hold, as the driver finds them while it reads.
Every,
}
2 changes: 1 addition & 1 deletion core/crates/core/src/browse/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ pub use build_count::build_count;
pub use build_export::build_export;
pub use build_page::build_page;
pub use declared_precision::DeclaredPrecisionSink;
pub use export_request::ExportRequest;
pub use export_request::{ExportFields, ExportRequest};
pub use page_layout::PageLayout;
pub use page_request::PageRequest;
pub use page_spec::PageSpec;
Expand Down
22 changes: 16 additions & 6 deletions core/crates/core/src/browse/tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -496,19 +496,28 @@ fn build_export_projects_only_visible_columns() {
ascending: true,
}];

let statement = build_export(
let table = table();
let request = ExportRequest {
table: &table,
columns: &columns,
filter: &filter,
sort: &sort,
identity: &identity,
fields: ExportFields::Every,
};

let statement = build_export(&dialect, &request).expect("the export");
let listed = build_export(
&dialect,
&ExportRequest {
table: &table(),
columns: &columns,
filter: &filter,
sort: &sort,
identity: &identity,
fields: ExportFields::Listed,
..request
},
)
.expect("the export");

let sql = statement.sql();
assert_eq!(listed.sql(), sql, "a table's rows hold only its columns");
let projection = sql
.strip_prefix("SELECT ")
.and_then(|rest| rest.split_once(" FROM "))
Expand Down Expand Up @@ -538,6 +547,7 @@ fn export_sql_in(dialect: &TestDialect, identity: RowIdentity, sort: &[SortKey])
filter: &FilterSet::default(),
sort,
identity: &identity,
fields: ExportFields::Listed,
},
)
.expect("the export");
Expand Down
37 changes: 35 additions & 2 deletions core/crates/core/src/document/document_filter.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,44 @@
use crate::column::ColumnInfo;
use crate::column::{ColumnInfo, ColumnKind};
use crate::edit::parse_filter_text;
use crate::filter::{BuildFilterError, Combinator, FilterOp, FilterRule, FilterSet, FilterValue, operators_for};
use crate::sql_syntax::mongo::{ShellValue, is_addressable_field, parse_value};
use crate::value::Value;

use super::shell_params::ShellParams;

const JSON_OPERATORS: &[FilterOp] = &[
FilterOp::Eq,
FilterOp::NotEq,
FilterOp::Lt,
FilterOp::LtEq,
FilterOp::Gt,
FilterOp::GtEq,
FilterOp::Between,
FilterOp::Contains,
FilterOp::StartsWith,
FilterOp::EndsWith,
FilterOp::Like,
FilterOp::NotLike,
FilterOp::Ilike,
FilterOp::In,
FilterOp::NotIn,
FilterOp::IsNull,
FilterOp::IsNotNull,
];

/// The operators a filter rule takes on a field, in the order the filter
/// strip lists them. A JSON column compares as well as matching text: a
/// value typed for it binds as the BSON its Extended JSON names, and the
/// server compares it with the field's values of that type, numbers of
/// every type with each other, so a field of ints and doubles keeps `<`
/// and Between.
pub fn filter_operators(column: &ColumnInfo) -> &'static [FilterOp] {
match column.column_type.kind {
ColumnKind::Json { .. } => JSON_OPERATORS,
_ => operators_for(&column.column_type.kind),
}
}

/// The filter document a collection's page, count and export read
/// under: one clause per rule, then the document the user typed, joined
/// with `$and` or `$or`. `None` matches every document.
Expand All @@ -23,7 +56,7 @@ pub(crate) fn filter_document(
.iter()
.find(|column| column.name == rule.column)
.ok_or_else(|| BuildFilterError::UnknownColumn(rule.column.clone()))?;
if !operators_for(&column.column_type.kind).contains(&rule.op) {
if !filter_operators(column).contains(&rule.op) {
return Err(BuildFilterError::OperatorNotAllowed {
column: rule.column.clone(),
op: rule.op,
Expand Down
8 changes: 6 additions & 2 deletions core/crates/core/src/document/document_read.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use crate::browse::{
BrowseSqlError, ExportRequest, PageLayout, PageRequest, PageSpec, PagingMode, RowKeyLayout, SortKey,
BrowseSqlError, ExportFields, ExportRequest, PageLayout, PageRequest, PageSpec, PagingMode, RowKeyLayout, SortKey,
};
use crate::column::ColumnInfo;
use crate::dialect::TablePaging;
Expand Down Expand Up @@ -100,6 +100,8 @@ pub(crate) fn count(table: &TableRef, columns: &[ColumnInfo], filter: &FilterSet
}

/// Every document the filter matches, in the page's order, for an export.
/// `ExportFields::Every` writes no `$project`, so the server sends each
/// whole document and the driver finds the columns in them.
pub(crate) fn export(request: &ExportRequest<'_>) -> Result<Statement, BrowseSqlError> {
let key = key_column(request.identity, request.columns)?;
let mut params = ShellParams::new();
Expand All @@ -114,7 +116,9 @@ pub(crate) fn export(request: &ExportRequest<'_>) -> Result<Statement, BrowseSql
if !sort.is_empty() {
stages.push(field("$sort", sort_document(sort)));
}
stages.push(field("$project", projection(request.columns)));
if request.fields == ExportFields::Listed {
stages.push(field("$project", projection(request.columns)));
}
Ok(params.statement(&aggregate(request.table, stages)))
}

Expand Down
49 changes: 36 additions & 13 deletions core/crates/core/src/document/document_write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,9 @@ use super::document_read::collection;
use super::shell_params::ShellParams;

/// A new document of the cells set. A cell left to the server is left
/// out, which for `_id` lets the server make one.
/// out, which for `_id` lets the server make one. A NULL cell is left out
/// too: a page reads a missing field as NULL, and an explicit `null` is a
/// field that a sparse or partial index counts.
pub(crate) fn insert(
table: &TableRef,
columns: &[ColumnInfo],
Expand All @@ -26,7 +28,7 @@ pub(crate) fn insert(
let mut params = ShellParams::new();
let mut fields = Vec::new();
for (column, cell) in columns.iter().zip(cells) {
if let Some(value) = cell.value() {
if let Some(value) = cell.value().filter(|value| !value.is_null()) {
fields.push((column.name.clone(), params.bind(value.clone(), Some(column))?));
}
}
Expand All @@ -36,7 +38,8 @@ pub(crate) fn insert(
Ok(params.statement(&call))
}

/// Several new documents, all of the same columns.
/// Several new documents, all of the same columns, each leaving out its
/// NULL cells as `insert` does.
pub(crate) fn insert_many(
table: &TableRef,
columns: &[&ColumnInfo],
Expand All @@ -56,7 +59,9 @@ pub(crate) fn insert_many(
}
let mut fields = Vec::with_capacity(columns.len());
for (&column, value) in columns.iter().zip(row) {
fields.push((column.name.clone(), params.bind(value, Some(column))?));
if !value.is_null() {
fields.push((column.name.clone(), params.bind(value, Some(column))?));
}
}
documents.push(ShellValue::Object(fields));
}
Expand All @@ -67,7 +72,9 @@ pub(crate) fn insert_many(
}

/// The fields changed on one document, with `$set`. Only the fields named
/// change; one the document lacked is added.
/// change; one the document lacked is added. A field set to NULL is
/// removed with `$unset`, as a new document leaves it out, so a sparse or
/// partial index stops counting it. A key keeps `$set`.
pub(crate) fn update(
table: &TableRef,
columns: &[ColumnInfo],
Expand All @@ -78,8 +85,10 @@ pub(crate) fn update(
if assignments.is_empty() {
return Err(BuildSqlError::NothingToUpdate);
}
let names = key_names(identity)?;
let mut params = ShellParams::new();
let mut set = Vec::with_capacity(assignments.len());
let mut unset = Vec::new();
for (index, input) in assignments {
let column = columns.get(*index).ok_or(BuildSqlError::UnknownKeyColumn {
name: index.to_string(),
Expand All @@ -94,13 +103,23 @@ pub(crate) fn update(
name: column.name.clone(),
});
};
set.push((column.name.clone(), params.bind(value.clone(), Some(column))?));
match value {
Value::Null if !names.contains(&column.name) => {
unset.push((column.name.clone(), ShellValue::string("")));
}
_ => set.push((column.name.clone(), params.bind(value.clone(), Some(column))?)),
}
}
let filter = key_filter(columns, identity, key, &mut params)?;
let operators = [("$set", set), ("$unset", unset)]
.into_iter()
.filter(|(_, fields)| !fields.is_empty())
.map(|(name, fields)| (name.to_owned(), ShellValue::Object(fields)))
.collect();
let mut call = collection(table);
call.methods.push(ShellMethod::new(
"updateOne",
vec![filter, field("$set", ShellValue::Object(set))],
vec![filter, ShellValue::Object(operators)],
));
Ok(params.statement(&call))
}
Expand Down Expand Up @@ -142,12 +161,7 @@ fn key_filter(
key: &[Value],
params: &mut ShellParams,
) -> Result<ShellValue, BuildSqlError> {
let names = match identity {
RowIdentity::UniqueKey { columns } => columns,
RowIdentity::SortingKey { expressions } => expressions,
RowIdentity::EngineRowId(_) => return Err(BuildSqlError::NoRowIdentity),
RowIdentity::Unordered => return Err(BuildSqlError::ReadOnlyRows),
};
let names = key_names(identity)?;
if names.len() != key.len() {
return Err(BuildSqlError::LengthMismatch {
expected: names.len(),
Expand All @@ -167,3 +181,12 @@ fn key_filter(
}
Ok(ShellValue::Object(fields))
}

fn key_names(identity: &RowIdentity) -> Result<&[String], BuildSqlError> {
match identity {
RowIdentity::UniqueKey { columns } => Ok(columns),
RowIdentity::SortingKey { expressions } => Ok(expressions),
RowIdentity::EngineRowId(_) => Err(BuildSqlError::NoRowIdentity),
RowIdentity::Unordered => Err(BuildSqlError::ReadOnlyRows),
}
}
1 change: 1 addition & 0 deletions core/crates/core/src/document/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ mod tests;
mod value_order;

pub(crate) use document_catalog::{create_database, delete_all_documents, drop_database};
pub use document_filter::filter_operators;
pub(crate) use document_filter::{filter_document, typed_document};
pub(crate) use document_read::{count, export, page};
pub(crate) use document_write::{delete, insert, insert_many, key_probe, update};
Expand Down
Loading
Loading