feat(spatial): add SRID-aware geometry previews

This commit is contained in:
amwps290 authored and GitHub committed 2026-07-31 22:31:51 +08:00
1 parent ee26b2a580
commit a6a072294a
57 files changed
+2771 -384

No files matched your search

+2
View File
@@ -4427,6 +4427,8 @@ mod tests {
columns: vec!["datcompatibility".to_string()],
column_types: vec!["text".to_string()],
column_sortables: vec![true],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![vec![serde_json::json!("M")]],
affected_rows: 0,
execution_time_ms: 1,
+1 -1
View File
@@ -880,7 +880,7 @@ fn is_export_insert_column(
) -> bool {
!is_internal_export_column(database_type, column)
&& !is_postgres_tsvector_export_column(database_type, column_type)
&& !(database_type == Some(DatabaseType::Mysql) && is_mysql_generated_column_extra(column_extra))
&& (database_type != Some(DatabaseType::Mysql) || !is_mysql_generated_column_extra(column_extra))
}
fn is_postgres_json_export_column(database_type: Option<DatabaseType>, column_type: Option<&str>) -> bool {
@@ -487,6 +487,8 @@ fn limited_query_result(result: ChJsonResult, execution_time_ms: u128, max_rows:
columns,
column_types,
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows,
affected_rows: 0,
execution_time_ms,
@@ -656,6 +658,8 @@ pub async fn execute_query_with_max_rows(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: 0,
execution_time_ms: start.elapsed().as_millis(),
@@ -349,6 +349,8 @@ fn query_result(
columns: result.columns,
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: result.rows,
affected_rows,
execution_time_ms,
@@ -1293,6 +1293,8 @@ fn parse_elasticsearch_response_with_sql_parser(
columns,
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows,
affected_rows: row_count,
execution_time_ms: start.elapsed().as_millis(),
@@ -1316,6 +1318,8 @@ fn parse_elasticsearch_response_with_sql_parser(
columns,
column_types,
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows,
affected_rows: row_count,
execution_time_ms: start.elapsed().as_millis(),
@@ -1485,6 +1489,8 @@ fn raw_json_response_result(
columns: vec!["status".to_string(), "response".to_string()],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![vec![serde_json::Value::Number(status.into()), serde_json::Value::String(body_text.into())]],
affected_rows: 0,
execution_time_ms: start.elapsed().as_millis(),
@@ -1549,6 +1555,8 @@ fn parse_elasticsearch_rest_response_with_sql_parser(
columns: vec!["response".to_string()],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows,
affected_rows,
execution_time_ms: start.elapsed().as_millis(),
@@ -1980,6 +1988,8 @@ pub(crate) fn parse_tabular_sql_response(
columns: column_names,
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: result_rows,
affected_rows: total_key
.and_then(|key| body.get(key))
@@ -476,6 +476,8 @@ pub async fn execute_query(client: &InfluxdbClient, database: &str, sql: &str) -
columns: s.columns.clone(),
column_types: vec![],
column_sortables: s.columns.iter().map(|_| false).collect(),
spatial_columns: vec![],
spatial_values: vec![],
rows: s.values.clone(),
affected_rows: s.values.len() as u64,
execution_time_ms: start.elapsed().as_millis(),
@@ -488,6 +490,8 @@ pub async fn execute_query(client: &InfluxdbClient, database: &str, sql: &str) -
columns: vec![],
column_types: vec![],
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: 0,
execution_time_ms: start.elapsed().as_millis(),
@@ -630,6 +634,8 @@ fn parse_flux_csv(text: &str, start: Instant) -> Result<QueryResult, String> {
let columns = headers.unwrap_or_default();
Ok(QueryResult {
column_sortables: columns.iter().map(|_| false).collect(),
spatial_columns: vec![],
spatial_values: vec![],
columns,
column_types: vec![],
affected_rows: rows.len() as u64,
+125 -22
View File
@@ -18,7 +18,7 @@ use crate::sql::{starts_with_executable_sql_keyword, starts_with_executable_sql_
use crate::types::{
ColumnInfo, CompletionAssistantCandidate, CompletionAssistantCandidateKind, CompletionAssistantMatchMode,
CompletionAssistantObjectKind, CompletionAssistantRequest, CompletionAssistantResponse, DatabaseInfo,
ForeignKeyInfo, IndexInfo, ObjectInfo, ObjectStatistics, QueryResult, TableInfo, TriggerInfo,
ForeignKeyInfo, IndexInfo, ObjectInfo, ObjectStatistics, QueryResult, SpatialColumnBuilder, TableInfo, TriggerInfo,
};
use super::file_validator::validate_file_path;
@@ -456,10 +456,8 @@ pub(crate) fn mysql_value_to_json(row: &mysql_async::Row, idx: usize) -> serde_j
return row_get::<Vec<u8>, _>(row, idx)
.map(|bytes| {
if matches!(column.column_type(), ColumnType::MYSQL_TYPE_GEOMETRY) {
// MySQL prefixes geometry WKB with a 4-byte SRID.
// Strip it before passing to the WKB parser.
let wkb = if bytes.len() >= 4 { &bytes[4..] } else { &bytes };
super::wkb::wkb_to_wkt(wkb)
decode_mysql_geometry(&bytes)
.map(|geometry| geometry.wkt)
.map(serde_json::Value::String)
.unwrap_or_else(|| super::binary_value_to_json(&bytes))
} else {
@@ -504,6 +502,63 @@ pub(crate) fn mysql_value_to_json(row: &mysql_async::Row, idx: usize) -> serde_j
.unwrap_or(serde_json::Value::Null)
}
fn decode_mysql_geometry(bytes: &[u8]) -> Option<super::wkb::DecodedGeometry> {
if bytes.len() >= 5 && matches!(bytes[4], 0 | 1) {
let prefix: [u8; 4] = bytes[..4].try_into().ok()?;
if let Some(mut geometry) = super::wkb::decode_wkb_geometry(&bytes[4..]) {
if geometry.srid.is_none() {
let srid = u32::from_le_bytes(prefix);
geometry.srid = (srid != 0).then_some(srid);
}
return Some(geometry);
}
}
super::wkb::decode_wkb_geometry(bytes)
}
fn mysql_spatial_column_builder(columns: &[mysql_async::Column]) -> SpatialColumnBuilder {
SpatialColumnBuilder::new(
columns
.iter()
.enumerate()
.filter_map(|(index, column)| (column.column_type() == ColumnType::MYSQL_TYPE_GEOMETRY).then_some(index)),
)
}
fn mysql_row_to_json_with_srids(
row: &mysql_async::Row,
spatial_columns: &mut SpatialColumnBuilder,
) -> (Vec<serde_json::Value>, Vec<Option<u32>>) {
let mut srids = vec![None; row.len()];
let values = (0..row.len())
.map(|idx| {
let is_geometry = row
.columns_ref()
.get(idx)
.is_some_and(|column| column.column_type() == ColumnType::MYSQL_TYPE_GEOMETRY);
if !is_geometry {
return mysql_value_to_json(row, idx);
}
let Some(bytes) = row_get::<Vec<u8>, _>(row, idx) else {
spatial_columns.observe(idx, None);
return serde_json::Value::Null;
};
match decode_mysql_geometry(&bytes) {
Some(geometry) => {
spatial_columns.observe(idx, geometry.srid);
srids[idx] = geometry.srid;
serde_json::Value::String(geometry.wkt)
}
None => {
spatial_columns.observe(idx, None);
super::binary_value_to_json(&bytes)
}
}
})
.collect();
(values, srids)
}
fn mysql_temporal_value_to_json(
column_type: ColumnType,
datetime: Option<NaiveDateTime>,
@@ -3540,6 +3595,8 @@ async fn execute_result_set_with_text_protocol_on_conn(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: result.affected_rows(),
execution_time_ms: start.elapsed().as_millis(),
@@ -3551,20 +3608,28 @@ async fn execute_result_set_with_text_protocol_on_conn(
}
let columns: Vec<String> = result.columns_ref().iter().map(|c| c.name_str().to_string()).collect();
let column_types: Vec<String> = result.columns_ref().iter().map(mysql_column_type_name).collect();
let mut spatial_columns = mysql_spatial_column_builder(result.columns_ref());
if should_collect_text_result_set(sql, row_limit, max_rows) {
let rows: Vec<mysql_async::Row> = result.collect_and_drop().await.map_err(|e| e.to_string())?;
let truncated = rows.len() > row_limit;
let mut spatial_values = Vec::new();
let result_rows = rows
.iter()
.take(row_limit)
.map(|row| (0..row.len()).map(|i| mysql_value_to_json(row, i)).collect())
.map(|row| {
let (values, srids) = mysql_row_to_json_with_srids(row, &mut spatial_columns);
spatial_values.push(srids);
values
})
.collect();
return Ok(QueryResult {
columns,
column_types,
column_sortables: vec![],
spatial_columns: spatial_columns.finish(),
spatial_values,
rows: result_rows,
affected_rows: 0,
execution_time_ms: start.elapsed().as_millis(),
@@ -3576,6 +3641,8 @@ async fn execute_result_set_with_text_protocol_on_conn(
}
let mut result_rows: Vec<Vec<serde_json::Value>> = Vec::new();
let mut spatial_values: Vec<Vec<Option<u32>>> = Vec::new();
let mut truncated = false;
let mut stream = result
.stream::<mysql_async::Row>()
.await
@@ -3584,22 +3651,21 @@ async fn execute_result_set_with_text_protocol_on_conn(
while let Some(row) = stream.next().await {
let row = row.map_err(|e| e.to_string())?;
let values: Vec<serde_json::Value> = (0..row.len()).map(|i| mysql_value_to_json(&row, i)).collect();
result_rows.push(values);
if result_rows.len() > row_limit {
if result_rows.len() >= row_limit {
truncated = true;
break;
}
}
let truncated = result_rows.len() > row_limit;
if truncated {
result_rows.truncate(row_limit);
let (values, srids) = mysql_row_to_json_with_srids(&row, &mut spatial_columns);
result_rows.push(values);
spatial_values.push(srids);
}
Ok(QueryResult {
columns,
column_types,
column_sortables: vec![],
spatial_columns: spatial_columns.finish(),
spatial_values,
rows: result_rows,
affected_rows: 0,
execution_time_ms: start.elapsed().as_millis(),
@@ -3631,8 +3697,11 @@ async fn execute_result_set_with_prepared_protocol_on_conn(
let mut result = conn.exec_iter(sql, ()).await.map_err(|e| e.to_string())?;
let columns: Vec<String> = result.columns_ref().iter().map(|c| c.name_str().to_string()).collect();
let column_types: Vec<String> = result.columns_ref().iter().map(mysql_column_type_name).collect();
let mut spatial_columns = mysql_spatial_column_builder(result.columns_ref());
let mut result_rows: Vec<Vec<serde_json::Value>> = Vec::new();
let mut spatial_values: Vec<Vec<Option<u32>>> = Vec::new();
let mut truncated = false;
let mut stream = result
.stream::<mysql_async::Row>()
.await
@@ -3641,22 +3710,21 @@ async fn execute_result_set_with_prepared_protocol_on_conn(
while let Some(row) = stream.next().await {
let row = row.map_err(|e| e.to_string())?;
let values: Vec<serde_json::Value> = (0..row.len()).map(|i| mysql_value_to_json(&row, i)).collect();
result_rows.push(values);
if result_rows.len() > row_limit {
if result_rows.len() >= row_limit {
truncated = true;
break;
}
}
let truncated = result_rows.len() > row_limit;
if truncated {
result_rows.truncate(row_limit);
let (values, srids) = mysql_row_to_json_with_srids(&row, &mut spatial_columns);
result_rows.push(values);
spatial_values.push(srids);
}
Ok(QueryResult {
columns,
column_types,
column_sortables: vec![],
spatial_columns: spatial_columns.finish(),
spatial_values,
rows: result_rows,
affected_rows: 0,
execution_time_ms: start.elapsed().as_millis(),
@@ -3916,6 +3984,8 @@ pub async fn execute_query_on_conn_with_max_rows(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows,
execution_time_ms: start.elapsed().as_millis(),
@@ -4246,6 +4316,39 @@ mod tests {
}
}
#[test]
fn mysql_geometry_decoder_retains_srid_prefix() {
let mut raw = 3857_u32.to_le_bytes().to_vec();
raw.extend_from_slice(&[
0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x40,
]);
let decoded = decode_mysql_geometry(&raw).unwrap();
assert_eq!(decoded.wkt, "POINT(1 2)");
assert_eq!(decoded.srid, Some(3857));
}
#[test]
fn mysql_geometry_decoder_accepts_unprefixed_wkb() {
let raw = [
0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x40,
];
let decoded = decode_mysql_geometry(&raw).unwrap();
assert_eq!(decoded.wkt, "POINT(1 2)");
assert_eq!(decoded.srid, None);
}
#[test]
fn mysql_geometry_srid_zero_is_unknown() {
let mut raw = 0_u32.to_le_bytes().to_vec();
raw.extend_from_slice(&[
0x01, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xf0, 0x3f, 0x00, 0x00, 0x00, 0x00, 0x00,
0x00, 0x00, 0x40,
]);
assert_eq!(decode_mysql_geometry(&raw).unwrap().srid, None);
}
#[test]
fn bytes_to_string_reuses_valid_utf8_and_falls_back_lossy() {
assert_eq!(super::bytes_to_string_lossy("héllo 世界".as_bytes().to_vec()), "héllo 世界");
+185 -17
View File
@@ -27,7 +27,7 @@ use crate::types::{
ColumnInfo, CompletionAssistantCandidate, CompletionAssistantCandidateKind, CompletionAssistantMatchMode,
CompletionAssistantObjectKind, CompletionAssistantRequest, CompletionAssistantResponse, DatabaseInfo,
DatabaseStorageInfo, ExtensionInfo, ForeignKeyInfo, FunctionInfo, IndexInfo, ObjectInfo, ObjectStatistics,
OwnerInfo, QueryResult, RuleInfo, SchemaInfo, SequenceInfo, TableInfo, TriggerInfo,
OwnerInfo, QueryResult, RuleInfo, SchemaInfo, SequenceInfo, SpatialColumnBuilder, TableInfo, TriggerInfo,
};
pub(crate) const GAUSSDB_COMPATIBILITY_SQL: &str =
@@ -605,6 +605,86 @@ pub(crate) fn pg_value_to_json_classified(row: &Row, idx: usize, col_type: PgCol
}
}
fn pg_value_to_json_with_srid(row: &Row, idx: usize, col_type: PgColType) -> (serde_json::Value, Option<u32>) {
if col_type != PgColType::Geometry {
return (pg_value_to_json_classified(row, idx, col_type), None);
}
if let Ok(PgRawBytes(raw)) = row.try_get::<_, PgRawBytes>(idx) {
return match super::wkb::decode_wkb_geometry(&raw) {
Some(geometry) => (serde_json::Value::String(geometry.wkt), geometry.srid),
None => (super::binary_value_to_json(&raw), None),
};
}
(serde_json::Value::Null, None)
}
fn pg_text_fallback_value(value: &str, col_type: Option<PgColType>) -> (serde_json::Value, Option<u32>) {
let (value, srid, _) = pg_text_fallback_value_with_spatial(value, col_type);
(value, srid)
}
fn pg_text_fallback_value_with_spatial(
value: &str,
col_type: Option<PgColType>,
) -> (serde_json::Value, Option<u32>, bool) {
match col_type {
Some(PgColType::Geometry) => decode_pg_text_wkb(value)
.map(|geometry| (serde_json::Value::String(geometry.wkt), geometry.srid, true))
.or_else(|| split_pg_ewkt(value, false).map(|(value, srid)| (value, srid, true)))
.unwrap_or_else(|| (serde_json::Value::String(value.to_string()), None, true)),
Some(_) => (serde_json::Value::String(value.to_string()), None, false),
None => decode_pg_text_wkb(value)
.map(|geometry| (serde_json::Value::String(geometry.wkt), geometry.srid, true))
.or_else(|| split_pg_ewkt(value, true).map(|(value, srid)| (value, srid, true)))
.unwrap_or_else(|| (serde_json::Value::String(value.to_string()), None, false)),
}
}
fn decode_pg_text_wkb(value: &str) -> Option<super::wkb::DecodedGeometry> {
let trimmed = value.trim();
let hex = trimmed
.strip_prefix("0x")
.or_else(|| trimmed.strip_prefix("0X"))
.or_else(|| trimmed.strip_prefix("\\x"))
.or_else(|| trimmed.strip_prefix("\\X"))
.unwrap_or(trimmed);
if hex.len() < 10 || !hex.len().is_multiple_of(2) || !matches!(&hex[..2], "00" | "01") || !hex.is_ascii() {
return None;
}
let bytes = hex
.as_bytes()
.chunks_exact(2)
.map(|pair| {
let text = std::str::from_utf8(pair).ok()?;
u8::from_str_radix(text, 16).ok()
})
.collect::<Option<Vec<_>>>()?;
super::wkb::decode_wkb_geometry(&bytes)
}
fn split_pg_ewkt(value: &str, require_recognizable_wkt: bool) -> Option<(serde_json::Value, Option<u32>)> {
let rest = value.strip_prefix("SRID=")?;
let (srid, wkt) = rest.split_once(';')?;
let srid = srid.parse::<i64>().ok().and_then(|value| u32::try_from(value).ok())?;
if require_recognizable_wkt && !is_recognizable_wkt(wkt) {
return None;
}
Some((serde_json::Value::String(wkt.to_string()), (srid != 0).then_some(srid)))
}
fn is_recognizable_wkt(value: &str) -> bool {
let trimmed = value.trim_start();
const TYPES: [&str; 7] =
["POINT", "LINESTRING", "POLYGON", "MULTIPOINT", "MULTILINESTRING", "MULTIPOLYGON", "GEOMETRYCOLLECTION"];
TYPES.iter().any(|geometry_type| {
trimmed
.get(..geometry_type.len())
.filter(|prefix| prefix.eq_ignore_ascii_case(geometry_type))
.and_then(|_| trimmed.as_bytes().get(geometry_type.len()))
.is_some_and(|next| next.is_ascii_whitespace() || *next == b'(')
})
}
/// Serialize a pgvector `vector` component with f32 shortest round-trip decimal text.
///
/// Casting through `f64` (or fixed fractional rounding) either expands binary noise or
@@ -883,6 +963,13 @@ async fn execute_select_prepared(
);
tokio::pin!(stream);
let mut result_rows: Vec<Vec<serde_json::Value>> = Vec::new();
let mut spatial_values: Vec<Vec<Option<u32>>> = Vec::new();
let mut spatial_columns = SpatialColumnBuilder::new(
column_classes
.iter()
.enumerate()
.filter_map(|(index, col_type)| (*col_type == PgColType::Geometry).then_some(index)),
);
let mut truncated = false;
let rows_start = Instant::now();
@@ -892,13 +979,19 @@ async fn execute_select_prepared(
break;
}
let row = row_result?;
result_rows.push(
(0..row.columns().len())
.map(|i| {
pg_value_to_json_classified(&row, i, column_classes.get(i).copied().unwrap_or(PgColType::Other))
})
.collect(),
);
let mut values = Vec::with_capacity(row.columns().len());
let mut row_srids = vec![None; row.columns().len()];
for (i, row_srid) in row_srids.iter_mut().enumerate() {
let col_type = column_classes.get(i).copied().unwrap_or(PgColType::Other);
let (value, srid) = pg_value_to_json_with_srid(&row, i, col_type);
if col_type == PgColType::Geometry {
spatial_columns.observe(i, srid);
*row_srid = srid;
}
values.push(value);
}
result_rows.push(values);
spatial_values.push(row_srids);
}
log::info!(
"[postgres][select:rows:done] elapsed_ms={} total_ms={} row_count={} truncated={}",
@@ -908,10 +1001,13 @@ async fn execute_select_prepared(
truncated
);
let (spatial_columns, spatial_values) = spatial_columns.finish_with_values(spatial_values);
Ok(PreparedSelectOutcome::Complete(QueryResult {
columns,
column_types,
column_sortables: Vec::new(),
spatial_columns,
spatial_values,
rows: result_rows,
affected_rows: 0,
execution_time_ms: start.elapsed().as_millis(),
@@ -937,6 +1033,15 @@ async fn execute_select_text(
tokio::pin!(stream);
let mut columns: Vec<String> = Vec::new();
let mut result_rows: Vec<Vec<serde_json::Value>> = Vec::new();
let mut spatial_values: Vec<Vec<Option<u32>>> = Vec::new();
let column_classes =
prepared_column_types.as_ref().map(|types| classify_pg_column_types(types)).unwrap_or_default();
let mut spatial_columns = SpatialColumnBuilder::new(
column_classes
.iter()
.enumerate()
.filter_map(|(index, col_type)| (*col_type == PgColType::Geometry).then_some(index)),
);
let mut truncated = false;
while let Some(message) = stream.next().await {
@@ -953,13 +1058,25 @@ async fn execute_select_text(
break;
}
let mut values = Vec::with_capacity(row.len());
for i in 0..row.len() {
values.push(match row.try_get(i).map_err(pg_error_to_string)? {
Some(value) => serde_json::Value::String(value.to_string()),
None => serde_json::Value::Null,
});
let mut row_srids = vec![None; row.len()];
for (i, row_srid) in row_srids.iter_mut().enumerate() {
match row.try_get(i).map_err(pg_error_to_string)? {
Some(value) => {
let (decoded, srid, is_spatial) =
pg_text_fallback_value_with_spatial(value, column_classes.get(i).copied());
values.push(decoded);
if is_spatial {
spatial_columns.observe(i, srid);
*row_srid = srid;
}
}
None => {
values.push(serde_json::Value::Null);
}
}
}
result_rows.push(values);
spatial_values.push(row_srids);
}
Err(_) if result_rows.len() >= row_limit => {
truncated = true;
@@ -971,10 +1088,13 @@ async fn execute_select_text(
}
}
let (spatial_columns, spatial_values) = spatial_columns.finish_with_values(spatial_values);
Ok(QueryResult {
column_types: matching_pg_text_column_types(&columns, prepared_column_types),
columns,
column_sortables: Vec::new(),
spatial_columns,
spatial_values,
rows: result_rows,
affected_rows: 0,
execution_time_ms: start.elapsed().as_millis(),
@@ -1112,6 +1232,8 @@ async fn stream_select_query_text(
let stream = client.simple_query_raw(sql).await.map_err(pg_error_to_string)?;
tokio::pin!(stream);
let mut columns: Vec<String> = Vec::new();
let column_classes =
prepared_column_types.as_ref().map(|types| classify_pg_column_types(types)).unwrap_or_default();
let mut rows_streamed = 0_u64;
while let Some(message) = stream.next().await {
match message.map_err(pg_error_to_string)? {
@@ -1132,7 +1254,7 @@ async fn stream_select_query_text(
let mut values = Vec::with_capacity(row.len());
for i in 0..row.len() {
values.push(match row.try_get(i).map_err(pg_error_to_string)? {
Some(value) => serde_json::Value::String(value.to_string()),
Some(value) => pg_text_fallback_value(value, column_classes.get(i).copied()).0,
None => serde_json::Value::Null,
});
}
@@ -1201,7 +1323,7 @@ pub async fn stream_query_rows(
match stream_query_rows_on_client(&client, sql, max_rows, cancelled, &mut on_row).await {
Ok(rows) => Ok(rows),
Err(error) if should_retry_postgres_text_query_message(&error.to_ascii_lowercase()) => {
stream_query_rows_text_on_client(&client, sql, max_rows, cancelled, &mut on_row).await
stream_query_rows_text_on_client(&client, sql, max_rows, cancelled, None, &mut on_row).await
}
Err(error) => Err(error),
}
@@ -1221,7 +1343,7 @@ async fn stream_query_rows_on_client(
"[postgres][row_stream:text_fallback] unsupported_type={} switching_to=simple_query",
unsupported_type
);
return stream_query_rows_text_on_client(client, sql, max_rows, cancelled, on_row).await;
return stream_query_rows_text_on_client(client, sql, max_rows, cancelled, Some(&column_classes), on_row).await;
}
let params: Vec<&(dyn tokio_postgres::types::ToSql + Sync)> = Vec::new();
let stream = client.query_raw(&stmt, params).await.map_err(pg_error_to_string)?;
@@ -1252,6 +1374,7 @@ async fn stream_query_rows_text_on_client(
sql: &str,
max_rows: Option<usize>,
cancelled: &AtomicBool,
column_classes: Option<&[PgColType]>,
on_row: &mut impl FnMut(&[serde_json::Value]) -> Result<(), String>,
) -> Result<u64, String> {
let stream = client.simple_query_raw(sql).await.map_err(pg_error_to_string)?;
@@ -1271,7 +1394,9 @@ async fn stream_query_rows_text_on_client(
let mut values = Vec::with_capacity(row.len());
for i in 0..row.len() {
values.push(match row.try_get(i).map_err(pg_error_to_string)? {
Some(value) => serde_json::Value::String(value.to_string()),
Some(value) => {
pg_text_fallback_value(value, column_classes.and_then(|classes| classes.get(i)).copied()).0
}
None => serde_json::Value::Null,
});
}
@@ -2871,6 +2996,8 @@ pub async fn execute_query_with_max_rows(
columns: vec![],
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: affected,
execution_time_ms: start.elapsed().as_millis(),
@@ -3379,6 +3506,8 @@ async fn execute_query_with_max_rows_inner(
columns: vec![],
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: affected,
execution_time_ms: start.elapsed().as_millis(),
@@ -4347,6 +4476,45 @@ mod tests {
assert_eq!(classify_pg_type(""), PgColType::Other);
}
#[test]
fn postgres_text_spatial_value_separates_srid_from_wkt() {
assert_eq!(
pg_text_fallback_value("SRID=4326;POINT(1 2)", Some(PgColType::Geometry)),
(serde_json::json!("POINT(1 2)"), Some(4326))
);
assert_eq!(
pg_text_fallback_value("SRID=0;POINT(1 2)", Some(PgColType::Geometry)),
(serde_json::json!("POINT(1 2)"), None)
);
assert_eq!(
pg_text_fallback_value("POINT(1 2)", Some(PgColType::Geometry)),
(serde_json::json!("POINT(1 2)"), None)
);
}
#[test]
fn postgres_text_spatial_value_decodes_hex_ewkb() {
let ewkb = "0101000020E6100000000000000000F03F0000000000000040";
for value in [ewkb.to_string(), format!("0x{ewkb}"), format!("\\x{ewkb}")] {
assert_eq!(
pg_text_fallback_value(&value, Some(PgColType::Geometry)),
(serde_json::json!("POINT(1 2)"), Some(4326))
);
assert_eq!(pg_text_fallback_value(&value, None), (serde_json::json!("POINT(1 2)"), Some(4326)));
}
let srid_zero = "010100002000000000000000000000F03F0000000000000040";
assert_eq!(pg_text_fallback_value(srid_zero, None), (serde_json::json!("POINT(1 2)"), None));
}
#[test]
fn postgres_text_fallback_does_not_reinterpret_ordinary_text() {
for value in ["SRID=abc;POINT(1 2)", "SRID=4326;not geometry", "0101-not-hex", "POINTLESS"] {
assert_eq!(pg_text_fallback_value(value, None), (serde_json::json!(value), None));
}
assert_eq!(pg_text_fallback_value("SRID=4326;point(1 2)", None), (serde_json::json!("point(1 2)"), Some(4326)));
}
struct DockerPostgres {
name: String,
port: u16,
+4
View File
@@ -297,6 +297,8 @@ pub async fn execute_query_with_max_rows(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows,
execution_time_ms: start.elapsed().as_millis(),
@@ -347,6 +349,8 @@ fn query_result_from_rqlite_result(
columns: result.columns,
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: result.values,
affected_rows: 0,
execution_time_ms,
+4
View File
@@ -2457,6 +2457,8 @@ fn execute_query_blocking(pool: &SqliteHandle, sql: &str, max_rows: Option<usize
columns,
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: result_rows,
affected_rows: 0,
execution_time_ms: start.elapsed().as_millis(),
@@ -2471,6 +2473,8 @@ fn execute_query_blocking(pool: &SqliteHandle, sql: &str, max_rows: Option<usize
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: conn.changes(),
execution_time_ms: start.elapsed().as_millis(),
File diff suppressed because it is too large. Load diff
+8
View File
@@ -343,6 +343,8 @@ pub async fn execute_query_with_max_rows(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: 0,
execution_time_ms: start.elapsed().as_millis(),
@@ -362,6 +364,8 @@ pub async fn execute_query_with_max_rows(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows,
execution_time_ms: start.elapsed().as_millis(),
@@ -378,6 +382,8 @@ pub async fn execute_query_with_max_rows(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: affected,
execution_time_ms: start.elapsed().as_millis(),
@@ -561,6 +567,8 @@ fn query_result_from_turso_result(
columns: result.columns,
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: result.values,
affected_rows: 0,
execution_time_ms,
+4
View File
@@ -794,6 +794,8 @@ fn json_to_query_result(status: u16, body: Value, start: Instant) -> QueryResult
columns: vec!["status".to_string(), "response".to_string()],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![vec![
Value::Number(status.into()),
Value::String(serde_json::to_string_pretty(&body).unwrap_or_else(|_| body.to_string())),
@@ -828,6 +830,8 @@ fn values_to_query_result(items: Vec<Value>, start: Instant) -> QueryResult {
columns,
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
affected_rows: rows.len() as u64,
rows,
execution_time_ms: start.elapsed().as_millis(),
+84 -21
View File
@@ -1,5 +1,6 @@
/// Shared WKB (Well-Known Binary) geometry parsing shared by PostgreSQL (EWKB)
/// and MySQL (standard WKB). Parses WKB bytes into WKT (Well-Known Text) strings.
/// and MySQL (standard WKB). Parses WKB bytes into WKT while retaining the
/// top-level EWKB SRID when present.
///
/// Supports: POINT, LINESTRING, POLYGON, MULTIPOINT, MULTILINESTRING,
/// MULTIPOLYGON, GEOMETRYCOLLECTION, and their Z/M/ZM variants.
@@ -36,6 +37,12 @@ enum WkbGeometry {
GeometryCollection { dims: WkbDimensions, geometries: Vec<WkbGeometry> },
}
#[derive(Debug, Clone, PartialEq)]
pub(crate) struct DecodedGeometry {
pub wkt: String,
pub srid: Option<u32>,
}
impl WkbGeometry {
fn to_wkt(&self) -> String {
match self {
@@ -211,7 +218,7 @@ fn parse_wkb_dimensions(type_word: u32) -> (u32, WkbDimensions, bool) {
(base_type, dims, has_srid)
}
fn read_wkb_point_coords(reader: &mut WkbReader<'_>, dims: WkbDimensions, allow_empty_point: bool) -> Option<Vec<f64>> {
fn read_wkb_point_coords(reader: &mut WkbReader<'_>, dims: WkbDimensions) -> Option<Option<Vec<f64>>> {
let endian = match reader.read_u8()? {
0 => WkbEndian::Big,
1 => WkbEndian::Little,
@@ -226,10 +233,7 @@ fn read_wkb_point_coords(reader: &mut WkbReader<'_>, dims: WkbDimensions, allow_
reader.read_u32(endian)?;
}
let coords = (0..parsed_dims.coordinate_len()).map(|_| reader.read_f64(endian)).collect::<Option<Vec<_>>>()?;
if allow_empty_point && coords.iter().all(|value| value.is_nan()) {
return None;
}
Some(coords)
Some((!coords.iter().all(|value| value.is_nan())).then_some(coords))
}
fn parse_wkb_points(reader: &mut WkbReader<'_>, endian: WkbEndian, dims: WkbDimensions) -> Option<Vec<Vec<f64>>> {
@@ -239,7 +243,7 @@ fn parse_wkb_points(reader: &mut WkbReader<'_>, endian: WkbEndian, dims: WkbDime
.collect::<Option<Vec<_>>>()
}
fn parse_wkb_geometry(reader: &mut WkbReader<'_>) -> Option<WkbGeometry> {
fn parse_wkb_geometry(reader: &mut WkbReader<'_>) -> Option<(WkbGeometry, Option<u32>)> {
let endian = match reader.read_u8()? {
0 => WkbEndian::Big,
1 => WkbEndian::Little,
@@ -247,11 +251,9 @@ fn parse_wkb_geometry(reader: &mut WkbReader<'_>) -> Option<WkbGeometry> {
};
let type_word = reader.read_u32(endian)?;
let (base_type, dims, has_srid) = parse_wkb_dimensions(type_word);
if has_srid {
reader.read_u32(endian)?;
}
let srid = has_srid.then(|| reader.read_u32(endian)).flatten().filter(|value| *value != 0);
match base_type {
let geometry = match base_type {
1 => {
let coords = (0..dims.coordinate_len()).map(|_| reader.read_f64(endian)).collect::<Option<Vec<_>>>()?;
let coords = if coords.iter().all(|value| value.is_nan()) { None } else { Some(coords) };
@@ -265,14 +267,13 @@ fn parse_wkb_geometry(reader: &mut WkbReader<'_>) -> Option<WkbGeometry> {
}
4 => {
let count = usize::try_from(reader.read_u32(endian)?).ok()?;
let points =
(0..count).map(|_| Some(read_wkb_point_coords(reader, dims, true))).collect::<Option<Vec<_>>>()?;
let points = (0..count).map(|_| read_wkb_point_coords(reader, dims)).collect::<Option<Vec<_>>>()?;
Some(WkbGeometry::MultiPoint { dims, points })
}
5 => {
let count = usize::try_from(reader.read_u32(endian)?).ok()?;
let lines = (0..count)
.map(|_| match parse_wkb_geometry(reader)? {
.map(|_| match parse_wkb_geometry(reader)?.0 {
WkbGeometry::LineString { points, .. } => Some(points),
_ => None,
})
@@ -282,7 +283,7 @@ fn parse_wkb_geometry(reader: &mut WkbReader<'_>) -> Option<WkbGeometry> {
6 => {
let count = usize::try_from(reader.read_u32(endian)?).ok()?;
let polygons = (0..count)
.map(|_| match parse_wkb_geometry(reader)? {
.map(|_| match parse_wkb_geometry(reader)?.0 {
WkbGeometry::Polygon { rings, .. } => Some(rings),
_ => None,
})
@@ -291,11 +292,22 @@ fn parse_wkb_geometry(reader: &mut WkbReader<'_>) -> Option<WkbGeometry> {
}
7 => {
let count = usize::try_from(reader.read_u32(endian)?).ok()?;
let geometries = (0..count).map(|_| parse_wkb_geometry(reader)).collect::<Option<Vec<_>>>()?;
let geometries =
(0..count).map(|_| parse_wkb_geometry(reader).map(|parsed| parsed.0)).collect::<Option<Vec<_>>>()?;
Some(WkbGeometry::GeometryCollection { dims, geometries })
}
_ => None,
}?;
Some((geometry, srid))
}
pub(crate) fn decode_wkb_geometry(raw: &[u8]) -> Option<DecodedGeometry> {
let mut reader = WkbReader::new(raw);
let (geometry, srid) = parse_wkb_geometry(&mut reader)?;
if reader.pos != raw.len() {
return None;
}
Some(DecodedGeometry { wkt: geometry.to_wkt(), srid })
}
/// Parse WKB/EWKB bytes into a WKT (Well-Known Text) string.
@@ -303,10 +315,61 @@ fn parse_wkb_geometry(reader: &mut WkbReader<'_>) -> Option<WkbGeometry> {
/// Supports both standard WKB (MySQL) and Extended WKB (PostgreSQL/PostGIS).
/// Returns `None` if the bytes cannot be parsed as valid WKB.
pub(crate) fn wkb_to_wkt(raw: &[u8]) -> Option<String> {
let mut reader = WkbReader::new(raw);
let geometry = parse_wkb_geometry(&mut reader)?;
if reader.pos != raw.len() {
return None;
decode_wkb_geometry(raw).map(|geometry| geometry.wkt)
}
#[cfg(test)]
mod tests {
use super::*;
fn decode_hex(value: &str) -> Vec<u8> {
value
.as_bytes()
.chunks_exact(2)
.map(|pair| {
let text = std::str::from_utf8(pair).unwrap();
u8::from_str_radix(text, 16).unwrap()
})
.collect()
}
Some(geometry.to_wkt())
#[test]
fn ewkb_decoder_retains_top_level_srid() {
let decoded = decode_wkb_geometry(&decode_hex("0101000020E6100000C520B07268195D404E62105839F44340")).unwrap();
assert_eq!(decoded.wkt, "POINT(116.397 39.908)");
assert_eq!(decoded.srid, Some(4326));
}
#[test]
fn standard_wkb_has_no_srid() {
let decoded = decode_wkb_geometry(&decode_hex("0101000000000000000000F03F0000000000000040")).unwrap();
assert_eq!(decoded.wkt, "POINT(1 2)");
assert_eq!(decoded.srid, None);
}
#[test]
fn big_endian_ewkb_retains_srid() {
let decoded = decode_wkb_geometry(&decode_hex("002000000100000F113FF00000000000004000000000000000")).unwrap();
assert_eq!(decoded.wkt, "POINT(1 2)");
assert_eq!(decoded.srid, Some(3857));
}
#[test]
fn ewkb_srid_zero_is_unknown() {
let decoded = decode_wkb_geometry(&decode_hex("010100002000000000000000000000F03F0000000000000040")).unwrap();
assert_eq!(decoded.wkt, "POINT(1 2)");
assert_eq!(decoded.srid, None);
}
#[test]
fn multipoint_preserves_valid_empty_points() {
let decoded =
decode_wkb_geometry(&decode_hex("0104000000010000000101000000000000000000F87F000000000000F87F")).unwrap();
assert_eq!(decoded.wkt, "MULTIPOINT(EMPTY)");
}
#[test]
fn multipoint_rejects_truncated_child_points() {
assert!(decode_wkb_geometry(&decode_hex("0104000000010000000101000000")).is_none());
}
}
+38
View File
@@ -1984,6 +1984,8 @@ fn error_query_result(message: String) -> db::QueryResult {
columns: vec!["Error".to_string()],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![vec![serde_json::Value::String(message)]],
affected_rows: 0,
execution_time_ms: 0,
@@ -1999,6 +2001,8 @@ fn empty_query_result(execution_time_ms: u128) -> db::QueryResult {
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: 0,
execution_time_ms,
@@ -2031,6 +2035,8 @@ async fn execute_multi_sqlserver(
columns: vec!["Error".to_string()],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![vec![serde_json::Value::String(canceled_error())]],
affected_rows: 0,
execution_time_ms: 0,
@@ -2083,6 +2089,8 @@ async fn execute_multi_sqlserver(
columns: vec!["Error".to_string()],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![vec![serde_json::Value::String(e)]],
affected_rows: 0,
execution_time_ms: 0,
@@ -2106,6 +2114,8 @@ async fn execute_multi_sqlserver(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: 0,
execution_time_ms: 0,
@@ -2232,6 +2242,8 @@ pub async fn execute_statements(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: total_affected,
execution_time_ms: start.elapsed().as_millis(),
@@ -2670,6 +2682,8 @@ async fn exec_tx_pg_inner(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: total_affected,
execution_time_ms: start.elapsed().as_millis(),
@@ -2754,6 +2768,8 @@ async fn exec_tx_mysql_inner(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: total_affected,
execution_time_ms: start.elapsed().as_millis(),
@@ -2818,6 +2834,8 @@ async fn exec_tx_sqlite_inner(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: total_affected,
execution_time_ms: start.elapsed().as_millis(),
@@ -2908,6 +2926,8 @@ async fn exec_tx_explicit_inner(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: total_affected,
execution_time_ms: start.elapsed().as_millis(),
@@ -2951,6 +2971,8 @@ async fn exec_tx_none_inner(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: total_affected,
execution_time_ms: start.elapsed().as_millis(),
@@ -3416,6 +3438,8 @@ async fn execute_manual_txn_postgres_statement(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: affected,
execution_time_ms: 0,
@@ -3456,6 +3480,8 @@ async fn execute_manual_txn_mysql_statement(
columns,
column_types,
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: data,
affected_rows: 0,
execution_time_ms: start.elapsed().as_millis(),
@@ -3472,6 +3498,8 @@ async fn execute_manual_txn_mysql_statement(
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows,
execution_time_ms: 0,
@@ -3505,6 +3533,8 @@ pub async fn commit_manual_transaction(state: &AppState, txn_session_id: &str) -
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: 0,
execution_time_ms: 0,
@@ -3530,6 +3560,8 @@ pub async fn rollback_manual_transaction(state: &AppState, txn_session_id: &str)
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: 0,
execution_time_ms: 0,
@@ -4322,6 +4354,8 @@ for line in sys.stdin:
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: 0,
execution_time_ms: 0,
@@ -4344,6 +4378,8 @@ for line in sys.stdin:
columns: vec![],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![],
affected_rows: 0,
execution_time_ms: 0,
@@ -4992,6 +5028,8 @@ for line in sys.stdin:
columns: vec!["id".to_string(), "nested".to_string()],
column_types: Vec::new(),
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![vec![
serde_json::json!(2_041_797_190_226_354_178_i64),
serde_json::json!([1, 2_041_797_190_226_354_178_i64]),
+18
View File
@@ -2773,6 +2773,8 @@ mod tests {
columns: vec!["Table".to_string(), "Extra".to_string(), "CREATE TABLE".to_string()],
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![vec![
serde_json::json!("users"),
serde_json::json!("ignored"),
@@ -2795,6 +2797,8 @@ mod tests {
columns: vec!["name".to_string(), "definition".to_string()],
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![vec![serde_json::json!("users"), serde_json::json!("CREATE TABLE `users` (`id` bigint);\n")]],
affected_rows: 0,
execution_time_ms: 0,
@@ -3288,6 +3292,8 @@ mod tests {
columns: vec!["table_name".to_string(), "table_type".to_string()],
column_types: vec![],
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![
vec![serde_json::json!("daily_revenue"), serde_json::json!("BASE TABLE")],
vec![serde_json::json!("revenue_view"), serde_json::json!("VIEW")],
@@ -3321,6 +3327,8 @@ mod tests {
],
column_types: vec![],
column_sortables: vec![],
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![
vec![
serde_json::json!("amount"),
@@ -3505,6 +3513,8 @@ mod tests {
columns: vec!["COMMENTS".to_string()],
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![vec![serde_json::json!("Customer table")]],
affected_rows: 0,
execution_time_ms: 0,
@@ -3520,6 +3530,8 @@ mod tests {
columns: vec!["COMMENTS".to_string()],
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![vec![serde_json::json!(" ")]],
affected_rows: 0,
execution_time_ms: 0,
@@ -3550,6 +3562,8 @@ mod tests {
columns: vec!["TABLE_NAME".to_string(), "COMMENTS".to_string()],
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![
vec![serde_json::json!("ORDERS"), serde_json::json!("Orders table")],
vec![serde_json::json!("PRODUCTS"), serde_json::json!(" ")],
@@ -3648,6 +3662,8 @@ mod tests {
],
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![
vec![
serde_json::json!("id"),
@@ -3762,6 +3778,8 @@ mod tests {
],
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![
vec![
serde_json::json!("ORDERS"),
+2
View File
@@ -195,6 +195,8 @@ mod tests {
],
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows: vec![vec![
serde_json::json!("kdb_utils"),
serde_json::json!("1.0"),
+2
View File
@@ -418,6 +418,8 @@ async fn fetch_table_export_batch(
columns: col_names.to_vec(),
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows: Vec::new(),
affected_rows: 0,
execution_time_ms: 0,
@@ -679,6 +679,8 @@ fn execute_change_transaction(
columns: Vec::new(),
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows: Vec::new(),
affected_rows: 0,
execution_time_ms: started_at.elapsed().as_millis(),
+2
View File
@@ -5323,6 +5323,8 @@ mod tests {
columns: Vec::new(),
column_types: Vec::new(),
column_sortables: Vec::new(),
spatial_columns: vec![],
spatial_values: vec![],
rows,
affected_rows: 0,
execution_time_ms: 0,
+99 -1
View File
@@ -226,6 +226,55 @@ pub struct CompletionAssistantResponse {
pub fallback_used: bool,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct SpatialColumn {
/// Zero-based index into `QueryResult.columns`.
pub column_index: usize,
/// SRID shared by the column's geometry cells. `None` when unknown/absent
/// (or SRID 0). A column reports the first non-null SRID it observes.
pub srid: Option<u32>,
}
#[derive(Debug, Default)]
pub(crate) struct SpatialColumnBuilder {
// column_index -> first non-null srid seen (sticky once set)
columns: std::collections::BTreeMap<usize, Option<u32>>,
}
impl SpatialColumnBuilder {
pub(crate) fn new(column_indices: impl IntoIterator<Item = usize>) -> Self {
let mut builder = Self::default();
for column_index in column_indices {
builder.columns.entry(column_index).or_insert(None);
}
builder
}
/// Record a geometry cell's SRID. The first non-null (and non-zero) value
/// wins; later observations for the same column are ignored.
pub(crate) fn observe(&mut self, column_index: usize, srid: Option<u32>) {
let entry = self.columns.entry(column_index).or_insert(None);
if entry.is_none() {
if let Some(value) = srid.filter(|value| *value != 0) {
*entry = Some(value);
}
}
}
pub(crate) fn finish(self) -> Vec<SpatialColumn> {
self.columns.into_iter().map(|(column_index, srid)| SpatialColumn { column_index, srid }).collect()
}
pub(crate) fn finish_with_values(
self,
spatial_values: Vec<Vec<Option<u32>>>,
) -> (Vec<SpatialColumn>, Vec<Vec<Option<u32>>>) {
let spatial_columns = self.finish();
let spatial_values = if spatial_columns.is_empty() { Vec::new() } else { spatial_values };
(spatial_columns, spatial_values)
}
}
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct QueryResult {
pub columns: Vec<String>,
@@ -238,6 +287,16 @@ pub struct QueryResult {
/// be shorter/empty when a driver cannot supply sortable information.
#[serde(default)]
pub column_sortables: Vec<bool>,
/// Spatial reference metadata for geometry/geography cells. Kept outside
/// `rows` so displayed, copied, exported, and edited values remain WKT.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub spatial_columns: Vec<SpatialColumn>,
/// Per-cell SRID metadata, parallel to `rows`: `spatial_values[row][column]`
/// is the SRID of that cell's geometry value (`None` for non-spatial cells
/// or unknown SRID). Unlike `spatial_columns` (a column-level hint), every
/// geometry value keeps its own SRID so mixed-SRID results stay correct.
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub spatial_values: Vec<Vec<Option<u32>>>,
pub rows: Vec<Vec<serde_json::Value>>,
pub affected_rows: u64,
pub execution_time_ms: u128,
@@ -384,7 +443,7 @@ pub struct OwnerInfo {
#[cfg(test)]
mod tests {
use super::{ObjectInfo, ObjectSourceKind};
use super::{ObjectInfo, ObjectSourceKind, SpatialColumn, SpatialColumnBuilder};
#[test]
fn list_objects_payload_preserves_optional_validity() {
@@ -396,6 +455,45 @@ mod tests {
assert_eq!(objects[0].object_type, "TRIGGER");
}
#[test]
fn spatial_builder_reports_first_non_null_srid_per_column() {
let mut builder = SpatialColumnBuilder::new([3]);
builder.observe(3, None);
builder.observe(1, Some(4326));
builder.observe(3, Some(3857));
builder.observe(3, Some(4490)); // ignored: column 3 already set
builder.observe(1, None); // ignored: column 1 already set
assert_eq!(
builder.finish(),
vec![
SpatialColumn { column_index: 1, srid: Some(4326) },
SpatialColumn { column_index: 3, srid: Some(3857) },
]
);
}
#[test]
fn spatial_builder_normalizes_zero_and_all_null() {
let mut builder = SpatialColumnBuilder::new([0]);
builder.observe(0, Some(0)); // SRID 0 -> unknown
assert_eq!(builder.finish(), vec![SpatialColumn { column_index: 0, srid: None }]);
assert!(SpatialColumnBuilder::default().finish().is_empty());
}
#[test]
fn spatial_builder_omits_values_without_spatial_columns() {
let values = vec![vec![None, None]];
let (columns, values) = SpatialColumnBuilder::default().finish_with_values(values);
assert!(columns.is_empty());
assert!(values.is_empty());
let expected_values = vec![vec![None, None]];
let (columns, values) = SpatialColumnBuilder::new([0]).finish_with_values(expected_values.clone());
assert_eq!(columns, vec![SpatialColumn { column_index: 0, srid: None }]);
assert_eq!(values, expected_values);
}
#[test]
fn object_source_kind_accepts_synonym_wire_value() {
let kind: ObjectSourceKind = serde_json::from_str("\"SYNONYM\"").unwrap();