Skip to content

Commit

Permalink
Merge pull request #13 from ValentinHerrmann/support-limit
Browse files Browse the repository at this point in the history
Support LIMIT clause in SQL queries
  • Loading branch information
ValentinHerrmann authored Jan 22, 2025
2 parents 040b6d9 + 8aa1272 commit 577f274
Show file tree
Hide file tree
Showing 6 changed files with 28 additions and 27 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,6 @@ A library that normalizes simple SQL queries and compares them first by equality
- FROM: one or more table from DB; no queries as tables!
- WHERE: single conditions; no Paranthesises!
- GROUP BY one or more columns

### V 0.2.1
- Support LIMIT clause
6 changes: 5 additions & 1 deletion sql_testing_tools/Helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ def normalizeSQLQuery(query, baseDict):

formatted_query = []

# Second pass to process SELECT, WHERE, GROUP BY, and ORDER BY clauses
# Second pass to process SELECT, WHERE, GROUP BY, ORDER BY, and LIMIT clauses
for token in parsed.tokens:
if token.is_whitespace:
continue
Expand All @@ -53,6 +53,8 @@ def normalizeSQLQuery(query, baseDict):
formatted_query.append('GROUP BY')
elif token.ttype is Keyword and token.value.upper() == 'ORDER BY':
formatted_query.append('ORDER BY')
elif token.ttype is Keyword and token.value.upper() == 'LIMIT':
formatted_query.append('LIMIT')
elif (isinstance(token, IdentifierList) or isinstance(token, Identifier)) and formatted_query and formatted_query[-1] == 'FROM':
formatted_query.append(Tp._from(token, alias_map, baseDict))
elif (isinstance(token, IdentifierList) or isinstance(token, Identifier)) and formatted_query and formatted_query[-1] == 'GROUP BY':
Expand All @@ -66,6 +68,8 @@ def normalizeSQLQuery(query, baseDict):
if isinstance(token, Function):
token = IdentifierList([token])
formatted_query.append(Tp._select(token, alias_map, baseDict))
elif formatted_query and formatted_query[-1] == 'LIMIT':
formatted_query.append(Tp._limit(token))
else:
formatted_query.append(str(token))

Expand Down
28 changes: 2 additions & 26 deletions sql_testing_tools/TokenProcessing.py
Original file line number Diff line number Diff line change
Expand Up @@ -341,29 +341,5 @@ def _where_twoConditionsOnTopLevel(where, alias_map, baseDict: dict, keyword, is
return keyword.join(current_condition)




# def _where_xx(where, alias_map, baseDict: dict):
# where_tokens = []
# for token in where.tokens:

# if isinstance(token, Comparison):
# left, operator, right = [t for t in token.tokens if not t.is_whitespace]
# left = _identifier(left, alias_map, baseDict)
# right = _identifier(right, alias_map, baseDict)
# if left.lower() >= right.lower():
# left, right = right, left
# if operator.value == ">":
# operator = "<"
# elif operator.value == "<":
# operator = ">"

# where_tokens.append(f"{left.lower()} {operator} {right.lower()}")
# elif token.ttype is Keyword:
# where_tokens.append(token.value.upper())
# elif token.is_whitespace or (token.ttype is Keyword and token.value.upper() == "WHERE"):
# continue
# else:
# where_tokens.append(str(token))
# return " ".join(where_tokens)

def _limit(limit):
return str(limit).strip()
12 changes: 12 additions & 0 deletions sql_testing_tools/tests/NormalizeQuery_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -198,3 +198,15 @@ def test_a15_ORDER_BY(self):
self.fail("\n" + q2 + "\n" + q2)
if q1 != q2:
self.fail("\n" + q1 + "\n" + q2)

def test_a16_LIMIT(self):
nr = '16'
td = Ba.getTableDict()
q1 = He.normalizeSQLQuery(self.readFile("sql_testing_tools/tests/v1/a"+nr+".sql"),td)
q2 = He.normalizeSQLQuery(self.readFile("sql_testing_tools/tests/v2/a"+nr+".sql"),td)
if q1 != q1:
self.fail("\n" + q1 + "\n" + q1)
if q2 != q2:
self.fail("\n" + q2 + "\n" + q2)
if q1 != q2:
self.fail("\n" + q1 + "\n" + q2)
3 changes: 3 additions & 0 deletions sql_testing_tools/tests/v1/a16.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT name, einwohner_m, einwohner_w
FROM Gemeinde
LIMIT 10
3 changes: 3 additions & 0 deletions sql_testing_tools/tests/v2/a16.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
SELECT name, einwohner_m, einwohner_w
FROM Gemeinde
LIMIT 10

0 comments on commit 577f274

Please sign in to comment.