vitess.io/vitess@v0.16.2/go/vt/vtgate/planbuilder/testdata/memory_sort_cases.json (about)

     1  [
     2    {
     3      "comment": "Test cases in this file follow the code in memory_sort.go.\n# scatter aggregate order by references ungrouped column",
     4      "query": "select a, b, count(*) from user group by a order by b",
     5      "v3-plan": {
     6        "QueryType": "SELECT",
     7        "Original": "select a, b, count(*) from user group by a order by b",
     8        "Instructions": {
     9          "OperatorType": "Sort",
    10          "Variant": "Memory",
    11          "OrderBy": "(1|3) ASC",
    12          "ResultColumns": 3,
    13          "Inputs": [
    14            {
    15              "OperatorType": "Aggregate",
    16              "Variant": "Ordered",
    17              "Aggregates": "sum_count(2) AS count",
    18              "GroupBy": "0",
    19              "ResultColumns": 4,
    20              "Inputs": [
    21                {
    22                  "OperatorType": "Route",
    23                  "Variant": "Scatter",
    24                  "Keyspace": {
    25                    "Name": "user",
    26                    "Sharded": true
    27                  },
    28                  "FieldQuery": "select a, b, count(*), weight_string(b), weight_string(a) from `user` where 1 != 1 group by a, weight_string(a)",
    29                  "OrderBy": "(0|4) ASC",
    30                  "Query": "select a, b, count(*), weight_string(b), weight_string(a) from `user` group by a, weight_string(a) order by a asc",
    31                  "ResultColumns": 4,
    32                  "Table": "`user`"
    33                }
    34              ]
    35            }
    36          ]
    37        }
    38      },
    39      "gen4-plan": {
    40        "QueryType": "SELECT",
    41        "Original": "select a, b, count(*) from user group by a order by b",
    42        "Instructions": {
    43          "OperatorType": "Aggregate",
    44          "Variant": "Ordered",
    45          "Aggregates": "random(1) AS b, sum_count_star(2) AS count(*)",
    46          "GroupBy": "(0|3)",
    47          "ResultColumns": 3,
    48          "Inputs": [
    49            {
    50              "OperatorType": "Route",
    51              "Variant": "Scatter",
    52              "Keyspace": {
    53                "Name": "user",
    54                "Sharded": true
    55              },
    56              "FieldQuery": "select a, b, count(*), weight_string(a), weight_string(b) from `user` where 1 != 1 group by a, weight_string(a)",
    57              "OrderBy": "(1|4) ASC, (0|3) ASC",
    58              "Query": "select a, b, count(*), weight_string(a), weight_string(b) from `user` group by a, weight_string(a) order by b asc, a asc",
    59              "Table": "`user`"
    60            }
    61          ]
    62        },
    63        "TablesUsed": [
    64          "user.user"
    65        ]
    66      }
    67    },
    68    {
    69      "comment": "scatter aggregate order by references aggregate expression",
    70      "query": "select a, b, count(*) k from user group by a order by k",
    71      "v3-plan": {
    72        "QueryType": "SELECT",
    73        "Original": "select a, b, count(*) k from user group by a order by k",
    74        "Instructions": {
    75          "OperatorType": "Sort",
    76          "Variant": "Memory",
    77          "OrderBy": "2 ASC",
    78          "Inputs": [
    79            {
    80              "OperatorType": "Aggregate",
    81              "Variant": "Ordered",
    82              "Aggregates": "sum_count(2) AS count",
    83              "GroupBy": "0",
    84              "Inputs": [
    85                {
    86                  "OperatorType": "Route",
    87                  "Variant": "Scatter",
    88                  "Keyspace": {
    89                    "Name": "user",
    90                    "Sharded": true
    91                  },
    92                  "FieldQuery": "select a, b, count(*) as k, weight_string(a) from `user` where 1 != 1 group by a, weight_string(a)",
    93                  "OrderBy": "(0|3) ASC",
    94                  "Query": "select a, b, count(*) as k, weight_string(a) from `user` group by a, weight_string(a) order by a asc",
    95                  "ResultColumns": 3,
    96                  "Table": "`user`"
    97                }
    98              ]
    99            }
   100          ]
   101        }
   102      },
   103      "gen4-plan": {
   104        "QueryType": "SELECT",
   105        "Original": "select a, b, count(*) k from user group by a order by k",
   106        "Instructions": {
   107          "OperatorType": "Sort",
   108          "Variant": "Memory",
   109          "OrderBy": "2 ASC",
   110          "ResultColumns": 3,
   111          "Inputs": [
   112            {
   113              "OperatorType": "Aggregate",
   114              "Variant": "Ordered",
   115              "Aggregates": "random(1) AS b, sum_count_star(2) AS k",
   116              "GroupBy": "(0|3)",
   117              "Inputs": [
   118                {
   119                  "OperatorType": "Route",
   120                  "Variant": "Scatter",
   121                  "Keyspace": {
   122                    "Name": "user",
   123                    "Sharded": true
   124                  },
   125                  "FieldQuery": "select a, b, count(*) as k, weight_string(a) from `user` where 1 != 1 group by a, weight_string(a)",
   126                  "OrderBy": "(0|3) ASC",
   127                  "Query": "select a, b, count(*) as k, weight_string(a) from `user` group by a, weight_string(a) order by a asc",
   128                  "Table": "`user`"
   129                }
   130              ]
   131            }
   132          ]
   133        },
   134        "TablesUsed": [
   135          "user.user"
   136        ]
   137      }
   138    },
   139    {
   140      "comment": "select a, b, count(*) k from user group by a order by b, a, k",
   141      "query": "select a, b, count(*) k from user group by a order by b, a, k",
   142      "v3-plan": {
   143        "QueryType": "SELECT",
   144        "Original": "select a, b, count(*) k from user group by a order by b, a, k",
   145        "Instructions": {
   146          "OperatorType": "Sort",
   147          "Variant": "Memory",
   148          "OrderBy": "(1|3) ASC, (0|4) ASC, 2 ASC",
   149          "ResultColumns": 3,
   150          "Inputs": [
   151            {
   152              "OperatorType": "Aggregate",
   153              "Variant": "Ordered",
   154              "Aggregates": "sum_count(2) AS count",
   155              "GroupBy": "0",
   156              "ResultColumns": 5,
   157              "Inputs": [
   158                {
   159                  "OperatorType": "Route",
   160                  "Variant": "Scatter",
   161                  "Keyspace": {
   162                    "Name": "user",
   163                    "Sharded": true
   164                  },
   165                  "FieldQuery": "select a, b, count(*) as k, weight_string(b), weight_string(a) from `user` where 1 != 1 group by a, weight_string(a)",
   166                  "OrderBy": "(0|4) ASC",
   167                  "Query": "select a, b, count(*) as k, weight_string(b), weight_string(a) from `user` group by a, weight_string(a) order by a asc",
   168                  "ResultColumns": 5,
   169                  "Table": "`user`"
   170                }
   171              ]
   172            }
   173          ]
   174        }
   175      },
   176      "gen4-plan": {
   177        "QueryType": "SELECT",
   178        "Original": "select a, b, count(*) k from user group by a order by b, a, k",
   179        "Instructions": {
   180          "OperatorType": "Sort",
   181          "Variant": "Memory",
   182          "OrderBy": "1 ASC, (0|3) ASC, 2 ASC",
   183          "ResultColumns": 3,
   184          "Inputs": [
   185            {
   186              "OperatorType": "Aggregate",
   187              "Variant": "Ordered",
   188              "Aggregates": "random(1) AS b, sum_count_star(2) AS k",
   189              "GroupBy": "(0|3)",
   190              "Inputs": [
   191                {
   192                  "OperatorType": "Route",
   193                  "Variant": "Scatter",
   194                  "Keyspace": {
   195                    "Name": "user",
   196                    "Sharded": true
   197                  },
   198                  "FieldQuery": "select a, b, count(*) as k, weight_string(a) from `user` where 1 != 1 group by a, weight_string(a)",
   199                  "OrderBy": "(0|3) ASC",
   200                  "Query": "select a, b, count(*) as k, weight_string(a) from `user` group by a, weight_string(a) order by a asc",
   201                  "Table": "`user`"
   202                }
   203              ]
   204            }
   205          ]
   206        },
   207        "TablesUsed": [
   208          "user.user"
   209        ]
   210      }
   211    },
   212    {
   213      "comment": "scatter aggregate with memory sort and limit",
   214      "query": "select a, b, count(*) k from user group by a order by k desc limit 10",
   215      "v3-plan": {
   216        "QueryType": "SELECT",
   217        "Original": "select a, b, count(*) k from user group by a order by k desc limit 10",
   218        "Instructions": {
   219          "OperatorType": "Limit",
   220          "Count": "INT64(10)",
   221          "Inputs": [
   222            {
   223              "OperatorType": "Sort",
   224              "Variant": "Memory",
   225              "OrderBy": "2 DESC",
   226              "Inputs": [
   227                {
   228                  "OperatorType": "Aggregate",
   229                  "Variant": "Ordered",
   230                  "Aggregates": "sum_count(2) AS count",
   231                  "GroupBy": "0",
   232                  "Inputs": [
   233                    {
   234                      "OperatorType": "Route",
   235                      "Variant": "Scatter",
   236                      "Keyspace": {
   237                        "Name": "user",
   238                        "Sharded": true
   239                      },
   240                      "FieldQuery": "select a, b, count(*) as k, weight_string(a) from `user` where 1 != 1 group by a, weight_string(a)",
   241                      "OrderBy": "(0|3) ASC",
   242                      "Query": "select a, b, count(*) as k, weight_string(a) from `user` group by a, weight_string(a) order by a asc",
   243                      "ResultColumns": 3,
   244                      "Table": "`user`"
   245                    }
   246                  ]
   247                }
   248              ]
   249            }
   250          ]
   251        }
   252      },
   253      "gen4-plan": {
   254        "QueryType": "SELECT",
   255        "Original": "select a, b, count(*) k from user group by a order by k desc limit 10",
   256        "Instructions": {
   257          "OperatorType": "Limit",
   258          "Count": "INT64(10)",
   259          "Inputs": [
   260            {
   261              "OperatorType": "Sort",
   262              "Variant": "Memory",
   263              "OrderBy": "2 DESC",
   264              "ResultColumns": 3,
   265              "Inputs": [
   266                {
   267                  "OperatorType": "Aggregate",
   268                  "Variant": "Ordered",
   269                  "Aggregates": "random(1) AS b, sum_count_star(2) AS k",
   270                  "GroupBy": "(0|3)",
   271                  "Inputs": [
   272                    {
   273                      "OperatorType": "Route",
   274                      "Variant": "Scatter",
   275                      "Keyspace": {
   276                        "Name": "user",
   277                        "Sharded": true
   278                      },
   279                      "FieldQuery": "select a, b, count(*) as k, weight_string(a) from `user` where 1 != 1 group by a, weight_string(a)",
   280                      "OrderBy": "(0|3) ASC",
   281                      "Query": "select a, b, count(*) as k, weight_string(a) from `user` group by a, weight_string(a) order by a asc",
   282                      "Table": "`user`"
   283                    }
   284                  ]
   285                }
   286              ]
   287            }
   288          ]
   289        },
   290        "TablesUsed": [
   291          "user.user"
   292        ]
   293      }
   294    },
   295    {
   296      "comment": "scatter aggregate with memory sort and order by number",
   297      "query": "select a, b, count(*) k from user group by a order by 1,3",
   298      "v3-plan": {
   299        "QueryType": "SELECT",
   300        "Original": "select a, b, count(*) k from user group by a order by 1,3",
   301        "Instructions": {
   302          "OperatorType": "Sort",
   303          "Variant": "Memory",
   304          "OrderBy": "(0|3) ASC, 2 ASC",
   305          "ResultColumns": 3,
   306          "Inputs": [
   307            {
   308              "OperatorType": "Aggregate",
   309              "Variant": "Ordered",
   310              "Aggregates": "sum_count(2) AS count",
   311              "GroupBy": "0",
   312              "ResultColumns": 4,
   313              "Inputs": [
   314                {
   315                  "OperatorType": "Route",
   316                  "Variant": "Scatter",
   317                  "Keyspace": {
   318                    "Name": "user",
   319                    "Sharded": true
   320                  },
   321                  "FieldQuery": "select a, b, count(*) as k, weight_string(a) from `user` where 1 != 1 group by a, weight_string(a)",
   322                  "OrderBy": "(0|3) ASC",
   323                  "Query": "select a, b, count(*) as k, weight_string(a) from `user` group by a, weight_string(a) order by 1 asc",
   324                  "ResultColumns": 4,
   325                  "Table": "`user`"
   326                }
   327              ]
   328            }
   329          ]
   330        }
   331      },
   332      "gen4-plan": {
   333        "QueryType": "SELECT",
   334        "Original": "select a, b, count(*) k from user group by a order by 1,3",
   335        "Instructions": {
   336          "OperatorType": "Sort",
   337          "Variant": "Memory",
   338          "OrderBy": "(0|3) ASC, 2 ASC",
   339          "ResultColumns": 3,
   340          "Inputs": [
   341            {
   342              "OperatorType": "Aggregate",
   343              "Variant": "Ordered",
   344              "Aggregates": "random(1) AS b, sum_count_star(2) AS k",
   345              "GroupBy": "(0|3)",
   346              "Inputs": [
   347                {
   348                  "OperatorType": "Route",
   349                  "Variant": "Scatter",
   350                  "Keyspace": {
   351                    "Name": "user",
   352                    "Sharded": true
   353                  },
   354                  "FieldQuery": "select a, b, count(*) as k, weight_string(a) from `user` where 1 != 1 group by a, weight_string(a)",
   355                  "OrderBy": "(0|3) ASC",
   356                  "Query": "select a, b, count(*) as k, weight_string(a) from `user` group by a, weight_string(a) order by a asc",
   357                  "Table": "`user`"
   358                }
   359              ]
   360            }
   361          ]
   362        },
   363        "TablesUsed": [
   364          "user.user"
   365        ]
   366      }
   367    },
   368    {
   369      "comment": "scatter aggregate with memory sort and order by number, reuse weight_string\n# we have to use a meaningless construct to test this. TODO: improve to do ordering once for textcol1",
   370      "query": "select textcol1 as t, count(*) k from user group by textcol1 order by textcol1, k, textcol1",
   371      "v3-plan": {
   372        "QueryType": "SELECT",
   373        "Original": "select textcol1 as t, count(*) k from user group by textcol1 order by textcol1, k, textcol1",
   374        "Instructions": {
   375          "OperatorType": "Sort",
   376          "Variant": "Memory",
   377          "OrderBy": "(0|2) ASC, 1 ASC, (0|2) ASC",
   378          "ResultColumns": 2,
   379          "Inputs": [
   380            {
   381              "OperatorType": "Aggregate",
   382              "Variant": "Ordered",
   383              "Aggregates": "sum_count(1) AS count",
   384              "GroupBy": "2",
   385              "ResultColumns": 3,
   386              "Inputs": [
   387                {
   388                  "OperatorType": "Route",
   389                  "Variant": "Scatter",
   390                  "Keyspace": {
   391                    "Name": "user",
   392                    "Sharded": true
   393                  },
   394                  "FieldQuery": "select textcol1 as t, count(*) as k, weight_string(textcol1) from `user` where 1 != 1 group by textcol1, weight_string(textcol1)",
   395                  "OrderBy": "(0|2) ASC, (0|2) ASC",
   396                  "Query": "select textcol1 as t, count(*) as k, weight_string(textcol1) from `user` group by textcol1, weight_string(textcol1) order by textcol1 asc, textcol1 asc",
   397                  "ResultColumns": 3,
   398                  "Table": "`user`"
   399                }
   400              ]
   401            }
   402          ]
   403        }
   404      },
   405      "gen4-plan": {
   406        "QueryType": "SELECT",
   407        "Original": "select textcol1 as t, count(*) k from user group by textcol1 order by textcol1, k, textcol1",
   408        "Instructions": {
   409          "OperatorType": "Sort",
   410          "Variant": "Memory",
   411          "OrderBy": "0 ASC COLLATE latin1_swedish_ci, 1 ASC, 0 ASC COLLATE latin1_swedish_ci",
   412          "Inputs": [
   413            {
   414              "OperatorType": "Aggregate",
   415              "Variant": "Ordered",
   416              "Aggregates": "sum_count_star(1) AS k",
   417              "GroupBy": "0 COLLATE latin1_swedish_ci",
   418              "Inputs": [
   419                {
   420                  "OperatorType": "Route",
   421                  "Variant": "Scatter",
   422                  "Keyspace": {
   423                    "Name": "user",
   424                    "Sharded": true
   425                  },
   426                  "FieldQuery": "select textcol1 as t, count(*) as k from `user` where 1 != 1 group by textcol1",
   427                  "OrderBy": "0 ASC COLLATE latin1_swedish_ci",
   428                  "Query": "select textcol1 as t, count(*) as k from `user` group by textcol1 order by textcol1 asc",
   429                  "Table": "`user`"
   430                }
   431              ]
   432            }
   433          ]
   434        },
   435        "TablesUsed": [
   436          "user.user"
   437        ]
   438      }
   439    },
   440    {
   441      "comment": "order by on a cross-shard derived table",
   442      "query": "select id from (select user.id, user.col from user join user_extra) as t order by id",
   443      "v3-plan": {
   444        "QueryType": "SELECT",
   445        "Original": "select id from (select user.id, user.col from user join user_extra) as t order by id",
   446        "Instructions": {
   447          "OperatorType": "Sort",
   448          "Variant": "Memory",
   449          "OrderBy": "(0|2) ASC",
   450          "ResultColumns": 1,
   451          "Inputs": [
   452            {
   453              "OperatorType": "SimpleProjection",
   454              "Columns": [
   455                0
   456              ],
   457              "Inputs": [
   458                {
   459                  "OperatorType": "Join",
   460                  "Variant": "Join",
   461                  "JoinColumnIndexes": "L:0,L:1,L:2",
   462                  "TableName": "`user`_user_extra",
   463                  "Inputs": [
   464                    {
   465                      "OperatorType": "Route",
   466                      "Variant": "Scatter",
   467                      "Keyspace": {
   468                        "Name": "user",
   469                        "Sharded": true
   470                      },
   471                      "FieldQuery": "select `user`.id, `user`.col, weight_string(`user`.id) from `user` where 1 != 1",
   472                      "Query": "select `user`.id, `user`.col, weight_string(`user`.id) from `user`",
   473                      "Table": "`user`"
   474                    },
   475                    {
   476                      "OperatorType": "Route",
   477                      "Variant": "Scatter",
   478                      "Keyspace": {
   479                        "Name": "user",
   480                        "Sharded": true
   481                      },
   482                      "FieldQuery": "select 1 from user_extra where 1 != 1",
   483                      "Query": "select 1 from user_extra",
   484                      "Table": "user_extra"
   485                    }
   486                  ]
   487                }
   488              ]
   489            }
   490          ]
   491        }
   492      },
   493      "gen4-plan": {
   494        "QueryType": "SELECT",
   495        "Original": "select id from (select user.id, user.col from user join user_extra) as t order by id",
   496        "Instructions": {
   497          "OperatorType": "Sort",
   498          "Variant": "Memory",
   499          "OrderBy": "(0|1) ASC",
   500          "ResultColumns": 1,
   501          "Inputs": [
   502            {
   503              "OperatorType": "SimpleProjection",
   504              "Columns": [
   505                0,
   506                2
   507              ],
   508              "Inputs": [
   509                {
   510                  "OperatorType": "Join",
   511                  "Variant": "Join",
   512                  "JoinColumnIndexes": "L:0,L:1,L:2",
   513                  "TableName": "`user`_user_extra",
   514                  "Inputs": [
   515                    {
   516                      "OperatorType": "Route",
   517                      "Variant": "Scatter",
   518                      "Keyspace": {
   519                        "Name": "user",
   520                        "Sharded": true
   521                      },
   522                      "FieldQuery": "select `user`.id, `user`.col, weight_string(`user`.id) from `user` where 1 != 1",
   523                      "Query": "select `user`.id, `user`.col, weight_string(`user`.id) from `user`",
   524                      "Table": "`user`"
   525                    },
   526                    {
   527                      "OperatorType": "Route",
   528                      "Variant": "Scatter",
   529                      "Keyspace": {
   530                        "Name": "user",
   531                        "Sharded": true
   532                      },
   533                      "FieldQuery": "select 1 from user_extra where 1 != 1",
   534                      "Query": "select 1 from user_extra",
   535                      "Table": "user_extra"
   536                    }
   537                  ]
   538                }
   539              ]
   540            }
   541          ]
   542        },
   543        "TablesUsed": [
   544          "user.user",
   545          "user.user_extra"
   546        ]
   547      }
   548    },
   549    {
   550      "comment": "order by on a cross-shard query. Note: this happens only when an order by column is from the second table",
   551      "query": "select user.col1 as a, user.col2 b, music.col3 c from user, music where user.id = music.id and user.id = 1 order by c",
   552      "v3-plan": {
   553        "QueryType": "SELECT",
   554        "Original": "select user.col1 as a, user.col2 b, music.col3 c from user, music where user.id = music.id and user.id = 1 order by c",
   555        "Instructions": {
   556          "OperatorType": "Sort",
   557          "Variant": "Memory",
   558          "OrderBy": "(2|3) ASC",
   559          "ResultColumns": 3,
   560          "Inputs": [
   561            {
   562              "OperatorType": "Join",
   563              "Variant": "Join",
   564              "JoinColumnIndexes": "L:0,L:1,R:0,R:1",
   565              "JoinVars": {
   566                "user_id": 2
   567              },
   568              "TableName": "`user`_music",
   569              "Inputs": [
   570                {
   571                  "OperatorType": "Route",
   572                  "Variant": "EqualUnique",
   573                  "Keyspace": {
   574                    "Name": "user",
   575                    "Sharded": true
   576                  },
   577                  "FieldQuery": "select `user`.col1 as a, `user`.col2 as b, `user`.id from `user` where 1 != 1",
   578                  "Query": "select `user`.col1 as a, `user`.col2 as b, `user`.id from `user` where `user`.id = 1",
   579                  "Table": "`user`",
   580                  "Values": [
   581                    "INT64(1)"
   582                  ],
   583                  "Vindex": "user_index"
   584                },
   585                {
   586                  "OperatorType": "Route",
   587                  "Variant": "EqualUnique",
   588                  "Keyspace": {
   589                    "Name": "user",
   590                    "Sharded": true
   591                  },
   592                  "FieldQuery": "select music.col3 as c, weight_string(music.col3) from music where 1 != 1",
   593                  "Query": "select music.col3 as c, weight_string(music.col3) from music where music.id = :user_id",
   594                  "Table": "music",
   595                  "Values": [
   596                    ":user_id"
   597                  ],
   598                  "Vindex": "music_user_map"
   599                }
   600              ]
   601            }
   602          ]
   603        }
   604      },
   605      "gen4-plan": {
   606        "QueryType": "SELECT",
   607        "Original": "select user.col1 as a, user.col2 b, music.col3 c from user, music where user.id = music.id and user.id = 1 order by c",
   608        "Instructions": {
   609          "OperatorType": "Sort",
   610          "Variant": "Memory",
   611          "OrderBy": "(2|3) ASC",
   612          "ResultColumns": 3,
   613          "Inputs": [
   614            {
   615              "OperatorType": "Join",
   616              "Variant": "Join",
   617              "JoinColumnIndexes": "L:1,L:2,R:0,R:1",
   618              "JoinVars": {
   619                "user_id": 0
   620              },
   621              "TableName": "`user`_music",
   622              "Inputs": [
   623                {
   624                  "OperatorType": "Route",
   625                  "Variant": "EqualUnique",
   626                  "Keyspace": {
   627                    "Name": "user",
   628                    "Sharded": true
   629                  },
   630                  "FieldQuery": "select `user`.id, `user`.col1 as a, `user`.col2 as b from `user` where 1 != 1",
   631                  "Query": "select `user`.id, `user`.col1 as a, `user`.col2 as b from `user` where `user`.id = 1",
   632                  "Table": "`user`",
   633                  "Values": [
   634                    "INT64(1)"
   635                  ],
   636                  "Vindex": "user_index"
   637                },
   638                {
   639                  "OperatorType": "Route",
   640                  "Variant": "EqualUnique",
   641                  "Keyspace": {
   642                    "Name": "user",
   643                    "Sharded": true
   644                  },
   645                  "FieldQuery": "select music.col3 as c, weight_string(music.col3) from music where 1 != 1",
   646                  "Query": "select music.col3 as c, weight_string(music.col3) from music where music.id = :user_id",
   647                  "Table": "music",
   648                  "Values": [
   649                    ":user_id"
   650                  ],
   651                  "Vindex": "music_user_map"
   652                }
   653              ]
   654            }
   655          ]
   656        },
   657        "TablesUsed": [
   658          "user.music",
   659          "user.user"
   660        ]
   661      }
   662    },
   663    {
   664      "comment": "Order by for join, with mixed cross-shard ordering",
   665      "query": "select user.col1 as a, user.col2, music.col3 from user join music on user.id = music.id where user.id = 1 order by 1 asc, 3 desc, 2 asc",
   666      "v3-plan": {
   667        "QueryType": "SELECT",
   668        "Original": "select user.col1 as a, user.col2, music.col3 from user join music on user.id = music.id where user.id = 1 order by 1 asc, 3 desc, 2 asc",
   669        "Instructions": {
   670          "OperatorType": "Sort",
   671          "Variant": "Memory",
   672          "OrderBy": "(0|3) ASC, (2|4) DESC, (1|5) ASC",
   673          "ResultColumns": 3,
   674          "Inputs": [
   675            {
   676              "OperatorType": "Join",
   677              "Variant": "Join",
   678              "JoinColumnIndexes": "L:0,L:1,R:0,L:2,R:1,L:3",
   679              "JoinVars": {
   680                "user_id": 4
   681              },
   682              "TableName": "`user`_music",
   683              "Inputs": [
   684                {
   685                  "OperatorType": "Route",
   686                  "Variant": "EqualUnique",
   687                  "Keyspace": {
   688                    "Name": "user",
   689                    "Sharded": true
   690                  },
   691                  "FieldQuery": "select `user`.col1 as a, `user`.col2, weight_string(`user`.col1), weight_string(`user`.col2), `user`.id from `user` where 1 != 1",
   692                  "Query": "select `user`.col1 as a, `user`.col2, weight_string(`user`.col1), weight_string(`user`.col2), `user`.id from `user` where `user`.id = 1",
   693                  "Table": "`user`",
   694                  "Values": [
   695                    "INT64(1)"
   696                  ],
   697                  "Vindex": "user_index"
   698                },
   699                {
   700                  "OperatorType": "Route",
   701                  "Variant": "EqualUnique",
   702                  "Keyspace": {
   703                    "Name": "user",
   704                    "Sharded": true
   705                  },
   706                  "FieldQuery": "select music.col3, weight_string(music.col3) from music where 1 != 1",
   707                  "Query": "select music.col3, weight_string(music.col3) from music where music.id = :user_id",
   708                  "Table": "music",
   709                  "Values": [
   710                    ":user_id"
   711                  ],
   712                  "Vindex": "music_user_map"
   713                }
   714              ]
   715            }
   716          ]
   717        }
   718      },
   719      "gen4-plan": {
   720        "QueryType": "SELECT",
   721        "Original": "select user.col1 as a, user.col2, music.col3 from user join music on user.id = music.id where user.id = 1 order by 1 asc, 3 desc, 2 asc",
   722        "Instructions": {
   723          "OperatorType": "Sort",
   724          "Variant": "Memory",
   725          "OrderBy": "(0|3) ASC, (2|4) DESC, (1|5) ASC",
   726          "ResultColumns": 3,
   727          "Inputs": [
   728            {
   729              "OperatorType": "Join",
   730              "Variant": "Join",
   731              "JoinColumnIndexes": "L:1,L:2,R:0,L:3,R:1,L:4",
   732              "JoinVars": {
   733                "user_id": 0
   734              },
   735              "TableName": "`user`_music",
   736              "Inputs": [
   737                {
   738                  "OperatorType": "Route",
   739                  "Variant": "EqualUnique",
   740                  "Keyspace": {
   741                    "Name": "user",
   742                    "Sharded": true
   743                  },
   744                  "FieldQuery": "select `user`.id, `user`.col1 as a, `user`.col2, weight_string(`user`.col1), weight_string(`user`.col2) from `user` where 1 != 1",
   745                  "Query": "select `user`.id, `user`.col1 as a, `user`.col2, weight_string(`user`.col1), weight_string(`user`.col2) from `user` where `user`.id = 1",
   746                  "Table": "`user`",
   747                  "Values": [
   748                    "INT64(1)"
   749                  ],
   750                  "Vindex": "user_index"
   751                },
   752                {
   753                  "OperatorType": "Route",
   754                  "Variant": "EqualUnique",
   755                  "Keyspace": {
   756                    "Name": "user",
   757                    "Sharded": true
   758                  },
   759                  "FieldQuery": "select music.col3, weight_string(music.col3) from music where 1 != 1",
   760                  "Query": "select music.col3, weight_string(music.col3) from music where music.id = :user_id",
   761                  "Table": "music",
   762                  "Values": [
   763                    ":user_id"
   764                  ],
   765                  "Vindex": "music_user_map"
   766                }
   767              ]
   768            }
   769          ]
   770        },
   771        "TablesUsed": [
   772          "user.music",
   773          "user.user"
   774        ]
   775      }
   776    },
   777    {
   778      "comment": "Order by for join, on text column in LHS.",
   779      "query": "select u.a, u.textcol1, un.col2 from user u join unsharded un order by u.textcol1, un.col2",
   780      "v3-plan": {
   781        "QueryType": "SELECT",
   782        "Original": "select u.a, u.textcol1, un.col2 from user u join unsharded un order by u.textcol1, un.col2",
   783        "Instructions": {
   784          "OperatorType": "Sort",
   785          "Variant": "Memory",
   786          "OrderBy": "(1|3) ASC, (2|4) ASC",
   787          "ResultColumns": 3,
   788          "Inputs": [
   789            {
   790              "OperatorType": "Join",
   791              "Variant": "Join",
   792              "JoinColumnIndexes": "L:0,L:1,R:0,L:2,R:1",
   793              "TableName": "`user`_unsharded",
   794              "Inputs": [
   795                {
   796                  "OperatorType": "Route",
   797                  "Variant": "Scatter",
   798                  "Keyspace": {
   799                    "Name": "user",
   800                    "Sharded": true
   801                  },
   802                  "FieldQuery": "select u.a, u.textcol1, weight_string(u.textcol1) from `user` as u where 1 != 1",
   803                  "Query": "select u.a, u.textcol1, weight_string(u.textcol1) from `user` as u",
   804                  "Table": "`user`"
   805                },
   806                {
   807                  "OperatorType": "Route",
   808                  "Variant": "Unsharded",
   809                  "Keyspace": {
   810                    "Name": "main",
   811                    "Sharded": false
   812                  },
   813                  "FieldQuery": "select un.col2, weight_string(un.col2) from unsharded as un where 1 != 1",
   814                  "Query": "select un.col2, weight_string(un.col2) from unsharded as un",
   815                  "Table": "unsharded"
   816                }
   817              ]
   818            }
   819          ]
   820        }
   821      },
   822      "gen4-plan": {
   823        "QueryType": "SELECT",
   824        "Original": "select u.a, u.textcol1, un.col2 from user u join unsharded un order by u.textcol1, un.col2",
   825        "Instructions": {
   826          "OperatorType": "Sort",
   827          "Variant": "Memory",
   828          "OrderBy": "(1|3) ASC COLLATE latin1_swedish_ci, (2|4) ASC",
   829          "ResultColumns": 3,
   830          "Inputs": [
   831            {
   832              "OperatorType": "Join",
   833              "Variant": "Join",
   834              "JoinColumnIndexes": "L:0,L:1,R:0,L:2,R:1",
   835              "TableName": "`user`_unsharded",
   836              "Inputs": [
   837                {
   838                  "OperatorType": "Route",
   839                  "Variant": "Scatter",
   840                  "Keyspace": {
   841                    "Name": "user",
   842                    "Sharded": true
   843                  },
   844                  "FieldQuery": "select u.a, u.textcol1, weight_string(u.textcol1) from `user` as u where 1 != 1",
   845                  "Query": "select u.a, u.textcol1, weight_string(u.textcol1) from `user` as u",
   846                  "Table": "`user`"
   847                },
   848                {
   849                  "OperatorType": "Route",
   850                  "Variant": "Unsharded",
   851                  "Keyspace": {
   852                    "Name": "main",
   853                    "Sharded": false
   854                  },
   855                  "FieldQuery": "select un.col2, weight_string(un.col2) from unsharded as un where 1 != 1",
   856                  "Query": "select un.col2, weight_string(un.col2) from unsharded as un",
   857                  "Table": "unsharded"
   858                }
   859              ]
   860            }
   861          ]
   862        },
   863        "TablesUsed": [
   864          "main.unsharded",
   865          "user.user"
   866        ]
   867      }
   868    },
   869    {
   870      "comment": "Order by for join, on text column in RHS.",
   871      "query": "select u.a, u.textcol1, un.col2 from unsharded un join user u order by u.textcol1, un.col2",
   872      "v3-plan": {
   873        "QueryType": "SELECT",
   874        "Original": "select u.a, u.textcol1, un.col2 from unsharded un join user u order by u.textcol1, un.col2",
   875        "Instructions": {
   876          "OperatorType": "Sort",
   877          "Variant": "Memory",
   878          "OrderBy": "(1|3) ASC, (2|4) ASC",
   879          "ResultColumns": 3,
   880          "Inputs": [
   881            {
   882              "OperatorType": "Join",
   883              "Variant": "Join",
   884              "JoinColumnIndexes": "R:0,R:1,L:0,R:2,L:1",
   885              "TableName": "unsharded_`user`",
   886              "Inputs": [
   887                {
   888                  "OperatorType": "Route",
   889                  "Variant": "Unsharded",
   890                  "Keyspace": {
   891                    "Name": "main",
   892                    "Sharded": false
   893                  },
   894                  "FieldQuery": "select un.col2, weight_string(un.col2) from unsharded as un where 1 != 1",
   895                  "Query": "select un.col2, weight_string(un.col2) from unsharded as un",
   896                  "Table": "unsharded"
   897                },
   898                {
   899                  "OperatorType": "Route",
   900                  "Variant": "Scatter",
   901                  "Keyspace": {
   902                    "Name": "user",
   903                    "Sharded": true
   904                  },
   905                  "FieldQuery": "select u.a, u.textcol1, weight_string(u.textcol1) from `user` as u where 1 != 1",
   906                  "Query": "select u.a, u.textcol1, weight_string(u.textcol1) from `user` as u",
   907                  "Table": "`user`"
   908                }
   909              ]
   910            }
   911          ]
   912        }
   913      },
   914      "gen4-plan": {
   915        "QueryType": "SELECT",
   916        "Original": "select u.a, u.textcol1, un.col2 from unsharded un join user u order by u.textcol1, un.col2",
   917        "Instructions": {
   918          "OperatorType": "Sort",
   919          "Variant": "Memory",
   920          "OrderBy": "(1|3) ASC COLLATE latin1_swedish_ci, (2|4) ASC",
   921          "ResultColumns": 3,
   922          "Inputs": [
   923            {
   924              "OperatorType": "Join",
   925              "Variant": "Join",
   926              "JoinColumnIndexes": "R:0,R:1,L:0,R:2,L:1",
   927              "TableName": "unsharded_`user`",
   928              "Inputs": [
   929                {
   930                  "OperatorType": "Route",
   931                  "Variant": "Unsharded",
   932                  "Keyspace": {
   933                    "Name": "main",
   934                    "Sharded": false
   935                  },
   936                  "FieldQuery": "select un.col2, weight_string(un.col2) from unsharded as un where 1 != 1",
   937                  "Query": "select un.col2, weight_string(un.col2) from unsharded as un",
   938                  "Table": "unsharded"
   939                },
   940                {
   941                  "OperatorType": "Route",
   942                  "Variant": "Scatter",
   943                  "Keyspace": {
   944                    "Name": "user",
   945                    "Sharded": true
   946                  },
   947                  "FieldQuery": "select u.a, u.textcol1, weight_string(u.textcol1) from `user` as u where 1 != 1",
   948                  "Query": "select u.a, u.textcol1, weight_string(u.textcol1) from `user` as u",
   949                  "Table": "`user`"
   950                }
   951              ]
   952            }
   953          ]
   954        },
   955        "TablesUsed": [
   956          "main.unsharded",
   957          "user.user"
   958        ]
   959      }
   960    },
   961    {
   962      "comment": "order by for vindex func",
   963      "query": "select id, keyspace_id, range_start, range_end from user_index where id = :id order by range_start",
   964      "v3-plan": {
   965        "QueryType": "SELECT",
   966        "Original": "select id, keyspace_id, range_start, range_end from user_index where id = :id order by range_start",
   967        "Instructions": {
   968          "OperatorType": "Sort",
   969          "Variant": "Memory",
   970          "OrderBy": "2 ASC",
   971          "Inputs": [
   972            {
   973              "OperatorType": "VindexFunc",
   974              "Variant": "VindexMap",
   975              "Columns": [
   976                0,
   977                1,
   978                2,
   979                3
   980              ],
   981              "Fields": {
   982                "id": "VARBINARY",
   983                "keyspace_id": "VARBINARY",
   984                "range_end": "VARBINARY",
   985                "range_start": "VARBINARY"
   986              },
   987              "Value": ":id",
   988              "Vindex": "user_index"
   989            }
   990          ]
   991        }
   992      },
   993      "gen4-plan": {
   994        "QueryType": "SELECT",
   995        "Original": "select id, keyspace_id, range_start, range_end from user_index where id = :id order by range_start",
   996        "Instructions": {
   997          "OperatorType": "Sort",
   998          "Variant": "Memory",
   999          "OrderBy": "2 ASC",
  1000          "Inputs": [
  1001            {
  1002              "OperatorType": "VindexFunc",
  1003              "Variant": "VindexMap",
  1004              "Columns": [
  1005                0,
  1006                1,
  1007                2,
  1008                3
  1009              ],
  1010              "Fields": {
  1011                "id": "VARBINARY",
  1012                "keyspace_id": "VARBINARY",
  1013                "range_end": "VARBINARY",
  1014                "range_start": "VARBINARY"
  1015              },
  1016              "Value": ":id",
  1017              "Vindex": "user_index"
  1018            }
  1019          ]
  1020        },
  1021        "TablesUsed": [
  1022          "user_index"
  1023        ]
  1024      }
  1025    },
  1026    {
  1027      "comment": "unary expression",
  1028      "query": "select a from user order by binary a desc",
  1029      "v3-plan": "VT12001: unsupported: in scatter query: complex ORDER BY expression: convert(a, binary)",
  1030      "gen4-plan": {
  1031        "QueryType": "SELECT",
  1032        "Original": "select a from user order by binary a desc",
  1033        "Instructions": {
  1034          "OperatorType": "Route",
  1035          "Variant": "Scatter",
  1036          "Keyspace": {
  1037            "Name": "user",
  1038            "Sharded": true
  1039          },
  1040          "FieldQuery": "select a, convert(a, binary), weight_string(convert(a, binary)) from `user` where 1 != 1",
  1041          "OrderBy": "(1|2) DESC",
  1042          "Query": "select a, convert(a, binary), weight_string(convert(a, binary)) from `user` order by convert(a, binary) desc",
  1043          "ResultColumns": 1,
  1044          "Table": "`user`"
  1045        },
  1046        "TablesUsed": [
  1047          "user.user"
  1048        ]
  1049      }
  1050    },
  1051    {
  1052      "comment": "unary expression in join query",
  1053      "query": "select u.a from user u join music m on u.a = m.a order by binary a desc",
  1054      "v3-plan": "VT12001: unsupported: in scatter query: complex ORDER BY expression: convert(a, binary)",
  1055      "gen4-plan": {
  1056        "QueryType": "SELECT",
  1057        "Original": "select u.a from user u join music m on u.a = m.a order by binary a desc",
  1058        "Instructions": {
  1059          "OperatorType": "Join",
  1060          "Variant": "Join",
  1061          "JoinColumnIndexes": "L:0",
  1062          "JoinVars": {
  1063            "u_a": 0
  1064          },
  1065          "TableName": "`user`_music",
  1066          "Inputs": [
  1067            {
  1068              "OperatorType": "Route",
  1069              "Variant": "Scatter",
  1070              "Keyspace": {
  1071                "Name": "user",
  1072                "Sharded": true
  1073              },
  1074              "FieldQuery": "select u.a, convert(a, binary), weight_string(convert(a, binary)) from `user` as u where 1 != 1",
  1075              "OrderBy": "(1|2) DESC",
  1076              "Query": "select u.a, convert(a, binary), weight_string(convert(a, binary)) from `user` as u order by convert(a, binary) desc",
  1077              "Table": "`user`"
  1078            },
  1079            {
  1080              "OperatorType": "Route",
  1081              "Variant": "Scatter",
  1082              "Keyspace": {
  1083                "Name": "user",
  1084                "Sharded": true
  1085              },
  1086              "FieldQuery": "select 1 from music as m where 1 != 1",
  1087              "Query": "select 1 from music as m where m.a = :u_a",
  1088              "Table": "music"
  1089            }
  1090          ]
  1091        },
  1092        "TablesUsed": [
  1093          "user.music",
  1094          "user.user"
  1095        ]
  1096      }
  1097    },
  1098    {
  1099      "comment": "intcol order by",
  1100      "query": "select id, intcol from user order by intcol",
  1101      "v3-plan": {
  1102        "QueryType": "SELECT",
  1103        "Original": "select id, intcol from user order by intcol",
  1104        "Instructions": {
  1105          "OperatorType": "Route",
  1106          "Variant": "Scatter",
  1107          "Keyspace": {
  1108            "Name": "user",
  1109            "Sharded": true
  1110          },
  1111          "FieldQuery": "select id, intcol from `user` where 1 != 1",
  1112          "OrderBy": "1 ASC",
  1113          "Query": "select id, intcol from `user` order by intcol asc",
  1114          "Table": "`user`"
  1115        }
  1116      },
  1117      "gen4-plan": {
  1118        "QueryType": "SELECT",
  1119        "Original": "select id, intcol from user order by intcol",
  1120        "Instructions": {
  1121          "OperatorType": "Route",
  1122          "Variant": "Scatter",
  1123          "Keyspace": {
  1124            "Name": "user",
  1125            "Sharded": true
  1126          },
  1127          "FieldQuery": "select id, intcol from `user` where 1 != 1",
  1128          "OrderBy": "1 ASC",
  1129          "Query": "select id, intcol from `user` order by intcol asc",
  1130          "Table": "`user`"
  1131        },
  1132        "TablesUsed": [
  1133          "user.user"
  1134        ]
  1135      }
  1136    },
  1137    {
  1138      "comment": "scatter order by with order by column not present",
  1139      "query": "select col from user order by id",
  1140      "v3-plan": "VT12001: unsupported: in scatter query: ORDER BY must reference a column in the SELECT list: id asc",
  1141      "gen4-plan": {
  1142        "QueryType": "SELECT",
  1143        "Original": "select col from user order by id",
  1144        "Instructions": {
  1145          "OperatorType": "Route",
  1146          "Variant": "Scatter",
  1147          "Keyspace": {
  1148            "Name": "user",
  1149            "Sharded": true
  1150          },
  1151          "FieldQuery": "select col, id, weight_string(id) from `user` where 1 != 1",
  1152          "OrderBy": "(1|2) ASC",
  1153          "Query": "select col, id, weight_string(id) from `user` order by id asc",
  1154          "ResultColumns": 1,
  1155          "Table": "`user`"
  1156        },
  1157        "TablesUsed": [
  1158          "user.user"
  1159        ]
  1160      }
  1161    }
  1162  ]