Skip to content

Commit 7cec2d3

Browse files
sfc-gh-zlisfc-gh-pczajka
authored andcommitted
SNOW-1934035 add support for file (#2177)
1 parent a81b1f9 commit 7cec2d3

File tree

2 files changed

+43
-0
lines changed

2 files changed

+43
-0
lines changed

src/snowflake/connector/constants.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -182,6 +182,9 @@ def struct_pa_type(metadata: ResultMetadataV2) -> DataType:
182182
),
183183
FieldType(name="VECTOR", dbapi_type=[DBAPI_TYPE_BINARY], pa_type=vector_pa_type),
184184
FieldType(name="MAP", dbapi_type=[DBAPI_TYPE_BINARY], pa_type=map_pa_type),
185+
FieldType(
186+
name="FILE", dbapi_type=[DBAPI_TYPE_STRING], pa_type=lambda _: pa.string()
187+
),
185188
)
186189

187190
FIELD_NAME_TO_ID: DefaultDict[Any, int] = defaultdict(int)

test/integ/test_cursor.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -724,6 +724,46 @@ def test_geometry(conn_cnx):
724724
assert row in expected_data
725725

726726

727+
@pytest.mark.skipolddriver
728+
def test_file(conn_cnx):
729+
"""Variant including JSON object."""
730+
name_file = random_string(5, "test_file_")
731+
with conn_cnx(
732+
session_parameters={
733+
"ENABLE_FILE_DATA_TYPE": True,
734+
},
735+
) as cnx:
736+
with cnx.cursor() as cur:
737+
cur.execute(
738+
f"create temporary table {name_file} as select "
739+
f"TO_FILE(OBJECT_CONSTRUCT('RELATIVE_PATH', 'some_new_file.jpeg', 'STAGE', '@myStage', "
740+
f"'STAGE_FILE_URL', 'some_new_file.jpeg', 'SIZE', 123, 'ETAG', 'xxx', 'CONTENT_TYPE', 'image/jpeg', "
741+
f"'LAST_MODIFIED', '2025-01-01')) as file_col"
742+
)
743+
744+
expected_data = [
745+
{
746+
"RELATIVE_PATH": "some_new_file.jpeg",
747+
"STAGE": "@myStage",
748+
"STAGE_FILE_URL": "some_new_file.jpeg",
749+
"SIZE": 123,
750+
"ETAG": "xxx",
751+
"CONTENT_TYPE": "image/jpeg",
752+
"LAST_MODIFIED": "2025-01-01",
753+
}
754+
]
755+
756+
with cnx.cursor() as cur:
757+
# Test with FILE return type
758+
result = cur.execute(f"select * from {name_file}")
759+
for metadata in [cur.description, cur._description_internal]:
760+
assert FIELD_ID_TO_NAME[metadata[0].type_code] == "FILE"
761+
data = result.fetchall()
762+
for raw_data in data:
763+
row = json.loads(raw_data[0])
764+
assert row in expected_data
765+
766+
727767
@pytest.mark.skipolddriver
728768
def test_vector(conn_cnx, is_public_test):
729769
if is_public_test:

0 commit comments

Comments
 (0)