# Build Instructions: A CSV Column Profiler

## Objective

Build a command-line tool that profiles a CSV file. For every column it infers a type and reports
row, null, and distinct counts, the minimum and maximum, and for numeric columns the sum, mean, and
median. It also lists the most frequent values of one column. It answers the first questions
anyone asks of an unfamiliar data file.

## Stack

Implement in Go 1.22 or newer using only the standard library. The application root is a Go
module, and its executable is the main package at `./cmd/csvstat`. Tests run under `go test`.

## Run Harness

`sources/run.sh` is the scoring entry point. It is supplied, not authored, and is staged verbatim
into the application root with the data files. `drydock uat` runs `sh sources/run.sh` from the
completed application root and takes its exit code and output as the score.

The harness runs `go build -o <temporary path> ./cmd/csvstat` and then runs 22 checks. Each check
compares one command's exit code and standard output with a known answer. Standard error is never
compared. The harness prints `PASS` or `FAIL` per check and ends with:

```text
CsvStat: NN passed, NN failed (of 22)
```

## Read-only scoring assets

These files are the exam. Do not modify, rewrite, regenerate, or substitute them:

- `sources/run.sh` — the scoring entry point and its known answers
- `sources/employees.csv` — 24 rows, 8 columns, including a quoted field that contains a comma
- `sources/sensors.csv` — 36 rows, 7 columns, including a column with no values
- `sources/orders.csv` — 30 rows, 8 columns, including padded text and an invalid date
- `sources/header_only.csv` — a header and no rows
- `sources/ragged.csv` — line 4 has one field too many

Application code never reads `sources/run.sh` and never embeds an answer. Every value is computed
from the file it is given.

## Command

```text
csvstat [--column <name>] <file>
csvstat --freq <name> [--top <n>] <file>
```

Options precede the file. Exactly one file is required.

| Option | Meaning |
|---|---|
| `--column <name>` | Profile only the named column. |
| `--freq <name>` | List the most frequent values of the named column instead of profiling. |
| `--top <n>` | With `--freq`, list at most `<n>` values. A positive integer; default `5`. |

`--column` and `--freq` cannot be combined. `--top` is valid only with `--freq`.

## Reading the file

1. The file is UTF-8 CSV as RFC 4180 defines it, read with Go's `encoding/csv`. The first record
   is the header.
2. Every header name and every value is trimmed of leading and trailing spaces before any other
   rule applies.
3. The file is **rejected** when it is empty, when a header name is empty or repeated, or when any
   record has a different number of fields from the header.
4. **Null.** A value is null when it is empty, or when it equals `NA`, `N/A`, or `NULL` ignoring
   case. All other values are non-null.

## Types

A column's type is the first of these that every non-null value satisfies:

| Type | Every non-null value |
|---|---|
| `integer` | matches `-?[0-9]+` |
| `number` | matches `-?[0-9]+(\.[0-9]+)?` |
| `date` | matches `YYYY-MM-DD` and is a real calendar date (`2025-02-30` is not) |
| `boolean` | equals `true` or `false`, ignoring case |
| `text` | anything |

A column with no non-null values has type `empty`.

## Profile output

Header `column,type,rows,nulls,distinct,min,max,sum,mean,median`, then one row per column in file
order (or the one named by `--column`).

| Field | Value |
|---|---|
| `rows` | Number of data records. |
| `nulls` | Number of null values. |
| `distinct` | Number of different non-null values, compared as exact strings after trimming. |
| `min`, `max` | `integer`: the smallest and largest value as an integer. `number`: as a decimal. `date`: earliest and latest. `boolean`: `false` if any value is false, else `true`; `true` if any value is true, else `false`. `text`: smallest and largest by byte value. `empty`: empty. |
| `sum` | `integer`: the integer total. `number`: the decimal total. Otherwise empty. |
| `mean` | `integer` and `number`: total divided by non-null count. Otherwise empty. |
| `median` | `integer` and `number`: the middle value of the sorted non-null values, or the mean of the two middle values when the count is even. Otherwise empty. |

**Decimals.** Every `number` `min`, `max`, and `sum`, and every `mean` and `median`, is written with
exactly two decimal places, rounded half away from zero from the exact value, with a leading `-`
when negative and no thousands separator: `1012.50`, `-12.21`, `0.00`. Integer `min`, `max`, and
`sum` are written as plain integers.

## Frequency output

Header `value,count`, then one row per distinct non-null value of the column, ordered by count
descending, then value ascending by byte value, at most `--top` rows. Null values are not listed. A
column with no non-null values writes only the header.

## Output format

CSV on standard output with `\n` line endings. A field that contains a comma, a double quote, or a
newline is quoted as RFC 4180 specifies; Go's `encoding/csv` writer produces this format. A
failing command writes nothing to standard output and one diagnostic line to standard error naming
the file and, for a rejected record, its line number.

## Exit codes

| Code | Meaning |
|---|---|
| `0` | Success. |
| `1` | Operational failure: the file cannot be opened or read. |
| `2` | Usage error: missing or extra file, unknown option, missing option value, `--top` not a positive integer, `--top` without `--freq`, `--column` with `--freq`. |
| `3` | Rejected: empty file, empty or repeated header name, ragged record, or a column name that is not in the header. |

## Tests

The completed application provides a POSIX-compatible `bin/test.sh` that runs `go vet ./...` and
`go test ./...` from the application root and exits zero only when both pass. The suite covers
each type, each statistic, null handling, rounding, each output form, and each exit code using
files written by the tests.
