skip to content

In Room, what does the annotation processor verify about a @Query's SQL, its :name parameters and its return type at build time?

level: middleimportance: should knowfreq 55%

answer

  1. errors at build, not in production
  2. schema built from your entities
  3. named parameters only
  4. IN (:list) expands
  5. QUERY_MISMATCH is a warning

basics

~20 s

Room prepares each @Query against the schema its entities define, so unknown tables or columns and SQL syntax errors fail the build; it binds :name placeholders to same-named parameters and checks result columns against the return type.

solid answer

~40 s

Room's processor builds a verification database from your `@Entity` classes and prepares every `@Query` against it, so a misspelt column, a missing table or broken syntax becomes a **build error** instead of a crash in production. Parameters are named only (`:tag`) and bound to the function parameter with that name; a collection parameter inside `IN (:tags)` is expanded at runtime to one `?` per element. Room also maps the result columns onto the return type: when they do not line up exactly but at least one matches, it reports the `QUERY_MISMATCH` warning and fills what it can. What it cannot check is anything that depends on data or speed, and it checks nothing in a `@RawQuery` or under `@SkipQueryVerification`.

go deeper

for a junior

Recall that Room checks @Query SQL against your entities at build time, and that parameters are written :name.

for a middle

Explain the verification database, how a List parameter expands inside IN, and how result columns map to a data class with QUERY_MISMATCH as the warning.

for a senior

Know the blind spots: data-dependent results, performance, @RawQuery, @SkipQueryVerification and devices whose schema was never migrated, and test for each.

for a principal

Treat warnings such as QUERY_MISMATCH as review signals and decide whether the build should fail on them, so verification stays a gate rather than noise.

## Why build-time checks are Room's case With hand-written SQL over a raw `SQLiteOpenHelper`, a typo in a column name is a string the compiler never looks at. It ships, and it fails on a user's phone the first time that screen runs. Room moves that failure to the build. Its documentation puts it plainly: Room validates SQL queries at compile time, so a problem with a query becomes a compilation error instead of a runtime failure. For many teams that single property is the reason to choose Room. To do it, Room's processor needs SQLite on the build machine. It opens a **verification database** through a SQLite JDBC driver, gives it the tables your entities describe, and prepares each query against it. If that database cannot be created, Room reports the `CANNOT_CREATE_VERIFICATION_DATABASE` warning and carries on without verification, with limited functionality. ## What Room verifies For every `@Query` function the processor works through these checks: 1. **The SQL parses and resolves.** Tables, columns and functions must exist in the schema your entities define. A misspelt `archvied` fails the build. 2. **The statement kind is supported.** `@Query` accepts `SELECT`, `INSERT`, `UPDATE` and `DELETE`. 3. **Every `:name` placeholder has a function parameter of that name.** Room supports only named parameters, precisely so SQL placeholders and function parameters cannot be confused. 4. **The parameter types can be bound,** and the return type fits the statement: `Long` for an `INSERT`, `Int` for an `UPDATE` or `DELETE`, a mappable type for a `SELECT`. 5. **The result columns map onto the return type.** A data class property must find a column of the same name (or of the name its `@ColumnInfo` gives). ## Binding parameters A parameter can be used more than once (`WHERE title LIKE :search OR body LIKE :search`), and several parameters can appear in one query. Values are bound, never concatenated, so a quote inside a tag name cannot change the statement. A **collection parameter** is Room's extension over plain SQL binding: ```kotlin @Query("SELECT * FROM notes WHERE tag IN (:tags)") suspend fun findByTags(tags: List<String>): List<Note> ``` For three tags Room rewrites the statement at runtime to `IN (?, ?, ?)` and binds each element. The number of values you can bind is set by the SQLite driver underneath; Room's documentation gives 999 for a driver over Android's SQLite bindings. ## Result mapping warnings | Situation | What Room reports | |---|---| | Result has extra columns, or the class has unmatched properties, with at least one match | `QUERY_MISMATCH` warning, and the matching properties are filled | | A required property has no column and no default value | build error | | Collection, `Array` or `Optional` return declared nullable | `UNNECESSARY_NULLABILITY_IN_DAO_RETURN_TYPE` warning | | `SELECT *` into a smaller data class | `QUERY_MISMATCH`, unless the function has `@RewriteQueriesToDropUnusedColumns` | `@RewriteQueriesToDropUnusedColumns` makes Room rewrite the query at compile time to select only the columns the return type uses. In Room 2.x the old `CURSOR_MISMATCH` name is deprecated in favour of `QUERY_MISMATCH`. In Room 3.0, a property that has a constructor default value is optional, so a result may omit that column. ## What the checks cannot catch - **Data-dependent behaviour**: a `LIKE` pattern that matches nothing, a tag that does not exist, an empty result where you expected a row. - **Performance**: a query that needs an index or scans the whole table compiles just as well. - **`@RawQuery` functions**, whose SQL exists only at runtime. - **Anything under `@SkipQueryVerification`**, which turns verification off for one function, one DAO or the whole database. Room describes it as a last resort for SQL it cannot understand. - **Schema drift on devices**: the check proves the SQL matches the entities, not that an installed database was migrated to match them; that is the job of Room's migrations. ## Reading a failure When the build stops on a query, the error is reported against the DAO function and describes what the verification database rejected, such as a missing column. Fix the SQL or the entity, not the check: suppressing it with `@SkipQueryVerification` only moves the failure back onto users' phones. Renaming a column is the classic case. Change `@ColumnInfo(name = ...)` on an entity, and every query that still uses the old name stops compiling at once, across every DAO. With hand-written SQL the same rename would be found one crash report at a time, which is exactly the comparison an interviewer wants you to draw.

  • What happens when an IN (:tags) query receives an empty list?
    Room expands the placeholder to match the collection's size, so an empty list leaves `IN ()` with no values, which SQLite accepts and which matches no rows. The query returns an empty list rather than every note, so a "no filter" case needs its own query or a guard in the caller.
  • When is @SkipQueryVerification justified?
    Rarely: when Room's verification cannot understand SQL that does work on the device, for example syntax the build machine's SQLite version does not know. Scope it to the single function, keep a test that runs the query against a real database, and remember Room may then understand less about the result.

  • Room checks only the SQL syntax; column names are checked when the query first runs.
  • Room lets you use positional ? placeholders in @Query just like plain SQLite.
  • A List parameter is bound as one comma-joined string inside IN.
  • A QUERY_MISMATCH warning means the query will crash at runtime.
  • Compile-time verification also proves the query will be fast enough.