# LogicTest: local

statement ok
CREATE TABLE foo (x CHAR PRIMARY KEY); INSERT INTO foo(x) VALUES ('a'), ('b')

query T
EXPLAIN (VERBOSE) SELECT max(ordinality) FROM foo WITH ORDINALITY
----
distribution: local
vectorized: true
·
• group (scalar)
│ columns: (max)
│ estimated row count: 1 (missing stats)
│ aggregate 0: max(ordinality)
│
└── • ordinality
    │ columns: ("ordinality")
    │ estimated row count: 1,000 (missing stats)
    │
    └── • scan
          columns: ()
          estimated row count: 1,000 (missing stats)
          table: foo@foo_pkey
          spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT * FROM foo WITH ORDINALITY WHERE ordinality > 1 ORDER BY ordinality
----
distribution: local
vectorized: true
·
• filter
│ columns: (x, "ordinality")
│ ordering: +"ordinality"
│ estimated row count: 333 (missing stats)
│ filter: "ordinality" > 1
│
└── • ordinality
    │ columns: (x, "ordinality")
    │ estimated row count: 1,000 (missing stats)
    │
    └── • scan
          columns: (x)
          estimated row count: 1,000 (missing stats)
          table: foo@foo_pkey
          spans: FULL SCAN

query T
EXPLAIN (VERBOSE) SELECT * FROM foo WITH ORDINALITY WHERE ordinality > 1 ORDER BY ordinality DESC
----
distribution: local
vectorized: true
·
• sort
│ columns: (x, "ordinality")
│ estimated row count: 333 (missing stats)
│ order: -"ordinality"
│
└── • filter
    │ columns: (x, "ordinality")
    │ estimated row count: 333 (missing stats)
    │ filter: "ordinality" > 1
    │
    └── • ordinality
        │ columns: (x, "ordinality")
        │ estimated row count: 1,000 (missing stats)
        │
        └── • scan
              columns: (x)
              estimated row count: 1,000 (missing stats)
              table: foo@foo_pkey
              spans: FULL SCAN

# Show that the primary key is used under ordinalityNode.
query T
EXPLAIN (VERBOSE) SELECT * FROM (SELECT * FROM foo WHERE x > 'a') WITH ORDINALITY
----
distribution: local
vectorized: true
·
• ordinality
│ columns: (x, "ordinality")
│ estimated row count: 333 (missing stats)
│
└── • scan
      columns: (x)
      estimated row count: 333 (missing stats)
      table: foo@foo_pkey
      spans: /"a\x00"-

# Show that the primary key cannot be used with a PK predicate
# outside of ordinalityNode.
query T
EXPLAIN (VERBOSE) SELECT * FROM foo WITH ORDINALITY WHERE x > 'a'
----
distribution: local
vectorized: true
·
• filter
│ columns: (x, "ordinality")
│ estimated row count: 333 (missing stats)
│ filter: x > 'a'
│
└── • ordinality
    │ columns: (x, "ordinality")
    │ estimated row count: 1,000 (missing stats)
    │
    └── • scan
          columns: (x)
          estimated row count: 1,000 (missing stats)
          table: foo@foo_pkey
          spans: FULL SCAN
