-
Notifications
You must be signed in to change notification settings - Fork 10
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use rowtype for sf metadata in arrow schema
- Loading branch information
Showing
3 changed files
with
49 additions
and
56 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,67 +1,43 @@ | ||
from typing import Any | ||
|
||
import pyarrow as pa | ||
|
||
from fakesnow.types import ColumnInfo | ||
|
||
|
||
def with_sf_metadata(schema: pa.Schema, rowtype: list[ColumnInfo]) -> pa.Schema: | ||
# expected by the snowflake connector | ||
# uses rowtype to populate metadata, rather than the arrow schema type, for consistency with | ||
# rowtype returned in the response | ||
|
||
assert len(schema) == len(rowtype), f"schema and rowtype must be same length but f{len(schema)=} f{len(rowtype)=}" | ||
|
||
def with_sf_metadata(schema: pa.Schema) -> pa.Schema: | ||
# see https://github.com/snowflakedb/snowflake-connector-python/blob/e9393a6/src/snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowTableIterator.cpp#L32 | ||
# and https://github.com/snowflakedb/snowflake-connector-python/blob/e9393a6/src/snowflake/connector/nanoarrow_cpp/ArrowIterator/SnowflakeType.cpp#L10 | ||
fms = [] | ||
for i, t in enumerate(schema.types): | ||
f = schema.field(i) | ||
|
||
# TODO: precision, scale, charLength etc. for all types | ||
|
||
if t == pa.bool_(): | ||
fm = f.with_metadata({"logicalType": "BOOLEAN"}) | ||
elif t == pa.int64(): | ||
# scale and precision required, see here | ||
# https://github.com/snowflakedb/snowflake-connector-python/blob/416ff57/src/snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowChunkIterator.cpp#L147 | ||
fm = f.with_metadata({"logicalType": "FIXED", "precision": "38", "scale": "0"}) | ||
elif t == pa.float64(): | ||
fm = f.with_metadata({"logicalType": "REAL"}) | ||
elif isinstance(t, pa.Decimal128Type): | ||
fm = f.with_metadata({"logicalType": "FIXED", "precision": str(t.precision), "scale": str(t.scale)}) | ||
elif t == pa.string(): | ||
# TODO: set charLength to size of column | ||
fm = f.with_metadata({"logicalType": "TEXT", "charLength": "16777216"}) | ||
else: | ||
raise NotImplementedError(f"Unsupported Arrow type: {t}") | ||
fms.append(fm) | ||
fms = [ | ||
schema.field(i).with_metadata( | ||
{ | ||
"logicalType": c["type"].upper(), | ||
# required for FIXED type see | ||
# https://github.com/snowflakedb/snowflake-connector-python/blob/416ff57/src/snowflake/connector/nanoarrow_cpp/ArrowIterator/CArrowChunkIterator.cpp#L147 | ||
"precision": str(c["precision"] or 38), | ||
"scale": str(c["scale"] or 0), | ||
"charLength": str(c["length"] or 0), | ||
} | ||
) | ||
for i, c in enumerate(rowtype) | ||
] | ||
return pa.schema(fms) | ||
|
||
|
||
def to_ipc(table: pa.Table) -> pa.Buffer: | ||
def to_ipc(table: pa.Table, rowtype: list[ColumnInfo]) -> pa.Buffer: | ||
batches = table.to_batches() | ||
if len(batches) != 1: | ||
raise NotImplementedError(f"{len(batches)} batches") | ||
batch = batches[0] | ||
|
||
sink = pa.BufferOutputStream() | ||
|
||
with pa.ipc.new_stream(sink, with_sf_metadata(table.schema)) as writer: | ||
with pa.ipc.new_stream(sink, with_sf_metadata(table.schema, rowtype)) as writer: | ||
writer.write_batch(batch) | ||
|
||
return sink.getvalue() | ||
|
||
|
||
# TODO: should this be derived before with_schema? | ||
def to_rowtype(schema: pa.Schema) -> list[dict[str, Any]]: | ||
return [ | ||
{ | ||
"name": f.name, | ||
# TODO | ||
# "database": "", | ||
# "schema": "", | ||
# "table": "", | ||
"nullable": f.nullable, | ||
"type": f.metadata.get(b"logicalType").decode("utf-8").lower(), # type: ignore | ||
# TODO | ||
# "byteLength": 20, | ||
"length": int(f.metadata.get(b"charLength")) if f.metadata.get(b"charLength") else None, # type: ignore | ||
"scale": int(f.metadata.get(b"scale")) if f.metadata.get(b"scale") else None, # type: ignore | ||
"precision": int(f.metadata.get(b"precision")) if f.metadata.get(b"precision") else None, # type: ignore | ||
"collation": None, | ||
} | ||
for f in schema | ||
] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters