From 00969de7b2773eac1d12948da0f4c6bdb5a8241e Mon Sep 17 00:00:00 2001 From: Kyle Lahnakoski Date: Thu, 24 Oct 2024 20:04:45 -0400 Subject: [PATCH] nested joins --- tests/test_big_sql.py | 26 +++++++++++++++++++++++++- tests/test_null.py | 2 -- tests/test_postgres.py | 35 ++++++++++++++++++++++------------- tests/test_resources.py | 2 -- tests/test_snowflake.py | 1 - tests/test_sqlglot.py | 2 -- tests/test_v9.py | 2 -- 7 files changed, 47 insertions(+), 23 deletions(-) diff --git a/tests/test_big_sql.py b/tests/test_big_sql.py index b77861c..e33052e 100644 --- a/tests/test_big_sql.py +++ b/tests/test_big_sql.py @@ -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 @@ -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 diff --git a/tests/test_null.py b/tests/test_null.py index 1f7b359..944c5d2 100644 --- a/tests/test_null.py +++ b/tests/test_null.py @@ -9,8 +9,6 @@ from unittest import TestCase -from mo_parsing.debug import Debugger - from mo_sql_parsing import parse try: diff --git a/tests/test_postgres.py b/tests/test_postgres.py index 45930a6..33933ce 100644 --- a/tests/test_postgres.py +++ b/tests/test_postgres.py @@ -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 @@ -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) @@ -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) diff --git a/tests/test_resources.py b/tests/test_resources.py index 715cead..8cbe99e 100644 --- a/tests/test_resources.py +++ b/tests/test_resources.py @@ -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" diff --git a/tests/test_snowflake.py b/tests/test_snowflake.py index 4f546dc..d400da7 100644 --- a/tests/test_snowflake.py +++ b/tests/test_snowflake.py @@ -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 diff --git a/tests/test_sqlglot.py b/tests/test_sqlglot.py index 2638815..dc3b59f 100644 --- a/tests/test_sqlglot.py +++ b/tests/test_sqlglot.py @@ -13,8 +13,6 @@ from unittest import skip, TestCase -from mo_parsing.debug import Debugger - from mo_sql_parsing import parse diff --git a/tests/test_v9.py b/tests/test_v9.py index 7d33aa1..26d5e93 100644 --- a/tests/test_v9.py +++ b/tests/test_v9.py @@ -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