# =============================================================================
# This schema is based on a business buying/selling online trading cards. This
# file simulates queries taking place while schema changes are *not* taking
# place. See the trading-mutation file for the same queries running against
# tables with simulated schema changes in progress.
# =============================================================================

# --------------------------------------------------
# Schema Definitions
# --------------------------------------------------

# Cards is the catalog of all cards that can be traded.
exec-ddl
CREATE TABLE Cards
(
  id INT NOT NULL,
  name VARCHAR(128) NOT NULL,
  rarity VARCHAR(1) NULL,
  setname VARCHAR(5) NULL,
  number INT NOT NULL,
  isfoil BIT NOT NULL,
  CONSTRAINT CardsPrimaryKey PRIMARY KEY
  (
    id ASC
  ),
  CONSTRAINT CardsNameSetNumber UNIQUE
  (
    name ASC,
    setname ASC,
    number ASC
  )
)
----

exec-ddl
ALTER TABLE Cards INJECT STATISTICS '[
  {
    "columns": ["id"],
    "distinct_count": 57000,
    "null_count": 0,
    "row_count": 57000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["name"],
    "distinct_count": 39000,
    "null_count": 0,
    "row_count": 57000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["setname"],
    "distinct_count": 162,
    "null_count": 0,
    "row_count": 57000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["number"],
    "distinct_count": 829,
    "null_count": 0,
    "row_count": 57000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["name", "setname"],
    "distinct_count": 56700,
    "null_count": 0,
    "row_count": 57000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["name", "setname", "number"],
    "distinct_count": 57000,
    "null_count": 0,
    "row_count": 57000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  }
]'
----

# CardsInfo tracks current inventory of each card, as well as current buy/sell
# price. It is partitioned on dealerid, which represents multiple licensees
# (dealers) using the trading software.
exec-ddl
CREATE TABLE CardsInfo
(
  dealerid OID NOT NULL,
  cardid INT NOT NULL,
  buyprice DECIMAL(10,4) NOT NULL,
  sellprice DECIMAL(10,4) NOT NULL,
  discount DECIMAL(10,4) NOT NULL,
  desiredinventory INT NOT NULL,
  actualinventory INT NOT NULL,
  maxinventory INT NOT NULL,
  version DECIMAL NOT NULL DEFAULT (cluster_logical_timestamp()),
  CONSTRAINT CardsInfoPrimaryKey PRIMARY KEY
  (
    dealerid ASC,
    cardid ASC
  ),
  CONSTRAINT CardsInfoCardIdKey FOREIGN KEY (cardid)
  REFERENCES Cards (id),
  UNIQUE INDEX CardsInfoVersionIndex (dealerid ASC, version ASC)
)
----

exec-ddl
ALTER TABLE CardsInfo INJECT STATISTICS '[
  {
    "columns": ["dealerid"],
    "distinct_count": 12,
    "null_count": 0,
    "row_count": 700000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["cardid"],
    "distinct_count": 57000,
    "null_count": 0,
    "row_count": 700000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["version"],
    "distinct_count": 700000,
    "null_count": 0,
    "row_count": 700000,
    "created_at": "2020-01-01 0:00:00.00000+00:00",
    "histo_col_type": "decimal",
    "histo_buckets": [
      {"num_eq": 0, "num_range": 0, "distinct_range": 0, "upper_bound": "1426741777604892000"},
      {"num_eq": 0, "num_range": 350000, "distinct_range": 350000, "upper_bound": "1584421693935036000"}
    ]
  },
  {
    "columns": ["dealerid", "cardid"],
    "distinct_count": 700000,
    "null_count": 0,
    "row_count": 700000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["dealerid", "version"],
    "distinct_count": 700000,
    "null_count": 0,
    "row_count": 700000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  }
]'
----

# InventoryDetails stores the quantity and location of all the cards.
exec-ddl
CREATE TABLE InventoryDetails
(
  dealerid OID NOT NULL,
  cardid INT NOT NULL,
  accountname VARCHAR(128) NOT NULL,
  quantity INT NOT NULL,
  version DECIMAL NOT NULL DEFAULT (cluster_logical_timestamp()),
  CONSTRAINT InventoryDetailsPrimaryKey PRIMARY KEY
  (
    dealerid ASC,
    cardid ASC,
    accountname ASC
  ),
  CONSTRAINT InventoryDetailsCardIdKey FOREIGN KEY (cardid)
  REFERENCES Cards (id)
)
----

exec-ddl
ALTER TABLE InventoryDetails INJECT STATISTICS '[
  {
    "columns": ["dealerid"],
    "distinct_count": 12,
    "null_count": 0,
    "row_count": 1700000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["cardid"],
    "distinct_count": 50000,
    "null_count": 0,
    "row_count": 1700000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["accountname"],
    "distinct_count": 150,
    "null_count": 0,
    "row_count": 1700000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["dealerid", "cardid"],
    "distinct_count": 250000,
    "null_count": 0,
    "row_count": 1700000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["dealerid", "cardid", "accountname"],
    "distinct_count": 170000,
    "null_count": 0,
    "row_count": 1700000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  }
]'
----

# Transactions records all buy/sell trading activity.
#
# NOTE: The TransactionsOpIndex is meant to be a partial index, only containing
#       non-NULL values. The operationid column is for idempotency, and
#       older values get set to NULL after X hours to save space. 99%+ of values
#       are NULL.
exec-ddl
CREATE TABLE Transactions
(
  dealerid OID NOT NULL,
  isbuy BOOL NOT NULL,
  date TIMESTAMPTZ NOT NULL,
  accountname VARCHAR(128) NOT NULL,
  customername VARCHAR(128) NOT NULL,
  operationid UUID,
  version DECIMAL NOT NULL DEFAULT (cluster_logical_timestamp()),
  CONSTRAINT TransactionsPrimaryKey PRIMARY KEY
  (
    dealerid ASC,
    isbuy ASC,
    date ASC
  ),
  UNIQUE INDEX TransactionsOpIndex (dealerid ASC, operationid ASC)
  --WHERE operationid IS NOT NULL
)
----

exec-ddl
ALTER TABLE Transactions INJECT STATISTICS '[
  {
    "columns": ["dealerid"],
    "distinct_count": 10,
    "null_count": 0,
    "row_count": 20000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["isbuy"],
    "distinct_count": 2,
    "null_count": 0,
    "row_count": 20000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["date"],
    "distinct_count": 20000000,
    "null_count": 0,
    "row_count": 20000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["operationid"],
    "distinct_count": 4000,
    "null_count": 19996000,
    "row_count": 20000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["dealerid", "isbuy"],
    "distinct_count": 15,
    "null_count": 0,
    "row_count": 20000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["dealerid", "isbuy", "date"],
    "distinct_count": 20000000,
    "null_count": 0,
    "row_count": 20000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  }
]'
----

# TransactionDetails records line items of each Transaction.
exec-ddl
CREATE TABLE TransactionDetails
(
  dealerid OID NOT NULL,
  isbuy BOOL NOT NULL,
  transactiondate TIMESTAMPTZ NOT NULL,
  cardid INT NOT NULL,
  quantity INT NOT NULL,
  sellprice DECIMAL(10,4) NOT NULL,
  buyprice DECIMAL(10,4) NOT NULL,
  version DECIMAL NOT NULL DEFAULT (cluster_logical_timestamp()),
  CONSTRAINT DetailsPrimaryKey PRIMARY KEY
  (
    dealerid ASC,
    isbuy ASC,
    transactiondate ASC,
    cardid ASC,
    quantity ASC
  ),
  CONSTRAINT DetailsDealerDateKey FOREIGN KEY (dealerid, isbuy, transactiondate)
  REFERENCES Transactions (dealerid, isbuy, date),
  CONSTRAINT DetailsCardIdKey FOREIGN KEY (cardid)
  REFERENCES Cards (id),
  INDEX DetailsCardIdIndex (dealerid ASC, isbuy ASC, cardid ASC)
)
----

exec-ddl
ALTER TABLE TransactionDetails INJECT STATISTICS '[
  {
    "columns": ["dealerid"],
    "distinct_count": 10,
    "null_count": 0,
    "row_count": 180000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["isbuy"],
    "distinct_count": 2,
    "null_count": 0,
    "row_count": 180000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["transactiondate"],
    "distinct_count": 180000000,
    "null_count": 0,
    "row_count": 180000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["cardid"],
    "distinct_count": 57000,
    "null_count": 0,
    "row_count": 180000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["dealerid", "isbuy"],
    "distinct_count": 15,
    "null_count": 0,
    "row_count": 180000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["dealerid", "isbuy", "transactiondate"],
    "distinct_count": 20000000,
    "null_count": 0,
    "row_count": 180000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["dealerid", "isbuy", "transactiondate", "cardid"],
    "distinct_count": 180000000,
    "null_count": 0,
    "row_count": 180000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["dealerid", "isbuy", "transactiondate", "cardid", "quantity"],
    "distinct_count": 180000000,
    "null_count": 0,
    "row_count": 180000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["dealerid", "isbuy", "cardid"],
    "distinct_count": 350000,
    "null_count": 0,
    "row_count": 180000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  }
]'
----

# PriceDetails records price history for each card.
exec-ddl
CREATE TABLE PriceDetails
(
    dealerid OID NOT NULL,
    cardid INT NOT NULL,
    pricedate TIMESTAMPTZ NOT NULL,
    pricedby VARCHAR(128) NOT NULL,
    buyprice DECIMAL(10,4) NOT NULL,
    sellprice DECIMAL(10,4) NOT NULL,
    discount DECIMAL(10,4) NOT NULL,
    version DECIMAL NOT NULL,
    CONSTRAINT PriceDetailsPrimaryKey PRIMARY KEY
    (
        dealerid ASC,
        cardid ASC,
        pricedate ASC
    ),
    CONSTRAINT PriceDetailsCardIdKey FOREIGN KEY (cardid)
    REFERENCES Cards (id)
)
----

exec-ddl
ALTER TABLE PriceDetails INJECT STATISTICS '[
  {
    "columns": ["dealerid"],
    "distinct_count": 2,
    "null_count": 0,
    "row_count": 40000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["cardid"],
    "distinct_count": 57000,
    "null_count": 0,
    "row_count": 40000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["pricedate"],
    "distinct_count": 40000000,
    "null_count": 0,
    "row_count": 40000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["dealerid", "cardid"],
    "distinct_count": 90000,
    "null_count": 0,
    "row_count": 40000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  },
  {
    "columns": ["dealerid", "cardid", "pricedate"],
    "distinct_count": 40000000,
    "null_count": 0,
    "row_count": 40000000,
    "created_at": "2020-01-01 0:00:00.00000+00:00"
  }
]'
----

# NOTE: These views should not be checking for the constant dealerid = 1.
# Instead, the dealerid should be derived from the current user, like this:
#
#   dealerid = (SELECT oid FROM pg_roles WHERE rolname = current_user);
#
# However, the optimizer and execution engine are not smart enough to do a good
# job with this. The following needs to be done:
#
#   1. Optimizer needs access to key information about pg_roles so that it can
#      infer that there will be at most one row that matches the predicate.
#   2. Optimizer needs to replace "current_user" with constant after the query
#      is prepared, as if it were a parameter.
#   3. Execution engine should allow "push down" into virtual tables, or else
#      this query will need to enumerate every user and role in order to find
#      the requested rolname.
#

exec-ddl
CREATE VIEW CardsView AS
    SELECT  id AS Id, name AS Name, rarity AS Rarity, setname AS SetName, number AS Number, isfoil AS IsFoil,
            buyprice AS BuyPrice, sellprice AS SellPrice, discount AS Discount,
            desiredinventory AS DesiredInventory, actualinventory AS ActualInventory,
            maxinventory AS MaxInventory, version AS Version
    FROM Cards
    JOIN CardsInfo
    ON id = cardid
    WHERE dealerid = 1
----

exec-ddl
CREATE VIEW TransactionsView AS
    SELECT
      date AS Date, accountname AS AccountName, customername AS CustomerName,
      isbuy AS IsBuy, operationid AS OperationId
    FROM Transactions
    WHERE dealerid = 1
----

exec-ddl
CREATE VIEW TransactionDetailsView AS
    SELECT  isbuy AS IsBuy, transactiondate AS TransactionDate, cardid AS CardId, quantity AS Quantity,
            sellprice AS SellPrice, buyprice AS BuyPrice
    FROM TransactionDetails
    WHERE dealerid = 1
----

exec-ddl
CREATE VIEW PriceDetailsView AS
    SELECT  cardid AS CardId, pricedate AS PriceDate, pricedby AS PricedBy, buyprice AS BuyPrice,
            sellprice AS SellPrice,
            (
                CASE
                    WHEN dealerid <> 1
                    THEN 0::DECIMAL(10,4)
                    ELSE discount
                END
            ) AS Discount
    FROM PriceDetails
    WHERE dealerid = 1
----

exec-ddl
CREATE VIEW GlobalInventoryView AS
    SELECT cardid, min(buyprice) AS BuyPrice, max(sellprice) AS SellPrice, max(discount) AS Discount,
        sum(desiredinventory) AS DesiredInventory,
        sum
        (
            CASE
                WHEN dealerid = 2 AND actualinventory > 24 THEN 24
                WHEN dealerid <> 1 AND actualinventory > maxinventory THEN maxinventory
                ELSE actualinventory
            END
        ) AS ActualInventory,
        sum(maxinventory) AS MaxInventory,
        max(version) AS Version
    FROM CardsInfo
    INNER JOIN Cards
    ON cardid = id
    WHERE (dealerid = 1 OR dealerid = 2 OR dealerid = 3 OR dealerid = 4)
    GROUP BY cardid
----

exec-ddl
CREATE VIEW GlobalCardsView AS
    SELECT c.id AS Id, c.name AS Name, c.rarity AS Rarity, c.setname AS SetName, c.number AS Number, c.isfoil AS IsFoil,
            inv.BuyPrice, inv.SellPrice, inv.Discount, inv.DesiredInventory, inv.ActualInventory,
            inv.MaxInventory, inv.Version
    FROM Cards c
    INNER JOIN GlobalInventoryView inv
    ON c.id = inv.cardid
----

# --------------------------------------------------
# SELECT Queries
# --------------------------------------------------

# Find all cards that have been modified in the last 5 seconds.
#
# Problems:
#   1. The wrong index is selected because of mismatched row estimates. The
#      CardsInfoVersionIndex should be used instead.
#
opt
SELECT
  Id, Name, Rarity, SetName, Number, IsFoil, BuyPrice, SellPrice,
  DesiredInventory, ActualInventory, Version, Discount, MaxInventory
FROM CardsView WHERE Version > 1584421773604892000.0000000000
----
project
 ├── columns: id:1(int!null) name:2(varchar!null) rarity:3(varchar) setname:4(varchar) number:5(int!null) isfoil:6(bit!null) buyprice:9(decimal!null) sellprice:10(decimal!null) desiredinventory:12(int!null) actualinventory:13(int!null) version:15(decimal!null) discount:11(decimal!null) maxinventory:14(int!null)
 ├── key: (15)
 ├── fd: (1)-->(2-6,9-15), (2,4,5)~~>(1,3,6), (15)-->(1-6,9-14)
 └── inner-join (lookup cards)
      ├── columns: id:1(int!null) name:2(varchar!null) rarity:3(varchar) setname:4(varchar) number:5(int!null) isfoil:6(bit!null) dealerid:7(oid!null) cardid:8(int!null) buyprice:9(decimal!null) sellprice:10(decimal!null) discount:11(decimal!null) desiredinventory:12(int!null) actualinventory:13(int!null) maxinventory:14(int!null) version:15(decimal!null)
      ├── key columns: [8] = [1]
      ├── lookup columns are key
      ├── key: (8)
      ├── fd: ()-->(7), (1)-->(2-6), (2,4,5)~~>(1,3,6), (8)-->(9-15), (15)-->(8-14), (1)==(8), (8)==(1)
      ├── select
      │    ├── columns: dealerid:7(oid!null) cardid:8(int!null) buyprice:9(decimal!null) sellprice:10(decimal!null) discount:11(decimal!null) desiredinventory:12(int!null) actualinventory:13(int!null) maxinventory:14(int!null) version:15(decimal!null)
      │    ├── key: (8)
      │    ├── fd: ()-->(7), (8)-->(9-15), (15)-->(8-14)
      │    ├── scan cardsinfo
      │    │    ├── columns: dealerid:7(oid!null) cardid:8(int!null) buyprice:9(decimal!null) sellprice:10(decimal!null) discount:11(decimal!null) desiredinventory:12(int!null) actualinventory:13(int!null) maxinventory:14(int!null) version:15(decimal!null)
      │    │    ├── constraint: /7/8: [/1 - /1]
      │    │    ├── key: (8)
      │    │    └── fd: ()-->(7), (8)-->(9-15), (15)-->(8-14)
      │    └── filters
      │         └── version > 1584421773604892000.0000000000 [type=bool, outer=(15), constraints=(/15: (/1584421773604892000.0000000000 - ]; tight)]
      └── filters (true)

# Get version of last card that was changed.
#
# Problems:
#   1. CardsView is actually a join between Cards and CardsInfo tables. But the
#      optimizer is missing join elimination rules. If those were available, we
#      could eliminate the join to Cards (because of FK).
#   2. InnerJoin can be pushed below GroupBy, which would put the GroupBy as the
#      input of the ScalarGroupBy.
#   3. ScalarGroupBy Max of a GroupBy Max is just ScalarGroupBy Max. Those two
#      would then be collapsed into one.
#   4. Furthermore, the join with the second Cards table could be eliminated,
#      just as with #1.
#
opt
SELECT coalesce(max(Version), 0) FROM GlobalCardsView
----
project
 ├── columns: coalesce:31(decimal)
 ├── cardinality: [1 - 1]
 ├── key: ()
 ├── fd: ()-->(31)
 ├── scalar-group-by
 │    ├── columns: max:30(decimal)
 │    ├── cardinality: [1 - 1]
 │    ├── key: ()
 │    ├── fd: ()-->(30)
 │    ├── inner-join (hash)
 │    │    ├── columns: c.id:1(int!null) cardid:8(int!null) max:29(decimal)
 │    │    ├── key: (8)
 │    │    ├── fd: (8)-->(29), (1)==(8), (8)==(1)
 │    │    ├── scan c@cardsnamesetnumber
 │    │    │    ├── columns: c.id:1(int!null)
 │    │    │    └── key: (1)
 │    │    ├── group-by
 │    │    │    ├── columns: cardid:8(int!null) max:29(decimal)
 │    │    │    ├── grouping columns: cardid:8(int!null)
 │    │    │    ├── key: (8)
 │    │    │    ├── fd: (8)-->(29)
 │    │    │    ├── inner-join (hash)
 │    │    │    │    ├── columns: dealerid:7(oid!null) cardid:8(int!null) version:15(decimal!null) cards.id:16(int!null)
 │    │    │    │    ├── key: (7,16)
 │    │    │    │    ├── fd: (7,8)-->(15), (7,15)-->(8), (8)==(16), (16)==(8)
 │    │    │    │    ├── scan cardsinfo@cardsinfoversionindex
 │    │    │    │    │    ├── columns: dealerid:7(oid!null) cardid:8(int!null) version:15(decimal!null)
 │    │    │    │    │    ├── constraint: /7/15: [/1 - /4]
 │    │    │    │    │    ├── key: (7,8)
 │    │    │    │    │    └── fd: (7,8)-->(15), (7,15)-->(8)
 │    │    │    │    ├── scan cards@cardsnamesetnumber
 │    │    │    │    │    ├── columns: cards.id:16(int!null)
 │    │    │    │    │    └── key: (16)
 │    │    │    │    └── filters
 │    │    │    │         └── cardid = cards.id [type=bool, outer=(8,16), constraints=(/8: (/NULL - ]; /16: (/NULL - ]), fd=(8)==(16), (16)==(8)]
 │    │    │    └── aggregations
 │    │    │         └── max [type=decimal, outer=(15)]
 │    │    │              └── variable: version [type=decimal]
 │    │    └── filters
 │    │         └── c.id = cardid [type=bool, outer=(1,8), constraints=(/1: (/NULL - ]; /8: (/NULL - ]), fd=(1)==(8), (8)==(1)]
 │    └── aggregations
 │         └── max [type=decimal, outer=(29)]
 │              └── variable: max [type=decimal]
 └── projections
      └── COALESCE(max, 0) [type=decimal, outer=(30)]

# Show last 20 transactions for a particular card.
#
# Problems:
#   1. Wrong join-type is selected (hash instead of lookup). This is because
#      "NOT IsBuy" needs to be mapped to "IsBuy = FALSE".
#   2. Index join should be applied after join between TransactionsView and
#      TransactionDetailsView.
#
opt
SELECT
  d.IsBuy, TransactionDate, CardId, Quantity, SellPrice, BuyPrice,
  t.IsBuy AS IsBuy2, Date, AccountName, CustomerName
FROM TransactionDetailsView d
INNER JOIN TransactionsView t
ON t.Date = d.TransactionDate
WHERE (d.CardId = 21953) AND NOT d.IsBuy AND NOT t.IsBuy
ORDER BY TransactionDate DESC
LIMIT 20
----
project
 ├── columns: isbuy:2(bool!null) transactiondate:3(timestamptz!null) cardid:4(int!null) quantity:5(int!null) sellprice:6(decimal!null) buyprice:7(decimal!null) isbuy2:10(bool!null) date:11(timestamptz!null) accountname:12(varchar!null) customername:13(varchar!null)
 ├── cardinality: [0 - 20]
 ├── key: (5,11)
 ├── fd: ()-->(2,4,10), (3,5)-->(6,7), (11)-->(12,13), (3)==(11), (11)==(3)
 ├── ordering: -(3|11) opt(2,4,10) [actual: -3]
 └── limit
      ├── columns: transactiondetails.dealerid:1(oid!null) transactiondetails.isbuy:2(bool!null) transactiondate:3(timestamptz!null) cardid:4(int!null) quantity:5(int!null) sellprice:6(decimal!null) buyprice:7(decimal!null) transactions.dealerid:9(oid!null) transactions.isbuy:10(bool!null) date:11(timestamptz!null) accountname:12(varchar!null) customername:13(varchar!null)
      ├── internal-ordering: -(3|11) opt(1,2,4,9,10)
      ├── cardinality: [0 - 20]
      ├── key: (5,11)
      ├── fd: ()-->(1,2,4,9,10), (3,5)-->(6,7), (11)-->(12,13), (3)==(11), (11)==(3)
      ├── ordering: -(3|11) opt(1,2,4,9,10) [actual: -3]
      ├── sort
      │    ├── columns: transactiondetails.dealerid:1(oid!null) transactiondetails.isbuy:2(bool!null) transactiondate:3(timestamptz!null) cardid:4(int!null) quantity:5(int!null) sellprice:6(decimal!null) buyprice:7(decimal!null) transactions.dealerid:9(oid!null) transactions.isbuy:10(bool!null) date:11(timestamptz!null) accountname:12(varchar!null) customername:13(varchar!null)
      │    ├── key: (5,11)
      │    ├── fd: ()-->(1,2,4,9,10), (3,5)-->(6,7), (11)-->(12,13), (3)==(11), (11)==(3)
      │    ├── ordering: -(3|11) opt(1,2,4,9,10) [actual: -3]
      │    └── inner-join (hash)
      │         ├── columns: transactiondetails.dealerid:1(oid!null) transactiondetails.isbuy:2(bool!null) transactiondate:3(timestamptz!null) cardid:4(int!null) quantity:5(int!null) sellprice:6(decimal!null) buyprice:7(decimal!null) transactions.dealerid:9(oid!null) transactions.isbuy:10(bool!null) date:11(timestamptz!null) accountname:12(varchar!null) customername:13(varchar!null)
      │         ├── key: (5,11)
      │         ├── fd: ()-->(1,2,4,9,10), (3,5)-->(6,7), (11)-->(12,13), (3)==(11), (11)==(3)
      │         ├── scan transactions
      │         │    ├── columns: transactions.dealerid:9(oid!null) transactions.isbuy:10(bool!null) date:11(timestamptz!null) accountname:12(varchar!null) customername:13(varchar!null)
      │         │    ├── constraint: /9/10/11: [/1/false - /1/false]
      │         │    ├── key: (11)
      │         │    └── fd: ()-->(9,10), (11)-->(12,13)
      │         ├── index-join transactiondetails
      │         │    ├── columns: transactiondetails.dealerid:1(oid!null) transactiondetails.isbuy:2(bool!null) transactiondate:3(timestamptz!null) cardid:4(int!null) quantity:5(int!null) sellprice:6(decimal!null) buyprice:7(decimal!null)
      │         │    ├── key: (3,5)
      │         │    ├── fd: ()-->(1,2,4), (3,5)-->(6,7)
      │         │    └── scan transactiondetails@detailscardidindex
      │         │         ├── columns: transactiondetails.dealerid:1(oid!null) transactiondetails.isbuy:2(bool!null) transactiondate:3(timestamptz!null) cardid:4(int!null) quantity:5(int!null)
      │         │         ├── constraint: /1/2/4/3/5: [/1/false/21953 - /1/false/21953]
      │         │         ├── key: (3,5)
      │         │         └── fd: ()-->(1,2,4)
      │         └── filters
      │              └── date = transactiondate [type=bool, outer=(3,11), constraints=(/3: (/NULL - ]; /11: (/NULL - ]), fd=(3)==(11), (11)==(3)]
      └── const: 20 [type=int]

# Show last 20 prices for a card.
opt
SELECT CardId, PriceDate, PricedBy, BuyPrice, SellPrice
FROM PriceDetailsView
WHERE CardId = 12345
ORDER BY PriceDate DESC
LIMIT 10
----
project
 ├── columns: cardid:2(int!null) pricedate:3(timestamptz!null) pricedby:4(varchar!null) buyprice:5(decimal!null) sellprice:6(decimal!null)
 ├── cardinality: [0 - 10]
 ├── key: (3)
 ├── fd: ()-->(2), (3)-->(4-6)
 ├── ordering: -3 opt(2) [actual: -3]
 └── scan pricedetails,rev
      ├── columns: dealerid:1(oid!null) cardid:2(int!null) pricedate:3(timestamptz!null) pricedby:4(varchar!null) buyprice:5(decimal!null) sellprice:6(decimal!null)
      ├── constraint: /1/2/3: [/1/12345 - /1/12345]
      ├── limit: 10(rev)
      ├── key: (3)
      ├── fd: ()-->(1,2), (3)-->(4-6)
      └── ordering: -3 opt(1,2) [actual: -3]

# Show next page of 50 cards.
#
# Problems:
#   1. This query will not compile, due to #46180.
#   2. The TransactionDate comparisons should be the last 2 days from the
#      current timestamp. However, the current timestamp is not treated as a
#      constant as it should be.
#   3. Missing rule to push "LIMIT 50" into GroupBy->LeftJoin complex. This
#      would need to be an exploration rule since it involves an ordering.
#      Or we could push down the "limit hint" into GroupBy->LeftJoin.
#   4. Wrong join-type (probably due to #3 above). Should be LookupJoin.
#
#opt
#SELECT
#  Id, Name, Rarity, SetName, Number, IsFoil, BuyPrice, SellPrice,
#  DesiredInventory, ActualInventory, Version, Discount, MaxInventory, Value AS TwoDaySales
#FROM
#(
#  SELECT *,
#    coalesce((
#      SELECT sum(Quantity)
#      FROM TransactionDetailsView d
#      WHERE
#        d.CardId = c.Id AND
#        d.IsBuy = FALSE AND
#        d.TransactionDate BETWEEN '2020-03-01'::TIMESTAMPTZ - INTERVAL '2 days' AND '2020-03-01'::TIMESTAMPTZ
#      ), 0) AS Value
#  FROM CardsView c
#) AS c
#WHERE (Name, SetName, Number) > ('Shock', '7E', 248)
#ORDER BY Name, SetName, Number
#LIMIT 50
#----

# Daily transaction query.
#
# Problems:
#   1. CardsView is actually a join between Cards and CardsInfo tables. But the
#      optimizer is missing join elimination rules. If those were available, we
#      could eliminate the join to Cards (because of FK).
#   2. Inequality predicate terms (accountname / customername) are too
#      selective.
#   3. The Date comparisons should be the last 7 days from the current
#      timestamp. However, the current timestamp is not treated as a constant as
#      it should be.
#
opt
SELECT
  extract(day from d.TransactionDate),
  sum(d.SellPrice * d.Quantity) AS TotalSell,
  sum(d.BuyPrice * d.Quantity) AS TotalBuy,
  sum((d.SellPrice - d.BuyPrice) * d.Quantity) AS TotalProfit
FROM TransactionDetailsView d, TransactionsView t, CardsView c
WHERE
  d.TransactionDate = t.Date AND
  c.Id = d.CardId AND
  NOT d.IsBuy AND
  NOT t.IsBuy AND
  t.Date BETWEEN '2020-03-01'::TIMESTAMPTZ - INTERVAL '7 days' AND '2020-03-01'::TIMESTAMPTZ AND
  t.AccountName <> 'someaccount' AND
  t.customername <> 'somecustomer'
GROUP BY extract(day from d.TransactionDate)
ORDER BY extract(day from d.TransactionDate)
----
group-by
 ├── columns: extract:37(int) totalsell:32(decimal) totalbuy:34(decimal) totalprofit:36(decimal)
 ├── grouping columns: column37:37(int)
 ├── key: (37)
 ├── fd: (37)-->(32,34,36)
 ├── ordering: +37
 ├── sort
 │    ├── columns: column31:31(decimal) column33:33(decimal) column35:35(decimal) column37:37(int)
 │    ├── ordering: +37
 │    └── project
 │         ├── columns: column31:31(decimal) column33:33(decimal) column35:35(decimal) column37:37(int)
 │         ├── inner-join (hash)
 │         │    ├── columns: transactiondetails.dealerid:1(oid!null) transactiondetails.isbuy:2(bool!null) transactiondate:3(timestamptz!null) transactiondetails.cardid:4(int!null) quantity:5(int!null) transactiondetails.sellprice:6(decimal!null) transactiondetails.buyprice:7(decimal!null) transactions.dealerid:9(oid!null) transactions.isbuy:10(bool!null) date:11(timestamptz!null) accountname:12(varchar!null) customername:13(varchar!null) id:16(int!null) cardsinfo.dealerid:22(oid!null) cardsinfo.cardid:23(int!null)
 │         │    ├── key: (5,11,23)
 │         │    ├── fd: ()-->(1,2,9,10,22), (3-5)-->(6,7), (11)-->(12,13), (16)==(4,23), (23)==(4,16), (3)==(11), (11)==(3), (4)==(16,23)
 │         │    ├── scan cardsinfo@cardsinfoversionindex
 │         │    │    ├── columns: cardsinfo.dealerid:22(oid!null) cardsinfo.cardid:23(int!null)
 │         │    │    ├── constraint: /22/30: [/1 - /1]
 │         │    │    ├── key: (23)
 │         │    │    └── fd: ()-->(22)
 │         │    ├── inner-join (hash)
 │         │    │    ├── columns: transactiondetails.dealerid:1(oid!null) transactiondetails.isbuy:2(bool!null) transactiondate:3(timestamptz!null) transactiondetails.cardid:4(int!null) quantity:5(int!null) transactiondetails.sellprice:6(decimal!null) transactiondetails.buyprice:7(decimal!null) transactions.dealerid:9(oid!null) transactions.isbuy:10(bool!null) date:11(timestamptz!null) accountname:12(varchar!null) customername:13(varchar!null) id:16(int!null)
 │         │    │    ├── key: (5,11,16)
 │         │    │    ├── fd: ()-->(1,2,9,10), (11)-->(12,13), (3-5)-->(6,7), (3)==(11), (11)==(3), (4)==(16), (16)==(4)
 │         │    │    ├── scan cards@cardsnamesetnumber
 │         │    │    │    ├── columns: id:16(int!null)
 │         │    │    │    └── key: (16)
 │         │    │    ├── inner-join (hash)
 │         │    │    │    ├── columns: transactiondetails.dealerid:1(oid!null) transactiondetails.isbuy:2(bool!null) transactiondate:3(timestamptz!null) transactiondetails.cardid:4(int!null) quantity:5(int!null) transactiondetails.sellprice:6(decimal!null) transactiondetails.buyprice:7(decimal!null) transactions.dealerid:9(oid!null) transactions.isbuy:10(bool!null) date:11(timestamptz!null) accountname:12(varchar!null) customername:13(varchar!null)
 │         │    │    │    ├── key: (4,5,11)
 │         │    │    │    ├── fd: ()-->(1,2,9,10), (11)-->(12,13), (3-5)-->(6,7), (3)==(11), (11)==(3)
 │         │    │    │    ├── scan transactiondetails
 │         │    │    │    │    ├── columns: transactiondetails.dealerid:1(oid!null) transactiondetails.isbuy:2(bool!null) transactiondate:3(timestamptz!null) transactiondetails.cardid:4(int!null) quantity:5(int!null) transactiondetails.sellprice:6(decimal!null) transactiondetails.buyprice:7(decimal!null)
 │         │    │    │    │    ├── constraint: /1/2/3/4/5: [/1/false/'2020-02-23 00:00:00+00:00' - /1/false/'2020-03-01 00:00:00+00:00']
 │         │    │    │    │    ├── key: (3-5)
 │         │    │    │    │    └── fd: ()-->(1,2), (3-5)-->(6,7)
 │         │    │    │    ├── select
 │         │    │    │    │    ├── columns: transactions.dealerid:9(oid!null) transactions.isbuy:10(bool!null) date:11(timestamptz!null) accountname:12(varchar!null) customername:13(varchar!null)
 │         │    │    │    │    ├── key: (11)
 │         │    │    │    │    ├── fd: ()-->(9,10), (11)-->(12,13)
 │         │    │    │    │    ├── scan transactions
 │         │    │    │    │    │    ├── columns: transactions.dealerid:9(oid!null) transactions.isbuy:10(bool!null) date:11(timestamptz!null) accountname:12(varchar!null) customername:13(varchar!null)
 │         │    │    │    │    │    ├── constraint: /9/10/11: [/1/false/'2020-02-23 00:00:00+00:00' - /1/false/'2020-03-01 00:00:00+00:00']
 │         │    │    │    │    │    ├── key: (11)
 │         │    │    │    │    │    └── fd: ()-->(9,10), (11)-->(12,13)
 │         │    │    │    │    └── filters
 │         │    │    │    │         ├── accountname != 'someaccount' [type=bool, outer=(12), constraints=(/12: (/NULL - /'someaccount') [/e'someaccount\x00' - ]; tight)]
 │         │    │    │    │         └── customername != 'somecustomer' [type=bool, outer=(13), constraints=(/13: (/NULL - /'somecustomer') [/e'somecustomer\x00' - ]; tight)]
 │         │    │    │    └── filters
 │         │    │    │         └── transactiondate = date [type=bool, outer=(3,11), constraints=(/3: (/NULL - ]; /11: (/NULL - ]), fd=(3)==(11), (11)==(3)]
 │         │    │    └── filters
 │         │    │         └── id = transactiondetails.cardid [type=bool, outer=(4,16), constraints=(/4: (/NULL - ]; /16: (/NULL - ]), fd=(4)==(16), (16)==(4)]
 │         │    └── filters
 │         │         └── id = cardsinfo.cardid [type=bool, outer=(16,23), constraints=(/16: (/NULL - ]; /23: (/NULL - ]), fd=(16)==(23), (23)==(16)]
 │         └── projections
 │              ├── transactiondetails.sellprice * quantity [type=decimal, outer=(5,6)]
 │              ├── transactiondetails.buyprice * quantity [type=decimal, outer=(5,7)]
 │              ├── quantity * (transactiondetails.sellprice - transactiondetails.buyprice) [type=decimal, outer=(5-7)]
 │              └── extract('day', transactiondate) [type=int, outer=(3)]
 └── aggregations
      ├── sum [type=decimal, outer=(31)]
      │    └── variable: column31 [type=decimal]
      ├── sum [type=decimal, outer=(33)]
      │    └── variable: column33 [type=decimal]
      └── sum [type=decimal, outer=(35)]
           └── variable: column35 [type=decimal]

# Check if transaction was already inserted, for idempotency.
#
# Problems:
#   1. Missing rule to transform AnyOp into ExistsOp when both the scalar
#      inclusion value and subquery column are non-NULL.
#
# NOTE: This looks awkward, but it's adapted from stored procedure code.
opt
SELECT
(
  '70F03EB1-4F58-4C26-B72D-C524A9D537DD'::UUID IN
  (
    SELECT coalesce(OperationId, '00000000-0000-0000-0000-000000000000'::UUID)
    FROM TransactionsView
    WHERE IsBuy = FALSE
  )
) AS AlreadyInserted
----
values
 ├── columns: alreadyinserted:9(bool)
 ├── cardinality: [1 - 1]
 ├── key: ()
 ├── fd: ()-->(9)
 └── tuple [type=tuple{bool}]
      └── any: eq [type=bool]
           ├── project
           │    ├── columns: coalesce:8(uuid)
           │    ├── scan transactions
           │    │    ├── columns: dealerid:1(oid!null) isbuy:2(bool!null) operationid:6(uuid)
           │    │    ├── constraint: /1/2/3: [/1/false - /1/false]
           │    │    ├── lax-key: (6)
           │    │    └── fd: ()-->(1,2)
           │    └── projections
           │         └── COALESCE(operationid, '00000000-0000-0000-0000-000000000000') [type=uuid, outer=(6)]
           └── const: '70f03eb1-4f58-4c26-b72d-c524a9d537dd' [type=uuid]

# Get account locations of a list of cards.
#
# Problems:
#   1. WITH is not inlined into the query, because it is marked as having side
#      effects, even though it does not.
#   2. unnest should be folded into VALUES operator when it operates over
#      constant ARRAY.
opt
WITH CardsToFind AS
(
  SELECT (IdAndQuantity)[1] AS Id, (IdAndQuantity)[2] AS Quantity
  FROM unnest(ARRAY[ARRAY[42948, 3], ARRAY[24924, 4]]) AS IdAndQuantity
)
SELECT AccountName, sum(Quantity) AS Quantity
FROM
(
    SELECT Id, AccountName, (CASE WHEN needed.Quantity < have.Quantity THEN needed.Quantity ELSE have.Quantity END) Quantity
    FROM CardsToFind AS needed
    INNER JOIN
    (
        SELECT CardId, AccountName, Quantity
        FROM InventoryDetails
        WHERE (dealerid = 1 OR dealerid = 2 OR dealerid = 3 OR dealerid = 4 OR dealerid = 5) AND
            AccountName = ANY ARRAY['account-1', 'account-2', 'account-3']
    ) AS have
    ON CardId = Id
) AS allData
GROUP BY AccountName
ORDER BY sum(Quantity) DESC
LIMIT 1000
----
sort
 ├── columns: accountname:8(varchar!null) quantity:12(decimal)
 ├── cardinality: [0 - 1000]
 ├── side-effects
 ├── key: (8)
 ├── fd: (8)-->(12)
 ├── ordering: -12
 └── with &1 (cardstofind)
      ├── columns: accountname:8(varchar!null) sum:12(decimal)
      ├── cardinality: [0 - 1000]
      ├── side-effects
      ├── key: (8)
      ├── fd: (8)-->(12)
      ├── project
      │    ├── columns: id:2(int) quantity:3(int)
      │    ├── side-effects
      │    ├── project-set
      │    │    ├── columns: unnest:1(int[])
      │    │    ├── side-effects
      │    │    ├── values
      │    │    │    ├── cardinality: [1 - 1]
      │    │    │    ├── key: ()
      │    │    │    └── tuple [type=tuple]
      │    │    └── zip
      │    │         └── function: unnest [type=int[], side-effects]
      │    │              └── const: ARRAY[ARRAY[42948,3],ARRAY[24924,4]] [type=int[][]]
      │    └── projections
      │         ├── unnest[1] [type=int, outer=(1)]
      │         └── unnest[2] [type=int, outer=(1)]
      └── limit
           ├── columns: accountname:8(varchar!null) sum:12(decimal)
           ├── internal-ordering: -12
           ├── cardinality: [0 - 1000]
           ├── key: (8)
           ├── fd: (8)-->(12)
           ├── sort
           │    ├── columns: accountname:8(varchar!null) sum:12(decimal)
           │    ├── key: (8)
           │    ├── fd: (8)-->(12)
           │    ├── ordering: -12
           │    └── group-by
           │         ├── columns: accountname:8(varchar!null) sum:12(decimal)
           │         ├── grouping columns: accountname:8(varchar!null)
           │         ├── key: (8)
           │         ├── fd: (8)-->(12)
           │         ├── project
           │         │    ├── columns: quantity:11(int) accountname:8(varchar!null)
           │         │    ├── inner-join (lookup inventorydetails)
           │         │    │    ├── columns: id:4(int!null) quantity:5(int) dealerid:6(oid!null) cardid:7(int!null) accountname:8(varchar!null) inventorydetails.quantity:9(int!null)
           │         │    │    ├── key columns: [6 7 8] = [6 7 8]
           │         │    │    ├── lookup columns are key
           │         │    │    ├── fd: (6-8)-->(9), (4)==(7), (7)==(4)
           │         │    │    ├── inner-join (lookup inventorydetails@inventorydetails_auto_index_inventorydetailscardidkey)
           │         │    │    │    ├── columns: id:4(int!null) quantity:5(int) dealerid:6(oid!null) cardid:7(int!null) accountname:8(varchar!null)
           │         │    │    │    ├── key columns: [4] = [7]
           │         │    │    │    ├── fd: (4)==(7), (7)==(4)
           │         │    │    │    ├── with-scan &1 (cardstofind)
           │         │    │    │    │    ├── columns: id:4(int) quantity:5(int)
           │         │    │    │    │    └── mapping:
           │         │    │    │    │         ├──  id:2(int) => id:4(int)
           │         │    │    │    │         └──  quantity:3(int) => quantity:5(int)
           │         │    │    │    └── filters
           │         │    │    │         ├── ((((dealerid = 1) OR (dealerid = 2)) OR (dealerid = 3)) OR (dealerid = 4)) OR (dealerid = 5) [type=bool, outer=(6), constraints=(/6: [/1 - /1] [/2 - /2] [/3 - /3] [/4 - /4] [/5 - /5]; tight)]
           │         │    │    │         └── accountname IN ('account-1', 'account-2', 'account-3') [type=bool, outer=(8), constraints=(/8: [/'account-1' - /'account-1'] [/'account-2' - /'account-2'] [/'account-3' - /'account-3']; tight)]
           │         │    │    └── filters (true)
           │         │    └── projections
           │         │         └── CASE WHEN quantity < inventorydetails.quantity THEN quantity ELSE inventorydetails.quantity END [type=int, outer=(5,9)]
           │         └── aggregations
           │              └── sum [type=decimal, outer=(11)]
           │                   └── variable: quantity [type=int]
           └── const: 1000 [type=int]

# Get buy/sell volume of a particular card in the last 2 days.
#
# Problems:
#   1. Multiple duplicate predicates. Scan is already constraining CardId,
#      IsBuy, and TransactionDate. But filters still contain those checks.
#
opt
SELECT coalesce((
    SELECT sum(Quantity) AS Volume
    FROM
    (
        SELECT d.Quantity
        FROM TransactionDetails d
        INNER JOIN Transactions t
        ON d.dealerid = t.dealerid AND d.isbuy = t.isbuy AND d.transactiondate = t.date
        WHERE
          (d.dealerid = 1 OR d.dealerid = 2 OR d.dealerid = 3 OR d.dealerid = 4 OR d.dealerid = 5) AND
          d.isbuy IN (TRUE, FALSE) AND
          d.cardid = 19483 AND
          d.transactiondate BETWEEN '2020-03-01'::TIMESTAMPTZ - INTERVAL '2 days' AND '2020-03-01'::TIMESTAMPTZ
        ORDER BY TransactionDate DESC
        LIMIT 100
    ) t
), 0)
----
values
 ├── columns: coalesce:17(decimal)
 ├── cardinality: [1 - 1]
 ├── key: ()
 ├── fd: ()-->(17)
 └── tuple [type=tuple{decimal}]
      └── coalesce [type=decimal]
           ├── subquery [type=decimal]
           │    └── scalar-group-by
           │         ├── columns: sum:16(decimal)
           │         ├── cardinality: [1 - 1]
           │         ├── key: ()
           │         ├── fd: ()-->(16)
           │         ├── limit
           │         │    ├── columns: d.dealerid:1(oid!null) d.isbuy:2(bool!null) transactiondate:3(timestamptz!null) cardid:4(int!null) quantity:5(int!null) t.dealerid:9(oid!null) t.isbuy:10(bool!null) date:11(timestamptz!null)
           │         │    ├── internal-ordering: -(3|11) opt(4)
           │         │    ├── cardinality: [0 - 100]
           │         │    ├── key: (5,9-11)
           │         │    ├── fd: ()-->(4), (1)==(9), (9)==(1), (2)==(10), (10)==(2), (3)==(11), (11)==(3)
           │         │    ├── sort
           │         │    │    ├── columns: d.dealerid:1(oid!null) d.isbuy:2(bool!null) transactiondate:3(timestamptz!null) cardid:4(int!null) quantity:5(int!null) t.dealerid:9(oid!null) t.isbuy:10(bool!null) date:11(timestamptz!null)
           │         │    │    ├── key: (5,9-11)
           │         │    │    ├── fd: ()-->(4), (1)==(9), (9)==(1), (2)==(10), (10)==(2), (3)==(11), (11)==(3)
           │         │    │    ├── ordering: -(3|11) opt(4) [actual: -3]
           │         │    │    └── inner-join (lookup transactions)
           │         │    │         ├── columns: d.dealerid:1(oid!null) d.isbuy:2(bool!null) transactiondate:3(timestamptz!null) cardid:4(int!null) quantity:5(int!null) t.dealerid:9(oid!null) t.isbuy:10(bool!null) date:11(timestamptz!null)
           │         │    │         ├── key columns: [1 2 3] = [9 10 11]
           │         │    │         ├── lookup columns are key
           │         │    │         ├── key: (5,9-11)
           │         │    │         ├── fd: ()-->(4), (1)==(9), (9)==(1), (2)==(10), (10)==(2), (3)==(11), (11)==(3)
           │         │    │         ├── select
           │         │    │         │    ├── columns: d.dealerid:1(oid!null) d.isbuy:2(bool!null) transactiondate:3(timestamptz!null) cardid:4(int!null) quantity:5(int!null)
           │         │    │         │    ├── key: (1-3,5)
           │         │    │         │    ├── fd: ()-->(4)
           │         │    │         │    ├── scan d@transactiondetails_auto_index_detailscardidkey
           │         │    │         │    │    ├── columns: d.dealerid:1(oid!null) d.isbuy:2(bool!null) transactiondate:3(timestamptz!null) cardid:4(int!null) quantity:5(int!null)
           │         │    │         │    │    ├── constraint: /4/1/2/3/5
           │         │    │         │    │    │    ├── [/19483/1/false/'2020-02-28 00:00:00+00:00' - /19483/1/false/'2020-03-01 00:00:00+00:00']
           │         │    │         │    │    │    ├── [/19483/1/true/'2020-02-28 00:00:00+00:00' - /19483/1/true/'2020-03-01 00:00:00+00:00']
           │         │    │         │    │    │    ├── [/19483/2/false/'2020-02-28 00:00:00+00:00' - /19483/2/false/'2020-03-01 00:00:00+00:00']
           │         │    │         │    │    │    ├── [/19483/2/true/'2020-02-28 00:00:00+00:00' - /19483/2/true/'2020-03-01 00:00:00+00:00']
           │         │    │         │    │    │    ├── [/19483/3/false/'2020-02-28 00:00:00+00:00' - /19483/3/false/'2020-03-01 00:00:00+00:00']
           │         │    │         │    │    │    ├── [/19483/3/true/'2020-02-28 00:00:00+00:00' - /19483/3/true/'2020-03-01 00:00:00+00:00']
           │         │    │         │    │    │    ├── [/19483/4/false/'2020-02-28 00:00:00+00:00' - /19483/4/false/'2020-03-01 00:00:00+00:00']
           │         │    │         │    │    │    ├── [/19483/4/true/'2020-02-28 00:00:00+00:00' - /19483/4/true/'2020-03-01 00:00:00+00:00']
           │         │    │         │    │    │    ├── [/19483/5/false/'2020-02-28 00:00:00+00:00' - /19483/5/false/'2020-03-01 00:00:00+00:00']
           │         │    │         │    │    │    └── [/19483/5/true/'2020-02-28 00:00:00+00:00' - /19483/5/true/'2020-03-01 00:00:00+00:00']
           │         │    │         │    │    ├── key: (1-3,5)
           │         │    │         │    │    └── fd: ()-->(4)
           │         │    │         │    └── filters
           │         │    │         │         └── ((((d.dealerid = 1) OR (d.dealerid = 2)) OR (d.dealerid = 3)) OR (d.dealerid = 4)) OR (d.dealerid = 5) [type=bool, outer=(1), constraints=(/1: [/1 - /1] [/2 - /2] [/3 - /3] [/4 - /4] [/5 - /5]; tight)]
           │         │    │         └── filters
           │         │    │              ├── (date >= '2020-02-28 00:00:00+00:00') AND (date <= '2020-03-01 00:00:00+00:00') [type=bool, outer=(11), constraints=(/11: [/'2020-02-28 00:00:00+00:00' - /'2020-03-01 00:00:00+00:00']; tight)]
           │         │    │              ├── ((((t.dealerid = 1) OR (t.dealerid = 2)) OR (t.dealerid = 3)) OR (t.dealerid = 4)) OR (t.dealerid = 5) [type=bool, outer=(9), constraints=(/9: [/1 - /1] [/2 - /2] [/3 - /3] [/4 - /4] [/5 - /5]; tight)]
           │         │    │              └── t.isbuy IN (false, true) [type=bool, outer=(10), constraints=(/10: [/false - /false] [/true - /true]; tight)]
           │         │    └── const: 100 [type=int]
           │         └── aggregations
           │              └── sum [type=decimal, outer=(5)]
           │                   └── variable: quantity [type=int]
           └── const: 0 [type=decimal]

# --------------------------------------------------
# INSERT/UPDATE/DELETE/UPSERT Queries
# --------------------------------------------------

# Insert buy or sell transaction.
opt
INSERT INTO Transactions (dealerid, isbuy, date, accountname, customername, operationid)
VALUES (1, FALSE, '2020-03-01', 'the-account', 'the-customer', '70F03EB1-4F58-4C26-B72D-C524A9D537DD')
----
insert transactions
 ├── columns: <none>
 ├── insert-mapping:
 │    ├──  column1:8 => dealerid:1
 │    ├──  column2:9 => isbuy:2
 │    ├──  column3:10 => date:3
 │    ├──  column4:11 => accountname:4
 │    ├──  column5:12 => customername:5
 │    ├──  column6:13 => operationid:6
 │    └──  column14:14 => version:7
 ├── cardinality: [0 - 0]
 ├── side-effects, mutations
 └── values
      ├── columns: column1:8(oid!null) column2:9(bool!null) column3:10(timestamptz!null) column4:11(string!null) column5:12(string!null) column6:13(uuid!null) column14:14(decimal)
      ├── cardinality: [1 - 1]
      ├── side-effects
      ├── key: ()
      ├── fd: ()-->(8-14)
      └── (1, false, '2020-03-01 00:00:00+00:00', 'the-account', 'the-customer', '70f03eb1-4f58-4c26-b72d-c524a9d537dd', cluster_logical_timestamp()) [type=tuple{oid, bool, timestamptz, string, string, uuid, decimal}]

# Upsert buy or sell transaction.
opt
UPSERT INTO Transactions (dealerid, isbuy, date, accountname, customername, operationid)
VALUES (1, FALSE, '2020-03-01', 'the-account', 'the-customer', '70F03EB1-4F58-4C26-B72D-C524A9D537DD')
----
upsert transactions
 ├── columns: <none>
 ├── canary column: 15
 ├── fetch columns: dealerid:15(oid) isbuy:16(bool) date:17(timestamptz) accountname:18(varchar) customername:19(varchar) operationid:20(uuid) version:21(decimal)
 ├── insert-mapping:
 │    ├──  column1:8 => dealerid:1
 │    ├──  column2:9 => isbuy:2
 │    ├──  column3:10 => date:3
 │    ├──  column4:11 => accountname:4
 │    ├──  column5:12 => customername:5
 │    ├──  column6:13 => operationid:6
 │    └──  column14:14 => version:7
 ├── update-mapping:
 │    ├──  column4:11 => accountname:4
 │    ├──  column5:12 => customername:5
 │    └──  column6:13 => operationid:6
 ├── cardinality: [0 - 0]
 ├── side-effects, mutations
 └── left-join (cross)
      ├── columns: column1:8(oid!null) column2:9(bool!null) column3:10(timestamptz!null) column4:11(string!null) column5:12(string!null) column6:13(uuid!null) column14:14(decimal) dealerid:15(oid) isbuy:16(bool) date:17(timestamptz) accountname:18(varchar) customername:19(varchar) operationid:20(uuid) version:21(decimal)
      ├── cardinality: [1 - 1]
      ├── side-effects
      ├── key: ()
      ├── fd: ()-->(8-21)
      ├── values
      │    ├── columns: column1:8(oid!null) column2:9(bool!null) column3:10(timestamptz!null) column4:11(string!null) column5:12(string!null) column6:13(uuid!null) column14:14(decimal)
      │    ├── cardinality: [1 - 1]
      │    ├── side-effects
      │    ├── key: ()
      │    ├── fd: ()-->(8-14)
      │    └── (1, false, '2020-03-01 00:00:00+00:00', 'the-account', 'the-customer', '70f03eb1-4f58-4c26-b72d-c524a9d537dd', cluster_logical_timestamp()) [type=tuple{oid, bool, timestamptz, string, string, uuid, decimal}]
      ├── scan transactions
      │    ├── columns: dealerid:15(oid!null) isbuy:16(bool!null) date:17(timestamptz!null) accountname:18(varchar!null) customername:19(varchar!null) operationid:20(uuid) version:21(decimal!null)
      │    ├── constraint: /15/16/17: [/1/false/'2020-03-01 00:00:00+00:00' - /1/false/'2020-03-01 00:00:00+00:00']
      │    ├── cardinality: [0 - 1]
      │    ├── key: ()
      │    └── fd: ()-->(15-21)
      └── filters (true)

# Insert structured data (c=CardId, q=Quantity, s=SellPrice, b=BuyPrice)
# represented as JSON into TransactionDetails table.
#
# Problems:
#   1. WITH is not inlined into the query, because it is marked as having side
#      effects, even though it does not.
#   2. jsonb_array_elements should be folded into VALUES operator when it
#      operates over constant JSON.
#
opt
WITH updates AS (SELECT jsonb_array_elements('[
    {"c": 49833, "q": 4, "s": 2.89, "b": 2.29},
    {"c": 29483, "q": 2, "s": 18.93, "b": 17.59}
  ]'::JSONB
) AS Detail)
UPSERT INTO TransactionDetails
(dealerid, isbuy, transactiondate, cardid, quantity, sellprice, buyprice)
SELECT
  1, FALSE, current_timestamp(), (Detail->'c')::TEXT::INT, (Detail->'q')::TEXT::INT,
  (Detail->'s')::TEXT::DECIMAL(10,4), (Detail->'b')::TEXT::DECIMAL(10,4)
FROM updates
----
with &1 (updates)
 ├── cardinality: [0 - 0]
 ├── side-effects, mutations
 ├── project-set
 │    ├── columns: jsonb_array_elements:1(jsonb)
 │    ├── side-effects
 │    ├── values
 │    │    ├── cardinality: [1 - 1]
 │    │    ├── key: ()
 │    │    └── tuple [type=tuple]
 │    └── zip
 │         └── function: jsonb_array_elements [type=jsonb, side-effects]
 │              └── const: '[{"b": 2.29, "c": 49833, "q": 4, "s": 2.89}, {"b": 17.59, "c": 29483, "q": 2, "s": 18.93}]' [type=jsonb]
 └── upsert transactiondetails
      ├── columns: <none>
      ├── canary column: 21
      ├── fetch columns: dealerid:21(oid) isbuy:22(bool) transactiondate:23(timestamptz) cardid:24(int) quantity:25(int) transactiondetails.sellprice:26(decimal) transactiondetails.buyprice:27(decimal) version:28(decimal)
      ├── insert-mapping:
      │    ├──  "?column?":11 => dealerid:2
      │    ├──  bool:12 => isbuy:3
      │    ├──  current_timestamp:13 => transactiondate:4
      │    ├──  int8:14 => cardid:5
      │    ├──  int8:15 => quantity:6
      │    ├──  sellprice:29 => transactiondetails.sellprice:7
      │    ├──  buyprice:30 => transactiondetails.buyprice:8
      │    └──  column18:18 => version:9
      ├── update-mapping:
      │    ├──  sellprice:29 => transactiondetails.sellprice:7
      │    └──  buyprice:30 => transactiondetails.buyprice:8
      ├── cardinality: [0 - 0]
      ├── side-effects, mutations
      └── project
           ├── columns: sellprice:29(decimal) buyprice:30(decimal) "?column?":11(oid!null) bool:12(bool!null) current_timestamp:13(timestamptz) int8:14(int) int8:15(int) column18:18(decimal) dealerid:21(oid) isbuy:22(bool) transactiondate:23(timestamptz) cardid:24(int) quantity:25(int) transactiondetails.sellprice:26(decimal) transactiondetails.buyprice:27(decimal) version:28(decimal)
           ├── side-effects
           ├── fd: ()-->(11,12), (23-25)-->(26-28)
           ├── left-join (lookup transactiondetails)
           │    ├── columns: "?column?":11(oid!null) bool:12(bool!null) current_timestamp:13(timestamptz) int8:14(int) int8:15(int) column18:18(decimal) sellprice:19(decimal) buyprice:20(decimal) dealerid:21(oid) isbuy:22(bool) transactiondate:23(timestamptz) cardid:24(int) quantity:25(int) transactiondetails.sellprice:26(decimal) transactiondetails.buyprice:27(decimal) version:28(decimal)
           │    ├── key columns: [37 38 13 14 15] = [21 22 23 24 25]
           │    ├── lookup columns are key
           │    ├── side-effects
           │    ├── fd: ()-->(11,12), (23-25)-->(26-28)
           │    ├── project
           │    │    ├── columns: "project_const_col_@21":37(oid!null) "project_const_col_@22":38(bool!null) sellprice:19(decimal) buyprice:20(decimal) column18:18(decimal) "?column?":11(oid!null) bool:12(bool!null) current_timestamp:13(timestamptz) int8:14(int) int8:15(int)
           │    │    ├── side-effects
           │    │    ├── fd: ()-->(11,12,37,38)
           │    │    ├── with-scan &1 (updates)
           │    │    │    ├── columns: detail:10(jsonb)
           │    │    │    └── mapping:
           │    │    │         └──  jsonb_array_elements:1(jsonb) => detail:10(jsonb)
           │    │    └── projections
           │    │         ├── const: 1 [type=oid]
           │    │         ├── const: false [type=bool]
           │    │         ├── crdb_internal.round_decimal_values((detail->'s')::STRING::DECIMAL(10,4), 4) [type=decimal, outer=(10)]
           │    │         ├── crdb_internal.round_decimal_values((detail->'b')::STRING::DECIMAL(10,4), 4) [type=decimal, outer=(10)]
           │    │         ├── cluster_logical_timestamp() [type=decimal, side-effects]
           │    │         ├── const: 1 [type=oid]
           │    │         ├── false [type=bool]
           │    │         ├── current_timestamp() [type=timestamptz, side-effects]
           │    │         ├── (detail->'c')::STRING::INT8 [type=int, outer=(10)]
           │    │         └── (detail->'q')::STRING::INT8 [type=int, outer=(10)]
           │    └── filters (true)
           └── projections
                ├── crdb_internal.round_decimal_values(sellprice, 4) [type=decimal, outer=(19)]
                └── crdb_internal.round_decimal_values(buyprice, 4) [type=decimal, outer=(20)]

# Delete inventory detail rows to reflect card transfers.
opt
DELETE FROM InventoryDetails
WHERE dealerid = 1 AND accountname = 'some-account' AND cardid = ANY ARRAY[29483, 1793, 294]
----
delete inventorydetails
 ├── columns: <none>
 ├── fetch columns: dealerid:6(oid) cardid:7(int) accountname:8(varchar)
 ├── cardinality: [0 - 0]
 ├── side-effects, mutations
 └── scan inventorydetails@inventorydetails_auto_index_inventorydetailscardidkey
      ├── columns: dealerid:6(oid!null) cardid:7(int!null) accountname:8(varchar!null)
      ├── constraint: /7/6/8
      │    ├── [/294/1/'some-account' - /294/1/'some-account']
      │    ├── [/1793/1/'some-account' - /1793/1/'some-account']
      │    └── [/29483/1/'some-account' - /29483/1/'some-account']
      ├── cardinality: [0 - 3]
      ├── key: (7)
      └── fd: ()-->(6,8)

# Update CardsInfo inventory numbers (by CardId, Quantity) to reflect card
# transfers.
#
# Problems:
#   1. WITH is not inlined into the query, because it is marked as having side
#      effects, even though it does not.
#   2. unnest should be folded into VALUES operator when it operates over
#      constant ARRAY.
opt
WITH Updates AS
(
  SELECT (Detail)[1] AS c, (Detail)[2] AS q
  FROM unnest(ARRAY[ARRAY[42948, 3], ARRAY[24924, 4]]) AS Detail
)
UPDATE CardsInfo ci
SET actualinventory = (SELECT coalesce(sum_INT(quantity), 0)
                       FROM InventoryDetails id
                       WHERE dealerid = 1 AND id.cardid = ci.cardid)
FROM Updates
WHERE ci.cardid = Updates.c AND ci.dealerid = 1
----
with &1 (updates)
 ├── cardinality: [0 - 0]
 ├── side-effects, mutations
 ├── project
 │    ├── columns: c:2(int) q:3(int)
 │    ├── side-effects
 │    ├── project-set
 │    │    ├── columns: unnest:1(int[])
 │    │    ├── side-effects
 │    │    ├── values
 │    │    │    ├── cardinality: [1 - 1]
 │    │    │    ├── key: ()
 │    │    │    └── tuple [type=tuple]
 │    │    └── zip
 │    │         └── function: unnest [type=int[], side-effects]
 │    │              └── const: ARRAY[ARRAY[42948,3],ARRAY[24924,4]] [type=int[][]]
 │    └── projections
 │         ├── unnest[1] [type=int, outer=(1)]
 │         └── unnest[2] [type=int, outer=(1)]
 └── update ci
      ├── columns: <none>
      ├── fetch columns: ci.dealerid:13(oid) ci.cardid:14(int) buyprice:15(decimal) sellprice:16(decimal) discount:17(decimal) desiredinventory:18(int) actualinventory:19(int) maxinventory:20(int) ci.version:21(decimal)
      ├── update-mapping:
      │    └──  column31:31 => actualinventory:10
      ├── cardinality: [0 - 0]
      ├── side-effects, mutations
      └── project
           ├── columns: column31:31(int) ci.dealerid:13(oid) ci.cardid:14(int!null) buyprice:15(decimal) sellprice:16(decimal) discount:17(decimal) desiredinventory:18(int) actualinventory:19(int) maxinventory:20(int) ci.version:21(decimal) c:22(int) q:23(int)
           ├── key: (14)
           ├── fd: ()-->(13), (14)-->(15-23,31), (21)-->(14-20), (14)==(22), (22)==(14)
           ├── group-by
           │    ├── columns: ci.dealerid:13(oid) ci.cardid:14(int!null) buyprice:15(decimal) sellprice:16(decimal) discount:17(decimal) desiredinventory:18(int) actualinventory:19(int) maxinventory:20(int) ci.version:21(decimal) c:22(int) q:23(int) sum_int:29(int)
           │    ├── grouping columns: ci.cardid:14(int!null)
           │    ├── key: (14)
           │    ├── fd: ()-->(13), (14)-->(13,15-23,29), (21)-->(14-20), (14)==(22), (22)==(14)
           │    ├── left-join (lookup inventorydetails)
           │    │    ├── columns: ci.dealerid:13(oid) ci.cardid:14(int!null) buyprice:15(decimal) sellprice:16(decimal) discount:17(decimal) desiredinventory:18(int) actualinventory:19(int) maxinventory:20(int) ci.version:21(decimal) c:22(int) q:23(int) id.dealerid:24(oid) id.cardid:25(int) quantity:27(int)
           │    │    ├── key columns: [35 14] = [24 25]
           │    │    ├── fd: ()-->(13), (14)-->(15-23), (21)-->(14-20), (14)==(22), (22)==(14)
           │    │    ├── project
           │    │    │    ├── columns: "project_const_col_@24":35(oid!null) ci.dealerid:13(oid) ci.cardid:14(int!null) buyprice:15(decimal) sellprice:16(decimal) discount:17(decimal) desiredinventory:18(int) actualinventory:19(int) maxinventory:20(int) ci.version:21(decimal) c:22(int) q:23(int)
           │    │    │    ├── key: (14)
           │    │    │    ├── fd: ()-->(13,35), (14)-->(15-23), (21)-->(14-20), (14)==(22), (22)==(14)
           │    │    │    ├── distinct-on
           │    │    │    │    ├── columns: ci.dealerid:13(oid) ci.cardid:14(int!null) buyprice:15(decimal) sellprice:16(decimal) discount:17(decimal) desiredinventory:18(int) actualinventory:19(int) maxinventory:20(int) ci.version:21(decimal) c:22(int) q:23(int)
           │    │    │    │    ├── grouping columns: ci.cardid:14(int!null)
           │    │    │    │    ├── key: (14)
           │    │    │    │    ├── fd: ()-->(13), (14)-->(13,15-23), (21)-->(14-20), (14)==(22), (22)==(14)
           │    │    │    │    ├── inner-join (lookup cardsinfo)
           │    │    │    │    │    ├── columns: ci.dealerid:13(oid!null) ci.cardid:14(int!null) buyprice:15(decimal!null) sellprice:16(decimal!null) discount:17(decimal!null) desiredinventory:18(int!null) actualinventory:19(int!null) maxinventory:20(int!null) ci.version:21(decimal!null) c:22(int!null) q:23(int)
           │    │    │    │    │    ├── key columns: [32 22] = [13 14]
           │    │    │    │    │    ├── lookup columns are key
           │    │    │    │    │    ├── fd: ()-->(13), (14)-->(15-21), (21)-->(14-20), (14)==(22), (22)==(14)
           │    │    │    │    │    ├── project
           │    │    │    │    │    │    ├── columns: "project_const_col_@13":32(oid!null) c:22(int) q:23(int)
           │    │    │    │    │    │    ├── fd: ()-->(32)
           │    │    │    │    │    │    ├── with-scan &1 (updates)
           │    │    │    │    │    │    │    ├── columns: c:22(int) q:23(int)
           │    │    │    │    │    │    │    └── mapping:
           │    │    │    │    │    │    │         ├──  c:2(int) => c:22(int)
           │    │    │    │    │    │    │         └──  q:3(int) => q:23(int)
           │    │    │    │    │    │    └── projections
           │    │    │    │    │    │         └── const: 1 [type=oid]
           │    │    │    │    │    └── filters (true)
           │    │    │    │    └── aggregations
           │    │    │    │         ├── first-agg [type=decimal, outer=(15)]
           │    │    │    │         │    └── variable: buyprice [type=decimal]
           │    │    │    │         ├── first-agg [type=decimal, outer=(16)]
           │    │    │    │         │    └── variable: sellprice [type=decimal]
           │    │    │    │         ├── first-agg [type=decimal, outer=(17)]
           │    │    │    │         │    └── variable: discount [type=decimal]
           │    │    │    │         ├── first-agg [type=int, outer=(18)]
           │    │    │    │         │    └── variable: desiredinventory [type=int]
           │    │    │    │         ├── first-agg [type=int, outer=(19)]
           │    │    │    │         │    └── variable: actualinventory [type=int]
           │    │    │    │         ├── first-agg [type=int, outer=(20)]
           │    │    │    │         │    └── variable: maxinventory [type=int]
           │    │    │    │         ├── first-agg [type=decimal, outer=(21)]
           │    │    │    │         │    └── variable: ci.version [type=decimal]
           │    │    │    │         ├── first-agg [type=int, outer=(22)]
           │    │    │    │         │    └── variable: c [type=int]
           │    │    │    │         ├── first-agg [type=int, outer=(23)]
           │    │    │    │         │    └── variable: q [type=int]
           │    │    │    │         └── const-agg [type=oid, outer=(13)]
           │    │    │    │              └── variable: ci.dealerid [type=oid]
           │    │    │    └── projections
           │    │    │         └── const: 1 [type=oid]
           │    │    └── filters (true)
           │    └── aggregations
           │         ├── sum-int [type=int, outer=(27)]
           │         │    └── variable: quantity [type=int]
           │         ├── const-agg [type=oid, outer=(13)]
           │         │    └── variable: ci.dealerid [type=oid]
           │         ├── const-agg [type=decimal, outer=(15)]
           │         │    └── variable: buyprice [type=decimal]
           │         ├── const-agg [type=decimal, outer=(16)]
           │         │    └── variable: sellprice [type=decimal]
           │         ├── const-agg [type=decimal, outer=(17)]
           │         │    └── variable: discount [type=decimal]
           │         ├── const-agg [type=int, outer=(18)]
           │         │    └── variable: desiredinventory [type=int]
           │         ├── const-agg [type=int, outer=(19)]
           │         │    └── variable: actualinventory [type=int]
           │         ├── const-agg [type=int, outer=(20)]
           │         │    └── variable: maxinventory [type=int]
           │         ├── const-agg [type=decimal, outer=(21)]
           │         │    └── variable: ci.version [type=decimal]
           │         ├── const-agg [type=int, outer=(22)]
           │         │    └── variable: c [type=int]
           │         └── const-agg [type=int, outer=(23)]
           │              └── variable: q [type=int]
           └── projections
                └── COALESCE(sum_int, 0) [type=int, outer=(29)]
