SQLite Forum

short circuiting subqueries with `IN`?
Login
Can I be expect that that my `IN` subquery will short-circuit on a positive match, or will the recursion complete first before testing begins?

(I couldn't spot the answer at https://www.sqlite.org/lang_expr.html or https://www.sqlite.org/optoverview.html)

Thanks!

```sql
SELECT ? IN (
  WITH RECURSIVE
    found(id) AS (
      SELECT self_hash_id
        FROM causal
        WHERE self_hash_id = ?
      UNION ALL
      SELECT parent_id
        FROM causal_parent
        INNER JOIN found ON found.id = causal_id
    )
  SELECT * FROM found
)
```

given

```sql
CREATE TABLE causal (
  self_hash_id INTEGER PRIMARY KEY NOT NULL REFERENCES hash(id),
  value_hash_id INTEGER NOT NULL REFERENCES hash(id)
);
CREATE INDEX causal_value_hash_id ON causal(value_hash_id);

CREATE TABLE causal_parent (
  causal_id INTEGER NOT NULL REFERENCES causal(self_hash_id),
  parent_id INTEGER NOT NULL REFERENCES causal(self_hash_id),
  PRIMARY KEY (causal_id, parent_id)
) WITHOUT ROWID;
CREATE INDEX causal_parent_causal_id ON causal_parent(causal_id);
CREATE INDEX causal_parent_parent_id ON causal_parent(parent_id);

CREATE TABLE hash (
  id INTEGER PRIMARY KEY NOT NULL,
  base32 TEXT NOT NULL
);
```