pandas.to_sql fails when writing more than 256 cells to a SQL Warehouse with method="multi"
Chưa có ai nhận issue này.
Đánh giá
- Độ khó
- 4/5
- Thời gian dự kiến
- 3-5 ngày
- Mức phù hợp với người mới
- 25/100
Hướng nghiên cứu
Tái hiện ví dụ pandas.DataFrame.to_sql với SQLAlchemy method="multi" bằng các phiên bản được liệt kê, sau đó theo dõi cách xử lý tham số trong databricks-sqlalchemy và databricks-sql-connector. So sánh hành vi của tham số có tên và tham số inline. Được xem là hoàn tất khi các thao tác ghi nhiều hàng không còn thất bại ở giới hạn 256 tham số và workaround hiện có để ghi từng hàng không còn cần thiết.
Do mô hình lập chỉ mục viết ra từ nội dung của issue.
Mô tả
This is a duplicate of https://github.com/databricks/databricks-sql-python/issues/300 . I am reposting it in this repo, as it still occurs with current versions of databricks-sqlalchemy~=1.0 (1.0.5) and databricks-sql-connector (4.0.2) together with the DBR 15.4 LTS ML versions of pandas (1.5.3) and sqlalchemy (1.4.39).
Problem
When using the multi-row insert mode of sqlalchemy, currently each value gets its own named parameter in the generated query:
INSERT INTO default.test_table (numeric_col, string_col)
VALUES
(:numeric_col_m0, :string_col_m0),
(:numeric_col_m1, :string_col_m1),
(:numeric_col_m2, :string_col_m2),
(:numeric_col_m3, :string_col_m3),
[...]
(:numeric_col_m998, :string_col_m998),
(:numeric_col_m999, :string_col_m999)
with parameters:
{
'numeric_col_m0': 0,
'string_col_m0': 'AAA',
'numeric_col_m1': 1,
'string_col_m1': 'AAA',
'numeric_col_m2',
[...],
'numeric_col_m999': 999,
'string_col_m999': 'AAA'
}
Consequently, even very small tables exceed the limit of 256 named parameters:
sqlalchemy.exc.OperationalError: (databricks.sql.exc.RequestError) Error during request to server: BAD_REQUEST: Parameterized query has too many parameters: 2000 parameters were given but the limit is 256.. BAD_REQUEST: Parameterized query has too many parameters: 2000 parameters were given but the limit is 256.
Example
import pandas as pd
from sqlalchemy import create_engine
sqlalchemy_connection_string = f"databricks://token:{token}@{host}?http_path={http_path}?catalog={catalog}"
engine = create_engine(sqlalchemy_connection_string)
test_data = pd.DataFrame({"numeric_col": range(1_000), "string_col": ["AAA"] * 1_000})
test_data.to_sql("test_table", engine, if_exists="replace", index=False, method="multi")
Workarounds
Inline Parameters
I tried to avoid this issue by using legacy inline parameters. However, I get a different error then:
sqlalchemy_connection_string = f"databricks://token:{token}@{host}?http_path={http_path}?catalog={catalog}"
engine = create_engine(sqlalchemy_connection_string, connect_args={"use_inline_params": "silent"})
test_data = pd.DataFrame({"numeric_col": range(1_000), "string_col": ["AAA"] * 1_000})
test_data.to_sql("test_table", engine, if_exists="replace", index=False, method="multi")
DatabaseError: (databricks.sql.exc.ServerOperationError) [UNBOUND_SQL_PARAMETER] Found the unbound parameter: numeric_col_m0. Please, fix `args` and provide a mapping of the parameter to either a SQL literal or collection constructor functions such as `map()`, `array()`, `struct()`. SQLSTATE: 42P02; line 1 pos 57
Writing row-by-row
The only workaround at the moment seems to be to insert the data row-by-row by not setting method in the to_sql() method.
However, this is prohibitively slow even for medium-sized data frames:
- Ngôn ngữ chính
- Python
- Star
- 24
- Fork
- 20
- Chỉ số merge pull request
- Không có pull request nào được merge trong 30 ngày
Chuẩn bị môi trường
- Không có Dockerfile hay tệp Docker Compose
- Không có mẫu pull request
- Đọc hướng dẫn đóng góp
Bắt đầu từ đâu
- Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
- Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
- Fork repository và làm thay đổi trên một nhánh.
- Mở pull request có tham chiếu số hiệu của issue.
Issue khác của databricks/databricks-sqlalchemy
-
get_columns() missing @reflection.cache causes a warehouse round-trip on every reflection callCó thể đã có người làm @TangoEnSkai đã nhận 48 ngày trước. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 92/100
-
get_foreign_keys() missing @reflection.cache causes excessive DESCRIBE TABLE EXTENDED queriesCó thể đã có người làm @TangoEnSkai đã nhận 48 ngày trước. Đang mở
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 90/100
-
TypeError when using Enum columns with the Databricks dialect (all 2.0.x releases)Có thể đã có người làm @TangoEnSkai đã nhận 48 ngày trước. Đang mở
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 70/100
-
Độ khó 3/5 1-2 ngày Mức phù hợp với người mới 64/100
databricks/databricks-sqlalchemy#77 · 2 bình luận ·
-
Unable to insert Python lists into ARRAY<STRING> columns using pandas to_sqlCó thể đã có người làm @aminghadersohi đã nhận 14 ngày trước. Đang mở
Độ khó 4/5 3-5 ngày Mức phù hợp với người mới 48/100
databricks/databricks-sqlalchemy#73 · 1 bình luận ·
Tất cả issue của databricks/databricks-sqlalchemy
Issue tương tự
-
enhancement good first issue
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 78/100
-
Update Python support to 3.15Đang mởpython-version
Độ khó 1/5 Dưới một giờ Mức phù hợp với người mới 88/100
-
bug
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
Maintainer thường phản hồi trong vòng 1 ngày
-
bug javascript P2-medium python release:v3.1
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 68/100
adrirubio/claude-deck#546 ·
Maintainer thường phản hồi trong vòng 1 ngày
-
area: desktop area: website priority: P2 type: feature
Độ khó 2/5 1-3 giờ Mức phù hợp với người mới 62/100
appandflow/stim#3411 · 1 bình luận ·
Maintainer thường phản hồi trong vòng 1 ngày