feat(sqlserver): add execution plan support
This commit is contained in:
16 files changed
+636
-23
No files matched your search
@@ -261,7 +261,7 @@
|
||||
"sqlFileExecution": true,
|
||||
"databaseCreate": true,
|
||||
"fieldLineage": true,
|
||||
"sqlExplain": false,
|
||||
"sqlExplain": true,
|
||||
"userAdmin": false,
|
||||
"driverManagement": false
|
||||
}
|
||||
|
||||
@@ -697,6 +697,9 @@ pub async fn stream_first_result_set(
|
||||
}
|
||||
|
||||
fn sqlserver_cell_to_json(cell: &ColumnData<'static>) -> serde_json::Value {
|
||||
if let Ok(Some(v)) = <&tiberius::xml::XmlData as FromSql>::from_sql(cell) {
|
||||
return serde_json::Value::String(v.as_ref().to_string());
|
||||
}
|
||||
if let Ok(Some(v)) = <&str as FromSql>::from_sql(cell) {
|
||||
return serde_json::Value::String(v.to_string());
|
||||
}
|
||||
@@ -1733,6 +1736,20 @@ pub async fn execute_batch_with_max_rows(
|
||||
);
|
||||
}
|
||||
}
|
||||
execute_simple_batch_with_max_rows(client, sql, max_rows).await
|
||||
}
|
||||
|
||||
/// Execute a SQL Server batch directly through TDS simple-query mode.
|
||||
///
|
||||
/// This intentionally bypasses result-set type probing and SQL rewriting. It is
|
||||
/// required while `SHOWPLAN_XML` is enabled because any probe issued on the same
|
||||
/// session is itself affected by SHOWPLAN state.
|
||||
pub async fn execute_simple_batch_with_max_rows(
|
||||
client: &mut SqlServerClient,
|
||||
sql: &str,
|
||||
max_rows: Option<usize>,
|
||||
) -> Result<Vec<QueryResult>, String> {
|
||||
let start = Instant::now();
|
||||
let stream = sqlserver_driver_result(client.simple_query(sql)).await?;
|
||||
let mut results = sqlserver_driver_result(collect_result_sets_limited(stream, start, max_rows)).await?;
|
||||
for result in &mut results {
|
||||
@@ -1829,6 +1846,9 @@ fn contains_transaction_control(sql: &str) -> bool {
|
||||
|
||||
fn requires_simple_query_batch(sql: &str) -> bool {
|
||||
let tokens = first_sql_tokens(sql, 4);
|
||||
if tokens.len() >= 2 && tokens[0].eq_ignore_ascii_case("SET") && tokens[1].eq_ignore_ascii_case("SHOWPLAN_XML") {
|
||||
return true;
|
||||
}
|
||||
if tokens.len() >= 2 && tokens[0].eq_ignore_ascii_case("CREATE") && tokens[1].eq_ignore_ascii_case("SCHEMA") {
|
||||
return true;
|
||||
}
|
||||
@@ -1904,7 +1924,7 @@ mod tests {
|
||||
CompletionAssistantMatchMode, CompletionAssistantObjectKind, CompletionAssistantRequest, QueryResult,
|
||||
};
|
||||
use chrono::NaiveDate;
|
||||
use std::time::Instant;
|
||||
use std::{borrow::Cow, time::Instant};
|
||||
use tiberius::{ColumnData, IntoSql};
|
||||
|
||||
#[test]
|
||||
@@ -1919,6 +1939,18 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_xml_cells_are_returned_as_strings() {
|
||||
let cell = ColumnData::Xml(Some(Cow::Owned(tiberius::xml::XmlData::new(
|
||||
"<ShowPlanXML><RelOp NodeId=\"0\" /></ShowPlanXML>",
|
||||
))));
|
||||
|
||||
assert_eq!(
|
||||
sqlserver_cell_to_json(&cell),
|
||||
serde_json::Value::String("<ShowPlanXML><RelOp NodeId=\"0\" /></ShowPlanXML>".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_endpoint_keeps_regular_hosts() {
|
||||
assert_eq!(
|
||||
@@ -1982,6 +2014,8 @@ mod tests {
|
||||
|
||||
#[test]
|
||||
fn sqlserver_module_definitions_require_simple_query_batch() {
|
||||
assert!(requires_simple_query_batch("SET SHOWPLAN_XML ON;"));
|
||||
assert!(requires_simple_query_batch("SET SHOWPLAN_XML OFF;"));
|
||||
assert!(requires_simple_query_batch("CREATE SCHEMA [analytics];"));
|
||||
assert!(requires_simple_query_batch("CREATE FUNCTION dbo.fn_demo() RETURNS INT AS BEGIN RETURN 1; END;"));
|
||||
assert!(requires_simple_query_batch("ALTER PROCEDURE dbo.usp_demo AS SELECT 1;"));
|
||||
@@ -2055,6 +2089,17 @@ mod tests {
|
||||
assert!(!execute_batch.contains("into_results"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_explicit_simple_batch_bypasses_result_type_probing() {
|
||||
let source = include_str!("sqlserver.rs");
|
||||
let simple_batch = source.split("pub async fn execute_simple_batch_with_max_rows").nth(1).unwrap();
|
||||
let simple_batch = simple_batch.split("fn strip_dbx_sqlserver_row_number_column").next().unwrap();
|
||||
|
||||
assert!(simple_batch.contains("client.simple_query(sql)"));
|
||||
assert!(!simple_batch.contains("sqlserver_unsafe_type_query"));
|
||||
assert!(!simple_batch.contains("describe_sqlserver_result_set"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn sqlserver_index_metadata_sql_avoids_string_agg_for_older_compatibility_levels() {
|
||||
let sql = sqlserver_indexes_sql("dbo", "DF_Rule");
|
||||
|
||||
@@ -4,7 +4,7 @@ use chrono::{DateTime, Duration as ChronoDuration, NaiveDate, NaiveDateTime, Nai
|
||||
use duckdb::types::{TimeUnit, Value, ValueRef};
|
||||
use futures::StreamExt;
|
||||
use mysql_async::prelude::Queryable;
|
||||
use serde::Serialize;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use sqlparser::ast::{
|
||||
visit_relations_mut, Ident, ObjectName, ObjectNamePart, ObjectType, Statement, TableFactor, VisitMut, VisitorMut,
|
||||
};
|
||||
@@ -367,6 +367,14 @@ fn qualifies_unqualified_agent_relations(db_type: Option<DatabaseType>) -> bool
|
||||
matches!(db_type, Some(DatabaseType::Iris | DatabaseType::Kingbase))
|
||||
}
|
||||
|
||||
#[derive(Clone, Copy, Debug, Default, Deserialize, PartialEq, Eq)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum QueryExecutionMode {
|
||||
#[default]
|
||||
Standard,
|
||||
Simple,
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug, Default)]
|
||||
pub struct QueryExecutionOptions {
|
||||
pub max_rows: Option<usize>,
|
||||
@@ -385,6 +393,9 @@ pub struct QueryExecutionOptions {
|
||||
/// When `true`, multi-statement execution continues after a statement error instead
|
||||
/// of stopping at the first failure. Connection-level failures always stop the batch.
|
||||
pub continue_on_error: bool,
|
||||
/// Explicit low-level execution path. `Simple` is currently used by SQL Server
|
||||
/// SHOWPLAN so the source SQL bypasses result-set probing and query rewriting.
|
||||
pub execution_mode: QueryExecutionMode,
|
||||
}
|
||||
|
||||
fn query_result_row_limit(max_rows: Option<usize>) -> usize {
|
||||
@@ -1405,6 +1416,7 @@ pub async fn do_execute(
|
||||
PoolKind::SqlServer(client) => {
|
||||
let client = client.clone();
|
||||
let max_rows = options.max_rows;
|
||||
let execution_mode = options.execution_mode;
|
||||
drop(connections);
|
||||
let mut client = match cancel_token.as_ref() {
|
||||
Some(token) => tokio::select! {
|
||||
@@ -1414,13 +1426,18 @@ pub async fn do_execute(
|
||||
},
|
||||
None => client.lock().await,
|
||||
};
|
||||
let result = wait_for_query_opt(
|
||||
cancel_token,
|
||||
query_timeout,
|
||||
db::sqlserver::execute_query_with_max_rows(&mut client, sql, max_rows),
|
||||
)
|
||||
.await
|
||||
.map(|result| truncate_result_with_max_rows(result, max_rows));
|
||||
let execution = async {
|
||||
if execution_mode == QueryExecutionMode::Simple {
|
||||
let mut results =
|
||||
db::sqlserver::execute_simple_batch_with_max_rows(&mut client, sql, max_rows).await?;
|
||||
Ok(results.remove(0))
|
||||
} else {
|
||||
db::sqlserver::execute_query_with_max_rows(&mut client, sql, max_rows).await
|
||||
}
|
||||
};
|
||||
let result = wait_for_query_opt(cancel_token, query_timeout, execution)
|
||||
.await
|
||||
.map(|result| truncate_result_with_max_rows(result, max_rows));
|
||||
drop(client);
|
||||
if matches!(result.as_ref(), Err(err) if should_discard_pool_after_error(pool_db_type, err)) {
|
||||
state.remove_pool_by_key(pool_key).await;
|
||||
@@ -2186,6 +2203,7 @@ async fn execute_multi_sqlserver(
|
||||
let mut all_results = Vec::new();
|
||||
let max_rows = options.max_rows;
|
||||
let query_timeout = resolve_query_timeout(options.timeout_secs);
|
||||
let execution_mode = options.execution_mode;
|
||||
|
||||
for batch in &batches {
|
||||
if is_canceled(&cancel_token) {
|
||||
@@ -2226,12 +2244,14 @@ async fn execute_multi_sqlserver(
|
||||
break;
|
||||
}
|
||||
|
||||
let result = wait_for_result_opt(
|
||||
cancel_token.clone(),
|
||||
query_timeout,
|
||||
db::sqlserver::execute_batch_with_max_rows(&mut client_guard, batch, max_rows),
|
||||
)
|
||||
.await;
|
||||
let execution = async {
|
||||
if execution_mode == QueryExecutionMode::Simple {
|
||||
db::sqlserver::execute_simple_batch_with_max_rows(&mut client_guard, batch, max_rows).await
|
||||
} else {
|
||||
db::sqlserver::execute_batch_with_max_rows(&mut client_guard, batch, max_rows).await
|
||||
}
|
||||
};
|
||||
let result = wait_for_result_opt(cancel_token.clone(), query_timeout, execution).await;
|
||||
drop(client_guard);
|
||||
|
||||
match result {
|
||||
@@ -3235,6 +3255,14 @@ mod tests {
|
||||
};
|
||||
use crate::storage::Storage;
|
||||
|
||||
#[test]
|
||||
fn query_execution_mode_deserializes_simple_client_value() {
|
||||
let mode: QueryExecutionMode = serde_json::from_str("\"simple\"").unwrap();
|
||||
|
||||
assert_eq!(mode, QueryExecutionMode::Simple);
|
||||
assert_eq!(QueryExecutionMode::default(), QueryExecutionMode::Standard);
|
||||
}
|
||||
|
||||
fn test_connection_config(db_type: DatabaseType) -> ConnectionConfig {
|
||||
ConnectionConfig {
|
||||
id: "conn-1".to_string(),
|
||||
|
||||
@@ -53,6 +53,9 @@ pub fn build_explain_sql(options: ExplainSqlOptions) -> ExplainSqlBuildResult {
|
||||
if !is_safe_explain_sql(&source) {
|
||||
return explain_err("unsafe");
|
||||
}
|
||||
if options.database_type == Some(DatabaseType::SqlServer) && crate::sql::split_sql_batches(&source).len() != 1 {
|
||||
return explain_err("unsafe");
|
||||
}
|
||||
|
||||
let sql = match options.database_type {
|
||||
Some(DatabaseType::Postgres | DatabaseType::MongoDb) => {
|
||||
@@ -62,6 +65,9 @@ pub fn build_explain_sql(options: ExplainSqlOptions) -> ExplainSqlBuildResult {
|
||||
format!("EXPLAIN {source}")
|
||||
}
|
||||
Some(DatabaseType::Oracle) => format!("EXPLAIN PLAN FOR {source}"),
|
||||
Some(DatabaseType::SqlServer) => {
|
||||
format!("SET SHOWPLAN_XML ON;\nGO\n{source}\nGO\nSET SHOWPLAN_XML OFF;")
|
||||
}
|
||||
Some(DatabaseType::Mysql) if options.format == Some(ExplainFormat::Standard) => {
|
||||
// MySQL 8.0.32+ may otherwise inherit TREE or JSON from the session-level explain_format.
|
||||
format!("EXPLAIN FORMAT=TRADITIONAL {source}")
|
||||
@@ -99,6 +105,7 @@ pub fn supports_explain_plan(database_type: Option<DatabaseType>) -> bool {
|
||||
| DatabaseType::Questdb
|
||||
| DatabaseType::Dameng
|
||||
| DatabaseType::Oracle
|
||||
| DatabaseType::SqlServer
|
||||
)
|
||||
)
|
||||
}
|
||||
@@ -202,6 +209,9 @@ pub fn is_write_sql_for_database(sql: &str, database_type: DatabaseType) -> bool
|
||||
}
|
||||
|
||||
fn is_write_sql_with_database_type(sql: &str, database_type: Option<DatabaseType>) -> bool {
|
||||
if database_type == Some(DatabaseType::SqlServer) && is_sqlserver_showplan_xml_set(sql) {
|
||||
return false;
|
||||
}
|
||||
if database_type.is_some_and(|database_type| has_dialect_specific_write(sql, database_type)) {
|
||||
return true;
|
||||
}
|
||||
@@ -220,6 +230,14 @@ fn is_write_sql_with_database_type(sql: &str, database_type: Option<DatabaseType
|
||||
.any(|statement| is_write_sql_statement(statement, detect_mysql_executable_comments, detect_select_into))
|
||||
}
|
||||
|
||||
fn is_sqlserver_showplan_xml_set(sql: &str) -> bool {
|
||||
let normalized = strip_sql_comments(sql)
|
||||
.split_whitespace()
|
||||
.map(|part| part.trim_end_matches(';').to_ascii_uppercase())
|
||||
.collect::<Vec<_>>();
|
||||
matches!(normalized.as_slice(), [set, showplan, value] if set == "SET" && showplan == "SHOWPLAN_XML" && matches!(value.as_str(), "ON" | "OFF"))
|
||||
}
|
||||
|
||||
fn is_mysql_compatible_database(database_type: DatabaseType) -> bool {
|
||||
matches!(
|
||||
database_type,
|
||||
@@ -716,6 +734,44 @@ mod tests {
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn builds_sqlserver_showplan_xml_batches() {
|
||||
let result = build_explain_sql(ExplainSqlOptions {
|
||||
database_type: Some(DatabaseType::SqlServer),
|
||||
format: None,
|
||||
sql: "WITH rows AS (SELECT 1 AS id) SELECT * FROM rows;".to_string(),
|
||||
});
|
||||
|
||||
assert_eq!(
|
||||
result,
|
||||
ExplainSqlBuildResult {
|
||||
ok: true,
|
||||
sql: Some(
|
||||
"SET SHOWPLAN_XML ON;\nGO\nWITH rows AS (SELECT 1 AS id) SELECT * FROM rows\nGO\nSET SHOWPLAN_XML OFF;"
|
||||
.to_string()
|
||||
),
|
||||
reason: None,
|
||||
}
|
||||
);
|
||||
|
||||
assert_eq!(
|
||||
build_explain_sql(ExplainSqlOptions {
|
||||
database_type: Some(DatabaseType::SqlServer),
|
||||
format: None,
|
||||
sql: "SELECT 1\nGO\nSELECT 2".to_string(),
|
||||
}),
|
||||
ExplainSqlBuildResult { ok: false, sql: None, reason: Some("unsafe".to_string()) }
|
||||
);
|
||||
assert!(
|
||||
build_explain_sql(ExplainSqlOptions {
|
||||
database_type: Some(DatabaseType::SqlServer),
|
||||
format: None,
|
||||
sql: "SELECT 'first line\nGO\nlast line' AS text".to_string(),
|
||||
})
|
||||
.ok
|
||||
);
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn validates_dameng_autotrace_sql_safety() {
|
||||
assert!(is_safe_dameng_autotrace_sql("SELECT * FROM t WHERE name = 'delete';"));
|
||||
@@ -1144,6 +1200,16 @@ mod tests {
|
||||
assert!(show_create_err.unwrap_err().contains("Write operation"));
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_read_only_allows_only_sqlserver_showplan_xml_session_switches() {
|
||||
for sql in ["SET SHOWPLAN_XML ON;", "-- explain\nSET SHOWPLAN_XML OFF"] {
|
||||
assert_eq!(check_read_only(sql, "readonly", DatabaseType::SqlServer), Ok(()));
|
||||
}
|
||||
assert!(check_read_only("SET SHOWPLAN_ALL ON", "readonly", DatabaseType::SqlServer).is_err());
|
||||
assert!(check_read_only("SET SHOWPLAN_XML ON; SELECT 1", "readonly", DatabaseType::SqlServer).is_err());
|
||||
assert!(check_read_only("SET SHOWPLAN_XML OFF; DROP TABLE users", "readonly", DatabaseType::SqlServer).is_err());
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn check_read_only_only_treats_executable_comments_as_writes_for_mysql_compatible_connections() {
|
||||
let mysql_executable_comment = "SELECT 3156 /*!50000 INTO OUTFILE '/var/lib/mysql-files/dbx_ro_probe.txt' */";
|
||||
|
||||
Reference in new issue
Block a user