SQLite Forum

Discovered bug in xBestIndex of virtual tables
Login
I've discovered that in some circumstances, xBestIndex won't report all constraints on a hidden column. Specifically, it happens when that column is constrained to another hidden column, but only when the query does not involve `JOINs`. Let's look at a series of examples. For all examples, I'll be using the following virtual table declaration:

```
CREATE TABLE vtab(foo HIDDEN, bar HIDDEN, x, y);
```

First, let's look at some examples that behave correctly.

#### Example 1:

In this example, xBestIndex reports two constraints: `"foo"`, `"bar"` (in that order). This is the correct behavior.

```
SELECT vtab.* FROM vtab, other_table WHERE foo = ? AND bar = ?;
```

#### Example 2:

In this example, xBestIndex reports three constraints: `"foo"`, `"bar"`, `"foo"` (in that order). Now, I'm not sure if that really makes sense, considering there's only two constraints in the `WHERE` clause, but I suppose you could argue that `foo = bar` can be reversed to imply a third constraint. I'll just give the benefit of the doubt and say this isn't a bug.

```
SELECT vtab.* FROM vtab, other_table WHERE foo = ? AND bar = foo;
```

#### Example 3:

This example is similar to #1, except we removed `other_table`. This one still behaves correctly. xBestIndex reports two constraints: `"foo"`, `"bar"` (in that order).

```
SELECT * FROM vtab WHERE foo = ? AND bar = ?;
```

#### Example 4:

This example is similar to #2, except we removed `other_table`. This is the case that causes the bug. You would except xBestIndex to report at least two constraints (or maybe three, like in example #2), but instead, only one constraint is reported, which is on `"foo"`. xBestIndex does not report any constraints on `"bar"`.

```
SELECT * FROM vtab WHERE foo = ? AND bar = foo;
```

It would be great if case #4 could be fixed. Thank you!