#!/usr/bin/env bash
# Apache Ossie: an introduction to the open semantic layer standard. The tutorial's commands, in order.
# Run in an empty folder that holds prepare.py, compute.py and sales.ossie.yaml.
set -uo pipefail

# Step 1 · Install the official tooling (spec 0.3.0.dev0, commit from 15 November 2026)
git clone -q https://github.com/apache/ossie
git -C ossie checkout -q aa500d3
python3 -m venv .venv
source .venv/bin/activate
pip install -q sqlglot==30.31.0 jsonschema==4.26.0 pyyaml==6.0.3 duckdb==1.5.6 ./ossie/python
pip list | grep ossie

# Step 2 · Generate the TPC-H data and compute the quarter's revenue three ways
python prepare.py

# Step 6 · Validate the model, then the tests
python ossie/validation/validate.py sales.ossie.yaml
sed '1,/SUM(gross_price/s/SUM(gross_price/SUM(l_extendedprice/' sales.ossie.yaml > column-test.yaml
python ossie/validation/validate.py column-test.yaml          # fails: a dataset-level metric reads a column
sed '1,/SUM(gross_price/s/SUM(gross_price/SUM(source.l_extendedprice/' sales.ossie.yaml > source-test.yaml
python ossie/validation/validate.py source-test.yaml          # passes: the column is named source.column
sed 's/from_columns: \[order_id\]/from_columns: [l_orderkey]/' sales.ossie.yaml > relation-test.yaml
python ossie/validation/validate.py relation-test.yaml        # fails: a relationship names a column
sed 's/COUNT(DISTINCT orders.order_id)/COUNT(DISTINCT order_id)/' sales.ossie.yaml > name-test.yaml
python ossie/validation/validate.py name-test.yaml            # passes, with a warning
awk '{print} /^          sql: l_extendedprice$/ {print "          dialects:"; print "            - dialect: TRINO"; print "              expression: l_extendedprice"}' sales.ossie.yaml > trino-test.yaml
python ossie/validation/validate.py trino-test.yaml           # passes: a TRINO variant next to sql

# Step 7 · Compute the metrics as a tool would
python compute.py sales.ossie.yaml tpch.duckdb
