Make type guessing for query args more robust

Improve the error handling of the type-guesser for queries
against metadata fields, and add some test coverage for
specific cases that came out of the discussion of the
source of the bug.

Fixes bug #1221736

Change-Id: I27f84aa2538d2a69a3d7b7e93c36813df8d35204
Merge "Make type guessing for query args more robust"