title: Hive 'limit' in subquery executes after full query
body: <p>I’m testing a rather taxing <code>rlike</code> function in a hive query. I figured I’d test against a subset first before applying it to my TB+ of data. The <strong>full</strong> query is:</p>
  
  <pre><code>create table proxy_parsed_clean as
  select
    a.*,
    case 
      when domainname rlike '.*:443$' then 1
      else 0
    end as used_https
  from proxy_parsed a;
  </code></pre>
  
  <p>Since there's so much data, I wrote a query that (ostensibly) would operate against a subset:</p>
  
  <pre><code>select
    case 
      when a.domainname rlike '.*:443$' then 1
      else 0
    end as used_https
  from (select domainname from proxy_parsed limit 10) a;
  </code></pre>
  
  <p>However, this seems to take <em>just</em> as long as the first query. Rather than applying the outer query to the subset, it seems to apply the case statement to the entire dataset and <em>then</em> limit. Running an <code>explain</code> confirmed my suspicions (<strong>notice the <code>limit</code> clause is moved to the end of the query</strong>):</p>
  
  <pre><code>&gt; explain select case when a.domainname rlike '.*:443$' then 1 else 0 end from (select domainname from proxy_parsed limit 10) a;
  
  +---------------------------------------------------------------------------------------------------------------------+--+
  |                                                       Explain                                                       |
  +---------------------------------------------------------------------------------------------------------------------+--+
  | STAGE DEPENDENCIES:                                                                                                 |
  |   Stage-1 is a root stage                                                                                           |
  |   Stage-0 depends on stages: Stage-1                                                                                |
  |                                                                                                                     |
  | STAGE PLANS:                                                                                                        |
  |   Stage: Stage-1                                                                                                    |
  |     Map Reduce                                                                                                      |
  |       Map Operator Tree:                                                                                            |
  |           TableScan                                                                                                 |
  |             alias: proxy_parsed                                                                                     |
  |             Statistics: Num rows: 157462377267 Data size: 6298495090688 Basic stats: COMPLETE Column stats: NONE    |
  |             Select Operator                                                                                         |
  |               expressions: domainname (type: varchar(40))                                                           |
  |               outputColumnNames: _col0                                                                              |
  |               Statistics: Num rows: 157462377267 Data size: 6298495090688 Basic stats: COMPLETE Column stats: NONE  |
  |               Limit                                                                                                 |
  |                 Number of rows: 10                                                                                  |
  |                 Statistics: Num rows: 10 Data size: 400 Basic stats: COMPLETE Column stats: NONE                    |
  |                 Reduce Output Operator                                                                              |
  |                   sort order:                                                                                       |
  |                   Statistics: Num rows: 10 Data size: 400 Basic stats: COMPLETE Column stats: NONE                  |
  |                   TopN Hash Memory Usage: 0.1                                                                       |
  |                   value expressions: _col0 (type: varchar(40))                                                      |
  |       Reduce Operator Tree:                                                                                         |
  |         Select Operator                                                                                             |
  |           expressions: VALUE._col0 (type: varchar(40))                                                              |
  |           outputColumnNames: _col0                                                                                  |
  |           Statistics: Num rows: 10 Data size: 400 Basic stats: COMPLETE Column stats: NONE                          |
  |           Limit                                                                                                     |
  |             Number of rows: 10                                                                                      |
  |             Statistics: Num rows: 10 Data size: 400 Basic stats: COMPLETE Column stats: NONE                        |
  |             Select Operator                                                                                         |
  |               expressions: CASE WHEN ((_col0 rlike '.*:443$')) THEN (1) ELSE (0) END (type: int)                    |
  |               outputColumnNames: _col0                                                                              |
  |               Statistics: Num rows: 10 Data size: 400 Basic stats: COMPLETE Column stats: NONE                      |
  |               File Output Operator                                                                                  |
  |                 compressed: false                                                                                   |
  |                 Statistics: Num rows: 10 Data size: 400 Basic stats: COMPLETE Column stats: NONE                    |
  |                 table:                                                                                              |
  |                     input format: org.apache.hadoop.mapred.TextInputFormat                                          |
  |                     output format: org.apache.hadoop.hive.ql.io.HiveIgnoreKeyTextOutputFormat                       |
  |                     serde: org.apache.hadoop.hive.serde2.lazy.LazySimpleSerDe                                       |
  |                                                                                                                     |
  |   Stage: Stage-0                                                                                                    |
  |     Fetch Operator                                                                                                  |
  |       limit: -1                                                                                                     |
  |       Processor Tree:                                                                                               |
  |         ListSink                                                                                                    |
  |                                                                                                                     |
  +---------------------------------------------------------------------------------------------------------------------+--+
  </code></pre>
  
  <p>If I am simply to run <code>select * from proxy_parsed limit 10;</code>, the query executes blazingly quickly. Can someone explain <strong>A)</strong>, why the query is not executing on the subset, and <strong>B)</strong> how to <em>make</em> it do so? </p>
  
  <p>I could just create a temp table, select 10 records into it and then execute the query, but that seems sloppy. Plus, I'd have a temp table to clean up after that. This behavior seems like a Hive bug, i.e., the <code>limit</code> behavior is clearly not behaving as it seems it should be in this case.</p>