title: bigquery: count on union of subqueries
body: <p>I have two tables (<code>light</code> and <code>enhanced</code>) with slightly different schemas. Second table (<code>enhanced</code>) has additional nullable field <code>t</code>. 
  I want to get count of rows from first and second table per groups based on two fields (<code>d,p</code>)</p>
  
  <p>To achieve this I'm using union of subqueries with asterisk</p>
  
  <pre><code>select
    d
    , p
    ,count(1) - count(t) light_cnt
    ,count(t) enhanced_cnt
  from 
    (select * from light)
    , (select * from enhanced)
  group by
    d, p
  </code></pre>
  
  <p>But this query returns wrong counts (something about twice as much)
  This is only happening when I group by two fields. Single field works well. I found out that it works correctly when I wrap union in another subquery</p>
  
  <pre><code>select
    d
    , p
    ,count(1) - count(t) light_cnt
    ,count(t) enhanced_cnt
  from
    (select * from 
        (select * from light)
        , (select * from enhanced)
    )
  group by
    d, p
  </code></pre>
  
  <p>Is this a bug or I'm doing something wrong?</p>
  
  <hr>
  
  <h2>EDIT:</h2>
  
  <p>I have reproduced same corrupted behaviour without <code>group by</code>, just with <code>where</code></p>
  
  <pre><code>select count(1) from enhanced where p = 124
  </code></pre>
  
  <p>returns 292</p>
  
  <pre><code>select count(1) from light where p = 124
  </code></pre>
  
  <p>returns 12512</p>
  
  <pre><code>select count(1)
  from (select * from light), (select * from enhanced)
  where p = 124
  </code></pre>
  
  <p>returns 12804 which is correct, while</p>
  
  <pre><code>select count(1), count(t)
  from (select * from light), (select * from enhanced)
  where p = 124
  </code></pre>
  
  <p>returns 24527, 501... very strange. Seems to be a bug.</p>
  
  <p>Workaround:</p>
  
  <pre><code>select count(1), count(t)
  from (select * from (select * from light), (select * from enhanced))
  where p = 124
  </code></pre>
  
  <p>returns 12804, 292. Correct.</p>
  
  <p>Both tables <code>light</code> and <code>enhanced</code> have complicated schemas inherited from avro. There are records and repeated fields. Field <code>p</code> and <code>t</code> are in abbverviated form in select above for sake of simplicity. Real is <code>p -&gt; record.record.record.id</code> (leaf is integer), and <code>t -&gt; record.time</code> (leaf is integer). None of records in path <code>p</code> nor <code>t</code> are repeatable. All are nullable.</p>