Skip to content

armgit / dbx

Public Imported from GitHub
0

25 MB lightweight cross-platform database client for 100+ databases, including MySQL, PostgreSQL, SQLite, Redis, MongoDB, DuckDB, SQL Server, and Dameng. Built-in AI, MCP Server, CLI, desktop and Docker. | 轻量级跨平台数据库管理工具,支持 MySQL、PostgreSQL、SQLite、Redis、MongoDB、达梦等 100+ 数据库,提供桌面端、Docker、CLI、内置 AI 助手和 MCP。

fix(csv): count decimal significant digits across separator

Jarvis268829afb273
diff --git a/crates/dbx-core/src/query/snapshot_export.rs b/crates/dbx-core/src/query/snapshot_export.rs
index 0c347ce637..e706d517d2 100644
--- a/crates/dbx-core/src/query/snapshot_export.rs
+++ b/crates/dbx-core/src/query/snapshot_export.rs
@@ -307,7 +307,8 @@ fn stream_formula_guard(reader: impl Read) -> Result<bool, String> {
Some(b'.') if !dot && digits > 0 => {
dot = true;
digits = 0;
- significant = 0;
+ // Precision is shared across both sides of the decimal point.
+ // Only leading zeros before the first nonzero digit are ignored.
}
None => return Ok(digits == 0),
_ => return Ok(true),
@@ -330,6 +331,14 @@ mod tests {
"-1.25",
"-00000000000000000000000000001",
"-1.00000000000000001",
+ "-123456789012345.6",
+ "-12345678.12345678",
+ "-12345678.01234567",
+ "-12345678.1234567",
+ "-00012345678.1234567",
+ "-0.000000000000000123456789012345",
+ "-0.0000000000000001234567890123456",
+ "-0.00000000000000000000",
"-2+cmd",
"-",
"'-1",
@@ -360,6 +369,10 @@ mod tests {
for value in [
format!("{}=cmd", " ".repeat(16385)),
format!("-{}1", "0".repeat(16385)),
+ format!("-{}12345678.1234567", "0".repeat(8182)),
+ format!("-{}12345678.01234567", "0".repeat(8182)),
+ format!("-0.{}123456789012345", "0".repeat(16385)),
+ format!("-0.{}1234567890123456", "0".repeat(16385)),
format!("{}中🙂", " ".repeat(16385)),
] {
let mut reader = CountingReader { data: value.as_bytes(), reads: 0 };
diff --git a/crates/dbx-formats/src/csv_export.rs b/crates/dbx-formats/src/csv_export.rs
index 7e9d538e2f..9d2c24e9cb 100644
--- a/crates/dbx-formats/src/csv_export.rs
+++ b/crates/dbx-formats/src/csv_export.rs
@@ -92,17 +92,16 @@ fn at_sign_begins_excel_formula(rest: &str) -> bool {
fn is_excel_exact_decimal_literal(value: &str) -> bool {
let bytes = value.as_bytes();
let digits = if matches!(bytes.first(), Some(b'+') | Some(b'-')) { &bytes[1..] } else { bytes };
- match digits.iter().position(|&byte| byte == b'.') {
- Some(dot) => is_exact_digit_run(&digits[..dot]) && is_exact_digit_run(&digits[dot + 1..]),
- None => is_exact_digit_run(digits),
- }
-}
-
-/// 整段是否为非空纯 ASCII 数字,且有效数字(去前导零)不超过 Excel 精度上限。
-fn is_exact_digit_run(bytes: &[u8]) -> bool {
- !bytes.is_empty()
- && bytes.iter().all(u8::is_ascii_digit)
- && bytes.iter().skip_while(|&&byte| byte == b'0').count() <= EXCEL_SIGNIFICANT_DIGIT_LIMIT
+ let valid_digits = |part: &[u8]| !part.is_empty() && part.iter().all(u8::is_ascii_digit);
+ let valid = match digits.iter().position(|&byte| byte == b'.') {
+ Some(dot) => valid_digits(&digits[..dot]) && valid_digits(&digits[dot + 1..]),
+ None => valid_digits(digits),
+ };
+ // Count the whole decimal together: fractional zeros are significant once
+ // the first nonzero digit has appeared in either part.
+ valid
+ && digits.iter().filter(|&&byte| byte != b'.').skip_while(|&&byte| byte == b'0').count()
+ <= EXCEL_SIGNIFICANT_DIGIT_LIMIT
}
/// 在单元格值开头写入公式中和前缀,每单元格调用一次:
@@ -841,6 +840,26 @@ mod tests {
);
}
+ #[test]
+ fn negative_decimal_precision_counts_digits_across_the_decimal_point() {
+ for (value, guarded) in [
+ ("-123456789012345.6", true),
+ ("-12345678.12345678", true),
+ ("-12345678.01234567", true),
+ ("-12345678.1234567", false),
+ ("-00012345678.1234567", false),
+ ("-0.000000000000000123456789012345", false),
+ ("-0.0000000000000001234567890123456", true),
+ ("-0.00000000000000000000", false),
+ ] {
+ let expected = format!("{}{value}", if guarded { "'" } else { "" });
+ let rows = [vec![json!(value)]];
+ assert_eq!(format_csv(&["v".to_string()], &rows), format!("\"v\"\n\"{expected}\""), "CSV: {value}");
+ assert_eq!(format_tsv(&["v".to_string()], &rows), format!("v\n{expected}"), "TSV: {value}");
+ assert_eq!(super::strip_formula_guard(&expected), value);
+ }
+ }
+
#[test]
fn formula_guard_covers_necessary_mode_raw_cells_and_tsv() {
let out = format_csv_with_quote_mode(