天天看點

Hive partition prune Failed

昨天發現線上的HiveQuery:

1

<code>select</code> <code>* </code><code>from</code> <code>db1.t1where dt </code><code>between</code> <code>to_char(getdate(‘variables’,’-40’),’yyyymmdd’) </code><code>and</code> <code>‘variables’</code><code>and</code> <code>hour</code><code>=’xxx’(其中t1 partitioned bydt,</code><code>hour</code><code>)</code>

不能進行partition prune導緻執行效率非常的差,問題出現在哪裡呢?

把To_Char函數的代碼翻出來就一目了然了:

2

3

4

5

6

7

8

9

10

11

<code>@UDFType</code><code>(deterministic= </code><code>false</code><code>)</code>

<code>@Description</code><code>(name= </code><code>"to_char"</code><code>,</code>

<code>        </code><code>value = </code><code>"_FUNC_(date, pattern)  converts a string with yyyy-MM-dd HH:mm:sspattern "</code> <code>+</code>

<code>                </code><code>"to a string with givenpattern.\n"</code>

<code>        </code><code>+</code><code>"_FUNC_(datetime, pattern)  converts a string with yyyy-MM-dd pattern"</code> <code>+</code>

<code>        </code><code>+</code><code>"_FUNC_(number [,format]) convertsa number to a string\n"</code><code>,</code>

<code>        </code><code>extended = </code><code>"Example:\n"</code>

<code>        </code><code>+</code><code>" &gt; SELECT to_char('2011-05-1110:00:12'.'yyyyMMdd') FROM src LIMIT 1;\n"</code>

<code>        </code><code>+</code><code>"20110511\n"</code>

<code>)</code>

注意到這個函數是一個“非确定性”函數,Hive在做partition prune時考慮三點不進行過濾處理:

1.如果是邏輯函數的話,若所有的child節點都為null則忽略

2.非确定性函數忽略

3.其他情況,隻要有child節點為null則忽略

而這裡的to_char正是第二種情況,這裡我們自己寫了個确定性UDF來解決該問題

注:另外一個類似的Case HIVE-1173

本文轉自MIKE老畢 51CTO部落格,原文連結:http://blog.51cto.com/boylook/1365734,如需轉載請自行聯系原作者