Skip to content

Types: Compile Float without a precision to DOUBLE - #317

Open
aminghadersohi wants to merge 2 commits into
crate:mainfrom
aminghadersohi:float-default-double
Open

aminghadersohi wants to merge 2 commits into
crate:mainfrom
aminghadersohi:float-default-double

Conversation

@aminghadersohi

@aminghadersohi aminghadersohi commented Sep 26, 2026 •

Copy link
Copy Markdown

Problem

sa.Float() without a precision compiles to FLOAT, which is CrateDB's 4-byte REAL. Python floats are 8-byte, so values written through a generic Float column are silently rounded to 24 bits of precision. Against CrateDB 5.10.16:

t = sa.Table("t", md, sa.Column("id", sa.Integer, primary_key=True), sa.Column("x", sa.Float))
# INSERT x = 0.1 + 2**-40  ->  column type "real", reads back 0.1

SQLAlchemy's Float documentation (quoted in visit_FLOAT's docstring) describes a precision-less Float as 8-byte on backends that distinguish the two. PostgreSQL and MSSQL render FLOAT there, which on those backends means double precision. To get a 4-byte type, the documentation says to pass a precision of 24 or lower, or to use REAL.

Change

Add visit_float for the generic Float type. With no precision it renders DOUBLE. Otherwise it defers to the existing visit_FLOAT, so a precision of 24 or lower still renders FLOAT and higher precisions render DOUBLE. The uppercase sa.FLOAT type is untouched and still renders FLOAT. REAL and Double are also untouched and render REAL and DOUBLE.

This changes the DDL of existing code that uses sa.Float(), so the changelog entry is marked BREAKING. Existing tables are not affected. pandas' to_sql uses Float(precision=53) for float64 columns, so its DDL is unchanged.

Tests

  • test_float_ddl in tests/compiler_test.py covers Float(), Float(asdecimal=True), precisions 10, 24, 25 and 53, FLOAT, REAL, and Double (on SQLAlchemy 2.0+, where it exists). The expected DDL in create_table_test.py changes to float_col DOUBLE.
  • The CI workflow's steps, run locally against CrateDB nightly (6.5.0): poe lint is clean and integration.py passes. pytest gives 208 passed on SQLAlchemy 2.1.1, 193 passed on 1.4.54, and 131 passed on 1.3.24. On 1.3 there is one failure, test_schema.py::test_correct_schema, which fails the same way on main.
  • Live check against CrateDB 5.10.16 with a Float() column: on main it was created as real and 0.1 + 2**-40 read back as 0.1. On this branch it is double precision and the value round-trips exactly.

CrateDB's FLOAT is the 4-byte REAL, so a generic Float column rounded
Python floats to 24 bits of precision. SQLAlchemy documents a
precision-less Float as 8-byte on backends that distinguish the two.
Float(precision<=24), REAL and the uppercase FLOAT type still render
4-byte columns.
@coderabbitai

coderabbitai Bot commented Sep 26, 2026 •

Copy link
Copy Markdown

Important

Review skipped

Auto reviews are disabled on this repository. Please check the settings in the CodeRabbit UI or the .coderabbit.yaml file in this repository. To trigger a single review, invoke the @coderabbitai review command.

⚙️ Run configuration

Configuration used: Organization UI

Review profile: CHILL

Plan: Advanced

Run ID: 349dc425-c850-4865-a53c-238012e2cb1f

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share

Comment @coderabbitai help to get the list of available commands.

sa.Double does not exist on SQLAlchemy 1.3/1.4, so importing
compiler_test failed and stopped the whole test run on those versions.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants