Skip to content

Commit 3d7b980

Browse files
fix issue with subtree generalization not applying to distinct on (#87)
1 parent ca3cde2 commit 3d7b980

File tree

2 files changed

+4
-4
lines changed

2 files changed

+4
-4
lines changed

core/rule_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1472,13 +1472,13 @@ def replaceSubtreesOfASTJson(astJson: Any, path: list, subtree: dict, var: str)
14721472
# if the subtree is the same as the given subtree
14731473
#
14741474
if RuleGenerator.sameSubtree(astJson, subtree):
1475-
# special case for 'select' list
1475+
# special case for 'select' or 'distinct_on' list
14761476
# e.g., for {'select': [{'value': 'V001.V002'}, {'value': 'V001.age'}, ...]}
14771477
# astJson = {'value': 'V001.V002'}
14781478
# subtree = {'value': 'V001.V002'}
14791479
# var = 'V005'
14801480
# we should return {'value': 'V005'}
1481-
if len(path) > 0 and path[-1] == 'select' and 'value'in astJson.keys():
1481+
if len(path) > 0 and path[-1] in ['select', 'distinct_on'] and 'value'in astJson.keys():
14821482
return {'value': var}
14831483
# otherwise
14841484
return var

tests/test_rule_generator.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2556,8 +2556,8 @@ def test_generate_spreadsheet_id_18():
25562556
assert type(rule) is dict
25572557

25582558
q0_rule, q1_rule = unify_variable_names(rule['pattern'], rule['rewrite'])
2559-
assert StringUtil.strim(RuleGenerator._fingerPrint(q0_rule)) == StringUtil.strim(RuleGenerator._fingerPrint("SELECT DISTINCT ON (<x1>) <x2>, <x3>, <x4>, COALESCE(<x5>.<x6>, <x7>), <<x8>> FROM <x9> LEFT JOIN <x5> ON <<x10>> LEFT JOIN <x11> ON <<x12>> WHERE <<x13>> AND <x11>.<x14> IN (<x15>, <x16>, <x17>, <x18>, <x19>, <x20>, <x21>) AND <<x22>> ORDER BY <x9>.<x23> DESC"))
2560-
assert StringUtil.strim(RuleGenerator._fingerPrint(q1_rule)) == StringUtil.strim(RuleGenerator._fingerPrint("SELECT <x2>, <x3>, <x1>, COALESCE((SELECT <x4>.<x5> FROM <x4> WHERE <<x9>> AND <<x21>> LIMIT <x15>), <x6>), (SELECT <<x7>> FROM <x10> WHERE <<x11>> AND <x10>.<x13> IN (<x14>, <x15>, <x16>, <x17>, <x18>, <x19>, <x20>) LIMIT <x15>) FROM <x8> WHERE <<x12>>"))
2559+
assert q0_rule == "SELECT DISTINCT ON (<x1>) <x2>, <x3>, <x1>, COALESCE(<x4>.<x5>, <x6>), <<x7>> FROM <x8> LEFT JOIN <x4> ON <<x9>> LEFT JOIN <x10> ON <<x11>> WHERE <<x12>> AND <x10>.<x13> IN (<x14>, <x15>, <x16>, <x17>, <x18>, <x19>, <x20>) AND <<x21>> ORDER BY <x8>.<x22> DESC"
2560+
assert q1_rule == "SELECT <x2>, <x3>, <x1>, COALESCE((SELECT <x4>.<x5> FROM <x4> WHERE <<x9>> AND <<x21>> LIMIT <x15>), <x6>), (SELECT <<x7>> FROM <x10> WHERE <<x11>> AND <x10>.<x13> IN (<x14>, <x15>, <x16>, <x17>, <x18>, <x19>, <x20>) LIMIT <x15>) FROM <x8> WHERE <<x12>>"
25612561

25622562
def test_generate_spreadsheet_id_20():
25632563
q0 = """SELECT * FROM (SELECT * FROM (SELECT NULL FROM EMP) WHERE N IS NULL) WHERE N IS NULL"""

0 commit comments

Comments
 (0)