vitess.io/vitess@v0.16.2/go/vt/vtgate/planbuilder/testdata/aggr_cases.json (about) 1 [ 2 { 3 "comment": "group by a unique vindex should revert to simple route, and having clause should find the correct symbols.", 4 "query": "select id, count(*) c from user group by id having max(col) > 10", 5 "v3-plan": { 6 "QueryType": "SELECT", 7 "Original": "select id, count(*) c from user group by id having max(col) > 10", 8 "Instructions": { 9 "OperatorType": "Route", 10 "Variant": "Scatter", 11 "Keyspace": { 12 "Name": "user", 13 "Sharded": true 14 }, 15 "FieldQuery": "select id, count(*) as c from `user` where 1 != 1 group by id", 16 "Query": "select id, count(*) as c from `user` group by id having max(col) > 10", 17 "Table": "`user`" 18 } 19 }, 20 "gen4-plan": { 21 "QueryType": "SELECT", 22 "Original": "select id, count(*) c from user group by id having max(col) > 10", 23 "Instructions": { 24 "OperatorType": "Route", 25 "Variant": "Scatter", 26 "Keyspace": { 27 "Name": "user", 28 "Sharded": true 29 }, 30 "FieldQuery": "select id, count(*) as c from `user` where 1 != 1 group by id", 31 "Query": "select id, count(*) as c from `user` group by id having max(col) > 10", 32 "Table": "`user`" 33 }, 34 "TablesUsed": [ 35 "user.user" 36 ] 37 } 38 }, 39 { 40 "comment": "scatter aggregate in a subquery", 41 "query": "select a from (select count(*) as a from user) t", 42 "v3-plan": { 43 "QueryType": "SELECT", 44 "Original": "select a from (select count(*) as a from user) t", 45 "Instructions": { 46 "OperatorType": "SimpleProjection", 47 "Columns": [ 48 0 49 ], 50 "Inputs": [ 51 { 52 "OperatorType": "Aggregate", 53 "Variant": "Scalar", 54 "Aggregates": "sum_count(0) AS count", 55 "Inputs": [ 56 { 57 "OperatorType": "Route", 58 "Variant": "Scatter", 59 "Keyspace": { 60 "Name": "user", 61 "Sharded": true 62 }, 63 "FieldQuery": "select count(*) as a from `user` where 1 != 1", 64 "Query": "select count(*) as a from `user`", 65 "Table": "`user`" 66 } 67 ] 68 } 69 ] 70 } 71 }, 72 "gen4-plan": { 73 "QueryType": "SELECT", 74 "Original": "select a from (select count(*) as a from user) t", 75 "Instructions": { 76 "OperatorType": "SimpleProjection", 77 "Columns": [ 78 0 79 ], 80 "Inputs": [ 81 { 82 "OperatorType": "Aggregate", 83 "Variant": "Scalar", 84 "Aggregates": "sum_count_star(0) AS a", 85 "Inputs": [ 86 { 87 "OperatorType": "Route", 88 "Variant": "Scatter", 89 "Keyspace": { 90 "Name": "user", 91 "Sharded": true 92 }, 93 "FieldQuery": "select count(*) as a from `user` where 1 != 1", 94 "Query": "select count(*) as a from `user`", 95 "Table": "`user`" 96 } 97 ] 98 } 99 ] 100 }, 101 "TablesUsed": [ 102 "user.user" 103 ] 104 } 105 }, 106 { 107 "comment": "scatter aggregate with non-aggregate expressions.", 108 "query": "select id, count(*) from user", 109 "v3-plan": { 110 "QueryType": "SELECT", 111 "Original": "select id, count(*) from user", 112 "Instructions": { 113 "OperatorType": "Aggregate", 114 "Variant": "Scalar", 115 "Aggregates": "sum_count(1) AS count", 116 "Inputs": [ 117 { 118 "OperatorType": "Route", 119 "Variant": "Scatter", 120 "Keyspace": { 121 "Name": "user", 122 "Sharded": true 123 }, 124 "FieldQuery": "select id, count(*) from `user` where 1 != 1", 125 "Query": "select id, count(*) from `user`", 126 "Table": "`user`" 127 } 128 ] 129 } 130 }, 131 "gen4-plan": { 132 "QueryType": "SELECT", 133 "Original": "select id, count(*) from user", 134 "Instructions": { 135 "OperatorType": "Aggregate", 136 "Variant": "Scalar", 137 "Aggregates": "random(0) AS id, sum_count_star(1) AS count(*)", 138 "Inputs": [ 139 { 140 "OperatorType": "Route", 141 "Variant": "Scatter", 142 "Keyspace": { 143 "Name": "user", 144 "Sharded": true 145 }, 146 "FieldQuery": "select id, count(*) from `user` where 1 != 1", 147 "Query": "select id, count(*) from `user`", 148 "Table": "`user`" 149 } 150 ] 151 }, 152 "TablesUsed": [ 153 "user.user" 154 ] 155 } 156 }, 157 { 158 "comment": "scatter aggregate using distinctdistinct", 159 "query": "select distinct col from user", 160 "v3-plan": { 161 "QueryType": "SELECT", 162 "Original": "select distinct col from user", 163 "Instructions": { 164 "OperatorType": "Aggregate", 165 "Variant": "Ordered", 166 "GroupBy": "0", 167 "Inputs": [ 168 { 169 "OperatorType": "Route", 170 "Variant": "Scatter", 171 "Keyspace": { 172 "Name": "user", 173 "Sharded": true 174 }, 175 "FieldQuery": "select col from `user` where 1 != 1", 176 "OrderBy": "0 ASC", 177 "Query": "select distinct col from `user` order by col asc", 178 "Table": "`user`" 179 } 180 ] 181 } 182 }, 183 "gen4-plan": { 184 "QueryType": "SELECT", 185 "Original": "select distinct col from user", 186 "Instructions": { 187 "OperatorType": "Aggregate", 188 "Variant": "Ordered", 189 "GroupBy": "0", 190 "Inputs": [ 191 { 192 "OperatorType": "Route", 193 "Variant": "Scatter", 194 "Keyspace": { 195 "Name": "user", 196 "Sharded": true 197 }, 198 "FieldQuery": "select col from `user` where 1 != 1", 199 "OrderBy": "0 ASC", 200 "Query": "select distinct col from `user` order by col asc", 201 "Table": "`user`" 202 } 203 ] 204 }, 205 "TablesUsed": [ 206 "user.user" 207 ] 208 } 209 }, 210 { 211 "comment": "scatter aggregate group by select col", 212 "query": "select col from user group by col", 213 "v3-plan": { 214 "QueryType": "SELECT", 215 "Original": "select col from user group by col", 216 "Instructions": { 217 "OperatorType": "Aggregate", 218 "Variant": "Ordered", 219 "GroupBy": "0", 220 "Inputs": [ 221 { 222 "OperatorType": "Route", 223 "Variant": "Scatter", 224 "Keyspace": { 225 "Name": "user", 226 "Sharded": true 227 }, 228 "FieldQuery": "select col from `user` where 1 != 1 group by col", 229 "OrderBy": "0 ASC", 230 "Query": "select col from `user` group by col order by col asc", 231 "Table": "`user`" 232 } 233 ] 234 } 235 }, 236 "gen4-plan": { 237 "QueryType": "SELECT", 238 "Original": "select col from user group by col", 239 "Instructions": { 240 "OperatorType": "Aggregate", 241 "Variant": "Ordered", 242 "GroupBy": "0", 243 "Inputs": [ 244 { 245 "OperatorType": "Route", 246 "Variant": "Scatter", 247 "Keyspace": { 248 "Name": "user", 249 "Sharded": true 250 }, 251 "FieldQuery": "select col from `user` where 1 != 1 group by col", 252 "OrderBy": "0 ASC", 253 "Query": "select col from `user` group by col order by col asc", 254 "Table": "`user`" 255 } 256 ] 257 }, 258 "TablesUsed": [ 259 "user.user" 260 ] 261 } 262 }, 263 { 264 "comment": "count with distinct group by unique vindex", 265 "query": "select id, count(distinct col) from user group by id", 266 "v3-plan": { 267 "QueryType": "SELECT", 268 "Original": "select id, count(distinct col) from user group by id", 269 "Instructions": { 270 "OperatorType": "Route", 271 "Variant": "Scatter", 272 "Keyspace": { 273 "Name": "user", 274 "Sharded": true 275 }, 276 "FieldQuery": "select id, count(distinct col) from `user` where 1 != 1 group by id", 277 "Query": "select id, count(distinct col) from `user` group by id", 278 "Table": "`user`" 279 } 280 }, 281 "gen4-plan": { 282 "QueryType": "SELECT", 283 "Original": "select id, count(distinct col) from user group by id", 284 "Instructions": { 285 "OperatorType": "Route", 286 "Variant": "Scatter", 287 "Keyspace": { 288 "Name": "user", 289 "Sharded": true 290 }, 291 "FieldQuery": "select id, count(distinct col) from `user` where 1 != 1 group by id", 292 "Query": "select id, count(distinct col) from `user` group by id", 293 "Table": "`user`" 294 }, 295 "TablesUsed": [ 296 "user.user" 297 ] 298 } 299 }, 300 { 301 "comment": "count with distinct unique vindex", 302 "query": "select col, count(distinct id) from user group by col", 303 "v3-plan": { 304 "QueryType": "SELECT", 305 "Original": "select col, count(distinct id) from user group by col", 306 "Instructions": { 307 "OperatorType": "Aggregate", 308 "Variant": "Ordered", 309 "Aggregates": "sum_count(1) AS count", 310 "GroupBy": "0", 311 "Inputs": [ 312 { 313 "OperatorType": "Route", 314 "Variant": "Scatter", 315 "Keyspace": { 316 "Name": "user", 317 "Sharded": true 318 }, 319 "FieldQuery": "select col, count(distinct id) from `user` where 1 != 1 group by col", 320 "OrderBy": "0 ASC", 321 "Query": "select col, count(distinct id) from `user` group by col order by col asc", 322 "Table": "`user`" 323 } 324 ] 325 } 326 }, 327 "gen4-plan": { 328 "QueryType": "SELECT", 329 "Original": "select col, count(distinct id) from user group by col", 330 "Instructions": { 331 "OperatorType": "Aggregate", 332 "Variant": "Ordered", 333 "Aggregates": "sum_count_distinct(1) AS count(distinct id)", 334 "GroupBy": "0", 335 "Inputs": [ 336 { 337 "OperatorType": "Route", 338 "Variant": "Scatter", 339 "Keyspace": { 340 "Name": "user", 341 "Sharded": true 342 }, 343 "FieldQuery": "select col, count(distinct id) from `user` where 1 != 1 group by col", 344 "OrderBy": "0 ASC", 345 "Query": "select col, count(distinct id) from `user` group by col order by col asc", 346 "Table": "`user`" 347 } 348 ] 349 }, 350 "TablesUsed": [ 351 "user.user" 352 ] 353 } 354 }, 355 { 356 "comment": "count with distinct no unique vindex", 357 "query": "select col1, count(distinct col2) from user group by col1", 358 "v3-plan": { 359 "QueryType": "SELECT", 360 "Original": "select col1, count(distinct col2) from user group by col1", 361 "Instructions": { 362 "OperatorType": "Aggregate", 363 "Variant": "Ordered", 364 "Aggregates": "count_distinct_count(1) AS count(distinct col2)", 365 "GroupBy": "0", 366 "Inputs": [ 367 { 368 "OperatorType": "Route", 369 "Variant": "Scatter", 370 "Keyspace": { 371 "Name": "user", 372 "Sharded": true 373 }, 374 "FieldQuery": "select col1, col2, weight_string(col1), weight_string(col2) from `user` where 1 != 1 group by col1, col2, weight_string(col1), weight_string(col2)", 375 "OrderBy": "(0|2) ASC, (1|3) ASC", 376 "Query": "select col1, col2, weight_string(col1), weight_string(col2) from `user` group by col1, col2, weight_string(col1), weight_string(col2) order by col1 asc, col2 asc", 377 "ResultColumns": 2, 378 "Table": "`user`" 379 } 380 ] 381 } 382 }, 383 "gen4-plan": { 384 "QueryType": "SELECT", 385 "Original": "select col1, count(distinct col2) from user group by col1", 386 "Instructions": { 387 "OperatorType": "Aggregate", 388 "Variant": "Ordered", 389 "Aggregates": "count_distinct(1|3) AS count(distinct col2)", 390 "GroupBy": "(0|2)", 391 "ResultColumns": 2, 392 "Inputs": [ 393 { 394 "OperatorType": "Route", 395 "Variant": "Scatter", 396 "Keyspace": { 397 "Name": "user", 398 "Sharded": true 399 }, 400 "FieldQuery": "select col1, col2, weight_string(col1), weight_string(col2) from `user` where 1 != 1 group by col1, weight_string(col1), col2, weight_string(col2)", 401 "OrderBy": "(0|2) ASC, (1|3) ASC", 402 "Query": "select col1, col2, weight_string(col1), weight_string(col2) from `user` group by col1, weight_string(col1), col2, weight_string(col2) order by col1 asc, col2 asc", 403 "Table": "`user`" 404 } 405 ] 406 }, 407 "TablesUsed": [ 408 "user.user" 409 ] 410 } 411 }, 412 { 413 "comment": "count with distinct no unique vindex and no group by", 414 "query": "select count(distinct col2) from user", 415 "v3-plan": { 416 "QueryType": "SELECT", 417 "Original": "select count(distinct col2) from user", 418 "Instructions": { 419 "OperatorType": "Aggregate", 420 "Variant": "Scalar", 421 "Aggregates": "count_distinct_count(0) AS count(distinct col2)", 422 "Inputs": [ 423 { 424 "OperatorType": "Route", 425 "Variant": "Scatter", 426 "Keyspace": { 427 "Name": "user", 428 "Sharded": true 429 }, 430 "FieldQuery": "select col2, weight_string(col2) from `user` where 1 != 1 group by col2, weight_string(col2)", 431 "OrderBy": "(0|1) ASC", 432 "Query": "select col2, weight_string(col2) from `user` group by col2, weight_string(col2) order by col2 asc", 433 "ResultColumns": 1, 434 "Table": "`user`" 435 } 436 ] 437 } 438 }, 439 "gen4-plan": { 440 "QueryType": "SELECT", 441 "Original": "select count(distinct col2) from user", 442 "Instructions": { 443 "OperatorType": "Aggregate", 444 "Variant": "Scalar", 445 "Aggregates": "count_distinct(0|1) AS count(distinct col2)", 446 "ResultColumns": 1, 447 "Inputs": [ 448 { 449 "OperatorType": "Route", 450 "Variant": "Scatter", 451 "Keyspace": { 452 "Name": "user", 453 "Sharded": true 454 }, 455 "FieldQuery": "select col2, weight_string(col2) from `user` where 1 != 1 group by col2, weight_string(col2)", 456 "OrderBy": "(0|1) ASC", 457 "Query": "select col2, weight_string(col2) from `user` group by col2, weight_string(col2) order by col2 asc", 458 "Table": "`user`" 459 } 460 ] 461 }, 462 "TablesUsed": [ 463 "user.user" 464 ] 465 } 466 }, 467 { 468 "comment": "count with distinct no unique vindex, count expression aliased", 469 "query": "select col1, count(distinct col2) c2 from user group by col1", 470 "v3-plan": { 471 "QueryType": "SELECT", 472 "Original": "select col1, count(distinct col2) c2 from user group by col1", 473 "Instructions": { 474 "OperatorType": "Aggregate", 475 "Variant": "Ordered", 476 "Aggregates": "count_distinct_count(1) AS c2", 477 "GroupBy": "0", 478 "Inputs": [ 479 { 480 "OperatorType": "Route", 481 "Variant": "Scatter", 482 "Keyspace": { 483 "Name": "user", 484 "Sharded": true 485 }, 486 "FieldQuery": "select col1, col2, weight_string(col1), weight_string(col2) from `user` where 1 != 1 group by col1, col2, weight_string(col1), weight_string(col2)", 487 "OrderBy": "(0|2) ASC, (1|3) ASC", 488 "Query": "select col1, col2, weight_string(col1), weight_string(col2) from `user` group by col1, col2, weight_string(col1), weight_string(col2) order by col1 asc, col2 asc", 489 "ResultColumns": 2, 490 "Table": "`user`" 491 } 492 ] 493 } 494 }, 495 "gen4-plan": { 496 "QueryType": "SELECT", 497 "Original": "select col1, count(distinct col2) c2 from user group by col1", 498 "Instructions": { 499 "OperatorType": "Aggregate", 500 "Variant": "Ordered", 501 "Aggregates": "count_distinct(1|3) AS c2", 502 "GroupBy": "(0|2)", 503 "ResultColumns": 2, 504 "Inputs": [ 505 { 506 "OperatorType": "Route", 507 "Variant": "Scatter", 508 "Keyspace": { 509 "Name": "user", 510 "Sharded": true 511 }, 512 "FieldQuery": "select col1, col2, weight_string(col1), weight_string(col2) from `user` where 1 != 1 group by col1, weight_string(col1), col2, weight_string(col2)", 513 "OrderBy": "(0|2) ASC, (1|3) ASC", 514 "Query": "select col1, col2, weight_string(col1), weight_string(col2) from `user` group by col1, weight_string(col1), col2, weight_string(col2) order by col1 asc, col2 asc", 515 "Table": "`user`" 516 } 517 ] 518 }, 519 "TablesUsed": [ 520 "user.user" 521 ] 522 } 523 }, 524 { 525 "comment": "sum with distinct no unique vindex", 526 "query": "select col1, sum(distinct col2) from user group by col1", 527 "v3-plan": { 528 "QueryType": "SELECT", 529 "Original": "select col1, sum(distinct col2) from user group by col1", 530 "Instructions": { 531 "OperatorType": "Aggregate", 532 "Variant": "Ordered", 533 "Aggregates": "sum_distinct_sum(1) AS sum(distinct col2)", 534 "GroupBy": "0", 535 "Inputs": [ 536 { 537 "OperatorType": "Route", 538 "Variant": "Scatter", 539 "Keyspace": { 540 "Name": "user", 541 "Sharded": true 542 }, 543 "FieldQuery": "select col1, col2, weight_string(col1), weight_string(col2) from `user` where 1 != 1 group by col1, col2, weight_string(col1), weight_string(col2)", 544 "OrderBy": "(0|2) ASC, (1|3) ASC", 545 "Query": "select col1, col2, weight_string(col1), weight_string(col2) from `user` group by col1, col2, weight_string(col1), weight_string(col2) order by col1 asc, col2 asc", 546 "ResultColumns": 2, 547 "Table": "`user`" 548 } 549 ] 550 } 551 }, 552 "gen4-plan": { 553 "QueryType": "SELECT", 554 "Original": "select col1, sum(distinct col2) from user group by col1", 555 "Instructions": { 556 "OperatorType": "Aggregate", 557 "Variant": "Ordered", 558 "Aggregates": "sum_distinct(1|3) AS sum(distinct col2)", 559 "GroupBy": "(0|2)", 560 "ResultColumns": 2, 561 "Inputs": [ 562 { 563 "OperatorType": "Route", 564 "Variant": "Scatter", 565 "Keyspace": { 566 "Name": "user", 567 "Sharded": true 568 }, 569 "FieldQuery": "select col1, col2, weight_string(col1), weight_string(col2) from `user` where 1 != 1 group by col1, weight_string(col1), col2, weight_string(col2)", 570 "OrderBy": "(0|2) ASC, (1|3) ASC", 571 "Query": "select col1, col2, weight_string(col1), weight_string(col2) from `user` group by col1, weight_string(col1), col2, weight_string(col2) order by col1 asc, col2 asc", 572 "Table": "`user`" 573 } 574 ] 575 }, 576 "TablesUsed": [ 577 "user.user" 578 ] 579 } 580 }, 581 { 582 "comment": "min with distinct no unique vindex. distinct is ignored.", 583 "query": "select col1, min(distinct col2) from user group by col1", 584 "v3-plan": { 585 "QueryType": "SELECT", 586 "Original": "select col1, min(distinct col2) from user group by col1", 587 "Instructions": { 588 "OperatorType": "Aggregate", 589 "Variant": "Ordered", 590 "Aggregates": "min(1)", 591 "GroupBy": "0", 592 "Inputs": [ 593 { 594 "OperatorType": "Route", 595 "Variant": "Scatter", 596 "Keyspace": { 597 "Name": "user", 598 "Sharded": true 599 }, 600 "FieldQuery": "select col1, min(distinct col2), weight_string(col1) from `user` where 1 != 1 group by col1, weight_string(col1)", 601 "OrderBy": "(0|2) ASC", 602 "Query": "select col1, min(distinct col2), weight_string(col1) from `user` group by col1, weight_string(col1) order by col1 asc", 603 "ResultColumns": 2, 604 "Table": "`user`" 605 } 606 ] 607 } 608 }, 609 "gen4-plan": { 610 "QueryType": "SELECT", 611 "Original": "select col1, min(distinct col2) from user group by col1", 612 "Instructions": { 613 "OperatorType": "Aggregate", 614 "Variant": "Ordered", 615 "Aggregates": "min(1|3) AS min(distinct col2)", 616 "GroupBy": "(0|2)", 617 "ResultColumns": 2, 618 "Inputs": [ 619 { 620 "OperatorType": "Route", 621 "Variant": "Scatter", 622 "Keyspace": { 623 "Name": "user", 624 "Sharded": true 625 }, 626 "FieldQuery": "select col1, col2, weight_string(col1), weight_string(col2) from `user` where 1 != 1 group by col1, weight_string(col1), col2, weight_string(col2)", 627 "OrderBy": "(0|2) ASC, (1|3) ASC", 628 "Query": "select col1, col2, weight_string(col1), weight_string(col2) from `user` group by col1, weight_string(col1), col2, weight_string(col2) order by col1 asc, col2 asc", 629 "Table": "`user`" 630 } 631 ] 632 }, 633 "TablesUsed": [ 634 "user.user" 635 ] 636 } 637 }, 638 { 639 "comment": "order by count distinct", 640 "query": "select col1, count(distinct col2) k from user group by col1 order by k", 641 "v3-plan": { 642 "QueryType": "SELECT", 643 "Original": "select col1, count(distinct col2) k from user group by col1 order by k", 644 "Instructions": { 645 "OperatorType": "Sort", 646 "Variant": "Memory", 647 "OrderBy": "1 ASC", 648 "Inputs": [ 649 { 650 "OperatorType": "Aggregate", 651 "Variant": "Ordered", 652 "Aggregates": "count_distinct_count(1) AS k", 653 "GroupBy": "0", 654 "Inputs": [ 655 { 656 "OperatorType": "Route", 657 "Variant": "Scatter", 658 "Keyspace": { 659 "Name": "user", 660 "Sharded": true 661 }, 662 "FieldQuery": "select col1, col2, weight_string(col1), weight_string(col2) from `user` where 1 != 1 group by col1, col2, weight_string(col1), weight_string(col2)", 663 "OrderBy": "(0|2) ASC, (1|3) ASC", 664 "Query": "select col1, col2, weight_string(col1), weight_string(col2) from `user` group by col1, col2, weight_string(col1), weight_string(col2) order by col1 asc, col2 asc", 665 "ResultColumns": 2, 666 "Table": "`user`" 667 } 668 ] 669 } 670 ] 671 } 672 }, 673 "gen4-plan": { 674 "QueryType": "SELECT", 675 "Original": "select col1, count(distinct col2) k from user group by col1 order by k", 676 "Instructions": { 677 "OperatorType": "Sort", 678 "Variant": "Memory", 679 "OrderBy": "1 ASC", 680 "ResultColumns": 2, 681 "Inputs": [ 682 { 683 "OperatorType": "Aggregate", 684 "Variant": "Ordered", 685 "Aggregates": "count_distinct(1|3) AS k", 686 "GroupBy": "(0|2)", 687 "Inputs": [ 688 { 689 "OperatorType": "Route", 690 "Variant": "Scatter", 691 "Keyspace": { 692 "Name": "user", 693 "Sharded": true 694 }, 695 "FieldQuery": "select col1, col2, weight_string(col1), weight_string(col2) from `user` where 1 != 1 group by col1, weight_string(col1), col2, weight_string(col2)", 696 "OrderBy": "(0|2) ASC, (1|3) ASC", 697 "Query": "select col1, col2, weight_string(col1), weight_string(col2) from `user` group by col1, weight_string(col1), col2, weight_string(col2) order by col1 asc, col2 asc", 698 "Table": "`user`" 699 } 700 ] 701 } 702 ] 703 }, 704 "TablesUsed": [ 705 "user.user" 706 ] 707 } 708 }, 709 { 710 "comment": "scatter aggregate group by aggregate function", 711 "query": "select count(*) b from user group by b", 712 "v3-plan": "VT03005: cannot group on 'b'", 713 "gen4-plan": "VT03005: cannot group on 'count(*)'" 714 }, 715 { 716 "comment": "scatter aggregate multiple group by (columns)", 717 "query": "select a, b, count(*) from user group by b, a", 718 "v3-plan": { 719 "QueryType": "SELECT", 720 "Original": "select a, b, count(*) from user group by b, a", 721 "Instructions": { 722 "OperatorType": "Aggregate", 723 "Variant": "Ordered", 724 "Aggregates": "sum_count(2) AS count", 725 "GroupBy": "1, 0", 726 "Inputs": [ 727 { 728 "OperatorType": "Route", 729 "Variant": "Scatter", 730 "Keyspace": { 731 "Name": "user", 732 "Sharded": true 733 }, 734 "FieldQuery": "select a, b, count(*), weight_string(b), weight_string(a) from `user` where 1 != 1 group by b, a, weight_string(b), weight_string(a)", 735 "OrderBy": "(1|3) ASC, (0|4) ASC", 736 "Query": "select a, b, count(*), weight_string(b), weight_string(a) from `user` group by b, a, weight_string(b), weight_string(a) order by b asc, a asc", 737 "ResultColumns": 3, 738 "Table": "`user`" 739 } 740 ] 741 } 742 }, 743 "gen4-plan": { 744 "QueryType": "SELECT", 745 "Original": "select a, b, count(*) from user group by b, a", 746 "Instructions": { 747 "OperatorType": "Aggregate", 748 "Variant": "Ordered", 749 "Aggregates": "sum_count_star(2) AS count(*)", 750 "GroupBy": "(0|3), (1|4)", 751 "ResultColumns": 3, 752 "Inputs": [ 753 { 754 "OperatorType": "Route", 755 "Variant": "Scatter", 756 "Keyspace": { 757 "Name": "user", 758 "Sharded": true 759 }, 760 "FieldQuery": "select a, b, count(*), weight_string(a), weight_string(b) from `user` where 1 != 1 group by a, weight_string(a), b, weight_string(b)", 761 "OrderBy": "(0|3) ASC, (1|4) ASC", 762 "Query": "select a, b, count(*), weight_string(a), weight_string(b) from `user` group by a, weight_string(a), b, weight_string(b) order by a asc, b asc", 763 "Table": "`user`" 764 } 765 ] 766 }, 767 "TablesUsed": [ 768 "user.user" 769 ] 770 } 771 }, 772 { 773 "comment": "scatter aggregate multiple group by (numbers)", 774 "query": "select a, b, count(*) from user group by 2, 1", 775 "v3-plan": { 776 "QueryType": "SELECT", 777 "Original": "select a, b, count(*) from user group by 2, 1", 778 "Instructions": { 779 "OperatorType": "Aggregate", 780 "Variant": "Ordered", 781 "Aggregates": "sum_count(2) AS count", 782 "GroupBy": "1, 0", 783 "Inputs": [ 784 { 785 "OperatorType": "Route", 786 "Variant": "Scatter", 787 "Keyspace": { 788 "Name": "user", 789 "Sharded": true 790 }, 791 "FieldQuery": "select a, b, count(*), weight_string(b), weight_string(a) from `user` where 1 != 1 group by 2, 1, weight_string(b), weight_string(a)", 792 "OrderBy": "(1|3) ASC, (0|4) ASC", 793 "Query": "select a, b, count(*), weight_string(b), weight_string(a) from `user` group by 2, 1, weight_string(b), weight_string(a) order by b asc, a asc", 794 "ResultColumns": 3, 795 "Table": "`user`" 796 } 797 ] 798 } 799 }, 800 "gen4-plan": { 801 "QueryType": "SELECT", 802 "Original": "select a, b, count(*) from user group by 2, 1", 803 "Instructions": { 804 "OperatorType": "Aggregate", 805 "Variant": "Ordered", 806 "Aggregates": "sum_count_star(2) AS count(*)", 807 "GroupBy": "(0|3), (1|4)", 808 "ResultColumns": 3, 809 "Inputs": [ 810 { 811 "OperatorType": "Route", 812 "Variant": "Scatter", 813 "Keyspace": { 814 "Name": "user", 815 "Sharded": true 816 }, 817 "FieldQuery": "select a, b, count(*), weight_string(a), weight_string(b) from `user` where 1 != 1 group by a, weight_string(a), b, weight_string(b)", 818 "OrderBy": "(0|3) ASC, (1|4) ASC", 819 "Query": "select a, b, count(*), weight_string(a), weight_string(b) from `user` group by a, weight_string(a), b, weight_string(b) order by a asc, b asc", 820 "Table": "`user`" 821 } 822 ] 823 }, 824 "TablesUsed": [ 825 "user.user" 826 ] 827 } 828 }, 829 { 830 "comment": "scatter aggregate multiple group by columns inverse order", 831 "query": "select a, b, count(*) from user group by b, a", 832 "v3-plan": { 833 "QueryType": "SELECT", 834 "Original": "select a, b, count(*) from user group by b, a", 835 "Instructions": { 836 "OperatorType": "Aggregate", 837 "Variant": "Ordered", 838 "Aggregates": "sum_count(2) AS count", 839 "GroupBy": "1, 0", 840 "Inputs": [ 841 { 842 "OperatorType": "Route", 843 "Variant": "Scatter", 844 "Keyspace": { 845 "Name": "user", 846 "Sharded": true 847 }, 848 "FieldQuery": "select a, b, count(*), weight_string(b), weight_string(a) from `user` where 1 != 1 group by b, a, weight_string(b), weight_string(a)", 849 "OrderBy": "(1|3) ASC, (0|4) ASC", 850 "Query": "select a, b, count(*), weight_string(b), weight_string(a) from `user` group by b, a, weight_string(b), weight_string(a) order by b asc, a asc", 851 "ResultColumns": 3, 852 "Table": "`user`" 853 } 854 ] 855 } 856 }, 857 "gen4-plan": { 858 "QueryType": "SELECT", 859 "Original": "select a, b, count(*) from user group by b, a", 860 "Instructions": { 861 "OperatorType": "Aggregate", 862 "Variant": "Ordered", 863 "Aggregates": "sum_count_star(2) AS count(*)", 864 "GroupBy": "(0|3), (1|4)", 865 "ResultColumns": 3, 866 "Inputs": [ 867 { 868 "OperatorType": "Route", 869 "Variant": "Scatter", 870 "Keyspace": { 871 "Name": "user", 872 "Sharded": true 873 }, 874 "FieldQuery": "select a, b, count(*), weight_string(a), weight_string(b) from `user` where 1 != 1 group by a, weight_string(a), b, weight_string(b)", 875 "OrderBy": "(0|3) ASC, (1|4) ASC", 876 "Query": "select a, b, count(*), weight_string(a), weight_string(b) from `user` group by a, weight_string(a), b, weight_string(b) order by a asc, b asc", 877 "Table": "`user`" 878 } 879 ] 880 }, 881 "TablesUsed": [ 882 "user.user" 883 ] 884 } 885 }, 886 { 887 "comment": "scatter aggregate group by column number", 888 "query": "select col from user group by 1", 889 "v3-plan": { 890 "QueryType": "SELECT", 891 "Original": "select col from user group by 1", 892 "Instructions": { 893 "OperatorType": "Aggregate", 894 "Variant": "Ordered", 895 "GroupBy": "0", 896 "Inputs": [ 897 { 898 "OperatorType": "Route", 899 "Variant": "Scatter", 900 "Keyspace": { 901 "Name": "user", 902 "Sharded": true 903 }, 904 "FieldQuery": "select col from `user` where 1 != 1 group by 1", 905 "OrderBy": "0 ASC", 906 "Query": "select col from `user` group by 1 order by col asc", 907 "Table": "`user`" 908 } 909 ] 910 } 911 }, 912 "gen4-plan": { 913 "QueryType": "SELECT", 914 "Original": "select col from user group by 1", 915 "Instructions": { 916 "OperatorType": "Aggregate", 917 "Variant": "Ordered", 918 "GroupBy": "0", 919 "Inputs": [ 920 { 921 "OperatorType": "Route", 922 "Variant": "Scatter", 923 "Keyspace": { 924 "Name": "user", 925 "Sharded": true 926 }, 927 "FieldQuery": "select col from `user` where 1 != 1 group by col", 928 "OrderBy": "0 ASC", 929 "Query": "select col from `user` group by col order by col asc", 930 "Table": "`user`" 931 } 932 ] 933 }, 934 "TablesUsed": [ 935 "user.user" 936 ] 937 } 938 }, 939 { 940 "comment": "scatter aggregate group by invalid column number", 941 "query": "select col from user group by 2", 942 "v3-plan": "VT03014: unknown column '2' in 'group statement'", 943 "gen4-plan": "Unknown column '2' in 'group statement'" 944 }, 945 { 946 "comment": "scatter aggregate order by null", 947 "query": "select count(*) from user order by null", 948 "v3-plan": { 949 "QueryType": "SELECT", 950 "Original": "select count(*) from user order by null", 951 "Instructions": { 952 "OperatorType": "Aggregate", 953 "Variant": "Scalar", 954 "Aggregates": "sum_count(0) AS count", 955 "Inputs": [ 956 { 957 "OperatorType": "Route", 958 "Variant": "Scatter", 959 "Keyspace": { 960 "Name": "user", 961 "Sharded": true 962 }, 963 "FieldQuery": "select count(*) from `user` where 1 != 1", 964 "Query": "select count(*) from `user`", 965 "Table": "`user`" 966 } 967 ] 968 } 969 }, 970 "gen4-plan": { 971 "QueryType": "SELECT", 972 "Original": "select count(*) from user order by null", 973 "Instructions": { 974 "OperatorType": "Aggregate", 975 "Variant": "Scalar", 976 "Aggregates": "sum_count_star(0) AS count(*)", 977 "Inputs": [ 978 { 979 "OperatorType": "Route", 980 "Variant": "Scatter", 981 "Keyspace": { 982 "Name": "user", 983 "Sharded": true 984 }, 985 "FieldQuery": "select count(*) from `user` where 1 != 1", 986 "Query": "select count(*) from `user`", 987 "Table": "`user`" 988 } 989 ] 990 }, 991 "TablesUsed": [ 992 "user.user" 993 ] 994 } 995 }, 996 { 997 "comment": "scatter aggregate with numbered order by columns", 998 "query": "select a, b, c, d, count(*) from user group by 1, 2, 3 order by 1, 2, 3", 999 "v3-plan": { 1000 "QueryType": "SELECT", 1001 "Original": "select a, b, c, d, count(*) from user group by 1, 2, 3 order by 1, 2, 3", 1002 "Instructions": { 1003 "OperatorType": "Aggregate", 1004 "Variant": "Ordered", 1005 "Aggregates": "sum_count(4) AS count", 1006 "GroupBy": "0, 1, 2", 1007 "Inputs": [ 1008 { 1009 "OperatorType": "Route", 1010 "Variant": "Scatter", 1011 "Keyspace": { 1012 "Name": "user", 1013 "Sharded": true 1014 }, 1015 "FieldQuery": "select a, b, c, d, count(*), weight_string(a), weight_string(b), weight_string(c) from `user` where 1 != 1 group by 1, 2, 3, weight_string(a), weight_string(b), weight_string(c)", 1016 "OrderBy": "(0|5) ASC, (1|6) ASC, (2|7) ASC", 1017 "Query": "select a, b, c, d, count(*), weight_string(a), weight_string(b), weight_string(c) from `user` group by 1, 2, 3, weight_string(a), weight_string(b), weight_string(c) order by 1 asc, 2 asc, 3 asc", 1018 "ResultColumns": 5, 1019 "Table": "`user`" 1020 } 1021 ] 1022 } 1023 }, 1024 "gen4-plan": { 1025 "QueryType": "SELECT", 1026 "Original": "select a, b, c, d, count(*) from user group by 1, 2, 3 order by 1, 2, 3", 1027 "Instructions": { 1028 "OperatorType": "Aggregate", 1029 "Variant": "Ordered", 1030 "Aggregates": "random(3) AS d, sum_count_star(4) AS count(*)", 1031 "GroupBy": "(0|5), (1|6), (2|7)", 1032 "ResultColumns": 5, 1033 "Inputs": [ 1034 { 1035 "OperatorType": "Route", 1036 "Variant": "Scatter", 1037 "Keyspace": { 1038 "Name": "user", 1039 "Sharded": true 1040 }, 1041 "FieldQuery": "select a, b, c, d, count(*), weight_string(a), weight_string(b), weight_string(c) from `user` where 1 != 1 group by a, weight_string(a), b, weight_string(b), c, weight_string(c)", 1042 "OrderBy": "(0|5) ASC, (1|6) ASC, (2|7) ASC", 1043 "Query": "select a, b, c, d, count(*), weight_string(a), weight_string(b), weight_string(c) from `user` group by a, weight_string(a), b, weight_string(b), c, weight_string(c) order by a asc, b asc, c asc", 1044 "Table": "`user`" 1045 } 1046 ] 1047 }, 1048 "TablesUsed": [ 1049 "user.user" 1050 ] 1051 } 1052 }, 1053 { 1054 "comment": "scatter aggregate with named order by columns", 1055 "query": "select a, b, c, d, count(*) from user group by 1, 2, 3 order by a, b, c", 1056 "v3-plan": { 1057 "QueryType": "SELECT", 1058 "Original": "select a, b, c, d, count(*) from user group by 1, 2, 3 order by a, b, c", 1059 "Instructions": { 1060 "OperatorType": "Aggregate", 1061 "Variant": "Ordered", 1062 "Aggregates": "sum_count(4) AS count", 1063 "GroupBy": "0, 1, 2", 1064 "Inputs": [ 1065 { 1066 "OperatorType": "Route", 1067 "Variant": "Scatter", 1068 "Keyspace": { 1069 "Name": "user", 1070 "Sharded": true 1071 }, 1072 "FieldQuery": "select a, b, c, d, count(*), weight_string(a), weight_string(b), weight_string(c) from `user` where 1 != 1 group by 1, 2, 3, weight_string(a), weight_string(b), weight_string(c)", 1073 "OrderBy": "(0|5) ASC, (1|6) ASC, (2|7) ASC", 1074 "Query": "select a, b, c, d, count(*), weight_string(a), weight_string(b), weight_string(c) from `user` group by 1, 2, 3, weight_string(a), weight_string(b), weight_string(c) order by a asc, b asc, c asc", 1075 "ResultColumns": 5, 1076 "Table": "`user`" 1077 } 1078 ] 1079 } 1080 }, 1081 "gen4-plan": { 1082 "QueryType": "SELECT", 1083 "Original": "select a, b, c, d, count(*) from user group by 1, 2, 3 order by a, b, c", 1084 "Instructions": { 1085 "OperatorType": "Aggregate", 1086 "Variant": "Ordered", 1087 "Aggregates": "random(3) AS d, sum_count_star(4) AS count(*)", 1088 "GroupBy": "(0|5), (1|6), (2|7)", 1089 "ResultColumns": 5, 1090 "Inputs": [ 1091 { 1092 "OperatorType": "Route", 1093 "Variant": "Scatter", 1094 "Keyspace": { 1095 "Name": "user", 1096 "Sharded": true 1097 }, 1098 "FieldQuery": "select a, b, c, d, count(*), weight_string(a), weight_string(b), weight_string(c) from `user` where 1 != 1 group by a, weight_string(a), b, weight_string(b), c, weight_string(c)", 1099 "OrderBy": "(0|5) ASC, (1|6) ASC, (2|7) ASC", 1100 "Query": "select a, b, c, d, count(*), weight_string(a), weight_string(b), weight_string(c) from `user` group by a, weight_string(a), b, weight_string(b), c, weight_string(c) order by a asc, b asc, c asc", 1101 "Table": "`user`" 1102 } 1103 ] 1104 }, 1105 "TablesUsed": [ 1106 "user.user" 1107 ] 1108 } 1109 }, 1110 { 1111 "comment": "scatter aggregate with jumbled order by columns", 1112 "query": "select a, b, c, d, count(*) from user group by 1, 2, 3, 4 order by d, b, a, c", 1113 "v3-plan": { 1114 "QueryType": "SELECT", 1115 "Original": "select a, b, c, d, count(*) from user group by 1, 2, 3, 4 order by d, b, a, c", 1116 "Instructions": { 1117 "OperatorType": "Aggregate", 1118 "Variant": "Ordered", 1119 "Aggregates": "sum_count(4) AS count", 1120 "GroupBy": "0, 1, 2, 3", 1121 "Inputs": [ 1122 { 1123 "OperatorType": "Route", 1124 "Variant": "Scatter", 1125 "Keyspace": { 1126 "Name": "user", 1127 "Sharded": true 1128 }, 1129 "FieldQuery": "select a, b, c, d, count(*), weight_string(d), weight_string(b), weight_string(a), weight_string(c) from `user` where 1 != 1 group by 1, 2, 3, 4, weight_string(d), weight_string(b), weight_string(a), weight_string(c)", 1130 "OrderBy": "(3|5) ASC, (1|6) ASC, (0|7) ASC, (2|8) ASC", 1131 "Query": "select a, b, c, d, count(*), weight_string(d), weight_string(b), weight_string(a), weight_string(c) from `user` group by 1, 2, 3, 4, weight_string(d), weight_string(b), weight_string(a), weight_string(c) order by d asc, b asc, a asc, c asc", 1132 "ResultColumns": 5, 1133 "Table": "`user`" 1134 } 1135 ] 1136 } 1137 }, 1138 "gen4-plan": { 1139 "QueryType": "SELECT", 1140 "Original": "select a, b, c, d, count(*) from user group by 1, 2, 3, 4 order by d, b, a, c", 1141 "Instructions": { 1142 "OperatorType": "Aggregate", 1143 "Variant": "Ordered", 1144 "Aggregates": "sum_count_star(4) AS count(*)", 1145 "GroupBy": "(3|8), (1|6), (0|5), (2|7)", 1146 "ResultColumns": 5, 1147 "Inputs": [ 1148 { 1149 "OperatorType": "Route", 1150 "Variant": "Scatter", 1151 "Keyspace": { 1152 "Name": "user", 1153 "Sharded": true 1154 }, 1155 "FieldQuery": "select a, b, c, d, count(*), weight_string(a), weight_string(b), weight_string(c), weight_string(d) from `user` where 1 != 1 group by a, weight_string(a), b, weight_string(b), c, weight_string(c), d, weight_string(d)", 1156 "OrderBy": "(3|8) ASC, (1|6) ASC, (0|5) ASC, (2|7) ASC", 1157 "Query": "select a, b, c, d, count(*), weight_string(a), weight_string(b), weight_string(c), weight_string(d) from `user` group by a, weight_string(a), b, weight_string(b), c, weight_string(c), d, weight_string(d) order by d asc, b asc, a asc, c asc", 1158 "Table": "`user`" 1159 } 1160 ] 1161 }, 1162 "TablesUsed": [ 1163 "user.user" 1164 ] 1165 } 1166 }, 1167 { 1168 "comment": "scatter aggregate with jumbled group by and order by columns", 1169 "query": "select a, b, c, d, count(*) from user group by 3, 2, 1, 4 order by d, b, a, c", 1170 "v3-plan": { 1171 "QueryType": "SELECT", 1172 "Original": "select a, b, c, d, count(*) from user group by 3, 2, 1, 4 order by d, b, a, c", 1173 "Instructions": { 1174 "OperatorType": "Aggregate", 1175 "Variant": "Ordered", 1176 "Aggregates": "sum_count(4) AS count", 1177 "GroupBy": "2, 1, 0, 3", 1178 "Inputs": [ 1179 { 1180 "OperatorType": "Route", 1181 "Variant": "Scatter", 1182 "Keyspace": { 1183 "Name": "user", 1184 "Sharded": true 1185 }, 1186 "FieldQuery": "select a, b, c, d, count(*), weight_string(d), weight_string(b), weight_string(a), weight_string(c) from `user` where 1 != 1 group by 3, 2, 1, 4, weight_string(d), weight_string(b), weight_string(a), weight_string(c)", 1187 "OrderBy": "(3|5) ASC, (1|6) ASC, (0|7) ASC, (2|8) ASC", 1188 "Query": "select a, b, c, d, count(*), weight_string(d), weight_string(b), weight_string(a), weight_string(c) from `user` group by 3, 2, 1, 4, weight_string(d), weight_string(b), weight_string(a), weight_string(c) order by d asc, b asc, a asc, c asc", 1189 "ResultColumns": 5, 1190 "Table": "`user`" 1191 } 1192 ] 1193 } 1194 }, 1195 "gen4-plan": { 1196 "QueryType": "SELECT", 1197 "Original": "select a, b, c, d, count(*) from user group by 3, 2, 1, 4 order by d, b, a, c", 1198 "Instructions": { 1199 "OperatorType": "Aggregate", 1200 "Variant": "Ordered", 1201 "Aggregates": "sum_count_star(4) AS count(*)", 1202 "GroupBy": "(3|8), (1|6), (0|5), (2|7)", 1203 "ResultColumns": 5, 1204 "Inputs": [ 1205 { 1206 "OperatorType": "Route", 1207 "Variant": "Scatter", 1208 "Keyspace": { 1209 "Name": "user", 1210 "Sharded": true 1211 }, 1212 "FieldQuery": "select a, b, c, d, count(*), weight_string(a), weight_string(b), weight_string(c), weight_string(d) from `user` where 1 != 1 group by a, weight_string(a), b, weight_string(b), c, weight_string(c), d, weight_string(d)", 1213 "OrderBy": "(3|8) ASC, (1|6) ASC, (0|5) ASC, (2|7) ASC", 1214 "Query": "select a, b, c, d, count(*), weight_string(a), weight_string(b), weight_string(c), weight_string(d) from `user` group by a, weight_string(a), b, weight_string(b), c, weight_string(c), d, weight_string(d) order by d asc, b asc, a asc, c asc", 1215 "Table": "`user`" 1216 } 1217 ] 1218 }, 1219 "TablesUsed": [ 1220 "user.user" 1221 ] 1222 } 1223 }, 1224 { 1225 "comment": "scatter aggregate with some descending order by cols", 1226 "query": "select a, b, c, count(*) from user group by 3, 2, 1 order by 1 desc, 3 desc, b", 1227 "v3-plan": { 1228 "QueryType": "SELECT", 1229 "Original": "select a, b, c, count(*) from user group by 3, 2, 1 order by 1 desc, 3 desc, b", 1230 "Instructions": { 1231 "OperatorType": "Aggregate", 1232 "Variant": "Ordered", 1233 "Aggregates": "sum_count(3) AS count", 1234 "GroupBy": "2, 1, 0", 1235 "Inputs": [ 1236 { 1237 "OperatorType": "Route", 1238 "Variant": "Scatter", 1239 "Keyspace": { 1240 "Name": "user", 1241 "Sharded": true 1242 }, 1243 "FieldQuery": "select a, b, c, count(*), weight_string(a), weight_string(c), weight_string(b) from `user` where 1 != 1 group by 3, 2, 1, weight_string(a), weight_string(c), weight_string(b)", 1244 "OrderBy": "(0|4) DESC, (2|5) DESC, (1|6) ASC", 1245 "Query": "select a, b, c, count(*), weight_string(a), weight_string(c), weight_string(b) from `user` group by 3, 2, 1, weight_string(a), weight_string(c), weight_string(b) order by 1 desc, 3 desc, b asc", 1246 "ResultColumns": 4, 1247 "Table": "`user`" 1248 } 1249 ] 1250 } 1251 }, 1252 "gen4-plan": { 1253 "QueryType": "SELECT", 1254 "Original": "select a, b, c, count(*) from user group by 3, 2, 1 order by 1 desc, 3 desc, b", 1255 "Instructions": { 1256 "OperatorType": "Aggregate", 1257 "Variant": "Ordered", 1258 "Aggregates": "sum_count_star(3) AS count(*)", 1259 "GroupBy": "(0|4), (2|6), (1|5)", 1260 "ResultColumns": 4, 1261 "Inputs": [ 1262 { 1263 "OperatorType": "Route", 1264 "Variant": "Scatter", 1265 "Keyspace": { 1266 "Name": "user", 1267 "Sharded": true 1268 }, 1269 "FieldQuery": "select a, b, c, count(*), weight_string(a), weight_string(b), weight_string(c) from `user` where 1 != 1 group by a, weight_string(a), b, weight_string(b), c, weight_string(c)", 1270 "OrderBy": "(0|4) DESC, (2|6) DESC, (1|5) ASC", 1271 "Query": "select a, b, c, count(*), weight_string(a), weight_string(b), weight_string(c) from `user` group by a, weight_string(a), b, weight_string(b), c, weight_string(c) order by a desc, c desc, b asc", 1272 "Table": "`user`" 1273 } 1274 ] 1275 }, 1276 "TablesUsed": [ 1277 "user.user" 1278 ] 1279 } 1280 }, 1281 { 1282 "comment": "invalid order by column numner for scatter", 1283 "query": "select col, count(*) from user group by col order by 5 limit 10", 1284 "v3-plan": "VT03014: unknown column '5' in 'order clause'", 1285 "gen4-plan": "Unknown column '5' in 'order clause'" 1286 }, 1287 { 1288 "comment": "aggregate with limit", 1289 "query": "select col, count(*) from user group by col limit 10", 1290 "v3-plan": { 1291 "QueryType": "SELECT", 1292 "Original": "select col, count(*) from user group by col limit 10", 1293 "Instructions": { 1294 "OperatorType": "Limit", 1295 "Count": "INT64(10)", 1296 "Inputs": [ 1297 { 1298 "OperatorType": "Aggregate", 1299 "Variant": "Ordered", 1300 "Aggregates": "sum_count(1) AS count", 1301 "GroupBy": "0", 1302 "Inputs": [ 1303 { 1304 "OperatorType": "Route", 1305 "Variant": "Scatter", 1306 "Keyspace": { 1307 "Name": "user", 1308 "Sharded": true 1309 }, 1310 "FieldQuery": "select col, count(*) from `user` where 1 != 1 group by col", 1311 "OrderBy": "0 ASC", 1312 "Query": "select col, count(*) from `user` group by col order by col asc limit :__upper_limit", 1313 "Table": "`user`" 1314 } 1315 ] 1316 } 1317 ] 1318 } 1319 }, 1320 "gen4-plan": { 1321 "QueryType": "SELECT", 1322 "Original": "select col, count(*) from user group by col limit 10", 1323 "Instructions": { 1324 "OperatorType": "Limit", 1325 "Count": "INT64(10)", 1326 "Inputs": [ 1327 { 1328 "OperatorType": "Aggregate", 1329 "Variant": "Ordered", 1330 "Aggregates": "sum_count_star(1) AS count(*)", 1331 "GroupBy": "0", 1332 "Inputs": [ 1333 { 1334 "OperatorType": "Route", 1335 "Variant": "Scatter", 1336 "Keyspace": { 1337 "Name": "user", 1338 "Sharded": true 1339 }, 1340 "FieldQuery": "select col, count(*) from `user` where 1 != 1 group by col", 1341 "OrderBy": "0 ASC", 1342 "Query": "select col, count(*) from `user` group by col order by col asc limit :__upper_limit", 1343 "Table": "`user`" 1344 } 1345 ] 1346 } 1347 ] 1348 }, 1349 "TablesUsed": [ 1350 "user.user" 1351 ] 1352 } 1353 }, 1354 { 1355 "comment": "Group by with collate operator", 1356 "query": "select user.col1 as a from user where user.id = 5 group by a collate utf8_general_ci", 1357 "v3-plan": { 1358 "QueryType": "SELECT", 1359 "Original": "select user.col1 as a from user where user.id = 5 group by a collate utf8_general_ci", 1360 "Instructions": { 1361 "OperatorType": "Route", 1362 "Variant": "EqualUnique", 1363 "Keyspace": { 1364 "Name": "user", 1365 "Sharded": true 1366 }, 1367 "FieldQuery": "select `user`.col1 as a from `user` where 1 != 1 group by a collate utf8_general_ci", 1368 "Query": "select `user`.col1 as a from `user` where `user`.id = 5 group by a collate utf8_general_ci", 1369 "Table": "`user`", 1370 "Values": [ 1371 "INT64(5)" 1372 ], 1373 "Vindex": "user_index" 1374 } 1375 }, 1376 "gen4-plan": { 1377 "QueryType": "SELECT", 1378 "Original": "select user.col1 as a from user where user.id = 5 group by a collate utf8_general_ci", 1379 "Instructions": { 1380 "OperatorType": "Route", 1381 "Variant": "EqualUnique", 1382 "Keyspace": { 1383 "Name": "user", 1384 "Sharded": true 1385 }, 1386 "FieldQuery": "select `user`.col1 as a from `user` where 1 != 1 group by a collate utf8_general_ci", 1387 "Query": "select `user`.col1 as a from `user` where `user`.id = 5 group by a collate utf8_general_ci", 1388 "Table": "`user`", 1389 "Values": [ 1390 "INT64(5)" 1391 ], 1392 "Vindex": "user_index" 1393 }, 1394 "TablesUsed": [ 1395 "user.user" 1396 ] 1397 } 1398 }, 1399 { 1400 "comment": "routing rules for aggregates", 1401 "query": "select id, count(*) from route2 group by id", 1402 "v3-plan": { 1403 "QueryType": "SELECT", 1404 "Original": "select id, count(*) from route2 group by id", 1405 "Instructions": { 1406 "OperatorType": "Route", 1407 "Variant": "Unsharded", 1408 "Keyspace": { 1409 "Name": "main", 1410 "Sharded": false 1411 }, 1412 "FieldQuery": "select id, count(*) from unsharded as route2 where 1 != 1 group by id", 1413 "Query": "select id, count(*) from unsharded as route2 group by id", 1414 "Table": "unsharded" 1415 } 1416 }, 1417 "gen4-plan": { 1418 "QueryType": "SELECT", 1419 "Original": "select id, count(*) from route2 group by id", 1420 "Instructions": { 1421 "OperatorType": "Route", 1422 "Variant": "Unsharded", 1423 "Keyspace": { 1424 "Name": "main", 1425 "Sharded": false 1426 }, 1427 "FieldQuery": "select id, count(*) from unsharded as route2 where 1 != 1 group by id", 1428 "Query": "select id, count(*) from unsharded as route2 group by id", 1429 "Table": "unsharded" 1430 }, 1431 "TablesUsed": [ 1432 "main.unsharded" 1433 ] 1434 } 1435 }, 1436 { 1437 "comment": "order by on a reference table", 1438 "query": "select col from ref order by col", 1439 "v3-plan": { 1440 "QueryType": "SELECT", 1441 "Original": "select col from ref order by col", 1442 "Instructions": { 1443 "OperatorType": "Route", 1444 "Variant": "Reference", 1445 "Keyspace": { 1446 "Name": "user", 1447 "Sharded": true 1448 }, 1449 "FieldQuery": "select col from ref where 1 != 1", 1450 "Query": "select col from ref order by col asc", 1451 "Table": "ref" 1452 } 1453 }, 1454 "gen4-plan": { 1455 "QueryType": "SELECT", 1456 "Original": "select col from ref order by col", 1457 "Instructions": { 1458 "OperatorType": "Route", 1459 "Variant": "Reference", 1460 "Keyspace": { 1461 "Name": "user", 1462 "Sharded": true 1463 }, 1464 "FieldQuery": "select col from ref where 1 != 1", 1465 "Query": "select col from ref order by col asc", 1466 "Table": "ref" 1467 }, 1468 "TablesUsed": [ 1469 "user.ref" 1470 ] 1471 } 1472 }, 1473 { 1474 "comment": "distinct and aggregate functions missing group by", 1475 "query": "select distinct a, count(*) from user", 1476 "v3-plan": { 1477 "QueryType": "SELECT", 1478 "Original": "select distinct a, count(*) from user", 1479 "Instructions": { 1480 "OperatorType": "Distinct", 1481 "Inputs": [ 1482 { 1483 "OperatorType": "Aggregate", 1484 "Variant": "Ordered", 1485 "Aggregates": "sum_count(1) AS count", 1486 "GroupBy": "0", 1487 "Inputs": [ 1488 { 1489 "OperatorType": "Route", 1490 "Variant": "Scatter", 1491 "Keyspace": { 1492 "Name": "user", 1493 "Sharded": true 1494 }, 1495 "FieldQuery": "select a, count(*), weight_string(a) from `user` where 1 != 1", 1496 "OrderBy": "(0|2) ASC", 1497 "Query": "select a, count(*), weight_string(a) from `user` order by a asc", 1498 "ResultColumns": 2, 1499 "Table": "`user`" 1500 } 1501 ] 1502 } 1503 ] 1504 } 1505 }, 1506 "gen4-plan": { 1507 "QueryType": "SELECT", 1508 "Original": "select distinct a, count(*) from user", 1509 "Instructions": { 1510 "OperatorType": "Aggregate", 1511 "Variant": "Ordered", 1512 "GroupBy": "0, 1", 1513 "Inputs": [ 1514 { 1515 "OperatorType": "Aggregate", 1516 "Variant": "Scalar", 1517 "Aggregates": "random(0) AS a, sum_count_star(1) AS count(*)", 1518 "Inputs": [ 1519 { 1520 "OperatorType": "Route", 1521 "Variant": "Scatter", 1522 "Keyspace": { 1523 "Name": "user", 1524 "Sharded": true 1525 }, 1526 "FieldQuery": "select a, count(*) from `user` where 1 != 1", 1527 "Query": "select a, count(*) from `user`", 1528 "Table": "`user`" 1529 } 1530 ] 1531 } 1532 ] 1533 }, 1534 "TablesUsed": [ 1535 "user.user" 1536 ] 1537 } 1538 }, 1539 { 1540 "comment": "distinct and aggregate functions", 1541 "query": "select distinct a, count(*) from user group by a", 1542 "v3-plan": { 1543 "QueryType": "SELECT", 1544 "Original": "select distinct a, count(*) from user group by a", 1545 "Instructions": { 1546 "OperatorType": "Distinct", 1547 "Inputs": [ 1548 { 1549 "OperatorType": "Aggregate", 1550 "Variant": "Ordered", 1551 "Aggregates": "sum_count(1) AS count", 1552 "GroupBy": "0, 0", 1553 "Inputs": [ 1554 { 1555 "OperatorType": "Route", 1556 "Variant": "Scatter", 1557 "Keyspace": { 1558 "Name": "user", 1559 "Sharded": true 1560 }, 1561 "FieldQuery": "select a, count(*), weight_string(a) from `user` where 1 != 1 group by a, weight_string(a)", 1562 "OrderBy": "(0|2) ASC, (0|2) ASC", 1563 "Query": "select a, count(*), weight_string(a) from `user` group by a, weight_string(a) order by a asc, a asc", 1564 "ResultColumns": 2, 1565 "Table": "`user`" 1566 } 1567 ] 1568 } 1569 ] 1570 } 1571 }, 1572 "gen4-plan": { 1573 "QueryType": "SELECT", 1574 "Original": "select distinct a, count(*) from user group by a", 1575 "Instructions": { 1576 "OperatorType": "Aggregate", 1577 "Variant": "Ordered", 1578 "GroupBy": "(0|2), 1", 1579 "ResultColumns": 2, 1580 "Inputs": [ 1581 { 1582 "OperatorType": "Aggregate", 1583 "Variant": "Ordered", 1584 "Aggregates": "sum_count_star(1) AS count(*)", 1585 "GroupBy": "(0|2)", 1586 "Inputs": [ 1587 { 1588 "OperatorType": "Route", 1589 "Variant": "Scatter", 1590 "Keyspace": { 1591 "Name": "user", 1592 "Sharded": true 1593 }, 1594 "FieldQuery": "select a, count(*), weight_string(a) from `user` where 1 != 1 group by a, weight_string(a)", 1595 "OrderBy": "(0|2) ASC", 1596 "Query": "select a, count(*), weight_string(a) from `user` group by a, weight_string(a) order by a asc", 1597 "Table": "`user`" 1598 } 1599 ] 1600 } 1601 ] 1602 }, 1603 "TablesUsed": [ 1604 "user.user" 1605 ] 1606 } 1607 }, 1608 { 1609 "comment": "Group by invalid column number (code is duplicated from symab).", 1610 "query": "select id from user group by 1.1", 1611 "v3-plan": "VT13001: [BUG] column number is not an INT", 1612 "gen4-plan": { 1613 "QueryType": "SELECT", 1614 "Original": "select id from user group by 1.1", 1615 "Instructions": { 1616 "OperatorType": "Aggregate", 1617 "Variant": "Ordered", 1618 "Aggregates": "random(0) AS id", 1619 "GroupBy": "1", 1620 "ResultColumns": 1, 1621 "Inputs": [ 1622 { 1623 "OperatorType": "Route", 1624 "Variant": "Scatter", 1625 "Keyspace": { 1626 "Name": "user", 1627 "Sharded": true 1628 }, 1629 "FieldQuery": "select id, 1.1 from `user` where 1 != 1 group by 1.1", 1630 "OrderBy": "1 ASC", 1631 "Query": "select id, 1.1 from `user` group by 1.1 order by 1.1 asc", 1632 "Table": "`user`" 1633 } 1634 ] 1635 }, 1636 "TablesUsed": [ 1637 "user.user" 1638 ] 1639 } 1640 }, 1641 { 1642 "comment": "Group by out of range column number (code is duplicated from symab).", 1643 "query": "select id from user group by 2", 1644 "v3-plan": "VT03014: unknown column '2' in 'group statement'", 1645 "gen4-plan": "Unknown column '2' in 'group statement'" 1646 }, 1647 { 1648 "comment": "here it is safe to remove the order by on the derived table since it will not influence the output of the count(*)", 1649 "query": "select count(*) from (select user.col, user_extra.extra from user join user_extra on user.id = user_extra.user_id order by user_extra.extra) a", 1650 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 1651 "gen4-plan": { 1652 "QueryType": "SELECT", 1653 "Original": "select count(*) from (select user.col, user_extra.extra from user join user_extra on user.id = user_extra.user_id order by user_extra.extra) a", 1654 "Instructions": { 1655 "OperatorType": "Aggregate", 1656 "Variant": "Scalar", 1657 "Aggregates": "sum_count_star(0) AS count(*)", 1658 "Inputs": [ 1659 { 1660 "OperatorType": "Route", 1661 "Variant": "Scatter", 1662 "Keyspace": { 1663 "Name": "user", 1664 "Sharded": true 1665 }, 1666 "FieldQuery": "select count(*) from (select `user`.col, user_extra.extra from `user`, user_extra where 1 != 1) as a where 1 != 1", 1667 "Query": "select count(*) from (select `user`.col, user_extra.extra from `user`, user_extra where `user`.id = user_extra.user_id order by user_extra.extra asc) as a", 1668 "Table": "`user`, user_extra" 1669 } 1670 ] 1671 }, 1672 "TablesUsed": [ 1673 "user.user", 1674 "user.user_extra" 1675 ] 1676 } 1677 }, 1678 { 1679 "comment": "order by inside derived tables can be ignored", 1680 "query": "select col from (select user.col, user_extra.extra from user join user_extra on user.id = user_extra.user_id order by user_extra.extra) a", 1681 "v3-plan": { 1682 "QueryType": "SELECT", 1683 "Original": "select col from (select user.col, user_extra.extra from user join user_extra on user.id = user_extra.user_id order by user_extra.extra) a", 1684 "Instructions": { 1685 "OperatorType": "SimpleProjection", 1686 "Columns": [ 1687 0 1688 ], 1689 "Inputs": [ 1690 { 1691 "OperatorType": "Route", 1692 "Variant": "Scatter", 1693 "Keyspace": { 1694 "Name": "user", 1695 "Sharded": true 1696 }, 1697 "FieldQuery": "select `user`.col, user_extra.extra, weight_string(user_extra.extra) from `user` join user_extra on `user`.id = user_extra.user_id where 1 != 1", 1698 "OrderBy": "(1|2) ASC", 1699 "Query": "select `user`.col, user_extra.extra, weight_string(user_extra.extra) from `user` join user_extra on `user`.id = user_extra.user_id order by user_extra.extra asc", 1700 "ResultColumns": 2, 1701 "Table": "`user`, user_extra" 1702 } 1703 ] 1704 } 1705 }, 1706 "gen4-plan": { 1707 "QueryType": "SELECT", 1708 "Original": "select col from (select user.col, user_extra.extra from user join user_extra on user.id = user_extra.user_id order by user_extra.extra) a", 1709 "Instructions": { 1710 "OperatorType": "Route", 1711 "Variant": "Scatter", 1712 "Keyspace": { 1713 "Name": "user", 1714 "Sharded": true 1715 }, 1716 "FieldQuery": "select col from (select `user`.col, user_extra.extra from `user`, user_extra where 1 != 1) as a where 1 != 1", 1717 "Query": "select col from (select `user`.col, user_extra.extra from `user`, user_extra where `user`.id = user_extra.user_id order by user_extra.extra asc) as a", 1718 "Table": "`user`, user_extra" 1719 }, 1720 "TablesUsed": [ 1721 "user.user", 1722 "user.user_extra" 1723 ] 1724 } 1725 }, 1726 { 1727 "comment": "here we keep the order since the column is visible on the outside, and used by the orderedAggregate", 1728 "query": "select col, count(*) from (select user.col, user_extra.extra from user join user_extra on user.id = user_extra.user_id order by user_extra.extra) a group by col", 1729 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 1730 "gen4-plan": { 1731 "QueryType": "SELECT", 1732 "Original": "select col, count(*) from (select user.col, user_extra.extra from user join user_extra on user.id = user_extra.user_id order by user_extra.extra) a group by col", 1733 "Instructions": { 1734 "OperatorType": "Aggregate", 1735 "Variant": "Ordered", 1736 "Aggregates": "sum_count_star(1) AS count(*)", 1737 "GroupBy": "0", 1738 "Inputs": [ 1739 { 1740 "OperatorType": "Route", 1741 "Variant": "Scatter", 1742 "Keyspace": { 1743 "Name": "user", 1744 "Sharded": true 1745 }, 1746 "FieldQuery": "select col, count(*) from (select `user`.col, user_extra.extra from `user`, user_extra where 1 != 1) as a where 1 != 1 group by col", 1747 "OrderBy": "0 ASC", 1748 "Query": "select col, count(*) from (select `user`.col, user_extra.extra from `user`, user_extra where `user`.id = user_extra.user_id order by user_extra.extra asc) as a group by col order by col asc", 1749 "Table": "`user`, user_extra" 1750 } 1751 ] 1752 }, 1753 "TablesUsed": [ 1754 "user.user", 1755 "user.user_extra" 1756 ] 1757 } 1758 }, 1759 { 1760 "comment": "optimize group by when using distinct with no aggregation", 1761 "query": "select distinct col1, col2 from user group by col1, col2", 1762 "v3-plan": { 1763 "QueryType": "SELECT", 1764 "Original": "select distinct col1, col2 from user group by col1, col2", 1765 "Instructions": { 1766 "OperatorType": "Aggregate", 1767 "Variant": "Ordered", 1768 "GroupBy": "0, 1, 0, 1", 1769 "Inputs": [ 1770 { 1771 "OperatorType": "Route", 1772 "Variant": "Scatter", 1773 "Keyspace": { 1774 "Name": "user", 1775 "Sharded": true 1776 }, 1777 "FieldQuery": "select col1, col2, weight_string(col1), weight_string(col2) from `user` where 1 != 1 group by col1, col2, weight_string(col1), weight_string(col2)", 1778 "OrderBy": "(0|2) ASC, (1|3) ASC, (0|2) ASC, (1|3) ASC", 1779 "Query": "select distinct col1, col2, weight_string(col1), weight_string(col2) from `user` group by col1, col2, weight_string(col1), weight_string(col2) order by col1 asc, col2 asc, col1 asc, col2 asc", 1780 "ResultColumns": 2, 1781 "Table": "`user`" 1782 } 1783 ] 1784 } 1785 }, 1786 "gen4-plan": { 1787 "QueryType": "SELECT", 1788 "Original": "select distinct col1, col2 from user group by col1, col2", 1789 "Instructions": { 1790 "OperatorType": "Aggregate", 1791 "Variant": "Ordered", 1792 "GroupBy": "(0|2), (1|3)", 1793 "ResultColumns": 2, 1794 "Inputs": [ 1795 { 1796 "OperatorType": "Route", 1797 "Variant": "Scatter", 1798 "Keyspace": { 1799 "Name": "user", 1800 "Sharded": true 1801 }, 1802 "FieldQuery": "select col1, col2, weight_string(col1), weight_string(col2) from `user` where 1 != 1 group by col1, col2", 1803 "OrderBy": "(0|2) ASC, (1|3) ASC", 1804 "Query": "select distinct col1, col2, weight_string(col1), weight_string(col2) from `user` group by col1, col2 order by col1 asc, col2 asc", 1805 "Table": "`user`" 1806 } 1807 ] 1808 }, 1809 "TablesUsed": [ 1810 "user.user" 1811 ] 1812 } 1813 }, 1814 { 1815 "comment": "do not use distinct when using only aggregates and no group by", 1816 "query": "select distinct count(*) from user", 1817 "v3-plan": { 1818 "QueryType": "SELECT", 1819 "Original": "select distinct count(*) from user", 1820 "Instructions": { 1821 "OperatorType": "Distinct", 1822 "Inputs": [ 1823 { 1824 "OperatorType": "Aggregate", 1825 "Variant": "Scalar", 1826 "Aggregates": "sum_count(0) AS count", 1827 "Inputs": [ 1828 { 1829 "OperatorType": "Route", 1830 "Variant": "Scatter", 1831 "Keyspace": { 1832 "Name": "user", 1833 "Sharded": true 1834 }, 1835 "FieldQuery": "select count(*) from `user` where 1 != 1", 1836 "Query": "select count(*) from `user`", 1837 "Table": "`user`" 1838 } 1839 ] 1840 } 1841 ] 1842 } 1843 }, 1844 "gen4-plan": { 1845 "QueryType": "SELECT", 1846 "Original": "select distinct count(*) from user", 1847 "Instructions": { 1848 "OperatorType": "Aggregate", 1849 "Variant": "Scalar", 1850 "Aggregates": "sum_count_star(0) AS count(*)", 1851 "Inputs": [ 1852 { 1853 "OperatorType": "Route", 1854 "Variant": "Scatter", 1855 "Keyspace": { 1856 "Name": "user", 1857 "Sharded": true 1858 }, 1859 "FieldQuery": "select count(*) from `user` where 1 != 1", 1860 "Query": "select count(*) from `user`", 1861 "Table": "`user`" 1862 } 1863 ] 1864 }, 1865 "TablesUsed": [ 1866 "user.user" 1867 ] 1868 } 1869 }, 1870 { 1871 "comment": "Grouping on join", 1872 "query": "select user.a from user join user_extra group by user.a", 1873 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 1874 "gen4-plan": { 1875 "QueryType": "SELECT", 1876 "Original": "select user.a from user join user_extra group by user.a", 1877 "Instructions": { 1878 "OperatorType": "Aggregate", 1879 "Variant": "Ordered", 1880 "GroupBy": "(0|1)", 1881 "ResultColumns": 1, 1882 "Inputs": [ 1883 { 1884 "OperatorType": "Projection", 1885 "Expressions": [ 1886 "[COLUMN 0] as a", 1887 "[COLUMN 1]" 1888 ], 1889 "Inputs": [ 1890 { 1891 "OperatorType": "Join", 1892 "Variant": "Join", 1893 "JoinColumnIndexes": "L:0,L:1", 1894 "TableName": "`user`_user_extra", 1895 "Inputs": [ 1896 { 1897 "OperatorType": "Route", 1898 "Variant": "Scatter", 1899 "Keyspace": { 1900 "Name": "user", 1901 "Sharded": true 1902 }, 1903 "FieldQuery": "select `user`.a, weight_string(`user`.a) from `user` where 1 != 1 group by `user`.a, weight_string(`user`.a)", 1904 "OrderBy": "(0|1) ASC", 1905 "Query": "select `user`.a, weight_string(`user`.a) from `user` group by `user`.a, weight_string(`user`.a) order by `user`.a asc", 1906 "Table": "`user`" 1907 }, 1908 { 1909 "OperatorType": "Route", 1910 "Variant": "Scatter", 1911 "Keyspace": { 1912 "Name": "user", 1913 "Sharded": true 1914 }, 1915 "FieldQuery": "select 1 from user_extra where 1 != 1", 1916 "Query": "select 1 from user_extra", 1917 "Table": "user_extra" 1918 } 1919 ] 1920 } 1921 ] 1922 } 1923 ] 1924 }, 1925 "TablesUsed": [ 1926 "user.user", 1927 "user.user_extra" 1928 ] 1929 } 1930 }, 1931 { 1932 "comment": "Cannot have more than one aggr(distinct...", 1933 "query": "select count(distinct a), count(distinct b) from user", 1934 "v3-plan": "VT12001: unsupported: only one DISTINCT aggregation allowed in a SELECT: count(distinct b)", 1935 "gen4-plan": "VT12001: unsupported: only one DISTINCT aggregation is allowed in a SELECT: count(distinct b)" 1936 }, 1937 { 1938 "comment": "multiple distinct functions with grouping.", 1939 "query": "select col1, count(distinct col2), sum(distinct col2) from user group by col1", 1940 "v3-plan": "VT12001: unsupported: only one DISTINCT aggregation allowed in a SELECT: sum(distinct col2)", 1941 "gen4-plan": { 1942 "QueryType": "SELECT", 1943 "Original": "select col1, count(distinct col2), sum(distinct col2) from user group by col1", 1944 "Instructions": { 1945 "OperatorType": "Aggregate", 1946 "Variant": "Ordered", 1947 "Aggregates": "count_distinct(1|4) AS count(distinct col2), sum_distinct(2|4) AS sum(distinct col2)", 1948 "GroupBy": "(0|3)", 1949 "ResultColumns": 3, 1950 "Inputs": [ 1951 { 1952 "OperatorType": "Route", 1953 "Variant": "Scatter", 1954 "Keyspace": { 1955 "Name": "user", 1956 "Sharded": true 1957 }, 1958 "FieldQuery": "select col1, col2, col2, weight_string(col1), weight_string(col2) from `user` where 1 != 1 group by col1, weight_string(col1), col2, weight_string(col2)", 1959 "OrderBy": "(0|3) ASC, (1|4) ASC", 1960 "Query": "select col1, col2, col2, weight_string(col1), weight_string(col2) from `user` group by col1, weight_string(col1), col2, weight_string(col2) order by col1 asc, col2 asc", 1961 "Table": "`user`" 1962 } 1963 ] 1964 }, 1965 "TablesUsed": [ 1966 "user.user" 1967 ] 1968 } 1969 }, 1970 { 1971 "comment": "aggregate query with order by aggregate column along with NULL", 1972 "query": "select col, count(*) k from user group by col order by null, k", 1973 "v3-plan": "VT12001: unsupported: in scatter query: complex ORDER BY expression: null", 1974 "gen4-plan": { 1975 "QueryType": "SELECT", 1976 "Original": "select col, count(*) k from user group by col order by null, k", 1977 "Instructions": { 1978 "OperatorType": "Sort", 1979 "Variant": "Memory", 1980 "OrderBy": "1 ASC", 1981 "Inputs": [ 1982 { 1983 "OperatorType": "Aggregate", 1984 "Variant": "Ordered", 1985 "Aggregates": "sum_count_star(1) AS k", 1986 "GroupBy": "0", 1987 "Inputs": [ 1988 { 1989 "OperatorType": "Route", 1990 "Variant": "Scatter", 1991 "Keyspace": { 1992 "Name": "user", 1993 "Sharded": true 1994 }, 1995 "FieldQuery": "select col, count(*) as k from `user` where 1 != 1 group by col", 1996 "OrderBy": "0 ASC", 1997 "Query": "select col, count(*) as k from `user` group by col order by col asc", 1998 "Table": "`user`" 1999 } 2000 ] 2001 } 2002 ] 2003 }, 2004 "TablesUsed": [ 2005 "user.user" 2006 ] 2007 } 2008 }, 2009 { 2010 "comment": "aggregate query with order by NULL", 2011 "query": "select col, count(*) k from user group by col order by null", 2012 "v3-plan": { 2013 "QueryType": "SELECT", 2014 "Original": "select col, count(*) k from user group by col order by null", 2015 "Instructions": { 2016 "OperatorType": "Aggregate", 2017 "Variant": "Ordered", 2018 "Aggregates": "sum_count(1) AS count", 2019 "GroupBy": "0", 2020 "Inputs": [ 2021 { 2022 "OperatorType": "Route", 2023 "Variant": "Scatter", 2024 "Keyspace": { 2025 "Name": "user", 2026 "Sharded": true 2027 }, 2028 "FieldQuery": "select col, count(*) as k from `user` where 1 != 1 group by col", 2029 "OrderBy": "0 ASC", 2030 "Query": "select col, count(*) as k from `user` group by col order by col asc", 2031 "Table": "`user`" 2032 } 2033 ] 2034 } 2035 }, 2036 "gen4-plan": { 2037 "QueryType": "SELECT", 2038 "Original": "select col, count(*) k from user group by col order by null", 2039 "Instructions": { 2040 "OperatorType": "Aggregate", 2041 "Variant": "Ordered", 2042 "Aggregates": "sum_count_star(1) AS k", 2043 "GroupBy": "0", 2044 "Inputs": [ 2045 { 2046 "OperatorType": "Route", 2047 "Variant": "Scatter", 2048 "Keyspace": { 2049 "Name": "user", 2050 "Sharded": true 2051 }, 2052 "FieldQuery": "select col, count(*) as k from `user` where 1 != 1 group by col", 2053 "OrderBy": "0 ASC", 2054 "Query": "select col, count(*) as k from `user` group by col order by col asc", 2055 "Table": "`user`" 2056 } 2057 ] 2058 }, 2059 "TablesUsed": [ 2060 "user.user" 2061 ] 2062 } 2063 }, 2064 { 2065 "comment": "join query on sharding key with group by a unique vindex with having clause.", 2066 "query": "select user.id, count(*) c from user, user_extra where user.id = user_extra.user_id group by user.id having max(user.col) > 10", 2067 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 2068 "gen4-plan": { 2069 "QueryType": "SELECT", 2070 "Original": "select user.id, count(*) c from user, user_extra where user.id = user_extra.user_id group by user.id having max(user.col) > 10", 2071 "Instructions": { 2072 "OperatorType": "Route", 2073 "Variant": "Scatter", 2074 "Keyspace": { 2075 "Name": "user", 2076 "Sharded": true 2077 }, 2078 "FieldQuery": "select `user`.id, count(*) as c from `user`, user_extra where 1 != 1 group by `user`.id", 2079 "Query": "select `user`.id, count(*) as c from `user`, user_extra where `user`.id = user_extra.user_id group by `user`.id having max(`user`.col) > 10", 2080 "Table": "`user`, user_extra" 2081 }, 2082 "TablesUsed": [ 2083 "user.user", 2084 "user.user_extra" 2085 ] 2086 } 2087 }, 2088 { 2089 "comment": "correlated subquery on sharding key with group by a unique vindex with having clause.", 2090 "query": "select count(*) from user where exists (select 1 from user_extra where user_id = user.id group by user_id having max(col) > 10)", 2091 "v3-plan": { 2092 "QueryType": "SELECT", 2093 "Original": "select count(*) from user where exists (select 1 from user_extra where user_id = user.id group by user_id having max(col) > 10)", 2094 "Instructions": { 2095 "OperatorType": "Aggregate", 2096 "Variant": "Scalar", 2097 "Aggregates": "sum_count(0) AS count", 2098 "Inputs": [ 2099 { 2100 "OperatorType": "Route", 2101 "Variant": "Scatter", 2102 "Keyspace": { 2103 "Name": "user", 2104 "Sharded": true 2105 }, 2106 "FieldQuery": "select count(*) from `user` where 1 != 1", 2107 "Query": "select count(*) from `user` where exists (select 1 from user_extra where user_id = `user`.id group by user_id having max(col) > 10 limit 1)", 2108 "Table": "`user`" 2109 } 2110 ] 2111 } 2112 }, 2113 "gen4-plan": { 2114 "QueryType": "SELECT", 2115 "Original": "select count(*) from user where exists (select 1 from user_extra where user_id = user.id group by user_id having max(col) > 10)", 2116 "Instructions": { 2117 "OperatorType": "Aggregate", 2118 "Variant": "Scalar", 2119 "Aggregates": "sum_count_star(0) AS count(*)", 2120 "Inputs": [ 2121 { 2122 "OperatorType": "Route", 2123 "Variant": "Scatter", 2124 "Keyspace": { 2125 "Name": "user", 2126 "Sharded": true 2127 }, 2128 "FieldQuery": "select count(*) from `user` where 1 != 1", 2129 "Query": "select count(*) from `user` where exists (select 1 from user_extra where user_id = `user`.id group by user_id having max(col) > 10 limit 1)", 2130 "Table": "`user`" 2131 } 2132 ] 2133 }, 2134 "TablesUsed": [ 2135 "user.user", 2136 "user.user_extra" 2137 ] 2138 } 2139 }, 2140 { 2141 "comment": "aggregation filtering by having on a route", 2142 "query": "select id from user group by id having count(id) = 10", 2143 "v3-plan": { 2144 "QueryType": "SELECT", 2145 "Original": "select id from user group by id having count(id) = 10", 2146 "Instructions": { 2147 "OperatorType": "Route", 2148 "Variant": "Scatter", 2149 "Keyspace": { 2150 "Name": "user", 2151 "Sharded": true 2152 }, 2153 "FieldQuery": "select id from `user` where 1 != 1 group by id", 2154 "Query": "select id from `user` group by id having count(id) = 10", 2155 "Table": "`user`" 2156 } 2157 }, 2158 "gen4-plan": { 2159 "QueryType": "SELECT", 2160 "Original": "select id from user group by id having count(id) = 10", 2161 "Instructions": { 2162 "OperatorType": "Route", 2163 "Variant": "Scatter", 2164 "Keyspace": { 2165 "Name": "user", 2166 "Sharded": true 2167 }, 2168 "FieldQuery": "select id from `user` where 1 != 1 group by id", 2169 "Query": "select id from `user` group by id having count(id) = 10", 2170 "Table": "`user`" 2171 }, 2172 "TablesUsed": [ 2173 "user.user" 2174 ] 2175 } 2176 }, 2177 { 2178 "comment": "weight_string addition to group by", 2179 "query": "select lower(textcol1) as v, count(*) from user group by v", 2180 "v3-plan": { 2181 "QueryType": "SELECT", 2182 "Original": "select lower(textcol1) as v, count(*) from user group by v", 2183 "Instructions": { 2184 "OperatorType": "Aggregate", 2185 "Variant": "Ordered", 2186 "Aggregates": "sum_count(1) AS count", 2187 "GroupBy": "0", 2188 "Inputs": [ 2189 { 2190 "OperatorType": "Route", 2191 "Variant": "Scatter", 2192 "Keyspace": { 2193 "Name": "user", 2194 "Sharded": true 2195 }, 2196 "FieldQuery": "select lower(textcol1) as v, count(*), weight_string(lower(textcol1)) from `user` where 1 != 1 group by v, weight_string(lower(textcol1))", 2197 "OrderBy": "(0|2) ASC", 2198 "Query": "select lower(textcol1) as v, count(*), weight_string(lower(textcol1)) from `user` group by v, weight_string(lower(textcol1)) order by v asc", 2199 "ResultColumns": 2, 2200 "Table": "`user`" 2201 } 2202 ] 2203 } 2204 }, 2205 "gen4-plan": { 2206 "QueryType": "SELECT", 2207 "Original": "select lower(textcol1) as v, count(*) from user group by v", 2208 "Instructions": { 2209 "OperatorType": "Aggregate", 2210 "Variant": "Ordered", 2211 "Aggregates": "sum_count_star(1) AS count(*)", 2212 "GroupBy": "(0|2)", 2213 "ResultColumns": 2, 2214 "Inputs": [ 2215 { 2216 "OperatorType": "Route", 2217 "Variant": "Scatter", 2218 "Keyspace": { 2219 "Name": "user", 2220 "Sharded": true 2221 }, 2222 "FieldQuery": "select lower(textcol1) as v, count(*), weight_string(lower(textcol1)) from `user` where 1 != 1 group by v, weight_string(lower(textcol1))", 2223 "OrderBy": "(0|2) ASC", 2224 "Query": "select lower(textcol1) as v, count(*), weight_string(lower(textcol1)) from `user` group by v, weight_string(lower(textcol1)) order by v asc", 2225 "Table": "`user`" 2226 } 2227 ] 2228 }, 2229 "TablesUsed": [ 2230 "user.user" 2231 ] 2232 } 2233 }, 2234 { 2235 "comment": "weight_string addition to group by when also there in order by", 2236 "query": "select char_length(texcol1) as a, count(*) from user group by a order by a", 2237 "v3-plan": { 2238 "QueryType": "SELECT", 2239 "Original": "select char_length(texcol1) as a, count(*) from user group by a order by a", 2240 "Instructions": { 2241 "OperatorType": "Aggregate", 2242 "Variant": "Ordered", 2243 "Aggregates": "sum_count(1) AS count", 2244 "GroupBy": "0", 2245 "Inputs": [ 2246 { 2247 "OperatorType": "Route", 2248 "Variant": "Scatter", 2249 "Keyspace": { 2250 "Name": "user", 2251 "Sharded": true 2252 }, 2253 "FieldQuery": "select char_length(texcol1) as a, count(*), weight_string(char_length(texcol1)) from `user` where 1 != 1 group by a, weight_string(char_length(texcol1))", 2254 "OrderBy": "(0|2) ASC", 2255 "Query": "select char_length(texcol1) as a, count(*), weight_string(char_length(texcol1)) from `user` group by a, weight_string(char_length(texcol1)) order by a asc", 2256 "ResultColumns": 2, 2257 "Table": "`user`" 2258 } 2259 ] 2260 } 2261 }, 2262 "gen4-plan": { 2263 "QueryType": "SELECT", 2264 "Original": "select char_length(texcol1) as a, count(*) from user group by a order by a", 2265 "Instructions": { 2266 "OperatorType": "Aggregate", 2267 "Variant": "Ordered", 2268 "Aggregates": "sum_count_star(1) AS count(*)", 2269 "GroupBy": "(0|2)", 2270 "ResultColumns": 2, 2271 "Inputs": [ 2272 { 2273 "OperatorType": "Route", 2274 "Variant": "Scatter", 2275 "Keyspace": { 2276 "Name": "user", 2277 "Sharded": true 2278 }, 2279 "FieldQuery": "select char_length(texcol1) as a, count(*), weight_string(char_length(texcol1)) from `user` where 1 != 1 group by a, weight_string(char_length(texcol1))", 2280 "OrderBy": "(0|2) ASC", 2281 "Query": "select char_length(texcol1) as a, count(*), weight_string(char_length(texcol1)) from `user` group by a, weight_string(char_length(texcol1)) order by a asc", 2282 "Table": "`user`" 2283 } 2284 ] 2285 }, 2286 "TablesUsed": [ 2287 "user.user" 2288 ] 2289 } 2290 }, 2291 { 2292 "comment": "order by inside and outside parenthesis select", 2293 "query": "(select id from user order by 1 desc) order by 1 asc limit 2", 2294 "v3-plan": { 2295 "QueryType": "SELECT", 2296 "Original": "(select id from user order by 1 desc) order by 1 asc limit 2", 2297 "Instructions": { 2298 "OperatorType": "Limit", 2299 "Count": "INT64(2)", 2300 "Inputs": [ 2301 { 2302 "OperatorType": "Route", 2303 "Variant": "Scatter", 2304 "Keyspace": { 2305 "Name": "user", 2306 "Sharded": true 2307 }, 2308 "FieldQuery": "select id, weight_string(id) from `user` where 1 != 1", 2309 "OrderBy": "(0|1) ASC", 2310 "Query": "select id, weight_string(id) from `user` order by 1 asc limit :__upper_limit", 2311 "ResultColumns": 1, 2312 "Table": "`user`" 2313 } 2314 ] 2315 } 2316 }, 2317 "gen4-plan": { 2318 "QueryType": "SELECT", 2319 "Original": "(select id from user order by 1 desc) order by 1 asc limit 2", 2320 "Instructions": { 2321 "OperatorType": "Limit", 2322 "Count": "INT64(2)", 2323 "Inputs": [ 2324 { 2325 "OperatorType": "Route", 2326 "Variant": "Scatter", 2327 "Keyspace": { 2328 "Name": "user", 2329 "Sharded": true 2330 }, 2331 "FieldQuery": "select id, weight_string(id) from `user` where 1 != 1", 2332 "OrderBy": "(0|1) ASC", 2333 "Query": "select id, weight_string(id) from `user` order by id asc limit :__upper_limit", 2334 "ResultColumns": 1, 2335 "Table": "`user`" 2336 } 2337 ] 2338 }, 2339 "TablesUsed": [ 2340 "user.user" 2341 ] 2342 } 2343 }, 2344 { 2345 "comment": "correlated subquery in exists clause with an ordering", 2346 "query": "select col, id from user where exists(select user_id from user_extra where user_id = 3 and user_id < user.id) order by id", 2347 "v3-plan": "VT12001: unsupported: cross-shard correlated subquery", 2348 "gen4-plan": { 2349 "QueryType": "SELECT", 2350 "Original": "select col, id from user where exists(select user_id from user_extra where user_id = 3 and user_id < user.id) order by id", 2351 "Instructions": { 2352 "OperatorType": "SemiJoin", 2353 "JoinVars": { 2354 "user_id": 0 2355 }, 2356 "ProjectedIndexes": "-2,-1", 2357 "TableName": "`user`_user_extra", 2358 "Inputs": [ 2359 { 2360 "OperatorType": "Route", 2361 "Variant": "Scatter", 2362 "Keyspace": { 2363 "Name": "user", 2364 "Sharded": true 2365 }, 2366 "FieldQuery": "select `user`.id, col, weight_string(id) from `user` where 1 != 1", 2367 "OrderBy": "(0|2) ASC", 2368 "Query": "select `user`.id, col, weight_string(id) from `user` order by id asc", 2369 "Table": "`user`" 2370 }, 2371 { 2372 "OperatorType": "Route", 2373 "Variant": "EqualUnique", 2374 "Keyspace": { 2375 "Name": "user", 2376 "Sharded": true 2377 }, 2378 "FieldQuery": "select 1 from user_extra where 1 != 1", 2379 "Query": "select 1 from user_extra where user_id = 3 and user_id < :user_id", 2380 "Table": "user_extra", 2381 "Values": [ 2382 "INT64(3)" 2383 ], 2384 "Vindex": "user_index" 2385 } 2386 ] 2387 }, 2388 "TablesUsed": [ 2389 "user.user", 2390 "user.user_extra" 2391 ] 2392 } 2393 }, 2394 { 2395 "comment": "Column and Literal equality filter on scatter aggregates", 2396 "query": "select count(*) a from user having a = 10", 2397 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 2398 "gen4-plan": { 2399 "QueryType": "SELECT", 2400 "Original": "select count(*) a from user having a = 10", 2401 "Instructions": { 2402 "OperatorType": "Filter", 2403 "Predicate": ":0 = 10", 2404 "Inputs": [ 2405 { 2406 "OperatorType": "Aggregate", 2407 "Variant": "Scalar", 2408 "Aggregates": "sum_count_star(0) AS a", 2409 "Inputs": [ 2410 { 2411 "OperatorType": "Route", 2412 "Variant": "Scatter", 2413 "Keyspace": { 2414 "Name": "user", 2415 "Sharded": true 2416 }, 2417 "FieldQuery": "select count(*) as a from `user` where 1 != 1", 2418 "Query": "select count(*) as a from `user`", 2419 "Table": "`user`" 2420 } 2421 ] 2422 } 2423 ] 2424 }, 2425 "TablesUsed": [ 2426 "user.user" 2427 ] 2428 } 2429 }, 2430 { 2431 "comment": "Equality filtering with column and string literal on scatter aggregates", 2432 "query": "select count(*) a from user having a = '1'", 2433 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 2434 "gen4-plan": { 2435 "QueryType": "SELECT", 2436 "Original": "select count(*) a from user having a = '1'", 2437 "Instructions": { 2438 "OperatorType": "Filter", 2439 "Predicate": ":0 = '1'", 2440 "Inputs": [ 2441 { 2442 "OperatorType": "Aggregate", 2443 "Variant": "Scalar", 2444 "Aggregates": "sum_count_star(0) AS a", 2445 "Inputs": [ 2446 { 2447 "OperatorType": "Route", 2448 "Variant": "Scatter", 2449 "Keyspace": { 2450 "Name": "user", 2451 "Sharded": true 2452 }, 2453 "FieldQuery": "select count(*) as a from `user` where 1 != 1", 2454 "Query": "select count(*) as a from `user`", 2455 "Table": "`user`" 2456 } 2457 ] 2458 } 2459 ] 2460 }, 2461 "TablesUsed": [ 2462 "user.user" 2463 ] 2464 } 2465 }, 2466 { 2467 "comment": "Column and Literal not equal filter on scatter aggregates", 2468 "query": "select count(*) a from user having a != 10", 2469 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 2470 "gen4-plan": { 2471 "QueryType": "SELECT", 2472 "Original": "select count(*) a from user having a != 10", 2473 "Instructions": { 2474 "OperatorType": "Filter", 2475 "Predicate": ":0 != 10", 2476 "Inputs": [ 2477 { 2478 "OperatorType": "Aggregate", 2479 "Variant": "Scalar", 2480 "Aggregates": "sum_count_star(0) AS a", 2481 "Inputs": [ 2482 { 2483 "OperatorType": "Route", 2484 "Variant": "Scatter", 2485 "Keyspace": { 2486 "Name": "user", 2487 "Sharded": true 2488 }, 2489 "FieldQuery": "select count(*) as a from `user` where 1 != 1", 2490 "Query": "select count(*) as a from `user`", 2491 "Table": "`user`" 2492 } 2493 ] 2494 } 2495 ] 2496 }, 2497 "TablesUsed": [ 2498 "user.user" 2499 ] 2500 } 2501 }, 2502 { 2503 "comment": "Not equal filter with column and string literal on scatter aggregates", 2504 "query": "select count(*) a from user having a != '1'", 2505 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 2506 "gen4-plan": { 2507 "QueryType": "SELECT", 2508 "Original": "select count(*) a from user having a != '1'", 2509 "Instructions": { 2510 "OperatorType": "Filter", 2511 "Predicate": ":0 != '1'", 2512 "Inputs": [ 2513 { 2514 "OperatorType": "Aggregate", 2515 "Variant": "Scalar", 2516 "Aggregates": "sum_count_star(0) AS a", 2517 "Inputs": [ 2518 { 2519 "OperatorType": "Route", 2520 "Variant": "Scatter", 2521 "Keyspace": { 2522 "Name": "user", 2523 "Sharded": true 2524 }, 2525 "FieldQuery": "select count(*) as a from `user` where 1 != 1", 2526 "Query": "select count(*) as a from `user`", 2527 "Table": "`user`" 2528 } 2529 ] 2530 } 2531 ] 2532 }, 2533 "TablesUsed": [ 2534 "user.user" 2535 ] 2536 } 2537 }, 2538 { 2539 "comment": "Greater than filter on scatter aggregates", 2540 "query": "select count(*) a from user having a > 10", 2541 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 2542 "gen4-plan": { 2543 "QueryType": "SELECT", 2544 "Original": "select count(*) a from user having a > 10", 2545 "Instructions": { 2546 "OperatorType": "Filter", 2547 "Predicate": ":0 > 10", 2548 "Inputs": [ 2549 { 2550 "OperatorType": "Aggregate", 2551 "Variant": "Scalar", 2552 "Aggregates": "sum_count_star(0) AS a", 2553 "Inputs": [ 2554 { 2555 "OperatorType": "Route", 2556 "Variant": "Scatter", 2557 "Keyspace": { 2558 "Name": "user", 2559 "Sharded": true 2560 }, 2561 "FieldQuery": "select count(*) as a from `user` where 1 != 1", 2562 "Query": "select count(*) as a from `user`", 2563 "Table": "`user`" 2564 } 2565 ] 2566 } 2567 ] 2568 }, 2569 "TablesUsed": [ 2570 "user.user" 2571 ] 2572 } 2573 }, 2574 { 2575 "comment": "Greater Equal filter on scatter aggregates", 2576 "query": "select count(*) a from user having a >= 10", 2577 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 2578 "gen4-plan": { 2579 "QueryType": "SELECT", 2580 "Original": "select count(*) a from user having a >= 10", 2581 "Instructions": { 2582 "OperatorType": "Filter", 2583 "Predicate": ":0 >= 10", 2584 "Inputs": [ 2585 { 2586 "OperatorType": "Aggregate", 2587 "Variant": "Scalar", 2588 "Aggregates": "sum_count_star(0) AS a", 2589 "Inputs": [ 2590 { 2591 "OperatorType": "Route", 2592 "Variant": "Scatter", 2593 "Keyspace": { 2594 "Name": "user", 2595 "Sharded": true 2596 }, 2597 "FieldQuery": "select count(*) as a from `user` where 1 != 1", 2598 "Query": "select count(*) as a from `user`", 2599 "Table": "`user`" 2600 } 2601 ] 2602 } 2603 ] 2604 }, 2605 "TablesUsed": [ 2606 "user.user" 2607 ] 2608 } 2609 }, 2610 { 2611 "comment": "Less than filter on scatter aggregates", 2612 "query": "select count(*) a from user having a < 10", 2613 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 2614 "gen4-plan": { 2615 "QueryType": "SELECT", 2616 "Original": "select count(*) a from user having a < 10", 2617 "Instructions": { 2618 "OperatorType": "Filter", 2619 "Predicate": ":0 < 10", 2620 "Inputs": [ 2621 { 2622 "OperatorType": "Aggregate", 2623 "Variant": "Scalar", 2624 "Aggregates": "sum_count_star(0) AS a", 2625 "Inputs": [ 2626 { 2627 "OperatorType": "Route", 2628 "Variant": "Scatter", 2629 "Keyspace": { 2630 "Name": "user", 2631 "Sharded": true 2632 }, 2633 "FieldQuery": "select count(*) as a from `user` where 1 != 1", 2634 "Query": "select count(*) as a from `user`", 2635 "Table": "`user`" 2636 } 2637 ] 2638 } 2639 ] 2640 }, 2641 "TablesUsed": [ 2642 "user.user" 2643 ] 2644 } 2645 }, 2646 { 2647 "comment": "Less Equal filter on scatter aggregates", 2648 "query": "select count(*) a from user having a <= 10", 2649 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 2650 "gen4-plan": { 2651 "QueryType": "SELECT", 2652 "Original": "select count(*) a from user having a <= 10", 2653 "Instructions": { 2654 "OperatorType": "Filter", 2655 "Predicate": ":0 <= 10", 2656 "Inputs": [ 2657 { 2658 "OperatorType": "Aggregate", 2659 "Variant": "Scalar", 2660 "Aggregates": "sum_count_star(0) AS a", 2661 "Inputs": [ 2662 { 2663 "OperatorType": "Route", 2664 "Variant": "Scatter", 2665 "Keyspace": { 2666 "Name": "user", 2667 "Sharded": true 2668 }, 2669 "FieldQuery": "select count(*) as a from `user` where 1 != 1", 2670 "Query": "select count(*) as a from `user`", 2671 "Table": "`user`" 2672 } 2673 ] 2674 } 2675 ] 2676 }, 2677 "TablesUsed": [ 2678 "user.user" 2679 ] 2680 } 2681 }, 2682 { 2683 "comment": "Less Equal filter on scatter with grouping", 2684 "query": "select col, count(*) a from user group by col having a <= 10", 2685 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 2686 "gen4-plan": { 2687 "QueryType": "SELECT", 2688 "Original": "select col, count(*) a from user group by col having a <= 10", 2689 "Instructions": { 2690 "OperatorType": "Filter", 2691 "Predicate": ":1 <= 10", 2692 "Inputs": [ 2693 { 2694 "OperatorType": "Aggregate", 2695 "Variant": "Ordered", 2696 "Aggregates": "sum_count_star(1) AS a", 2697 "GroupBy": "0", 2698 "Inputs": [ 2699 { 2700 "OperatorType": "Route", 2701 "Variant": "Scatter", 2702 "Keyspace": { 2703 "Name": "user", 2704 "Sharded": true 2705 }, 2706 "FieldQuery": "select col, count(*) as a from `user` where 1 != 1 group by col", 2707 "OrderBy": "0 ASC", 2708 "Query": "select col, count(*) as a from `user` group by col order by col asc", 2709 "Table": "`user`" 2710 } 2711 ] 2712 } 2713 ] 2714 }, 2715 "TablesUsed": [ 2716 "user.user" 2717 ] 2718 } 2719 }, 2720 { 2721 "comment": "We should be able to find grouping keys on ordered aggregates", 2722 "query": "select count(*) as a, val1 from user group by val1 having a = 1.00", 2723 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 2724 "gen4-plan": { 2725 "QueryType": "SELECT", 2726 "Original": "select count(*) as a, val1 from user group by val1 having a = 1.00", 2727 "Instructions": { 2728 "OperatorType": "SimpleProjection", 2729 "Columns": [ 2730 0, 2731 1 2732 ], 2733 "Inputs": [ 2734 { 2735 "OperatorType": "Filter", 2736 "Predicate": ":0 = 1.00", 2737 "Inputs": [ 2738 { 2739 "OperatorType": "Aggregate", 2740 "Variant": "Ordered", 2741 "Aggregates": "sum_count_star(0) AS a", 2742 "GroupBy": "(1|2)", 2743 "Inputs": [ 2744 { 2745 "OperatorType": "Route", 2746 "Variant": "Scatter", 2747 "Keyspace": { 2748 "Name": "user", 2749 "Sharded": true 2750 }, 2751 "FieldQuery": "select count(*) as a, val1, weight_string(val1) from `user` where 1 != 1 group by val1, weight_string(val1)", 2752 "OrderBy": "(1|2) ASC", 2753 "Query": "select count(*) as a, val1, weight_string(val1) from `user` group by val1, weight_string(val1) order by val1 asc", 2754 "Table": "`user`" 2755 } 2756 ] 2757 } 2758 ] 2759 } 2760 ] 2761 }, 2762 "TablesUsed": [ 2763 "user.user" 2764 ] 2765 } 2766 }, 2767 { 2768 "comment": "distinct on text column with collation", 2769 "query": "select col, count(distinct textcol1) from user group by col", 2770 "v3-plan": { 2771 "QueryType": "SELECT", 2772 "Original": "select col, count(distinct textcol1) from user group by col", 2773 "Instructions": { 2774 "OperatorType": "Aggregate", 2775 "Variant": "Ordered", 2776 "Aggregates": "count_distinct_count(1) AS count(distinct textcol1)", 2777 "GroupBy": "0", 2778 "Inputs": [ 2779 { 2780 "OperatorType": "Route", 2781 "Variant": "Scatter", 2782 "Keyspace": { 2783 "Name": "user", 2784 "Sharded": true 2785 }, 2786 "FieldQuery": "select col, textcol1, weight_string(textcol1) from `user` where 1 != 1 group by col, textcol1, weight_string(textcol1)", 2787 "OrderBy": "0 ASC, (1|2) ASC", 2788 "Query": "select col, textcol1, weight_string(textcol1) from `user` group by col, textcol1, weight_string(textcol1) order by col asc, textcol1 asc", 2789 "ResultColumns": 2, 2790 "Table": "`user`" 2791 } 2792 ] 2793 } 2794 }, 2795 "gen4-plan": { 2796 "QueryType": "SELECT", 2797 "Original": "select col, count(distinct textcol1) from user group by col", 2798 "Instructions": { 2799 "OperatorType": "Aggregate", 2800 "Variant": "Ordered", 2801 "Aggregates": "count_distinct(1 COLLATE latin1_swedish_ci) AS count(distinct textcol1)", 2802 "GroupBy": "0", 2803 "Inputs": [ 2804 { 2805 "OperatorType": "Route", 2806 "Variant": "Scatter", 2807 "Keyspace": { 2808 "Name": "user", 2809 "Sharded": true 2810 }, 2811 "FieldQuery": "select col, textcol1 from `user` where 1 != 1 group by col, textcol1", 2812 "OrderBy": "0 ASC, 1 ASC COLLATE latin1_swedish_ci", 2813 "Query": "select col, textcol1 from `user` group by col, textcol1 order by col asc, textcol1 asc", 2814 "Table": "`user`" 2815 } 2816 ] 2817 }, 2818 "TablesUsed": [ 2819 "user.user" 2820 ] 2821 } 2822 }, 2823 { 2824 "comment": "aggregation filtering by having on a route with no group by with non-unique vindex filter", 2825 "query": "select 1 from user having count(id) = 10 and name = 'a'", 2826 "v3-plan": { 2827 "QueryType": "SELECT", 2828 "Original": "select 1 from user having count(id) = 10 and name = 'a'", 2829 "Instructions": { 2830 "OperatorType": "Route", 2831 "Variant": "Equal", 2832 "Keyspace": { 2833 "Name": "user", 2834 "Sharded": true 2835 }, 2836 "FieldQuery": "select 1 from `user` where 1 != 1", 2837 "Query": "select 1 from `user` having count(id) = 10 and `name` = 'a'", 2838 "Table": "`user`", 2839 "Values": [ 2840 "VARCHAR(\"a\")" 2841 ], 2842 "Vindex": "name_user_map" 2843 } 2844 }, 2845 "gen4-plan": { 2846 "QueryType": "SELECT", 2847 "Original": "select 1 from user having count(id) = 10 and name = 'a'", 2848 "Instructions": { 2849 "OperatorType": "SimpleProjection", 2850 "Columns": [ 2851 0 2852 ], 2853 "Inputs": [ 2854 { 2855 "OperatorType": "Filter", 2856 "Predicate": ":1 = 10", 2857 "Inputs": [ 2858 { 2859 "OperatorType": "Aggregate", 2860 "Variant": "Scalar", 2861 "Aggregates": "random(0) AS 1, sum_count(1) AS count(id)", 2862 "Inputs": [ 2863 { 2864 "OperatorType": "VindexLookup", 2865 "Variant": "Equal", 2866 "Keyspace": { 2867 "Name": "user", 2868 "Sharded": true 2869 }, 2870 "Values": [ 2871 "VARCHAR(\"a\")" 2872 ], 2873 "Vindex": "name_user_map", 2874 "Inputs": [ 2875 { 2876 "OperatorType": "Route", 2877 "Variant": "IN", 2878 "Keyspace": { 2879 "Name": "user", 2880 "Sharded": true 2881 }, 2882 "FieldQuery": "select `name`, keyspace_id from name_user_vdx where 1 != 1", 2883 "Query": "select `name`, keyspace_id from name_user_vdx where `name` in ::__vals", 2884 "Table": "name_user_vdx", 2885 "Values": [ 2886 ":name" 2887 ], 2888 "Vindex": "user_index" 2889 }, 2890 { 2891 "OperatorType": "Route", 2892 "Variant": "ByDestination", 2893 "Keyspace": { 2894 "Name": "user", 2895 "Sharded": true 2896 }, 2897 "FieldQuery": "select 1, count(id) from `user` where 1 != 1", 2898 "Query": "select 1, count(id) from `user` where `name` = 'a'", 2899 "Table": "`user`" 2900 } 2901 ] 2902 } 2903 ] 2904 } 2905 ] 2906 } 2907 ] 2908 }, 2909 "TablesUsed": [ 2910 "user.user" 2911 ] 2912 } 2913 }, 2914 { 2915 "comment": "Aggregates and joins", 2916 "query": "select count(*) from user join user_extra", 2917 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 2918 "gen4-plan": { 2919 "QueryType": "SELECT", 2920 "Original": "select count(*) from user join user_extra", 2921 "Instructions": { 2922 "OperatorType": "Aggregate", 2923 "Variant": "Scalar", 2924 "Aggregates": "sum_count_star(0) AS count(*)", 2925 "Inputs": [ 2926 { 2927 "OperatorType": "Projection", 2928 "Expressions": [ 2929 "[COLUMN 0] * COALESCE([COLUMN 1], INT64(1)) as count(*)" 2930 ], 2931 "Inputs": [ 2932 { 2933 "OperatorType": "Join", 2934 "Variant": "Join", 2935 "JoinColumnIndexes": "L:0,R:1", 2936 "TableName": "`user`_user_extra", 2937 "Inputs": [ 2938 { 2939 "OperatorType": "Route", 2940 "Variant": "Scatter", 2941 "Keyspace": { 2942 "Name": "user", 2943 "Sharded": true 2944 }, 2945 "FieldQuery": "select count(*) from `user` where 1 != 1", 2946 "Query": "select count(*) from `user`", 2947 "Table": "`user`" 2948 }, 2949 { 2950 "OperatorType": "Route", 2951 "Variant": "Scatter", 2952 "Keyspace": { 2953 "Name": "user", 2954 "Sharded": true 2955 }, 2956 "FieldQuery": "select 1, count(*) from user_extra where 1 != 1 group by 1", 2957 "Query": "select 1, count(*) from user_extra group by 1", 2958 "Table": "user_extra" 2959 } 2960 ] 2961 } 2962 ] 2963 } 2964 ] 2965 }, 2966 "TablesUsed": [ 2967 "user.user", 2968 "user.user_extra" 2969 ] 2970 } 2971 }, 2972 { 2973 "comment": "aggregation filtering by having on a route with no group by", 2974 "query": "select 1 from user having count(id) = 10", 2975 "v3-plan": { 2976 "QueryType": "SELECT", 2977 "Original": "select 1 from user having count(id) = 10", 2978 "Instructions": { 2979 "OperatorType": "Route", 2980 "Variant": "Scatter", 2981 "Keyspace": { 2982 "Name": "user", 2983 "Sharded": true 2984 }, 2985 "FieldQuery": "select 1 from `user` where 1 != 1", 2986 "Query": "select 1 from `user` having count(id) = 10", 2987 "Table": "`user`" 2988 } 2989 }, 2990 "gen4-plan": { 2991 "QueryType": "SELECT", 2992 "Original": "select 1 from user having count(id) = 10", 2993 "Instructions": { 2994 "OperatorType": "SimpleProjection", 2995 "Columns": [ 2996 0 2997 ], 2998 "Inputs": [ 2999 { 3000 "OperatorType": "Filter", 3001 "Predicate": ":1 = 10", 3002 "Inputs": [ 3003 { 3004 "OperatorType": "Aggregate", 3005 "Variant": "Scalar", 3006 "Aggregates": "random(0) AS 1, sum_count(1) AS count(id)", 3007 "Inputs": [ 3008 { 3009 "OperatorType": "Route", 3010 "Variant": "Scatter", 3011 "Keyspace": { 3012 "Name": "user", 3013 "Sharded": true 3014 }, 3015 "FieldQuery": "select 1, count(id) from `user` where 1 != 1", 3016 "Query": "select 1, count(id) from `user`", 3017 "Table": "`user`" 3018 } 3019 ] 3020 } 3021 ] 3022 } 3023 ] 3024 }, 3025 "TablesUsed": [ 3026 "user.user" 3027 ] 3028 } 3029 }, 3030 { 3031 "comment": "Aggregate on join", 3032 "query": "select user.a, count(*) from user join user_extra group by user.a", 3033 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 3034 "gen4-plan": { 3035 "QueryType": "SELECT", 3036 "Original": "select user.a, count(*) from user join user_extra group by user.a", 3037 "Instructions": { 3038 "OperatorType": "Aggregate", 3039 "Variant": "Ordered", 3040 "Aggregates": "sum_count_star(1) AS count(*)", 3041 "GroupBy": "(0|2)", 3042 "ResultColumns": 2, 3043 "Inputs": [ 3044 { 3045 "OperatorType": "Projection", 3046 "Expressions": [ 3047 "[COLUMN 0] as a", 3048 "[COLUMN 2] * COALESCE([COLUMN 3], INT64(1)) as count(*)", 3049 "[COLUMN 1]" 3050 ], 3051 "Inputs": [ 3052 { 3053 "OperatorType": "Join", 3054 "Variant": "Join", 3055 "JoinColumnIndexes": "L:1,L:2,L:0,R:1", 3056 "TableName": "`user`_user_extra", 3057 "Inputs": [ 3058 { 3059 "OperatorType": "Route", 3060 "Variant": "Scatter", 3061 "Keyspace": { 3062 "Name": "user", 3063 "Sharded": true 3064 }, 3065 "FieldQuery": "select count(*), `user`.a, weight_string(`user`.a) from `user` where 1 != 1 group by `user`.a, weight_string(`user`.a)", 3066 "OrderBy": "(1|2) ASC", 3067 "Query": "select count(*), `user`.a, weight_string(`user`.a) from `user` group by `user`.a, weight_string(`user`.a) order by `user`.a asc", 3068 "Table": "`user`" 3069 }, 3070 { 3071 "OperatorType": "Route", 3072 "Variant": "Scatter", 3073 "Keyspace": { 3074 "Name": "user", 3075 "Sharded": true 3076 }, 3077 "FieldQuery": "select 1, count(*) from user_extra where 1 != 1 group by 1", 3078 "Query": "select 1, count(*) from user_extra group by 1", 3079 "Table": "user_extra" 3080 } 3081 ] 3082 } 3083 ] 3084 } 3085 ] 3086 }, 3087 "TablesUsed": [ 3088 "user.user", 3089 "user.user_extra" 3090 ] 3091 } 3092 }, 3093 { 3094 "comment": "Aggregate on other table in join", 3095 "query": "select user.a, count(user_extra.a) from user join user_extra group by user.a", 3096 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 3097 "gen4-plan": { 3098 "QueryType": "SELECT", 3099 "Original": "select user.a, count(user_extra.a) from user join user_extra group by user.a", 3100 "Instructions": { 3101 "OperatorType": "Aggregate", 3102 "Variant": "Ordered", 3103 "Aggregates": "sum_count(1) AS count(user_extra.a)", 3104 "GroupBy": "(0|2)", 3105 "ResultColumns": 2, 3106 "Inputs": [ 3107 { 3108 "OperatorType": "Projection", 3109 "Expressions": [ 3110 "[COLUMN 0] as a", 3111 "[COLUMN 2] * COALESCE([COLUMN 3], INT64(1)) as count(user_extra.a)", 3112 "[COLUMN 1]" 3113 ], 3114 "Inputs": [ 3115 { 3116 "OperatorType": "Join", 3117 "Variant": "Join", 3118 "JoinColumnIndexes": "L:1,L:2,L:0,R:1", 3119 "TableName": "`user`_user_extra", 3120 "Inputs": [ 3121 { 3122 "OperatorType": "Route", 3123 "Variant": "Scatter", 3124 "Keyspace": { 3125 "Name": "user", 3126 "Sharded": true 3127 }, 3128 "FieldQuery": "select count(*), `user`.a, weight_string(`user`.a) from `user` where 1 != 1 group by `user`.a, weight_string(`user`.a)", 3129 "OrderBy": "(1|2) ASC", 3130 "Query": "select count(*), `user`.a, weight_string(`user`.a) from `user` group by `user`.a, weight_string(`user`.a) order by `user`.a asc", 3131 "Table": "`user`" 3132 }, 3133 { 3134 "OperatorType": "Route", 3135 "Variant": "Scatter", 3136 "Keyspace": { 3137 "Name": "user", 3138 "Sharded": true 3139 }, 3140 "FieldQuery": "select 1, count(user_extra.a) from user_extra where 1 != 1 group by 1", 3141 "Query": "select 1, count(user_extra.a) from user_extra group by 1", 3142 "Table": "user_extra" 3143 } 3144 ] 3145 } 3146 ] 3147 } 3148 ] 3149 }, 3150 "TablesUsed": [ 3151 "user.user", 3152 "user.user_extra" 3153 ] 3154 } 3155 }, 3156 { 3157 "comment": "aggregation spread out across three routes", 3158 "query": "select count(u.textcol1), count(ue.foo), us.bar from user u join user_extra ue on u.foo = ue.bar join unsharded us on ue.bar = us.baz group by us.bar", 3159 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 3160 "gen4-plan": { 3161 "QueryType": "SELECT", 3162 "Original": "select count(u.textcol1), count(ue.foo), us.bar from user u join user_extra ue on u.foo = ue.bar join unsharded us on ue.bar = us.baz group by us.bar", 3163 "Instructions": { 3164 "OperatorType": "Aggregate", 3165 "Variant": "Ordered", 3166 "Aggregates": "sum_count(0) AS count(u.textcol1), sum_count(1) AS count(ue.foo)", 3167 "GroupBy": "(2|3)", 3168 "ResultColumns": 3, 3169 "Inputs": [ 3170 { 3171 "OperatorType": "Projection", 3172 "Expressions": [ 3173 "([COLUMN 2] * COALESCE([COLUMN 3], INT64(1))) * COALESCE([COLUMN 4], INT64(1)) as count(u.textcol1)", 3174 "([COLUMN 5] * COALESCE([COLUMN 6], INT64(1))) * COALESCE([COLUMN 7], INT64(1)) as count(ue.foo)", 3175 "[COLUMN 0] as bar", 3176 "[COLUMN 1]" 3177 ], 3178 "Inputs": [ 3179 { 3180 "OperatorType": "Sort", 3181 "Variant": "Memory", 3182 "OrderBy": "(0|1) ASC", 3183 "Inputs": [ 3184 { 3185 "OperatorType": "Join", 3186 "Variant": "Join", 3187 "JoinColumnIndexes": "R:0,R:1,L:1,R:2,R:3,L:2,R:4,R:5", 3188 "JoinVars": { 3189 "u_foo": 0 3190 }, 3191 "TableName": "`user`_user_extra_unsharded", 3192 "Inputs": [ 3193 { 3194 "OperatorType": "Route", 3195 "Variant": "Scatter", 3196 "Keyspace": { 3197 "Name": "user", 3198 "Sharded": true 3199 }, 3200 "FieldQuery": "select u.foo, count(u.textcol1), count(*), weight_string(u.foo) from `user` as u where 1 != 1 group by u.foo, weight_string(u.foo)", 3201 "Query": "select u.foo, count(u.textcol1), count(*), weight_string(u.foo) from `user` as u group by u.foo, weight_string(u.foo)", 3202 "Table": "`user`" 3203 }, 3204 { 3205 "OperatorType": "Join", 3206 "Variant": "Join", 3207 "JoinColumnIndexes": "R:1,R:2,L:1,R:0,L:2,R:0", 3208 "JoinVars": { 3209 "ue_bar": 0 3210 }, 3211 "TableName": "user_extra_unsharded", 3212 "Inputs": [ 3213 { 3214 "OperatorType": "Route", 3215 "Variant": "Scatter", 3216 "Keyspace": { 3217 "Name": "user", 3218 "Sharded": true 3219 }, 3220 "FieldQuery": "select ue.bar, count(*), count(ue.foo), weight_string(ue.bar) from user_extra as ue where 1 != 1 group by ue.bar, weight_string(ue.bar)", 3221 "Query": "select ue.bar, count(*), count(ue.foo), weight_string(ue.bar) from user_extra as ue where ue.bar = :u_foo group by ue.bar, weight_string(ue.bar)", 3222 "Table": "user_extra" 3223 }, 3224 { 3225 "OperatorType": "Route", 3226 "Variant": "Unsharded", 3227 "Keyspace": { 3228 "Name": "main", 3229 "Sharded": false 3230 }, 3231 "FieldQuery": "select count(*), us.bar, weight_string(us.bar) from unsharded as us where 1 != 1 group by us.bar, weight_string(us.bar)", 3232 "Query": "select count(*), us.bar, weight_string(us.bar) from unsharded as us where us.baz = :ue_bar group by us.bar, weight_string(us.bar)", 3233 "Table": "unsharded" 3234 } 3235 ] 3236 } 3237 ] 3238 } 3239 ] 3240 } 3241 ] 3242 } 3243 ] 3244 }, 3245 "TablesUsed": [ 3246 "main.unsharded", 3247 "user.user", 3248 "user.user_extra" 3249 ] 3250 } 3251 }, 3252 { 3253 "comment": "using two distinct columns - min with distinct vindex, sum with distinct without vindex", 3254 "query": "select col1, min(distinct id), sum(distinct col3) from user group by col1", 3255 "v3-plan": { 3256 "QueryType": "SELECT", 3257 "Original": "select col1, min(distinct id), sum(distinct col3) from user group by col1", 3258 "Instructions": { 3259 "OperatorType": "Aggregate", 3260 "Variant": "Ordered", 3261 "Aggregates": "min(1), sum_distinct_sum(2) AS sum(distinct col3)", 3262 "GroupBy": "0", 3263 "Inputs": [ 3264 { 3265 "OperatorType": "Route", 3266 "Variant": "Scatter", 3267 "Keyspace": { 3268 "Name": "user", 3269 "Sharded": true 3270 }, 3271 "FieldQuery": "select col1, min(distinct id), col3, weight_string(col1), weight_string(col3) from `user` where 1 != 1 group by col1, col3, weight_string(col1), weight_string(col3)", 3272 "OrderBy": "(0|3) ASC, (2|4) ASC", 3273 "Query": "select col1, min(distinct id), col3, weight_string(col1), weight_string(col3) from `user` group by col1, col3, weight_string(col1), weight_string(col3) order by col1 asc, col3 asc", 3274 "ResultColumns": 3, 3275 "Table": "`user`" 3276 } 3277 ] 3278 } 3279 }, 3280 "gen4-plan": { 3281 "QueryType": "SELECT", 3282 "Original": "select col1, min(distinct id), sum(distinct col3) from user group by col1", 3283 "Instructions": { 3284 "OperatorType": "Aggregate", 3285 "Variant": "Ordered", 3286 "Aggregates": "min(1) AS min(distinct id), sum_distinct(2|4) AS sum(distinct col3)", 3287 "GroupBy": "(0|3)", 3288 "ResultColumns": 3, 3289 "Inputs": [ 3290 { 3291 "OperatorType": "Route", 3292 "Variant": "Scatter", 3293 "Keyspace": { 3294 "Name": "user", 3295 "Sharded": true 3296 }, 3297 "FieldQuery": "select col1, min(distinct id), col3, weight_string(col1), weight_string(col3) from `user` where 1 != 1 group by col1, weight_string(col1), col3, weight_string(col3)", 3298 "OrderBy": "(0|3) ASC, (2|4) ASC", 3299 "Query": "select col1, min(distinct id), col3, weight_string(col1), weight_string(col3) from `user` group by col1, weight_string(col1), col3, weight_string(col3) order by col1 asc, col3 asc", 3300 "Table": "`user`" 3301 } 3302 ] 3303 }, 3304 "TablesUsed": [ 3305 "user.user" 3306 ] 3307 } 3308 }, 3309 { 3310 "comment": "aggregation on top of semijoin", 3311 "query": "select count(*) from user where exists (select 0 from user_extra where user.apa = user_extra.bar)", 3312 "v3-plan": "VT12001: unsupported: cross-shard correlated subquery", 3313 "gen4-plan": { 3314 "QueryType": "SELECT", 3315 "Original": "select count(*) from user where exists (select 0 from user_extra where user.apa = user_extra.bar)", 3316 "Instructions": { 3317 "OperatorType": "Aggregate", 3318 "Variant": "Scalar", 3319 "Aggregates": "sum_count_star(0) AS count(*)", 3320 "Inputs": [ 3321 { 3322 "OperatorType": "Projection", 3323 "Expressions": [ 3324 "[COLUMN 1] as count(*)" 3325 ], 3326 "Inputs": [ 3327 { 3328 "OperatorType": "SemiJoin", 3329 "JoinVars": { 3330 "user_apa": 0 3331 }, 3332 "TableName": "`user`_user_extra", 3333 "Inputs": [ 3334 { 3335 "OperatorType": "Route", 3336 "Variant": "Scatter", 3337 "Keyspace": { 3338 "Name": "user", 3339 "Sharded": true 3340 }, 3341 "FieldQuery": "select `user`.apa, count(*), weight_string(`user`.apa) from `user` where 1 != 1 group by `user`.apa, weight_string(`user`.apa)", 3342 "Query": "select `user`.apa, count(*), weight_string(`user`.apa) from `user` group by `user`.apa, weight_string(`user`.apa)", 3343 "Table": "`user`" 3344 }, 3345 { 3346 "OperatorType": "Route", 3347 "Variant": "Scatter", 3348 "Keyspace": { 3349 "Name": "user", 3350 "Sharded": true 3351 }, 3352 "FieldQuery": "select 1 from user_extra where 1 != 1", 3353 "Query": "select 1 from user_extra where user_extra.bar = :user_apa", 3354 "Table": "user_extra" 3355 } 3356 ] 3357 } 3358 ] 3359 } 3360 ] 3361 }, 3362 "TablesUsed": [ 3363 "user.user", 3364 "user.user_extra" 3365 ] 3366 } 3367 }, 3368 { 3369 "comment": "we have to track the order of distinct aggregation expressions", 3370 "query": "select val2, count(distinct val1), count(*) from user group by val2", 3371 "v3-plan": { 3372 "QueryType": "SELECT", 3373 "Original": "select val2, count(distinct val1), count(*) from user group by val2", 3374 "Instructions": { 3375 "OperatorType": "Aggregate", 3376 "Variant": "Ordered", 3377 "Aggregates": "count_distinct_count(1) AS count(distinct val1), sum_count(2) AS count", 3378 "GroupBy": "0", 3379 "Inputs": [ 3380 { 3381 "OperatorType": "Route", 3382 "Variant": "Scatter", 3383 "Keyspace": { 3384 "Name": "user", 3385 "Sharded": true 3386 }, 3387 "FieldQuery": "select val2, val1, count(*), weight_string(val2), weight_string(val1) from `user` where 1 != 1 group by val2, val1, weight_string(val2), weight_string(val1)", 3388 "OrderBy": "(0|3) ASC, (1|4) ASC", 3389 "Query": "select val2, val1, count(*), weight_string(val2), weight_string(val1) from `user` group by val2, val1, weight_string(val2), weight_string(val1) order by val2 asc, val1 asc", 3390 "ResultColumns": 3, 3391 "Table": "`user`" 3392 } 3393 ] 3394 } 3395 }, 3396 "gen4-plan": { 3397 "QueryType": "SELECT", 3398 "Original": "select val2, count(distinct val1), count(*) from user group by val2", 3399 "Instructions": { 3400 "OperatorType": "Aggregate", 3401 "Variant": "Ordered", 3402 "Aggregates": "count_distinct(1|4) AS count(distinct val1), sum_count_star(2) AS count(*)", 3403 "GroupBy": "(0|3)", 3404 "ResultColumns": 3, 3405 "Inputs": [ 3406 { 3407 "OperatorType": "Route", 3408 "Variant": "Scatter", 3409 "Keyspace": { 3410 "Name": "user", 3411 "Sharded": true 3412 }, 3413 "FieldQuery": "select val2, val1, count(*), weight_string(val2), weight_string(val1) from `user` where 1 != 1 group by val2, weight_string(val2), val1, weight_string(val1)", 3414 "OrderBy": "(0|3) ASC, (1|4) ASC", 3415 "Query": "select val2, val1, count(*), weight_string(val2), weight_string(val1) from `user` group by val2, weight_string(val2), val1, weight_string(val1) order by val2 asc, val1 asc", 3416 "Table": "`user`" 3417 } 3418 ] 3419 }, 3420 "TablesUsed": [ 3421 "user.user" 3422 ] 3423 } 3424 }, 3425 { 3426 "comment": "group by column alias", 3427 "query": "select ascii(val1) as a, count(*) from user group by a", 3428 "v3-plan": { 3429 "QueryType": "SELECT", 3430 "Original": "select ascii(val1) as a, count(*) from user group by a", 3431 "Instructions": { 3432 "OperatorType": "Aggregate", 3433 "Variant": "Ordered", 3434 "Aggregates": "sum_count(1) AS count", 3435 "GroupBy": "0", 3436 "Inputs": [ 3437 { 3438 "OperatorType": "Route", 3439 "Variant": "Scatter", 3440 "Keyspace": { 3441 "Name": "user", 3442 "Sharded": true 3443 }, 3444 "FieldQuery": "select ascii(val1) as a, count(*), weight_string(ascii(val1)) from `user` where 1 != 1 group by a, weight_string(ascii(val1))", 3445 "OrderBy": "(0|2) ASC", 3446 "Query": "select ascii(val1) as a, count(*), weight_string(ascii(val1)) from `user` group by a, weight_string(ascii(val1)) order by a asc", 3447 "ResultColumns": 2, 3448 "Table": "`user`" 3449 } 3450 ] 3451 } 3452 }, 3453 "gen4-plan": { 3454 "QueryType": "SELECT", 3455 "Original": "select ascii(val1) as a, count(*) from user group by a", 3456 "Instructions": { 3457 "OperatorType": "Aggregate", 3458 "Variant": "Ordered", 3459 "Aggregates": "sum_count_star(1) AS count(*)", 3460 "GroupBy": "(0|2)", 3461 "ResultColumns": 2, 3462 "Inputs": [ 3463 { 3464 "OperatorType": "Route", 3465 "Variant": "Scatter", 3466 "Keyspace": { 3467 "Name": "user", 3468 "Sharded": true 3469 }, 3470 "FieldQuery": "select ascii(val1) as a, count(*), weight_string(ascii(val1)) from `user` where 1 != 1 group by a, weight_string(ascii(val1))", 3471 "OrderBy": "(0|2) ASC", 3472 "Query": "select ascii(val1) as a, count(*), weight_string(ascii(val1)) from `user` group by a, weight_string(ascii(val1)) order by a asc", 3473 "Table": "`user`" 3474 } 3475 ] 3476 }, 3477 "TablesUsed": [ 3478 "user.user" 3479 ] 3480 } 3481 }, 3482 { 3483 "comment": "multiple distinct aggregations on the same column is allowed", 3484 "query": "select tcol1, count(distinct tcol2), sum(distinct tcol2) from user group by tcol1", 3485 "v3-plan": "VT12001: unsupported: only one DISTINCT aggregation allowed in a SELECT: sum(distinct tcol2)", 3486 "gen4-plan": { 3487 "QueryType": "SELECT", 3488 "Original": "select tcol1, count(distinct tcol2), sum(distinct tcol2) from user group by tcol1", 3489 "Instructions": { 3490 "OperatorType": "Aggregate", 3491 "Variant": "Ordered", 3492 "Aggregates": "count_distinct(1|4) AS count(distinct tcol2), sum_distinct(2|4) AS sum(distinct tcol2)", 3493 "GroupBy": "(0|3)", 3494 "ResultColumns": 3, 3495 "Inputs": [ 3496 { 3497 "OperatorType": "Route", 3498 "Variant": "Scatter", 3499 "Keyspace": { 3500 "Name": "user", 3501 "Sharded": true 3502 }, 3503 "FieldQuery": "select tcol1, tcol2, tcol2, weight_string(tcol1), weight_string(tcol2) from `user` where 1 != 1 group by tcol1, weight_string(tcol1), tcol2, weight_string(tcol2)", 3504 "OrderBy": "(0|3) ASC, (1|4) ASC", 3505 "Query": "select tcol1, tcol2, tcol2, weight_string(tcol1), weight_string(tcol2) from `user` group by tcol1, weight_string(tcol1), tcol2, weight_string(tcol2) order by tcol1 asc, tcol2 asc", 3506 "Table": "`user`" 3507 } 3508 ] 3509 }, 3510 "TablesUsed": [ 3511 "user.user" 3512 ] 3513 } 3514 }, 3515 { 3516 "comment": "multiple distinct aggregations on the same column in different positions", 3517 "query": "select count(distinct tcol2), tcol1, count(*), sum(distinct tcol2) from user group by tcol1", 3518 "v3-plan": "VT12001: unsupported: only one DISTINCT aggregation allowed in a SELECT: sum(distinct tcol2)", 3519 "gen4-plan": { 3520 "QueryType": "SELECT", 3521 "Original": "select count(distinct tcol2), tcol1, count(*), sum(distinct tcol2) from user group by tcol1", 3522 "Instructions": { 3523 "OperatorType": "Aggregate", 3524 "Variant": "Ordered", 3525 "Aggregates": "count_distinct(0|4) AS count(distinct tcol2), sum_count_star(2) AS count(*), sum_distinct(3|4) AS sum(distinct tcol2)", 3526 "GroupBy": "(1|5)", 3527 "ResultColumns": 4, 3528 "Inputs": [ 3529 { 3530 "OperatorType": "Route", 3531 "Variant": "Scatter", 3532 "Keyspace": { 3533 "Name": "user", 3534 "Sharded": true 3535 }, 3536 "FieldQuery": "select tcol2, tcol1, count(*), tcol2, weight_string(tcol2), weight_string(tcol1) from `user` where 1 != 1 group by tcol2, weight_string(tcol2), tcol1, weight_string(tcol1)", 3537 "OrderBy": "(1|5) ASC, (0|4) ASC", 3538 "Query": "select tcol2, tcol1, count(*), tcol2, weight_string(tcol2), weight_string(tcol1) from `user` group by tcol2, weight_string(tcol2), tcol1, weight_string(tcol1) order by tcol1 asc, tcol2 asc", 3539 "Table": "`user`" 3540 } 3541 ] 3542 }, 3543 "TablesUsed": [ 3544 "user.user" 3545 ] 3546 } 3547 }, 3548 { 3549 "comment": "distinct aggregation will 3 table join query", 3550 "query": "select u.textcol1, count(distinct u.val2) from user u join user u2 on u.val2 = u2.id join music m on u2.val2 = m.id group by u.textcol1", 3551 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 3552 "gen4-plan": { 3553 "QueryType": "SELECT", 3554 "Original": "select u.textcol1, count(distinct u.val2) from user u join user u2 on u.val2 = u2.id join music m on u2.val2 = m.id group by u.textcol1", 3555 "Instructions": { 3556 "OperatorType": "Aggregate", 3557 "Variant": "Ordered", 3558 "Aggregates": "count_distinct(1|2) AS count(distinct u.val2)", 3559 "GroupBy": "0 COLLATE latin1_swedish_ci", 3560 "ResultColumns": 2, 3561 "Inputs": [ 3562 { 3563 "OperatorType": "Projection", 3564 "Expressions": [ 3565 "[COLUMN 0] as textcol1", 3566 "[COLUMN 1] as val2", 3567 "[COLUMN 2]" 3568 ], 3569 "Inputs": [ 3570 { 3571 "OperatorType": "Join", 3572 "Variant": "Join", 3573 "JoinColumnIndexes": "L:2,L:3,L:5", 3574 "JoinVars": { 3575 "u2_val2": 0 3576 }, 3577 "TableName": "`user`_`user`_music", 3578 "Inputs": [ 3579 { 3580 "OperatorType": "Join", 3581 "Variant": "Join", 3582 "JoinColumnIndexes": "R:0,R:0,L:2,L:0,R:1,L:1", 3583 "JoinVars": { 3584 "u_val2": 0 3585 }, 3586 "TableName": "`user`_`user`", 3587 "Inputs": [ 3588 { 3589 "OperatorType": "Route", 3590 "Variant": "Scatter", 3591 "Keyspace": { 3592 "Name": "user", 3593 "Sharded": true 3594 }, 3595 "FieldQuery": "select u.val2, weight_string(u.val2), u.textcol1 from `user` as u where 1 != 1 group by u.val2, weight_string(u.val2), u.textcol1", 3596 "OrderBy": "2 ASC COLLATE latin1_swedish_ci, (0|1) ASC", 3597 "Query": "select u.val2, weight_string(u.val2), u.textcol1 from `user` as u group by u.val2, weight_string(u.val2), u.textcol1 order by u.textcol1 asc, u.val2 asc", 3598 "Table": "`user`" 3599 }, 3600 { 3601 "OperatorType": "Route", 3602 "Variant": "EqualUnique", 3603 "Keyspace": { 3604 "Name": "user", 3605 "Sharded": true 3606 }, 3607 "FieldQuery": "select u2.val2, weight_string(u2.val2) from `user` as u2 where 1 != 1 group by u2.val2, weight_string(u2.val2)", 3608 "Query": "select u2.val2, weight_string(u2.val2) from `user` as u2 where u2.id = :u_val2 group by u2.val2, weight_string(u2.val2)", 3609 "Table": "`user`", 3610 "Values": [ 3611 ":u_val2" 3612 ], 3613 "Vindex": "user_index" 3614 } 3615 ] 3616 }, 3617 { 3618 "OperatorType": "Route", 3619 "Variant": "EqualUnique", 3620 "Keyspace": { 3621 "Name": "user", 3622 "Sharded": true 3623 }, 3624 "FieldQuery": "select 1 from music as m where 1 != 1", 3625 "Query": "select 1 from music as m where m.id = :u2_val2", 3626 "Table": "music", 3627 "Values": [ 3628 ":u2_val2" 3629 ], 3630 "Vindex": "music_user_map" 3631 } 3632 ] 3633 } 3634 ] 3635 } 3636 ] 3637 }, 3638 "TablesUsed": [ 3639 "user.music", 3640 "user.user" 3641 ] 3642 } 3643 }, 3644 { 3645 "comment": "interleaving grouping, aggregation and join", 3646 "query": "select user.col, min(user_extra.foo), user.bar, max(user_extra.bar) from user join user_extra on user.col = user_extra.bar group by user.col, user.bar", 3647 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 3648 "gen4-plan": { 3649 "QueryType": "SELECT", 3650 "Original": "select user.col, min(user_extra.foo), user.bar, max(user_extra.bar) from user join user_extra on user.col = user_extra.bar group by user.col, user.bar", 3651 "Instructions": { 3652 "OperatorType": "Aggregate", 3653 "Variant": "Ordered", 3654 "Aggregates": "min(1) AS min(user_extra.foo), max(3) AS max(user_extra.bar)", 3655 "GroupBy": "0, (2|4)", 3656 "ResultColumns": 4, 3657 "Inputs": [ 3658 { 3659 "OperatorType": "Projection", 3660 "Expressions": [ 3661 "[COLUMN 0] as col", 3662 "[COLUMN 3] as min(user_extra.foo)", 3663 "[COLUMN 1] as bar", 3664 "[COLUMN 4] as max(user_extra.bar)", 3665 "[COLUMN 2]" 3666 ], 3667 "Inputs": [ 3668 { 3669 "OperatorType": "Join", 3670 "Variant": "Join", 3671 "JoinColumnIndexes": "L:0,L:1,L:2,R:1,R:2", 3672 "JoinVars": { 3673 "user_col": 0 3674 }, 3675 "TableName": "`user`_user_extra", 3676 "Inputs": [ 3677 { 3678 "OperatorType": "Route", 3679 "Variant": "Scatter", 3680 "Keyspace": { 3681 "Name": "user", 3682 "Sharded": true 3683 }, 3684 "FieldQuery": "select `user`.col, `user`.bar, weight_string(`user`.bar) from `user` where 1 != 1 group by `user`.col, `user`.bar, weight_string(`user`.bar)", 3685 "OrderBy": "0 ASC, (1|2) ASC", 3686 "Query": "select `user`.col, `user`.bar, weight_string(`user`.bar) from `user` group by `user`.col, `user`.bar, weight_string(`user`.bar) order by `user`.col asc, `user`.bar asc", 3687 "Table": "`user`" 3688 }, 3689 { 3690 "OperatorType": "Route", 3691 "Variant": "Scatter", 3692 "Keyspace": { 3693 "Name": "user", 3694 "Sharded": true 3695 }, 3696 "FieldQuery": "select 1, min(user_extra.foo), max(user_extra.bar) from user_extra where 1 != 1 group by 1", 3697 "Query": "select 1, min(user_extra.foo), max(user_extra.bar) from user_extra where user_extra.bar = :user_col group by 1", 3698 "Table": "user_extra" 3699 } 3700 ] 3701 } 3702 ] 3703 } 3704 ] 3705 }, 3706 "TablesUsed": [ 3707 "user.user", 3708 "user.user_extra" 3709 ] 3710 } 3711 }, 3712 { 3713 "comment": "group_concat on single shards", 3714 "query": "select group_concat(user_id order by name), id from user group by id", 3715 "v3-plan": { 3716 "QueryType": "SELECT", 3717 "Original": "select group_concat(user_id order by name), id from user group by id", 3718 "Instructions": { 3719 "OperatorType": "Route", 3720 "Variant": "Scatter", 3721 "Keyspace": { 3722 "Name": "user", 3723 "Sharded": true 3724 }, 3725 "FieldQuery": "select group_concat(user_id order by `name` asc), id from `user` where 1 != 1 group by id", 3726 "Query": "select group_concat(user_id order by `name` asc), id from `user` group by id", 3727 "Table": "`user`" 3728 } 3729 }, 3730 "gen4-plan": { 3731 "QueryType": "SELECT", 3732 "Original": "select group_concat(user_id order by name), id from user group by id", 3733 "Instructions": { 3734 "OperatorType": "Route", 3735 "Variant": "Scatter", 3736 "Keyspace": { 3737 "Name": "user", 3738 "Sharded": true 3739 }, 3740 "FieldQuery": "select group_concat(user_id order by `name` asc), id from `user` where 1 != 1 group by id", 3741 "Query": "select group_concat(user_id order by `name` asc), id from `user` group by id", 3742 "Table": "`user`" 3743 }, 3744 "TablesUsed": [ 3745 "user.user" 3746 ] 3747 } 3748 }, 3749 { 3750 "comment": "select count(distinct user_id, name) from unsharded", 3751 "query": "select count(distinct user_id, name) from unsharded", 3752 "v3-plan": { 3753 "QueryType": "SELECT", 3754 "Original": "select count(distinct user_id, name) from unsharded", 3755 "Instructions": { 3756 "OperatorType": "Route", 3757 "Variant": "Unsharded", 3758 "Keyspace": { 3759 "Name": "main", 3760 "Sharded": false 3761 }, 3762 "FieldQuery": "select count(distinct user_id, `name`) from unsharded where 1 != 1", 3763 "Query": "select count(distinct user_id, `name`) from unsharded", 3764 "Table": "unsharded" 3765 } 3766 }, 3767 "gen4-plan": { 3768 "QueryType": "SELECT", 3769 "Original": "select count(distinct user_id, name) from unsharded", 3770 "Instructions": { 3771 "OperatorType": "Route", 3772 "Variant": "Unsharded", 3773 "Keyspace": { 3774 "Name": "main", 3775 "Sharded": false 3776 }, 3777 "FieldQuery": "select count(distinct user_id, `name`) from unsharded where 1 != 1", 3778 "Query": "select count(distinct user_id, `name`) from unsharded", 3779 "Table": "unsharded" 3780 }, 3781 "TablesUsed": [ 3782 "main.unsharded" 3783 ] 3784 } 3785 }, 3786 { 3787 "comment": "select count(distinct user_id, name) from user", 3788 "query": "select count(distinct user_id, name) from user", 3789 "v3-plan": "VT12001: unsupported: only one expression is allowed inside aggregates: count(distinct user_id, `name`)", 3790 "gen4-plan": "VT03001: aggregate functions take a single argument 'count(distinct user_id, `name`)'" 3791 }, 3792 { 3793 "comment": "select sum(col) from (select user.col as col, 32 from user join user_extra) t", 3794 "query": "select sum(col) from (select user.col as col, 32 from user join user_extra) t", 3795 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 3796 "gen4-plan": { 3797 "QueryType": "SELECT", 3798 "Original": "select sum(col) from (select user.col as col, 32 from user join user_extra) t", 3799 "Instructions": { 3800 "OperatorType": "Aggregate", 3801 "Variant": "Scalar", 3802 "Aggregates": "sum(0) AS sum(col)", 3803 "Inputs": [ 3804 { 3805 "OperatorType": "Projection", 3806 "Expressions": [ 3807 "[COLUMN 2] * COALESCE([COLUMN 3], INT64(1)) as sum(col)" 3808 ], 3809 "Inputs": [ 3810 { 3811 "OperatorType": "Join", 3812 "Variant": "Join", 3813 "JoinColumnIndexes": "L:0,L:1,L:2,R:1", 3814 "TableName": "`user`_user_extra", 3815 "Inputs": [ 3816 { 3817 "OperatorType": "Route", 3818 "Variant": "Scatter", 3819 "Keyspace": { 3820 "Name": "user", 3821 "Sharded": true 3822 }, 3823 "FieldQuery": "select `user`.col as col, 32, sum(col) from `user` where 1 != 1", 3824 "Query": "select `user`.col as col, 32, sum(col) from `user`", 3825 "Table": "`user`" 3826 }, 3827 { 3828 "OperatorType": "Route", 3829 "Variant": "Scatter", 3830 "Keyspace": { 3831 "Name": "user", 3832 "Sharded": true 3833 }, 3834 "FieldQuery": "select 1, count(*) from user_extra where 1 != 1 group by 1", 3835 "Query": "select 1, count(*) from user_extra group by 1", 3836 "Table": "user_extra" 3837 } 3838 ] 3839 } 3840 ] 3841 } 3842 ] 3843 }, 3844 "TablesUsed": [ 3845 "user.user", 3846 "user.user_extra" 3847 ] 3848 } 3849 }, 3850 { 3851 "comment": "find aggregation expression and use column offset in filter", 3852 "query": "select foo, count(*) from user group by foo having count(*) = 3", 3853 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 3854 "gen4-plan": { 3855 "QueryType": "SELECT", 3856 "Original": "select foo, count(*) from user group by foo having count(*) = 3", 3857 "Instructions": { 3858 "OperatorType": "SimpleProjection", 3859 "Columns": [ 3860 0, 3861 1 3862 ], 3863 "Inputs": [ 3864 { 3865 "OperatorType": "Filter", 3866 "Predicate": ":1 = 3", 3867 "Inputs": [ 3868 { 3869 "OperatorType": "Aggregate", 3870 "Variant": "Ordered", 3871 "Aggregates": "sum_count_star(1) AS count(*)", 3872 "GroupBy": "(0|2)", 3873 "Inputs": [ 3874 { 3875 "OperatorType": "Route", 3876 "Variant": "Scatter", 3877 "Keyspace": { 3878 "Name": "user", 3879 "Sharded": true 3880 }, 3881 "FieldQuery": "select foo, count(*), weight_string(foo) from `user` where 1 != 1 group by foo, weight_string(foo)", 3882 "OrderBy": "(0|2) ASC", 3883 "Query": "select foo, count(*), weight_string(foo) from `user` group by foo, weight_string(foo) order by foo asc", 3884 "Table": "`user`" 3885 } 3886 ] 3887 } 3888 ] 3889 } 3890 ] 3891 }, 3892 "TablesUsed": [ 3893 "user.user" 3894 ] 3895 } 3896 }, 3897 { 3898 "comment": "find aggregation expression and use column offset in filter times two", 3899 "query": "select foo, sum(foo), sum(bar) from user group by foo having sum(foo)+sum(bar) = 42", 3900 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 3901 "gen4-plan": { 3902 "QueryType": "SELECT", 3903 "Original": "select foo, sum(foo), sum(bar) from user group by foo having sum(foo)+sum(bar) = 42", 3904 "Instructions": { 3905 "OperatorType": "SimpleProjection", 3906 "Columns": [ 3907 0, 3908 1, 3909 2 3910 ], 3911 "Inputs": [ 3912 { 3913 "OperatorType": "Filter", 3914 "Predicate": ":1 + :2 = 42", 3915 "Inputs": [ 3916 { 3917 "OperatorType": "Aggregate", 3918 "Variant": "Ordered", 3919 "Aggregates": "sum(1) AS sum(foo), sum(2) AS sum(bar)", 3920 "GroupBy": "(0|3)", 3921 "Inputs": [ 3922 { 3923 "OperatorType": "Route", 3924 "Variant": "Scatter", 3925 "Keyspace": { 3926 "Name": "user", 3927 "Sharded": true 3928 }, 3929 "FieldQuery": "select foo, sum(foo), sum(bar), weight_string(foo) from `user` where 1 != 1 group by foo, weight_string(foo)", 3930 "OrderBy": "(0|3) ASC", 3931 "Query": "select foo, sum(foo), sum(bar), weight_string(foo) from `user` group by foo, weight_string(foo) order by foo asc", 3932 "Table": "`user`" 3933 } 3934 ] 3935 } 3936 ] 3937 } 3938 ] 3939 }, 3940 "TablesUsed": [ 3941 "user.user" 3942 ] 3943 } 3944 }, 3945 { 3946 "comment": "find aggregation expression and use column offset in filter times three", 3947 "query": "select foo, sum(foo) as fooSum, sum(bar) as barSum from user group by foo having fooSum+sum(bar) = 42", 3948 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 3949 "gen4-plan": { 3950 "QueryType": "SELECT", 3951 "Original": "select foo, sum(foo) as fooSum, sum(bar) as barSum from user group by foo having fooSum+sum(bar) = 42", 3952 "Instructions": { 3953 "OperatorType": "SimpleProjection", 3954 "Columns": [ 3955 0, 3956 1, 3957 2 3958 ], 3959 "Inputs": [ 3960 { 3961 "OperatorType": "Filter", 3962 "Predicate": ":1 + :2 = 42", 3963 "Inputs": [ 3964 { 3965 "OperatorType": "Aggregate", 3966 "Variant": "Ordered", 3967 "Aggregates": "sum(1) AS fooSum, sum(2) AS barSum", 3968 "GroupBy": "(0|3)", 3969 "Inputs": [ 3970 { 3971 "OperatorType": "Route", 3972 "Variant": "Scatter", 3973 "Keyspace": { 3974 "Name": "user", 3975 "Sharded": true 3976 }, 3977 "FieldQuery": "select foo, sum(foo) as fooSum, sum(bar) as barSum, weight_string(foo) from `user` where 1 != 1 group by foo, weight_string(foo)", 3978 "OrderBy": "(0|3) ASC", 3979 "Query": "select foo, sum(foo) as fooSum, sum(bar) as barSum, weight_string(foo) from `user` group by foo, weight_string(foo) order by foo asc", 3980 "Table": "`user`" 3981 } 3982 ] 3983 } 3984 ] 3985 } 3986 ] 3987 }, 3988 "TablesUsed": [ 3989 "user.user" 3990 ] 3991 } 3992 }, 3993 { 3994 "comment": "having should be able to add new aggregation expressions in having", 3995 "query": "select foo from user group by foo having count(*) = 3", 3996 "v3-plan": "VT12001: unsupported: filtering on results of aggregates", 3997 "gen4-plan": { 3998 "QueryType": "SELECT", 3999 "Original": "select foo from user group by foo having count(*) = 3", 4000 "Instructions": { 4001 "OperatorType": "SimpleProjection", 4002 "Columns": [ 4003 0 4004 ], 4005 "Inputs": [ 4006 { 4007 "OperatorType": "Filter", 4008 "Predicate": ":1 = 3", 4009 "Inputs": [ 4010 { 4011 "OperatorType": "Aggregate", 4012 "Variant": "Ordered", 4013 "Aggregates": "sum_count_star(1) AS count(*)", 4014 "GroupBy": "(0|2)", 4015 "Inputs": [ 4016 { 4017 "OperatorType": "Route", 4018 "Variant": "Scatter", 4019 "Keyspace": { 4020 "Name": "user", 4021 "Sharded": true 4022 }, 4023 "FieldQuery": "select foo, count(*), weight_string(foo) from `user` where 1 != 1 group by foo, weight_string(foo)", 4024 "OrderBy": "(0|2) ASC", 4025 "Query": "select foo, count(*), weight_string(foo) from `user` group by foo, weight_string(foo) order by foo asc", 4026 "Table": "`user`" 4027 } 4028 ] 4029 } 4030 ] 4031 } 4032 ] 4033 }, 4034 "TablesUsed": [ 4035 "user.user" 4036 ] 4037 } 4038 }, 4039 { 4040 "comment": "select u.id from user u join user_extra ue on ue.id = u.id group by u.id having count(u.name) = 3", 4041 "query": "select u.id from user u join user_extra ue on ue.id = u.id group by u.id having count(u.name) = 3", 4042 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 4043 "gen4-plan": { 4044 "QueryType": "SELECT", 4045 "Original": "select u.id from user u join user_extra ue on ue.id = u.id group by u.id having count(u.name) = 3", 4046 "Instructions": { 4047 "OperatorType": "SimpleProjection", 4048 "Columns": [ 4049 0 4050 ], 4051 "Inputs": [ 4052 { 4053 "OperatorType": "Filter", 4054 "Predicate": ":1 = 3", 4055 "Inputs": [ 4056 { 4057 "OperatorType": "Aggregate", 4058 "Variant": "Ordered", 4059 "Aggregates": "sum_count(1) AS count(u.`name`)", 4060 "GroupBy": "(0|2)", 4061 "Inputs": [ 4062 { 4063 "OperatorType": "Projection", 4064 "Expressions": [ 4065 "[COLUMN 0] as id", 4066 "[COLUMN 2] * COALESCE([COLUMN 3], INT64(1)) as count(u.`name`)", 4067 "[COLUMN 1]" 4068 ], 4069 "Inputs": [ 4070 { 4071 "OperatorType": "Sort", 4072 "Variant": "Memory", 4073 "OrderBy": "(0|1) ASC", 4074 "Inputs": [ 4075 { 4076 "OperatorType": "Join", 4077 "Variant": "Join", 4078 "JoinColumnIndexes": "R:1,R:2,L:1,R:0", 4079 "JoinVars": { 4080 "ue_id": 0 4081 }, 4082 "TableName": "user_extra_`user`", 4083 "Inputs": [ 4084 { 4085 "OperatorType": "Route", 4086 "Variant": "Scatter", 4087 "Keyspace": { 4088 "Name": "user", 4089 "Sharded": true 4090 }, 4091 "FieldQuery": "select ue.id, count(*), weight_string(ue.id) from user_extra as ue where 1 != 1 group by ue.id, weight_string(ue.id)", 4092 "Query": "select ue.id, count(*), weight_string(ue.id) from user_extra as ue group by ue.id, weight_string(ue.id)", 4093 "Table": "user_extra" 4094 }, 4095 { 4096 "OperatorType": "Route", 4097 "Variant": "EqualUnique", 4098 "Keyspace": { 4099 "Name": "user", 4100 "Sharded": true 4101 }, 4102 "FieldQuery": "select count(u.`name`), u.id, weight_string(u.id) from `user` as u where 1 != 1 group by u.id, weight_string(u.id)", 4103 "Query": "select count(u.`name`), u.id, weight_string(u.id) from `user` as u where u.id = :ue_id group by u.id, weight_string(u.id)", 4104 "Table": "`user`", 4105 "Values": [ 4106 ":ue_id" 4107 ], 4108 "Vindex": "user_index" 4109 } 4110 ] 4111 } 4112 ] 4113 } 4114 ] 4115 } 4116 ] 4117 } 4118 ] 4119 } 4120 ] 4121 }, 4122 "TablesUsed": [ 4123 "user.user", 4124 "user.user_extra" 4125 ] 4126 } 4127 }, 4128 { 4129 "comment": "select u.id from user u join user_extra ue on ue.user_id = u.id group by u.id having count(u.name) = 3", 4130 "query": "select u.id from user u join user_extra ue on ue.user_id = u.id group by u.id having count(u.name) = 3", 4131 "v3-plan": { 4132 "QueryType": "SELECT", 4133 "Original": "select u.id from user u join user_extra ue on ue.user_id = u.id group by u.id having count(u.name) = 3", 4134 "Instructions": { 4135 "OperatorType": "Route", 4136 "Variant": "Scatter", 4137 "Keyspace": { 4138 "Name": "user", 4139 "Sharded": true 4140 }, 4141 "FieldQuery": "select u.id from `user` as u join user_extra as ue on ue.user_id = u.id where 1 != 1 group by u.id", 4142 "Query": "select u.id from `user` as u join user_extra as ue on ue.user_id = u.id group by u.id having count(u.`name`) = 3", 4143 "Table": "`user`, user_extra" 4144 } 4145 }, 4146 "gen4-plan": { 4147 "QueryType": "SELECT", 4148 "Original": "select u.id from user u join user_extra ue on ue.user_id = u.id group by u.id having count(u.name) = 3", 4149 "Instructions": { 4150 "OperatorType": "Route", 4151 "Variant": "Scatter", 4152 "Keyspace": { 4153 "Name": "user", 4154 "Sharded": true 4155 }, 4156 "FieldQuery": "select u.id from `user` as u, user_extra as ue where 1 != 1 group by u.id", 4157 "Query": "select u.id from `user` as u, user_extra as ue where ue.user_id = u.id group by u.id having count(u.`name`) = 3", 4158 "Table": "`user`, user_extra" 4159 }, 4160 "TablesUsed": [ 4161 "user.user", 4162 "user.user_extra" 4163 ] 4164 } 4165 }, 4166 { 4167 "comment": "only extract the aggregation once, even if used twice", 4168 "query": "select u.id from user u join user_extra ue on ue.id = u.id group by u.id having count(*) < 3 and count(*) > 5", 4169 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 4170 "gen4-plan": { 4171 "QueryType": "SELECT", 4172 "Original": "select u.id from user u join user_extra ue on ue.id = u.id group by u.id having count(*) < 3 and count(*) > 5", 4173 "Instructions": { 4174 "OperatorType": "SimpleProjection", 4175 "Columns": [ 4176 0 4177 ], 4178 "Inputs": [ 4179 { 4180 "OperatorType": "Filter", 4181 "Predicate": ":1 < 3 and :1 > 5", 4182 "Inputs": [ 4183 { 4184 "OperatorType": "Aggregate", 4185 "Variant": "Ordered", 4186 "Aggregates": "sum_count_star(1) AS count(*)", 4187 "GroupBy": "(0|2)", 4188 "Inputs": [ 4189 { 4190 "OperatorType": "Projection", 4191 "Expressions": [ 4192 "[COLUMN 0] as id", 4193 "[COLUMN 2] * COALESCE([COLUMN 3], INT64(1)) as count(*)", 4194 "[COLUMN 1]" 4195 ], 4196 "Inputs": [ 4197 { 4198 "OperatorType": "Sort", 4199 "Variant": "Memory", 4200 "OrderBy": "(0|1) ASC", 4201 "Inputs": [ 4202 { 4203 "OperatorType": "Join", 4204 "Variant": "Join", 4205 "JoinColumnIndexes": "R:1,R:2,L:1,R:0", 4206 "JoinVars": { 4207 "ue_id": 0 4208 }, 4209 "TableName": "user_extra_`user`", 4210 "Inputs": [ 4211 { 4212 "OperatorType": "Route", 4213 "Variant": "Scatter", 4214 "Keyspace": { 4215 "Name": "user", 4216 "Sharded": true 4217 }, 4218 "FieldQuery": "select ue.id, count(*), weight_string(ue.id) from user_extra as ue where 1 != 1 group by ue.id, weight_string(ue.id)", 4219 "Query": "select ue.id, count(*), weight_string(ue.id) from user_extra as ue group by ue.id, weight_string(ue.id)", 4220 "Table": "user_extra" 4221 }, 4222 { 4223 "OperatorType": "Route", 4224 "Variant": "EqualUnique", 4225 "Keyspace": { 4226 "Name": "user", 4227 "Sharded": true 4228 }, 4229 "FieldQuery": "select count(*), u.id, weight_string(u.id) from `user` as u where 1 != 1 group by u.id, weight_string(u.id)", 4230 "Query": "select count(*), u.id, weight_string(u.id) from `user` as u where u.id = :ue_id group by u.id, weight_string(u.id)", 4231 "Table": "`user`", 4232 "Values": [ 4233 ":ue_id" 4234 ], 4235 "Vindex": "user_index" 4236 } 4237 ] 4238 } 4239 ] 4240 } 4241 ] 4242 } 4243 ] 4244 } 4245 ] 4246 } 4247 ] 4248 }, 4249 "TablesUsed": [ 4250 "user.user", 4251 "user.user_extra" 4252 ] 4253 } 4254 }, 4255 { 4256 "comment": "select (select 1 from user u having count(ue.col) > 10) from user_extra ue", 4257 "query": "select (select 1 from user u having count(ue.col) > 10) from user_extra ue", 4258 "v3-plan": "VT03020: symbol ue.col not found in subquery", 4259 "gen4-plan": { 4260 "QueryType": "SELECT", 4261 "Original": "select (select 1 from user u having count(ue.col) > 10) from user_extra ue", 4262 "Instructions": { 4263 "OperatorType": "Subquery", 4264 "Variant": "PulloutValue", 4265 "PulloutVars": [ 4266 "__sq1" 4267 ], 4268 "Inputs": [ 4269 { 4270 "OperatorType": "SimpleProjection", 4271 "Columns": [ 4272 0 4273 ], 4274 "Inputs": [ 4275 { 4276 "OperatorType": "Filter", 4277 "Predicate": ":1 > 10", 4278 "Inputs": [ 4279 { 4280 "OperatorType": "Aggregate", 4281 "Variant": "Scalar", 4282 "Aggregates": "random(0) AS 1, sum_count(1) AS count(ue.col)", 4283 "Inputs": [ 4284 { 4285 "OperatorType": "Route", 4286 "Variant": "Scatter", 4287 "Keyspace": { 4288 "Name": "user", 4289 "Sharded": true 4290 }, 4291 "FieldQuery": "select 1, count(ue.col) from `user` as u where 1 != 1", 4292 "Query": "select 1, count(ue.col) from `user` as u", 4293 "Table": "`user`" 4294 } 4295 ] 4296 } 4297 ] 4298 } 4299 ] 4300 }, 4301 { 4302 "OperatorType": "Route", 4303 "Variant": "Scatter", 4304 "Keyspace": { 4305 "Name": "user", 4306 "Sharded": true 4307 }, 4308 "FieldQuery": "select :__sq1 from user_extra as ue where 1 != 1", 4309 "Query": "select :__sq1 from user_extra as ue", 4310 "Table": "user_extra" 4311 } 4312 ] 4313 }, 4314 "TablesUsed": [ 4315 "user.user", 4316 "user.user_extra" 4317 ] 4318 } 4319 }, 4320 { 4321 "comment": "group by and ',' joins with condition", 4322 "query": "select user.col from user join user_extra on user_extra.col = user.col group by user.id", 4323 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 4324 "gen4-plan": { 4325 "QueryType": "SELECT", 4326 "Original": "select user.col from user join user_extra on user_extra.col = user.col group by user.id", 4327 "Instructions": { 4328 "OperatorType": "Aggregate", 4329 "Variant": "Ordered", 4330 "Aggregates": "random(0) AS col", 4331 "GroupBy": "(2|1)", 4332 "ResultColumns": 1, 4333 "Inputs": [ 4334 { 4335 "OperatorType": "Projection", 4336 "Expressions": [ 4337 "[COLUMN 2] as col", 4338 "[COLUMN 1]", 4339 "[COLUMN 0] as id" 4340 ], 4341 "Inputs": [ 4342 { 4343 "OperatorType": "Join", 4344 "Variant": "Join", 4345 "JoinColumnIndexes": "L:1,L:2,L:0", 4346 "JoinVars": { 4347 "user_col": 0 4348 }, 4349 "TableName": "`user`_user_extra", 4350 "Inputs": [ 4351 { 4352 "OperatorType": "Route", 4353 "Variant": "Scatter", 4354 "Keyspace": { 4355 "Name": "user", 4356 "Sharded": true 4357 }, 4358 "FieldQuery": "select `user`.col, `user`.id, weight_string(`user`.id) from `user` where 1 != 1 group by `user`.col, `user`.id, weight_string(`user`.id)", 4359 "OrderBy": "(1|2) ASC", 4360 "Query": "select `user`.col, `user`.id, weight_string(`user`.id) from `user` group by `user`.col, `user`.id, weight_string(`user`.id) order by `user`.id asc", 4361 "Table": "`user`" 4362 }, 4363 { 4364 "OperatorType": "Route", 4365 "Variant": "Scatter", 4366 "Keyspace": { 4367 "Name": "user", 4368 "Sharded": true 4369 }, 4370 "FieldQuery": "select 1 from user_extra where 1 != 1 group by 1", 4371 "Query": "select 1 from user_extra where user_extra.col = :user_col group by 1", 4372 "Table": "user_extra" 4373 } 4374 ] 4375 } 4376 ] 4377 } 4378 ] 4379 }, 4380 "TablesUsed": [ 4381 "user.user", 4382 "user.user_extra" 4383 ] 4384 } 4385 }, 4386 { 4387 "comment": "scatter aggregate symtab lookup error", 4388 "query": "select id, b as id, count(*) from user order by id", 4389 "v3-plan": "VT03021: ambiguous symbol reference: id", 4390 "gen4-plan": { 4391 "QueryType": "SELECT", 4392 "Original": "select id, b as id, count(*) from user order by id", 4393 "Instructions": { 4394 "OperatorType": "Aggregate", 4395 "Variant": "Scalar", 4396 "Aggregates": "random(0) AS id, random(1) AS id, sum_count_star(2) AS count(*)", 4397 "ResultColumns": 3, 4398 "Inputs": [ 4399 { 4400 "OperatorType": "Route", 4401 "Variant": "Scatter", 4402 "Keyspace": { 4403 "Name": "user", 4404 "Sharded": true 4405 }, 4406 "FieldQuery": "select id, b as id, count(*), weight_string(b) from `user` where 1 != 1", 4407 "OrderBy": "(1|3) ASC", 4408 "Query": "select id, b as id, count(*), weight_string(b) from `user` order by id asc", 4409 "Table": "`user`" 4410 } 4411 ] 4412 }, 4413 "TablesUsed": [ 4414 "user.user" 4415 ] 4416 } 4417 }, 4418 { 4419 "comment": "aggr and non-aggr without group by (with query does not give useful result out)", 4420 "query": "select id, count(*) from user", 4421 "v3-plan": { 4422 "QueryType": "SELECT", 4423 "Original": "select id, count(*) from user", 4424 "Instructions": { 4425 "OperatorType": "Aggregate", 4426 "Variant": "Scalar", 4427 "Aggregates": "sum_count(1) AS count", 4428 "Inputs": [ 4429 { 4430 "OperatorType": "Route", 4431 "Variant": "Scatter", 4432 "Keyspace": { 4433 "Name": "user", 4434 "Sharded": true 4435 }, 4436 "FieldQuery": "select id, count(*) from `user` where 1 != 1", 4437 "Query": "select id, count(*) from `user`", 4438 "Table": "`user`" 4439 } 4440 ] 4441 } 4442 }, 4443 "gen4-plan": { 4444 "QueryType": "SELECT", 4445 "Original": "select id, count(*) from user", 4446 "Instructions": { 4447 "OperatorType": "Aggregate", 4448 "Variant": "Scalar", 4449 "Aggregates": "random(0) AS id, sum_count_star(1) AS count(*)", 4450 "Inputs": [ 4451 { 4452 "OperatorType": "Route", 4453 "Variant": "Scatter", 4454 "Keyspace": { 4455 "Name": "user", 4456 "Sharded": true 4457 }, 4458 "FieldQuery": "select id, count(*) from `user` where 1 != 1", 4459 "Query": "select id, count(*) from `user`", 4460 "Table": "`user`" 4461 } 4462 ] 4463 }, 4464 "TablesUsed": [ 4465 "user.user" 4466 ] 4467 } 4468 }, 4469 { 4470 "comment": "group by and ',' joins", 4471 "query": "select user.id from user, user_extra group by id", 4472 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 4473 "gen4-plan": { 4474 "QueryType": "SELECT", 4475 "Original": "select user.id from user, user_extra group by id", 4476 "Instructions": { 4477 "OperatorType": "Aggregate", 4478 "Variant": "Ordered", 4479 "GroupBy": "(0|1)", 4480 "ResultColumns": 1, 4481 "Inputs": [ 4482 { 4483 "OperatorType": "Projection", 4484 "Expressions": [ 4485 "[COLUMN 0] as id", 4486 "[COLUMN 1]" 4487 ], 4488 "Inputs": [ 4489 { 4490 "OperatorType": "Join", 4491 "Variant": "Join", 4492 "JoinColumnIndexes": "L:0,L:1", 4493 "TableName": "`user`_user_extra", 4494 "Inputs": [ 4495 { 4496 "OperatorType": "Route", 4497 "Variant": "Scatter", 4498 "Keyspace": { 4499 "Name": "user", 4500 "Sharded": true 4501 }, 4502 "FieldQuery": "select id, weight_string(id) from `user` where 1 != 1 group by id, weight_string(id)", 4503 "OrderBy": "(0|1) ASC", 4504 "Query": "select id, weight_string(id) from `user` group by id, weight_string(id) order by id asc", 4505 "Table": "`user`" 4506 }, 4507 { 4508 "OperatorType": "Route", 4509 "Variant": "Scatter", 4510 "Keyspace": { 4511 "Name": "user", 4512 "Sharded": true 4513 }, 4514 "FieldQuery": "select 1 from user_extra where 1 != 1", 4515 "Query": "select 1 from user_extra", 4516 "Table": "user_extra" 4517 } 4518 ] 4519 } 4520 ] 4521 } 4522 ] 4523 }, 4524 "TablesUsed": [ 4525 "user.user", 4526 "user.user_extra" 4527 ] 4528 } 4529 }, 4530 { 4531 "comment": "count on column from LIMIT", 4532 "query": "select count(city) from (select phone, id, city from user where id > 12 limit 10) as x", 4533 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 4534 "gen4-plan": { 4535 "QueryType": "SELECT", 4536 "Original": "select count(city) from (select phone, id, city from user where id > 12 limit 10) as x", 4537 "Instructions": { 4538 "OperatorType": "Aggregate", 4539 "Variant": "Scalar", 4540 "Aggregates": "count(0) AS count(city)", 4541 "Inputs": [ 4542 { 4543 "OperatorType": "Projection", 4544 "Expressions": [ 4545 "[COLUMN 2] as count(city)" 4546 ], 4547 "Inputs": [ 4548 { 4549 "OperatorType": "Limit", 4550 "Count": "INT64(10)", 4551 "Inputs": [ 4552 { 4553 "OperatorType": "Route", 4554 "Variant": "Scatter", 4555 "Keyspace": { 4556 "Name": "user", 4557 "Sharded": true 4558 }, 4559 "FieldQuery": "select phone, id, city from `user` where 1 != 1", 4560 "Query": "select phone, id, city from `user` where id > 12 limit :__upper_limit", 4561 "Table": "`user`" 4562 } 4563 ] 4564 } 4565 ] 4566 } 4567 ] 4568 }, 4569 "TablesUsed": [ 4570 "user.user" 4571 ] 4572 } 4573 }, 4574 { 4575 "comment": "count(*) on column from LIMIT", 4576 "query": "select count(*) from (select phone, id, city from user where id > 12 limit 10) as x", 4577 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 4578 "gen4-plan": { 4579 "QueryType": "SELECT", 4580 "Original": "select count(*) from (select phone, id, city from user where id > 12 limit 10) as x", 4581 "Instructions": { 4582 "OperatorType": "Aggregate", 4583 "Variant": "Scalar", 4584 "Aggregates": "count_star(0) AS count(*)", 4585 "Inputs": [ 4586 { 4587 "OperatorType": "Projection", 4588 "Expressions": [ 4589 "[COLUMN 0] as count(*)" 4590 ], 4591 "Inputs": [ 4592 { 4593 "OperatorType": "Limit", 4594 "Count": "INT64(10)", 4595 "Inputs": [ 4596 { 4597 "OperatorType": "Route", 4598 "Variant": "Scatter", 4599 "Keyspace": { 4600 "Name": "user", 4601 "Sharded": true 4602 }, 4603 "FieldQuery": "select phone, id, city from `user` where 1 != 1", 4604 "Query": "select phone, id, city from `user` where id > 12 limit :__upper_limit", 4605 "Table": "`user`" 4606 } 4607 ] 4608 } 4609 ] 4610 } 4611 ] 4612 }, 4613 "TablesUsed": [ 4614 "user.user" 4615 ] 4616 } 4617 }, 4618 { 4619 "comment": "count non-null columns incoming from outer joins should work well", 4620 "query": "select count(col) from (select user_extra.col as col from user left join user_extra on user.id = user_extra.id limit 10) as x", 4621 "plan": { 4622 "QueryType": "SELECT", 4623 "Original": "select count(col) from (select user_extra.col as col from user left join user_extra on user.id = user_extra.id limit 10) as x", 4624 "Instructions": { 4625 "OperatorType": "Aggregate", 4626 "Variant": "Scalar", 4627 "Aggregates": "count(0) AS count(col)", 4628 "Inputs": [ 4629 { 4630 "OperatorType": "Projection", 4631 "Expressions": [ 4632 "[COLUMN 0] as count(col)" 4633 ], 4634 "Inputs": [ 4635 { 4636 "OperatorType": "Limit", 4637 "Count": "INT64(10)", 4638 "Inputs": [ 4639 { 4640 "OperatorType": "Join", 4641 "Variant": "LeftJoin", 4642 "JoinColumnIndexes": "R:0", 4643 "JoinVars": { 4644 "user_id": 0 4645 }, 4646 "TableName": "`user`_user_extra", 4647 "Inputs": [ 4648 { 4649 "OperatorType": "Route", 4650 "Variant": "Scatter", 4651 "Keyspace": { 4652 "Name": "user", 4653 "Sharded": true 4654 }, 4655 "FieldQuery": "select `user`.id from `user` where 1 != 1", 4656 "Query": "select `user`.id from `user`", 4657 "Table": "`user`" 4658 }, 4659 { 4660 "OperatorType": "Route", 4661 "Variant": "Scatter", 4662 "Keyspace": { 4663 "Name": "user", 4664 "Sharded": true 4665 }, 4666 "FieldQuery": "select user_extra.col as col from user_extra where 1 != 1", 4667 "Query": "select user_extra.col as col from user_extra where user_extra.id = :user_id", 4668 "Table": "user_extra" 4669 } 4670 ] 4671 } 4672 ] 4673 } 4674 ] 4675 } 4676 ] 4677 }, 4678 "TablesUsed": [ 4679 "user.user", 4680 "user.user_extra" 4681 ] 4682 } 4683 }, 4684 { 4685 "comment": "grouping on data from derived table", 4686 "query": "select val1, count(*) from (select id, val1 from user where val2 < 4 order by val1 limit 2) as x group by val1", 4687 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 4688 "gen4-plan": { 4689 "QueryType": "SELECT", 4690 "Original": "select val1, count(*) from (select id, val1 from user where val2 < 4 order by val1 limit 2) as x group by val1", 4691 "Instructions": { 4692 "OperatorType": "Aggregate", 4693 "Variant": "Ordered", 4694 "Aggregates": "count_star(1) AS count(*)", 4695 "GroupBy": "(0|2)", 4696 "ResultColumns": 2, 4697 "Inputs": [ 4698 { 4699 "OperatorType": "Projection", 4700 "Expressions": [ 4701 "[COLUMN 1] as val1", 4702 "[COLUMN 0] as count(*)", 4703 "[COLUMN 2]" 4704 ], 4705 "Inputs": [ 4706 { 4707 "OperatorType": "Limit", 4708 "Count": "INT64(2)", 4709 "Inputs": [ 4710 { 4711 "OperatorType": "Route", 4712 "Variant": "Scatter", 4713 "Keyspace": { 4714 "Name": "user", 4715 "Sharded": true 4716 }, 4717 "FieldQuery": "select id, val1, weight_string(val1) from `user` where 1 != 1", 4718 "OrderBy": "(1|2) ASC, (1|2) ASC", 4719 "Query": "select id, val1, weight_string(val1) from `user` where val2 < 4 order by val1 asc, val1 asc limit :__upper_limit", 4720 "Table": "`user`" 4721 } 4722 ] 4723 } 4724 ] 4725 } 4726 ] 4727 }, 4728 "TablesUsed": [ 4729 "user.user" 4730 ] 4731 } 4732 }, 4733 { 4734 "comment": "Can't inline derived table when it has HAVING with aggregation function", 4735 "query": "select * from (select id from user having count(*) = 1) s", 4736 "v3-plan": { 4737 "QueryType": "SELECT", 4738 "Original": "select * from (select id from user having count(*) = 1) s", 4739 "Instructions": { 4740 "OperatorType": "Route", 4741 "Variant": "Scatter", 4742 "Keyspace": { 4743 "Name": "user", 4744 "Sharded": true 4745 }, 4746 "FieldQuery": "select * from (select id from `user` where 1 != 1) as s where 1 != 1", 4747 "Query": "select * from (select id from `user` having count(*) = 1) as s", 4748 "Table": "`user`" 4749 } 4750 }, 4751 "gen4-plan": { 4752 "QueryType": "SELECT", 4753 "Original": "select * from (select id from user having count(*) = 1) s", 4754 "Instructions": { 4755 "OperatorType": "SimpleProjection", 4756 "Columns": [ 4757 0 4758 ], 4759 "Inputs": [ 4760 { 4761 "OperatorType": "Filter", 4762 "Predicate": ":1 = 1", 4763 "Inputs": [ 4764 { 4765 "OperatorType": "Aggregate", 4766 "Variant": "Scalar", 4767 "Aggregates": "random(0) AS id, sum_count_star(1) AS count(*)", 4768 "Inputs": [ 4769 { 4770 "OperatorType": "Route", 4771 "Variant": "Scatter", 4772 "Keyspace": { 4773 "Name": "user", 4774 "Sharded": true 4775 }, 4776 "FieldQuery": "select id, count(*) from `user` where 1 != 1", 4777 "Query": "select id, count(*) from `user`", 4778 "Table": "`user`" 4779 } 4780 ] 4781 } 4782 ] 4783 } 4784 ] 4785 }, 4786 "TablesUsed": [ 4787 "user.user" 4788 ] 4789 } 4790 }, 4791 { 4792 "comment": "Group By X Order By X", 4793 "query": "SELECT user.intcol FROM user GROUP BY user.intcol ORDER BY COUNT(user.intcol)", 4794 "v3-plan": "VT12001: unsupported: in scatter query: complex ORDER BY expression: count(`user`.intcol)", 4795 "gen4-plan": { 4796 "QueryType": "SELECT", 4797 "Original": "SELECT user.intcol FROM user GROUP BY user.intcol ORDER BY COUNT(user.intcol)", 4798 "Instructions": { 4799 "OperatorType": "Sort", 4800 "Variant": "Memory", 4801 "OrderBy": "1 ASC", 4802 "ResultColumns": 1, 4803 "Inputs": [ 4804 { 4805 "OperatorType": "Aggregate", 4806 "Variant": "Ordered", 4807 "Aggregates": "sum_count(1) AS count(`user`.intcol)", 4808 "GroupBy": "0", 4809 "Inputs": [ 4810 { 4811 "OperatorType": "Route", 4812 "Variant": "Scatter", 4813 "Keyspace": { 4814 "Name": "user", 4815 "Sharded": true 4816 }, 4817 "FieldQuery": "select `user`.intcol, count(`user`.intcol) from `user` where 1 != 1 group by `user`.intcol", 4818 "OrderBy": "0 ASC", 4819 "Query": "select `user`.intcol, count(`user`.intcol) from `user` group by `user`.intcol order by `user`.intcol asc", 4820 "Table": "`user`" 4821 } 4822 ] 4823 } 4824 ] 4825 }, 4826 "TablesUsed": [ 4827 "user.user" 4828 ] 4829 } 4830 }, 4831 { 4832 "comment": "AggregateRandom in non full group by query", 4833 "query": "select u.id, u.name, count(m.predef1) from user.user as u join user.user_extra as m on u.id = m.order group by u.id", 4834 "v3-plan": "VT12001: unsupported: cross-shard query with aggregates", 4835 "gen4-plan": { 4836 "QueryType": "SELECT", 4837 "Original": "select u.id, u.name, count(m.predef1) from user.user as u join user.user_extra as m on u.id = m.order group by u.id", 4838 "Instructions": { 4839 "OperatorType": "Aggregate", 4840 "Variant": "Ordered", 4841 "Aggregates": "random(1) AS name, sum_count(2) AS count(m.predef1)", 4842 "GroupBy": "(0|3)", 4843 "ResultColumns": 3, 4844 "Inputs": [ 4845 { 4846 "OperatorType": "Projection", 4847 "Expressions": [ 4848 "[COLUMN 0] as id", 4849 "[COLUMN 2] as name", 4850 "[COLUMN 3] * COALESCE([COLUMN 4], INT64(1)) as count(m.predef1)", 4851 "[COLUMN 1]" 4852 ], 4853 "Inputs": [ 4854 { 4855 "OperatorType": "Sort", 4856 "Variant": "Memory", 4857 "OrderBy": "(0|1) ASC", 4858 "Inputs": [ 4859 { 4860 "OperatorType": "Join", 4861 "Variant": "Join", 4862 "JoinColumnIndexes": "R:2,R:3,R:0,L:1,R:1", 4863 "JoinVars": { 4864 "m_order": 0 4865 }, 4866 "TableName": "user_extra_`user`", 4867 "Inputs": [ 4868 { 4869 "OperatorType": "Route", 4870 "Variant": "Scatter", 4871 "Keyspace": { 4872 "Name": "user", 4873 "Sharded": true 4874 }, 4875 "FieldQuery": "select m.`order`, count(m.predef1), weight_string(m.`order`) from user_extra as m where 1 != 1 group by m.`order`, weight_string(m.`order`)", 4876 "Query": "select m.`order`, count(m.predef1), weight_string(m.`order`) from user_extra as m group by m.`order`, weight_string(m.`order`)", 4877 "Table": "user_extra" 4878 }, 4879 { 4880 "OperatorType": "Route", 4881 "Variant": "EqualUnique", 4882 "Keyspace": { 4883 "Name": "user", 4884 "Sharded": true 4885 }, 4886 "FieldQuery": "select u.`name`, count(*), u.id, weight_string(u.id) from `user` as u where 1 != 1 group by u.id, weight_string(u.id)", 4887 "Query": "select u.`name`, count(*), u.id, weight_string(u.id) from `user` as u where u.id = :m_order group by u.id, weight_string(u.id)", 4888 "Table": "`user`", 4889 "Values": [ 4890 ":m_order" 4891 ], 4892 "Vindex": "user_index" 4893 } 4894 ] 4895 } 4896 ] 4897 } 4898 ] 4899 } 4900 ] 4901 }, 4902 "TablesUsed": [ 4903 "user.user", 4904 "user.user_extra" 4905 ] 4906 } 4907 }, 4908 { 4909 "comment": "Aggregation in a left join query", 4910 "query": "select count (u.id) from user u left join user_extra ue on u.col = ue.col", 4911 "plan": { 4912 "QueryType": "SELECT", 4913 "Original": "select count (u.id) from user u left join user_extra ue on u.col = ue.col", 4914 "Instructions": { 4915 "OperatorType": "Aggregate", 4916 "Variant": "Scalar", 4917 "Aggregates": "sum_count(0) AS count(u.id)", 4918 "Inputs": [ 4919 { 4920 "OperatorType": "Projection", 4921 "Expressions": [ 4922 "[COLUMN 0] * COALESCE([COLUMN 1], INT64(1)) as count(u.id)" 4923 ], 4924 "Inputs": [ 4925 { 4926 "OperatorType": "Join", 4927 "Variant": "LeftJoin", 4928 "JoinColumnIndexes": "L:1,R:1", 4929 "JoinVars": { 4930 "u_col": 0 4931 }, 4932 "TableName": "`user`_user_extra", 4933 "Inputs": [ 4934 { 4935 "OperatorType": "Route", 4936 "Variant": "Scatter", 4937 "Keyspace": { 4938 "Name": "user", 4939 "Sharded": true 4940 }, 4941 "FieldQuery": "select u.col, count(u.id) from `user` as u where 1 != 1 group by u.col", 4942 "Query": "select u.col, count(u.id) from `user` as u group by u.col", 4943 "Table": "`user`" 4944 }, 4945 { 4946 "OperatorType": "Route", 4947 "Variant": "Scatter", 4948 "Keyspace": { 4949 "Name": "user", 4950 "Sharded": true 4951 }, 4952 "FieldQuery": "select 1, count(*) from user_extra as ue where 1 != 1 group by 1", 4953 "Query": "select 1, count(*) from user_extra as ue where ue.col = :u_col group by 1", 4954 "Table": "user_extra" 4955 } 4956 ] 4957 } 4958 ] 4959 } 4960 ] 4961 }, 4962 "TablesUsed": [ 4963 "user.user", 4964 "user.user_extra" 4965 ] 4966 } 4967 }, 4968 { 4969 "comment": "Aggregations from derived table used in arithmetic outside derived table", 4970 "query": "select A.a, A.b, (A.a / A.b) as d from (select sum(a) as a, sum(b) as b from user) A", 4971 "v3-plan": "VT12001: unsupported: expression on results of a cross-shard subquery", 4972 "gen4-plan": { 4973 "QueryType": "SELECT", 4974 "Original": "select A.a, A.b, (A.a / A.b) as d from (select sum(a) as a, sum(b) as b from user) A", 4975 "Instructions": { 4976 "OperatorType": "Projection", 4977 "Expressions": [ 4978 "[COLUMN 0] as a", 4979 "[COLUMN 1] as b", 4980 "[COLUMN 0] / [COLUMN 1] as d" 4981 ], 4982 "Inputs": [ 4983 { 4984 "OperatorType": "Aggregate", 4985 "Variant": "Scalar", 4986 "Aggregates": "sum(0) AS a, sum(1) AS b", 4987 "Inputs": [ 4988 { 4989 "OperatorType": "Route", 4990 "Variant": "Scatter", 4991 "Keyspace": { 4992 "Name": "user", 4993 "Sharded": true 4994 }, 4995 "FieldQuery": "select sum(a) as a, sum(b) as b from `user` where 1 != 1", 4996 "Query": "select sum(a) as a, sum(b) as b from `user`", 4997 "Table": "`user`" 4998 } 4999 ] 5000 } 5001 ] 5002 }, 5003 "TablesUsed": [ 5004 "user.user" 5005 ] 5006 } 5007 }, 5008 { 5009 "comment": "when pushing predicates into derived tables, make sure to put them in HAVING when they contain aggregations", 5010 "query": "select t1.portalId, t1.flowId from (select portalId, flowId, count(*) as count from user_extra where localDate > :v1 group by user_id, flowId order by null) as t1 where count >= :v2", 5011 "v3-plan": { 5012 "QueryType": "SELECT", 5013 "Original": "select t1.portalId, t1.flowId from (select portalId, flowId, count(*) as count from user_extra where localDate > :v1 group by user_id, flowId order by null) as t1 where count >= :v2", 5014 "Instructions": { 5015 "OperatorType": "Route", 5016 "Variant": "Scatter", 5017 "Keyspace": { 5018 "Name": "user", 5019 "Sharded": true 5020 }, 5021 "FieldQuery": "select t1.portalId, t1.flowId from (select portalId, flowId, count(*) as `count` from user_extra where 1 != 1 group by user_id, flowId) as t1 where 1 != 1", 5022 "Query": "select t1.portalId, t1.flowId from (select portalId, flowId, count(*) as `count` from user_extra where localDate > :v1 group by user_id, flowId order by null) as t1 where `count` >= :v2", 5023 "Table": "user_extra" 5024 } 5025 }, 5026 "gen4-plan": { 5027 "QueryType": "SELECT", 5028 "Original": "select t1.portalId, t1.flowId from (select portalId, flowId, count(*) as count from user_extra where localDate > :v1 group by user_id, flowId order by null) as t1 where count >= :v2", 5029 "Instructions": { 5030 "OperatorType": "Route", 5031 "Variant": "Scatter", 5032 "Keyspace": { 5033 "Name": "user", 5034 "Sharded": true 5035 }, 5036 "FieldQuery": "select t1.portalId, t1.flowId from (select portalId, flowId, count(*) as `count` from user_extra where 1 != 1 group by user_id, flowId) as t1 where 1 != 1", 5037 "Query": "select t1.portalId, t1.flowId from (select portalId, flowId, count(*) as `count` from user_extra where localDate > :v1 group by user_id, flowId order by null) as t1 where `count` >= :v2", 5038 "Table": "user_extra" 5039 }, 5040 "TablesUsed": [ 5041 "user.user_extra" 5042 ] 5043 } 5044 }, 5045 { 5046 "comment": "aggregation, where and derived tables - we can push extremums", 5047 "query": "SELECT foo FROM (SELECT foo, max(baz) as bazo FROM (SELECT foo, baz FROM user) f GROUP BY foo) tt WHERE bazo BETWEEN 100 AND 200", 5048 "v3-plan": "VT12001: unsupported: filtering on results of cross-shard subquery", 5049 "gen4-plan": { 5050 "QueryType": "SELECT", 5051 "Original": "SELECT foo FROM (SELECT foo, max(baz) as bazo FROM (SELECT foo, baz FROM user) f GROUP BY foo) tt WHERE bazo BETWEEN 100 AND 200", 5052 "Instructions": { 5053 "OperatorType": "SimpleProjection", 5054 "Columns": [ 5055 0 5056 ], 5057 "Inputs": [ 5058 { 5059 "OperatorType": "Aggregate", 5060 "Variant": "Ordered", 5061 "Aggregates": "max(1) AS bazo", 5062 "GroupBy": "(0|2)", 5063 "Inputs": [ 5064 { 5065 "OperatorType": "Route", 5066 "Variant": "Scatter", 5067 "Keyspace": { 5068 "Name": "user", 5069 "Sharded": true 5070 }, 5071 "FieldQuery": "select foo, max(baz) as bazo, weight_string(foo) from (select foo, baz from `user` where 1 != 1) as f where 1 != 1 group by foo, weight_string(foo)", 5072 "OrderBy": "(0|2) ASC", 5073 "Query": "select foo, max(baz) as bazo, weight_string(foo) from (select foo, baz from `user` having max(baz) between 100 and 200) as f group by foo, weight_string(foo) order by foo asc", 5074 "Table": "`user`" 5075 } 5076 ] 5077 } 5078 ] 5079 }, 5080 "TablesUsed": [ 5081 "user.user" 5082 ] 5083 } 5084 }, 5085 { 5086 "comment": "aggregation, where and derived tables - we can't push aggregations that might need a second layer of aggregation", 5087 "query": "SELECT foo FROM (SELECT foo, count(baz) as bazo FROM (SELECT foo, baz FROM user) f GROUP BY foo) tt WHERE bazo BETWEEN 100 AND 200", 5088 "v3-plan": "VT12001: unsupported: filtering on results of cross-shard subquery", 5089 "gen4-plan": { 5090 "QueryType": "SELECT", 5091 "Original": "SELECT foo FROM (SELECT foo, count(baz) as bazo FROM (SELECT foo, baz FROM user) f GROUP BY foo) tt WHERE bazo BETWEEN 100 AND 200", 5092 "Instructions": { 5093 "OperatorType": "SimpleProjection", 5094 "Columns": [ 5095 1 5096 ], 5097 "Inputs": [ 5098 { 5099 "OperatorType": "Filter", 5100 "Predicate": "bazo between 100 and 200", 5101 "Inputs": [ 5102 { 5103 "OperatorType": "SimpleProjection", 5104 "Columns": [ 5105 1, 5106 0 5107 ], 5108 "Inputs": [ 5109 { 5110 "OperatorType": "Aggregate", 5111 "Variant": "Ordered", 5112 "Aggregates": "sum_count(1) AS bazo", 5113 "GroupBy": "(0|2)", 5114 "Inputs": [ 5115 { 5116 "OperatorType": "Route", 5117 "Variant": "Scatter", 5118 "Keyspace": { 5119 "Name": "user", 5120 "Sharded": true 5121 }, 5122 "FieldQuery": "select foo, count(baz) as bazo, weight_string(foo) from (select foo, baz from `user` where 1 != 1) as f where 1 != 1 group by foo, weight_string(foo)", 5123 "OrderBy": "(0|2) ASC", 5124 "Query": "select foo, count(baz) as bazo, weight_string(foo) from (select foo, baz from `user`) as f group by foo, weight_string(foo) order by foo asc", 5125 "Table": "`user`" 5126 } 5127 ] 5128 } 5129 ] 5130 } 5131 ] 5132 } 5133 ] 5134 }, 5135 "TablesUsed": [ 5136 "user.user" 5137 ] 5138 } 5139 } 5140 ]