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
47 changes: 47 additions & 0 deletions core/crates/core/src/sql_syntax/script/script_plan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,24 @@ impl ScriptPlan {
.collect()
}

// Where the first statement's terminator starts, or the text's end when nothing ends it, lexed without keeping the
// tokens: SQLite reads only the first statement of a stored CREATE text, whatever follows it. A semicolon inside
// parentheses ends nothing, since SQLite reads it as one of a virtual table's module arguments, which the editor's
// split, like `sqlite3_complete`, does not know.
pub fn first_statement_end(text: &str, grammar: SqlGrammar) -> usize {
match rules_for(grammar) {
Some(rules) => Builder::new(
text,
rules,
LexicalSettings::default_for(grammar),
ServerReading::Client,
";",
)
.first_statement_end(),
None => text.len(),
}
}

pub fn batches(&self) -> &[ScriptBatch] {
&self.batches
}
Expand Down Expand Up @@ -217,6 +235,35 @@ impl<'a> Builder<'a> {
(plan, settled)
}

fn first_statement_end(mut self) -> usize {
let mut depth = 0usize;
while self.lexer.position() < self.text.len() {
let start = self.lexer.position();
let ended_at = if self.reads_directives && self.lexer.at_line_start() && self.accept_directive() {
start
} else {
let delimiter = (self.rules.boundary == Boundary::DelimiterDirectives).then_some(self.delimiter);
let Some(token) = self.lexer.next_token(delimiter) else {
break;
};
match token.kind {
ScriptTokenKind::OpenParen => depth += 1,
ScriptTokenKind::CloseParen => depth = depth.saturating_sub(1),
ScriptTokenKind::Semicolon if depth > 0 => continue,
_ => {}
}
let token_start = token.range.start;
self.accept(token);
token_start
};
self.tokens.clear();
if !self.statements.is_empty() {
return ended_at;
}
}
self.text.len()
}

fn accept_directive(&mut self) -> bool {
let line_start = self.lexer.position();
match self.rules.boundary {
Expand Down
31 changes: 31 additions & 0 deletions core/crates/core/tests/script_corpus.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,37 @@ fn batch_texts<'a>(plan: &ScriptPlan, text: &'a str) -> Vec<&'a str> {
.collect()
}

#[test]
fn the_first_statement_ends_where_its_terminator_starts() {
let cases = [
("CREATE TABLE t (a)", "CREATE TABLE t (a)"),
("CREATE TABLE t (a); DROP TABLE t", "CREATE TABLE t (a)"),
(
"CREATE TABLE t (a DEFAULT ';', \"b;\" /* ; */, [c;] -- ;\n); SELECT 1",
"CREATE TABLE t (a DEFAULT ';', \"b;\" /* ; */, [c;] -- ;\n)",
),
(
"CREATE TRIGGER r AFTER INSERT ON t BEGIN SELECT 1; SELECT 2; END; SELECT 3",
"CREATE TRIGGER r AFTER INSERT ON t BEGIN SELECT 1; SELECT 2; END",
),
("CREATE INDEX i ON t (a) /* ; */", "CREATE INDEX i ON t (a) /* ; */"),
("CREATE VIEW v AS SELECT 1 -- note", "CREATE VIEW v AS SELECT 1 -- note"),
(
"CREATE VIRTUAL TABLE t USING fts4(a;b, tokenize=simple (x;y)); DROP TABLE t",
"CREATE VIRTUAL TABLE t USING fts4(a;b, tokenize=simple (x;y))",
),
(
"CREATE VIRTUAL TABLE t USING fts4(a;b)",
"CREATE VIRTUAL TABLE t USING fts4(a;b)",
),
("", ""),
];
for (text, first) in cases {
let end = ScriptPlan::first_statement_end(text, SqlGrammar::Sqlite);
assert_eq!(text.get(..end), Some(first), "{text}");
}
}

#[test]
fn dollar_quoted_body_hides_semicolons() {
let text = "CREATE FUNCTION f() RETURNS int AS $$ SELECT 1; SELECT 2; $$ LANGUAGE sql;\nSELECT 3;";
Expand Down
92 changes: 61 additions & 31 deletions core/crates/drivers/sqlite/src/catalog/dependents.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashSet;
use std::collections::{HashMap, HashSet};

use tablepro_core::ddl::{SchemaObjectKind, SchemaObjectSource};
use tablepro_core::sql_syntax::SqlGrammar;
Expand All @@ -13,32 +13,40 @@ pub(crate) struct SchemaRow {
pub sql: String,
}

/// The objects a rebuild of `table` has to recreate, in schema order.
///
/// Dropping a table drops its own indexes and triggers. A view, or a
/// trigger on another table, only names it in its SQL, and breaks when
/// the table is gone for a moment, so those are found by the name in
/// their token stream. A view that names a kept view is kept too.
// The objects a rebuild of `table` has to recreate, in schema order. Dropping a table drops its own indexes and
// triggers. A view, or a trigger on another table, only names it in its SQL and breaks while the table is gone, so those
// are found by the name in their tokens, and a view naming a kept view is kept too. Each kept name is looked up once
// among the rows that mention it, so a file's views in any order cost one pass over their words.
pub(crate) fn select_dependents(table: &str, rows: Vec<SchemaRow>) -> Vec<SchemaObjectSource> {
let mut names: Vec<String> = vec![table.to_owned()];
let mut kept = vec![false; rows.len()];
let mentions: Vec<HashSet<String>> = rows.iter().map(|row| words(&row.sql)).collect();

loop {
let mut grew = false;
for (index, row) in rows.iter().enumerate() {
if kept[index] || !keeps(row, &mentions[index], table, &names) {
let mut mentioned_by: HashMap<String, Vec<usize>> = HashMap::new();
for (index, row) in rows.iter().enumerate() {
match row.kind {
SchemaObjectKind::Index | SchemaObjectKind::Trigger if row.table.eq_ignore_ascii_case(table) => {
kept[index] = true;
}
SchemaObjectKind::Index => {}
SchemaObjectKind::Trigger | SchemaObjectKind::View => {
for word in words(&row.sql) {
mentioned_by.entry(word).or_default().push(index);
}
}
}
}
let mut pending = vec![table.to_lowercase()];
let mut named: HashSet<String> = pending.iter().cloned().collect();
while let Some(name) = pending.pop() {
for &index in mentioned_by.get(&name).map_or(&[][..], Vec::as_slice) {
if kept[index] {
continue;
}
kept[index] = true;
if row.kind == SchemaObjectKind::View {
names.push(row.name.clone());
grew = true;
let row = &rows[index];
let folded = row.name.to_lowercase();
if row.kind == SchemaObjectKind::View && named.insert(folded.clone()) {
pending.push(folded);
}
}
if !grew {
break;
}
}

rows.into_iter()
Expand All @@ -53,17 +61,6 @@ pub(crate) fn select_dependents(table: &str, rows: Vec<SchemaRow>) -> Vec<Schema
.collect()
}

fn keeps(row: &SchemaRow, mentions: &HashSet<String>, table: &str, names: &[String]) -> bool {
let owned_by_table = row.table.eq_ignore_ascii_case(table);
match row.kind {
SchemaObjectKind::Index | SchemaObjectKind::Trigger if owned_by_table => true,
SchemaObjectKind::Index => false,
SchemaObjectKind::Trigger | SchemaObjectKind::View => {
names.iter().any(|name| mentions.contains(&name.to_lowercase()))
}
}
}

/// Every identifier the SQL names, lowercased and unquoted. A name
/// inside a string literal or a comment is not a reference.
fn words(sql: &str) -> HashSet<String> {
Expand Down Expand Up @@ -196,6 +193,39 @@ mod tests {
assert!(select_dependents("orders", rows).is_empty());
}

#[test]
fn views_stored_before_the_views_they_name_are_kept() {
let mut rows: Vec<SchemaRow> = (1..2_000)
.rev()
.map(|index| {
row(
SchemaObjectKind::View,
&format!("v{index}"),
&format!("v{index}"),
&format!("CREATE VIEW v{index} AS SELECT * FROM v{}", index - 1),
)
})
.collect();
rows.push(row(
SchemaObjectKind::View,
"v0",
"v0",
"CREATE VIEW v0 AS SELECT * FROM Orders",
));
rows.push(row(
SchemaObjectKind::View,
"other",
"other",
"CREATE VIEW other AS SELECT 1",
));

let selected = select_dependents("orders", rows);

assert_eq!(selected.len(), 2_000);
assert_eq!(selected.first().map(|object| object.name.as_str()), Some("v1999"));
assert_eq!(selected.last().map(|object| object.name.as_str()), Some("v0"));
}

#[test]
fn a_quoted_name_reads_as_the_identifier_it_spells() {
assert_eq!(unquote("\"a\"\"b\""), "a\"b");
Expand Down
40 changes: 28 additions & 12 deletions core/crates/drivers/sqlite/src/catalog/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ use tablepro_core::column::{
use tablepro_core::sql_syntax::{LiteralToken, MAX_EXPRESSION_LEN, SqlGrammar, parse_expression};
use tablepro_core::value::{SqlDecimal, Value};

use crate::statement_limit::{byte_length, capped};

pub(crate) use dependents::{SchemaRow, select_dependents};
pub use runtime_version::{SQLITE_MIN_VERSION, SqliteVersion, check_runtime_version};
pub(crate) use sqlite_column_row::{SqliteColumnRow, TableFacts};
Expand Down Expand Up @@ -66,36 +68,47 @@ pub(crate) fn objects_sql(schema_sql: &str) -> String {
/// By the table too, which the sidebar names a trigger by.
pub(crate) fn trigger_sql(schema_sql: &str) -> String {
format!(
"SELECT sql FROM {schema_sql}.sqlite_schema \
WHERE type = 'trigger' AND name = ?1 AND tbl_name = ?2"
"SELECT {} FROM {schema_sql}.sqlite_schema \
WHERE type = 'trigger' AND name = ?1 AND tbl_name = ?2",
capped("sql")
)
}

pub(crate) fn object_sql(schema_sql: &str) -> String {
format!("SELECT type, sql FROM {schema_sql}.sqlite_schema WHERE name = ?1 COLLATE NOCASE")
format!(
"SELECT type, {} FROM {schema_sql}.sqlite_schema WHERE name = ?1 COLLATE NOCASE",
capped("sql")
)
}

pub(crate) fn index_sql(schema_sql: &str) -> String {
format!("SELECT sql FROM {schema_sql}.sqlite_schema WHERE type = 'index' AND name = ?1")
format!(
"SELECT {} FROM {schema_sql}.sqlite_schema WHERE type = 'index' AND name = ?1",
capped("sql")
)
}

pub(crate) fn dependents_sql(schema_sql: &str) -> String {
format!(
"SELECT type, name, tbl_name, sql FROM {schema_sql}.sqlite_schema \
WHERE type IN ('index', 'trigger', 'view') AND sql IS NOT NULL ORDER BY rowid"
"SELECT type, name, tbl_name, {} FROM {schema_sql}.sqlite_schema \
WHERE type IN ('index', 'trigger', 'view') AND sql IS NOT NULL ORDER BY rowid",
capped("sql")
)
}

// `small`: at most 64 KiB, with a key list SQLite cannot compile past ?3 bytes: no more key columns than REFERENCES
// and commas, each with three names no longer than the text and 512 bytes of opcodes.
// and commas, each with three names no longer than the text and 512 bytes of opcodes. Only a small one's text is read:
// counting a longer one would copy it whole, a stored tail with it.
fn sized_tables(schema_sql: &str) -> String {
let bytes = byte_length("sql");
format!(
"WITH tables AS (SELECT rowid, name, sql, length(CAST(sql AS BLOB)) AS bytes, \
(length(sql) - length(replace(lower(sql), 'references', ''))) / 10 \
+ length(sql) - length(replace(sql, ',', '')) AS key_columns \
"WITH tables AS (SELECT rowid, name, {bytes} AS bytes, CASE WHEN {bytes} <= 65536 THEN sql END AS sql \
FROM {schema_sql}.sqlite_schema WHERE type = 'table' AND sql IS NOT NULL), \
sized AS (SELECT rowid, name, sql, \
bytes <= 65536 AND (key_columns + 1) * (3 * bytes + 512) <= ?3 AS small FROM tables)"
CASE WHEN bytes <= 65536 THEN \
((length(sql) - length(replace(lower(sql), 'references', ''))) / 10 \
+ length(sql) - length(replace(sql, ',', '')) + 1) * (3 * bytes + 512) <= ?3 \
ELSE 0 END AS small FROM tables)"
)
}

Expand All @@ -122,7 +135,10 @@ pub(crate) fn large_tables_sql(schema_sql: &str) -> String {

// By rowid: a lookup by name scans the whole schema, once per child.
pub(crate) fn schema_row_sql(schema_sql: &str) -> String {
format!("SELECT name, sql FROM {schema_sql}.sqlite_schema WHERE rowid = ?1")
format!(
"SELECT name, {} FROM {schema_sql}.sqlite_schema WHERE rowid = ?1",
capped("sql")
)
}

/// `sqlite_stat1` exists only once ANALYZE has run, so its presence is
Expand Down
19 changes: 9 additions & 10 deletions core/crates/drivers/sqlite/src/catalog/runtime_version.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,10 @@ pub struct SqliteVersion {
pub patch: u32,
}

/// The oldest library the driver reads a catalogue from. 3.37.0 added
/// `PRAGMA table_list`, which is what says whether a table is STRICT.
/// Every system that ships GTK 4.22 has a newer one, so the floor turns
/// away no supported system.
pub const SQLITE_MIN_VERSION: SqliteVersion = SqliteVersion::new(3, 37, 0);
// The oldest library the driver reads a catalogue from. 3.43.0 added `octet_length`, which sizes a stored text without
// reading it, and 3.37.0 `PRAGMA table_list`, which says whether a table is STRICT. Every system that ships GTK 4.22
// has a newer one, so the floor turns away no supported system.
pub const SQLITE_MIN_VERSION: SqliteVersion = SqliteVersion::new(3, 43, 0);

impl SqliteVersion {
pub const fn new(major: u32, minor: u32, patch: u32) -> Self {
Expand Down Expand Up @@ -46,7 +45,7 @@ pub fn check_runtime_version(version: &SqliteVersion) -> Result<(), DriverError>
if *version < SQLITE_MIN_VERSION {
return Err(DriverError::Unsupported {
feature: UnsupportedFeature::MinimumVersion {
required: "SQLite 3.37.0",
required: "SQLite 3.43.0",
},
});
}
Expand All @@ -58,18 +57,18 @@ mod tests {
use super::*;

#[test]
fn sqlite_runtime_version_below_3_37_is_unsupported() {
let refused = check_runtime_version(&SqliteVersion::new(3, 36, 0));
fn sqlite_runtime_version_below_3_43_is_unsupported() {
let refused = check_runtime_version(&SqliteVersion::new(3, 42, 0));

assert_eq!(
refused,
Err(DriverError::Unsupported {
feature: UnsupportedFeature::MinimumVersion {
required: "SQLite 3.37.0"
required: "SQLite 3.43.0"
}
})
);
assert_eq!(check_runtime_version(&SqliteVersion::new(3, 37, 0)), Ok(()));
assert_eq!(check_runtime_version(&SqliteVersion::new(3, 43, 0)), Ok(()));
assert_eq!(check_runtime_version(&SqliteVersion::new(3, 45, 1)), Ok(()));
}

Expand Down
Loading
Loading