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

@@ -873,6 +873,38 @@ pub(super) fn build_db2_table_select_page_sql(
)
}
/// Neo4j 5.0 replaced `id()` with `elementId()`, and servers before that only know `id()`, so the
/// generated Cypher has to pick the spelling the connected server accepts. An unknown version keeps
/// `elementId()`, which is what every caller without connection metadata got before.
pub fn neo4j_element_id_function(server_version: Option<&str>) -> &'static str {
match neo4j_major_version(server_version) {
Some(major) if major < NEO4J_ELEMENT_ID_MIN_MAJOR_VERSION => NEO4J_LEGACY_ELEMENT_ID_FUNCTION,
_ => "elementId",
}
}
/// The identity function Neo4j used before 5.0. Unlike `elementId()`, which returns a string, it
/// returns the node's internal `Integer` id, so callers comparing a value read from a grid have to
/// compare numbers.
pub const NEO4J_LEGACY_ELEMENT_ID_FUNCTION: &str = "id";
/// Extracts the leading `major` from version strings such as `4.4.44`, `Neo4j/5.26.0` or
/// `Neo4j/2025.01.0`. The product name itself contains a digit ("Neo4j"), so only a token that
/// *starts* with digits counts as the version. Anything without one is treated as unknown.
fn neo4j_major_version(server_version: Option<&str>) -> Option<u32> {
let version = server_version?.trim();
version.split(|character: char| !character.is_ascii_alphanumeric()).find_map(|token| {
let digits: String = token.chars().take_while(char::is_ascii_digit).collect();
if digits.is_empty() {
None
} else {
digits.parse().ok()
}
})
}
const NEO4J_ELEMENT_ID_MIN_MAJOR_VERSION: u32 = 5;
pub(super) fn build_neo4j_table_select_sql(options: &TableDataSelectSqlOptions, limit: usize) -> String {
let label = quote_table_identifier(Some(DatabaseType::Neo4j), &options.table_name);
let predicate = normalize_where_input(options.where_input.as_deref());
@@ -891,7 +923,8 @@ pub(super) fn build_neo4j_table_select_sql(options: &TableDataSelectSqlOptions,
.join(", ")
};
let returns = format!(
"elementId(n) AS {}, {returned_columns}",
"{}(n) AS {}, {returned_columns}",
neo4j_element_id_function(options.server_version.as_deref()),
quote_table_identifier(Some(DatabaseType::Neo4j), DBX_NEO4J_ELEMENT_ID_COLUMN)
);
let order_by = options.order_by.as_deref().filter(|order| !order.trim().is_empty());
@@ -979,6 +1012,7 @@ mod tests {
database_type: Some(database_type),
driver_profile: None,
identifier_quote: None,
server_version: None,
schema: None,
table_name: table.to_string(),
catalog: catalog.map(|c| c.to_string()),
@@ -1575,6 +1575,37 @@ fn builds_oracle_and_neo4j_table_data_queries() {
);
}
#[test]
fn neo4j_element_id_function_follows_the_connected_server_version() {
// Neo4j 4.x and older only know `id()`; `elementId()` arrived in 5.0.
assert_eq!(neo4j_element_id_function(Some("4.4.44")), "id");
assert_eq!(neo4j_element_id_function(Some("Neo4j/4.4.44")), "id");
assert_eq!(neo4j_element_id_function(Some("Neo4j 4.4.44 (community)")), "id");
assert_eq!(neo4j_element_id_function(Some("3.5.35")), "id");
assert_eq!(neo4j_element_id_function(Some("Neo4j/5.26.0")), "elementId");
assert_eq!(neo4j_element_id_function(Some("Neo4j/2025.01.0")), "elementId");
// Unknown or unparsable versions keep the current spelling.
assert_eq!(neo4j_element_id_function(None), "elementId");
assert_eq!(neo4j_element_id_function(Some("")), "elementId");
assert_eq!(neo4j_element_id_function(Some("Neo4j Community")), "elementId");
}
#[test]
fn builds_neo4j_table_select_with_the_legacy_element_id_on_neo4j_4() {
assert_eq!(
build_table_data_select_sql(TableDataSelectSqlOptions {
database_type: Some(DatabaseType::Neo4j),
table_name: "Employee".to_string(),
primary_keys: vec!["id".to_string()],
columns: vec!["id".to_string(), "role".to_string()],
server_version: Some("Neo4j/4.4.44".to_string()),
limit: Some(100),
..Default::default()
}),
"MATCH (n:`Employee`) RETURN id(n) AS `__DBX_ELEMENT_ID`, n.`id` AS `id`, n.`role` AS `role` LIMIT 100;"
);
}
#[test]
fn oracle_unknown_table_type_does_not_assume_rowid_support() {
assert_eq!(
@@ -25,6 +25,11 @@ pub struct TableDataSelectSqlOptions {
pub driver_profile: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub identifier_quote: Option<String>,
/// Server version reported by the connection (for example `Neo4j/4.4.44`).
/// Engines that renamed a built-in function across releases use it to emit
/// SQL the connected server understands instead of the newest spelling.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub server_version: Option<String>,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub schema: Option<String>,
pub table_name: String,