prql icon indicating copy to clipboard operation
prql copied to clipboard

`text.contains` underspecified: Text or pattern?

Open ftsfranklin opened this issue 8 months ago • 3 comments

What's up?

The text substring search functions (contains, starts_with, ends_with) take a pattern rather than plain text, which may be surprising.

The documentation doesn't specify if it's a substring or a pattern.

Since it doesn't escape special characters, it is treated like a pattern, so wildcards pass through:

  • Input: filter (text.contains '%ne_dle' haystack)
  • Output: AND haystack LIKE CONCAT('%', '%ne_dle', '%')
  • The current behavior uses CONCAT to surround the input with %, even if you give a literal. (Some SQL flavors don't allow three args to CONCAT, such as Oracle before 23, and Redshift.)

What's the intended behavior? It should be documented.

  • If it's intended to be a pattern:
    • For literals, it SHOULD just do LIKE '%ne_dle%', right? This is a minor optimization.
  • If it's meant to be plain text:
    • Escape: LIKE '%ne\_dle%' ESCAPE '\'
      • Transact-SQL: Special characters are %_[].
      • The escape character can be any symbol character which isn't in the string, or else it would need to double existing escape characters in the string.
    • Use INSTR(haystack, 'ne_dle') > 0.
      • Microsoft's Transact: CHARINDEX instead of INSTR.
      • For starts_with, compare to = 1.
      • For ends_with, this may be complicated.
        • INSTR(haystack, 'ne_dle', -1) will search backwards, but the right value is dependent on the lengths of both strings: = LENGTH(haystack) - LENGTH('ne_dle') + 1
        • MySQL doesn't have three-arg INSTR. Instead, it has LOCATE, which can't search from the end of the string, so it will return the first position.
        • SUBSTR(haystack, LENGTH(haystack) - LENGTH('ne_dle') + 1) = 'ne_dle', but may be inefficient by copying.
        • REVERSE turns ends_with into starts_with, but may be inefficient.

If it's supposed to be plain text, it may also be useful to give LIKE as an alternative, such as a text.like function (#1123). I don't know what the project's principle is for handling differences in LIKE syntax.

ftsfranklin avatar Mar 27 '26 19:03 ftsfranklin

text.contains <input> does a simple LIKE %<input>% Logic is simple for now

Image

For ILIKE logic simply add a text.lower

from invoices
filter (
  billing_address | text.lower | text.contains "pl"
)

PrettyWood avatar Jun 15 '26 07:06 PrettyWood

@PrettyWood, maybe the Playground updated since you tried it, but I'm getting a CONCAT wrapper on 0.13.14: Image

Convenient for testing:

#prql target:sql.clickhouse
#prql target:sql.duckdb
#prql target:sql.generic
#prql target:sql.glaredb
#prql target:sql.mysql
#prql target:sql.postgres
#prql target:sql.sqlite  # SQLite uses `||` instead of `CONCAT`
#prql target:sql.mssql
#prql target:sql.ansi
#prql target:sql.bigquery
#prql target:sql.snowflake
#prql target:sql.oracle

from invoices
filter (
  billing_address | text.contains "ace"
)

ftsfranklin avatar Jul 24 '26 19:07 ftsfranklin

Both of these check out, and both now have a PR.

"Text or pattern?" — it's a pattern. text.contains, text.starts_with and text.ends_with all compile to SQL LIKE, and the argument is spliced into the pattern without escaping, so % and _ keep their wildcard meaning. Compiled on 196184a2:

from invoices
filter (billing_address | text.contains "50%_x")
WHERE
  billing_address LIKE CONCAT('%', '50%_x', '%')

The page says only "Returns true if col contains sub", which reads as a literal substring test — that's the underspecification. #6268 adds a section stating the LIKE semantics, that PRQL doesn't escape the metacharacters, and that case sensitivity is the database's collation (so text.lower on both sides, as PrettyWood showed, is the portable case-insensitive form).

The CONCAT wrapper is a real bug, but only on sql.oracle. The generic definition emits LIKE CONCAT('%', sub, '%'), which is fine on every target that takes a three-argument CONCAT. Oracle's CONCAT takes exactly two arguments before 23ai, so on sql.oracle the generated query is rejected outright. #6267 gives Oracle a || override, mirroring the one sql.redshift already has:

-- before:  "billing_address" LIKE CONCAT('%', 'ne_dle', '%')
-- after:   "billing_address" LIKE '%' || 'ne_dle' || '%'

starts_with and ends_with are unaffected — both pass two arguments, which Oracle accepts.

Per-target output for text.contains on 196184a2
target generated predicate
sql.generic, sql.postgres billing_address LIKE CONCAT('%', 'ne_dle', '%')
sql.sqlite, sql.redshift billing_address LIKE '%' || 'ne_dle' || '%'
sql.oracle (before #6267) "billing_address" LIKE CONCAT('%', 'ne_dle', '%') — invalid before 23ai
sql.oracle (after #6267) "billing_address" LIKE '%' || 'ne_dle' || '%'

Reference for the Oracle signature: Oracle 21c SQL Language Reference, CONCAT. Verified against the documented signature and the compiler's output, not against a live Oracle instance — the repo has no Oracle container in test-dbs-external.

Both PRs need a maintainer to merge; leaving this open until they land.

prql-bot avatar Sep 01 '26 08:09 prql-bot