Skip to content

SELECT INTO OUTFILE loses DOUBLE precision during CSV round trip #25605

Closed

Description

@Ariznawlll

Bug summary

SELECT ... INTO OUTFILE loses precision when exporting an unscaled DOUBLE. Loading the generated CSV back into a DOUBLE column can therefore change the value and break an exact round trip.

This is reproducible on both:

  • main: 7821d21c723372e45a8f65da3bbb896a157b1036
  • 4.1-dev: 7c060bd1292664acc0892525d2fd66581f893c97

Reproduction

drop table if exists t_rt_src;
drop table if exists t_rt_dst;

create table t_rt_src (
  id bigint primary key,
  f double,
  d date,
  s varchar(200)
);

insert into t_rt_src values
  (1, 3.14159265358979, '2024-01-15', 'hello "world"'),
  (2, null, null, null);

select * from t_rt_src into outfile '/tmp/rt_test.csv';

create table t_rt_dst like t_rt_src;
load data infile '/tmp/rt_test.csv'
  into table t_rt_dst
  fields terminated by ',' enclosed by '"'
  lines terminated by '\n'
  ignore 1 lines;

select s.id, s.f as src_f, d.f as dst_f, s.f = d.f as f_equal
from t_rt_src s join t_rt_dst d on s.id = d.id
order by s.id;

The non-NULL row does not compare equal after the round trip. The original value is 3.14159265358979, while formatting it with float32 precision produces 3.1415927 (parsed as approximately 3.1415927410125732).

Expected behavior

An exported DOUBLE should retain sufficient precision to round-trip back to the same DOUBLE value.

Actual behavior

Only the all-NULL row remains an exact match. The MOTR regression therefore reports an exact-match count of 1 instead of 2:

https://github.com/matrixorigin/mo-nightly-regression/actions/runs/29136235983/job/86501442683

Root cause

In pkg/frontend/export.go, the types.T_float64 default formatting path passes bitSize=32 to strconv.FormatFloat:

case types.T_float64:
    val := vector.GetFixedAtNoTypeCheck[float64](vec, i)
    if vec.GetType().Scale < 0 || vec.GetType().Width == 0 {
        formatOutputString(ep,
            []byte(strconv.FormatFloat(float64(val), 'f', -1, 32)),
            symbol[j], closeby, flag[j], buffer)
    }

Source:

case types.T_float64:
val := vector.GetFixedAtNoTypeCheck[float64](vec, i)
if vec.GetType().Scale < 0 || vec.GetType().Width == 0 {
formatOutputString(ep, []byte(strconv.FormatFloat(float64(val), 'f', -1, 32)), symbol[j], closeby, flag[j], buffer)
} else {
formatOutputString(ep, []byte(strconv.FormatFloat(float64(val), 'f', int(vec.GetType().Scale), 64)), symbol[j], closeby, flag[j], buffer)

Suggested fix

Use bitSize=64 for types.T_float64 and add a regression that exports and reloads a high-precision DOUBLE, then verifies exact equality. The fix should also be backported to 4.1-dev.

Activity

  1. added
    kind/bug
    on Jul 11, 2026
  2. added theissue type on Jul 11, 2026
  3. added
    priority/p0
    severity/s0
    on Jul 12, 2026
  4. daviszhen commented on Jul 30, 2026

    @daviszhen
    Contributor

    fixed

  5. Ariznawlll commented on Jul 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main(截至 2026-07-30 16:30,Asia/Shanghai)
    • commit:41332062a6740978f269088a92ee362a888c7bd7
    • 部署方式:从该官方 commit 源码构建;etc/launch-with-proxy/launch.toml(2 CN + 1 TN + Proxy),SQL 入口 127.0.0.1:6001
    • 测试日期:2026-07-30

    原问题验证

    执行 SELECT ... INTO OUTFILE 后再 LOAD DATA 回同构表,以 <=> 逐行精确比较 DOUBLE、DATE、字符串与 NULL;5/5 行完全一致,完整黑盒 3/3 通过。

    Happy Path 覆盖

    1. 3.14159265358979 与 -1.2345678901234567 精确回环。
    2. 最大有限 DOUBLE 1.7976931348623157e308 和最小 subnormal 5e-324 精确回环。
    3. 带双引号、逗号字符串、日期和全 NULL 行均正确。

    Unhappy Path 覆盖

    1. 该 issue 的失败面是合法 DOUBLE 被错误降精度,不涉及非法输入;补测 NULL、CSV 引号/逗号和极值,未出现截断、错列或部分行差异。

    白盒验证

    TestConstructByteFormatsUnscaledFloat64WithFullPrecision 连续 3 轮通过。

    回归测试

    修复提交 8694a8d26 已扩展 test/distributed/cases/load_data/load_data.sql。

    测试证据

    完整 3 轮 SQL 结果、测试脚本、Proxy 黑盒日志、定向 UT 与 -race 输出:https://gist.github.com/Ariznawlll/b8f1a22642a58fd5264482687534728b

    测试结论

    测试通过。

  6. Ariznawlll commented on Jul 30, 2026

    @Ariznawlll
    CollaboratorAuthor

    测试版本

    • 分支:main
    • commit:41332062a6740978f269088a92ee362a888c7bd7
    • 部署方式:官方 commit 源码构建,2 CN + 1 TN + Proxy,经 Proxy 执行客户端 SQL
    • 测试日期:2026-07-30

    原问题验证

    使用 SELECT ... INTO OUTFILE 导出同构表,再由 LOAD DATA 导回新表。以主键关联后分别对 DOUBLE、DATE、VARCHAR 使用 <=> 精确比较:5 行的比较结果为 (总行数, DOUBLE 相等数, DATE 相等数, VARCHAR 相等数) = (5, 5, 5, 5)。

    Happy Path 覆盖

    1. 覆盖普通高精度值 3.14159265358979、负值 -1.2345678901234567、最大有限 DOUBLE 1.7976931348623157e308 以及最小 subnormal 5e-324,导出/导入后均与源值精确相等。
    2. 覆盖全 NULL 行、DATE、含双引号字符串和含逗号字符串;目标表行数保持为 5。
    3. 新增 MOTR script 用 stage 路径完成真实的导出/导入链路,连续执行 3/3 轮均通过。

    Unhappy Path 覆盖

    1. 对不存在的 stage CSV 执行 LOAD DATA 被拒绝;随后读取目标表,行数仍为 0,确认失败没有写入部分数据。

    白盒验证

    不适用。本次新增的是客户端可见的导出/导入回环回归;源码中的 float64 格式化定向 UT 已在此前验证中覆盖。

    回归测试

    • MOTR PR:matrixorigin/motr#72
    • 新增文件:issue_25605_outfile_load_roundtrip.py
    • 定向回归:./motr --repeat 3 14_issue_regression.issue_25605_outfile_load_roundtrip -v,3/3 通过。
    • 整套回归:./motr --suite 14_issue_regression -j 6 -v,125 通过、0 失败、2 个 opt-in 场景跳过。

    测试证据

    测试结论

    测试通过。

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

kind/bugpriority/p0severity/s0

Type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions