exec
CREATE TABLE t.orders (oid INT PRIMARY KEY, cid INT, value DECIMAL, date DATE)
----

# In the string version, the constants are not anonymized.
plan-string
SELECT oid FROM t.orders WHERE oid = 123
----
0 scan  (oid int) 
0 .table orders@primary (oid int) 
0 .spans /123-/123/# (oid int) 

plan-tree
SELECT oid FROM t.orders WHERE oid = 123
----
name: scan
attrs:
- key: table
  value: orders@primary
- key: spans
  value: 1 span
children: []

plan-string
SELECT cid, date, value FROM t.orders
----
0 render  (cid int, date date, value decimal) 
0 .render 0 (@1)[int] (cid int, date date, value decimal) 
0 .render 1 (@3)[date] (cid int, date date, value decimal) 
0 .render 2 (@2)[decimal] (cid int, date date, value decimal) 
1 scan  (cid int, date date, value decimal) 
1 .table orders@primary (cid int, date date, value decimal) 
1 .spans ALL (cid int, date date, value decimal) 

plan-tree
SELECT cid, date, value FROM t.orders
----
name: render
attrs:
- key: render
  value: cid
- key: render
  value: date
- key: render
  value: value
children:
- name: scan
  attrs:
  - key: table
    value: orders@primary
  - key: spans
    value: ALL
  children: []

plan-string
SELECT cid, sum(value) FROM t.orders WHERE date > '2015-01-01' GROUP BY cid ORDER BY 1 - sum(value)
----
0 render  (cid int, sum decimal) 
0 .render 0 (@2)[int] (cid int, sum decimal) 
0 .render 1 (@3)[decimal] (cid int, sum decimal) 
1 sort  (cid int, sum decimal) 
1 .order +column6 (cid int, sum decimal) 
2 render  (cid int, sum decimal) 
2 .render 0 ((1)[decimal] - (@2)[decimal])[decimal] (cid int, sum decimal) 
2 .render 1 (@1)[int] (cid int, sum decimal) 
2 .render 2 (@2)[decimal] (cid int, sum decimal) 
3 group  (cid int, sum decimal) 
3 .aggregate 0 cid (cid int, sum decimal) 
3 .aggregate 1 sum(value) (cid int, sum decimal) 
3 .group by cid (cid int, sum decimal) 
4 render  (cid int, sum decimal) 
4 .render 0 (@1)[int] (cid int, sum decimal) 
4 .render 1 (@2)[decimal] (cid int, sum decimal) 
5 scan  (cid int, sum decimal) 
5 .table orders@primary (cid int, sum decimal) 
5 .spans ALL (cid int, sum decimal) 
5 .filter ((@3)[date] > ('2015-01-01')[date])[bool] (cid int, sum decimal) 

plan-tree
SELECT cid, sum(value) FROM t.orders WHERE date > '2015-01-01' GROUP BY cid ORDER BY 1 - sum(value)
----
name: render
attrs:
- key: render
  value: cid
- key: render
  value: sum
children:
- name: sort
  attrs:
  - key: order
    value: +column6
  children:
  - name: render
    attrs:
    - key: render
      value: _ - sum
    - key: render
      value: cid
    - key: render
      value: sum
    children:
    - name: group
      attrs:
      - key: aggregate 0
        value: cid
      - key: aggregate 1
        value: sum(value)
      - key: group by
        value: cid
      children:
      - name: render
        attrs:
        - key: render
          value: cid
        - key: render
          value: value
        children:
        - name: scan
          attrs:
          - key: table
            value: orders@primary
          - key: spans
            value: ALL
          - key: filter
            value: date > _
          children: []

plan-string
SELECT value FROM (SELECT cid, date, value FROM t.orders)
----
0 scan  (value decimal) 
0 .table orders@primary (value decimal) 
0 .spans ALL (value decimal) 

plan-tree
SELECT value FROM (SELECT cid, date, value FROM t.orders)
----
name: scan
attrs:
- key: table
  value: orders@primary
- key: spans
  value: ALL
children: []

plan-string
SELECT cid, date, value FROM t.orders WHERE date IN (SELECT date FROM t.orders)
----
0 render  (cid int, date date, value decimal) 
0 .render 0 (@1)[int] (cid int, date date, value decimal) 
0 .render 1 (@3)[date] (cid int, date date, value decimal) 
0 .render 2 (@2)[decimal] (cid int, date date, value decimal) 
1 hash-join  (cid int, date date, value decimal) 
1 .type inner (cid int, date date, value decimal) 
1 .equality (date) = (date) (cid int, date date, value decimal) 
1 .right cols are key  (cid int, date date, value decimal) 
2 scan  (cid int, date date, value decimal) 
2 .table orders@primary (cid int, date date, value decimal) 
2 .spans ALL (cid int, date date, value decimal) 
2 distinct  (cid int, date date, value decimal) 
2 .distinct on date (cid int, date date, value decimal) 
3 scan  (cid int, date date, value decimal) 
3 .table orders@primary (cid int, date date, value decimal) 
3 .spans ALL (cid int, date date, value decimal) 

plan-tree
SELECT cid, date, value FROM t.orders WHERE date IN (SELECT date FROM t.orders)
----
name: render
attrs:
- key: render
  value: cid
- key: render
  value: date
- key: render
  value: value
children:
- name: hash-join
  attrs:
  - key: type
    value: inner
  - key: equality
    value: (date) = (date)
  - key: right cols are key
    value: ""
  children:
  - name: scan
    attrs:
    - key: table
      value: orders@primary
    - key: spans
      value: ALL
    children: []
  - name: distinct
    attrs:
    - key: distinct on
      value: date
    children:
    - name: scan
      attrs:
      - key: table
        value: orders@primary
      - key: spans
        value: ALL
      children: []

exec
CREATE TABLE t.movies (
  id SERIAL PRIMARY KEY,
  title TEXT,
  released INT
)
----

exec
CREATE TABLE t.actors (
  id SERIAL PRIMARY KEY,
  name TEXT
)
----

# Subquery.
plan-string
SELECT id AS movie_id, title, (SELECT name FROM t.actors WHERE name = 'Foo') FROM t.movies
----
0 root  (movie_id int, title string, name string) 
1 render  (movie_id int, title string, name string) 
1 .render 0 (@1)[int] (movie_id int, title string, name string) 
1 .render 1 (@2)[string] (movie_id int, title string, name string) 
1 .render 2 (@S1)[string] (movie_id int, title string, name string) 
2 scan  (movie_id int, title string, name string) 
2 .table movies@primary (movie_id int, title string, name string) 
2 .spans ALL (movie_id int, title string, name string) 
1 subquery  (movie_id int, title string, name string) 
1 .id @S1 (movie_id int, title string, name string) 
1 .original sql (SELECT name FROM t.actors WHERE name = 'Foo') (movie_id int, title string, name string) 
1 .exec mode one row (movie_id int, title string, name string) 
2 max1row  (movie_id int, title string, name string) 
3 scan  (movie_id int, title string, name string) 
3 .table actors@primary (movie_id int, title string, name string) 
3 .spans ALL (movie_id int, title string, name string) 
3 .filter ((@1)[string] = ('Foo')[string])[bool] (movie_id int, title string, name string) 

plan-tree
SELECT id AS movie_id, title, (SELECT name FROM t.actors WHERE name = 'Foo') FROM t.movies
----
name: root
attrs: []
children:
- name: render
  attrs:
  - key: render
    value: id
  - key: render
    value: title
  - key: render
    value: (SELECT name FROM t.actors WHERE name = _)
  children:
  - name: scan
    attrs:
    - key: table
      value: movies@primary
    - key: spans
      value: ALL
    children: []
- name: subquery
  attrs:
  - key: id
    value: '@S1'
  - key: original sql
    value: (SELECT name FROM t.actors WHERE name = _)
  - key: exec mode
    value: one row
  children:
  - name: max1row
    attrs: []
    children:
    - name: scan
      attrs:
      - key: table
        value: actors@primary
      - key: spans
        value: ALL
      - key: filter
        value: name = _
      children: []
