Skip to content

Commit 4bac622

Browse files
authored
py: raise error if dataframe has no column names (#1826)
Fixes: #1782 Signed-off-by: Abhinav Gyawali <22275402+abhizer@users.noreply.github.com>
1 parent 168810c commit 4bac622

File tree

3 files changed

+30
-0
lines changed

3 files changed

+30
-0
lines changed

python/feldera/_helpers.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,20 @@
11
import pandas as pd
22

33

4+
def ensure_dataframe_has_columns(df: pd.DataFrame):
5+
"""
6+
Ensures that the DataFrame has column names set.
7+
"""
8+
9+
if [v for v in range(df.shape[1])] == list(df.columns):
10+
raise ValueError(
11+
"""
12+
DataFrame has no column names set.
13+
Input DataFrame must have column names set and they must be consistent with the columns in the input table.
14+
"""
15+
)
16+
17+
418
def dataframe_from_response(buffer: list[list[dict]]):
519
"""
620
Converts the response from Feldera to a pandas DataFrame.

python/feldera/sql_context.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
from feldera.sql_schema import SQLSchema
1515
from feldera.output_handler import OutputHandler
1616
from feldera._callback_runner import CallbackRunner, _CallbackRunnerInstruction
17+
from feldera._helpers import ensure_dataframe_has_columns
1718
from enum import Enum
1819

1920

@@ -226,6 +227,8 @@ def connect_source_pandas(self, table_name: str, df: pandas.DataFrame):
226227
:param df: The pandas DataFrame to be pushed to the pipeline.
227228
"""
228229

230+
ensure_dataframe_has_columns(df)
231+
229232
tbl = self.tables.get(table_name)
230233

231234
if tbl:

python/tests/test_wireframes.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,19 @@ def callback(df: pd.DataFrame, seq_no: int):
104104

105105
sql.run_to_completion()
106106

107+
def test_df_without_columns(self):
108+
109+
sql = SQLContext('df_without_columns', TEST_CLIENT).get_or_create()
110+
TBL_NAME = "student"
111+
112+
df = pd.DataFrame([(1, "a"), (2, "b"), (3, "c")])
113+
114+
sql.register_table(TBL_NAME, SQLSchema({"id": "INT", "name": "STRING"}))
115+
sql.register_view("s", f"SELECT * FROM {TBL_NAME}")
116+
117+
with self.assertRaises(ValueError):
118+
sql.connect_source_pandas(TBL_NAME, df)
119+
107120

108121
if __name__ == '__main__':
109122
unittest.main()

0 commit comments

Comments
 (0)