Skip to content

Commit 20ea69c

Browse files
committed
Add cross join test
1 parent 9eada12 commit 20ea69c

File tree

2 files changed

+239
-1
lines changed

2 files changed

+239
-1
lines changed

tests/converter.test.js

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import {Converter} from "../src/converter";
22
import {test, expect} from "@jest/globals";
3-
import {complex_ast} from './ast'
3+
import {complex_ast} from './ast';
4+
import {cross_join_ast} from './crossjoin';
45

56

67
function getQueryBuilder(ast) {
@@ -25,3 +26,16 @@ test('complex sql', () => {
2526
->orderBy('posts.created_at','desc')
2627
->get();`);
2728
});
29+
30+
test('cross join', () => {
31+
expect(getQueryBuilder(cross_join_ast)).toBe(`DB::table('posts')
32+
->crossJoinSub(function ($query) {
33+
\t$query->from('posts')
34+
\t\t->select('count', DB::raw("'max'(created_date) as created_date"))
35+
\t\t->groupBy('count');
36+
},'max_counts')
37+
->select('posts.*')
38+
->where('posts.count','=','max_counts.count')
39+
->where('posts.created_date','=','max_counts.created_date')
40+
->get();`);
41+
});

tests/crossjoin.js

Lines changed: 224 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,224 @@
1+
// select posts.* from posts,
2+
// (select count,max(created_date) as created_date
3+
// from posts
4+
// group by count) max_counts
5+
// where posts.count=max_counts.count
6+
// and posts.created_date=max_counts.created_date
7+
let cross_join_ast = [
8+
{
9+
"Query": {
10+
"with": null,
11+
"body": {
12+
"Select": {
13+
"distinct": false,
14+
"top": null,
15+
"projection": [
16+
{
17+
"QualifiedWildcard": [
18+
{
19+
"value": "posts",
20+
"quote_style": null
21+
}
22+
]
23+
}
24+
],
25+
"from": [
26+
{
27+
"relation": {
28+
"Table": {
29+
"name": [
30+
{
31+
"value": "posts",
32+
"quote_style": null
33+
}
34+
],
35+
"alias": null,
36+
"args": [],
37+
"with_hints": []
38+
}
39+
},
40+
"joins": []
41+
},
42+
{
43+
"relation": {
44+
"Derived": {
45+
"lateral": false,
46+
"subquery": {
47+
"with": null,
48+
"body": {
49+
"Select": {
50+
"distinct": false,
51+
"top": null,
52+
"projection": [
53+
{
54+
"UnnamedExpr": {
55+
"Identifier": {
56+
"value": "count",
57+
"quote_style": null
58+
}
59+
}
60+
},
61+
{
62+
"ExprWithAlias": {
63+
"expr": {
64+
"Function": {
65+
"name": [
66+
{
67+
"value": "max",
68+
"quote_style": null
69+
}
70+
],
71+
"args": [
72+
{
73+
"Unnamed": {
74+
"Expr": {
75+
"Identifier": {
76+
"value": "created_date",
77+
"quote_style": null
78+
}
79+
}
80+
}
81+
}
82+
],
83+
"over": null,
84+
"distinct": false
85+
}
86+
},
87+
"alias": {
88+
"value": "created_date",
89+
"quote_style": null
90+
}
91+
}
92+
}
93+
],
94+
"from": [
95+
{
96+
"relation": {
97+
"Table": {
98+
"name": [
99+
{
100+
"value": "posts",
101+
"quote_style": null
102+
}
103+
],
104+
"alias": null,
105+
"args": [],
106+
"with_hints": []
107+
}
108+
},
109+
"joins": []
110+
}
111+
],
112+
"lateral_views": [],
113+
"selection": null,
114+
"group_by": [
115+
{
116+
"Identifier": {
117+
"value": "count",
118+
"quote_style": null
119+
}
120+
}
121+
],
122+
"cluster_by": [],
123+
"distribute_by": [],
124+
"sort_by": [],
125+
"having": null
126+
}
127+
},
128+
"order_by": [],
129+
"limit": null,
130+
"offset": null,
131+
"fetch": null
132+
},
133+
"alias": {
134+
"name": {
135+
"value": "max_counts",
136+
"quote_style": null
137+
},
138+
"columns": []
139+
}
140+
}
141+
},
142+
"joins": []
143+
}
144+
],
145+
"lateral_views": [],
146+
"selection": {
147+
"BinaryOp": {
148+
"left": {
149+
"BinaryOp": {
150+
"left": {
151+
"CompoundIdentifier": [
152+
{
153+
"value": "posts",
154+
"quote_style": null
155+
},
156+
{
157+
"value": "count",
158+
"quote_style": null
159+
}
160+
]
161+
},
162+
"op": "Eq",
163+
"right": {
164+
"CompoundIdentifier": [
165+
{
166+
"value": "max_counts",
167+
"quote_style": null
168+
},
169+
{
170+
"value": "count",
171+
"quote_style": null
172+
}
173+
]
174+
}
175+
}
176+
},
177+
"op": "And",
178+
"right": {
179+
"BinaryOp": {
180+
"left": {
181+
"CompoundIdentifier": [
182+
{
183+
"value": "posts",
184+
"quote_style": null
185+
},
186+
{
187+
"value": "created_date",
188+
"quote_style": null
189+
}
190+
]
191+
},
192+
"op": "Eq",
193+
"right": {
194+
"CompoundIdentifier": [
195+
{
196+
"value": "max_counts",
197+
"quote_style": null
198+
},
199+
{
200+
"value": "created_date",
201+
"quote_style": null
202+
}
203+
]
204+
}
205+
}
206+
}
207+
}
208+
},
209+
"group_by": [],
210+
"cluster_by": [],
211+
"distribute_by": [],
212+
"sort_by": [],
213+
"having": null
214+
}
215+
},
216+
"order_by": [],
217+
"limit": null,
218+
"offset": null,
219+
"fetch": null
220+
}
221+
}
222+
];
223+
224+
export {cross_join_ast};

0 commit comments

Comments
 (0)