Skip to content

Commit 9dbc3a8

Browse files
committed
feat(native): reject cursor intrinsics outside models natively behind the preview engine
1 parent 686d0d3 commit 9dbc3a8

20 files changed

Lines changed: 337 additions & 1 deletion

File tree

‎Cargo.lock‎

Lines changed: 3 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎crates/sqlbuild-attachments/Cargo.toml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,3 +9,6 @@ rust-version.workspace = true
99

1010
[lints]
1111
workspace = true
12+
13+
[dependencies]
14+
sqlbuild-sqltext.workspace = true
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub(crate) mod scan;
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
//! Python's cursor intrinsic scan over quotes, comments and identifier boundaries.
2+
3+
use sqlbuild_sqltext::sql_scan::main::comment_end::comment_end;
4+
use sqlbuild_sqltext::sql_scan::main::non_code_end::non_code_end;
5+
use sqlbuild_sqltext::sql_scan::models::QuotePolicy;
6+
7+
const INTRINSIC_NAMES: [&[u8]; 2] = [b"__cursor_start", b"__cursor_end"];
8+
/// Only dollar quotes: Python's own quote scan does not double backticks, so quotes run here.
9+
const DOLLAR_POLICY: QuotePolicy = QuotePolicy {
10+
backtick_identifiers: false,
11+
single_quote_backslash_escapes: false,
12+
double_quote_backslash_escapes: false,
13+
dollar_quotes: true,
14+
};
15+
16+
/// The next scan offset after `index`, or `Err` where Python finds an intrinsic or raises.
17+
pub(crate) fn step(bytes: &[u8], index: usize) -> Result<usize, ()> {
18+
if let Some(end) = comment_end(bytes, index).map_err(|_| ())? {
19+
return Ok(end);
20+
}
21+
match bytes[index] {
22+
quote @ (b'\'' | b'"' | b'`') => return quoted_end(bytes, index, quote),
23+
b'$' => {
24+
return Ok(non_code_end(bytes, index, DOLLAR_POLICY)
25+
.map_err(|_| ())?
26+
.unwrap_or(index + 1));
27+
}
28+
_ => {}
29+
}
30+
for name in INTRINSIC_NAMES {
31+
if bytes[index..].starts_with(name) {
32+
let end: usize = index + name.len();
33+
let before: Option<u8> = index.checked_sub(1).map(|previous| bytes[previous]);
34+
if !continues_identifier(before)? && !continues_identifier(bytes.get(end).copied())? {
35+
return Err(());
36+
}
37+
return Ok(end);
38+
}
39+
}
40+
Ok(index + 1)
41+
}
42+
43+
/// Python's quoted text end: `'` and `"` double to escape, backticks do not.
44+
fn quoted_end(bytes: &[u8], start: usize, quote: u8) -> Result<usize, ()> {
45+
let mut index: usize = start + 1;
46+
loop {
47+
index += bytes[index..]
48+
.iter()
49+
.position(|byte| *byte == quote)
50+
.ok_or(())?;
51+
if quote != b'`' && bytes.get(index + 1) == Some(&quote) {
52+
index += 2;
53+
continue;
54+
}
55+
return Ok(index + 1);
56+
}
57+
}
58+
59+
/// Python's `isalnum() or _`; non-ASCII neighbours defer to Python's Unicode rules.
60+
fn continues_identifier(byte: Option<u8>) -> Result<bool, ()> {
61+
match byte {
62+
None => Ok(false),
63+
Some(byte) if !byte.is_ascii() => Err(()),
64+
Some(byte) => Ok(byte.is_ascii_alphanumeric() || byte == b'_'),
65+
}
66+
}
67+
68+
/// Python's `any(name in sql for name in _INTRINSIC_NAMES)`.
69+
pub(crate) fn contains_intrinsic_name(bytes: &[u8]) -> bool {
70+
for name in INTRINSIC_NAMES {
71+
if bytes.windows(name.len()).any(|window| window == name) {
72+
return true;
73+
}
74+
}
75+
false
76+
}
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
//! Python's `reject_cursor_intrinsics` for SQL that it accepts.
2+
3+
use crate::cursor_intrinsics::_helpers::scan::{contains_intrinsic_name, step};
4+
use crate::cursor_intrinsics::models::IntrinsicCheck;
5+
6+
/// `Free` where Python accepts `sql`; anything Python could reject or judge by Unicode defers.
7+
#[must_use]
8+
pub fn intrinsic_free(sql: &str, reserved_markers: &[String]) -> IntrinsicCheck {
9+
if reserved_markers
10+
.iter()
11+
.any(|marker| sql.contains(marker.as_str()))
12+
{
13+
return IntrinsicCheck::Deferred;
14+
}
15+
let bytes: &[u8] = sql.as_bytes();
16+
if !contains_intrinsic_name(bytes) {
17+
return IntrinsicCheck::Free;
18+
}
19+
let mut index: usize = 0;
20+
while index < bytes.len() {
21+
let Ok(next) = step(bytes, index) else {
22+
return IntrinsicCheck::Deferred;
23+
};
24+
index = next;
25+
}
26+
IntrinsicCheck::Free
27+
}
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
pub mod intrinsic_free;
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pub(crate) mod _helpers;
2+
pub mod main;
3+
pub mod models;
4+
5+
#[cfg(test)]
6+
mod tests;
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
//! Outcome of the native cursor intrinsic check.
2+
3+
/// Whether SQL is free of cursor intrinsics, or Python must decide (and usually raise).
4+
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
5+
pub enum IntrinsicCheck {
6+
Free,
7+
Deferred,
8+
}
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
#[path = "tests/test_intrinsic_free.rs"]
2+
mod intrinsic_free;
3+
#[path = "tests/test_types.rs"]
4+
mod test_types;
Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
use crate::cursor_intrinsics::main::intrinsic_free::intrinsic_free;
2+
use crate::cursor_intrinsics::models::IntrinsicCheck;
3+
use crate::cursor_intrinsics::tests::test_types::IntrinsicCheckTestCase;
4+
5+
#[test]
6+
fn given_sql_when_checking_cursor_intrinsics_then_python_acceptance_is_returned() {
7+
let test_cases = [
8+
IntrinsicCheckTestCase {
9+
description: "SQL without intrinsic names is free",
10+
sql: "SELECT 'unclosed",
11+
expected_check: IntrinsicCheck::Free,
12+
},
13+
IntrinsicCheckTestCase {
14+
description: "names inside quotes, comments, dollar quotes and identifiers are free",
15+
sql: "SELECT '__cursor_start()', `a``__cursor_end`, x__cursor_end -- __cursor_end\n\
16+
/* __cursor_start */ $tag$__cursor_end()$tag$, __cursor_start_at",
17+
expected_check: IntrinsicCheck::Free,
18+
},
19+
IntrinsicCheckTestCase {
20+
description: "an intrinsic call defers so Python raises",
21+
sql: "WHERE ts >= __cursor_start()",
22+
expected_check: IntrinsicCheck::Deferred,
23+
},
24+
IntrinsicCheckTestCase {
25+
description: "an unclosed quote after an intrinsic name defers",
26+
sql: "SELECT x__cursor_start, 'open",
27+
expected_check: IntrinsicCheck::Deferred,
28+
},
29+
IntrinsicCheckTestCase {
30+
description: "a non-ASCII neighbour defers to Python's Unicode identifier rules",
31+
sql: "SELECT é__cursor_end",
32+
expected_check: IntrinsicCheck::Deferred,
33+
},
34+
IntrinsicCheckTestCase {
35+
description: "a reserved marker defers",
36+
sql: "SELECT __reserved_marker__",
37+
expected_check: IntrinsicCheck::Deferred,
38+
},
39+
];
40+
41+
for test_case in test_cases {
42+
assert_eq!(
43+
intrinsic_free(test_case.sql, &["__reserved_marker__".to_owned()]),
44+
test_case.expected_check,
45+
"{}",
46+
test_case.description
47+
);
48+
}
49+
}

0 commit comments

Comments
 (0)