# Parquet and DuckDB

> Read an Asense Parquet export with DuckDB or Polars, convert the stored values to real units in SQL, and start from six runnable examples.

> **Coming soon:** The examples repository, `asense-duckdb-examples`, and the [public dataset](https://asense.semitronica.pt/docs/dataset/) are not available for download yet. The queries on this page are the ones the examples run.

## The export

An Asense Parquet export has one folder per table, each holding Snappy-compressed files sorted by time:

```text
data/
  acc/part-00000.parquet ...
  gyr/ ...
  ain/ ...
  fft/ ...
  data/ ...
  connected/ ...
```

Every folder shares one schema, the columns of the Asense data lake, and a table leaves the columns it does not use empty. The [data model](https://asense.semitronica.pt/docs/data-model/) pages describe each table's fields.

Values are stored as the device sends them, so the queries below convert them in SQL. Some packets were sent before the device clock was set, and their `time` lies in January 1970. Filter them out with `time > 1.6e12`.

## Count what an export holds

DuckDB reads Parquet folders directly, with no import step:

```sql
select count(*),
       min(make_timestamp(time * 1000)) as first,
       max(make_timestamp(time * 1000)) as last
from read_parquet('data/acc/*.parquet')
where time > 1.6e12;
```

## Vibration per hour

Each `acc` packet holds raw counts with x, y and z interleaved in the list `axyz`. A count becomes g with `count × scale × 2^-15`. This query unpacks every packet and returns the vibration RMS per hour and axis, in mg:

```sql
with packets as (
    select time, scale, axyz, unnest(range(1, len(axyz) + 1)) as k
    from read_parquet('data/acc/*.parquet')
    where time > 1.6e12
)
select time_bucket(interval 1 hour, make_timestamp(time * 1000)) as hour,
       ['x', 'y', 'z'][((k - 1) % 3) + 1] as axis,
       stddev_pop(axyz[k] * scale * 2 ** -15) * 1000 as rms_mg
from packets
group by all
order by hour, axis;
```

## Tilt per day

The hourly `data` records store the tilt angles `theta` and `phi` multiplied by 2^24, in degrees, and the temperature `tamb` in °C:

```sql
select date_trunc('day', make_timestamp(time * 1000)) as day,
       avg(theta / 2 ** 24) as theta_deg,
       avg(phi / 2 ** 24) as phi_deg,
       avg(tamb) as temp_c
from read_parquet('data/data/*.parquet')
where time > 1.6e12
group by all
order by day;
```

The two largest spectral peaks per axis in the same records are stored as bin numbers (`nx1`, `nx2`, `ny1` ...). A bin becomes a frequency with `bin × odr / 1024` Hz, where `odr` is the record's own sampling rate.

## The examples

The `asense-duckdb-examples` repository holds six short scripts. Each one asks a single question, runs on the export with DuckDB and Polars, and has a notebook version:

| Example | Question |
|---|---|
| `01_read_one_device.py` | What does a device's export hold, and over which dates? |
| `02_resample_hourly.py` | How does raw vibration look hour by hour, and tilt day by day? |
| `03_peak_frequency.py` | At which frequencies does the device vibrate most, hour by hour? |
| `04_tilt_trend.py` | Is this sensor still tilting? Steps from re-mounting are split off and the temperature part removed, and the slope comes with a 95 % interval |
| `05_fft_spectrum.py` | What does the vibration spectrum look like, and which spectra hold spikes? |
| `06_lden_lnight.py` | How loud is it by day, evening and night? This needs an Environmental Sensor's `env` table |

Run one with `uv run examples/01_read_one_device.py`, after setting `ASENSE_DATA` to the export's `data` folder. The same queries work on any device's export, because every export has the same layout.

Source page: https://asense.semitronica.pt/docs/parquet-duckdb/
