Skip to content

Commit 24956ff

Browse files
committed
do not convert left and right joins to cross joins when joining on true
1 parent 9aefd19 commit 24956ff

File tree

3 files changed

+102
-15
lines changed

3 files changed

+102
-15
lines changed

enginetest/join_op_tests.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2046,6 +2046,9 @@ WHERE
20462046
},
20472047
},
20482048
},
2049+
{
2050+
name: "joining with subquery on empty table",
2051+
},
20492052
}
20502053

20512054
var rangeJoinOpTests = []JoinOpTests{

enginetest/memory_engine_test.go

Lines changed: 91 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -200,38 +200,116 @@ func TestSingleQueryPrepared(t *testing.T) {
200200

201201
// Convenience test for debugging a single query. Unskip and set to the desired query.
202202
func TestSingleScript(t *testing.T) {
203-
t.Skip()
203+
// t.Skip()
204204
var scripts = []queries.ScriptTest{
205205
{
206-
Name: "AS OF propagates to nested CALLs",
207-
SetUpScript: []string{},
206+
Name: "hash join with % condition",
207+
SetUpScript: []string{
208+
"create table t1(c0 int)",
209+
"create table t2(c0 int)",
210+
"insert into t1 values (1),(2),(3)",
211+
"insert into t2 values (4),(5),(6)",
212+
},
208213
Assertions: []queries.ScriptTestAssertion{
209214
{
210-
Query: "create procedure create_proc() create table t (i int primary key, j int);",
215+
Query: `select /*+ HASH_JOIN(t1, t2) */ * from t1 join t2 on t1.c0 % 3 = t2.c0 % 3`,
211216
Expected: []sql.Row{
212-
{types.NewOkResult(0)},
217+
{1, 4},
218+
{2, 5},
219+
{3, 6},
213220
},
214221
},
222+
},
223+
},
224+
{
225+
Skip: true,
226+
Assertions: []queries.ScriptTestAssertion{
215227
{
216-
Query: "call create_proc()",
228+
// test cross join used as projected subquery expression
229+
Query: "select 1 as exprAlias, 2, 3, (select exprAlias + count(*) from one_pk_three_idx a cross join one_pk_three_idx b);",
230+
Expected: []sql.Row{{1, 2, 3, 65}},
231+
},
232+
{
233+
Query: "SELECT count(*), (SELECT i FROM mytable WHERE i = 1 group by i);",
217234
Expected: []sql.Row{
218-
{types.NewOkResult(0)},
235+
{1, 1},
219236
},
220237
},
221238
},
222239
},
240+
{
241+
Skip: true,
242+
Name: "strings cast to numbers in log",
243+
SetUpScript: []string{},
244+
Assertions: []queries.ScriptTestAssertion{
245+
{
246+
Query: "select (11 + '11asdf')",
247+
Expected: []sql.Row{{float64(22)}},
248+
},
249+
{
250+
Skip: true,
251+
Query: "select log('10asdf', '100f')",
252+
Expected: []sql.Row{{float64(2)}},
253+
},
254+
{
255+
Skip: true,
256+
Query: "select log('a10asdf', 'b100f')",
257+
Expected: []sql.Row{{nil}},
258+
},
259+
},
260+
},
261+
{
262+
Skip: true,
263+
Name: "strings cast to numbers",
264+
SetUpScript: []string{
265+
//"create table test01(pk varchar(20) primary key)",
266+
"create table test01(pk varchar(30))",
267+
`insert into test01 values (' 3 12 4'),
268+
(' 3.2 12 4'),('-3.1234'),('-3.1a'),('-5+8'),('+3.1234'),
269+
('11d'),('11wha?'),('11'),('12'),('1a1'),('a1a1'),('11-5'),
270+
('3. 12 4'),('5.932887e+07'),('5.932887e+07abc'),('5.932887e7'),('5.932887e7abc')`,
271+
},
272+
Assertions: []queries.ScriptTestAssertion{
273+
{
274+
Skip: true,
275+
Query: "select pk from test01 where pk=11 order by pk",
276+
Expected: []sql.Row{{"11"}, {"11-5"}, {"11d"}, {"11wha?"}},
277+
},
278+
{
279+
Query: "select pk from test01 where pk in (11) order by pk",
280+
Expected: []sql.Row{{"11"}, {"11-5"}, {"11d"}, {"11wha?"}},
281+
},
282+
{
283+
Skip: true,
284+
Query: "select pk from test01 where pk in ('11')",
285+
Expected: []sql.Row{{"11"}},
286+
},
287+
{
288+
Skip: true,
289+
Query: "select log('10asdf', '100f')",
290+
Expected: []sql.Row{{float64(2)}},
291+
},
292+
{
293+
Skip: true,
294+
Query: "select log('a10asdf', 'b100f')",
295+
Expected: []sql.Row{{nil}},
296+
},
297+
},
298+
},
223299
}
224300

225301
for _, test := range scripts {
226302
harness := enginetest.NewMemoryHarness("", 1, testNumPartitions, true, nil)
227-
//harness.UseServer()
303+
harness.Setup(setup.SimpleSetup...)
304+
// harness.UseServer()
228305
engine, err := harness.NewEngine(t)
306+
// engine.EngineAnalyzer().Coster = memo.NewLookupBiasedCoster()
229307
if err != nil {
230308
panic(err)
231309
}
232310

233-
//engine.EngineAnalyzer().Debug = true
234-
//engine.EngineAnalyzer().Verbose = true
311+
engine.EngineAnalyzer().Debug = true
312+
engine.EngineAnalyzer().Verbose = true
235313

236314
enginetest.TestScriptWithEngine(t, engine, harness, test)
237315
}
@@ -922,10 +1000,6 @@ func TestVectorFunctions(t *testing.T) {
9221000
enginetest.TestVectorFunctions(t, enginetest.NewDefaultMemoryHarness())
9231001
}
9241002

925-
func TestVectorType(t *testing.T) {
926-
enginetest.TestVectorType(t, enginetest.NewDefaultMemoryHarness())
927-
}
928-
9291003
func TestIndexPrefix(t *testing.T) {
9301004
enginetest.TestIndexPrefix(t, enginetest.NewDefaultMemoryHarness())
9311005
}
@@ -1001,6 +1075,9 @@ func TestDatabaseCollationWire(t *testing.T) {
10011075
}
10021076

10031077
func TestTypesOverWire(t *testing.T) {
1078+
if _, ok := os.LookupEnv("CI_TEST"); !ok {
1079+
// t.Skip("Skipping test that requires CI_TEST=true")
1080+
}
10041081
harness := enginetest.NewDefaultMemoryHarness()
10051082
enginetest.TestTypesOverWire(t, harness, harness.SessionBuilder())
10061083
}

sql/planbuilder/from.go

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,13 @@ func (b *Builder) isUsingJoin(te *ast.JoinTableExpr) bool {
7878
strings.EqualFold(te.Join, ast.NaturalRightJoinStr)
7979
}
8080

81+
func (b *Builder) canConvertToCrossJoin(te *ast.JoinTableExpr) bool {
82+
return !strings.EqualFold(te.Join, ast.LeftJoinStr) &&
83+
!strings.EqualFold(te.Join, ast.RightJoinStr) &&
84+
(te.Condition.On == nil || te.Condition.On == ast.BoolVal(true)) &&
85+
te.Condition.Using == nil
86+
}
87+
8188
func (b *Builder) buildJoin(inScope *scope, te *ast.JoinTableExpr) (outScope *scope) {
8289
b.qFlags.Set(sql.QFlagInnerJoin)
8390

@@ -101,7 +108,7 @@ func (b *Builder) buildJoin(inScope *scope, te *ast.JoinTableExpr) (outScope *sc
101108
outScope.appendColumnsFromScope(rightScope)
102109

103110
// cross join
104-
if (te.Condition.On == nil || te.Condition.On == ast.BoolVal(true)) && te.Condition.Using == nil {
111+
if b.canConvertToCrossJoin(te) {
105112
if rast, ok := te.RightExpr.(*ast.AliasedTableExpr); ok && rast.Lateral {
106113
var err error
107114
outScope.node, err = b.f.buildJoin(leftScope.node, rightScope.node, plan.JoinTypeLateralCross, expression.NewLiteral(true, types.Boolean))

0 commit comments

Comments
 (0)