<?xml version="1.0" encoding="UTF-8"?><rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:atom="http://www.w3.org/2005/Atom" version="2.0" xmlns:cc="http://cyber.law.harvard.edu/rss/creativeCommonsRssModule.html">
    <channel>
        <title><![CDATA[Stories by ok.souhei on Medium]]></title>
        <description><![CDATA[Stories by ok.souhei on Medium]]></description>
        <link>https://medium.com/@oksouhei?source=rss-1c9d3f33cb47------2</link>
        <image>
            <url>https://cdn-images-1.medium.com/fit/c/150/150/1*CRLe04FVgquErXpDZMW7pw.jpeg</url>
            <title>Stories by ok.souhei on Medium</title>
            <link>https://medium.com/@oksouhei?source=rss-1c9d3f33cb47------2</link>
        </image>
        <generator>Medium</generator>
        <lastBuildDate>Mon, 30 Nov 2026 16:53:28 GMT</lastBuildDate>
        <atom:link href="https://medium.com/@oksouhei/feed" rel="self" type="application/rss+xml"/>
        <webMaster><![CDATA[yourfriends@medium.com]]></webMaster>
        <atom:link href="http://medium.superfeedr.com/" rel="hub"/>
        <item>
            <title><![CDATA[How Query Engines Workを片手にDataFusionのexampleを動かす]]></title>
            <link>https://medium.com/@oksouhei/how-query-engines-work%E3%82%92%E7%89%87%E6%89%8B%E3%81%ABdatafusion%E3%81%AEexample%E3%82%92%E5%8B%95%E3%81%8B%E3%81%99-7f121c7083c8?source=rss-1c9d3f33cb47------2</link>
            <guid isPermaLink="false">https://medium.com/p/7f121c7083c8</guid>
            <dc:creator><![CDATA[ok.souhei]]></dc:creator>
            <pubDate>Tue, 15 Aug 2023 12:47:22 GMT</pubDate>
            <atom:updated>2023-08-15T14:32:30.639Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://github.com/apache/arrow-datafusion">Apache Arrow DataFusion</a>や<a href="https://github.com/apache/arrow-rs">Apache ArrowのRust Implementation</a>の作者<a href="https://github.com/andygrove">Andyさん</a>が書いた、その名もずばりHow Query Engines Workという<a href="https://howqueryengineswork.com/">テキスト</a>と<a href="https://github.com/andygrove/how-query-engines-work">サンプルコード</a>があります。</p><p>テキストは当初はlearnpub.comでの有償販売のみだったと思いますが、現在は無料でも公開されています。非常にわかりやすく、簡潔に記述されており、勉強になりました。</p><p>今回、このテキストとサンプルコードを読んだ上で、DataFusionのexampleを動かしてみました。</p><h3>クエリーエンジンの大まかな構成</h3><p>クエリーエンジンの概要はそれこそHow Query Engines Workにしっかり書いてあるのですが、「指定されたSQL文を実行する」ための大まかな手順は以下の形になるようです。</p><ul><li>TokenizerでSQL文を分割し、Tokenのリストを作成</li><li>ParserでTokenのリストをASTに変換</li><li>SQL PlannerでASTをLogical Planに変換</li><li>OptimizerでLogical Planを最適化する</li><li>Query PlannerでLogical PlanからPhysical Planに変換</li><li>Physical Planを実行して、結果を取得</li></ul><p>各コンポーネントに対応するサンプルコードはこの辺りです。</p><p>Tokenizer</p><ul><li><a href="https://github.com/andygrove/how-query-engines-work/blob/main/jvm/sql/src/main/kotlin/SqlTokenizer.kt">https://github.com/andygrove/how-query-engines-work/blob/main/jvm/sql/src/main/kotlin/SqlTokenizer.kt</a></li></ul><p>Parser</p><ul><li><a href="https://github.com/andygrove/how-query-engines-work/blob/main/jvm/sql/src/main/kotlin/SqlParser.kt">https://github.com/andygrove/how-query-engines-work/blob/main/jvm/sql/src/main/kotlin/SqlParser.kt</a></li></ul><p>AST</p><ul><li><a href="https://github.com/andygrove/how-query-engines-work/blob/main/jvm/sql/src/main/kotlin/Expressions.kt">https://github.com/andygrove/how-query-engines-work/blob/main/jvm/sql/src/main/kotlin/Expressions.kt</a></li></ul><p>SQL Planner</p><ul><li><a href="https://github.com/andygrove/how-query-engines-work/blob/main/jvm/sql/src/main/kotlin/SqlPlanner.kt">https://github.com/andygrove/how-query-engines-work/blob/main/jvm/sql/src/main/kotlin/SqlPlanner.kt</a></li></ul><p>Logical Plan</p><ul><li><a href="https://github.com/andygrove/how-query-engines-work/blob/main/jvm/logical-plan/src/main/kotlin/LogicalPlan.kt">https://github.com/andygrove/how-query-engines-work/blob/main/jvm/logical-plan/src/main/kotlin/LogicalPlan.kt</a></li></ul><p>Optimizer</p><ul><li><a href="https://github.com/andygrove/how-query-engines-work/blob/main/jvm/optimizer/src/main/kotlin/Optimizer.kt">https://github.com/andygrove/how-query-engines-work/blob/main/jvm/optimizer/src/main/kotlin/Optimizer.kt</a></li></ul><p>Query Planner</p><ul><li><a href="https://github.com/andygrove/how-query-engines-work/blob/main/jvm/query-planner/src/main/kotlin/QueryPlanner.kt">https://github.com/andygrove/how-query-engines-work/blob/main/jvm/query-planner/src/main/kotlin/QueryPlanner.kt</a></li></ul><p>Physical Plan</p><ul><li><a href="https://github.com/andygrove/how-query-engines-work/blob/main/jvm/physical-plan/src/main/kotlin/PhysicalPlan.kt">https://github.com/andygrove/how-query-engines-work/blob/main/jvm/physical-plan/src/main/kotlin/PhysicalPlan.kt</a></li></ul><h3>DataFusionのexampleを動かしてみる</h3><p><a href="https://github.com/apache/arrow-datafusion/tree/master/datafusion-examples">https://github.com/apache/arrow-datafusion/tree/master/datafusion-examples</a>のREADMEに実行方法が記載されています。</p><pre>&gt; cargo run --example csv_sql<br>    Finished dev [unoptimized + debuginfo] target(s) in 0.34s<br>     Running `target/debug/examples/csv_sql`<br>+----+-----------------------------+-----------------------------+<br>| c1 | MIN(aggregate_test_100.c12) | MAX(aggregate_test_100.c12) |<br>+----+-----------------------------+-----------------------------+<br>| c  | 0.2667177795079635          | 0.991517828651004           |<br>| d  | 0.061029375346466685        | 0.9748360509016578          |<br>| e  | 0.01479305307777301         | 0.9965400387585364          |<br>| a  | 0.02182578039211991         | 0.9567595541247681          |<br>| b  | 0.04893135681998029         | 0.9185813970744787          |<br>+----+-----------------------------+-----------------------------+<br>+----+------+<br>| c2 | c3   |<br>+----+------+<br>| 1  | -85  |<br>| 3  | 13   |<br>| 4  | -38  |<br>| 4  | -54  |<br>| 5  | 36   |<br>| 1  | -25  |<br>| 5  | -31  |<br>| 2  | 45   |<br>| 3  | 13   |<br>| 3  | 17   |<br>| 4  | 65   |<br>| 4  | -101 |<br>| 2  | -48  |<br>| 1  | -56  |<br>| 1  | -5   |<br>| 3  | 14   |<br>| 1  | 83   |<br>| 3  | -12  |<br>| 3  | -72  |<br>| 2  | -43  |<br>| 5  | -101 |<br>+----+------+</pre><p>このexampleをVS Codeでステップ実行し、How Query Engines Workの記載に類似したシステム構成なのか確認したいと思います。</p><h4>VS Codeでデバッグ実行するための準備</h4><p>OSは macOS 13.5です。</p><p>以下の拡張機能をインストールします。</p><ul><li><a href="https://code.visualstudio.com/docs/languages/rust">https://code.visualstudio.com/docs/languages/rust</a></li><li><a href="https://jmarketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb">https://jmarketplace.visualstudio.com/items?itemName=vadimcn.vscode-lldb</a></li></ul><p>デバッグの初回実行時に以下を実施すると、.vscode/launch.jsonが自動生成されます。</p><ul><li>デバッガーでLLDBを選択</li><li>cargo.tomlから設定ファイルを自動生成するか聞かれるのでYesを選択</li></ul><h4>実行</h4><p>デバッグメニューがズラズラ表示されるので「Debug example ‘csv_sql’」を選択して実行します。</p><p><a href="https://github.com/apache/arrow-datafusion/blob/master/datafusion-examples/examples/csv_sql.rs">https://github.com/apache/arrow-datafusion/blob/master/datafusion-examples/examples/csv_sql.rs</a> が実行されるので、ブレークポイントを適当に設定して挙動を確認します。</p><p>その結果、DataFusionも以下のように、おおむねHow Query Engins Workの記載内容に則した実装になっていることが確認できました。</p><p>Tokenizer</p><ul><li><a href="https://docs.rs/sqlparser/latest/sqlparser/">https://docs.rs/sqlparser/latest/sqlparser/</a></li></ul><p>Parser</p><ul><li>datafusion_sql::parser::DFParser</li><li><a href="https://github.com/apache/arrow-datafusion/blob/main/datafusion/sql/src/parser.rs">https://github.com/apache/arrow-datafusion/blob/main/datafusion/sql/src/parser.rs</a></li></ul><p>AST</p><ul><li>datafusion_sql::parser::Statement</li><li><a href="https://github.com/apache/arrow-datafusion/blob/main/datafusion/sql/src/parser.rs">https://github.com/apache/arrow-datafusion/blob/main/datafusion/sql/src/parser.rs</a></li></ul><p>SQL Planner</p><ul><li>datafusion_sql::planner::SqlToRel</li><li><a href="https://github.com/apache/arrow-datafusion/blob/main/datafusion/sql/src/planner.rs">https://github.com/apache/arrow-datafusion/blob/main/datafusion/sql/src/planner.rs</a></li></ul><p>Logical Plan</p><ul><li>datafusion_expr::logical_plan::plan::LogicalPlan</li><li><a href="https://github.com/apache/arrow-datafusion/blob/main/datafusion/expr/src/logical_plan/plan.rs">https://github.com/apache/arrow-datafusion/blob/main/datafusion/expr/src/logical_plan/plan.rs</a></li></ul><p>Optimizer</p><ul><li>datafusion_optimizer::optimizer::Optimizer</li><li><a href="https://github.com/apache/arrow-datafusion/blob/main/datafusion/optimizer/src/optimizer.rs">https://github.com/apache/arrow-datafusion/blob/main/datafusion/optimizer/src/optimizer.rs</a></li></ul><p>Query Planner</p><ul><li>datafusion::physhical_planner::DefaultPhysicalPlanner</li><li><a href="https://github.com/apache/arrow-datafusion/blob/main/datafusion/core/src/physical_planner.rs">https://github.com/apache/arrow-datafusion/blob/main/datafusion/core/src/physical_planner.rs</a></li></ul><p>Physical Plan</p><ul><li>datafusion::physical_plan::ExecutionPlan</li><li><a href="https://github.com/apache/arrow-datafusion/blob/main/datafusion/core/src/physical_plan/mod.rs">https://github.com/apache/arrow-datafusion/blob/main/datafusion/core/src/physical_plan/mod.rs</a></li></ul><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=7f121c7083c8" width="1" height="1" alt="">]]></content:encoded>
        </item>
        <item>
            <title><![CDATA[Headless BIのMetriqlを試してみた]]></title>
            <link>https://medium.com/@oksouhei/headless-bi%E3%81%AEmetriql%E3%82%92%E8%A9%A6%E3%81%97%E3%81%A6%E3%81%BF%E3%81%9F-11df769d9ba9?source=rss-1c9d3f33cb47------2</link>
            <guid isPermaLink="false">https://medium.com/p/11df769d9ba9</guid>
            <dc:creator><![CDATA[ok.souhei]]></dc:creator>
            <pubDate>Thu, 04 Nov 2021 13:49:52 GMT</pubDate>
            <atom:updated>2021-11-05T01:34:34.882Z</atom:updated>
            <content:encoded><![CDATA[<p><a href="https://metriql.com/">Metriql</a>というOSSが<a href="https://www.dataengineeringpodcast.com/metriql-open-source-headless-bi-episode-228/">Data Engineering Podcast</a>で紹介されており、気になったので動かしてみました。</p><h3>Metriqlとは</h3><p>Metriqlは自らをHeadless BIと定義しています。Headless BIというのは、一般的なBIツールから可視化機能を分離して、メトリクス定義の管理に特化したソフトウェアを指すようです。</p><p>メトリクス定義の管理と可視化を分離する理由というか、課題意識に関しては<a href="https://benn.substack.com/p/metrics-layer">この記事</a>が参考になりました。</p><blockquote>The core problem is that there’s no central repository for defining a metric. Without that, metric formulas are scattered across tools, buried in hidden dashboards, and recreated, rewritten, and reused with no oversight or guidance.</blockquote><blockquote>Without a central metrics repository, these two workflows — metric extraction in one case, and complex strategic research in the other — will remain apart. Even if they share the same database and the same transformed tables, the stories they tell will be inconsistent until they share the same metrics.</blockquote><p>社内の様々な部門が様々なツールを利用して行うデータ活用に整合性を保つためには、メトリクス定義を可視化ツールや分析ツールと分離して一元管理するのが有用だ、という話自体は真っ当だと思います。</p><h4>デザイン</h4><p>Metriqlはdbtのextentionとしてデザインされており、メトリクス定義はdbtのyamlを利用して設定管理します。また、Metriqlはあくまで定義を管理するだけでメトリクスの値自体は保持しておらず、つどDWH等に問い合わせるようになっています。</p><h3>動かしてみる</h3><p>動作確認するためにデータソース とdbt、そしてメトリクスのコンシューマーが必要です（たぶん）。</p><p>今回は、データソースにBiqQuery、コンシューマーにTrino CLIを利用しました。</p><p>手元の環境はmacOS、クラウドサービスとしてGCPを利用します。</p><h4>BigQueryの用意</h4><ul><li>サービスアカウントでアクセスする事にしたので、テスト用にBigQueryの権限を持つサービスアカウントを作成し、秘密鍵をダウンロードする</li><li>テスト用のデータセットを作っておく</li></ul><h4>dbtの用意</h4><p>基本的に<a href="https://metriql.com/tutorial/for-starters">チュートリアル</a>に書いてある通りに実行するが、コンシューマーの動作確認をしやすいよう、models/exmapleディレクトリの中を書き換える。</p><pre>&gt; cat dbt-sample/models/example/order.sql </pre><pre>{{ config(materialized=&#39;table&#39;) }}</pre><pre>with source_data as (</pre><pre>    select 1 as user_id, 100 as price, &#39;type_a&#39; as segment<br>    union all<br>    select 1 as user_id, 200 as price, &#39;type_b&#39; as segment<br>    union all<br>    select 2 as user_id, 500 as price, &#39;type_a&#39; as segment</pre><pre>)</pre><pre>select * from source_data</pre><pre>&gt; cat dbt-sample/models/example/schema.yml </pre><pre>version: 2 </pre><pre>models:<br>  - name: order<br>    columns:<br>      - name: user_id<br>      - name: price<br>      - name: segment<br>        meta:<br>          metriql.dimension:<br>            type: string<br>    meta:<br>      metriql:<br>        measures:<br>          total_orders:<br>            aggregation: count<br>          total_users:<br>            aggregation: count_unique<br>            column: user_id<br>          revenue:<br>            aggregation: sum<br>            column: price</pre><h4>Metriqlの設定</h4><p><a href="https://metriql.com/metriql-cli/cli-overview#installation">ここ</a>に書いてある通りにインストールし、起動する。sourceからコンパイルしてローカルホストで実行。</p><pre>&gt; ./target/metriql-0.4-SNAPSHOT-bundle/metriql-0.4-SNAPSHOT/bin/metriql serve --project-dir ../dbt-sample</pre><h4>Trino CLIの設定</h4><p>これも<a href="https://metriql.com/integrations/services/cli">ドキュメント通り</a>。ローカルホストで実行して同じローカルホストのMetriqlサーバに接続する。</p><pre>&gt; ./trino --server localhost:5656 --catalog metriql                                                                                          <br>trino&gt; show tables from public;<br>         Table          <br>------------------------<br> model_dbt_sample_order <br>(1 row)</pre><pre>Query dde17772-6df0-478c-b835-a12295fbeb36, FINISHED, 1 node<br>Splits: 1 total, 0 done (0.00%)<br>1.19 [0 rows, 0B] [0 rows/s, 0B/s]</pre><p>MQLというDSLを利用してメトリクスの値を取得できる</p><pre>trino&gt; select total_orders, total_users from &quot;ref(&#39;order&#39;)&quot;;<br> total_orders | total_users <br>--------------+-------------<br>            3 |           2 <br>(1 row)</pre><pre>Query 4fc6d212-3d34-3f6e-a536-af1309073d34, FINISHED, 1 node<br>Splits: 1 total, 0 done (0.00%)<br>0.10 [0 rows, 0B] [0 rows/s, 0B/s]</pre><pre>trino&gt; select revenue from &quot;ref(&#39;order&#39;)&quot;;<br> revenue <br>---------<br>     800 <br>(1 row)</pre><pre>Query f736e21a-6f5e-49d9-bd2a-ec0387429be5, FINISHED, 1 node<br>Splits: 1 total, 0 done (0.00%)<br>3.33 [0 rows, 0B] [0 rows/s, 0B/s]</pre><pre>trino&gt; select revenue, segment from &quot;ref(&#39;order&#39;)&quot;;<br> revenue | segment <br>---------+---------<br>     200 | type_b  <br>     600 | type_a  <br>(2 rows)</pre><pre>trino&gt; select total_orders, total_users, segment from &quot;ref(&#39;order&#39;)&quot;;<br> total_orders | total_users | segment <br>--------------+-------------+---------<br>            1 |           1 | type_b  <br>            2 |           2 | type_a  <br>(2 rows)</pre><pre>Query 9ea172c7-0992-33d1-8d33-a8b046a3461a, FINISHED, 1 node<br>Splits: 1 total, 0 done (0.00%)<br>0.10 [0 rows, 0B] [0 rows/s, 0B/s]</pre><p>measureやdimensionの定義が共有されるので多少管理が楽になるというのはありつつ、結局MQLなりで行うMetriqlに対する問い合わせ自体は分散管理してしまいそうなので、どこまで楽になっているのかはちょっとわかりませんでした。ただ、方向性は気になるので、これからもウォッチしていきたいなと思います。</p><img src="https://medium.com/_/stat?event=post.clientViewed&referrerSource=full_rss&postId=11df769d9ba9" width="1" height="1" alt="">]]></content:encoded>
        </item>
    </channel>
</rss>