Skip to content

Commit

Permalink
Merge pull request #874 from DalgoT4D/download-ui4t-parse-bug
Browse files Browse the repository at this point in the history
fixed parsing of csv file while downloading
  • Loading branch information
fatchat authored Oct 8, 2024
2 parents 66241f6 + 7404f8c commit 71c0fe9
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 6 deletions.
14 changes: 12 additions & 2 deletions ddpui/api/warehouse_api.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
import json
import csv
from io import StringIO
import sqlparse
from sqlparse.tokens import Keyword, Number, Token
import uuid
Expand Down Expand Up @@ -254,10 +256,18 @@ def stream_warehouse_data(
if not data:
break
if not header_written:
yield ",".join(data[0].keys()) + "\n" # Write CSV header
output = StringIO()
writer = csv.DictWriter(output, fieldnames=data[0].keys())
writer.writeheader()
yield output.getvalue()
header_written = True
output = StringIO()
writer = csv.DictWriter(output, fieldnames=data[0].keys())
for row in data:
yield ",".join(map(str, row.values())) + "\n" # Write CSV row
writer.writerow(row)
yield output.getvalue()
output.seek(0)
output.truncate(0)
page += 1
logger.info(f"Streaming page {page} of {schema_name}.{table_name}")

Expand Down
8 changes: 4 additions & 4 deletions ddpui/tests/api_tests/test_warehouse_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -180,10 +180,10 @@ def test_download_warehouse_data_success(orguser):
content = b"".join(response.streaming_content).decode("utf-8")

# Assertions to verify the streaming response content
assert "col1,col2\n" in content # Check header
assert "value1,value2\n" in content # Check first row
assert "value3,value4\n" in content # Check second row
assert "value5,value6\n" in content # Check second row
assert "col1,col2\r\n" in content # Check header
assert "value1,value2\r\n" in content # Check first row
assert "value3,value4\r\n" in content # Check second row
assert "value5,value6\r\n" in content # Check second row
assert content.count("\n") == 4


Expand Down

0 comments on commit 71c0fe9

Please sign in to comment.