Skip to content

Commit

Permalink
test cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
aspicer committed Oct 25, 2024
1 parent 82b4d63 commit 18d0d53
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 18 deletions.
34 changes: 34 additions & 0 deletions posthog/hogql/test/_test_parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
Dict,
VariableDeclaration,
SelectSetNode,
SelectSetQuery,
)

from posthog.hogql.parser import parse_program
Expand Down Expand Up @@ -1394,6 +1395,39 @@ def test_select_union_all(self):
),
)

def test_nested_selects(self):
self.assertEqual(
self._select("(select 1 intersect select 2) union all (select 3 except select 4)"),
SelectSetQuery(
initial_select_query=SelectSetQuery(
initial_select_query=SelectQuery(select=[Constant(value=1)]),
subsequent_select_queries=[
SelectSetNode(
select_query=SelectQuery(
select=[Constant(value=2)],
),
set_operator="INTERSECT",
)
],
),
subsequent_select_queries=[
SelectSetNode(
select_query=SelectSetQuery(
initial_select_query=SelectQuery(
select=[Constant(value=3)],
),
subsequent_select_queries=[
SelectSetNode(
select_query=SelectQuery(select=[Constant(value=4)]), set_operator="EXCEPT"
)
],
),
set_operator="UNION ALL",
)
],
),
)

def test_sample_clause(self):
self.assertEqual(
self._select("select 1 from events sample 1/10 offset 999"),
Expand Down
28 changes: 10 additions & 18 deletions posthog/hogql/test/test_printer.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,15 +115,15 @@ def test_to_printed_hogql(self):
)

def test_intersect(self):
expr = parse_select("""select 1 as id intersect select 2 as id""", backend="python")
expr = parse_select("""select 1 as id intersect select 2 as id""")
response = to_printed_hogql(expr, self.team)
self.assertEqual(
response,
f"SELECT\n 1 AS id\nLIMIT 50000\nINTERSECT\nSELECT\n 2 AS id\nLIMIT {MAX_SELECT_RETURNED_ROWS}",
)

def test_except(self):
expr = parse_select("""select 1 as id except select 2 as id""", backend="python")
expr = parse_select("""select 1 as id except select 2 as id""")
response = to_printed_hogql(expr, self.team)
self.assertEqual(
response,
Expand All @@ -132,13 +132,10 @@ def test_except(self):

# these share the same priority, should stay in order
def test_except_and_union(self):
exprs = [
parse_select("""select 1 as id except select 2 as id union all select 3 as id""", backend=backend)
for backend in ("python", "cpp")
]
responses = [to_printed_hogql(expr, self.team) for expr in exprs]
expr = parse_select("""select 1 as id except select 2 as id union all select 3 as id""")
response = to_printed_hogql(expr, self.team)
self.assertEqual(
responses[0],
response,
(
"SELECT\n"
" 1 AS id\n"
Expand All @@ -153,16 +150,12 @@ def test_except_and_union(self):
"LIMIT 50000"
),
)
self.assertEqual(responses[0], responses[1])

def test_union_and_except(self):
exprs = [
parse_select("""select 1 as id union all select 2 as id except select 3 as id""", backend=backend)
for backend in ("python", "cpp")
]
responses = [to_printed_hogql(expr, self.team) for expr in exprs]
expr = parse_select("""select 1 as id union all select 2 as id except select 3 as id""")
response = to_printed_hogql(expr, self.team)
self.assertEqual(
responses[0],
response,
(
"SELECT\n"
" 1 AS id\n"
Expand All @@ -177,10 +170,9 @@ def test_union_and_except(self):
"LIMIT 50000"
),
)
self.assertEqual(responses[0], responses[1])

def test_intersect3(self):
expr = parse_select("""select 1 as id intersect select 2 as id intersect select 3 as id""", backend="python")
expr = parse_select("""select 1 as id intersect select 2 as id intersect select 3 as id""")
response = to_printed_hogql(expr, self.team)
self.assertEqual(
response,
Expand All @@ -198,7 +190,7 @@ def test_intersect3(self):
)

def test_union3(self):
expr = parse_select("""select 1 as id union all select 2 as id union all select 3 as id""", backend="python")
expr = parse_select("""select 1 as id union all select 2 as id union all select 3 as id""")
response = to_printed_hogql(expr, self.team)
self.assertEqual(
response,
Expand Down

0 comments on commit 18d0d53

Please sign in to comment.