Exercise 2: An Unindexed Column Always Falls Back to a Scan — Possible Solution ==================================================================== THE TEST ------------------------------ choose_plan(('name', '=', 'user5'), {'age'}) # only 'age' is indexed choose_plan(('name', '>', 'user5'), {'age'}) choose_plan(('name', '!=', 'user5'), {'age'}) RESULT ------------------------------ All three calls return ('scan', None, None) -- regardless of which operator is used. WHY THE COLUMN CHECK DOMINATES, REGARDLESS OF OPERATOR ------------------------------ choose_plan's own condition is: if op == '=' and col in indexed_columns: return ('index', col, value) return ('scan', None, None) This is a single `and` condition -- BOTH halves have to be true for the index branch to be taken at all. For a WHERE clause on 'name', col is 'name', and indexed_columns is {'age'} -- 'name' in {'age'} is False, regardless of what op happens to be. Since the `and` short-circuits (and even if it didn't, both operands would still need to be true), the whole condition is False whenever the column itself lacks an index, and the function falls through to `return ('scan', None, None)` unconditionally -- the operator was never even the deciding factor in this particular case. WHY THIS MATTERS AS A SEPARATE CHECK FROM THE MAIN BUG ------------------------------ The chapter's own main bug was about the OPERATOR half of this condition (op == '=') being missing entirely in the naive version. This exercise confirms the OTHER half -- col in indexed_columns -- was never in question at all; it was already correctly implemented from the start, in both the naive and fixed versions. Testing it independently here confirms the fix (adding the operator check) didn't accidentally introduce a NEW problem with the column check while solving the operator problem -- both conditions are independently verified to gate the index path correctly. WHY THIS WORKS AS AN ANSWER ------------------------------ A real query planner has to get BOTH conditions right at once: use the index only when an index genuinely exists for the queried column, AND only when the operator is one the index can actually answer safely. Testing each condition in isolation (this exercise for the column check, the chapter's own main example for the operator check) confirms neither one is silently relying on the other to compensate for a gap.