Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

mongdb查询的结果集中缺少字段 #1192

Merged
merged 1 commit into from
Oct 17, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions sql/engines/mongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -825,6 +825,8 @@ def parse_tuple(self, cursor, db_name, tb_name, projection=None):
result = self.get_all_columns_by_tb(db_name=db_name, tb_name=tb_name)
columns = result.rows
columns.insert(0, "mongodballdata") # 隐藏JSON结果列
columns = self.fill_query_columns(cursor, columns)

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这里应该遗漏了前面insert的mongodballdata

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fill_query_columns方法会解析出cursor(查询结果)中所有包含的字段,检查是否在columns中,如果不在就添加至columns。所以不会遗漏mongodballdata字段

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

mongodballdata是为了前台展示json增加的虚拟列,不存在于查询结果中

Copy link
Contributor Author

@cpzt cpzt Oct 17, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fill_query_columns方法只会往columns添加字段,不会影响原来的columns(mongodballdata还在columns中);可以看一下测试用例test_fill_query_columns

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

抱歉,看到变量重新赋值,没注意到

for ro in cursor:
json_col = json.dumps(ro, ensure_ascii=False, indent=2, separators=(",", ":"))
row.insert(0, json_col)
Expand All @@ -851,3 +853,13 @@ def parse_tuple(self, cursor, db_name, tb_name, projection=None):
rows.append(tuple(row))
row.clear()
return tuple(rows), columns

@staticmethod
def fill_query_columns(cursor, columns):
"""补充结果集中`get_all_columns_by_tb`未获取的字段"""
cols = columns
for ro in cursor:
for key in ro.keys():
if key not in cols:
cols.append(key)
return cols
8 changes: 8 additions & 0 deletions sql/engines/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -1653,3 +1653,11 @@ def test_execute(self, mock_get_master, mock_exec_cmd):
check_result = self.engine.execute("some_db", sql)
mock_get_master.assert_called_once()
self.assertEqual(check_result.rows[0].__dict__["errlevel"], 0)

def test_fill_query_columns(self):
columns = ["_id", "title", "tags", "likes"]
cursor = [{"_id": {"$oid": "5f10162029684728e70045ab"}, "title": "MongoDB", "text": "archery", "likes": 100},
{"_id": {"$oid": "7f10162029684728e70045ab"}, "author": "archery"}]
cols = self.engine.fill_query_columns(cursor, columns=columns)
print(cols)
self.assertEqual(cols, ["_id", "title", "tags", "likes", "text", "author"])