Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/nice-months-nail.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'@tanstack/db': patch
---

Fix like/ilike `%` and `_` not matching newline characters
1 change: 1 addition & 0 deletions packages/db-collection-e2e/src/fixtures/seed-data.ts
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,7 @@ export function generateSeedData(): SeedDataResult {
`Rose ${i}`,
`sam ${i}`,
`Tina ${i}`,
`Ursula ${i}\nNewline`,
]
const name = names[i % names.length]

Expand Down
21 changes: 21 additions & 0 deletions packages/db-collection-e2e/src/suites/predicates.suite.ts
Original file line number Diff line number Diff line change
Expand Up @@ -412,6 +412,27 @@ export function createPredicatesTestSuite(
await query.cleanup()
})

it(`should filter with like() with wildcard pattern matching newline`, async () => {
const config = await getConfig()
const usersCollection = config.collections.onDemand.users

const query = createLiveQueryCollection((q) =>
q
.from({ user: usersCollection })
.where(({ user }) => like(user.name, `Ursula%`)),
)

await query.preload()
await waitForQueryData(query, { minSize: 1 })

const results = Array.from(query.state.values())
expect(results.length).toBeGreaterThan(0)
// should match names starting with "Ursula" even if it contains a newline character
assertAllItemsMatch(query, (u) => u.name.startsWith(`Ursula`))

await query.cleanup()
})

it(`should filter with like() with lower() function`, async () => {
const config = await getConfig()
const usersCollection = config.collections.onDemand.users
Expand Down
3 changes: 2 additions & 1 deletion packages/db/src/query/compiler/evaluators.ts
Original file line number Diff line number Diff line change
Expand Up @@ -516,6 +516,7 @@ function evaluateLike(
regexPattern = regexPattern.replace(/%/g, `.*`) // % matches any sequence
regexPattern = regexPattern.replace(/_/g, `.`) // _ matches any single char

const regex = new RegExp(`^${regexPattern}$`)
// 's' (dotAll flag) makes '.' match all characters including line terminations
const regex = new RegExp(`^${regexPattern}$`, 's')
return regex.test(searchValue)
}
17 changes: 17 additions & 0 deletions packages/db/tests/query/compiler/evaluators.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,23 @@ describe(`evaluators`, () => {
// In 3-valued logic, ilike with undefined pattern returns UNKNOWN (null)
expect(compiled({})).toBe(null)
})

it(`like % wildcard matches across newline characters`, () => {
const func = new Func(`like`, [
new Value(`hello\nworld`),
new Value(`%world`),
])
const compiled = compileExpression(func)

expect(compiled({})).toBe(true)
})

it(`like _ wildcard matches a newline character`, () => {
const func = new Func(`like`, [new Value(`a\nb`), new Value(`a_b`)])
const compiled = compileExpression(func)

expect(compiled({})).toBe(true)
})
})

describe(`comparison operators`, () => {
Expand Down
2 changes: 1 addition & 1 deletion packages/query-db-collection/e2e/query-filter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -556,7 +556,7 @@ function evaluateLike(
regexPattern = regexPattern.replace(/%/g, `.*`) // % matches any sequence
regexPattern = regexPattern.replace(/_/g, `.`) // _ matches any single char

const regex = new RegExp(`^${regexPattern}$`)
const regex = new RegExp(`^${regexPattern}$`, 's')
return regex.test(searchValue)
}

Expand Down
Loading