IMPALA-1211: Query should be unregistered if SetResultCache returns error status

Otherwise, the query will hang around until the session or connection is
closed.  Enhance a test so that this is regression tested.

While I'm here, some minor cleanup: hoist out some code that's sprinkled
between the return_val writes to make it more obvious what the code is doing.

Change-Id: I594e583c4e10f12c3f9b920ea4d44e5da7df3558
Reviewed-on: http://gerrit.sjc.cloudera.com:8080/4211
Reviewed-by: Henry Robinson <henry@cloudera.com>
Tested-by: jenkins
(cherry picked from commit 799d1fc6cd0e871cdf7eeb6bcde12822574d8ff2)
Reviewed-on: http://gerrit.sjc.cloudera.com:8080/4244
Reviewed-by: Daniel Hecht <dhecht@cloudera.com>
Tested-by: Daniel Hecht <dhecht@cloudera.com>