rewrite_test.go 27.3 KB
Newer Older
martianzhang's avatar
martianzhang 已提交
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
/*
 * Copyright 2018 Xiaomi, Inc.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package ast

import (
	"fmt"
21
	"sort"
martianzhang's avatar
martianzhang 已提交
22 23 24 25 26 27
	"testing"

	"github.com/XiaoMi/soar/common"
)

func TestRewrite(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
28
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
29
	orgTestDSNStatus := common.Config.TestDSN.Disable
martianzhang's avatar
martianzhang 已提交
30 31 32 33
	common.Config.TestDSN.Disable = false
	testSQL := []map[string]string{
		{
			"input":  `SELECT * FROM film`,
martianzhang's avatar
martianzhang 已提交
34
			"output": `select film_id, title, description, release_year, language_id, original_language_id, rental_duration from film;`,
martianzhang's avatar
martianzhang 已提交
35 36 37
		},
		{
			"input":  `SELECT film.*, actor.actor_id FROM film,actor`,
martianzhang's avatar
martianzhang 已提交
38
			"output": `select film_id, title, description, release_year, language_id, original_language_id, rental_duration, actor.actor_id from film, actor;`,
martianzhang's avatar
martianzhang 已提交
39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101
		},
		{
			"input":  `insert into film values(1,2,3,4,5)`,
			"output": `insert into film(film_id, title, description, release_year, language_id) values (1, 2, 3, 4, 5);`,
		},
		{
			"input":  `insert into sakila.film values(1,2)`,
			"output": `insert into sakila.film(film_id, title) values (1, 2);`,
		},
		{
			"input":  `replace into sakila.film select id from tb`,
			"output": `replace into sakila.film(film_id) select id from tb;`,
		},
		{
			"input":  `replace into sakila.film select id, title, description from tb`,
			"output": `replace into sakila.film(film_id, title, description) select id, title, description from tb;`,
		},
		{
			"input":  `insert into film values(1,2,3,4,5)`,
			"output": `insert into film(film_id, title, description, release_year, language_id) values (1, 2, 3, 4, 5);`,
		},
		{
			"input":  `insert into sakila.film values(1,2)`,
			"output": `insert into sakila.film(film_id, title) values (1, 2);`,
		},
		{
			"input":  `replace into sakila.film select id from tb`,
			"output": `replace into sakila.film(film_id) select id from tb;`,
		},
		{
			"input":  `replace into sakila.film select id, title, description from tb`,
			"output": `replace into sakila.film(film_id, title, description) select id, title, description from tb;`,
		},
		{
			"input":  "DELETE FROM tbl WHERE col1=1 ORDER BY col",
			"output": "delete from tbl where col1 = 1;",
		},
		{
			"input":  "UPDATE tbl SET col =1 WHERE col1=1 ORDER BY col",
			"output": "update tbl set col = 1 where col1 = 1;",
		},
	}

	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"])
		rw.Columns = map[string]map[string][]*common.Column{
			"sakila": {
				"film": {
					{Name: "film_id", Table: "film"},
					{Name: "title", Table: "film"},
					{Name: "description", Table: "film"},
					{Name: "release_year", Table: "film"},
					{Name: "language_id", Table: "film"},
					{Name: "original_language_id", Table: "film"},
					{Name: "rental_duration", Table: "film"},
				},
			},
		}
		rw.Rewrite()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
102
	common.Config.TestDSN.Disable = orgTestDSNStatus
martianzhang's avatar
martianzhang 已提交
103
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
104 105 106
}

func TestRewriteStar2Columns(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
107
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
108
	orgTestDSNStatus := common.Config.TestDSN.Disable
martianzhang's avatar
martianzhang 已提交
109 110 111 112
	common.Config.TestDSN.Disable = false
	testSQL := []map[string]string{
		{
			"input":  `SELECT * FROM film`,
martianzhang's avatar
martianzhang 已提交
113
			"output": `select film_id, title from film`,
martianzhang's avatar
martianzhang 已提交
114 115
		},
		{
martianzhang's avatar
martianzhang 已提交
116 117
			"input":  `SELECT film.* FROM film`,
			"output": `select film_id, title from film`,
martianzhang's avatar
martianzhang 已提交
118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
		},
	}

	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"])
		rw.Columns = map[string]map[string][]*common.Column{
			"sakila": {
				"film": {
					{Name: "film_id", Table: "film"},
					{Name: "title", Table: "film"},
				},
			},
		}
		rw.RewriteStar2Columns()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165

	testSQL2 := []map[string]string{
		{
			"input":  `SELECT film.* FROM film, actor`,
			"output": `select film.film_id, film.title from film, actor`,
		},
		{
			"input":  `SELECT film.*, actor.actor_id FROM film, actor`,
			"output": `select film.film_id, film.title, actor.actor_id from film, actor`,
		},
	}

	for _, sql := range testSQL2 {
		rw := NewRewrite(sql["input"])
		rw.Columns = map[string]map[string][]*common.Column{
			"sakila": {
				"film": {
					{Name: "film_id", Table: "film"},
					{Name: "title", Table: "film"},
				},
				"actor": {
					{Name: "actor_id", Table: "actor"},
				},
			},
		}
		rw.RewriteStar2Columns()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
166
	common.Config.TestDSN.Disable = orgTestDSNStatus
martianzhang's avatar
martianzhang 已提交
167
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
168 169 170
}

func TestRewriteInsertColumns(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
171
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210
	testSQL := []map[string]string{
		{
			"input":  `insert into film values(1,2,3,4,5)`,
			"output": `insert into film(film_id, title, description, release_year, language_id) values (1, 2, 3, 4, 5)`,
		},
		{
			"input":  `insert into sakila.film values(1,2)`,
			"output": `insert into sakila.film(film_id, title) values (1, 2)`,
		},
		{
			"input":  `replace into sakila.film select id from tb`,
			"output": `replace into sakila.film(film_id) select id from tb`,
		},
		{
			"input":  `replace into sakila.film select id, title, description from tb`,
			"output": `replace into sakila.film(film_id, title, description) select id, title, description from tb`,
		},
	}

	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"])
		rw.Columns = map[string]map[string][]*common.Column{
			"sakila": {
				"film": {
					{Name: "film_id", Table: "film"},
					{Name: "title", Table: "film"},
					{Name: "description", Table: "film"},
					{Name: "release_year", Table: "film"},
					{Name: "language_id", Table: "film"},
					{Name: "original_language_id", Table: "film"},
					{Name: "rental_duration", Table: "film"},
				},
			},
		}
		rw.RewriteInsertColumns()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
211
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
212 213 214
}

func TestRewriteHaving(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
215
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235
	testSQL := []map[string]string{
		{
			"input":  `SELECT state, COUNT(*) FROM Drivers GROUP BY state HAVING state IN ('GA', 'TX') ORDER BY state`,
			"output": "select state, COUNT(*) from Drivers where state in ('GA', 'TX') group by state order by state asc",
		},
		{
			"input":  `SELECT state, COUNT(*) FROM Drivers WHERE col =1 GROUP BY state HAVING state IN ('GA', 'TX') ORDER BY state`,
			"output": "select state, COUNT(*) from Drivers where (col = 1) and state in ('GA', 'TX') group by state order by state asc",
		},
		{
			"input":  `SELECT state, COUNT(*) FROM Drivers WHERE col =1 or col1 =2 GROUP BY state HAVING state IN ('GA', 'TX') ORDER BY state`,
			"output": "select state, COUNT(*) from Drivers where (col = 1 or col1 = 2) and state in ('GA', 'TX') group by state order by state asc",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteHaving()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
236
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
237 238 239
}

func TestRewriteAddOrderByNull(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
240
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
241 242 243 244 245 246 247 248 249 250 251 252
	testSQL := []map[string]string{
		{
			"input":  "SELECT sum(col1) FROM tbl GROUP BY col",
			"output": "select sum(col1) from tbl group by col order by null",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteAddOrderByNull()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
253
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
254 255 256
}

func TestRewriteRemoveDMLOrderBy(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
257
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273
	testSQL := []map[string]string{
		{
			"input":  "DELETE FROM tbl WHERE col1=1 ORDER BY col",
			"output": "delete from tbl where col1 = 1",
		},
		{
			"input":  "UPDATE tbl SET col =1 WHERE col1=1 ORDER BY col",
			"output": "update tbl set col = 1 where col1 = 1",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteRemoveDMLOrderBy()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
274
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
275 276 277
}

func TestRewriteGroupByConst(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
278
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304
	testSQL := []map[string]string{
		{
			"input":  "select 1;",
			"output": "select 1 from dual",
		},
		/*
				{
					"input":  "SELECT col1 FROM tbl GROUP BY 1;",
					"output": "select col1 from tbl GROUP BY col1",
				},
			    {
					"input":  "SELECT col1, col2 FROM tbl GROUP BY 1, 2;",
					"output": "select col1, col2 from tbl GROUP BY col1, col2",
				},
			    {
					"input":  "SELECT col1, col2, col3 FROM tbl GROUP BY 1, 3;",
					"output": "select col1, col2, col3 from tbl GROUP BY col1, col3",
				},
		*/
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteGroupByConst()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
305
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
306 307 308
}

func TestRewriteStandard(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
309
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
310 311 312 313 314 315 316 317 318 319 320 321
	testSQL := []map[string]string{
		{
			"input":  "SELECT sum(col1) FROM tbl GROUP BY 1;",
			"output": "select sum(col1) from tbl group by 1",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteStandard()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
322
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
323 324 325
}

func TestRewriteCountStar(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
326
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342
	testSQL := []map[string]string{
		{
			"input":  "SELECT count(col) FROM tbl GROUP BY 1;",
			"output": "select count(*) from tbl group by 1",
		},
		{
			"input":  "SELECT COUNT(tb.col) FROM tbl GROUP BY 1;",
			"output": "select COUNT(tb.*) from tbl group by 1",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteCountStar()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
343
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
344 345 346
}

func TestRewriteInnoDB(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
347
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363
	testSQL := []map[string]string{
		{
			"input":  "CREATE TABLE t1(id bigint(20) NOT NULL AUTO_INCREMENT);",
			"output": "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB ",
		},
		{
			"input":  "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=memory ",
			"output": "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB ",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteInnoDB()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
364
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
365 366 367
}

func TestRewriteAutoIncrement(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
368
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
	testSQL := []map[string]string{
		{
			"input":  "CREATE TABLE t1(id bigint(20) NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=123802;",
			"output": "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB auto_increment=1 ",
		},
		{
			"input":  "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB",
			"output": "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteAutoIncrement()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
385
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
386 387 388
}

func TestRewriteIntWidth(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
389
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
390 391 392
	testSQL := []map[string]string{
		{
			"input":  "CREATE TABLE t1(id bigint(10) NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=123802;",
martianzhang's avatar
martianzhang 已提交
393
			"output": "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB AUTO_INCREMENT=123802",
martianzhang's avatar
martianzhang 已提交
394 395 396
		},
		{
			"input":  "CREATE TABLE t1(id bigint NOT NULL AUTO_INCREMENT) ENGINE=InnoDB AUTO_INCREMENT=123802;",
martianzhang's avatar
martianzhang 已提交
397
			"output": "create table t1 (\n\tid bigint(20) not null auto_increment\n) ENGINE=InnoDB AUTO_INCREMENT=123802",
martianzhang's avatar
martianzhang 已提交
398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
		},
		{
			"input":  "create table t1(id int(20) not null auto_increment) ENGINE=InnoDB;",
			"output": "create table t1 (\n\tid int(10) not null auto_increment\n) ENGINE=InnoDB",
		},
		{
			"input":  "create table t1(id int not null auto_increment) ENGINE=InnoDB;",
			"output": "create table t1 (\n\tid int not null auto_increment\n) ENGINE=InnoDB",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteIntWidth()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
414
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
415 416 417
}

func TestRewriteAlwaysTrue(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
418
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435
	testSQL := []map[string]string{
		{
			"input":  "SELECT count(col) FROM tbl where 1=1;",
			"output": "select count(col) from tbl",
		},
		{
			"input":  "SELECT count(col) FROM tbl where col=col;",
			"output": "select count(col) from tbl where col = col",
		},
		{
			"input":  "SELECT count(col) FROM tbl where col=col2;",
			"output": "select count(col) from tbl where col = col2",
		},
		{
			"input":  "SELECT count(col) FROM tbl where 1>=1;",
			"output": "select count(col) from tbl",
		},
martianzhang's avatar
martianzhang 已提交
436 437 438 439
		{
			"input":  "SELECT count(col) FROM tbl where 2>1;",
			"output": "select count(col) from tbl",
		},
martianzhang's avatar
martianzhang 已提交
440 441 442 443
		{
			"input":  "SELECT count(col) FROM tbl where 1<=1;",
			"output": "select count(col) from tbl",
		},
martianzhang's avatar
martianzhang 已提交
444 445 446 447
		{
			"input":  "SELECT count(col) FROM tbl where 1<2;",
			"output": "select count(col) from tbl",
		},
martianzhang's avatar
martianzhang 已提交
448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471
		{
			"input":  "SELECT count(col) FROM tbl where 1=1 and 2=2;",
			"output": "select count(col) from tbl",
		},
		{
			"input":  "SELECT count(col) FROM tbl where 1=1 or 2=3;",
			"output": "select count(col) from tbl where 2 = 3",
		},
		{
			"input":  "SELECT count(col) FROM tbl where 1=1 and 3=3 or 2=3;",
			"output": "select count(col) from tbl where 2 = 3",
		},
		{
			"input":  "SELECT count(col) FROM tbl where 1=1 and 3=3 or 2!=3;",
			"output": "select count(col) from tbl",
		},
		{
			"input":  "SELECT count(col) FROM tbl where 1=1 or 2=3 and 3=3 ;",
			"output": "select count(col) from tbl where 2 = 3",
		},
		{
			"input":  "SELECT count(col) FROM tbl where (1=1);",
			"output": "select count(col) from tbl",
		},
martianzhang's avatar
martianzhang 已提交
472 473 474 475
		{
			"input":  "SELECT count(col) FROM tbl where a=1;",
			"output": "select count(col) from tbl where a = 1",
		},
martianzhang's avatar
martianzhang 已提交
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
		{
			"input":  "SELECT count(col) FROM tbl where ('a'= 'a' or 'b' = 'b') and a = 'b';",
			"output": "select count(col) from tbl where a = 'b'",
		},
		{
			"input":  "SELECT count(col) FROM tbl where (('a'= 'a' or 'b' = 'b') and a = 'b');",
			"output": "select count(col) from tbl where (a = 'b')",
		},
		{
			"input":  "SELECT count(col) FROM tbl where 'a'= 'a' or ('b' = 'b' and a = 'b');",
			"output": "select count(col) from tbl where (a = 'b')",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteAlwaysTrue()
		if rw == nil {
			t.Errorf("NoRw")
		} else if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
497
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
498 499 500 501
}

// TODO:
func TestRewriteSubQuery2Join(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
502
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
503
	orgTestDSNStatus := common.Config.TestDSN.Disable
martianzhang's avatar
martianzhang 已提交
504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528
	common.Config.TestDSN.Disable = true
	testSQL := []map[string]string{
		{
			// 这个case是官方文档给的,但不一定正确,需要视表结构的定义来进行判断
			"input":  `SELECT * FROM t1 WHERE id IN (SELECT id FROM t2);`,
			"output": "",
			//"output": `SELECT DISTINCT t1.* FROM t1, t2 WHERE t1.id=t2.id;`,
		},
		{
			"input":  `SELECT * FROM t1 WHERE id NOT IN (SELECT id FROM t2);`,
			"output": "",
			//"output": `SELECT table1.* FROM t1 LEFT JOIN t2 ON t1.id=t2.id WHERE t2.id IS NULL;`,
		},
		{
			"input":  `SELECT * FROM t1 WHERE NOT EXISTS (SELECT id FROM t2 WHERE t1.id=t2.id);`,
			"output": "",
			//"output": `SELECT table1.* FROM table1 LEFT JOIN table2 ON table1.id=table2.id WHERE table2.id IS NULL;`,
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteSubQuery2Join()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
529
	common.Config.TestDSN.Disable = orgTestDSNStatus
martianzhang's avatar
martianzhang 已提交
530
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
531 532 533
}

func TestRewriteDML2Select(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
534
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586
	testSQL := []map[string]string{
		{
			"input":  "DELETE city, country FROM city INNER JOIN country using (country_id) WHERE city.city_id = 1;",
			"output": "select * from city join country using (country_id) where city.city_id = 1",
		}, {
			"input":  "DELETE city FROM city LEFT JOIN country ON city.country_id = country.country_id WHERE country.country IS NULL;",
			"output": "select * from city left join country on city.country_id = country.country_id where country.country is null",
		}, {
			"input":  "DELETE a1, a2 FROM city AS a1 INNER JOIN country AS a2 WHERE a1.country_id=a2.country_id",
			"output": "select * from city as a1 join country as a2 where a1.country_id = a2.country_id",
		}, {
			"input":  "DELETE FROM a1, a2 USING city AS a1 INNER JOIN country AS a2 WHERE a1.country_id=a2.country_id",
			"output": "select * from city as a1 join country as a2 where a1.country_id = a2.country_id",
		}, {
			"input":  "DELETE FROM film WHERE length > 100;",
			"output": "select * from film where length > 100",
		}, {
			"input":  "UPDATE city INNER JOIN country USING(country_id) SET city.city = 'Abha', city.last_update = '2006-02-15 04:45:25', country.country = 'Afghanistan' WHERE city.city_id=10;",
			"output": "select * from city join country using (country_id) where city.city_id = 10",
		}, {
			"input":  "UPDATE city INNER JOIN country ON city.country_id = country.country_id INNER JOIN address ON city.city_id = address.city_id SET city.city = 'Abha', city.last_update = '2006-02-15 04:45:25', country.country = 'Afghanistan' WHERE city.city_id=10;",
			"output": "select * from city join country on city.country_id = country.country_id join address on city.city_id = address.city_id where city.city_id = 10",
		}, {
			"input":  "UPDATE city, country SET city.city = 'Abha', city.last_update = '2006-02-15 04:45:25', country.country = 'Afghanistan' WHERE city.country_id = country.country_id AND city.city_id=10;",
			"output": "select * from city, country where city.country_id = country.country_id and city.city_id = 10",
		}, {
			"input":  "UPDATE film SET length = 10 WHERE language_id = 20;",
			"output": "select * from film where language_id = 20",
		}, {
			"input":  "INSERT INTO city (country_id) SELECT country_id FROM country;",
			"output": "select country_id from country",
		}, {
			"input":  "INSERT INTO city (country_id) VALUES (1),(2),(3);",
			"output": "select 1 from DUAL",
		}, {
			"input":  "INSERT INTO city (country_id) VALUES (10);",
			"output": "select 1 from DUAL",
		}, {
			"input":  "INSERT INTO city (country_id) SELECT 10 FROM DUAL;",
			"output": "select 10 from dual",
		}, {
			"input":  "replace INTO city (country_id) SELECT 10 FROM DUAL;",
			"output": "select 10 from dual",
		},
	}

	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteDML2Select()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
587
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
588 589 590
}

func TestRewriteDistinctStar(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
591
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624
	testSQL := []map[string]string{
		{
			"input":  `SELECT DISTINCT * FROM film;`,
			"output": "SELECT * FROM film;",
		},
		{
			"input":  `SELECT COUNT(DISTINCT *) FROM film;`,
			"output": "SELECT COUNT(*) FROM film;",
		},
		{
			"input":  `SELECT DISTINCT film.* FROM film;`,
			"output": "SELECT * FROM film;",
		},
		{
			"input":  "SELECT DISTINCT col FROM film;",
			"output": "SELECT DISTINCT col FROM film;",
		},
		{
			"input":  "SELECT DISTINCT film.* FROM film, tbl;",
			"output": "SELECT DISTINCT film.* FROM film, tbl;",
		},
		{

			"input":  "SELECT DISTINCT * FROM film, tbl;",
			"output": "SELECT DISTINCT * FROM film, tbl;",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteDistinctStar()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
625
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
626 627 628
}

func TestMergeAlterTables(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
629
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
630 631 632 633
	sqls := []string{
		// ADD|DROP INDEX
		// TODO: PRIMARY KEY, [UNIQUE|FULLTEXT|SPATIAL] INDEX
		"CREATE INDEX part_of_name ON customer (name(10));",
634
		"create index idx_test_cca on test_bb(test_cc);", // https://github.com/XiaoMi/soar/issues/205
martianzhang's avatar
martianzhang 已提交
635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659
		"alter table `sakila`.`t1` add index `idx_col`(`col`)",
		"alter table `sakila`.`t1` add UNIQUE index `idx_col`(`col`)",
		"alter table `sakila`.`t1` add index `idx_ID`(`ID`)",

		// ADD|DROP COLUMN
		"ALTER TABLE t2 DROP COLUMN c, DROP COLUMN d;",
		"ALTER TABLE T2 ADD COLUMN C int;",
		"ALTER TABLE T2 ADD COLUMN D int FIRST;",
		"ALTER TABLE T2 ADD COLUMN E int AFTER D;",

		// RENAME COLUMN
		"ALTER TABLE t1 RENAME COLUMN a TO b",

		// RENAME INDEX
		"ALTER TABLE t1 RENAME INDEX idx_a TO idx_b",
		"ALTER TABLE t1 RENAME KEY idx_a TO idx_b",

		// RENAME TABLE
		"ALTER TABLE db.old_table RENAME new_table;",
		"ALTER TABLE old_table RENAME TO new_table;",
		"ALTER TABLE old_table RENAME AS new_table;",

		// MODIFY & CHANGE
		"ALTER TABLE t1 MODIFY col1 BIGINT UNSIGNED DEFAULT 1 COMMENT 'my column';",
		"ALTER TABLE t1 CHANGE b a INT NOT NULL;",
660 661 662

		// table name quote in back ticks
		"alter table `t3`add index `idx_a`(a)",
663
		"alter table`t3`drop index`idx_b`",
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679
	}

	alterSQLs := MergeAlterTables(sqls...)
	var sortedAlterSQLs []string
	for item := range alterSQLs {
		sortedAlterSQLs = append(sortedAlterSQLs, item)
	}
	sort.Strings(sortedAlterSQLs)

	err := common.GoldenDiff(func() {
		for _, tb := range sortedAlterSQLs {
			fmt.Println(tb, ":", alterSQLs[tb])
		}
	}, t.Name(), update)
	if err != nil {
		t.Error(err)
martianzhang's avatar
martianzhang 已提交
680
	}
martianzhang's avatar
martianzhang 已提交
681
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
682 683 684
}

func TestRewriteUnionAll(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
685
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
686 687 688 689 690 691 692 693 694 695 696 697
	testSQL := []map[string]string{
		{
			"input":  `select country_id from city union select country_id from country;`,
			"output": "select country_id from city union all select country_id from country",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteUnionAll()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
698
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
699 700
}
func TestRewriteTruncate(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
701
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
702 703 704 705 706 707 708 709 710 711 712 713
	testSQL := []map[string]string{
		{
			"input":  `delete from tbl;`,
			"output": "truncate table tbl",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteTruncate()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
714
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
715 716 717
}

func TestRewriteOr2In(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
718
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756
	testSQL := []map[string]string{
		{
			"input":  `select country_id from city where country_id = 1 or country_id = 2 or country_id = 3;`,
			"output": "select country_id from city where country_id in (1, 2, 3)",
		},
		// TODO or中的恒真条件
		{
			"input":  `select country_id from city where country_id != 1 or country_id != 2 or country_id = 3;`,
			"output": "select country_id from city where country_id != 1 or country_id != 2 or country_id = 3",
		},
		// col = 1 or col is null不可转为IN
		{
			"input":  `select country_id from city where col = 1 or col is null;`,
			"output": "select country_id from city where col = 1 or col is null",
		},
		{
			"input":  `select country_id from city where col1 = 1 or col2 = 1 or col2 = 2;`,
			"output": "select country_id from city where col1 = 1 or col2 in (1, 2)",
		},
		{
			"input":  `select country_id from city where col1 = 1 or col2 = 1 or col2 = 2 or col1 = 3;`,
			"output": "select country_id from city where col2 in (1, 2) or col1 in (1, 3)",
		},
		{
			"input":  `select country_id from city where (col1 = 1 or col2 = 1 or col2 = 2 ) or col1 = 3;`,
			"output": "select country_id from city where (col1 = 1 or col2 in (1, 2)) or col1 = 3",
		},
		{
			"input":  `select country_id from city where col1 = 1 or (col2 = 1 or col2 = 2 ) or col1 = 3;`,
			"output": "select country_id from city where (col2 in (1, 2)) or col1 in (1, 3)",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteOr2In()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
757
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
758 759 760
}

func TestRmParenthesis(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
761
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785
	testSQL := []map[string]string{
		{
			"input":  `select country_id from city where (country_id = 1);`,
			"output": "select country_id from city where country_id = 1",
		},
		{
			"input":  `select * from city where a = 1 and (country_id = 1);`,
			"output": "select * from city where a = 1 and country_id = 1",
		},
		{
			"input":  `select country_id from city where (country_id = 1) or country_id = 1 ;`,
			"output": "select country_id from city where country_id = 1 or country_id = 1",
		},
		{
			"input":  `select country_id from city where col = 1 or (country_id = 1) or country_id = 1 ;`,
			"output": "select country_id from city where col = 1 or country_id = 1 or country_id = 1",
		},
	}
	for _, sql := range testSQL {
		rw := NewRewrite(sql["input"]).RewriteRmParenthesis()
		if rw.NewSQL != sql["output"] {
			t.Errorf("want: %s\ngot: %s", sql["output"], rw.NewSQL)
		}
	}
martianzhang's avatar
martianzhang 已提交
786
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
787 788 789
}

func TestListRewriteRules(t *testing.T) {
martianzhang's avatar
martianzhang 已提交
790
	common.Log.Debug("Entering function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
791 792
	err := common.GoldenDiff(func() {
		ListRewriteRules(RewriteRules)
martianzhang's avatar
martianzhang 已提交
793 794 795 796
		orgReportType := common.Config.ReportType
		common.Config.ReportType = "json"
		ListRewriteRules(RewriteRules)
		common.Config.ReportType = orgReportType
martianzhang's avatar
martianzhang 已提交
797 798 799 800
	}, t.Name(), update)
	if err != nil {
		t.Error(err)
	}
martianzhang's avatar
martianzhang 已提交
801
	common.Log.Debug("Exiting function: %s", common.GetFunctionName())
martianzhang's avatar
martianzhang 已提交
802
}