fix(neo4j): pick node identity function by server version

This commit is contained in:
zipg authored and GitHub committed 2026-09-28 23:01:49 +08:00
1 parent 9bb52c198f
commit 625d84bb4a
25 files changed
+461 -30

No files matched your search

+38 -6
View File
@@ -1491,7 +1491,8 @@ async fn execute_get_sample_data(
// Reuse the table-data builder so identifier quoting and row limiting follow
// the active database instead of assuming PostgreSQL syntax.
let sql = build_sample_data_sql(db_type, schema.as_deref(), table, limit);
let server_version = connection_server_version(state, connection_id).await;
let sql = build_sample_data_sql(db_type, server_version.as_deref(), schema.as_deref(), table, limit);
// Delegate to execute_execute_query with a synthetic tool call
let synthetic_call = ToolCall {
@@ -1512,9 +1513,28 @@ async fn execute_get_sample_data(
.await
}
fn build_sample_data_sql(db_type: &DatabaseType, schema: Option<&str>, table: &str, limit: usize) -> String {
/// The sample-data statement is produced by the shared dialect code, so it has to match the
/// connected server too: Neo4j below 5 only accepts `id()` where 5+ uses `elementId()`.
async fn connection_server_version(state: &Arc<AppState>, connection_id: &str) -> Option<String> {
state
.configs
.read()
.await
.get(connection_id)
.and_then(|config| config.database_info.as_ref())
.and_then(|info| info.product_version.clone())
}
fn build_sample_data_sql(
db_type: &DatabaseType,
server_version: Option<&str>,
schema: Option<&str>,
table: &str,
limit: usize,
) -> String {
build_table_data_select_sql(TableDataSelectSqlOptions {
database_type: Some(*db_type),
server_version: server_version.map(str::to_owned),
schema: schema.map(str::to_owned),
table_name: table.to_string(),
limit: Some(limit),
@@ -2640,23 +2660,35 @@ for line in sys.stdin:
#[test]
fn sample_data_sql_uses_database_identifier_and_limit_syntax() {
assert_eq!(
build_sample_data_sql(&DatabaseType::Mysql, Some("app"), "sys_tenant", 20),
build_sample_data_sql(&DatabaseType::Mysql, None, Some("app"), "sys_tenant", 20),
"SELECT * FROM `app`.`sys_tenant` LIMIT 20;"
);
assert_eq!(
build_sample_data_sql(&DatabaseType::Postgres, Some("public"), "sys_tenant", 20),
build_sample_data_sql(&DatabaseType::Postgres, None, Some("public"), "sys_tenant", 20),
"SELECT * FROM \"public\".\"sys_tenant\" LIMIT 20;"
);
assert_eq!(
build_sample_data_sql(&DatabaseType::SqlServer, Some("dbo"), "sys_tenant", 20),
build_sample_data_sql(&DatabaseType::SqlServer, None, Some("dbo"), "sys_tenant", 20),
"SELECT TOP (20) * FROM [dbo].[sys_tenant]"
);
assert_eq!(
build_sample_data_sql(&DatabaseType::Oracle, Some("APP"), "SYS_TENANT", 20),
build_sample_data_sql(&DatabaseType::Oracle, None, Some("APP"), "SYS_TENANT", 20),
"SELECT * FROM (SELECT * FROM \"APP\".\"SYS_TENANT\") WHERE ROWNUM <= 20"
);
}
#[test]
fn sample_data_sql_follows_the_connected_neo4j_identifier_function() {
assert_eq!(
build_sample_data_sql(&DatabaseType::Neo4j, Some("Neo4j/4.4.44"), None, "Person", 20),
"MATCH (n:`Person`) RETURN id(n) AS `__DBX_ELEMENT_ID`, n LIMIT 20;"
);
assert_eq!(
build_sample_data_sql(&DatabaseType::Neo4j, Some("Neo4j/5.26.0"), None, "Person", 20),
"MATCH (n:`Person`) RETURN elementId(n) AS `__DBX_ELEMENT_ID`, n LIMIT 20;"
);
}
#[test]
fn selected_schema_overrides_the_tool_schema_argument() {
let call = ToolCall {
+54 -8
View File
@@ -36,18 +36,27 @@ pub struct TableCsvExportOptions {
pub null_literal: String,
}
async fn connection_database_type(state: &AppState, connection_id: &str) -> Result<DatabaseType, String> {
/// Export pages go through the same dialect code as the grid, so the statement has to match the
/// connected server too: Neo4j below 5 only accepts `id()` where 5+ uses `elementId()`. That needs
/// the recorded version next to the connection type.
async fn connection_server_version_and_type(
state: &AppState,
connection_id: &str,
) -> Result<(Option<String>, DatabaseType), String> {
state
.configs
.read()
.await
.get(connection_id)
.map(|config| config.db_type)
.map(|config| {
let server_version = config.database_info.as_ref().and_then(|info| info.product_version.clone());
(server_version, config.db_type)
})
.ok_or_else(|| format!("Connection config not found: {connection_id}"))
}
pub async fn export_table_data_csv_core(state: &AppState, options: TableCsvExportOptions) -> Result<u64, String> {
let database_type = connection_database_type(state, &options.connection_id).await?;
let (server_version, database_type) = connection_server_version_and_type(state, &options.connection_id).await?;
// This loop pages with `LIMIT <page_size> OFFSET <n>` and stops at the first
// short page. Neither half holds for SOQL: a `/query` response carries at most
// 2000 rows and hands the rest back as a QueryLocator (`has_more` +
@@ -67,6 +76,7 @@ pub async fn export_table_data_csv_core(state: &AppState, options: TableCsvExpor
let outcome = write_table_csv_pages(
&mut writer,
database_type,
server_version.as_deref(),
&options,
client_session_id.as_deref(),
|sql, query_options| async move {
@@ -93,6 +103,7 @@ pub async fn export_table_data_csv_core(state: &AppState, options: TableCsvExpor
async fn write_table_csv_pages<Execute, QueryFuture>(
writer: &mut impl Write,
database_type: DatabaseType,
server_version: Option<&str>,
options: &TableCsvExportOptions,
client_session_id: Option<&str>,
mut execute_page: Execute,
@@ -113,6 +124,7 @@ where
loop {
let sql = build_table_data_select_sql(TableDataSelectSqlOptions {
database_type: Some(database_type),
server_version: server_version.map(str::to_owned),
schema: options.schema.clone(),
table_name: options.table_name.clone(),
table_type: None,
@@ -225,6 +237,7 @@ mod tests {
let count = write_table_csv_pages(
&mut output,
DatabaseType::Cassandra,
None,
&export_options(usize::MAX),
Some("export-test"),
|sql, options| {
@@ -266,6 +279,7 @@ mod tests {
let count = write_table_csv_pages(
&mut Vec::new(),
DatabaseType::Cassandra,
None,
&export_options(2),
Some("export-test"),
|_, _| {
@@ -289,6 +303,7 @@ mod tests {
let error = write_table_csv_pages(
&mut Vec::new(),
DatabaseType::Cassandra,
None,
&export_options(2),
Some("export-test"),
|_, _| {
@@ -309,6 +324,7 @@ mod tests {
let error = write_table_csv_pages(
&mut Vec::new(),
DatabaseType::Cassandra,
None,
&export_options(2),
Some("export-test"),
|_, options| {
@@ -331,23 +347,53 @@ mod tests {
async fn non_cassandra_csv_export_preserves_limit_offset_paging() {
let mut pages = vec![page(&[1, 2], None, false), page(&[3], None, false)].into_iter();
let mut sqls = Vec::new();
let count =
write_table_csv_pages(&mut Vec::new(), DatabaseType::Sqlite, &export_options(2), None, |sql, options| {
let count = write_table_csv_pages(
&mut Vec::new(),
DatabaseType::Sqlite,
None,
&export_options(2),
None,
|sql, options| {
sqls.push(sql);
assert_eq!(options.max_rows, Some(2));
assert!(options.page_size.is_none());
assert!(options.result_session_id.is_none());
assert!(options.client_session_id.is_none());
std::future::ready(Ok(pages.next().unwrap()))
})
.await
.unwrap();
},
)
.await
.unwrap();
assert_eq!(count, 3);
assert_eq!(sqls.len(), 2);
assert!(sqls[0].contains("LIMIT 2"));
assert!(sqls[1].contains("OFFSET 2"));
}
#[tokio::test]
async fn csv_export_uses_the_identifier_function_the_neo4j_server_understands() {
for (server_version, identifier) in [("Neo4j/4.4.44", "id(n)"), ("Neo4j/5.26.0", "elementId(n)")] {
let mut pages = vec![page(&[1], None, false)].into_iter();
let mut sqls = Vec::new();
let options = export_options(2);
write_table_csv_pages(
&mut Vec::new(),
DatabaseType::Neo4j,
Some(server_version),
&options,
None,
|sql, _| {
sqls.push(sql);
std::future::ready(Ok(pages.next().expect("unexpected extra export page")))
},
)
.await
.unwrap();
assert_eq!(sqls.len(), 1);
assert!(sqls[0].contains(identifier), "{server_version} export SQL was {}", sqls[0]);
}
}
fn salesforce_config(id: &str) -> ConnectionConfig {
let mut config = serde_json::from_value::<ConnectionConfig>(serde_json::json!({
"id": id,