# LogicTest: local
#
# This file tests that we build scalar expressions correctly. We do this by
# putting expressions inside projections and checking that they roundtrip
# correctly.

statement ok
CREATE TABLE t (a INT, b INT, c INT, d INT, j JSONB, s STRING)

query T
EXPLAIN (VERBOSE) SELECT 1 + 2 AS r
----
distribution: local
vectorized: true
·
• values
  columns: (r)
  size: 1 column, 1 row
  row 0, expr 0: 3

query T
EXPLAIN (VERBOSE) SELECT true AS r
----
distribution: local
vectorized: true
·
• values
  columns: (r)
  size: 1 column, 1 row
  row 0, expr 0: true

query T
EXPLAIN (VERBOSE) SELECT false AS r
----
distribution: local
vectorized: true
·
• values
  columns: (r)
  size: 1 column, 1 row
  row 0, expr 0: false

query T
EXPLAIN (VERBOSE) SELECT (1, 2) AS r
----
distribution: local
vectorized: true
·
• values
  columns: (r)
  size: 1 column, 1 row
  row 0, expr 0: (1, 2)

query T
EXPLAIN (VERBOSE) SELECT (true, false) AS r
----
distribution: local
vectorized: true
·
• values
  columns: (r)
  size: 1 column, 1 row
  row 0, expr 0: (true, false)

query T
EXPLAIN (VERBOSE) SELECT 1 + 2 AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: 3
│
└── • scan
      columns: ()
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a + 2 AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: a + 2
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a >= 5 AND b <= 10 AND c < 4 AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: ((a >= 5) AND (b <= 10)) AND (c < 4)
│
└── • scan
      columns: (a, b, c)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a >= 5 OR b <= 10 OR c < 4 AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: ((a >= 5) OR (b <= 10)) OR (c < 4)
│
└── • scan
      columns: (a, b, c)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT NOT (a = 5) AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: a != 5
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT NOT (a > 5 AND b >= 10) AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: (a <= 5) OR (b < 10)
│
└── • scan
      columns: (a, b)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT (a >= 5 AND b <= 10) OR (a <= 10 AND c > 5) AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: ((a >= 5) AND (b <= 10)) OR ((a <= 10) AND (c > 5))
│
└── • scan
      columns: (a, b, c)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT NOT (a >= 5 OR b <= 10) AND NOT (c >= 10) AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: ((a < 5) AND (b > 10)) AND (c < 10)
│
└── • scan
      columns: (a, b, c)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT (a, b) = (1, 2)  AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: (a = 1) AND (b = 2)
│
└── • scan
      columns: (a, b)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a IN (1, 2) AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: a IN (1, 2)
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT (a, b) IN ((1, 2), (3, 4)) AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: (a, b) IN ((1, 2), (3, 4))
│
└── • scan
      columns: (a, b)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT (a, b + c, 5 + d * 2) = (b+c, 8, a - c)  AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: ((a = (b + c)) AND ((b + c) = 8)) AND (((d * 2) + 5) = (a - c))
│
└── • scan
      columns: (a, b, c, d)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT ((a, b), (c, d)) = ((1, 2), (3, 4))  AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: (((a = 1) AND (b = 2)) AND (c = 3)) AND (d = 4)
│
└── • scan
      columns: (a, b, c, d)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT (a, (b, 'a'), (c, 'b', 5)) = (9, (a+c, s), (5, s, a)) AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: (((((a = 9) AND (b = (a + c))) AND (s = 'a')) AND (c = 5)) AND (s = 'b')) AND (a = 5)
│
└── • scan
      columns: (a, b, c, s)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a IS NULL AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: a IS NULL
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a IS NOT DISTINCT FROM NULL AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: a IS NULL
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT (a, b) IS NULL AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: (a, b) IS NULL
│
└── • scan
      columns: (a, b)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT (a, b) IS NOT DISTINCT FROM NULL AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: false
│
└── • scan
      columns: ()
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a IS NOT DISTINCT FROM b AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: a IS NOT DISTINCT FROM b
│
└── • scan
      columns: (a, b)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a IS NOT NULL AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: a IS NOT NULL
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a IS DISTINCT FROM NULL AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: a IS NOT NULL
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT (a, b) IS NOT NULL AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: (a, b) IS NOT NULL
│
└── • scan
      columns: (a, b)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT (a, b) IS DISTINCT FROM NULL AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: true
│
└── • scan
      columns: ()
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a IS DISTINCT FROM b AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: a IS DISTINCT FROM b
│
└── • scan
      columns: (a, b)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT +a + (-b) AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: a + (-b)
│
└── • scan
      columns: (a, b)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT CASE a WHEN 1 THEN 2 WHEN 2 THEN 3 ELSE 4 END AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: CASE a WHEN 1 THEN 2 WHEN 2 THEN 3 ELSE 4 END
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT CASE WHEN a = 2 THEN 1 ELSE 2 END AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: CASE WHEN a = 2 THEN 1 ELSE 2 END
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT CASE a + 3 WHEN 5 * b THEN 1 % b WHEN 6 THEN 2 ELSE -1 END AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: CASE a + 3 WHEN b * 5 THEN 1 % b WHEN 6 THEN 2 ELSE -1 END
│
└── • scan
      columns: (a, b)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

# Tests for CASE with no ELSE statement
query T
EXPLAIN (VERBOSE) SELECT CASE WHEN a = 2 THEN 1 END AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: CASE WHEN a = 2 THEN 1 ELSE CAST(NULL AS INT8) END
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT CASE a WHEN 2 THEN 1 END AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: CASE a WHEN 2 THEN 1 ELSE CAST(NULL AS INT8) END
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

# TODO(radu): IS OF not supported yet.
#query TTTTT
#EXPLAIN (VERBOSE) SELECT a FROM t WHERE a IS OF (INT)
#----
#filter     ·       ·                (a)  ·
# │         filter  t.a IS OF (INT)  ·    ·
# └── scan  ·       ·                (a)  ·
#·          table   t@primary        ·    ·
#·          spans   ALL              ·    ·

query T
EXPLAIN (VERBOSE) SELECT length(s) FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (length)
│ render length: length(s)
│
└── • scan
      columns: (s)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT j @> '{"a": 1}' AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: j @> '{"a": 1}'
│
└── • scan
      columns: (j)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT '{"a": 1}' <@ j AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: '{"a": 1}' <@ j
│
└── • scan
      columns: (j)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT j->>'a' AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: j->>'a'
│
└── • scan
      columns: (j)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT j->'a' AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: j->'a'
│
└── • scan
      columns: (j)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT j ? 'a' AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: j ? 'a'
│
└── • scan
      columns: (j)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT j ?| ARRAY['a', 'b', 'c'] AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: j ?| ARRAY['a','b','c']
│
└── • scan
      columns: (j)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT j ?& ARRAY['a', 'b', 'c'] AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: j ?& ARRAY['a','b','c']
│
└── • scan
      columns: (j)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT j#>ARRAY['a'] AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: j#>ARRAY['a']
│
└── • scan
      columns: (j)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT j#>>ARRAY['a'] AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: j#>>ARRAY['a']
│
└── • scan
      columns: (j)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN


query T
EXPLAIN (VERBOSE) SELECT CAST(a AS string), b::float FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (a, b)
│ render a: a::STRING
│ render b: b::FLOAT8
│
└── • scan
      columns: (a, b)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT CAST(a + b + c AS string) FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (text)
│ render text: (c + (a + b))::STRING
│
└── • scan
      columns: (a, b, c)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT s::VARCHAR(2) FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (s)
│ render s: s::VARCHAR(2)
│
└── • scan
      columns: (s)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT COALESCE(a, b) FROM (VALUES (1, 2), (3, NULL), (NULL, 4), (NULL, NULL)) AS v(a, b)
----
distribution: local
vectorized: true
·
• render
│ columns: ("coalesce")
│ render coalesce: COALESCE(column1, column2)
│
└── • values
      columns: (column1, column2)
      size: 2 columns, 4 rows
      row 0, expr 0: 1
      row 0, expr 1: 2
      row 1, expr 0: 3
      row 1, expr 1: CAST(NULL AS INT8)
      row 2, expr 0: CAST(NULL AS INT8)
      row 2, expr 1: 4
      row 3, expr 0: CAST(NULL AS INT8)
      row 3, expr 1: CAST(NULL AS INT8)

query T
EXPLAIN (VERBOSE) SELECT COALESCE(a, b, c) FROM (VALUES (1, 2, 3), (NULL, 4, 5), (NULL, NULL, 6), (NULL, NULL, NULL)) AS v(a, b, c)
----
distribution: local
vectorized: true
·
• render
│ columns: ("coalesce")
│ render coalesce: COALESCE(column1, column2, column3)
│
└── • values
      columns: (column1, column2, column3)
      size: 3 columns, 4 rows
      row 0, expr 0: 1
      row 0, expr 1: 2
      row 0, expr 2: 3
      row 1, expr 0: CAST(NULL AS INT8)
      row 1, expr 1: 4
      row 1, expr 2: 5
      row 2, expr 0: CAST(NULL AS INT8)
      row 2, expr 1: CAST(NULL AS INT8)
      row 2, expr 2: 6
      row 3, expr 0: CAST(NULL AS INT8)
      row 3, expr 1: CAST(NULL AS INT8)
      row 3, expr 2: CAST(NULL AS INT8)

query T
EXPLAIN (VERBOSE) SELECT a FROM t WHERE a BETWEEN b AND d
----
distribution: local
vectorized: true
·
• project
│ columns: (a)
│
└── • filter
    │ columns: (a, b, d)
    │ estimated row count: 110 (missing stats)
    │ filter: (a >= b) AND (a <= d)
    │
    └── • scan
          columns: (a, b, d)
          estimated row count: 1,000 (missing stats)
          table: t@t_pkey
          spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a FROM t WHERE a NOT BETWEEN b AND d
----
distribution: local
vectorized: true
·
• project
│ columns: (a)
│
└── • filter
    │ columns: (a, b, d)
    │ estimated row count: 330 (missing stats)
    │ filter: (a < b) OR (a > d)
    │
    └── • scan
          columns: (a, b, d)
          estimated row count: 1,000 (missing stats)
          table: t@t_pkey
          spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a BETWEEN SYMMETRIC b AND d AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: ((a >= b) AND (a <= d)) OR ((a >= d) AND (a <= b))
│
└── • scan
      columns: (a, b, d)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT a NOT BETWEEN SYMMETRIC b AND d AS r FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r)
│ render r: ((a < b) OR (a > d)) AND ((a < d) OR (a > b))
│
└── • scan
      columns: (a, b, d)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT ARRAY[]:::int[] FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: ("array")
│ render array: ARRAY[]
│
└── • scan
      columns: ()
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT ARRAY[1, 2, 3] FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: ("array")
│ render array: ARRAY[1,2,3]
│
└── • scan
      columns: ()
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT ARRAY[a + 1, 2, 3] FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: ("array")
│ render array: ARRAY[a + 1, 2, 3]
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT 1 > ANY ARRAY[a + 1, 2, 3] FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: ("?column?")
│ render ?column?: 1 > ANY ARRAY[a + 1, 2, 3]
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT 1 = ANY (1, 2, 3) FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: ("?column?")
│ render ?column?: true
│
└── • scan
      columns: ()
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT 1 = ANY () FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: ("?column?")
│ render ?column?: false
│
└── • scan
      columns: ()
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT least(NULL, greatest(NULL, least(1, NULL), 2, 3), greatest(5, 6), a) FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: ("least")
│ render least: least(NULL, 3, 6, a)
│
└── • scan
      columns: (a)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN



query T
EXPLAIN (VERBOSE) SELECT * FROM pg_attribute WHERE attrelid='t'::regclass
----
distribution: local
vectorized: true
·
• virtual table
  columns: (attrelid, attname, atttypid, attstattarget, attlen, attnum, attndims, attcacheoff, atttypmod, attbyval, attstorage, attalign, attnotnull, atthasdef, attidentity, attgenerated, attisdropped, attislocal, attinhcount, attcollation, attacl, attoptions, attfdwoptions, atthasmissing, attmissingval, attishidden)
  estimated row count: 10 (missing stats)
  table: pg_attribute@pg_attribute_attrelid_idx
  spans: [/t - /t]

query T
EXPLAIN (VERBOSE) SELECT * FROM pg_proc WHERE oid = 1::regproc
----
distribution: local
vectorized: true
·
• virtual table
  columns: (oid, proname, pronamespace, proowner, prolang, procost, prorows, provariadic, prosupport, prokind, prosecdef, proleakproof, proisstrict, proretset, provolatile, proparallel, pronargs, pronargdefaults, prorettype, proargtypes, proallargtypes, proargmodes, proargnames, proargdefaults, protrftypes, prosrc, probin, prosqlbody, proconfig, proacl)
  estimated row count: 10 (missing stats)
  table: pg_proc@pg_proc_oid_idx
  spans: [/array_agg - /array_agg]

query T
EXPLAIN (VERBOSE) SELECT * FROM pg_proc WHERE oid = '12345'::regprocedure
----
distribution: local
vectorized: true
·
• virtual table
  columns: (oid, proname, pronamespace, proowner, prolang, procost, prorows, provariadic, prosupport, prokind, prosecdef, proleakproof, proisstrict, proretset, provolatile, proparallel, pronargs, pronargdefaults, prorettype, proargtypes, proallargtypes, proargmodes, proargnames, proargdefaults, protrftypes, prosrc, probin, prosqlbody, proconfig, proacl)
  estimated row count: 10 (missing stats)
  table: pg_proc@pg_proc_oid_idx
  spans: [/12345 - /12345]


query T
EXPLAIN (VERBOSE) select * from pg_class where oid = 't'::regclass
----
distribution: local
vectorized: true
·
• virtual table
  columns: (oid, relname, relnamespace, reltype, reloftype, relowner, relam, relfilenode, reltablespace, relpages, reltuples, relallvisible, reltoastrelid, relhasindex, relisshared, relpersistence, relistemp, relkind, relnatts, relchecks, relhasoids, relhaspkey, relhasrules, relhastriggers, relhassubclass, relfrozenxid, relacl, reloptions, relforcerowsecurity, relispartition, relispopulated, relreplident, relrewrite, relrowsecurity, relpartbound, relminmxid)
  estimated row count: 10 (missing stats)
  table: pg_class@pg_class_oid_idx
  spans: [/t - /t]

query T
EXPLAIN (VERBOSE) SELECT * FROM pg_class WHERE oid = '3'::OID
----
distribution: local
vectorized: true
·
• virtual table
  columns: (oid, relname, relnamespace, reltype, reloftype, relowner, relam, relfilenode, reltablespace, relpages, reltuples, relallvisible, reltoastrelid, relhasindex, relisshared, relpersistence, relistemp, relkind, relnatts, relchecks, relhasoids, relhaspkey, relhasrules, relhastriggers, relhassubclass, relfrozenxid, relacl, reloptions, relforcerowsecurity, relispartition, relispopulated, relreplident, relrewrite, relrowsecurity, relpartbound, relminmxid)
  estimated row count: 10 (missing stats)
  table: pg_class@pg_class_oid_idx
  spans: [/3 - /3]

query T
EXPLAIN (VERBOSE) SELECT * FROM pg_class WHERE oid = 't'::regclass::oid
----
distribution: local
vectorized: true
·
• virtual table
  columns: (oid, relname, relnamespace, reltype, reloftype, relowner, relam, relfilenode, reltablespace, relpages, reltuples, relallvisible, reltoastrelid, relhasindex, relisshared, relpersistence, relistemp, relkind, relnatts, relchecks, relhasoids, relhaspkey, relhasrules, relhastriggers, relhassubclass, relfrozenxid, relacl, reloptions, relforcerowsecurity, relispartition, relispopulated, relreplident, relrewrite, relrowsecurity, relpartbound, relminmxid)
  estimated row count: 10 (missing stats)
  table: pg_class@pg_class_oid_idx
  spans: [/106 - /106]

query T
EXPLAIN (VERBOSE) SELECT * FROM pg_class WHERE oid = 101::oid
----
distribution: local
vectorized: true
·
• virtual table
  columns: (oid, relname, relnamespace, reltype, reloftype, relowner, relam, relfilenode, reltablespace, relpages, reltuples, relallvisible, reltoastrelid, relhasindex, relisshared, relpersistence, relistemp, relkind, relnatts, relchecks, relhasoids, relhaspkey, relhasrules, relhastriggers, relhassubclass, relfrozenxid, relacl, reloptions, relforcerowsecurity, relispartition, relispopulated, relreplident, relrewrite, relrowsecurity, relpartbound, relminmxid)
  estimated row count: 10 (missing stats)
  table: pg_class@pg_class_oid_idx
  spans: [/101 - /101]

statement ok
CREATE TYPE status AS ENUM ('open', 'closed', 'inactive');

query T
EXPLAIN (VERBOSE) SELECT * FROM pg_type WHERE oid = 'status'::regtype
----
distribution: local
vectorized: true
·
• virtual table
  columns: (oid, typname, typnamespace, typowner, typlen, typbyval, typtype, typcategory, typispreferred, typisdefined, typdelim, typrelid, typelem, typarray, typinput, typoutput, typreceive, typsend, typmodin, typmodout, typanalyze, typalign, typstorage, typnotnull, typbasetype, typtypmod, typndims, typcollation, typdefaultbin, typdefault, typacl)
  estimated row count: 10 (missing stats)
  table: pg_type@pg_type_oid_idx
  spans: [/status - /status]

query T
EXPLAIN (VERBOSE) SELECT * FROM pg_type WHERE oid = 'bool'::regtype
----
distribution: local
vectorized: true
·
• virtual table
  columns: (oid, typname, typnamespace, typowner, typlen, typbyval, typtype, typcategory, typispreferred, typisdefined, typdelim, typrelid, typelem, typarray, typinput, typoutput, typreceive, typsend, typmodin, typmodout, typanalyze, typalign, typstorage, typnotnull, typbasetype, typtypmod, typndims, typcollation, typdefaultbin, typdefault, typacl)
  estimated row count: 10 (missing stats)
  table: pg_type@pg_type_oid_idx
  spans: [/boolean - /boolean]

query T
EXPLAIN (VERBOSE) SELECT * FROM pg_type WHERE oid = 'bool[]'::regtype
----
distribution: local
vectorized: true
·
• virtual table
  columns: (oid, typname, typnamespace, typowner, typlen, typbyval, typtype, typcategory, typispreferred, typisdefined, typdelim, typrelid, typelem, typarray, typinput, typoutput, typreceive, typsend, typmodin, typmodout, typanalyze, typalign, typstorage, typnotnull, typbasetype, typtypmod, typndims, typcollation, typdefaultbin, typdefault, typacl)
  estimated row count: 10 (missing stats)
  table: pg_type@pg_type_oid_idx
  spans: [/boolean[] - /boolean[]]

query T
EXPLAIN (VERBOSE) SELECT * FROM pg_type WHERE oid = 1::regtype
----
distribution: local
vectorized: true
·
• virtual table
  columns: (oid, typname, typnamespace, typowner, typlen, typbyval, typtype, typcategory, typispreferred, typisdefined, typdelim, typrelid, typelem, typarray, typinput, typoutput, typreceive, typsend, typmodin, typmodout, typanalyze, typalign, typstorage, typnotnull, typbasetype, typtypmod, typndims, typcollation, typdefaultbin, typdefault, typacl)
  estimated row count: 10 (missing stats)
  table: pg_type@pg_type_oid_idx
  spans: [/1 - /1]

query T
EXPLAIN (VERBOSE) SELECT * FROM pg_type WHERE oid = 1::regtype::oid
----
distribution: local
vectorized: true
·
• virtual table
  columns: (oid, typname, typnamespace, typowner, typlen, typbyval, typtype, typcategory, typispreferred, typisdefined, typdelim, typrelid, typelem, typarray, typinput, typoutput, typreceive, typsend, typmodin, typmodout, typanalyze, typalign, typstorage, typnotnull, typbasetype, typtypmod, typndims, typcollation, typdefaultbin, typdefault, typacl)
  estimated row count: 10 (missing stats)
  table: pg_type@pg_type_oid_idx
  spans: [/1 - /1]

query T
EXPLAIN (VERBOSE) SELECT * FROM pg_namespace WHERE oid = 'public'::regnamespace
----
distribution: local
vectorized: true
·
• virtual table
  columns: (oid, nspname, nspowner, nspacl)
  estimated row count: 10 (missing stats)
  table: pg_namespace@pg_namespace_oid_idx
  spans: [/public - /public]

query T
EXPLAIN (VERBOSE) SELECT CASE WHEN current_database() = 'test' THEN 42 ELSE 1/3 END
----
distribution: local
vectorized: true
·
• values
  columns: ("case")
  size: 1 column, 1 row
  row 0, expr 0: 42

# Don't fold random(), but fold current_database().
query T
EXPLAIN (VERBOSE) SELECT random(), current_database()
----
distribution: local
vectorized: true
·
• values
  columns: (random, current_database)
  size: 2 columns, 1 row
  row 0, expr 0: random()
  row 0, expr 1: 'test'

# Don't fold non-constants.
query T
EXPLAIN (VERBOSE) SELECT 1::FLOAT + length(upper(concat('a', 'b', 'c')))::FLOAT AS r1,
                         1::FLOAT + length(upper(concat('a', 'b', s)))::FLOAT AS r2 FROM t
----
distribution: local
vectorized: true
·
• render
│ columns: (r1, r2)
│ render r1: 4.0
│ render r2: length(upper(concat('a', 'b', s)))::FLOAT8 + 1.0
│
└── • scan
      columns: (s)
      estimated row count: 1,000 (missing stats)
      table: t@t_pkey
      spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT ARRAY(SELECT generate_series(1,10) ORDER BY 1 DESC)
----
distribution: local
vectorized: true
·
• root
│ columns: ("array")
│
├── • values
│     columns: ("array")
│     size: 1 column, 1 row
│     row 0, expr 0: ARRAY @S1
│
└── • subquery
    │ id: @S1
    │ original sql: (SELECT generate_series(1, 10) ORDER BY 1 DESC)
    │ exec mode: all rows
    │
    └── • sort
        │ columns: (generate_series)
        │ estimated row count: 10
        │ order: -generate_series
        │
        └── • project set
            │ columns: (generate_series)
            │ estimated row count: 10
            │ render 0: generate_series(1, 10)
            │
            └── • emptyrow
                  columns: ()

query T
EXPLAIN (VERBOSE) SELECT ARRAY(SELECT a FROM t ORDER BY b)
----
distribution: local
vectorized: true
·
• root
│ columns: ("array")
│
├── • values
│     columns: ("array")
│     size: 1 column, 1 row
│     row 0, expr 0: ARRAY @S1
│
└── • subquery
    │ id: @S1
    │ original sql: (SELECT a FROM t ORDER BY b)
    │ exec mode: all rows
    │
    └── • project
        │ columns: (a)
        │
        └── • sort
            │ columns: (a, b)
            │ estimated row count: 1,000 (missing stats)
            │ order: +b
            │
            └── • scan
                  columns: (a, b)
                  estimated row count: 1,000 (missing stats)
                  table: t@t_pkey
                  spans: FULL SCAN

# Regression test for #47327. The span should have an end value of -1.
statement ok
CREATE TABLE t0(c0 DECIMAL UNIQUE); INSERT INTO t0(c0) VALUES(0);

query T
EXPLAIN (VERBOSE) SELECT t0.c0 FROM t0 WHERE t0.c0 BETWEEN t0.c0 AND INTERVAL '-1'::DECIMAL
----
distribution: local
vectorized: true
·
• scan
  columns: (c0)
  estimated row count: 330 (missing stats)
  table: t0@t0_c0_key
  spans: /!NULL-/-1/PrefixEnd

# Regression test for #57959. This should not cause an error due to
# "comparison overload not found".
query error pq: incompatible value type: cannot determine type of empty array. Consider casting to the desired type, for example ARRAY[]::int[]
EXPLAIN (VERBOSE) SELECT * FROM t0 WHERE (CASE WHEN (t0.c0) IN (t0.c0) THEN ARRAY[NULL] ELSE ARRAY[] END) IS NULL

# TODO(rytaft): On Postgres this case returns ERROR: cannot determine type of
# empty array.
query T
EXPLAIN (VERBOSE) SELECT * FROM t0 WHERE (CASE WHEN (t0.c0) IN (t0.c0) THEN ARRAY[] ELSE ARRAY[NULL] END) IS NULL
----
distribution: local
vectorized: true
·
• filter
│ columns: (c0)
│ estimated row count: 333 (missing stats)
│ filter: CASE WHEN (c0 IS DISTINCT FROM CAST(NULL AS DECIMAL)) OR CAST(NULL AS BOOL) THEN ARRAY[] ELSE ARRAY[NULL] END IS NULL
│
└── • scan
      columns: (c0)
      estimated row count: 1,000 (missing stats)
      table: t0@t0_pkey
      spans: FULL SCAN

# Regression tests for not checking whether arguments to a binary op are
# non-NULL when folding (#94264).
query T
EXPLAIN SELECT 2-(9223372036854775807+436256318) < (CASE WHEN false THEN -1 END)
----
distribution: local
vectorized: true
·
• values
  size: 1 column, 1 row

query T
EXPLAIN SELECT (9223372036854775807+436256318)-2 < (CASE WHEN false THEN -1 END)
----
distribution: local
vectorized: true
·
• values
  size: 1 column, 1 row

query T
EXPLAIN SELECT (9223372036854775807+436256318)+2 < (CASE WHEN false THEN -1 END)
----
distribution: local
vectorized: true
·
• values
  size: 1 column, 1 row
