exec-ddl
CREATE TABLE abc (
    a INT NOT NULL,
    b TEXT DEFAULT ('foo'),
    c FLOAT AS (a::float) STORED
)
----

exec-ddl
ALTER TABLE abc INJECT STATISTICS '[
  {
    "columns": ["a"],
    "created_at": "2018-01-01 1:00:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 2000
  },
  {
    "columns": ["b"],
    "created_at": "2018-01-01 1:30:00.00000+00:00",
    "row_count": 2000,
    "distinct_count": 10
  }
]'
----

exec-ddl
CREATE TABLE xyz (
    x TEXT PRIMARY KEY,
    y INT8 NOT NULL,
    z FLOAT8
)
----

# Statistics should be derived from INSERT input columns and transferred to
# RETURNING columns.
build
SELECT *
FROM [INSERT INTO xyz (x, y, z) SELECT b, a, c FROM abc WHERE b='foo' RETURNING *]
WHERE z > 1.0
----
with &1
 ├── columns: x:12(string!null) y:13(int!null) z:14(float!null)
 ├── volatile, mutations
 ├── stats: [rows=66.51058, distinct(14)=43.4214, null(14)=0]
 ├── fd: ()-->(12), (13)-->(14)
 ├── insert xyz
 │    ├── columns: xyz.x:1(string!null) xyz.y:2(int!null) xyz.z:3(float)
 │    ├── insert-mapping:
 │    │    ├── b:7 => xyz.x:1
 │    │    ├── a:6 => xyz.y:2
 │    │    └── c:8 => xyz.z:3
 │    ├── return-mapping:
 │    │    ├── b:7 => xyz.x:1
 │    │    ├── a:6 => xyz.y:2
 │    │    └── c:8 => xyz.z:3
 │    ├── volatile, mutations
 │    ├── stats: [rows=200, distinct(1)=1, null(1)=0, distinct(2)=200, null(2)=0, distinct(3)=130.264, null(3)=2]
 │    ├── fd: ()-->(1), (2)-->(3)
 │    └── project
 │         ├── columns: a:6(int!null) b:7(string!null) c:8(float)
 │         ├── stats: [rows=200, distinct(6)=200, null(6)=0, distinct(7)=1, null(7)=0, distinct(8)=130.264, null(8)=2]
 │         ├── fd: ()-->(7), (6)-->(8)
 │         └── select
 │              ├── columns: a:6(int!null) b:7(string!null) c:8(float) rowid:9(int!null) abc.crdb_internal_mvcc_timestamp:10(decimal) abc.tableoid:11(oid)
 │              ├── stats: [rows=200, distinct(6)=200, null(6)=0, distinct(7)=1, null(7)=0, distinct(8)=130.264, null(8)=2]
 │              ├── key: (9)
 │              ├── fd: ()-->(7), (9)-->(6,8,10,11), (6)-->(8)
 │              ├── scan abc
 │              │    ├── columns: a:6(int!null) b:7(string) c:8(float) rowid:9(int!null) abc.crdb_internal_mvcc_timestamp:10(decimal) abc.tableoid:11(oid)
 │              │    ├── computed column expressions
 │              │    │    └── c:8
 │              │    │         └── a:6::FLOAT8 [type=float]
 │              │    ├── stats: [rows=2000, distinct(6)=2000, null(6)=0, distinct(7)=10, null(7)=0, distinct(8)=200, null(8)=20, distinct(9)=2000, null(9)=0]
 │              │    ├── key: (9)
 │              │    └── fd: (9)-->(6-8,10,11), (6)-->(8)
 │              └── filters
 │                   └── b:7 = 'foo' [type=bool, outer=(7), constraints=(/7: [/'foo' - /'foo']; tight), fd=()-->(7)]
 └── select
      ├── columns: x:12(string!null) y:13(int!null) z:14(float!null)
      ├── stats: [rows=66.51058, distinct(14)=43.4214, null(14)=0]
      ├── fd: ()-->(12), (13)-->(14)
      ├── with-scan &1
      │    ├── columns: x:12(string!null) y:13(int!null) z:14(float)
      │    ├── mapping:
      │    │    ├──  xyz.x:1(string) => x:12(string)
      │    │    ├──  xyz.y:2(int) => y:13(int)
      │    │    └──  xyz.z:3(float) => z:14(float)
      │    ├── stats: [rows=200, distinct(12)=1, null(12)=0, distinct(13)=200, null(13)=0, distinct(14)=130.264, null(14)=2]
      │    └── fd: ()-->(12), (13)-->(14)
      └── filters
           └── z:14 > 1.0 [type=bool, outer=(14), constraints=(/14: [/1.0000000000000002 - ]; tight)]

# Cardinality is zero.
build
INSERT INTO xyz (x, y, z) SELECT b, a, c FROM abc WHERE False RETURNING *
----
insert xyz
 ├── columns: x:1(string!null) y:2(int!null) z:3(float)
 ├── insert-mapping:
 │    ├── b:7 => x:1
 │    ├── a:6 => y:2
 │    └── c:8 => z:3
 ├── return-mapping:
 │    ├── b:7 => x:1
 │    ├── a:6 => y:2
 │    └── c:8 => z:3
 ├── cardinality: [0 - 0]
 ├── volatile, mutations
 ├── stats: [rows=0]
 ├── fd: (2)-->(3)
 └── project
      ├── columns: a:6(int!null) b:7(string) c:8(float)
      ├── cardinality: [0 - 0]
      ├── stats: [rows=0]
      ├── fd: (6)-->(8)
      └── select
           ├── columns: a:6(int!null) b:7(string) c:8(float) rowid:9(int!null) abc.crdb_internal_mvcc_timestamp:10(decimal) abc.tableoid:11(oid)
           ├── cardinality: [0 - 0]
           ├── stats: [rows=0]
           ├── key: (9)
           ├── fd: (9)-->(6-8,10,11), (6)-->(8)
           ├── scan abc
           │    ├── columns: a:6(int!null) b:7(string) c:8(float) rowid:9(int!null) abc.crdb_internal_mvcc_timestamp:10(decimal) abc.tableoid:11(oid)
           │    ├── computed column expressions
           │    │    └── c:8
           │    │         └── a:6::FLOAT8 [type=float]
           │    ├── stats: [rows=2000]
           │    ├── key: (9)
           │    └── fd: (9)-->(6-8,10,11), (6)-->(8)
           └── filters
                └── false [type=bool, constraints=(contradiction; tight)]
