Skip to content

Commit fe2e55e

Browse files
authored
Merge pull request #17 from zbrookle/parentheses
BUG: Fix issue where you can't perform algebra on the left side of an expression
2 parents 6f49e9f + 66bebf9 commit fe2e55e

4 files changed

Lines changed: 41 additions & 7 deletions

File tree

dataframe_sql/grammar/sql.grammar

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -104,12 +104,8 @@ not_in_expr: expression_math "NOT"i "IN"i "(" [expression_math ","] expression_m
104104
boolean: "true"i -> true
105105
| "false"i -> false
106106
?number_expr: product
107-
| number_expr "+" product -> add
108-
| number_expr "-" product -> sub
109107

110108
?product: NUMBER
111-
| product "*" NUMBER -> mul
112-
| product "/" NUMBER -> div
113109

114110
integer: /[1-9][0-9]*/
115111
STAR: "*"

dataframe_sql/sql_select_query.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,7 @@ def __init__(self, sql: str, show_exececution_plan: bool = False):
123123
def parse_sql(self):
124124
try:
125125
tree = self.parser.parse(self.sql)
126+
print(tree.pretty())
126127

127128
table_info = TableInfo()
128129

dataframe_sql/tests/pandas_sql_functionality_test.py

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1394,9 +1394,45 @@ def test_sql_data_types():
13941394
tm.assert_frame_equal(pandas_frame, my_frame)
13951395

13961396

1397+
def test_order_of_operations_no_parens():
1398+
"""
1399+
Test math parentheses
1400+
:return:
1401+
"""
1402+
1403+
my_frame = query("select 20 * avocado_id + 3 / 20 as my_math from avocado")
1404+
1405+
pandas_frame = AVOCADO.copy()[["avocado_id"]]
1406+
pandas_frame["my_math"] = 20 * pandas_frame["avocado_id"] + 3 / 20
1407+
1408+
pandas_frame = pandas_frame.drop(columns=["avocado_id"])
1409+
1410+
tm.assert_frame_equal(pandas_frame, my_frame)
1411+
1412+
1413+
# def test_order_of_operations_with_parens():
1414+
# """
1415+
# Test math parentheses
1416+
# :return:
1417+
# """
1418+
#
1419+
# my_frame = query(
1420+
# "select 20 * (avocado_id + 3) / (20 + avocado_id) as my_math from " "avocado"
1421+
# )
1422+
#
1423+
# pandas_frame = AVOCADO.copy()[["avocado_id"]]
1424+
# pandas_frame["my_math"] = (
1425+
# 20 * (pandas_frame["avocado_id"] + 3) / (20 + pandas_frame["avocado_id"])
1426+
# )
1427+
#
1428+
# pandas_frame = pandas_frame.drop(columns=["avocado_id"])
1429+
#
1430+
# tm.assert_frame_equal(pandas_frame, my_frame)
1431+
1432+
13971433
if __name__ == "__main__":
13981434
register_env_tables()
13991435

1400-
test_sql_data_types()
1436+
# test_order_of_operations_with_parens()
14011437

14021438
remove_env_tables()

dataframe_sql/tests/sql_execution_plan_test.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ def test_using_math():
8282
"select temp, 1 + 2 * 3 as my_number from forest_fires",
8383
show_execution_plan=True,
8484
)
85-
assert plan == "FOREST_FIRES.loc[:, ['temp']].assign(my_number=7, )"
85+
86+
assert plan == "FOREST_FIRES.loc[:, ['temp']].assign(my_number=1 + 2 * 3)"
8687

8788

8889
def test_distinct():
@@ -1086,6 +1087,6 @@ def test_timestamps():
10861087
if __name__ == "__main__":
10871088
register_env_tables()
10881089

1089-
# test_rank_statement_one_column()
1090+
test_using_math()
10901091

10911092
remove_env_tables()

0 commit comments

Comments
 (0)