1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

DatabricksにおけるSQLパイプライン構文のサポート

Posted at

こちらのアップデートです。

SQL パイプライン構文のサポート
SQL パイプラインを作成できるようになりました。SQL パイプラインは、次の例に示すように、 SELECT c2 FROM T WHERE c1 = 5などの標準クエリをステップバイステップのシーケンスに構造化します。

FROM T
|> SELECT c2
|> WHERE c1 = 5

SQL パイプラインでサポートされている構文については、「SQL パイプライン構文」を参照してください。
この業界横断的な拡張の背景についてはSQL Has Problems. We Can Fix Them: Pipe Syntax In SQL (by Google Research)をご覧ください。

そもそもSQLパイプライン構文って?となりましたが、こちらの記事で詳細に説明されていました。

確かにSQLはデータを取得するテーブルを指定するFROM句が後ろに来るので、思考が若干行ったり来たりするというのはあるかもしれません。パイプラインはLinuxや最近ではLCELでも採用されている概念なので、処理の順番を意識しながらロジックを構成する上では好適なアプローチだと思います。

ということで、こちらのパイプライン構文がDatabricksでもサポートされたので試してみます。

前提条件

  • Databricksランタイム16.2以降のクラスターあるいはSQLウェアハウスが必要です。

以下のようなSQLを対象にします。samples.tcph配下のテーブルにクエリーを実行します。

SELECT c_count, COUNT(*) AS custdist
    FROM
    (SELECT c_custkey, COUNT(o_orderkey) c_count
      FROM customer
      LEFT OUTER JOIN orders ON c_custkey = o_custkey
                             AND o_comment NOT LIKE '%unusual%packages%'
     GROUP BY c_custkey
  ) AS c_orders
  GROUP BY c_count
  ORDER BY custdist DESC, c_count DESC;

以下のような結果が返ってきます。

c_count custdist
0 250013
10 32906
9 32587
11 31032
8 29152
12 28004
13 24959
7 23414
:

パイプライン構文で記述すると以下のようになります。FROM句からスタートして、結合や集計をパイプ|>で結合していきます。

FROM customer
  |> LEFT OUTER JOIN orders ON c_custkey = o_custkey
                            AND o_comment NOT LIKE '%unusual%packages%'
  |> AGGREGATE COUNT(o_orderkey) c_count
     GROUP BY c_custkey
  |> AGGREGATE COUNT(*) AS custdist
     GROUP BY c_count
  |> ORDER BY custdist DESC, c_count DESC;

同じ結果が得られました。

c_count custdist
0 250013
10 32906
9 32587
11 31032
8 29152
12 28004
13 24959
7 23414
:

もう少しステップバイステップで実行してみます。FROM句のみを実行します。

FROM customer 
c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment
412445 Customer#000412445 0QAB3OjYnbP6mA0B,kgf 21 31-421-403-4333 5358.33 BUILDING arefully blithely regular epi
412446 Customer#000412446 5u8MSbyiC7J,7PuY4Ivaq1JRbTCMKeNVqg 20 30-487-949-7942 9441.59 MACHINERY sleep according to the fluffily even forges. fluffily careful packages after the ironic, silent deposi
412447 Customer#000412447 HC4ZT62gKPgrjr ceoaZgFOunlUogr7GO 7 17-797-466-6308 7868.75 AUTOMOBILE aggle blithely among the carefully express excus
412448 Customer#000412448 hJok1MMrDgH 6 16-541-510-4964 6060.98 MACHINERY ly silent requests boost slyly. express courts sleep according to the fluf
412449 Customer#000412449 zAt1nZNG01gOhIqgyDtDa S,Y0VSofZJs1dd 14 24-710-983-5536 4973.84 HOUSEHOLD refully final theodolites. final, slow excuses sleep quickly! quickly ironic idea
:

LIMIT句を追加します。

FROM customer 
|> LIMIT 10

10行のみを取得できました。

c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment
412445 Customer#000412445 0QAB3OjYnbP6mA0B,kgf 21 31-421-403-4333 5358.33 BUILDING arefully blithely regular epi
412446 Customer#000412446 5u8MSbyiC7J,7PuY4Ivaq1JRbTCMKeNVqg 20 30-487-949-7942 9441.59 MACHINERY sleep according to the fluffily even forges. fluffily careful packages after the ironic, silent deposi
412447 Customer#000412447 HC4ZT62gKPgrjr ceoaZgFOunlUogr7GO 7 17-797-466-6308 7868.75 AUTOMOBILE aggle blithely among the carefully express excus
412448 Customer#000412448 hJok1MMrDgH 6 16-541-510-4964 6060.98 MACHINERY ly silent requests boost slyly. express courts sleep according to the fluf
412449 Customer#000412449 zAt1nZNG01gOhIqgyDtDa S,Y0VSofZJs1dd 14 24-710-983-5536 4973.84 HOUSEHOLD refully final theodolites. final, slow excuses sleep quickly! quickly ironic idea
412450 Customer#000412450 fUD6IoGdtF 20 30-293-696-5047 4406.28 BUILDING refully final dolphins after the carefully bold packages sleep quickly express deposits. fluffily
412451 Customer#000412451 W2Ge0Qd8adH 20 30-590-724-6711 2290.38 BUILDING slow asymptotes will are carefully final packages. slyly regular fox
412452 Customer#000412452 Ij4xiPIeNEP1uR5p7H 10 20-492-590-3363 3426.64 AUTOMOBILE sleep slyly after the sometimes even ideas. slyly express theodolites dazzle furiously ironic dependenci
412453 Customer#000412453 4DmSxDPMmfidKQB3W50FIzkjZESEW3LPgLBuQbic 21 31-480-724-9665 4592.14 MACHINERY against the slyly regular requests-- pending, pending accounts boost quic
412454 Customer#000412454 ZQfKDMUyEfn 9 19-898-261-2669 2035.91 FURNITURE quickly. blithely special theodolites about the excus

WHERE句を追加します。

FROM customer 
|> LIMIT 10
|> WHERE c_nationkey > 10

さらに絞り込まれました。

c_custkey c_name c_address c_nationkey c_phone c_acctbal c_mktsegment c_comment
412445 Customer#000412445 0QAB3OjYnbP6mA0B,kgf 21 31-421-403-4333 5358.33 BUILDING arefully blithely regular epi
412446 Customer#000412446 5u8MSbyiC7J,7PuY4Ivaq1JRbTCMKeNVqg 20 30-487-949-7942 9441.59 MACHINERY sleep according to the fluffily even forges. fluffily careful packages after the ironic, silent deposi
412449 Customer#000412449 zAt1nZNG01gOhIqgyDtDa S,Y0VSofZJs1dd 14 24-710-983-5536 4973.84 HOUSEHOLD refully final theodolites. final, slow excuses sleep quickly! quickly ironic idea
412450 Customer#000412450 fUD6IoGdtF 20 30-293-696-5047 4406.28 BUILDING refully final dolphins after the carefully bold packages sleep quickly express deposits. fluffily
412451 Customer#000412451 W2Ge0Qd8adH 20 30-590-724-6711 2290.38 BUILDING slow asymptotes will are carefully final packages. slyly regular fox
412453 Customer#000412453 4DmSxDPMmfidKQB3W50FIzkjZESEW3LPgLBuQbic 21 31-480-724-9665 4592.14 MACHINERY against the slyly regular requests-- pending, pending accounts boost quic

複雑なクエリーを構築する際の選択肢になりますので、是非ご活用ください!

はじめてのDatabricks

はじめてのDatabricks

Databricks無料トライアル

Databricks無料トライアル

1
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?