# LogicTest: local

statement ok
CREATE TABLE a (
  a INT PRIMARY KEY,
  b TEXT,
  FAMILY (a,b),
  INVERTED INDEX(b gin_trgm_ops)
)

query T
EXPLAIN SELECT * FROM a WHERE b LIKE '%foo%'
----
distribution: local
vectorized: true
·
• filter
│ filter: b LIKE '%foo%'
│
└── • index join
    │ table: a@a_pkey
    │
    └── • scan
          missing stats
          table: a@a_b_idx
          spans: 1 span

query T
EXPLAIN SELECT * FROM a WHERE b ILIKE '%foo%'
----
distribution: local
vectorized: true
·
• filter
│ filter: b ILIKE '%foo%'
│
└── • index join
    │ table: a@a_pkey
    │
    └── • scan
          missing stats
          table: a@a_b_idx
          spans: 1 span

query T
EXPLAIN SELECT * FROM a WHERE b LIKE '%foo%' OR b ILIKE '%bar%'
----
distribution: local
vectorized: true
·
• filter
│ filter: (b LIKE '%foo%') OR (b ILIKE '%bar%')
│
└── • index join
    │ table: a@a_pkey
    │
    └── • inverted filter
        │ inverted column: b_inverted_key
        │ num spans: 2
        │
        └── • scan
              missing stats
              table: a@a_b_idx
              spans: 2 spans

query T
EXPLAIN SELECT * FROM a WHERE b LIKE '%foo%' OR b ILIKE '%bar%'
----
distribution: local
vectorized: true
·
• filter
│ filter: (b LIKE '%foo%') OR (b ILIKE '%bar%')
│
└── • index join
    │ table: a@a_pkey
    │
    └── • inverted filter
        │ inverted column: b_inverted_key
        │ num spans: 2
        │
        └── • scan
              missing stats
              table: a@a_b_idx
              spans: 2 spans

query T
EXPLAIN SELECT * FROM a WHERE b LIKE '%foo%zoo%'
----
distribution: local
vectorized: true
·
• filter
│ filter: b LIKE '%foo%zoo%'
│
└── • index join
    │ table: a@a_pkey
    │
    └── • inverted filter
        │ inverted column: b_inverted_key
        │ num spans: 2
        │
        └── • scan
              missing stats
              table: a@a_b_idx
              spans: 2 spans

# Test that trigram index can't support searches with fewer than 3 characters.
query T
EXPLAIN SELECT * FROM a WHERE b LIKE '%fo'
----
distribution: local
vectorized: true
·
• filter
│ filter: b LIKE '%fo'
│
└── • scan
      missing stats
      table: a@a_pkey
      spans: FULL SCAN

# Test that trigram indexes can't support searches with no constant args.
# columns.
query T
EXPLAIN SELECT * FROM a WHERE b LIKE b
----
distribution: local
vectorized: true
·
• filter
│ filter: b LIKE b
│
└── • scan
      missing stats
      table: a@a_pkey
      spans: FULL SCAN

# Test that trigram indexes accelerate the % operator.
query T
EXPLAIN SELECT * FROM a WHERE b % 'foo'
----
distribution: local
vectorized: true
·
• filter
│ filter: b % 'foo'
│
└── • index join
    │ table: a@a_pkey
    │
    └── • distinct
        │ distinct on: a
        │
        └── • scan
              missing stats
              table: a@a_b_idx
              spans: [/" fo" - /" fo"] [/"foo" - /"foo"] [/"oo " - /"oo "]

# Test that trigram indexes accelerate the % operator with an OR if the
# constant has more than one trigram.
query T
EXPLAIN SELECT * FROM a WHERE b % 'foob'
----
distribution: local
vectorized: true
·
• filter
│ filter: b % 'foob'
│
└── • index join
    │ table: a@a_pkey
    │
    └── • distinct
        │ distinct on: a
        │
        └── • scan
              missing stats
              table: a@a_b_idx
              spans: [/" fo" - /" fo"] [/"foo" - /"foo"] [/"ob " - /"ob "] [/"oob" - /"oob"]

# Test that trigram indexes can accelerate the % operator if there are fewer
# than 3 characters in the constant by using padded trigrams.
query T
EXPLAIN SELECT * FROM a WHERE b % 'fo'
----
distribution: local
vectorized: true
·
• filter
│ filter: b % 'fo'
│
└── • index join
    │ table: a@a_pkey
    │
    └── • distinct
        │ distinct on: a
        │
        └── • scan
              missing stats
              table: a@a_b_idx
              spans: [/"  f" - /"  f"] [/" fo" - /" fo"] [/"fo " - /"fo "]

# Test that trigram indexes can accelerate the % operator in reverse order.
query T
EXPLAIN SELECT * FROM a WHERE 'blah' % b
----
distribution: local
vectorized: true
·
• filter
│ filter: 'blah' % b
│
└── • index join
    │ table: a@a_pkey
    │
    └── • distinct
        │ distinct on: a
        │
        └── • scan
              missing stats
              table: a@a_b_idx
              spans: [/" bl" - /" bl"] [/"ah " - /"ah "] [/"bla" - /"bla"] [/"lah" - /"lah"]

# Test that trigram indexes can't accelerate the % operator with no constant
# columns.
query T
EXPLAIN SELECT * FROM a WHERE b % b
----
distribution: local
vectorized: true
·
• filter
│ filter: b % b
│
└── • scan
      missing stats
      table: a@a_pkey
      spans: FULL SCAN


# Test that trigram indexes can accelerate the equality operator.
query T
EXPLAIN SELECT * FROM a WHERE b = 'foobar'
----
distribution: local
vectorized: true
·
• filter
│ filter: b = 'foobar'
│
└── • index join
    │ table: a@a_pkey
    │
    └── • inverted filter
        │ inverted column: b_inverted_key
        │ num spans: 4
        │
        └── • scan
              missing stats
              table: a@a_b_idx
              spans: 4 spans

query T
EXPLAIN SELECT * FROM a WHERE b = 'foo'
----
distribution: local
vectorized: true
·
• filter
│ filter: b = 'foo'
│
└── • index join
    │ table: a@a_pkey
    │
    └── • scan
          missing stats
          table: a@a_b_idx
          spans: 1 span

# Test that trigram indexes accelerate even when the datatype is non-String.
statement ok
CREATE TABLE b (
  a VARCHAR,
  INVERTED INDEX(a gin_trgm_ops)
)

query T
EXPLAIN SELECT * FROM b WHERE a % 'foob'
----
distribution: local
vectorized: true
·
• filter
│ filter: a % 'foob'
│
└── • index join
    │ table: b@b_pkey
    │
    └── • distinct
        │ distinct on: rowid
        │
        └── • scan
              missing stats
              table: b@b_a_idx
              spans: [/" fo" - /" fo"] [/"foo" - /"foo"] [/"ob " - /"ob "] [/"oob" - /"oob"]

# Regression test for #88925.
statement ok
CREATE TABLE t88925 (
  a INT PRIMARY KEY,
  b TEXT NOT NULL,
  INVERTED INDEX t88925_b_idx (b gin_trgm_ops)
)

# The secondary index cannot be used because LIKE is not commutative.
statement error pgcode 42809 index "t88925_b_idx" is inverted and cannot be used for this query
SELECT * FROM t88925@t88925_b_idx WHERE 'aab' LIKE b

# The secondary index cannot be used because ILIKE is not commutative.
statement error pgcode 42809 index "t88925_b_idx" is inverted and cannot be used for this query
SELECT * FROM t88925@t88925_b_idx WHERE 'aab' ILIKE b
