Hive で複数のテーブルを LEFT JOIN するとき、左側のテーブルには存在するけど、右側のテーブルには存在しない値があると、そのカラムは NULL になってしまう。
NULL じゃなくて、デフォルト値をいれたいんだよねーと思って方法を探ってみたら、COALESCE という関数にたどり着いた。引数の中から最初に現れた NULL じゃないヤツを返してくれるようだ。Hive の LanguageManual UDF にはこんな感じで書かれている。
Name (Signature) | Description |
---|---|
COALESCE(T v1, T v2, ...) | Returns the first v that is not NULL, or NULL if all v's are NULL. |
つまり、次のように書けば、デフォルト値を返すことができる。
SELECT
COALESCE(table_b.posibble_null_column, 0) -- default value is 0
FROM
table_a LEFT JOIN table_b ON (table_a.id = table_b.id)
...
この COALESCE 関数は、Hive 専用という訳ではなく、PostgreSQL や MySQL にも存在する。というか、標準 SQL なんだな、これ。知らなかったし、なんと読めばいいのか綴りを見ても全く分からないので覚えられない。「MYSQLの関数 COALESCE」によると、読み方は「コーラス」らしい。
後から思いついたんだけど、NULL だったらデフォルト値を入れるという動作なら、オレがよく使う CASE 文での条件分岐でも実現できたし、さらに調べてみると NVL 関数というのもあった。同じく Hive のリファレンスではこのように書かれている。
Name (Signature) | Description |
---|---|
nvl(T value, T default_value) | Returns default value if value is null else returns value (as of HIve 0.11). |
なんでリファレンス内の COALESCE は大文字で nvl は小文字なんだ?という疑問はあるのだが、「引数の値が NULL だったらデフォルト値を返す」という期待の動作そのものじゃないか、NVL 関数は。
COALESCE 関数を使ったコード修正は「pull request は作ったけど、まだレビューの依頼を出してない状態」なので、今のうちに NVL 関数で動作確認してみよう。問題がなければ、NVL 関数を使ったコードに修正した上でレビューの依頼を出そうっと。
NVL 関数のほうが「目的」を明確に表しているので、後からコード(HiveQL)を触る場合に、他の人にも未来のオレにも意図が伝わりやすくなるだろうし。
【追記】
Facebook で「NVL 関数は Oracle の方言である」というフィードバックを頂いた。確かにそこは調べていなかった。Oracle / Hive に閉じこもるなら NVL 関数でも良いけど、標準 SQL 的な知識を蓄えるなら COALESCE 関数の方が適切かもしれない。
たいした話じゃなくてもアウトプットしてみると、より良い知見が頂けてしまう流れ、たいへんありがたい。
✅I Will Promote Your Business In Worldwide To Any Niche ... <br> 1.I search in google after the keyward offered by you in order to find sites
Hi there <br> <br>I just checked out your website tdiary.net and your site takes longer than 3 seconds to load :( <br> <br>If your site takes so Long Time To Load you are losing more than half your traffic & 50% of visitors won't return if they have trouble loading a page... <br> <br>That's a really expensive mistake to make, specially if you are paying for traffic. I can help you to improve that. <br> <br>Please contact me by my email for details: wp_optimiser@mail.com <br> <br>Regards, <br>Coates
Hello there <br> <br>I just checked out your website tdiary.net and wanted to find out if you need help for SEO Link Building ? <br> <br>If you aren't using SEO Software then you will know the amount of work load involved in creating accounts, confirming emails and submitting your contents to thousands of websites. <br> <br>With THIS SOFTWARE the link submission process will be the easiest task and completely automated, you will be able to build unlimited number of links and increase traffic to your websites which will lead to a higher number of customers and much more sales for you. <br> <br>IF YOU ARE INTERESTED, We offer you 7 days free trial <br>CONTACT US ==> seosubmitter@mail.com <br> <br> <br>Regards, <br>Kershaw
Hello <br> <br>NO MATTER WHAT YOU WANT, WE HAVE - so do not hesitate to ask even if it is not listed below !! <br> <br>I provide the following mentioned services. <br> <br>* ADWORDS $100 coupon <br>* BING $100 coupon - now you can use two bing ads coupons per account. <br>* AWS Amazon $150 coupons <br>* Digital ocean $50 coupon <br>* WINDOWS keys lifetime license -version 7,8 and 10 <br>* OFFICE 365 lifetime access <br>* OFFICE 2016 AND 2019 Product Keys <br>* NETFLIX Accounts with 6 months validity <br>* GMAIL accounts <br>* ADWORDS Verified account with $500 credits + VPS <br>* BING verified account with $100 credits + VPS <br>* AMAZON AWS RDP/VPS Free Tier accounts <br>* Google CLOUD accounts with $300 credits. <br>* Lynda.com Accounts <br>* DOMINOS & PIZZA HUT PIZZAS available(for USA) only <br>* EDU emails <br>* AZURE Accounts with $200 credits <br>* ADSENSE Accounts with domain and Website <br>* Quality NICHE Websites/Blogs <br>* CPA Accounts Approval of any company <br>* PAYPAL verified accounts with NO 21 days hold <br>* ANY OTHER SERVICE you are looking for <br> <br>If you are interested in the following services, please let me know here: bestservice@post.com <br> <br> <br>Thanks
Hello <br> <br>Tired of Waiting FOREVER to earn a profit online? <br> <br>NEW Web-App Allows You To Legally Hijack Traffic And Authority From Wikipedia AND YouTube To Earn Affiliate Commissions In 24 Hours Or Less - in ANY Niche! <br> <br>No Previous Skills Or Experience Required. You can literally be a COMPLETE Newbie and Get RESULTS with just 5 minutes of actual “work”.. <br> <br>IF YOU ARE INTERESTED, CONTACT US ==> sayedasaliha748@gmail.com <br> <br>And Of Course, You Also Have A 30-DAY, YOUR-MONEY Back GUARANTEE <br>Once you Join TODAY, You'll Also GET AMAZING BONUSES <br> <br>Regards, <br>TrafficJacker
Hello! <br> <br>You Need Leads, Sales, Conversions, Traffic for tdiary.net ? Will Findet... <br> <br>I WILL SEND 5 MILLION MESSAGES VIA WEBSITE CONTACT FORM <br> <br>Don't believe me? Since you're reading this message then you're living proof that contact form advertising works! <br>We can send your ad to people via their Website Contact Form. <br> <br>IF YOU ARE INTERESTED, Contact us => lisaf2zw526@gmail.com <br> <br>Regards, <br>Pelsaert <br> <br>
Good day! <br> <br>You Need Leads, Sales, Conversions, Traffic for tdiary.net ? Will Findet... <br> <br>I WILL SEND 5 MILLION MESSAGES VIA WEBSITE CONTACT FORM <br> <br>Don't believe me? Since you're reading this message then you're living proof that contact form advertising works! <br>We can send your ad to people via their Website Contact Form. <br> <br>IF YOU ARE INTERESTED, Contact us => lisaf2zw526@gmail.com <br> <br>Regards, <br>Heflin <br> <br>
Howdy <br> <br>I just checked out your website tdiary.net and wanted to find out if you need help for SEO Link Building ? <br> <br>If you aren't using SEO Software then you will know the amount of work load involved in creating accounts, confirming emails and submitting your contents to thousands of websites. <br> <br>With THIS SOFTWARE the link submission process will be the easiest task and completely automated, you will be able to build unlimited number of links and increase traffic to your websites which will lead to a higher number of customers and much more sales for you. <br> <br>IF YOU ARE INTERESTED, We offer you 7 days free trial <br>CONTACT US ==> seosubmitter@mail.com <br> <br>Regards, <br>Best Seo Software <br> <br>
Discounts on popular products <br> <br>How do you do? <br> <br>Only we have such low prices. Choose what you like. <br> <br>Hot Sales 40% : <br> <br>- Dresses <br>- Men's Sets <br>- Mobile Phone Accessories <br>- Office Software <br>- Video Games <br>- Beads & Jewelry Making <br>- Major Appliances <br>- Bags & Shoes <br>- Puzzles & Games <br>- Sneakers <br>- Bath & Shower <br>- Bathroom Fixtures <br> <br>You can see even more here: https://rb.gy/yk3vte <br> <br>The best offer on the market. Best price and fast delivery.
Hi there <br> <br>DFY Suite is an established, high-quality social syndication system that allows you to get stunning content syndication <br>for your videos or niche sites WITHOUT having to do ANY of the work yourself. <br> <br> + There is NO software to download or install <br> + There is NO account creation needed on your part <br> + There is NO having to deal with proxies of captchas <br> + There are NO complicated tutorials you have to watch <br> <br>DFY Suite 2 with more advanced features will become your powerful tool to serve the online marketing industry <br>which gets more and more competitive. You will be able to skyrocket your business with very little effort. <br> <br>MORE INFO HERE=> https://bit.ly/2yObYYs <br> <br>Kind Regards, <br>Rocky Hargrave