Skip to content

Fix SQLAlchemy Decimal arithmetic type adaptation - #1028

Merged
joe-clickhouse merged 1 commit into
mainfrom
joe/fix-decimal-arithmetic
Sep 6, 2026
Merged

Fix SQLAlchemy Decimal arithmetic type adaptation#1028
joe-clickhouse merged 1 commit into
mainfrom
joe/fix-decimal-arithmetic

Conversation

@joe-clickhouse

Copy link
Copy Markdown
Contributor

Summary

Decimal arithmetic such as col * 100 now builds correctly. Reuses SQLAlchemy's Numeric adaptation rules while preserving configured types and constructor validation.

Closes #1027

Checklist

Delete items not relevant to your PR:

  • Unit and integration tests covering the common scenarios were added
  • A human-readable description of the changes was provided to include in CHANGELOG

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The change is narrowly scoped to SQLAlchemy type adaptation and is backed by targeted unit and integration regression tests covering the reported failure mode.

Pull request overview

Fixes SQLAlchemy expression type adaptation for cc_sqlalchemy Decimal so arithmetic like col * 100 no longer triggers a Decimal() construction with invalid default precision/scale, while keeping configured ClickHouse decimal types and SQLAlchemy numeric promotion behavior intact.

Changes:

  • Override Decimal._expression_adaptations to reuse SQLAlchemy Numeric adaptation rules (avoids constructing unconfigured Decimal() during expression adaptation).
  • Add unit coverage for literal, composed, mixed-type, Interval, and TypeDecorator arithmetic scenarios, plus constructor validation.
  • Add integration coverage that compiles and executes representative decimal arithmetic and validates round-trip decimal.Decimal results.
File summaries
File Description
clickhouse_connect/cc_sqlalchemy/datatypes/sqltypes.py Reuses SQLAlchemy Numeric expression adaptation rules for ClickHouse Decimal types to prevent invalid default construction during arithmetic.
tests/unit_tests/test_sqlalchemy/test_decimal_arithmetic.py Adds unit tests ensuring decimal arithmetic builds correctly and preserves configured types, promotion rules, and TypeDecorator processing.
tests/integration_tests/test_sqlalchemy/test_decimal_arithmetic.py Adds integration tests validating compiled SQL + parameter binding and actual ClickHouse round-trip results for common decimal arithmetic.
CHANGELOG.md Documents the user-visible bug fix and links the closed issue.
Review details
  • Files reviewed: 4/4 changed files
  • Comments generated: 0
  • Review effort level: Lite

💡 Configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

@joe-clickhouse
joe-clickhouse merged commit 87ccee0 into main Sep 6, 2026
33 checks passed
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.

[sqlalchemy] arithmetic on a Decimal column raises ArgumentError: Invalid precision or scale for ClickHouse Decimal type

2 participants