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

Fix bug in match_node #51

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
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
2 changes: 1 addition & 1 deletion core/query_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,7 +138,7 @@ def match_node(query_node: Any, rule_node: Any, rule: dict, memo: dict) -> bool:
# handle case when query_node = {'all_columns': {}} and rule_node = {"value": "V001"}
# we want "V001" to match "all_columns"
#
if QueryRewriter.is_var(rule_node['value']) and not QueryRewriter.is_list(query_node) and 'all_columns' in query_node.keys():
if QueryRewriter.is_var(rule_node['value']) and QueryRewriter.is_dict(query_node) and 'all_columns' in query_node.keys():
memo[rule_node['value']] = list(query_node.keys())[0]
return True

Expand Down
11 changes: 11 additions & 0 deletions data/rules.py
Original file line number Diff line number Diff line change
Expand Up @@ -392,6 +392,17 @@
# 'mapping': "{\"x\": \"V1\", \"y\": \"V2\"}",
'database': 'mysql'
},

{
'id': 103,
'key': 'stackoverflow_1',
'name': 'Stackoverflow 1',
'pattern': 'SELECT DISTINCT <<x2>> FROM <<x1>> WHERE <<y1>>',
'constraints': '',
'rewrite': 'SELECT <<x2>> FROM <<x1>> WHERE <<y1>> GROUP BY <<x2>>',
'actions': '',
'database': 'postgresql'
},
]

# fetch one rule by key (json attributes are in json)
Expand Down
27 changes: 27 additions & 0 deletions tests/test_query_rewriter.py
Original file line number Diff line number Diff line change
Expand Up @@ -1052,6 +1052,33 @@ def test_rewrite_rule_query_rule_wetune_90():
assert format(parse(q1)) == format(parse(_q1))


def test_rewrite_stackoverflow_1():
q0 = '''
SELECT DISTINCT my_table.foo, your_table.boo
FROM my_table, your_table
WHERE my_table.num = 1 OR your_table.num = 2
'''
q1 = '''
SELECT
my_table.foo,
your_table.boo
FROM
my_table,
your_table
WHERE
my_table.num = 1
OR your_table.num = 2
GROUP BY
my_table.foo,
your_table.boo
'''
rule_keys = ['stackoverflow_1', 'remove_self_join']

rules = [get_rule(k) for k in rule_keys]
_q1, _rewrite_path = QueryRewriter.rewrite(q0, rules)
assert format(parse(q1)) == format(parse(_q1))


# TODO - TBI
#
def test_rewrite_postgresql():
Expand Down