Skip to content

Commit 9777c54

Browse files
authored
Fix samples (#91)
* Keep rows in cursor * Add samples * Check if rows is not None
1 parent 6a66a51 commit 9777c54

File tree

2 files changed

+20
-10
lines changed

2 files changed

+20
-10
lines changed

src/sodaspark/scan.py

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,7 @@ class Cursor:
2929

3030
def __init__(self) -> None:
3131
self._df: DataFrame | None = None
32+
self._rows: list[Row] | None = None
3233

3334
def __enter__(self) -> Cursor:
3435
return self
@@ -84,6 +85,7 @@ def close(self) -> None:
8485
https://github.com/mkleehammer/pyodbc/wiki/Cursor#close
8586
"""
8687
self._df = None
88+
self._rows = None
8789

8890
def execute(self, sql: str, *parameters: Any) -> None:
8991
"""
@@ -112,24 +114,22 @@ def execute(self, sql: str, *parameters: Any) -> None:
112114
spark_session = SparkSession.builder.getOrCreate()
113115
self._df = spark_session.sql(sql)
114116

115-
def fetchall(self) -> list[Row]:
117+
def fetchall(self) -> list[Row] | None:
116118
"""
117119
Fetch all data.
118120
119121
Returns
120122
-------
121-
out : list[Row]
123+
out : list[Row] | None
122124
The rows.
123125
124126
Source
125127
------
126128
https://github.com/mkleehammer/pyodbc/wiki/Cursor#fetchall
127129
"""
128-
if self._df is None:
129-
rows = list()
130-
else:
131-
rows = self._df.collect()
132-
return rows
130+
if self._rows is None and self._df is not None:
131+
self._rows = self._df.collect()
132+
return self._rows
133133

134134
def fetchone(self) -> Row | None:
135135
"""
@@ -144,10 +144,17 @@ def fetchone(self) -> Row | None:
144144
------
145145
https://github.com/mkleehammer/pyodbc/wiki/Cursor#fetchone
146146
"""
147-
if self._df is None:
148-
row = None
147+
if self._rows is None and self._df is not None:
148+
self._rows = self._df.collect()
149+
150+
if self._rows is not None:
151+
try:
152+
row = self._rows.pop(0)
153+
except IndexError:
154+
row = None
149155
else:
150-
row = self._df.first()
156+
row = None
157+
151158
return row
152159

153160

static/demodata.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
table_name: demodata
2+
samples:
3+
table_limit: 50
4+
failed_limit: 50
25
metrics:
36
- row_count
47
- missing_count

0 commit comments

Comments
 (0)