Skip to content

Commit

Permalink
nested joins
Browse files Browse the repository at this point in the history
  • Loading branch information
klahnakoski committed Oct 25, 2024
1 parent 94eefca commit 00969de
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 23 deletions.
26 changes: 25 additions & 1 deletion tests/test_big_sql.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@


import json
from unittest import TestCase
from unittest import TestCase, skip

from mo_json import value2json
from mo_times import Timer
Expand All @@ -17,6 +17,30 @@


class TestBigSql(TestCase):
@skip("too slow")
def test_large_expression(self):
# too slow because the faster() look-ahead can not use regexs
# specifically, the use of a number in front of an identifier without space
# with scale_* => 0.9sec
# without scale_* => 1.1sec
#
# also, extra checks for prefix operators
#
# snagged from sqlglot under the mit license
# https://github.com/tobymao/sqlglot/blob/6294f9e6d08111c6088f5ed9846e7a64f7724801/benchmarks/bench.py#L61
crazy = "SELECT 1+"
crazy += "+".join(str(i) for i in range(500))
crazy += " AS a, 2*"
crazy += "*".join(str(i) for i in range(500))
crazy += " AS b FROM x"
try:
result = parse("select 1")
except Exception:
pass
with Timer("parse long expression"):
result = parse(crazy)


def test_issue_103b(self):
# 0 1 2 3 4 5 6 7 8 9
# 012345678901234567890123456789012345678901234567890123456789012345678901234567890123456789
Expand Down
2 changes: 0 additions & 2 deletions tests/test_null.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,6 @@

from unittest import TestCase

from mo_parsing.debug import Debugger

from mo_sql_parsing import parse

try:
Expand Down
35 changes: 22 additions & 13 deletions tests/test_postgres.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@

from unittest import TestCase

from mo_parsing.debug import Debugger
from mo_testing import assertAlmostEqual

from mo_sql_parsing import parse

Expand Down Expand Up @@ -553,23 +553,13 @@ def test_issue_248_regex_operator1(self):

def test_issue_248_regex_operator2(self):
sql = """SELECT 'abc' ~* 'abc'"""
try:
result = parse(sql)
except Exception:
pass
with Debugger():
result = parse(sql)
result = parse(sql)
expected = {"select": {"value": {"regexp": [{"literal": "abc"}, {"literal": "abc"}], "ignore_case": True}}}
self.assertEqual(result, expected)

def test_issue_248_regex_operator3(self):
sql = """SELECT 'abc' !~ 'abc'"""
try:
result = parse(sql)
except Exception:
pass
with Debugger():
result = parse(sql)
result = parse(sql)

expected = {"select": {"value": {"not_regexp": [{"literal": "abc"}, {"literal": "abc"}]}}}
self.assertEqual(result, expected)
Expand All @@ -579,3 +569,22 @@ def test_issue_248_regex_operator4(self):
result = parse(sql)
expected = {"select": {"value": {"not_regexp": [{"literal": "abc"}, {"literal": "abc"}], "ignore_case": True}}}
self.assertEqual(result, expected)


def test_issue_253_joins(self):
sql = """select t1.col1, t2.col2, t3.col3
from table1 t1
join table2 t2
left join table3 t3
on t3.id = t2.id
on t1.id = t2.id"""
result = parse(sql)
expected = {
"from": [
{"name": "t1", "value": "table1"},
{"join": {'name': 't2', "value": "table2"}, "on": {"eq": ["t1.id", "t2.id"]}},
{"left join": {'name': 't3', "value": "table3"}, "on": {"eq": ["t3.id", "t2.id"]}}
],
"select": [{"value": "t1.col1"}, {"value": "t2.col2"}, {"value": "t3.col3"}]
}
assertAlmostEqual(result, expected)
2 changes: 0 additions & 2 deletions tests/test_resources.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,6 @@
import os
from unittest import TestCase

from mo_parsing.debug import Debugger

from mo_sql_parsing import parse

IS_MASTER = os.environ.get("TRAVIS_BRANCH") == "master"
Expand Down
1 change: 0 additions & 1 deletion tests/test_snowflake.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@

from unittest import TestCase

from mo_parsing.debug import Debugger
from mo_testing.fuzzytestcase import add_error_reporting

from mo_sql_parsing import parse, normal_op
Expand Down
2 changes: 0 additions & 2 deletions tests/test_sqlglot.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@

from unittest import skip, TestCase

from mo_parsing.debug import Debugger

from mo_sql_parsing import parse


Expand Down
2 changes: 0 additions & 2 deletions tests/test_v9.py
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
from unittest import TestCase

from mo_logs import logger
from mo_parsing.debug import Debugger
from mo_testing.fuzzytestcase import add_error_reporting

from mo_sql_parsing import parse as _parse, format

from tests.test_format_and_parse import EXCEPTION_MESSAGE


Expand Down

0 comments on commit 00969de

Please sign in to comment.