Skip to content

HCK-18354: escape index key names in the reverse-engineering WHERE clause - #75

Merged
serhii-filonenko merged 1 commit into
developfrom
fix/re-escape-index-key-in-where
Oct 29, 2026
Merged

serhii-filonenko merged 1 commit into
developfrom
fix/re-escape-index-key-in-where

Conversation

@Sylvie-P

@Sylvie-P Sylvie-P commented Oct 29, 2026 •

Copy link
Copy Markdown
Contributor
Sub-bugHCK-18354 [Couchbase V7+][Delta model] RE of the model after delta model apply is failed because of the spaces

Jira: [HCK-18354](https://hackolade.atlassian.net/browse/HCK-18354)

Problem

Reverse-engineering a collection that has no primary index imports nothing when its first index is on a field whose name has a space (like New field1). The log shows:

syntax error - line 1, column 110, near 'el-sample` WHERE New', at: field1

It was found after applying an alter script that drops the primary index (HCK-18345).

Why

Without a primary index, Couchbase can't read all documents of a collection. So the plugin adds a WHERE clause on the leading key of an index, which lets Couchbase use that index. The key name was put in the query without backticks: WHERE New field1 LIKE '%'. Couchbase requires backticks around any field name that is not a simple word, so the query failed. The plugin then got 0 documents and skipped the collection as empty.

The same happens with other characters. A hyphen is common (first-name) and is worse, because there is no error: first-name is read as "first minus name", and the query silently returns nothing.

Also, if the first index of the collection was an Array, Metadata or function-only index, it has no simple key and the code crashed the same way.

Fix

In reverse_engineering/helpers/queryHelper.js:

  • The key name is wrapped in backticks, each part of a nested path separately: `New field1`, `address`.`zip code`.
  • The plugin takes the first leading key it finds among the indexes, and skips the ones without a simple key.
  • If no index has a simple key, there is no WHERE clause. Couchbase then refuses the query for lack of index, and the plugin uses INFER, which was already the fallback for this case.
  • With a primary index, nothing changes: there is no WHERE clause.

Limit

A field whose name really contains a dot would be split by mistake. The plugin already loses this information when it reads the index keys (it removes the backticks). It's rare, and would be a separate ticket.

Tests

  • npm test: 157 passed. New test/reverse_engineering/queryHelper.test.js: space, hyphen, nested path, leading key, indexes without simple key, primary index, and an index read from system:indexes with the key `New field1`.
  • 7 of the 9 new tests fail on the previous code.
  • Not tested against a real instance yet.

…ause

Without a primary index, the plugin reads documents with a WHERE clause on
the leading key of an index. The key name was not escaped, so a field name
with a space (like "New field1") made the query fail with a syntax error,
and the collection was skipped as empty.

Key names are now wrapped in backticks, each part of a nested path
separately. Indexes without a simple key (Array, Metadata, function only)
are skipped instead of crashing.

Co-authored-by: Cursor <cursoragent@cursor.com>
@Sylvie-P Sylvie-P self-assigned this Oct 29, 2026
@sonarqubecloud

Copy link
Copy Markdown

@serhii-filonenko
serhii-filonenko enabled auto-merge (squash) October 29, 2026 13:26
@serhii-filonenko
serhii-filonenko merged commit 9e13751 into develop Oct 29, 2026
3 checks passed
@serhii-filonenko
serhii-filonenko deleted the fix/re-escape-index-key-in-where branch October 29, 2026 14:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants