Datasets:
Remove the old measurement-only query script
Browse files- query_sample.py +0 -80
query_sample.py
DELETED
|
@@ -1,80 +0,0 @@
|
|
| 1 |
-
#!/usr/bin/env python3
|
| 2 |
-
"""Run SQL against the sample Parquet, a local file, or a direct HTTPS URL."""
|
| 3 |
-
import argparse
|
| 4 |
-
from pathlib import Path
|
| 5 |
-
|
| 6 |
-
import duckdb
|
| 7 |
-
|
| 8 |
-
|
| 9 |
-
def main():
|
| 10 |
-
parser = argparse.ArgumentParser(description=__doc__)
|
| 11 |
-
parser.add_argument(
|
| 12 |
-
'source', nargs='?',
|
| 13 |
-
default=str(Path(__file__).with_name('cycling_sample.parquet')),
|
| 14 |
-
help='Local Parquet path or direct public HTTP(S) file URL',
|
| 15 |
-
)
|
| 16 |
-
parser.add_argument('--sql', help='Optional SQL query; the table is named samples')
|
| 17 |
-
parser.add_argument('--check', action='store_true', help='Validate the 600-row demo schema and reference values')
|
| 18 |
-
args = parser.parse_args()
|
| 19 |
-
|
| 20 |
-
with duckdb.connect(':memory:') as con:
|
| 21 |
-
con.read_parquet(args.source).create_view('samples')
|
| 22 |
-
if args.check:
|
| 23 |
-
actual = [(r[0], r[1]) for r in con.sql('DESCRIBE samples').fetchall()]
|
| 24 |
-
expected = [('athlete_id', 'VARCHAR'), ('activity_id', 'INTEGER'), ('secs', 'INTEGER')]
|
| 25 |
-
expected += [(name, 'DOUBLE') for name in ['km', 'power', 'hr', 'cad', 'alt']]
|
| 26 |
-
if actual != expected:
|
| 27 |
-
raise SystemExit(f'Unexpected schema: {actual}')
|
| 28 |
-
values = con.sql('''
|
| 29 |
-
SELECT COUNT(*), MIN(secs), MAX(secs), ROUND(AVG(power), 2),
|
| 30 |
-
COUNT(power), COUNT(DISTINCT (athlete_id, activity_id))
|
| 31 |
-
FROM samples
|
| 32 |
-
''').fetchone()
|
| 33 |
-
if values != (600, 0, 599, 253.3, 600, 1):
|
| 34 |
-
raise SystemExit(f'Unexpected demo values: {values}')
|
| 35 |
-
print('PASS: numeric types, 600 rows, one activity, seconds 0..599, mean power 253.30 W')
|
| 36 |
-
if args.sql:
|
| 37 |
-
con.sql(args.sql).show()
|
| 38 |
-
return
|
| 39 |
-
|
| 40 |
-
print('\nSchema:')
|
| 41 |
-
con.sql('DESCRIBE samples').show()
|
| 42 |
-
|
| 43 |
-
print('\nActivity summary:')
|
| 44 |
-
con.sql('''
|
| 45 |
-
SELECT
|
| 46 |
-
athlete_id,
|
| 47 |
-
activity_id,
|
| 48 |
-
COUNT(*) AS sample_count,
|
| 49 |
-
MIN(secs) AS first_second,
|
| 50 |
-
MAX(secs) AS last_second,
|
| 51 |
-
ROUND(AVG(power), 2) AS average_power_w,
|
| 52 |
-
ROUND(AVG(hr), 2) AS average_heart_rate
|
| 53 |
-
FROM samples
|
| 54 |
-
GROUP BY athlete_id, activity_id
|
| 55 |
-
''').show()
|
| 56 |
-
|
| 57 |
-
print('\nFirst five samples:')
|
| 58 |
-
con.sql('''
|
| 59 |
-
SELECT secs, km, power, hr, cad, alt
|
| 60 |
-
FROM samples
|
| 61 |
-
ORDER BY athlete_id, activity_id, secs
|
| 62 |
-
LIMIT 5
|
| 63 |
-
''').show()
|
| 64 |
-
|
| 65 |
-
print('\nMean power per minute:')
|
| 66 |
-
con.sql('''
|
| 67 |
-
SELECT
|
| 68 |
-
athlete_id,
|
| 69 |
-
activity_id,
|
| 70 |
-
CAST(FLOOR(secs / 60) AS INTEGER) AS minute,
|
| 71 |
-
COUNT(*) AS sample_count,
|
| 72 |
-
ROUND(AVG(power), 2) AS average_power_w
|
| 73 |
-
FROM samples
|
| 74 |
-
GROUP BY athlete_id, activity_id, minute
|
| 75 |
-
ORDER BY athlete_id, activity_id, minute
|
| 76 |
-
''').show()
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
if __name__ == '__main__':
|
| 80 |
-
main()
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|