pacman82 / arrow-odbc

Fill Apache Arrow record batches from an ODBC data source in Rust.
MIT License
52 stars 10 forks source link

Panic with divide by zero on empty schema #81

Closed keraion closed 1 year ago

keraion commented 1 year ago

Since release of arrow_odbc 3.0.0 (arrow_odbc_py 2.0.0), when running a multi-statement query that doesn't generate a result set in the second or later statement now fails due to a division by zero error when calculating the buffer_size_in_rows. The first statement returns a result set to allow the Cursor to be created, and the statement handle still has more results; however, the calculation of the buffer size now panics.

import arrow_odbc as ao

reader = ao.read_arrow_batches_from_odbc(
    connection_string="Driver={DB2};"
    "Hostname=host;"
    "Port=50000;"
    "Protocol=TCPIP;"
    "Database=db2db;"
    "UID=user;"
    "PWD=password;",
    query="""
SELECT 1 
FROM SYSIBM.SYSDUMMY1
;

DECLARE GLOBAL TEMPORARY TABLE MY_DGTT AS ( 
    SELECT *, 5 AS FIVE
    FROM SYSIBM.SYSDUMMY1 
) 
WITH DATA 
ON COMMIT PRESERVE ROWS 
NOT LOGGED 
WITH REPLACE
;

SELECT * 
FROM SESSION.MY_DGTT
;
""",
)

while True:
    print("-------------------------")
    print(reader.schema or "No Schema")
    if reader.schema:
        print("-------------------------")
        for batch in reader:
            print(batch)
    if not reader.more_results():
        break

Error Message:

thread '<unnamed>' panicked at /root/.cargo/registry/src/index.crates.io-6f17d22bba15001f/arrow-odbc-3.0.0/src/reader/odbc_reader.rs:467:30:
attempt to divide by zero
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

Expected Results:

-------------------------
1: int32 not null
-------------------------
pyarrow.RecordBatch
1: int32 not null
----
1: [1]
-------------------------
No Schema
-------------------------
IBMREQD: string not null
FIVE: int32 not null
-------------------------
pyarrow.RecordBatch
IBMREQD: string not null
FIVE: int32 not null
----
IBMREQD: ["Y"]
FIVE: [5]
pacman82 commented 1 year ago

Thanks for your contribution! arrow-odbc 3.1.1 is released which has your fixed merged.

pacman82 commented 1 year ago

Oh, and also sorry for breaking your workflow in the first place.