github.com/matrixorigin/matrixone@v1.2.0/pkg/sql/plan/partition_range_test.go (about) 1 // Copyright 2022 Matrix Origin 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package plan 16 17 import "testing" 18 19 func TestCreateRangePartitionTable(t *testing.T) { 20 sql := `CREATE TABLE employees ( 21 id INT NOT NULL, 22 fname VARCHAR(30), 23 lname VARCHAR(30), 24 hired DATE NOT NULL DEFAULT '1970-01-01', 25 separated DATE NOT NULL DEFAULT '9999-12-31', 26 job_code INT NOT NULL, 27 store_id INT NOT NULL 28 ) 29 PARTITION BY RANGE (store_id) ( 30 PARTITION p0 VALUES LESS THAN (6), 31 PARTITION p1 VALUES LESS THAN (11), 32 PARTITION p2 VALUES LESS THAN (16), 33 PARTITION p3 VALUES LESS THAN (21) 34 );` 35 36 mock := NewMockOptimizer(false) 37 logicPlan, err := buildSingleStmt(mock, t, sql) 38 if err != nil { 39 t.Fatalf("%+v", err) 40 } 41 outPutPlan(logicPlan, true, t) 42 } 43 44 // -----------------------Range Partition------------------------------------- 45 func TestRangePartition(t *testing.T) { 46 sqls := []string{ 47 `CREATE TABLE employees ( 48 id INT NOT NULL, 49 fname VARCHAR(30), 50 lname VARCHAR(30), 51 hired DATE NOT NULL DEFAULT '1970-01-01', 52 separated DATE NOT NULL DEFAULT '9999-12-31', 53 job_code INT NOT NULL, 54 store_id INT NOT NULL 55 ) 56 PARTITION BY RANGE (store_id) ( 57 PARTITION p0 VALUES LESS THAN (6), 58 PARTITION p1 VALUES LESS THAN (11), 59 PARTITION p2 VALUES LESS THAN (16), 60 PARTITION p3 VALUES LESS THAN (21) 61 );`, 62 63 `CREATE TABLE t1 ( 64 id INT NOT NULL, 65 fname VARCHAR(30), 66 lname VARCHAR(30), 67 hired DATE NOT NULL DEFAULT '1970-01-01', 68 separated DATE NOT NULL DEFAULT '9999-12-31', 69 job_code INT, 70 store_id INT 71 ) 72 PARTITION BY RANGE ( YEAR(separated) + job_code % (7*24)) ( 73 PARTITION p0 VALUES LESS THAN (1991), 74 PARTITION p1 VALUES LESS THAN (1996), 75 PARTITION p2 VALUES LESS THAN (2001), 76 PARTITION p3 VALUES LESS THAN MAXVALUE 77 );`, 78 79 `CREATE TABLE t1 ( 80 year_col INT, 81 some_data INT 82 ) 83 PARTITION BY RANGE (year_col) ( 84 PARTITION p0 VALUES LESS THAN (1991), 85 PARTITION p1 VALUES LESS THAN (1995), 86 PARTITION p2 VALUES LESS THAN (1999), 87 PARTITION p3 VALUES LESS THAN (2002), 88 PARTITION p4 VALUES LESS THAN (2006), 89 PARTITION p5 VALUES LESS THAN (2012) 90 );`, 91 92 `CREATE TABLE t1 ( 93 year_col INT, 94 some_data INT 95 ) 96 PARTITION BY RANGE (year_col) ( 97 PARTITION p0 VALUES LESS THAN (1991) COMMENT = 'Data for the years previous to 1991', 98 PARTITION p1 VALUES LESS THAN (1995) COMMENT = 'Data for the years previous to 1995', 99 PARTITION p2 VALUES LESS THAN (1999) COMMENT = 'Data for the years previous to 1999', 100 PARTITION p3 VALUES LESS THAN (2002) COMMENT = 'Data for the years previous to 2002', 101 PARTITION p4 VALUES LESS THAN (2006) COMMENT = 'Data for the years previous to 2006', 102 PARTITION p5 VALUES LESS THAN (2012) COMMENT = 'Data for the years previous to 2012' 103 );`, 104 105 `CREATE TABLE employees ( 106 id INT NOT NULL, 107 fname VARCHAR(30), 108 lname VARCHAR(30), 109 hired DATE NOT NULL DEFAULT '1970-01-01', 110 separated DATE NOT NULL DEFAULT '9999-12-31', 111 job_code INT NOT NULL, 112 store_id INT NOT NULL 113 ) 114 PARTITION BY RANGE (store_id) ( 115 PARTITION p0 VALUES LESS THAN (6), 116 PARTITION p1 VALUES LESS THAN (11), 117 PARTITION p2 VALUES LESS THAN (16), 118 PARTITION p3 VALUES LESS THAN MAXVALUE 119 );`, 120 121 `CREATE TABLE employees ( 122 id INT NOT NULL, 123 fname VARCHAR(30), 124 lname VARCHAR(30), 125 hired DATE NOT NULL DEFAULT '1970-01-01', 126 separated DATE NOT NULL DEFAULT '9999-12-31', 127 job_code INT NOT NULL, 128 store_id INT NOT NULL 129 ) 130 PARTITION BY RANGE (job_code) ( 131 PARTITION p0 VALUES LESS THAN (100), 132 PARTITION p1 VALUES LESS THAN (1000), 133 PARTITION p2 VALUES LESS THAN (10000) 134 );`, 135 136 `CREATE TABLE employees ( 137 id INT NOT NULL, 138 fname VARCHAR(30), 139 lname VARCHAR(30), 140 hired DATE NOT NULL DEFAULT '1970-01-01', 141 separated DATE NOT NULL DEFAULT '9999-12-31', 142 job_code INT, 143 store_id INT 144 ) 145 PARTITION BY RANGE ( YEAR(separated) ) ( 146 PARTITION p0 VALUES LESS THAN (1991), 147 PARTITION p1 VALUES LESS THAN (1996), 148 PARTITION p2 VALUES LESS THAN (2001), 149 PARTITION p3 VALUES LESS THAN MAXVALUE 150 );`, 151 152 `CREATE TABLE employees ( 153 id INT NOT NULL, 154 fname VARCHAR(30), 155 lname VARCHAR(30), 156 hired DATE NOT NULL DEFAULT '1970-01-01', 157 separated DATE NOT NULL DEFAULT '9999-12-31', 158 job_code INT NOT NULL, 159 store_id INT NOT NULL, 160 PRIMARY KEY(id, store_id) 161 ) 162 PARTITION BY RANGE (store_id) ( 163 PARTITION p0 VALUES LESS THAN (6), 164 PARTITION p1 VALUES LESS THAN (11), 165 PARTITION p2 VALUES LESS THAN (16), 166 PARTITION p3 VALUES LESS THAN (21) 167 );`, 168 169 `CREATE TABLE employees ( 170 id INT NOT NULL, 171 fname VARCHAR(30), 172 lname VARCHAR(30), 173 hired DATE NOT NULL DEFAULT '1970-01-01', 174 separated DATE NOT NULL DEFAULT '9999-12-31', 175 job_code INT NOT NULL, 176 store_id INT NOT NULL, 177 PRIMARY KEY(id, store_id) 178 ) 179 PARTITION BY RANGE (store_id + 5) ( 180 PARTITION p0 VALUES LESS THAN (6), 181 PARTITION p1 VALUES LESS THAN (11), 182 PARTITION p2 VALUES LESS THAN (16), 183 PARTITION p3 VALUES LESS THAN (21) 184 );`, 185 186 `CREATE TABLE employees ( 187 id INT NOT NULL, 188 fname VARCHAR(30), 189 lname VARCHAR(30), 190 hired DATE NOT NULL DEFAULT '1970-01-01', 191 separated DATE NOT NULL DEFAULT '9999-12-31', 192 job_code INT NOT NULL, 193 store_id INT NOT NULL, 194 PRIMARY KEY(id, hired) 195 ) 196 PARTITION BY RANGE (year(hired)) ( 197 PARTITION p0 VALUES LESS THAN (6), 198 PARTITION p1 VALUES LESS THAN (11), 199 PARTITION p2 VALUES LESS THAN (16), 200 PARTITION p3 VALUES LESS THAN (21) 201 );`, 202 203 `CREATE TABLE members ( 204 firstname VARCHAR(25) NOT NULL, 205 lastname VARCHAR(25) NOT NULL, 206 username VARCHAR(16) NOT NULL, 207 email VARCHAR(35), 208 joined DATE NOT NULL 209 ) 210 PARTITION BY RANGE( YEAR(joined) ) PARTITIONS 5 ( 211 PARTITION p0 VALUES LESS THAN (1960), 212 PARTITION p1 VALUES LESS THAN (1970), 213 PARTITION p2 VALUES LESS THAN (1980), 214 PARTITION p3 VALUES LESS THAN (1990), 215 PARTITION p4 VALUES LESS THAN MAXVALUE 216 );`, 217 218 `CREATE TABLE titles ( 219 emp_no INT NOT NULL, 220 title VARCHAR(50) NOT NULL, 221 from_date DATE NOT NULL, 222 to_date DATE, 223 PRIMARY KEY (emp_no,title, from_date) 224 ) 225 PARTITION BY RANGE (to_days(from_date)) 226 ( 227 partition p01 values less than (to_days('1985-12-31')), 228 partition p02 values less than (to_days('1986-12-31')), 229 partition p03 values less than (to_days('1987-12-31')), 230 partition p04 values less than (to_days('1988-12-31')), 231 partition p05 values less than (to_days('1989-12-31')), 232 partition p06 values less than (to_days('1990-12-31')), 233 partition p07 values less than (to_days('1991-12-31')), 234 partition p08 values less than (to_days('1992-12-31')), 235 partition p09 values less than (to_days('1993-12-31')), 236 partition p10 values less than (to_days('1994-12-31')), 237 partition p11 values less than (to_days('1995-12-31')), 238 partition p12 values less than (to_days('1996-12-31')) 239 );`, 240 241 `CREATE TABLE tange_test ( 242 id int NULL, 243 name varchar(255) NULL, 244 date datetime NULL 245 ) 246 PARTITION BY RANGE (year(date)) ( 247 PARTITION p0 VALUES LESS THAN (2000), 248 PARTITION p1 VALUES LESS THAN (2001), 249 PARTITION p2 VALUES LESS THAN (2002), 250 PARTITION p3 VALUES LESS THAN (2003), 251 PARTITION p4 VALUES LESS THAN MAXVALUE 252 );`, 253 `create table t1 ( 254 t_name varchar(255) NOT NULL, 255 t_date datetime NOT NULL 256 ) 257 partition by range (year(t_date)*100+month(t_date)) ( 258 partition p201201 values less than (201202), 259 partition p201202 values less than (201203), 260 partition p201203 values less than (201204) 261 );`, 262 263 //`CREATE TABLE quarterly_report_status ( 264 // report_id INT NOT NULL, 265 // report_status VARCHAR(20) NOT NULL, 266 // report_updated TIMESTAMP NOT NULL 267 //) 268 // PARTITION BY RANGE ( UNIX_TIMESTAMP(report_updated) ) ( 269 // PARTITION p0 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-01-01 00:00:00') ), 270 // PARTITION p1 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-04-01 00:00:00') ), 271 // PARTITION p2 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-07-01 00:00:00') ), 272 // PARTITION p3 VALUES LESS THAN ( UNIX_TIMESTAMP('2008-10-01 00:00:00') ), 273 // PARTITION p4 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-01-01 00:00:00') ), 274 // PARTITION p5 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-04-01 00:00:00') ), 275 // PARTITION p6 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-07-01 00:00:00') ), 276 // PARTITION p7 VALUES LESS THAN ( UNIX_TIMESTAMP('2009-10-01 00:00:00') ), 277 // PARTITION p8 VALUES LESS THAN ( UNIX_TIMESTAMP('2010-01-01 00:00:00') ), 278 // PARTITION p9 VALUES LESS THAN (MAXVALUE) 279 //);`, 280 } 281 282 mock := NewMockOptimizer(false) 283 for _, sql := range sqls { 284 t.Log(sql) 285 logicPlan, err := buildSingleStmt(mock, t, sql) 286 if err != nil { 287 t.Fatalf("%+v", err) 288 } 289 outPutPlan(logicPlan, true, t) 290 } 291 } 292 293 func TestRangePartitionError(t *testing.T) { 294 sqls := []string{ 295 `create table t31 (a int not null) partition by range( a );`, 296 `create table t32 (a int not null) partition by range columns( a );`, 297 `CREATE TABLE employees ( 298 id INT NOT NULL, 299 fname VARCHAR(30), 300 lname VARCHAR(30), 301 hired DATE NOT NULL DEFAULT '1970-01-01', 302 separated DATE NOT NULL DEFAULT '9999-12-31', 303 job_code INT NOT NULL, 304 store_id INT NOT NULL, 305 PRIMARY KEY(id, store_id) 306 ) 307 PARTITION BY RANGE (job_code) ( 308 PARTITION p0 VALUES LESS THAN (6), 309 PARTITION p1 VALUES LESS THAN (11), 310 PARTITION p2 VALUES LESS THAN (16), 311 PARTITION p3 VALUES LESS THAN (21) 312 );`, 313 314 `CREATE TABLE employees ( 315 id INT NOT NULL, 316 fname VARCHAR(30), 317 lname VARCHAR(30), 318 hired DATE NOT NULL DEFAULT '1970-01-01', 319 separated DATE NOT NULL DEFAULT '9999-12-31', 320 job_code INT NOT NULL, 321 store_id INT NOT NULL, 322 PRIMARY KEY(id, store_id) 323 ) 324 PARTITION BY RANGE (job_code + 5) ( 325 PARTITION p0 VALUES LESS THAN (6), 326 PARTITION p1 VALUES LESS THAN (11), 327 PARTITION p2 VALUES LESS THAN (16), 328 PARTITION p3 VALUES LESS THAN (21) 329 );`, 330 331 `CREATE TABLE employees ( 332 id INT NOT NULL, 333 fname VARCHAR(30), 334 lname VARCHAR(30), 335 hired DATE NOT NULL DEFAULT '1970-01-01', 336 separated DATE NOT NULL DEFAULT '9999-12-31', 337 job_code INT NOT NULL, 338 store_id INT NOT NULL, 339 PRIMARY KEY(id, hired) 340 ) 341 PARTITION BY RANGE (year(separated)) ( 342 PARTITION p0 VALUES LESS THAN (6), 343 PARTITION p1 VALUES LESS THAN (11), 344 PARTITION p2 VALUES LESS THAN (16), 345 PARTITION p3 VALUES LESS THAN (21) 346 );`, 347 348 `CREATE TABLE employees ( 349 id INT NOT NULL, 350 fname VARCHAR(30), 351 lname VARCHAR(30), 352 hired DATE NOT NULL DEFAULT '1970-01-01', 353 separated DATE NOT NULL DEFAULT '9999-12-31', 354 job_code INT NOT NULL, 355 store_id INT NOT NULL, 356 PRIMARY KEY(id, store_id) 357 ) 358 PARTITION BY RANGE (job_code + store_id) ( 359 PARTITION p0 VALUES LESS THAN (6), 360 PARTITION p1 VALUES LESS THAN (11), 361 PARTITION p2 VALUES LESS THAN (16), 362 PARTITION p3 VALUES LESS THAN (21) 363 );`, 364 365 `CREATE TABLE members ( 366 firstname VARCHAR(25) NOT NULL, 367 lastname VARCHAR(25) NOT NULL, 368 username VARCHAR(16) NOT NULL, 369 email VARCHAR(35), 370 joined DATE NOT NULL 371 ) 372 PARTITION BY RANGE( YEAR(joined) ) PARTITIONS 4 ( 373 PARTITION p0 VALUES LESS THAN (1960), 374 PARTITION p1 VALUES LESS THAN (1970), 375 PARTITION p2 VALUES LESS THAN (1980), 376 PARTITION p3 VALUES LESS THAN (1990), 377 PARTITION p4 VALUES LESS THAN MAXVALUE 378 );`, 379 `CREATE TABLE employees ( 380 id INT NOT NULL, 381 fname VARCHAR(30), 382 lname VARCHAR(30), 383 hired DATE NOT NULL DEFAULT '1970-01-01', 384 separated DATE NOT NULL DEFAULT '9999-12-31', 385 job_code INT NOT NULL, 386 store_id INT NOT NULL 387 ) 388 PARTITION BY RANGE (store_id) ( 389 PARTITION p0 VALUES LESS THAN (6), 390 PARTITION p1 VALUES LESS THAN (11), 391 PARTITION p2 VALUES LESS THAN MAXVALUE, 392 PARTITION p3 VALUES LESS THAN (21) 393 );`, 394 `CREATE TABLE employees ( 395 id INT NOT NULL, 396 fname VARCHAR(30), 397 lname VARCHAR(30), 398 hired DATE NOT NULL DEFAULT '1970-01-01', 399 separated DATE NOT NULL DEFAULT '9999-12-31', 400 job_code INT NOT NULL, 401 store_id INT NOT NULL 402 ) 403 PARTITION BY RANGE (store_id) ( 404 PARTITION p0 VALUES LESS THAN (6), 405 PARTITION p1 VALUES LESS THAN (11), 406 PARTITION p2 VALUES LESS THAN (10), 407 PARTITION p3 VALUES LESS THAN (21) 408 );`, 409 `CREATE TABLE employees ( 410 id INT NOT NULL, 411 fname VARCHAR(30), 412 lname VARCHAR(30), 413 hired DATE NOT NULL DEFAULT '1970-01-01', 414 separated DATE NOT NULL DEFAULT '9999-12-31', 415 job_code INT NOT NULL, 416 store_id INT NOT NULL 417 ) 418 PARTITION BY RANGE (store_id) ( 419 PARTITION p0 VALUES LESS THAN (6), 420 PARTITION p1 VALUES LESS THAN (11), 421 PARTITION p2 VALUES LESS THAN (11), 422 PARTITION p3 VALUES LESS THAN (21) 423 );`, 424 `create table t1 ( 425 t_name varchar(255) NOT NULL, 426 t_date datetime NOT NULL 427 ) 428 partition by range (year(t_date)*100+month(t_date)) ( 429 partition p201201 values less than (201202), 430 partition p201202 values less than (201203), 431 partition p201203 values less than (201202) 432 );`, 433 } 434 435 mock := NewMockOptimizer(false) 436 for _, sql := range sqls { 437 _, err := buildSingleStmt(mock, t, sql) 438 t.Log(sql) 439 t.Log(err) 440 if err == nil { 441 t.Fatalf("%+v", err) 442 } 443 } 444 } 445 446 // ---------------------Range Columns Partition-------------------------------- 447 func TestRangeColumnsPartition(t *testing.T) { 448 sqls := []string{ 449 `CREATE TABLE rc ( 450 a INT NOT NULL, 451 b INT NOT NULL 452 ) 453 PARTITION BY RANGE COLUMNS(a,b) ( 454 PARTITION p0 VALUES LESS THAN (10,5), 455 PARTITION p1 VALUES LESS THAN (20,10), 456 PARTITION p2 VALUES LESS THAN (50,20), 457 PARTITION p3 VALUES LESS THAN (65,30) 458 );`, 459 460 `CREATE TABLE rc ( 461 a INT NOT NULL, 462 b INT NOT NULL 463 ) 464 PARTITION BY RANGE COLUMNS(a,b) ( 465 PARTITION p0 VALUES LESS THAN (10,5), 466 PARTITION p1 VALUES LESS THAN (20,10), 467 PARTITION p2 VALUES LESS THAN (50,MAXVALUE), 468 PARTITION p3 VALUES LESS THAN (65,MAXVALUE), 469 PARTITION p4 VALUES LESS THAN (MAXVALUE,MAXVALUE) 470 );`, 471 472 `CREATE TABLE rc ( 473 a INT NOT NULL, 474 b INT NOT NULL 475 ) 476 PARTITION BY RANGE COLUMNS(a,b) ( 477 PARTITION p0 VALUES LESS THAN (10,5) COMMENT = 'Data for LESS THAN (10,5)', 478 PARTITION p1 VALUES LESS THAN (20,10) COMMENT = 'Data for LESS THAN (20,10)', 479 PARTITION p2 VALUES LESS THAN (50,MAXVALUE) COMMENT = 'Data for LESS THAN (50,MAXVALUE)', 480 PARTITION p3 VALUES LESS THAN (65,MAXVALUE) COMMENT = 'Data for LESS THAN (65,MAXVALUE)', 481 PARTITION p4 VALUES LESS THAN (MAXVALUE,MAXVALUE) COMMENT = 'Data for LESS THAN (MAXVALUE,MAXVALUE)' 482 );`, 483 484 `CREATE TABLE rcx ( 485 a INT, 486 b INT, 487 c CHAR(3), 488 d INT 489 ) 490 PARTITION BY RANGE COLUMNS(a,d,c) ( 491 PARTITION p0 VALUES LESS THAN (5,10,'ggg'), 492 PARTITION p1 VALUES LESS THAN (10,20,'mmm'), 493 PARTITION p2 VALUES LESS THAN (15,30,'sss'), 494 PARTITION p3 VALUES LESS THAN (MAXVALUE,MAXVALUE,MAXVALUE) 495 );`, 496 497 `CREATE TABLE t1 ( 498 col1 INT NOT NULL, 499 col2 INT NOT NULL, 500 col3 INT NOT NULL, 501 col4 INT NOT NULL, 502 PRIMARY KEY(col1, col3) 503 ) 504 PARTITION BY RANGE COLUMNS(col1,col3) ( 505 PARTITION p0 VALUES LESS THAN (10,5), 506 PARTITION p1 VALUES LESS THAN (20,10), 507 PARTITION p2 VALUES LESS THAN (50,20), 508 PARTITION p3 VALUES LESS THAN (65,30) 509 );`, 510 511 `CREATE TABLE rc ( 512 a INT NOT NULL, 513 b INT NOT NULL 514 ) 515 PARTITION BY RANGE COLUMNS(a,b) PARTITIONS 4 ( 516 PARTITION p0 VALUES LESS THAN (10,5), 517 PARTITION p1 VALUES LESS THAN (20,10), 518 PARTITION p2 VALUES LESS THAN (50,20), 519 PARTITION p3 VALUES LESS THAN (65,30) 520 );`, 521 522 `CREATE TABLE rc (c1 INT, c2 TIME) 523 PARTITION BY RANGE COLUMNS(c2) ( 524 PARTITION p0 VALUES LESS THAN('12:30:45'), 525 PARTITION p1 VALUES LESS THAN('15:30:45'), 526 PARTITION p2 VALUES LESS THAN('17:30:45'), 527 PARTITION p3 VALUES LESS THAN('19:30:45'), 528 PARTITION p4 VALUES LESS THAN(MAXVALUE) 529 );`, 530 531 `CREATE TABLE members ( 532 firstname VARCHAR(25) NOT NULL, 533 lastname VARCHAR(25) NOT NULL, 534 username VARCHAR(16) NOT NULL, 535 email VARCHAR(35), 536 joined DATE NOT NULL 537 ) 538 PARTITION BY RANGE COLUMNS(joined) ( 539 PARTITION p0 VALUES LESS THAN ('1960-01-01'), 540 PARTITION p1 VALUES LESS THAN ('1970-01-01'), 541 PARTITION p2 VALUES LESS THAN ('1980-01-01'), 542 PARTITION p3 VALUES LESS THAN ('1990-01-01'), 543 PARTITION p4 VALUES LESS THAN MAXVALUE 544 );`, 545 `CREATE TABLE rc ( 546 a INT NOT NULL, 547 b INT NOT NULL 548 ) 549 PARTITION BY RANGE COLUMNS(a,b) ( 550 PARTITION p0 VALUES LESS THAN (10,MAXVALUE), 551 PARTITION p1 VALUES LESS THAN (20,10), 552 PARTITION p2 VALUES LESS THAN (50,MAXVALUE), 553 PARTITION p3 VALUES LESS THAN (65,MAXVALUE), 554 PARTITION p4 VALUES LESS THAN (MAXVALUE,MAXVALUE) 555 );`, 556 `CREATE TABLE rc ( 557 a INT NOT NULL, 558 b INT NOT NULL 559 ) 560 PARTITION BY RANGE COLUMNS(a,b) ( 561 PARTITION p0 VALUES LESS THAN (10,5), 562 PARTITION p1 VALUES LESS THAN (20,10), 563 PARTITION p2 VALUES LESS THAN (50,40), 564 PARTITION p3 VALUES LESS THAN (50,MAXVALUE), 565 PARTITION p4 VALUES LESS THAN (MAXVALUE,MAXVALUE) 566 );`, 567 } 568 mock := NewMockOptimizer(false) 569 for _, sql := range sqls { 570 t.Log(sql) 571 logicPlan, err := buildSingleStmt(mock, t, sql) 572 if err != nil { 573 t.Fatalf("%+v", err) 574 } 575 outPutPlan(logicPlan, true, t) 576 } 577 } 578 579 func TestRangeColumnsPartitionError(t *testing.T) { 580 sqls := []string{ 581 `CREATE TABLE rc3 ( 582 a INT NOT NULL, 583 b INT NOT NULL 584 ) 585 PARTITION BY RANGE COLUMNS(a,b) ( 586 PARTITION p0 VALUES LESS THAN (a,5), 587 PARTITION p1 VALUES LESS THAN (20,10), 588 PARTITION p2 VALUES LESS THAN (50,20), 589 PARTITION p3 VALUES LESS THAN (65,30) 590 );`, 591 592 `CREATE TABLE rc3 ( 593 a INT NOT NULL, 594 b INT NOT NULL 595 ) 596 PARTITION BY RANGE COLUMNS(a,b) ( 597 PARTITION p0 VALUES LESS THAN (a+7,5), 598 PARTITION p1 VALUES LESS THAN (20,10), 599 PARTITION p2 VALUES LESS THAN (50,20), 600 PARTITION p3 VALUES LESS THAN (65,30) 601 );`, 602 603 `CREATE TABLE t1 ( 604 col1 INT NOT NULL, 605 col2 INT NOT NULL, 606 col3 INT NOT NULL, 607 col4 INT NOT NULL, 608 PRIMARY KEY(col1, col3) 609 ) 610 PARTITION BY RANGE COLUMNS(col1,col2) ( 611 PARTITION p0 VALUES LESS THAN (10,5), 612 PARTITION p1 VALUES LESS THAN (20,10), 613 PARTITION p2 VALUES LESS THAN (50,20), 614 PARTITION p3 VALUES LESS THAN (65,30) 615 );`, 616 617 `CREATE TABLE rc ( 618 a INT NOT NULL, 619 b INT NOT NULL 620 ) 621 PARTITION BY RANGE COLUMNS(a,b) PARTITIONS 5 ( 622 PARTITION p0 VALUES LESS THAN (10,5), 623 PARTITION p1 VALUES LESS THAN (20,10), 624 PARTITION p2 VALUES LESS THAN (50,20), 625 PARTITION p3 VALUES LESS THAN (65,30) 626 );`, 627 628 `CREATE TABLE rc ( 629 a INT NOT NULL, 630 b INT NOT NULL 631 ) 632 PARTITION BY RANGE COLUMNS(a,b) ( 633 PARTITION p0 VALUES LESS THAN (10,5), 634 PARTITION p1 VALUES IN( 1,2 ), 635 PARTITION p2 VALUES LESS THAN (50,20), 636 PARTITION p3 VALUES LESS THAN (65,30) 637 );`, 638 639 `CREATE TABLE rc (c1 INT, c2 TIMESTAMP) 640 PARTITION BY RANGE COLUMNS(c2) ( 641 PARTITION p0 VALUES LESS THAN('1990-01-01'), 642 PARTITION p1 VALUES LESS THAN('1995-01-01'), 643 PARTITION p2 VALUES LESS THAN('2000-01-01'), 644 PARTITION p3 VALUES LESS THAN('2005-01-01'), 645 PARTITION p4 VALUES LESS THAN(MAXVALUE) 646 );`, 647 `CREATE TABLE members ( 648 firstname VARCHAR(25) NOT NULL, 649 lastname VARCHAR(25) NOT NULL, 650 username VARCHAR(16) NOT NULL, 651 email VARCHAR(35), 652 joined DATE NOT NULL 653 ) 654 PARTITION BY RANGE COLUMNS(joined) ( 655 PARTITION p0 VALUES LESS THAN ('1960-01-01'), 656 PARTITION p1 VALUES LESS THAN ('2070-01-01'), 657 PARTITION p2 VALUES LESS THAN ('1980-01-01'), 658 PARTITION p3 VALUES LESS THAN ('1990-01-01'), 659 PARTITION p4 VALUES LESS THAN MAXVALUE 660 );`, 661 `CREATE TABLE rc ( 662 a INT NOT NULL, 663 b INT NOT NULL 664 ) 665 PARTITION BY RANGE COLUMNS(a,b) ( 666 PARTITION p0 VALUES LESS THAN (10,5), 667 PARTITION p1 VALUES LESS THAN (20,10), 668 PARTITION p2 VALUES LESS THAN (50,MAXVALUE), 669 PARTITION p3 VALUES LESS THAN (50,MAXVALUE), 670 PARTITION p4 VALUES LESS THAN (MAXVALUE,MAXVALUE) 671 );`, 672 } 673 674 mock := NewMockOptimizer(false) 675 for _, sql := range sqls { 676 _, err := buildSingleStmt(mock, t, sql) 677 t.Log(sql) 678 t.Log(err) 679 if err == nil { 680 t.Fatalf("%+v", err) 681 } 682 } 683 } 684 685 func TestRangePartitionFunctionError(t *testing.T) { 686 sqls := []string{ 687 `CREATE TABLE r1 ( 688 a INT, 689 b INT 690 ) 691 PARTITION BY RANGE (a) ( 692 PARTITION p0 VALUES LESS THAN (5/2), 693 PARTITION p1 VALUES LESS THAN (MAXVALUE) 694 );`, 695 696 `CREATE TABLE r1 ( 697 a INT, 698 b INT 699 ) 700 PARTITION BY RANGE (a) ( 701 PARTITION p0 VALUES LESS THAN (5.2), 702 PARTITION p1 VALUES LESS THAN (12) 703 );`, 704 705 `CREATE TABLE r1 ( 706 a INT, 707 b FLOAT 708 ) 709 PARTITION BY RANGE (b) ( 710 PARTITION p0 VALUES LESS THAN (12), 711 PARTITION p1 VALUES LESS THAN (MAXVALUE) 712 );`, 713 //`create TABLE t1 ( 714 // col1 int, 715 // col2 float 716 //) 717 //partition by range( case when col1 > 0 then 10 else 20 end ) ( 718 // partition p0 values less than (2), 719 // partition p1 values less than (6) 720 //);`, 721 } 722 mock := NewMockOptimizer(false) 723 for _, sql := range sqls { 724 _, err := buildSingleStmt(mock, t, sql) 725 t.Log(sql) 726 t.Log(err) 727 if err == nil { 728 t.Fatalf("%+v", err) 729 } 730 } 731 }