File size: 6,526 Bytes
97902b8
 
 
 
 
 
 
e64bc6a
97902b8
 
 
 
 
 
 
 
 
 
 
e64bc6a
 
 
 
 
 
 
97902b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e64bc6a
97902b8
 
 
e64bc6a
97902b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e64bc6a
97902b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e64bc6a
97902b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e64bc6a
97902b8
 
e64bc6a
97902b8
 
 
e64bc6a
97902b8
e64bc6a
 
97902b8
 
 
 
 
e64bc6a
97902b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e64bc6a
97902b8
 
e64bc6a
97902b8
e64bc6a
97902b8
 
 
 
 
 
 
 
 
e64bc6a
97902b8
 
 
 
e64bc6a
97902b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e64bc6a
 
 
 
 
 
 
 
 
 
97902b8
 
 
 
 
 
 
 
 
 
 
 
 
 
e64bc6a
97902b8
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# /// script
# requires-python = ">=3.10"
# dependencies = [
#     "marimo",
#     "duckdb==1.2.1",
#     "pyarrow==19.0.1",
#     "plotly.express",
#     "sqlglot==27.0.0",
# ]
# ///

import marimo

__generated_with = "0.14.10"
app = marimo.App(width="medium")


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
    # Loading Parquet files with DuckDB
    *By [Thomas Liang](https://github.com/thliang01)*
    #
    """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        [Apache Parquet](https://parquet.apache.org/) is a popular columnar storage format, optimized for analytics. Its columnar nature allows query engines like DuckDB to read only the necessary columns, leading to significant performance gains, especially for wide tables.

        DuckDB has excellent, built-in support for reading Parquet files, making it incredibly easy to query and analyze Parquet data directly without a separate loading step.

        In this notebook, we'll explore how to load and analyze Airbnb's stock price data from a remote Parquet file:
        <ul>
            <li>Querying a remote Parquet file directly.</li>
            <li>Using the `read_parquet` function for more control.</li>
            <li>Creating a persistent table from a Parquet file.</li>
            <li>Performing basic data analysis and visualization.</li>
        </ul>
        """
    )
    return


@app.cell
def _():
    AIRBNB_URL = 'https://huggingface.co/datasets/BatteRaquette58/airbnb-stock-price/resolve/main/data/airbnb-stock.parquet'
    return (AIRBNB_URL,)


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""## Using `FROM` to query Parquet files""")
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        The simplest way to query a Parquet file is to use it directly in a `FROM` clause, just like you would with a table. DuckDB will automatically detect that it's a Parquet file and read it accordingly.

        Let's query a dataset of Airbnb's stock price from Hugging Face.
        """
    )
    return


@app.cell
def _(AIRBNB_URL, mo, null):
    mo.sql(
        f"""
        SELECT *
        FROM '{AIRBNB_URL}'
        LIMIT 5;
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""## Using `read_parquet`""")
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        For more control, you can use the `read_parquet` table function. This is useful when you need to specify options, for example, when dealing with multiple files or specific data types.
        Some useful options for `read_parquet` include:

        - `binary_as_string=True`: Reads `BINARY` columns as `VARCHAR`.
        - `filename=True`: Adds a `filename` column with the path of the file for each row.
        - `hive_partitioning=True`: Enables reading of Hive-partitioned datasets.

        Here, we'll use `read_parquet` to select only a few relevant columns. This is much more efficient than `SELECT *` because DuckDB only needs to read the data for the columns we specify.
        """
    )
    return


@app.cell
def _(AIRBNB_URL, mo):
    mo.sql(
        f"""
        SELECT Date, Open, "close_last", High, Low
        FROM read_parquet('{AIRBNB_URL}')
        LIMIT 5;
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        You can also read multiple Parquet files at once using a glob pattern. For example, to read all Parquet files in a directory `data/`:

        ```sql
        SELECT * FROM read_parquet('data/*.parquet');
        ```
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""## Creating a table from a Parquet file""")
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
        While querying Parquet files directly is powerful, sometimes it's useful to load the data into a persistent table within your DuckDB database. This can simplify subsequent queries and is a good practice if you'll be accessing the data frequently.
        """
    )
    return


@app.cell
def _(AIRBNB_URL, mo):
    stock_table = mo.sql(
        f"""
        CREATE OR REPLACE TABLE airbnb_stock AS
        SELECT * FROM read_parquet('{AIRBNB_URL}');
        """
    )
    return airbnb_stock, stock_table


@app.cell(hide_code=True)
def _(mo, stock_table):
    mo.md(
        f"""
    {stock_table}

    Now that the `airbnb_stock` table is created, we can query it like any other SQL table.
    """
    )
    return


@app.cell
def _(airbnb_stock, mo):
    mo.sql(
        f"""
        SELECT * FROM airbnb_stock LIMIT 5;
        """
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""## Analysis and Visualization""")
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""Let's perform a simple analysis: plotting the closing stock price over time.""")
    return


@app.cell
def _(airbnb_stock, mo):
    stock_data = mo.sql(
        f"""
        SELECT
        CAST(to_timestamp(Date) AS DATE) AS "Date",
            "close_last"
        FROM airbnb_stock
        ORDER BY "Date";
        """
    )
    return (stock_data,)


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""Now we can easily visualize this result using marimo's integration with plotting libraries like Plotly.""")
    return


@app.cell
def _(px, stock_data):
    px.line(
        stock_data,
        x="Date",
        y="close_last",
        title="Airbnb (ABNB) Stock Price Over Time",
        labels={"Date": "Date", "close_last": "Closing Price (USD)"},
    )
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(r"""## Conclusion""")
    return


@app.cell(hide_code=True)
def _(mo):
    mo.md(
        r"""
    In this notebook, we've seen how easy it is to work with Parquet files in DuckDB. We learned how to:
    <ul>
        <li>Query Parquet files directly from a URL using a simple `FROM` clause.</li>
        <li>Use the `read_parquet` function for more fine-grained control and efficiency.</li>
        <li>Load data from a Parquet file into a DuckDB table.</li>
        <li>Seamlessly analyze and visualize the data using SQL and Python.</li>
    </ul>

    DuckDB's native Parquet support makes it a powerful tool for interactive data analysis on large datasets without complex ETL pipelines.
    """
    )
    return


@app.cell
def _():
    import marimo as mo
    import plotly.express as px
    return mo, px


@app.cell
def _():
    import pyarrow
    return


if __name__ == "__main__":
    app.run()