Skip to content

Commit 76e8709

Browse files
authored
Merge pull request #504 from wudidapaopao/fix_multi_refer_df
Fix crash on multi refer Python(df)
2 parents fe7989a + 7a9e6db commit 76e8709

File tree

2 files changed

+28
-1
lines changed

2 files changed

+28
-1
lines changed

programs/local/StoragePython.cpp

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,21 @@ void StoragePython::prepareColumnCache(
151151
#endif
152152

153153
auto & data_source = data_source_wrapper->getDataSource();
154-
if (column_cache == nullptr)
154+
155+
bool need_rebuild = (column_cache == nullptr) || (column_cache->size() != names.size());
156+
if (!need_rebuild)
157+
{
158+
for (size_t i = 0; i < names.size(); ++i)
159+
{
160+
if ((*column_cache)[i].name != names[i])
161+
{
162+
need_rebuild = true;
163+
break;
164+
}
165+
}
166+
}
167+
168+
if (need_rebuild)
155169
{
156170
column_cache = std::make_shared<PyColumnVec>(names.size());
157171
for (size_t i = 0; i < names.size(); ++i)

tests/test_query_py.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -400,6 +400,19 @@ def test_parquet_row_number_order(self):
400400
finally:
401401
os.unlink(path)
402402

403+
def test_multiple_python_references_in_subquery(self):
404+
df = pd.DataFrame({'id': [1, 2, 3], 'value': [10, 20, 30]})
405+
406+
result = chdb.query('''
407+
SELECT id, value + (SELECT SUM(value) FROM Python(df)) AS total
408+
FROM Python(df)
409+
ORDER BY id
410+
''', "DataFrame")
411+
412+
self.assertEqual(len(result), 3)
413+
self.assertEqual(result['id'].tolist(), [1, 2, 3])
414+
self.assertEqual(result['total'].tolist(), [70, 80, 90])
415+
403416

404417
if __name__ == "__main__":
405418
unittest.main(verbosity=3)

0 commit comments

Comments
 (0)