github.com/cockroachdb/cockroach@v20.2.0-alpha.1+incompatible/pkg/sql/opt/memo/testdata/memo (about)

     1  exec-ddl
     2  CREATE TABLE a (x INT PRIMARY KEY, y INT)
     3  ----
     4  
     5  exec-ddl
     6  CREATE TABLE b (x STRING PRIMARY KEY, z DECIMAL NOT NULL)
     7  ----
     8  
     9  build
    10  SELECT y, b.x, y+1 AS c
    11  FROM a, b
    12  WHERE a.y>1 AND a.x::string=b.x
    13  ORDER BY y
    14  LIMIT 10
    15  ----
    16  limit
    17   ├── columns: y:2(int!null) x:3(string!null) c:5(int!null)
    18   ├── internal-ordering: +2
    19   ├── cardinality: [0 - 10]
    20   ├── fd: (2)-->(5)
    21   ├── ordering: +2
    22   ├── sort
    23   │    ├── columns: y:2(int!null) b.x:3(string!null) c:5(int!null)
    24   │    ├── fd: (2)-->(5)
    25   │    ├── ordering: +2
    26   │    ├── limit hint: 10.00
    27   │    └── project
    28   │         ├── columns: c:5(int!null) y:2(int!null) b.x:3(string!null)
    29   │         ├── fd: (2)-->(5)
    30   │         ├── select
    31   │         │    ├── columns: a.x:1(int!null) y:2(int!null) b.x:3(string!null) z:4(decimal!null)
    32   │         │    ├── key: (1,3)
    33   │         │    ├── fd: (1)-->(2), (3)-->(4)
    34   │         │    ├── inner-join (cross)
    35   │         │    │    ├── columns: a.x:1(int!null) y:2(int) b.x:3(string!null) z:4(decimal!null)
    36   │         │    │    ├── key: (1,3)
    37   │         │    │    ├── fd: (1)-->(2), (3)-->(4)
    38   │         │    │    ├── scan a
    39   │         │    │    │    ├── columns: a.x:1(int!null) y:2(int)
    40   │         │    │    │    ├── key: (1)
    41   │         │    │    │    └── fd: (1)-->(2)
    42   │         │    │    ├── scan b
    43   │         │    │    │    ├── columns: b.x:3(string!null) z:4(decimal!null)
    44   │         │    │    │    ├── key: (3)
    45   │         │    │    │    └── fd: (3)-->(4)
    46   │         │    │    └── filters (true)
    47   │         │    └── filters
    48   │         │         └── and [type=bool, outer=(1-3), constraints=(/2: [/2 - ])]
    49   │         │              ├── gt [type=bool]
    50   │         │              │    ├── variable: y:2 [type=int]
    51   │         │              │    └── const: 1 [type=int]
    52   │         │              └── eq [type=bool]
    53   │         │                   ├── cast: STRING [type=string]
    54   │         │                   │    └── variable: a.x:1 [type=int]
    55   │         │                   └── variable: b.x:3 [type=string]
    56   │         └── projections
    57   │              └── plus [as=c:5, type=int, outer=(2)]
    58   │                   ├── variable: y:2 [type=int]
    59   │                   └── const: 1 [type=int]
    60   └── const: 10 [type=int]
    61  
    62  opt
    63  SELECT y, b.x, y+1 AS c
    64  FROM a, b
    65  WHERE a.y>1 AND a.x::string=b.x
    66  ORDER BY y
    67  LIMIT 10
    68  ----
    69  project
    70   ├── columns: y:2(int!null) x:3(string!null) c:6(int!null)
    71   ├── cardinality: [0 - 10]
    72   ├── fd: (2)-->(6)
    73   ├── ordering: +2
    74   ├── limit
    75   │    ├── columns: y:2(int!null) b.x:3(string!null) column5:5(string!null)
    76   │    ├── internal-ordering: +2
    77   │    ├── cardinality: [0 - 10]
    78   │    ├── fd: (3)==(5), (5)==(3)
    79   │    ├── ordering: +2
    80   │    ├── inner-join (lookup b)
    81   │    │    ├── columns: y:2(int!null) b.x:3(string!null) column5:5(string!null)
    82   │    │    ├── key columns: [5] = [3]
    83   │    │    ├── lookup columns are key
    84   │    │    ├── fd: (3)==(5), (5)==(3)
    85   │    │    ├── ordering: +2
    86   │    │    ├── limit hint: 10.00
    87   │    │    ├── sort
    88   │    │    │    ├── columns: y:2(int!null) column5:5(string!null)
    89   │    │    │    ├── ordering: +2
    90   │    │    │    ├── limit hint: 100.00
    91   │    │    │    └── project
    92   │    │    │         ├── columns: column5:5(string!null) y:2(int!null)
    93   │    │    │         ├── select
    94   │    │    │         │    ├── columns: a.x:1(int!null) y:2(int!null)
    95   │    │    │         │    ├── key: (1)
    96   │    │    │         │    ├── fd: (1)-->(2)
    97   │    │    │         │    ├── scan a
    98   │    │    │         │    │    ├── columns: a.x:1(int!null) y:2(int)
    99   │    │    │         │    │    ├── key: (1)
   100   │    │    │         │    │    └── fd: (1)-->(2)
   101   │    │    │         │    └── filters
   102   │    │    │         │         └── gt [type=bool, outer=(2), constraints=(/2: [/2 - ]; tight)]
   103   │    │    │         │              ├── variable: y:2 [type=int]
   104   │    │    │         │              └── const: 1 [type=int]
   105   │    │    │         └── projections
   106   │    │    │              └── cast: STRING [as=column5:5, type=string, outer=(1)]
   107   │    │    │                   └── variable: a.x:1 [type=int]
   108   │    │    └── filters (true)
   109   │    └── const: 10 [type=int]
   110   └── projections
   111        └── plus [as=c:6, type=int, outer=(2)]
   112             ├── variable: y:2 [type=int]
   113             └── const: 1 [type=int]
   114  
   115  memo
   116  SELECT y, b.x, y+1 AS c
   117  FROM a, b
   118  WHERE a.y>1 AND a.x::string=b.x
   119  ORDER BY y
   120  LIMIT 10
   121  ----
   122  memo (optimized, ~18KB, required=[presentation: y:2,x:3,c:6] [ordering: +2])
   123   ├── G1: (project G2 G3 y x)
   124   │    ├── [presentation: y:2,x:3,c:6] [ordering: +2]
   125   │    │    ├── best: (project G2="[ordering: +2]" G3 y x)
   126   │    │    └── cost: 1722.59
   127   │    └── []
   128   │         ├── best: (project G2 G3 y x)
   129   │         └── cost: 1722.59
   130   ├── G2: (limit G4 G5 ordering=+2)
   131   │    ├── [ordering: +2]
   132   │    │    ├── best: (limit G4="[ordering: +2] [limit hint: 10.00]" G5 ordering=+2)
   133   │    │    └── cost: 1722.38
   134   │    └── []
   135   │         ├── best: (limit G4="[ordering: +2] [limit hint: 10.00]" G5 ordering=+2)
   136   │         └── cost: 1722.38
   137   ├── G3: (projections G6)
   138   ├── G4: (inner-join G7 G8 G9) (inner-join G8 G7 G9) (lookup-join G7 G10 b,keyCols=[5],outCols=(2,3,5)) (merge-join G8 G7 G10 inner-join,+3,+5)
   139   │    ├── [ordering: +2] [limit hint: 10.00]
   140   │    │    ├── best: (lookup-join G7="[ordering: +2] [limit hint: 100.00]" G10 b,keyCols=[5],outCols=(2,3,5))
   141   │    │    └── cost: 1722.27
   142   │    └── []
   143   │         ├── best: (inner-join G8 G7 G9)
   144   │         └── cost: 2108.40
   145   ├── G5: (const 10)
   146   ├── G6: (plus G11 G12)
   147   ├── G7: (project G13 G14 y)
   148   │    ├── [ordering: +2] [limit hint: 100.00]
   149   │    │    ├── best: (sort G7)
   150   │    │    └── cost: 1119.26
   151   │    ├── [ordering: +5]
   152   │    │    ├── best: (sort G7)
   153   │    │    └── cost: 1119.26
   154   │    └── []
   155   │         ├── best: (project G13 G14 y)
   156   │         └── cost: 1056.71
   157   ├── G8: (scan b,cols=(3))
   158   │    ├── [ordering: +3]
   159   │    │    ├── best: (scan b,cols=(3))
   160   │    │    └── cost: 1030.02
   161   │    └── []
   162   │         ├── best: (scan b,cols=(3))
   163   │         └── cost: 1030.02
   164   ├── G9: (filters G15)
   165   ├── G10: (filters)
   166   ├── G11: (variable y)
   167   ├── G12: (const 1)
   168   ├── G13: (select G16 G17)
   169   │    ├── [ordering: +2] [limit hint: 100.00]
   170   │    │    ├── best: (sort G13)
   171   │    │    └── cost: 1112.58
   172   │    └── []
   173   │         ├── best: (select G16 G17)
   174   │         └── cost: 1050.03
   175   ├── G14: (projections G18)
   176   ├── G15: (eq G19 G20)
   177   ├── G16: (scan a)
   178   │    ├── [ordering: +2] [limit hint: 300.00]
   179   │    │    ├── best: (sort G16)
   180   │    │    └── cost: 1259.35
   181   │    └── []
   182   │         ├── best: (scan a)
   183   │         └── cost: 1040.02
   184   ├── G17: (filters G21)
   185   ├── G18: (cast G22 STRING)
   186   ├── G19: (variable column5)
   187   ├── G20: (variable b.x)
   188   ├── G21: (gt G11 G12)
   189   └── G22: (variable a.x)
   190  
   191  # Test interning of expressions.
   192  memo
   193  SELECT 1 AS a, 1+z AS b, left(x, 10)::TIMESTAMP AS c, left(x, 10)::TIMESTAMPTZ AS d
   194  FROM b
   195  WHERE z=1 AND concat(x, 'foo', x)=concat(x, 'foo', x)
   196  ----
   197  memo (optimized, ~4KB, required=[presentation: a:3,b:4,c:5,d:6])
   198   ├── G1: (project G2 G3)
   199   │    └── [presentation: a:3,b:4,c:5,d:6]
   200   │         ├── best: (project G2 G3)
   201   │         └── cost: 1050.21
   202   ├── G2: (select G4 G5)
   203   │    └── []
   204   │         ├── best: (select G4 G5)
   205   │         └── cost: 1050.03
   206   ├── G3: (projections G6 G7 G8 G9)
   207   ├── G4: (scan b)
   208   │    └── []
   209   │         ├── best: (scan b)
   210   │         └── cost: 1040.02
   211   ├── G5: (filters G10 G11)
   212   ├── G6: (const 1)
   213   ├── G7: (plus G12 G13)
   214   ├── G8: (cast G14 TIMESTAMP)
   215   ├── G9: (cast G14 TIMESTAMPTZ)
   216   ├── G10: (eq G12 G13)
   217   ├── G11: (eq G15 G15)
   218   ├── G12: (variable z)
   219   ├── G13: (const 1)
   220   ├── G14: (function G16 left)
   221   ├── G15: (function G17 concat)
   222   ├── G16: (scalar-list G18 G19)
   223   ├── G17: (scalar-list G18 G20 G18)
   224   ├── G18: (variable x)
   225   ├── G19: (const 10)
   226   └── G20: (const 'foo')
   227  
   228  # Test topological sorting
   229  memo
   230  SELECT x FROM a WHERE x = 1 AND x+y = 1
   231  ----
   232  memo (optimized, ~5KB, required=[presentation: x:1])
   233   ├── G1: (project G2 G3 x)
   234   │    └── [presentation: x:1]
   235   │         ├── best: (project G2 G3 x)
   236   │         └── cost: 1.09
   237   ├── G2: (select G4 G5) (select G6 G7)
   238   │    └── []
   239   │         ├── best: (select G6 G7)
   240   │         └── cost: 1.07
   241   ├── G3: (projections)
   242   ├── G4: (scan a)
   243   │    └── []
   244   │         ├── best: (scan a)
   245   │         └── cost: 1040.02
   246   ├── G5: (filters G8 G9)
   247   ├── G6: (scan a,constrained)
   248   │    └── []
   249   │         ├── best: (scan a,constrained)
   250   │         └── cost: 1.05
   251   ├── G7: (filters G9)
   252   ├── G8: (eq G10 G11)
   253   ├── G9: (eq G12 G13)
   254   ├── G10: (variable x)
   255   ├── G11: (const 1)
   256   ├── G12: (variable y)
   257   └── G13: (const 0)
   258  
   259  memo 
   260  SELECT x, y FROM a UNION SELECT x+1, y+1 FROM a
   261  ----
   262  memo (optimized, ~4KB, required=[presentation: x:7,y:8])
   263   ├── G1: (union G2 G3)
   264   │    └── [presentation: x:7,y:8]
   265   │         ├── best: (union G2 G3)
   266   │         └── cost: 2150.06
   267   ├── G2: (scan a)
   268   │    └── []
   269   │         ├── best: (scan a)
   270   │         └── cost: 1040.02
   271   ├── G3: (project G4 G5)
   272   │    └── []
   273   │         ├── best: (project G4 G5)
   274   │         └── cost: 1070.03
   275   ├── G4: (scan a)
   276   │    └── []
   277   │         ├── best: (scan a)
   278   │         └── cost: 1040.02
   279   ├── G5: (projections G6 G7)
   280   ├── G6: (plus G8 G9)
   281   ├── G7: (plus G10 G9)
   282   ├── G8: (variable a.x)
   283   ├── G9: (const 1)
   284   └── G10: (variable a.y)
   285  
   286  memo
   287  SELECT array_agg(x) FROM (SELECT * FROM a)
   288  ----
   289  memo (optimized, ~3KB, required=[presentation: array_agg:3])
   290   ├── G1: (scalar-group-by G2 G3 cols=())
   291   │    └── [presentation: array_agg:3]
   292   │         ├── best: (scalar-group-by G2 G3 cols=())
   293   │         └── cost: 1040.05
   294   ├── G2: (scan a,cols=(1))
   295   │    └── []
   296   │         ├── best: (scan a,cols=(1))
   297   │         └── cost: 1030.02
   298   ├── G3: (aggregations G4)
   299   ├── G4: (array-agg G5)
   300   └── G5: (variable x)
   301  
   302  memo
   303  SELECT array_agg(x) FROM (SELECT * FROM a) GROUP BY y
   304  ----
   305  memo (optimized, ~3KB, required=[presentation: array_agg:3])
   306   ├── G1: (project G2 G3 array_agg)
   307   │    └── [presentation: array_agg:3]
   308   │         ├── best: (project G2 G3 array_agg)
   309   │         └── cost: 1072.05
   310   ├── G2: (group-by G4 G5 cols=(2))
   311   │    └── []
   312   │         ├── best: (group-by G4 G5 cols=(2))
   313   │         └── cost: 1071.04
   314   ├── G3: (projections)
   315   ├── G4: (scan a)
   316   │    └── []
   317   │         ├── best: (scan a)
   318   │         └── cost: 1040.02
   319   ├── G5: (aggregations G6)
   320   ├── G6: (array-agg G7)
   321   └── G7: (variable x)
   322  
   323  memo
   324  SELECT array_agg(x) FROM (SELECT * FROM a ORDER BY y)
   325  ----
   326  memo (optimized, ~2KB, required=[presentation: array_agg:3])
   327   ├── G1: (scalar-group-by G2 G3 cols=(),ordering=+2)
   328   │    └── [presentation: array_agg:3]
   329   │         ├── best: (scalar-group-by G2="[ordering: +2]" G3 cols=(),ordering=+2)
   330   │         └── cost: 1269.38
   331   ├── G2: (scan a)
   332   │    ├── [ordering: +2]
   333   │    │    ├── best: (sort G2)
   334   │    │    └── cost: 1259.35
   335   │    └── []
   336   │         ├── best: (scan a)
   337   │         └── cost: 1040.02
   338   ├── G3: (aggregations G4)
   339   ├── G4: (array-agg G5)
   340   └── G5: (variable x)
   341  
   342  memo
   343  SELECT DISTINCT field FROM [EXPLAIN SELECT 123 AS k]
   344  ----
   345  memo (optimized, ~9KB, required=[presentation: field:6])
   346   ├── G1: (distinct-on G2 G3 cols=(6))
   347   │    └── [presentation: field:6]
   348   │         ├── best: (distinct-on G2 G3 cols=(6))
   349   │         └── cost: 0.56
   350   ├── G2: (project G4 G5)
   351   │    └── []
   352   │         ├── best: (project G4 G5)
   353   │         └── cost: 0.24
   354   ├── G3: (aggregations)
   355   ├── G4: (explain G6 [presentation: k:1])
   356   │    └── []
   357   │         ├── best: (explain G6="[presentation: k:1]" [presentation: k:1])
   358   │         └── cost: 0.03
   359   ├── G5: (projections G7)
   360   ├── G6: (values G8 id=v1)
   361   │    └── [presentation: k:1]
   362   │         ├── best: (values G8 id=v1)
   363   │         └── cost: 0.02
   364   ├── G7: (variable field)
   365   ├── G8: (scalar-list G9)
   366   ├── G9: (tuple G10)
   367   ├── G10: (scalar-list G11)
   368   └── G11: (const 123)
   369  
   370  memo
   371  SELECT DISTINCT tag FROM [SHOW TRACE FOR SESSION]
   372  ----
   373  memo (optimized, ~7KB, required=[presentation: tag:11])
   374   ├── G1: (distinct-on G2 G3 cols=(11))
   375   │    └── [presentation: tag:11]
   376   │         ├── best: (distinct-on G2 G3 cols=(11))
   377   │         └── cost: 0.54
   378   ├── G2: (project G4 G5)
   379   │    └── []
   380   │         ├── best: (project G4 G5)
   381   │         └── cost: 0.22
   382   ├── G3: (aggregations)
   383   ├── G4: (show-trace-for-session &{TRACE false [1 2 3 4 5 6 7]})
   384   │    └── []
   385   │         ├── best: (show-trace-for-session &{TRACE false [1 2 3 4 5 6 7]})
   386   │         └── cost: 0.01
   387   ├── G5: (projections G6)
   388   └── G6: (variable tag)