@@ -8505,6 +8505,172 @@ where
85058505 },
85068506 },
85078507 },
8508+ {
8509+ // https://github.com/dolthub/dolt/issues/8728
8510+ Name : "test merge join optimization (removing sort node over indexed tables) does not break ordering" ,
8511+ SetUpScript : []string {
8512+ "create table t1 (i int primary key);" ,
8513+ "create table t2 (j int primary key);" ,
8514+ "insert into t1 values (1), (2), (3);" ,
8515+ "insert into t2 values (2), (3), (4);" ,
8516+
8517+ "create table t3 (i int, j int, primary key (i, j));" ,
8518+ "create table t4 (x int, y int, primary key (x, y));" ,
8519+ "insert into t3 values (1, 1), (1, 2), (2, 2), (3, 3);" ,
8520+ "insert into t4 values (2, 2), (3, 3), (4, 4);" ,
8521+ },
8522+ Assertions : []ScriptTestAssertion {
8523+ {
8524+ Query : "select /*+ MERGE_JOIN(t1, t2) */ * from t1 join t2 on t1.i = t2.j order by t1.i;" ,
8525+ Expected : []sql.Row {
8526+ {2 , 2 },
8527+ {3 , 3 },
8528+ },
8529+ },
8530+ {
8531+ Query : "select /*+ MERGE_JOIN(t1, t2) */ * from t1 join t2 on t1.i = t2.j order by t2.j;" ,
8532+ Expected : []sql.Row {
8533+ {2 , 2 },
8534+ {3 , 3 },
8535+ },
8536+ },
8537+ {
8538+ Query : "select /*+ MERGE_JOIN(t1, t2) */ * from t1 join t2 on t1.i = t2.j order by t1.i desc;" ,
8539+ Expected : []sql.Row {
8540+ {3 , 3 },
8541+ {2 , 2 },
8542+ },
8543+ },
8544+ {
8545+ Query : "select /*+ MERGE_JOIN(t1, t2) */ * from t1 join t2 on t1.i = t2.j order by t2.j desc;" ,
8546+ Expected : []sql.Row {
8547+ {3 , 3 },
8548+ {2 , 2 },
8549+ },
8550+ },
8551+ {
8552+ // InSubquery expressions can be optimized into MERGE_JOINs, so this optimization should apply over this as well
8553+ Query : "select /*+ MERGE_JOIN(t1, t2) */ * from t1 where ((i in (select j from t2 where j > 2))) order by i desc;" ,
8554+ Expected : []sql.Row {
8555+ {3 },
8556+ },
8557+ },
8558+
8559+ {
8560+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t3.i;" ,
8561+ Expected : []sql.Row {
8562+ {2 , 2 , 2 , 2 },
8563+ {3 , 3 , 3 , 3 },
8564+ },
8565+ },
8566+ {
8567+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t3.i desc;" ,
8568+ Expected : []sql.Row {
8569+ {3 , 3 , 3 , 3 },
8570+ {2 , 2 , 2 , 2 },
8571+ },
8572+ },
8573+ {
8574+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t4.x;" ,
8575+ Expected : []sql.Row {
8576+ {2 , 2 , 2 , 2 },
8577+ {3 , 3 , 3 , 3 },
8578+ },
8579+ },
8580+ {
8581+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t4.x desc;" ,
8582+ Expected : []sql.Row {
8583+ {3 , 3 , 3 , 3 },
8584+ {2 , 2 , 2 , 2 },
8585+ },
8586+ },
8587+ {
8588+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t3.i, t3.j;" ,
8589+ Expected : []sql.Row {
8590+ {2 , 2 , 2 , 2 },
8591+ {3 , 3 , 3 , 3 },
8592+ },
8593+ },
8594+ {
8595+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t3.i desc, t3.j desc;" ,
8596+ Expected : []sql.Row {
8597+ {3 , 3 , 3 , 3 },
8598+ {2 , 2 , 2 , 2 },
8599+ },
8600+ },
8601+ {
8602+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t4.x, t4.y;" ,
8603+ Expected : []sql.Row {
8604+ {2 , 2 , 2 , 2 },
8605+ {3 , 3 , 3 , 3 },
8606+ },
8607+ },
8608+ {
8609+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t4.x desc, t4.y desc;" ,
8610+ Expected : []sql.Row {
8611+ {3 , 3 , 3 , 3 },
8612+ {2 , 2 , 2 , 2 },
8613+ },
8614+ },
8615+ {
8616+ // The Sort node can be optimized out of this query, but currently is not
8617+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t3.i, t4.x;" ,
8618+ Expected : []sql.Row {
8619+ {2 , 2 , 2 , 2 },
8620+ {3 , 3 , 3 , 3 },
8621+ },
8622+ },
8623+ {
8624+ // The Sort node can be optimized out of this query, but currently is not
8625+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t3.i, t4.x desc;" ,
8626+ Expected : []sql.Row {
8627+ {2 , 2 , 2 , 2 },
8628+ {3 , 3 , 3 , 3 },
8629+ },
8630+ },
8631+ {
8632+ // The Sort node can be optimized out of this query, but currently is not
8633+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t3.i, t3.j, t4.x;" ,
8634+ Expected : []sql.Row {
8635+ {2 , 2 , 2 , 2 },
8636+ {3 , 3 , 3 , 3 },
8637+ },
8638+ },
8639+ {
8640+ // The Sort node can be optimized out of this query, but currently is not
8641+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t3.i, t3.j, t4.x, t4.y;" ,
8642+ Expected : []sql.Row {
8643+ {2 , 2 , 2 , 2 },
8644+ {3 , 3 , 3 , 3 },
8645+ },
8646+ },
8647+
8648+ {
8649+ // Sort node cannot be optimized out of this query
8650+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t3.j;" ,
8651+ Expected : []sql.Row {
8652+ {2 , 2 , 2 , 2 },
8653+ {3 , 3 , 3 , 3 },
8654+ },
8655+ },
8656+ {
8657+ // Sort node cannot be optimized out of this query
8658+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t4.y;" ,
8659+ Expected : []sql.Row {
8660+ {2 , 2 , 2 , 2 },
8661+ {3 , 3 , 3 , 3 },
8662+ },
8663+ },
8664+ {
8665+ // Sort node cannot be optimized out of this query
8666+ Query : "select /*+ MERGE_JOIN(t3, t4) */ * from t3 join t4 on t3.i = t4.x order by t3.i, t3.j desc;" ,
8667+ Expected : []sql.Row {
8668+ {2 , 2 , 2 , 2 },
8669+ {3 , 3 , 3 , 3 },
8670+ },
8671+ },
8672+ },
8673+ },
85088674
85098675 // Char tests
85108676 {
@@ -9189,7 +9355,7 @@ where
91899355 },
91909356 },
91919357 {
9192- Name : "enum conversion to strings " ,
9358+ Name : "enum conversions " ,
91939359 Dialect : "mysql" ,
91949360 SetUpScript : []string {
91959361 "create table t (e enum('abc', 'defg', 'hijkl'));" ,
@@ -9325,6 +9491,38 @@ where
93259491 {"abc" },
93269492 },
93279493 },
9494+ {
9495+ Query : "select e, cast(e as unsigned) from t order by e;" ,
9496+ Expected : []sql.Row {
9497+ {"abc" , uint64 (1 )},
9498+ {"defg" , uint64 (2 )},
9499+ {"hijkl" , uint64 (3 )},
9500+ },
9501+ },
9502+ {
9503+ Query : "select e, cast(e as decimal) from t order by e;" ,
9504+ Expected : []sql.Row {
9505+ {"abc" , "1" },
9506+ {"defg" , "2" },
9507+ {"hijkl" , "3" },
9508+ },
9509+ },
9510+ {
9511+ Query : "select e, cast(e as float) from t order by e;" ,
9512+ Expected : []sql.Row {
9513+ {"abc" , float32 (1 )},
9514+ {"defg" , float32 (2 )},
9515+ {"hijkl" , float32 (3 )},
9516+ },
9517+ },
9518+ {
9519+ Query : "select e, cast(e as double) from t order by e;" ,
9520+ Expected : []sql.Row {
9521+ {"abc" , float64 (1 )},
9522+ {"defg" , float64 (2 )},
9523+ {"hijkl" , float64 (3 )},
9524+ },
9525+ },
93289526 },
93299527 },
93309528 {
@@ -9787,7 +9985,7 @@ where
97879985 },
97889986 },
97899987 {
9790- Name : "set conversion to strings " ,
9988+ Name : "set conversions " ,
97919989 Dialect : "mysql" ,
97929990 SetUpScript : []string {
97939991 "create table t (s set('abc', 'defg', 'hijkl'));" ,
@@ -9911,25 +10109,59 @@ where
991110109 },
991210110 },
991310111 {
9914- // https://github.com/dolthub/dolt/issues/9511
9915- Skip : true ,
991610112 Query : "select s, cast(s as char) from t order by s;" ,
991710113 Expected : []sql.Row {
991810114 {"abc" , "abc" },
10115+ {"defg" , "defg" },
991910116 {"abc,defg" , "abc,defg" },
992010117 {"abc,defg,hijkl" , "abc,defg,hijkl" },
992110118 },
992210119 },
992310120 {
9924- // https://github.com/dolthub/dolt/issues/9511
9925- Skip : true ,
992610121 Query : "select s, cast(s as binary) from t order by s;" ,
992710122 Expected : []sql.Row {
992810123 {"abc" , []uint8 ("abc" )},
10124+ {"defg" , []uint8 ("defg" )},
992910125 {"abc,defg" , []uint8 ("abc,defg" )},
993010126 {"abc,defg,hijkl" , []uint8 ("abc,defg,hijkl" )},
993110127 },
993210128 },
10129+ {
10130+ Query : "select s, cast(s as unsigned) from t order by s;" ,
10131+ Expected : []sql.Row {
10132+ {"abc" , uint64 (1 )},
10133+ {"defg" , uint64 (2 )},
10134+ {"abc,defg" , uint64 (3 )},
10135+ {"abc,defg,hijkl" , uint64 (7 )},
10136+ },
10137+ },
10138+ {
10139+ Query : "select s, cast(s as decimal) from t order by s;" ,
10140+ Expected : []sql.Row {
10141+ {"abc" , "1" },
10142+ {"defg" , "2" },
10143+ {"abc,defg" , "3" },
10144+ {"abc,defg,hijkl" , "7" },
10145+ },
10146+ },
10147+ {
10148+ Query : "select s, cast(s as float) from t order by s;" ,
10149+ Expected : []sql.Row {
10150+ {"abc" , float32 (1 )},
10151+ {"defg" , float32 (2 )},
10152+ {"abc,defg" , float32 (3 )},
10153+ {"abc,defg,hijkl" , float32 (7 )},
10154+ },
10155+ },
10156+ {
10157+ Query : "select s, cast(s as double) from t order by s;" ,
10158+ Expected : []sql.Row {
10159+ {"abc" , float64 (1 )},
10160+ {"defg" , float64 (2 )},
10161+ {"abc,defg" , float64 (3 )},
10162+ {"abc,defg,hijkl" , float64 (7 )},
10163+ },
10164+ },
993310165 },
993410166 },
993510167 {
0 commit comments