Search OpenAlex for Related Scholarly Works
Search OpenAlex for related scholarly works, shape the response with DuckDB, and export a CSV.
Audit a date-partitioned Parquet lake with DuckDB. Find files outside their day folder and the UTC offset or load date that explains them.
id: parquet-partition-day-alignment-audit
namespace: company.team
description: >
Audit whether each day folder of a Parquet lake holds the events of that day.
It reads the min and max event time of every file with DuckDB, flags the files
that stick out of their day, and tests which UTC offset would put the lake
right, for example a partition day computed in local time from a UTC column.
inputs:
- id: demo_scenario
type: SELECT
displayName: Demo scenario
description: >
Used only when lake_glob is empty. The demo lake holds 23,040 events, one
per minute, over 16 days. IST_SHIFT: the folder day was computed in
UTC+05:30 from a UTC event time. LOAD_DATE: the folder day is the day
after the event. BACKFILL: partitioned in UTC, plus a late backfill file
in the wrong day and a file that spans two days. ALIGNED: partitioned in
UTC.
values:
- IST_SHIFT
- LOAD_DATE
- BACKFILL
- ALIGNED
defaults: IST_SHIFT
- id: lake_glob
type: STRING
required: false
displayName: Parquet files to audit
description: >
Glob for your Parquet files, one folder per day, for example
/data/events/*/*.parquet or s3://bucket/events/*/*.parquet. It must match
only Parquet files and must not contain a single quote or a semicolon.
Leave empty to audit the demo lake that this flow builds itself.
validator: "^[^';]*$"
- id: partition_key
type: STRING
displayName: Partition key
description: Name of the folder level that holds the day, as in dt=2026-09-02.
The value must be YYYY-MM-DD.
defaults: dt
validator: "^[A-Za-z_][A-Za-z0-9_]*$"
- id: time_column
type: STRING
displayName: Event time column
description: >
The column that holds the event time, stored in the files and not a
partition folder. It must be a TIMESTAMP, a TIMESTAMP WITH TIME ZONE or a
TIMESTAMP_NS, and its values are read as UTC. The demo lake also has note
(text) and empty_ts (all NULL), to show the guards.
defaults: event_ts
validator: "^[A-Za-z_][A-Za-z0-9_]*$"
- id: partition_utc_offset_minutes
type: INT
displayName: UTC offset of the partition day (minutes)
description: >
The offset your partition day is meant to follow. 0 means the folder day
is the UTC date of the event. 330 means it is the date in UTC+05:30. The
audit judges every file against this offset and also tries all the others.
defaults: 0
min: -720
max: 840
- id: tolerance_minutes
type: INT
displayName: Tolerance (minutes)
description: >
A file may stick out of its day by this many minutes at either end and
still count as aligned. Use it for late stragglers and small clock skew.
It is not applied to rows_outside, which counts exact rows.
defaults: 0
min: 0
max: 720
- id: method
type: SELECT
displayName: Method
description: >
FOOTER reads the min and max of the column from the file footers and no
data. It cannot judge files written without statistics, such as Spark
files with INT96 timestamps. SCAN reads only the time column, judges every
file and also counts the rows that are outside their day.
values:
- FOOTER
- SCAN
defaults: FOOTER
- id: on_findings
type: SELECT
displayName: On findings
description: >
REPORT ends the run SUCCESS and says what it found. FAIL ends it FAILED
when any file is outside its day, so a schedule can alert on it.
values:
- REPORT
- FAIL
defaults: REPORT
triggers:
- id: daily
type: io.kestra.plugin.core.trigger.Schedule
description: Disabled. In a real pipeline, set the lake_glob, time_column and
offset defaults to your dataset and run this after the daily load.
cron: "0 7 * * *"
disabled: true
tasks:
- id: workspace
type: io.kestra.plugin.core.flow.WorkingDirectory
description: Shares one working directory between the tasks, so the demo lake
and the file ranges are visible to every task.
tasks:
- id: build_demo_lake
type: io.kestra.plugin.jdbc.duckdb.Queries
runIf: "{{ (inputs.lake_glob ?? '') == '' }}"
description: >
Stand-in for your lake, skipped when lake_glob is set. Writes 23,040
events, one per minute from 2026-08-16 18:30 UTC, as two Parquet files
per day folder. The scenario only changes how the folder day is
computed. One thread keeps the files the same on every run.
sql: |
SET threads = 1;
-- One event per minute for 16 days. The first event is local midnight in UTC+05:30.
CREATE TABLE lake AS
SELECT CAST(i AS BIGINT) AS id,
TIMESTAMP '2026-08-16 18:30:00' + to_minutes(CAST(i AS BIGINT)) AS event_ts,
'order ' || i AS note,
CAST(NULL AS TIMESTAMP) AS empty_ts
FROM range(23040) t(i);
-- The scenario is the rule that chose the folder day.
CREATE TABLE placed AS
SELECT *,
CASE '{{ inputs.demo_scenario }}'
WHEN 'IST_SHIFT' THEN CAST(event_ts + to_minutes(330) AS DATE)
WHEN 'LOAD_DATE' THEN CAST(event_ts AS DATE) + 1
ELSE CAST(event_ts AS DATE)
END AS dt
FROM lake;
COPY (SELECT * FROM placed WHERE id % 2 = 0)
TO '{{ workingDir }}/lake' (FORMAT parquet, PARTITION_BY (dt), FILENAME_PATTERN 'a-{i}', OVERWRITE_OR_IGNORE);
COPY (SELECT * FROM placed WHERE id % 2 = 1)
TO '{{ workingDir }}/lake' (FORMAT parquet, PARTITION_BY (dt), FILENAME_PATTERN 'b-{i}', OVERWRITE_OR_IGNORE);
- id: add_demo_strays
type: io.kestra.plugin.jdbc.duckdb.Queries
runIf: "{{ (inputs.lake_glob ?? '') == '' and inputs.demo_scenario == 'BACKFILL'
}}"
description: >
Demo only. Adds two files to the BACKFILL lake: a late backfill of
2026-08-20 events written into the folder of 2026-09-01, and a
compacted file in the folder of 2026-08-28 that holds two days of
events.
sql: |
COPY (
SELECT CAST(2000000 + i AS BIGINT) AS id,
TIMESTAMP '2026-08-20 06:00:00' + to_minutes(CAST(i AS BIGINT)) AS event_ts,
'backfill ' || i AS note,
CAST(NULL AS TIMESTAMP) AS empty_ts
FROM range(600) t(i)
) TO '{{ workingDir }}/lake/dt=2026-09-01/backfill.parquet' (FORMAT parquet);
COPY (
SELECT CAST(3000000 + i AS BIGINT) AS id,
TIMESTAMP '2026-08-27 12:00:00' + to_minutes(CAST(i AS BIGINT) * 4) AS event_ts,
'compacted ' || i AS note,
CAST(NULL AS TIMESTAMP) AS empty_ts
FROM range(720) t(i)
) TO '{{ workingDir }}/lake/dt=2026-08-28/compacted.parquet' (FORMAT parquet);
- id: find_files
type: io.kestra.plugin.jdbc.duckdb.Queries
description: >
List the files lake_glob matches and check that the folder level named
by partition_key exists in their paths and holds dates. A wrong path,
a wrong key or another date format would otherwise look like a lake
with nothing to judge.
fetchType: FETCH_ONE
sql: |
CREATE TABLE files AS
SELECT file FROM glob(COALESCE(NULLIF('{{ inputs.lake_glob ?? '' }}', ''), '{{ workingDir }}/lake/*/*.parquet'));
WITH c AS (
SELECT count(*) AS files_total,
count(*) FILTER (WHERE regexp_matches(file, '(^|/){{ inputs.partition_key }}=[^/]*/')) AS files_with_key,
count(*) FILTER (WHERE try_cast(regexp_extract(file, '(^|/){{ inputs.partition_key }}=([^/]*)/', 2) AS DATE) IS NOT NULL) AS files_with_date
FROM files
)
SELECT files_total, files_with_key, files_with_date,
COALESCE(NULLIF('{{ inputs.lake_glob ?? '' }}', ''), '{{ workingDir }}/lake/*/*.parquet') AS lake_glob,
CASE WHEN files_total = 0 THEN 'NO_FILES'
WHEN files_with_key = 0 THEN 'NO_PARTITION_KEY'
WHEN files_with_date = 0 THEN 'NO_DATES'
ELSE 'OK' END AS scan_status,
CASE WHEN files_total = 0 THEN 'lake_glob matched no files. A wrong path would otherwise look like an empty lake. Nothing was audited.'
WHEN files_with_key = 0 THEN files_total || ' files matched, but none has a {{ inputs.partition_key }}= folder in its path. Check partition_key. Nothing was audited.'
WHEN files_with_date = 0 THEN files_with_key || ' files have a {{ inputs.partition_key }}= folder, but none of its values is a date such as 2026-09-02. Nothing was audited.'
ELSE files_total || ' files matched.' END AS reason
FROM c;
- id: scan_guard
type: io.kestra.plugin.core.execution.Fail
runIf: "{{ (outputs.find_files.outputs | last).row.scan_status != 'OK' }}"
description: Stop before the audit when there is nothing valid to audit.
errorMessage: "BLOCKED_{{ (outputs.find_files.outputs | last).row.scan_status
}}: {{ (outputs.find_files.outputs | last).row.reason }}"
- id: inspect_column
type: io.kestra.plugin.jdbc.duckdb.Queries
description: >
Check that the column exists in the files and is a timestamp type. A
column that is missing, a partition folder or a text column would
otherwise be judged as if every file had no usable times.
fetchType: FETCH_ONE
sql: |
CREATE TABLE cols AS
SELECT column_name, column_type
FROM (DESCRIBE SELECT * FROM read_parquet('{{ (outputs.find_files.outputs | last).row.lake_glob }}', hive_partitioning = false, union_by_name = true))
WHERE lower(column_name) = lower('{{ inputs.time_column }}');
WITH c AS (
SELECT (SELECT count(*) FROM cols) AS found,
(SELECT any_value(column_type) FROM cols) AS column_type,
(SELECT regexp_matches(any_value(column_type), '^(TIMESTAMP( WITH TIME ZONE)?|TIMESTAMP_S|TIMESTAMP_MS|TIMESTAMP_NS)$') FROM cols) AS type_ok
)
SELECT column_type,
CASE WHEN found = 0 THEN 'COLUMN_NOT_FOUND'
WHEN NOT type_ok THEN 'UNSUPPORTED_TYPE'
ELSE 'OK' END AS column_status,
CASE WHEN found = 0 THEN '{{ inputs.time_column }} is not a top-level column stored in the files. A partition folder such as dt=2026-09-02 is not stored in them. Nothing was audited.'
WHEN NOT type_ok THEN '{{ inputs.time_column }} has type ' || column_type || '. The audit needs a TIMESTAMP, a TIMESTAMP WITH TIME ZONE or a TIMESTAMP_NS. Nothing was audited.'
ELSE '{{ inputs.time_column }} is ' || column_type || '.' END AS reason
FROM c;
- id: column_guard
type: io.kestra.plugin.core.execution.Fail
runIf: "{{ (outputs.inspect_column.outputs | last).row.column_status != 'OK' }}"
description: Stop before the audit when the column cannot be judged.
errorMessage: "BLOCKED_{{ (outputs.inspect_column.outputs |
last).row.column_status }}: {{ (outputs.inspect_column.outputs |
last).row.reason }}"
- id: read_ranges
type: io.kestra.plugin.jdbc.duckdb.Queries
description: >
Get the first and last event time of every file, as UTC, and write
them with the folder day and the row count to ranges.parquet in the
working directory. FOOTER reads the footers only. SCAN reads the time
column. Only the branch of the method you chose reads anything. Empty
files and files without a valid day in their path are counted and left
out. Nothing is changed.
fetchType: FETCH_ONE
sql: |
-- One row per file from the footers, with its folder day. A zero row file has nothing to judge.
CREATE TABLE pfm AS
SELECT file_name, num_rows,
try_cast(regexp_extract(file_name, '(^|/){{ inputs.partition_key }}=([^/]*)/', 2) AS DATE) AS day
FROM parquet_file_metadata('{{ (outputs.find_files.outputs | last).row.lake_glob }}');
-- FOOTER: min and max of the column per row group, folded into one range per file. A row group that has values but
-- no statistics makes the file unusable. A TIMESTAMP WITH TIME ZONE statistic is converted to UTC without the session time zone.
CREATE TABLE footer AS
SELECT file_name AS f,
CAST(sum(row_group_num_rows) AS BIGINT) AS n_rows,
min(lo) AS lo, max(hi) AS hi,
count(*) FILTER (WHERE COALESCE(stats_null_count, -1) <> num_values AND (lo IS NULL OR hi IS NULL)) AS groups_unranged
FROM (
SELECT file_name, row_group_num_rows, num_values, stats_null_count,
CASE WHEN '{{ (outputs.inspect_column.outputs | last).row.column_type }}' = 'TIMESTAMP WITH TIME ZONE'
THEN make_timestamp(epoch_us(TRY_CAST(stats_min_value AS TIMESTAMP WITH TIME ZONE)))
ELSE TRY_CAST(stats_min_value AS TIMESTAMP) END AS lo,
CASE WHEN '{{ (outputs.inspect_column.outputs | last).row.column_type }}' = 'TIMESTAMP WITH TIME ZONE'
THEN make_timestamp(epoch_us(TRY_CAST(stats_max_value AS TIMESTAMP WITH TIME ZONE)))
ELSE TRY_CAST(stats_max_value AS TIMESTAMP) END AS hi
FROM parquet_metadata('{{ (outputs.find_files.outputs | last).row.lake_glob }}')
WHERE lower(path_in_schema) = lower('{{ inputs.time_column }}')
AND '{{ inputs.method }}' = 'FOOTER'
)
GROUP BY file_name;
-- SCAN: the same ranges from the data, plus the rows whose own day is not the folder day at the chosen offset.
CREATE TABLE scan AS
SELECT filename AS f, count(*) AS n_rows, min(ts) AS lo, max(ts) AS hi,
count(*) FILTER (WHERE ts IS NOT NULL AND day IS NOT NULL
AND CAST(ts + to_minutes({{ inputs.partition_utc_offset_minutes }}) AS DATE) <> day) AS rows_outside,
any_value(day) AS day
FROM (
SELECT filename,
try_cast(regexp_extract(filename, '(^|/){{ inputs.partition_key }}=([^/]*)/', 2) AS DATE) AS day,
CASE WHEN '{{ (outputs.inspect_column.outputs | last).row.column_type }}' = 'TIMESTAMP WITH TIME ZONE'
THEN make_timestamp(epoch_us("{{ inputs.time_column }}"))
ELSE CAST("{{ inputs.time_column }}" AS TIMESTAMP) END AS ts
FROM read_parquet('{{ (outputs.find_files.outputs | last).row.lake_glob }}', hive_partitioning = false, union_by_name = true, filename = true)
WHERE '{{ inputs.method }}' = 'SCAN'
)
GROUP BY filename;
CREATE TABLE ranges AS
SELECT f, try_cast(regexp_extract(f, '(^|/){{ inputs.partition_key }}=([^/]*)/', 2) AS DATE) AS day,
n_rows, CAST(NULL AS BIGINT) AS rows_outside, lo, hi
FROM footer
WHERE groups_unranged = 0 AND lo IS NOT NULL AND hi IS NOT NULL
UNION ALL
SELECT f, day, n_rows, rows_outside, lo, hi
FROM scan
WHERE lo IS NOT NULL;
COPY (SELECT * FROM ranges WHERE day IS NOT NULL)
TO '{{ workingDir }}/ranges.parquet' (FORMAT parquet);
WITH c AS (
SELECT (SELECT count(*) FROM pfm WHERE num_rows = 0) AS files_empty,
(SELECT count(*) FROM pfm WHERE num_rows > 0 AND day IS NULL) AS files_without_day,
(SELECT count(*) FROM pfm WHERE num_rows > 0 AND day IS NOT NULL) AS files_judgeable,
(SELECT count(*) FROM ranges WHERE day IS NOT NULL) AS files_ranged
)
SELECT files_empty, files_without_day, files_judgeable, files_ranged,
files_judgeable - files_ranged AS files_no_times,
CASE WHEN files_ranged = 0 THEN 'NO_TIMES' ELSE 'OK' END AS ranges_status,
CASE WHEN files_ranged = 0 AND '{{ inputs.method }}' = 'FOOTER'
THEN 'None of the ' || files_judgeable || ' files has min and max statistics for {{ inputs.time_column }} in its footer. The column is all NULL, or the files were written without statistics, as Spark does with INT96 timestamps. Set method to SCAN to read the column itself. Nothing was audited.'
WHEN files_ranged = 0
THEN '{{ inputs.time_column }} is NULL in every row of the ' || files_judgeable || ' files. Nothing was audited.'
ELSE files_ranged || ' of ' || files_judgeable || ' files have an event time range.' END AS reason
FROM c;
- id: ranges_guard
type: io.kestra.plugin.core.execution.Fail
runIf: "{{ (outputs.read_ranges.outputs | last).row.ranges_status != 'OK' }}"
description: Stop before the audit when no file has an event time range.
errorMessage: "BLOCKED_{{ (outputs.read_ranges.outputs | last).row.ranges_status
}}: {{ (outputs.read_ranges.outputs | last).row.reason }}"
- id: audit
type: io.kestra.plugin.jdbc.duckdb.Queries
description: >
Judge every file against the day of its folder at the offset you set,
then try every offset from -24 hours to +24 hours in 15 minute steps
and find the one that puts the most files inside their day. It reads
only ranges.parquet and changes nothing.
fetchType: FETCH_ONE
outputFiles:
- misaligned_files
- offset_scores
sql: |
CREATE MACRO off_label(m) AS
CASE WHEN m < 0 THEN '-' ELSE '+' END || lpad(CAST(abs(m) // 60 AS VARCHAR), 2, '0') || ':' || lpad(CAST(abs(m) % 60 AS VARCHAR), 2, '0');
-- The offset and the tolerance in microseconds. A day is 86,400,000,000 of them.
CREATE TABLE k AS
SELECT CAST({{ inputs.partition_utc_offset_minutes }} AS BIGINT) * 60000000 AS o_us,
CAST({{ inputs.tolerance_minutes }} AS BIGINT) * 60000000 AS tol_us;
CREATE TABLE r AS
SELECT f, day, n_rows, rows_outside, lo, hi,
epoch_us(lo) AS lo_us, epoch_us(hi) AS hi_us, epoch_us(CAST(day AS TIMESTAMP)) AS s_us
FROM read_parquet('{{ workingDir }}/ranges.parquet');
-- Shifted by the offset, a file is ALIGNED when its whole range is inside its day, give or take the tolerance. Else it
-- spans more than a day, or sits in another day, or crosses an edge of its own day.
CREATE TABLE st AS
SELECT r.*,
CASE WHEN r.lo_us + k.o_us >= r.s_us - k.tol_us AND r.hi_us + k.o_us < r.s_us + 86400000000 + k.tol_us THEN 'ALIGNED'
WHEN r.hi_us - r.lo_us >= 86400000000 THEN 'SPANS_DAYS'
WHEN r.hi_us + k.o_us < r.s_us OR r.lo_us + k.o_us >= r.s_us + 86400000000 THEN 'MISFILED'
ELSE 'SPILLS_OVER' END AS status
FROM r, k;
-- Every offset on the grid, scored by how many files it puts inside their day.
CREATE TABLE grid AS
SELECT CAST(o AS BIGINT) AS o, CAST(o AS BIGINT) * 60000000 AS o_us
FROM range(-1440, 1441, 15) t(o);
CREATE TABLE score AS
SELECT g.o,
count(*) FILTER (WHERE r.lo_us + g.o_us >= r.s_us - k.tol_us AND r.hi_us + g.o_us < r.s_us + 86400000000 + k.tol_us) AS aligned
FROM r, grid g, k
GROUP BY g.o;
-- The best offset. On a tie the one closest to yours wins, so a lake that is already right keeps its offset.
CREATE TABLE best AS
SELECT o AS best_o, aligned AS best_aligned, CAST(o AS BIGINT) * 60000000 AS best_us
FROM score
ORDER BY aligned DESC, abs(o - ({{ inputs.partition_utc_offset_minutes }})), o
LIMIT 1;
-- Offsets next to the best one that score the same. The files may not cover enough of a day to tell them apart.
CREATE TABLE band AS
WITH top AS (
SELECT o, o // 15 - row_number() OVER (ORDER BY o) AS grp
FROM score
WHERE aligned = (SELECT best_aligned FROM best)
)
SELECT min(o) AS band_from, max(o) AS band_to
FROM top
WHERE grp = (SELECT grp FROM top WHERE o = (SELECT best_o FROM best));
CREATE TABLE sm AS
SELECT count(*) AS files, count(DISTINCT day) AS partitions,
CAST(min(day) AS VARCHAR) AS first_day, CAST(max(day) AS VARCHAR) AS last_day,
count(*) FILTER (WHERE status = 'ALIGNED') AS aligned_files,
count(*) FILTER (WHERE status = 'SPILLS_OVER') AS spills_files,
count(*) FILTER (WHERE status = 'MISFILED') AS misfiled_files,
count(*) FILTER (WHERE status = 'SPANS_DAYS') AS spans_files,
CAST(sum(n_rows) AS BIGINT) AS rows_total,
CAST(COALESCE(sum(n_rows) FILTER (WHERE status <> 'ALIGNED'), 0) AS BIGINT) AS rows_in_misaligned_files,
CAST(sum(rows_outside) AS BIGINT) AS rows_outside
FROM st;
CREATE TABLE bad AS
SELECT st.f AS file_name, CAST(st.day AS VARCHAR) AS partition_day, st.status, st.n_rows AS rows, st.rows_outside,
CAST(st.lo AS VARCHAR) AS first_event, CAST(st.hi AS VARCHAR) AS last_event,
round(greatest(0, st.s_us - (st.lo_us + k.o_us)) / 3600000000.0, 2) AS hours_before_day,
round(greatest(0, (st.hi_us + k.o_us) - (st.s_us + 86400000000)) / 3600000000.0, 2) AS hours_after_day,
(st.lo_us + best.best_us >= st.s_us - k.tol_us AND st.hi_us + best.best_us < st.s_us + 86400000000 + k.tol_us) AS fits_at_best_offset
FROM st, k, best
WHERE st.status <> 'ALIGNED'
ORDER BY st.n_rows DESC, st.f
LIMIT 1000;
CREATE TABLE top_scores AS
SELECT 'UTC' || off_label(o) AS utc_offset, o AS offset_minutes, aligned AS files_inside_their_day
FROM score
ORDER BY aligned DESC, abs(o - ({{ inputs.partition_utc_offset_minutes }})), o
LIMIT 10;
COPY bad TO '{{ outputFiles.misaligned_files }}' (HEADER, DELIMITER ',');
COPY top_scores TO '{{ outputFiles.offset_scores }}' (HEADER, DELIMITER ',');
WITH d AS (
SELECT sm.*, best.best_o, best.best_aligned, band.band_from, band.band_to,
sm.files - sm.aligned_files AS misaligned_files,
best.best_aligned - sm.aligned_files AS gain,
{{ inputs.partition_utc_offset_minutes }} AS current_o
FROM sm, best, band
), x AS (
SELECT *,
CASE WHEN misaligned_files = 0 THEN 'ALIGNED'
WHEN gain >= 2 AND gain * 2 >= misaligned_files THEN 'SHIFT_DETECTED'
ELSE 'MISALIGNED' END AS decision,
CASE WHEN best_o = 0 THEN 'the folder day is the UTC date of the event time'
WHEN best_o > 840 AND band_to = 1440 THEN 'the folder day is one day after the event day, as when the load date or the run date is used as the partition'
WHEN best_o < -720 AND band_from = -1440 THEN 'the folder day is one day before the event day'
WHEN best_o BETWEEN -720 AND 840 THEN 'the folder day is the date of the event time in UTC' || off_label(best_o)
|| ', as when a writer computes the day in local time from a UTC column'
ELSE 'the folder day is the event time shifted by ' || round(best_o / 60.0, 2) || ' hours, which is not a time zone' END AS cause
FROM d
)
SELECT decision,
CASE decision
WHEN 'ALIGNED' THEN 'Audited ' || files || ' files in ' || partitions || ' partitions (' || first_day || ' to ' || last_day
|| ') at UTC' || off_label(current_o) || ': every file holds only events of the day in its folder.'
WHEN 'SHIFT_DETECTED' THEN misaligned_files || ' of ' || files || ' files hold events outside the day in their folder at UTC' || off_label(current_o)
|| '. At UTC' || off_label(best_o) || ' ' || best_aligned || ' of ' || files || ' would be inside, so ' || cause || '.'
|| CASE WHEN band_from <> band_to THEN ' The files do not cover enough of a day to tell the offsets from UTC' || off_label(band_from)
|| ' to UTC' || off_label(band_to) || ' apart, and all of them fit equally.' ELSE '' END
ELSE misaligned_files || ' of ' || files || ' files hold events outside the day in their folder at UTC' || off_label(current_o)
|| ' (crossing a day edge: ' || spills_files || ', in another day: ' || misfiled_files || ', spanning more than a day: ' || spans_files || '). '
|| CASE WHEN gain > 0 THEN 'The best single offset, UTC' || off_label(best_o) || ', puts only ' || gain || ' more files inside their day, so one shifted rule does not explain them.'
ELSE 'No other offset puts more files inside their day, so one shifted rule does not explain them.' END
END
|| CASE WHEN {{ (outputs.read_ranges.outputs | last).row.files_no_times }} > 0 THEN ' ' || {{ (outputs.read_ranges.outputs | last).row.files_no_times }}
|| ' file(s) have no usable event time and were not judged.' ELSE '' END
|| CASE WHEN {{ (outputs.read_ranges.outputs | last).row.files_without_day }} > 0 THEN ' ' || {{ (outputs.read_ranges.outputs | last).row.files_without_day }}
|| ' file(s) have no valid {{ inputs.partition_key }}= date in their path and were not judged.' ELSE '' END AS reason,
'{{ inputs.method }}' AS method,
'{{ inputs.time_column }}' AS column_name,
files, partitions, first_day, last_day,
aligned_files, spills_files, misfiled_files, spans_files,
rows_total, rows_in_misaligned_files, rows_outside,
current_o AS current_offset_minutes, best_o AS best_offset_minutes, band_from AS best_band_from_minutes, band_to AS best_band_to_minutes,
best_aligned AS files_inside_at_best, files - best_aligned AS files_outside_at_best,
CASE WHEN decision = 'SHIFT_DETECTED' THEN cause ELSE '' END AS probable_cause,
CASE WHEN decision = 'SHIFT_DETECTED' THEN
CASE WHEN best_o BETWEEN -720 AND 840
THEN 'Two ways out. If UTC' || off_label(best_o) || ' is the rule you want, set partition_utc_offset_minutes to ' || best_o || '. If '
ELSE 'The files follow a shift that partition_utc_offset_minutes cannot express. If ' END
|| 'UTC' || off_label(current_o) || ' is the rule you want, fix the writer and rewrite the files into those days with: COPY (SELECT *, CAST(make_timestamp(epoch_us("{{ inputs.time_column }}")) + to_minutes(' || current_o
|| ') AS DATE) AS {{ inputs.partition_key }} FROM read_parquet(' || chr(39) || '{{ (outputs.find_files.outputs | last).row.lake_glob }}' || chr(39)
|| ', hive_partitioning = false, union_by_name = true)) TO ' || chr(39) || '<new folder>' || chr(39)
|| ' (FORMAT parquet, PARTITION_BY ({{ inputs.partition_key }}))'
ELSE '' END AS suggestion
FROM x;
- id: result
type: io.kestra.plugin.core.output.OutputValues
description: The row of audit, so the Switch and the logs read one place.
values:
row: "{{ (outputs.audit.outputs | last).row | toJson }}"
- id: log_result
type: io.kestra.plugin.core.log.Log
message: "{{ outputs.result.values.row | jq('.reason') | first }}"
- id: route
type: io.kestra.plugin.core.flow.Switch
description: >
ALIGNED logs and ends. SHIFT_DETECTED and MISALIGNED warn, and fail the
run when on_findings is FAIL.
value: "{{ outputs.result.values.row | jq('.decision') | first }}"
cases:
ALIGNED:
- id: log_aligned
type: io.kestra.plugin.core.log.Log
message: "No action needed. Every judged file is inside the day of its folder."
SHIFT_DETECTED:
- id: log_shift
type: io.kestra.plugin.core.log.Log
level: WARN
message: >-
One offset explains the misaligned files. {{
outputs.result.values.row | jq('.suggestion') | first }}. The files
and the offsets tried are in outputs.audit.outputFiles.
- id: fail_shift
type: io.kestra.plugin.core.execution.Fail
runIf: "{{ inputs.on_findings == 'FAIL' }}"
errorMessage: "SHIFT_DETECTED: {{ outputs.result.values.row | jq('.reason') |
first }}"
MISALIGNED:
- id: log_misaligned
type: io.kestra.plugin.core.log.Log
level: WARN
message: "Files sit in the wrong day and no single offset explains them. The
files are in outputs.audit.outputFiles.misaligned_files."
- id: fail_misaligned
type: io.kestra.plugin.core.execution.Fail
runIf: "{{ inputs.on_findings == 'FAIL' }}"
errorMessage: "MISALIGNED: {{ outputs.result.values.row | jq('.reason') | first }}"
defaults:
- id: unexpected_decision
type: io.kestra.plugin.core.execution.Fail
errorMessage: "Unexpected decision {{ outputs.result.values.row |
jq('.decision') | first }}. Check the audit task logs."
errors:
- id: report_failure
type: io.kestra.plugin.core.log.Log
level: ERROR
message: "Partition day alignment audit failed in {{ flow.id }}, execution {{
execution.id }}. A BLOCKED_ message means the files, the partition key or
the time column cannot be used as given and nothing was audited.
SHIFT_DETECTED or MISALIGNED means on_findings is FAIL. Otherwise read the
task logs: an unreadable Parquet file stops the scan and its path is in
the error."
Daily Parquet partitions are only as good as the code that chose the day. If a writer computed dt in local time from a UTC column, the folder dt=2026-09-02 holds events from the evening of Sep 1 to the afternoon of Sep 2, and every query that filters on dt is wrong by hours. This flow checks it from the file footers and tells you which offset would put the lake right.
build_demo_lake writes a deterministic demo lake with DuckDB, unless lake_glob points at your own files. The default demo was partitioned in UTC+05:30.find_files, inspect_column and read_ranges stop the run with a BLOCKED_ message when the glob matches nothing, there is no dt= date folder, the column is missing or not a timestamp, or no file has an event time range.read_ranges gets the first and last event time of every file from its footer (method: FOOTER), or scans only that column (method: SCAN, for Spark INT96 files and exact row counts).audit marks each file ALIGNED, SPILLS_OVER, MISFILED or SPANS_DAYS, then tries every UTC offset from -24h to +24h in 15 minute steps and reports the one that fits the most files.Switch routes ALIGNED, SHIFT_DETECTED and MISALIGNED.on_findings: FAIL turns a new partition bug into a failed run that can alert.dt=2026-09-02/.s3:// or gs:// paths needs DuckDB storage access that this flow does not set up.demo_scenario: BACKFILL to see files in the wrong day that no offset explains.lake_glob, time_column and the offset your partitions should follow, then enable the daily trigger.Search OpenAlex for related scholarly works, shape the response with DuckDB, and export a CSV.
Put a JSON settings document into OpenSearch under a known key, Get it back, and log a field from the returned document.
A weekly Request task deletes the app-logs index dated thirty days before the run and confirms the deletion in Discord with the HTTP status.
A size-zero OpenSearch search counts yesterday's level ERROR log documents with date math and posts the total to Slack every morning.
Extract data lineage facets and schema during in-process DuckDB analytical SQL transformations, emitting OpenLineage 1.0 events to Marquez with automated quality gating.
Check an index's ISM policy attachment through the ISM Explain API, alert Slack on a gap or an unreachable index, and optionally attach the expected policy with the ISM Add Policy API.
