A percent sign in a statement reaches the server doubled

未關閉
#853 0 則留言 0 個 reaction 已指派 1 人 在 GitHub 檢視

@florinutz 已經在處理了。

開始於 2026年11月26日。

  • #854 來自 @florinutz —— 未關閉

評估

這個 Issue 還沒有評估資料。

描述

bug

SQLAlchemy writes a percent sign in a statement as %% when the driver takes %(name)s parameters, and the driver is expected to send one %. crate-python takes these parameters since 2.2.0, but its cursor sends %% to the server unchanged. sqlalchemy-cratedb uses them since 0.43.0, so from that release a percent sign in a statement arrives doubled (0.42.0 returns the right results):

# docker run -d --rm --name crate -p 4200:4200 crate:6.5.1 -Cdiscovery.type=single-node
# uv run --with sqlalchemy-cratedb==0.45.0 python repro.py
import sqlalchemy as sa

e = sa.create_engine("crate://localhost:4200")
with e.connect() as c:
    # expected 'a%', actual 'a%%'
    print(c.execute(sa.text("SELECT 'a%'")).scalar())
    # expected 1, actual ProgrammingError: ParsingException ... 'SELECT 7 %%'
    try:
        print(c.execute(sa.select(sa.literal_column("7") % sa.literal_column("3"))).scalar())
    except Exception as ex:
        print(type(ex).__name__, str(ex).splitlines()[0])
    # expected 'a%b', actual 'a%%b'
    print(c.execute(sa.text("SELECT 'a%' || :x"), {"x": "b"}).scalar())

In DDL, server_default="100%" stores 100%%, and a table with Computed("id % 3") cannot be created. A value passed as a parameter is not affected.

The cursor shows it without SQLAlchemy:

# crate-python returns 'a%%b', psycopg returns 'a%b'
cursor.execute("SELECT 'a%%' || %(x)s", {"x": "b"})

When execute or executemany gets parameters as a dict, the cursor needs to turn %% into %, as psycopg does (SQLAlchemy passes an empty dict for a statement without parameters). A call without parameters needs no change, because psycopg leaves %% as it is there too.

主要語言
Python
星號
85
分支
37
PR 合併指標
30 天內沒有已合併 PR

環境準備

  • 沒有 Dockerfile 或 Docker Compose 檔案
  • 沒有 Pull Request 範本
  • 閱讀貢獻指南

從這裡開始

  1. 先讀完整個 Issue,再讀專案的貢獻指南。
  2. 在 Issue 下留言說明你要接手 —— 這能避免兩個人做同樣的事。
  3. Fork 儲存庫,在一個分支上完成修改。
  4. 送出 Pull Request,並在描述裡引用這個 Issue 編號。

crate/crate-python 的其他 Issue

查看 crate/crate-python 的全部 Issue

相似的 Issue

更多 Python Issue

把新 issue 寄到你的電子郵件信箱

精選適合新手參與的 GitHub issue 摘要。