static USAGE: &str = r#"
Compute summary statistics & infers data types for each column in a CSV.
> IMPORTANT: `stats` is heavily optimized for speed. It ASSUMES the CSV is well-formed & UTF-8 encoded.
> This allows it to employ numerous performance optimizations (skip repetitive UTF-8 validation, skip
> bounds checks, cache results, etc.) that may result in undefined behavior if the CSV is not well-formed.
> All these optimizations are GUARANTEED to work with well-formed CSVs.
> If you encounter problems generating stats, use `qsv validate` FIRST to confirm the CSV is valid.
> For MAXIMUM PERFORMANCE, create an index for the CSV first with 'qsv index' to enable multithreading,
> or set --cache-threshold option or set the QSV_AUTOINDEX_SIZE environment variable to automatically
> create an index when the file size is greater than the specified size (in bytes).
Summary stats include sum, min/max/range, sort order/sortiness, min/max/sum/avg/stddev/variance/cv length,
mean, standard error of the mean (SEM), geometric mean, harmonic mean, stddev, variance, coefficient of
variation (CV), nullcount, n_negative, n_zero, n_positive, max_precision, sparsity,
Median Absolute Deviation (MAD), quartiles, lower/upper inner/outer fences, skewness, median,
cardinality/uniqueness ratio, mode/s & "antimode/s" & percentiles.
Note that some stats require loading the entire file into memory, so they must be enabled explicitly.
By default, the following "streaming" statistics are reported for *every* column:
sum, min/max/range values, sort order/"sortiness", min/max/sum/avg/stddev/variance/cv length, mean, sem,
geometric_mean, harmonic_mean,stddev, variance, cv, nullcount, n_negative, n_zero, n_positive,
max_precision & sparsity.
The default set of statistics corresponds to ones that can be computed efficiently on a stream of data
(i.e., constant memory) and works with arbitrarily large CSVs.
The following additional "non-streaming, advanced" statistics require loading the entire file into memory:
cardinality/uniqueness ratio, modes/antimodes, median, MAD, quartiles and its related measures
(q1, q2, q3, IQR, lower/upper fences & skewness) and percentiles.
When computing "non-streaming" statistics, a memory-aware chunking algorithm is used to dynamically
calculate chunk size based on available memory & record sampling. This SHOULD help process arbitrarily
large "real-world" files by creating smaller chunks that fit in available memory.
However, there is still a chance that the command will run out of memory if the cardinality of
several columns is very high.
Chunk size is dynamically calculated based on the number of logical CPUs detected.
You can override this behavior by setting the QSV_STATS_CHUNK_MEMORY_MB environment variable
(set to 0 for dynamic sizing, or a positive number for a fixed memory limit per chunk,
or -1 for CPU-based chunking (1 chunk = records/number of CPUs)).
"Antimode" is the least frequently occurring non-zero value and is the opposite of mode.
It returns "*ALL" if all the values are unique, and only returns a preview of the first
10 antimodes, truncating after 100 characters (configurable with QSV_ANTIMODES_LEN).
If you need all the antimode values of a column, run the `frequency` command with --limit set
to zero. The resulting frequency table will have all the "antimode" values.
Summary statistics for dates are also computed when --infer-dates is enabled, with DateTime
results in rfc3339 format and Date results in "yyyy-mm-dd" format in the UTC timezone.
Date range, stddev, variance, MAD & IQR are returned in days, not timestamp milliseconds.
Each column's data type is also inferred (NULL, Integer, String, Float, Date, DateTime and
Boolean with --infer-boolean option).
For String data types, it also determines if the column is all ASCII characters.
Unlike the sniff command, stats' data type inferences are GUARANTEED, as the entire file
is scanned, and not just sampled.
Note that the Date and DateTime data types are only inferred with the --infer-dates option
as its an expensive operation to match a date candidate against 19 possible date formats,
with each format, having several variants.
The date formats recognized and its sub-variants along with examples can be found at
https://github.com/dathere/qsv-dateparser?tab=readme-ov-file#accepted-date-formats.
Computing statistics on a large file can be made MUCH faster if you create an index for it
first with 'qsv index' to enable multithreading. With an index, the file is split into chunks
and each chunk is processed in parallel.
As stats is a central command in qsv, and can be expensive to compute, `stats` caches results
in <FILESTEM>.stats.csv & if the --stats-json option is used, <FILESTEM>.stats.csv.data.jsonl
(e.g., qsv stats nyc311.csv will create nyc311.stats.csv & nyc311.stats.csv.data.jsonl).
The arguments used to generate the cached stats are saved in <FILESTEM>.stats.csv.jsonl.
If stats have already been computed for the input file with similar arguments and the file
hasn't changed, the stats will be loaded from the cache instead of recomputing it.
These cached stats are also used by other qsv commands (currently `describegpt`, `frequency`,
`joinp`, `pivotp`, `schema`, `sqlp` & `tojsonl`) to work smarter & faster.
If the cached stats are not current (i.e., the input file is newer than the cached stats),
the cached stats will be ignored and recomputed.
Examples:
# Compute "streaming" statistics for "nyc311.csv"
qsv stats nyc311.csv
# Compute all statistics for "nyc311.csv"
qsv stats --everything nyc311.csv
# Compute all statistics for "nyc311.tsv" (Tab-separated)
qsv stats -E nyc311.tsv
# Compute all stats for "nyc311.tsv", inferring dates using sniff to auto-detect date columns
qsv stats -E --infer-dates nyc311.tsv
# Compute all stats for "nyc311.tab", inferring dates only for columns
# with "_date" & "_dte" in the column names
qsv stats -E --infer-dates --dates-whitelist _date,_dte nyc311.tab
# Compute all stats, infer dates and boolean data types for "nyc311.ssv" file
qsv stats -E --infer-dates --infer-boolean nyc311.ssv
# In addition to basic "streaming" stats, also compute cardinality for "nyc311.csv"
qsv stats --cardinality nyc311.csv
# Prefer DMY format when inferring dates for the "nyc311.csv"
qsv stats -E --infer-dates --prefer-dmy nyc311.csv
# Infer data types only for the "nyc311.csv" file:
qsv stats --typesonly nyc311.csv
# Infer data types only, including boolean and date types for "nyc311.csv"
$ qsv stats --typesonly --infer-boolean --infer-dates nyc311.csv
# Automatically create an index for the "nyc311.csv" file to enable multithreading
# if it's larger than 5MB and there is no existing index file:
qsv stats -E --cache-threshold -5000000 nyc311.csv
# Auto-create a TEMPORARY index for the "nyc311.csv" file to enable multithreading
# if it's larger than 5MB and delete the index and the stats cache file after the stats run:
qsv stats -E --cache-threshold -5000005 nyc311.csv
For more examples, see https://github.com/dathere/qsv/tree/master/resources/test
If the polars feature is enabled, support additional tabular file formats and
compression formats:
$ qsv stats data.parquet // Parquet
$ qsv stats data.avro // Avro
$ qsv stats data.jsonl // JSON Lines
$ qsv stats data.json (will only work with a JSON Array)
$ qsv stats data.csv.gz // Gzipped CSV
$ qsv stats data.tab.zlib // Zlib-compressed Tab-separated
$ qsv stats data.ssv.zst // Zstd-compressed Semicolon-separated
For more info, see https://github.com/dathere/qsv/blob/master/docs/STATS_DEFINITIONS.md
Usage:
qsv stats [options] [<input>]
qsv stats --help
stats options:
-s, --select <arg> Select a subset of columns to compute stats for.
See 'qsv select --help' for the format details.
This is provided here because piping 'qsv select'
into 'qsv stats' will prevent the use of indexing.
-E, --everything Compute all statistics available.
--typesonly Infer data types only and do not compute statistics.
Note that if you want to infer dates and boolean types, you'll
still need to use the --infer-dates & --infer-boolean options.
BOOLEAN INFERENCING:
--infer-boolean Infer boolean data type. This automatically enables
the --cardinality option. When a column's cardinality is 2,
and the 2 values' are in the true/false patterns specified
by --boolean-patterns, the data type is inferred as boolean.
--boolean-patterns <arg> Comma-separated list of boolean pattern pairs in the format
"true_pattern:false_pattern". Each pattern can be a string
of any length. The patterns are case-insensitive. If a pattern
ends with a "*", it is treated as a prefix. For example,
"t*:f*,y*:n*" will match "true", "truthy", "Truth" as boolean true
values so long as the corresponding false pattern (e.g. False, f, etc.)
is also matched & cardinality is 2. Ignored if --infer-boolean is false.
[default: 1:0,t*:f*,y*:n*]
--mode Compute the mode/s & antimode/s. Multimodal-aware.
If there are multiple modes/antimodes, they are separated by the
QSV_STATS_SEPARATOR environment variable. If not set, the default
separator is "|".
Uses memory proportional to the cardinality of each column.
--cardinality Compute the cardinality and the uniqueness ratio.
This is automatically enabled if --infer-boolean is enabled.
https://en.wikipedia.org/wiki/Cardinality_(SQL_statements)
Uses memory proportional to the number of unique values in each column.
NUMERIC & DATE/DATETIME STATS THAT REQUIRE IN-MEMORY SORTING:
The following statistics are only computed for numeric & date/datetime
columns & require loading & sorting ALL the selected columns' data
in memory FIRST before computing the statistics.
--median Compute the median.
Loads & sorts all the selected columns' data in memory.
https://en.wikipedia.org/wiki/Median
--mad Compute the median absolute deviation (MAD).
https://en.wikipedia.org/wiki/Median_absolute_deviation
--quartiles Compute the quartiles (using method 3), the IQR, the lower/upper,
inner/outer fences and skewness.
https://en.wikipedia.org/wiki/Quartile#Method_3
--percentiles Compute custom percentiles using the nearest rank method.
https://en.wikipedia.org/wiki/Percentile#The_nearest-rank_method
--percentile-list <arg> Comma-separated list of percentiles to compute.
For example, "5,10,40,60,90,95" will compute percentiles
5th, 10th, 40th, 60th, 90th, and 95th.
Multiple percentiles are separated by the QSV_STATS_SEPARATOR
environment variable. If not set, the default separator is "|".
It is ignored if --percentiles is not set.
Special values "deciles" and "quintiles" are automatically expanded
to "10,20,30,40,50,60,70,80,90" and "20,40,60,80" respectively.
[default: 5,10,40,60,90,95]
--round <decimal_places> Round statistics to <decimal_places>. Rounding is done following
Midpoint Nearest Even (aka "Bankers Rounding") rule.
https://docs.rs/rust_decimal/latest/rust_decimal/enum.RoundingStrategy.html
If set to the sentinel value 9999, no rounding is done.
For dates - range, stddev & IQR are always at least 5 decimal places as
they are reported in days, and 5 places gives us millisecond precision.
[default: 4]
--nulls Include NULLs in the population size for computing
mean and standard deviation.
--weight <column> Compute weighted statistics using the specified column as weights.
The weight column must be numeric. When specified, all statistics
(mean, stddev, variance, median, quartiles, mode, etc.) will be
computed using weighted algorithms. The weight column is automatically
excluded from statistics computation. Missing or non-numeric weights
default to 1.0. Zero and negative weights are ignored and do not
contribute to the statistics. The output filename will be
<FILESTEM>.stats.weighted.csv to distinguish from unweighted statistics.
DATE INFERENCING:
--infer-dates Infer date/datetime data types. This is an expensive
option and should only be used when you know there
are date/datetime fields.
Also, if timezone is not specified in the data, it'll
be set to UTC.
--dates-whitelist <list> The comma-separated, case-insensitive patterns to look for when
shortlisting fields for date inferencing.
i.e. if the field's name has any of these patterns,
it is shortlisted for date inferencing.
Special values:
* "all" - inspect ALL fields for date/datetime types
* "sniff" - use `qsv sniff` to auto-detect date/datetime columns
Note that false positive date matches WILL most likely occur
when using "all" as unix epoch timestamps are just numbers.
Be sure to only use "all" if you know ALL the columns you're
inspecting are dates, boolean or string fields.
To avoid false positives, preprocess the file first
with the `datefmt` command to convert unix epoch timestamp
columns to RFC3339 format.
When set to "sniff", we do two-stage date inferencing.
First running sniff on the input file and then second,
only inferring dates for the columns that sniff identifies
as date/datetime candidates.
This is much faster than "all", and more convenient than
manually specifying patterns in the whitelist.
[default: sniff]
--prefer-dmy Parse dates in dmy format. Otherwise, use mdy format.
Ignored if --infer-dates is false.
--force Force recomputing stats even if valid precomputed stats
cache exists.
-j, --jobs <arg> The number of jobs to run in parallel.
This works only when the given CSV has an index.
Note that a file handle is opened for each job.
When not set, the number of jobs is set to the
number of CPUs detected.
--stats-jsonl Also write the stats in JSONL format.
If set, the stats will be written to <FILESTEM>.stats.csv.data.jsonl.
Note that this option used internally by other qsv "smart" commands (see
https://github.com/dathere/qsv/blob/master/docs/PERFORMANCE.md#stats-cache)
to load cached stats to make them work smarter & faster.
You can preemptively create the stats-jsonl file by using this option
BEFORE running "smart" commands and they will automatically use it.
-c, --cache-threshold <arg> Controls the creation of stats cache files.
* when greater than 1, the threshold in milliseconds before caching
stats results. If a stats run takes longer than this threshold,
the stats results will be cached.
* 0 to suppress caching.
* 1 to force caching.
* a negative number to automatically create an index when
the input file size is greater than abs(arg) in bytes.
If the negative number ends with 5, it will delete the index
file and the stats cache file after the stats run. Otherwise,
the index file and the cache files are kept.
[default: 5000]
--vis-whitespace Visualize whitespace characters in the output.
See https://github.com/dathere/qsv/wiki/Supplemental#whitespace-markers
for the list of whitespace markers.
Common options:
-h, --help Display this message
-o, --output <file> Write output to <file> instead of stdout.
-n, --no-headers When set, the first row will NOT be interpreted
as column names. i.e., They will be included
in statistics.
-d, --delimiter <arg> The field delimiter for READING CSV data.
Must be a single character. (default: ,)
--memcheck Check if there is enough memory to load the entire
CSV into memory using CONSERVATIVE heuristics.
This option is ignored when computing default, streaming
statistics, as it is not needed.
"#;
use std::{
fmt, fs,
io::{self, BufRead, Seek, Write},
iter::repeat_n,
path::{Path, PathBuf},
str,
sync::OnceLock,
};
use blake3;
use crossbeam_channel;
use foldhash::{HashMap, HashMapExt};
use itertools::Itertools;
use phf::phf_map;
use qsv_dateparser::parse_with_preference;
use rayon::{
iter::{IntoParallelRefIterator, ParallelIterator},
slice::ParallelSliceMut,
};
use serde::{Deserialize, Serialize};
#[cfg(target_endian = "little")]
use simd_json::{OwnedValue, prelude::ValueAsScalar, prelude::ValueObjectAccess};
use smallvec::SmallVec;
use stats::{Commute, MinMax, OnlineStats, Unsorted, merge_all};
use tempfile::NamedTempFile;
use threadpool::ThreadPool;
use self::FieldType::{TDate, TDateTime, TFloat, TInteger, TNull, TString};
use crate::{
CliError, CliResult,
config::{Config, Delimiter, get_delim_by_extension},
select::{SelectColumns, Selection},
util,
};
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Clone, Deserialize)]
pub struct Args {
pub arg_input: Option<String>,
pub flag_select: SelectColumns,
pub flag_everything: bool,
pub flag_typesonly: bool,
pub flag_infer_boolean: bool,
pub flag_boolean_patterns: String,
pub flag_mode: bool,
pub flag_cardinality: bool,
pub flag_median: bool,
pub flag_mad: bool,
pub flag_quartiles: bool,
pub flag_percentiles: bool,
pub flag_percentile_list: String,
pub flag_round: u32,
pub flag_nulls: bool,
pub flag_infer_dates: bool,
pub flag_dates_whitelist: String,
pub flag_prefer_dmy: bool,
pub flag_force: bool,
pub flag_jobs: Option<usize>,
pub flag_stats_jsonl: bool,
pub flag_cache_threshold: isize,
pub flag_output: Option<String>,
pub flag_no_headers: bool,
pub flag_delimiter: Option<Delimiter>,
pub flag_memcheck: bool,
pub flag_vis_whitespace: bool,
pub flag_weight: Option<String>,
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Default)]
struct StatsArgs {
arg_input: String,
flag_select: String,
flag_everything: bool,
flag_typesonly: bool,
flag_infer_boolean: bool,
flag_mode: bool,
flag_cardinality: bool,
flag_median: bool,
flag_mad: bool,
flag_quartiles: bool,
flag_percentiles: bool,
flag_percentile_list: String,
flag_round: u32,
flag_nulls: bool,
flag_infer_dates: bool,
flag_dates_whitelist: String,
flag_prefer_dmy: bool,
flag_no_headers: bool,
flag_delimiter: String,
flag_output_snappy: bool,
canonical_input_path: String,
canonical_stats_path: String,
record_count: u64,
date_generated: String,
compute_duration_ms: u64,
qsv_version: String,
flag_weight: String,
field_count: u64,
filesize_bytes: u64,
hash: FileHash,
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Default)]
struct FileHash {
#[serde(rename = "BLAKE3", skip_serializing_if = "String::is_empty")]
blake3: String,
}
#[cfg(target_endian = "little")]
impl StatsArgs {
fn from_owned_value(value: &OwnedValue) -> Result<Self, Box<dyn std::error::Error>> {
let get_str = |key: &str| -> String {
value
.get(key)
.and_then(|v| v.as_str())
.unwrap_or_default()
.to_string()
};
let get_str_or = |key: &str, default: &str| -> String {
value
.get(key)
.and_then(|v| v.as_str())
.unwrap_or(default)
.to_string()
};
let get_bool = |key: &str| -> bool {
value
.get(key)
.and_then(simd_json::prelude::ValueAsScalar::as_bool)
.unwrap_or_default()
};
let get_u64 = |key: &str| -> u64 {
value
.get(key)
.and_then(simd_json::prelude::ValueAsScalar::as_u64)
.unwrap_or_default()
};
let get_hash = || -> FileHash {
value
.get("hash")
.map(|h| FileHash {
blake3: h
.get("BLAKE3")
.and_then(|v| v.as_str())
.unwrap_or_default()
.to_string(),
})
.unwrap_or_default()
};
Ok(Self {
arg_input: get_str("arg_input"),
flag_select: get_str("flag_select"),
flag_everything: get_bool("flag_everything"),
flag_typesonly: get_bool("flag_typesonly"),
flag_infer_boolean: get_bool("flag_infer_boolean"),
flag_mode: get_bool("flag_mode"),
flag_cardinality: get_bool("flag_cardinality"),
flag_median: get_bool("flag_median"),
flag_mad: get_bool("flag_mad"),
flag_quartiles: get_bool("flag_quartiles"),
flag_percentiles: get_bool("flag_percentiles"),
flag_percentile_list: get_str_or("flag_percentile_list", "5,10,40,60,90,95"),
flag_round: get_u64("flag_round") as u32,
flag_nulls: get_bool("flag_nulls"),
flag_infer_dates: get_bool("flag_infer_dates"),
flag_dates_whitelist: get_str("flag_dates_whitelist"),
flag_prefer_dmy: get_bool("flag_prefer_dmy"),
flag_no_headers: get_bool("flag_no_headers"),
flag_delimiter: get_str("flag_delimiter"),
flag_output_snappy: get_bool("flag_output_snappy"),
canonical_input_path: get_str("canonical_input_path"),
canonical_stats_path: get_str("canonical_stats_path"),
record_count: get_u64("record_count"),
date_generated: get_str("date_generated"),
compute_duration_ms: get_u64("compute_duration_ms"),
qsv_version: get_str("qsv_version"),
flag_weight: get_str("flag_weight"),
field_count: get_u64("field_count"),
filesize_bytes: get_u64("filesize_bytes"),
hash: get_hash(),
})
}
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Default, Debug)]
pub struct StatsData {
pub field: String,
pub r#type: String,
#[serde(default)]
pub is_ascii: bool,
pub sum: Option<f64>,
pub min: Option<String>,
pub max: Option<String>,
pub range: Option<f64>,
pub sort_order: Option<String>,
pub min_length: Option<usize>,
pub max_length: Option<usize>,
pub sum_length: Option<usize>,
pub avg_length: Option<f64>,
pub stddev_length: Option<f64>,
pub variance_length: Option<f64>,
pub cv_length: Option<f64>,
pub mean: Option<f64>,
pub sem: Option<f64>,
pub stddev: Option<f64>,
pub variance: Option<f64>,
pub cv: Option<f64>,
pub nullcount: u64,
pub n_negative: Option<u64>,
pub n_zero: Option<u64>,
pub n_positive: Option<u64>,
pub max_precision: Option<u32>,
pub sparsity: Option<f64>,
pub mad: Option<f64>,
pub lower_outer_fence: Option<f64>,
pub lower_inner_fence: Option<f64>,
pub q1: Option<f64>,
pub q2_median: Option<f64>,
pub q3: Option<f64>,
pub iqr: Option<f64>,
pub upper_inner_fence: Option<f64>,
pub upper_outer_fence: Option<f64>,
pub skewness: Option<f64>,
pub cardinality: u64,
pub uniqueness_ratio: Option<f64>,
pub mode: Option<String>,
pub mode_count: Option<u64>,
pub mode_occurrences: Option<u64>,
pub antimode: Option<String>,
pub antimode_count: Option<u64>,
pub antimode_occurrences: Option<u64>,
}
#[derive(Clone, Serialize, Deserialize, PartialEq, Eq)]
pub enum JsonTypes {
Int,
Float,
Bool,
String,
}
pub static STATSDATA_TYPES_MAP: phf::Map<&'static str, JsonTypes> = phf_map! {
"field" => JsonTypes::String,
"type" => JsonTypes::String,
"is_ascii" => JsonTypes::Bool,
"sum" => JsonTypes::Float,
"min" => JsonTypes::String,
"max" => JsonTypes::String,
"range" => JsonTypes::Float,
"sort_order" => JsonTypes::String,
"sortiness" => JsonTypes::Float,
"min_length" => JsonTypes::Int,
"max_length" => JsonTypes::Int,
"sum_length" => JsonTypes::Int,
"avg_length" => JsonTypes::Float,
"stddev_length" => JsonTypes::Float,
"variance_length" => JsonTypes::Float,
"cv_length" => JsonTypes::Float,
"mean" => JsonTypes::Float,
"sem" => JsonTypes::Float,
"geometric_mean" => JsonTypes::Float,
"harmonic_mean" => JsonTypes::Float,
"stddev" => JsonTypes::Float,
"variance" => JsonTypes::Float,
"cv" => JsonTypes::Float,
"nullcount" => JsonTypes::Int,
"n_negative" => JsonTypes::Int,
"n_zero" => JsonTypes::Int,
"n_positive" => JsonTypes::Int,
"max_precision" => JsonTypes::Int,
"sparsity" => JsonTypes::Float,
"mad" => JsonTypes::Float,
"lower_outer_fence" => JsonTypes::Float,
"lower_inner_fence" => JsonTypes::Float,
"q1" => JsonTypes::Float,
"q2_median" => JsonTypes::Float,
"q3" => JsonTypes::Float,
"iqr" => JsonTypes::Float,
"upper_inner_fence" => JsonTypes::Float,
"upper_outer_fence" => JsonTypes::Float,
"skewness" => JsonTypes::Float,
"cardinality" => JsonTypes::Int,
"uniqueness_ratio" => JsonTypes::Float,
"mode" => JsonTypes::String,
"mode_count" => JsonTypes::Int,
"mode_occurrences" => JsonTypes::Int,
"antimode" => JsonTypes::String,
"antimode_count" => JsonTypes::Int,
"antimode_occurrences" => JsonTypes::Int,
};
static INFER_DATE_FLAGS: OnceLock<SmallVec<[bool; 50]>> = OnceLock::new();
static RECORD_COUNT: OnceLock<u64> = OnceLock::new();
static ANTIMODES_LEN: OnceLock<usize> = OnceLock::new();
static STATS_SEPARATOR: OnceLock<String> = OnceLock::new();
static STATS_STRING_MAX_LENGTH: OnceLock<Option<usize>> = OnceLock::new();
const OVERFLOW_STRING: &str = "*OVERFLOW*";
const UNDERFLOW_STRING: &str = "*UNDERFLOW*";
const MS_IN_DAY: f64 = 86_400_000.0;
const MS_IN_DAY_INT: i64 = 86_400_000;
const DAY_DECIMAL_PLACES: u32 = 5;
const MAX_STAT_COLUMNS: usize = 47;
const FINGERPRINT_HASH_COLUMNS: usize = 26;
const MAX_ANTIMODES: usize = 10;
const PAR_SORT_THRESHOLD: usize = 10_000;
const DEFAULT_ANTIMODES_LEN: usize = 100;
pub const DEFAULT_STATS_SEPARATOR: &str = "|";
static BOOLEAN_PATTERNS: OnceLock<Vec<BooleanPattern>> = OnceLock::new();
#[derive(Clone, Debug)]
struct BooleanPattern {
true_pattern: String,
false_pattern: String,
}
impl BooleanPattern {
fn matches(&self, value: &str) -> Option<bool> {
let value_lower = value.to_lowercase();
if value_lower == self.true_pattern {
return Some(true);
} else if value_lower == self.false_pattern {
return Some(false);
}
if self.true_pattern.ends_with('*') {
let prefix = &self.true_pattern[..self.true_pattern.len() - 1];
if value_lower.starts_with(prefix) {
return Some(true);
}
}
if self.false_pattern.ends_with('*') {
let prefix = &self.false_pattern[..self.false_pattern.len() - 1];
if value_lower.starts_with(prefix) {
return Some(false);
}
}
None
}
}
fn parse_boolean_patterns(boolean_patterns: &str) -> CliResult<Vec<BooleanPattern>> {
let mut patterns = Vec::new();
for pair in boolean_patterns.split(',') {
let mut parts = pair.split(':');
let true_pattern = parts.next().unwrap_or("").trim().to_lowercase();
let false_pattern = parts.next().unwrap_or("").trim().to_lowercase();
if true_pattern.is_empty() || false_pattern.is_empty() {
return fail_incorrectusage_clierror!("Invalid boolean pattern: {pair}");
}
patterns.push(BooleanPattern {
true_pattern,
false_pattern,
});
}
if patterns.is_empty() {
return fail_incorrectusage_clierror!("Boolean patterns must have at least one pattern");
}
Ok(patterns)
}
pub fn run(argv: &[&str]) -> CliResult<()> {
let mut args: Args = util::get_args(USAGE, argv)?;
if args.flag_typesonly {
args.flag_everything = false;
args.flag_mode = false;
args.flag_cardinality = false;
args.flag_median = false;
args.flag_quartiles = false;
args.flag_mad = false;
}
if args.flag_percentile_list.to_lowercase() == "deciles" {
args.flag_percentile_list = "10,20,30,40,50,60,70,80,90".to_string();
} else if args.flag_percentile_list.to_lowercase() == "quintiles" {
args.flag_percentile_list = "20,40,60,80".to_string();
}
let percentile_list = args.flag_percentile_list.split(',').collect::<Vec<&str>>();
for p in percentile_list {
if fast_float2::parse::<f64, &[u8]>(p.trim().as_bytes()).is_err() {
return fail_incorrectusage_clierror!(
"Invalid percentile list: {}: {}",
args.flag_percentile_list,
p
);
}
}
if args.flag_infer_boolean {
if !args.flag_cardinality {
args.flag_cardinality = true;
}
let patterns = parse_boolean_patterns(&args.flag_boolean_patterns)?;
let _ = BOOLEAN_PATTERNS.set(patterns);
}
args.flag_prefer_dmy = args.flag_prefer_dmy || util::get_envvar_flag("QSV_PREFER_DMY");
let stdout_output_flag = args.flag_output.is_none();
let mut current_stats_args = StatsArgs {
arg_input: args.arg_input.clone().unwrap_or_default(),
flag_select: format!("{:?}", args.flag_select),
flag_everything: args.flag_everything,
flag_typesonly: args.flag_typesonly,
flag_infer_boolean: args.flag_infer_boolean,
flag_mode: args.flag_mode,
flag_cardinality: args.flag_cardinality,
flag_median: args.flag_median,
flag_mad: args.flag_mad,
flag_quartiles: args.flag_quartiles,
flag_percentiles: args.flag_percentiles,
flag_percentile_list: args.flag_percentile_list.clone(),
flag_round: args.flag_round,
flag_nulls: args.flag_nulls,
flag_infer_dates: args.flag_infer_dates,
flag_dates_whitelist: args.flag_dates_whitelist.clone(),
flag_prefer_dmy: args.flag_prefer_dmy,
flag_no_headers: args.flag_no_headers,
flag_delimiter: args
.flag_delimiter
.as_ref()
.map(|d| (d.as_byte() as char).to_string())
.unwrap_or_default(),
flag_output_snappy: if stdout_output_flag {
false
} else {
let p = args.flag_output.clone().unwrap();
p.to_ascii_lowercase().ends_with(".sz")
},
canonical_input_path: String::new(),
canonical_stats_path: String::new(),
record_count: 0,
date_generated: String::new(),
compute_duration_ms: 0,
qsv_version: env!("CARGO_PKG_VERSION").to_string(),
flag_weight: args.flag_weight.clone().unwrap_or_default(),
field_count: 0,
filesize_bytes: 0,
hash: FileHash::default(),
};
let stats_csv_tempfile = if current_stats_args.flag_output_snappy {
tempfile::Builder::new().suffix(".sz").tempfile()?
} else {
NamedTempFile::new()?
};
let (output_extension, output_delim, snappy) = match args.flag_output {
Some(ref output_path) => get_delim_by_extension(Path::new(&output_path), b','),
_ => (String::new(), b',', false),
};
let stats_csv_tempfile_fname = format!(
"{stem}.{prime_ext}{snappy_ext}",
stem = stats_csv_tempfile.path().to_str().unwrap(),
prime_ext = output_extension,
snappy_ext = if snappy { ".sz" } else { "" }
);
let wconfig = Config::new(Some(stats_csv_tempfile_fname.clone()).as_ref())
.delimiter(Some(Delimiter(output_delim)));
let mut wtr = wconfig.writer()?;
let mut rconfig = args.rconfig();
if let Some(format_error) = rconfig.format_error {
return fail_incorrectusage_clierror!("{format_error}");
}
let mut stdin_tempfile_path = None;
if rconfig.is_stdin() {
log::info!("Reading from stdin");
let temp_dir =
crate::config::TEMP_FILE_DIR.get_or_init(|| tempfile::TempDir::new().unwrap().keep());
let mut stdin_file = tempfile::Builder::new().tempfile_in(temp_dir)?;
let stdin = std::io::stdin();
let mut stdin_handle = stdin.lock();
std::io::copy(&mut stdin_handle, &mut stdin_file)?;
drop(stdin_handle);
let (mut preview_file, tempfile_path) = stdin_file
.keep()
.or(Err("Cannot keep temporary file".to_string()))?;
if std::env::var("QSV_DEFAULT_DELIMITER").is_err() {
preview_file.seek(std::io::SeekFrom::Start(0))?;
let mut first_line = String::new();
let mut reader = io::BufReader::new(&preview_file);
reader.read_line(&mut first_line)?;
let tab_count = first_line.matches('\t').count();
let semicolon_count = first_line.matches(';').count();
let comma_count = first_line.matches(',').count();
let space_groups = first_line
.split(|c: char| !c.is_whitespace())
.filter(|s| !s.is_empty())
.count();
let inferred = if tab_count > 0
|| (space_groups > 2 && comma_count == 0 && semicolon_count == 0)
{
"\t"
} else if semicolon_count > 0 && semicolon_count >= comma_count {
";"
} else {
","
};
unsafe { std::env::set_var("QSV_DEFAULT_DELIMITER", inferred) };
}
stdin_tempfile_path = Some(tempfile_path.clone());
args.arg_input = Some(tempfile_path.to_string_lossy().to_string());
rconfig.path = Some(tempfile_path);
} else {
if let Some(path) = rconfig.path.clone()
&& !path.exists()
{
return fail_clierror!("File {:?} does not exist", path.display());
}
}
let resolved_whitelist =
if args.flag_infer_dates && args.flag_dates_whitelist.eq_ignore_ascii_case("sniff") {
if let Some(ref path) = rconfig.path {
log::info!("Resolving dates-whitelist 'sniff' for {}", path.display());
resolve_sniff_whitelist(path)?
} else {
args.flag_dates_whitelist.clone()
}
} else {
args.flag_dates_whitelist.clone()
};
current_stats_args
.flag_dates_whitelist
.clone_from(&resolved_whitelist);
let mut compute_stats = true;
let mut create_cache = args.flag_cache_threshold == 1
|| args.flag_stats_jsonl
|| args.flag_cache_threshold.is_negative();
let mut autoindex_set = false;
let write_stats_jsonl = args.flag_stats_jsonl;
if let Some(path) = rconfig.path.clone() {
let path_file_stem = path.file_stem().unwrap().to_str().unwrap();
let stats_file = stats_path(&path, false, args.flag_weight.is_some())?;
if stats_file.exists() && !args.flag_force {
let stats_args_json_file = stats_file.with_extension("csv.json");
let existing_stats_args_json_str =
match fs::read_to_string(stats_args_json_file.clone()) {
Ok(s) => s,
Err(e) => {
log::warn!(
"Could not read {path_file_stem}.stats.csv.json: {e:?}, recomputing..."
);
let _ = fs::remove_file(&stats_file);
let _ = fs::remove_file(&stats_args_json_file);
String::new()
},
};
if !existing_stats_args_json_str.is_empty() {
let time_saved: u64;
let existing_stats_args_json: StatsArgs = {
#[cfg(target_endian = "big")]
let mut stat_args =
match serde_json::from_str::<StatsArgs>(&existing_stats_args_json_str) {
Ok(args) => args,
Err(e) => {
log::warn!(
"Could not deserialize {path_file_stem}.stats.csv.json: \
{e:?}, recomputing..."
);
let _ = fs::remove_file(&stats_file);
let _ = fs::remove_file(&stats_args_json_file);
StatsArgs::default()
},
};
#[cfg(target_endian = "little")]
let mut stat_args = {
let mut json_buffer = existing_stats_args_json_str.into_bytes();
match simd_json::to_owned_value(&mut json_buffer) {
Ok(value) => match StatsArgs::from_owned_value(&value) {
Ok(args) => args,
Err(e) => {
log::warn!(
"Could not deserialize {path_file_stem}.stats.csv.json: \
{e:?}, recomputing..."
);
let _ = fs::remove_file(&stats_file);
let _ = fs::remove_file(&stats_args_json_file);
StatsArgs::default()
},
},
Err(e) => {
log::warn!(
"Could not parse {path_file_stem}.stats.csv.json: {e:?}, \
recomputing..."
);
let _ = fs::remove_file(&stats_file);
let _ = fs::remove_file(&stats_args_json_file);
StatsArgs::default()
},
}
};
stat_args.canonical_input_path = String::new();
stat_args.canonical_stats_path = String::new();
stat_args.record_count = 0;
stat_args.date_generated = String::new();
time_saved = stat_args.compute_duration_ms;
stat_args.compute_duration_ms = 0;
stat_args.field_count = 0;
stat_args.filesize_bytes = 0;
stat_args.hash = FileHash::default();
stat_args
};
let input_file_modified = fs::metadata(&path)?.modified()?;
let stats_file_modified = fs::metadata(&stats_file)
.and_then(|m| m.modified())
.unwrap_or(input_file_modified);
#[allow(clippy::nonminimal_bool)]
if stats_file_modified > input_file_modified
&& (existing_stats_args_json == current_stats_args
|| existing_stats_args_json.flag_everything
&& existing_stats_args_json.flag_infer_dates
== current_stats_args.flag_infer_dates
&& existing_stats_args_json.flag_dates_whitelist
== current_stats_args.flag_dates_whitelist
&& existing_stats_args_json.flag_prefer_dmy
== current_stats_args.flag_prefer_dmy
&& existing_stats_args_json.flag_no_headers
== current_stats_args.flag_no_headers
&& existing_stats_args_json.flag_delimiter
== current_stats_args.flag_delimiter
&& existing_stats_args_json.flag_nulls == current_stats_args.flag_nulls
&& existing_stats_args_json.qsv_version
== current_stats_args.qsv_version)
{
log::info!(
"{path_file_stem}.stats.csv already exists and is current. Skipping \
compute and using cached stats instead - {time_saved} milliseconds \
saved...",
);
compute_stats = false;
} else {
log::info!(
"{path_file_stem}.stats.csv already exists, but is older than the input \
file or the args have changed, recomputing...",
);
let _ = fs::remove_file(&stats_file);
}
}
}
if compute_stats {
let start_time = std::time::Instant::now();
if args.flag_cache_threshold.is_negative() {
rconfig.autoindex_size = args.flag_cache_threshold.unsigned_abs() as u64;
autoindex_set = true;
}
let mut indexed_result = rconfig.indexed()?;
let will_use_parallel = match &indexed_result {
Some(_) => {
match args.flag_jobs {
Some(num_jobs) => num_jobs != 1,
_ => true, }
},
None => false, };
if !will_use_parallel
&& (args.flag_everything
|| args.flag_mode
|| args.flag_cardinality
|| args.flag_median
|| args.flag_quartiles
|| args.flag_mad
|| args.flag_percentiles)
{
match util::mem_file_check(&path, false, args.flag_memcheck) {
Ok(_) => {
},
Err(e) => {
if indexed_result.is_none() && !rconfig.is_stdin() {
log::info!(
"File too large for sequential processing. Auto-creating index to \
enable parallel processing..."
);
match util::create_index_for_file(&path, &rconfig) {
Ok(()) => {
indexed_result = rconfig.indexed()?;
if indexed_result.is_some() {
log::info!(
"Index created successfully. Switching to parallel \
processing."
);
} else {
return Err(e);
}
},
Err(index_err) => {
log::warn!("Failed to auto-create index: {index_err}");
return Err(e);
},
}
} else {
return Err(e);
}
},
}
}
let record_count: u64;
let (headers, stats) = match indexed_result {
None => {
record_count = util::count_rows(&rconfig).unwrap();
args.sequential_stats(&resolved_whitelist)
},
Some(idx) => {
record_count = idx.count();
match args.flag_jobs {
Some(num_jobs) => {
if num_jobs == 1 {
args.sequential_stats(&resolved_whitelist)
} else {
args.parallel_stats(&resolved_whitelist, record_count)
}
},
_ => args.parallel_stats(&resolved_whitelist, record_count),
}
},
}?;
let _ = RECORD_COUNT.set(record_count);
let stats_sr_vec = args.stats_to_records(stats, args.flag_vis_whitespace);
let mut work_br;
let mut stats_br_vec: Vec<csv::ByteRecord> = Vec::with_capacity(stats_sr_vec.len());
let stats_headers_sr = args.stats_headers();
wtr.write_record(&stats_headers_sr)?;
let fields = headers.iter().zip(stats_sr_vec);
for (i, (header, stat)) in fields.enumerate() {
let header = if args.flag_no_headers {
i.to_string().into_bytes()
} else {
header.to_vec()
};
let stat = stat.iter().map(str::as_bytes);
work_br = vec![&*header]
.into_iter()
.chain(stat)
.collect::<csv::ByteRecord>();
wtr.write_record(&work_br)?;
stats_br_vec.push(work_br);
}
let ds_column_count = headers.len() as u64;
let ds_filesize_bytes = fs::metadata(&path)?.len();
let stats_hash = {
let mut hash_input = Vec::with_capacity(FINGERPRINT_HASH_COLUMNS);
for record in &stats_br_vec {
for field in record.iter().take(FINGERPRINT_HASH_COLUMNS) {
let s = String::from_utf8_lossy(field);
if let Ok(f) = s.parse::<f64>() {
hash_input.extend_from_slice(format!("{f:.10}").as_bytes());
} else {
hash_input.extend_from_slice(field);
}
hash_input.push(0x1F); }
hash_input.push(b'\n');
}
hash_input.extend_from_slice(
format!("{record_count}\x1F{ds_column_count}\x1F{ds_filesize_bytes}\n")
.as_bytes(),
);
blake3::hash(hash_input.as_slice()).to_hex().to_string()
};
current_stats_args.field_count = ds_column_count;
current_stats_args.filesize_bytes = ds_filesize_bytes;
current_stats_args.hash = FileHash { blake3: stats_hash };
current_stats_args.compute_duration_ms = start_time.elapsed().as_millis() as u64;
create_cache = create_cache
|| current_stats_args.compute_duration_ms > args.flag_cache_threshold as u64;
if create_cache {
current_stats_args.canonical_input_path =
path.canonicalize()?.to_str().unwrap().to_string();
current_stats_args.record_count = record_count;
current_stats_args.date_generated = chrono::Utc::now().to_rfc3339();
}
}
}
wtr.flush()?;
if let Some(pb) = stdin_tempfile_path {
std::fs::remove_file(pb)?;
}
let currstats_filename = if compute_stats {
stats_csv_tempfile_fname
} else {
stats_path(
rconfig.path.as_ref().unwrap(),
false,
args.flag_weight.is_some(),
)?
.to_str()
.unwrap()
.to_owned()
};
if rconfig.is_stdin() {
let mut stats_pathbuf = stats_path(
rconfig.path.as_ref().unwrap(),
true,
args.flag_weight.is_some(),
)?;
fs::copy(currstats_filename.clone(), stats_pathbuf.clone())?;
stats_pathbuf.set_extension("csv.json");
#[cfg(target_endian = "big")]
let json_string = serde_json::to_string_pretty(¤t_stats_args)?;
#[cfg(target_endian = "little")]
let json_string = simd_json::to_string_pretty(¤t_stats_args)?;
std::fs::write(stats_pathbuf, json_string)?;
} else if let Some(path) = rconfig.path {
let mut stats_pathbuf = path.clone();
if args.flag_weight.is_some() {
stats_pathbuf.set_extension("stats.weighted.csv");
} else {
stats_pathbuf.set_extension("stats.csv");
}
if currstats_filename != stats_pathbuf.to_str().unwrap() {
fs::copy(currstats_filename.clone(), stats_pathbuf.clone())?;
}
if args.flag_cache_threshold == 0
|| (args.flag_cache_threshold.is_negative() && args.flag_cache_threshold % 10 == -5)
{
if autoindex_set {
let index_file = path.with_extension("csv.idx");
log::debug!("deleting index file: {}", index_file.display());
if std::fs::remove_file(index_file.clone()).is_err() {
log::warn!("Could not remove index file: {}", index_file.display());
}
}
if fs::remove_file(stats_pathbuf.clone()).is_err() {
log::warn!(
"Could not remove stats cache file: {}",
stats_pathbuf.display()
);
}
create_cache = false;
}
if compute_stats && create_cache {
stats_pathbuf.set_extension("csv.json");
std::fs::File::create(stats_pathbuf.clone())?;
current_stats_args.canonical_stats_path = stats_pathbuf
.clone()
.canonicalize()?
.to_str()
.unwrap()
.to_string();
#[cfg(target_endian = "big")]
let json_string = serde_json::to_string_pretty(¤t_stats_args)?;
#[cfg(target_endian = "little")]
let json_string = simd_json::to_string_pretty(¤t_stats_args)?;
std::fs::write(stats_pathbuf.clone(), json_string)?;
if write_stats_jsonl {
let mut stats_jsonl_pathbuf = stats_pathbuf.clone();
stats_jsonl_pathbuf.set_extension("data.jsonl");
util::csv_to_jsonl(
&currstats_filename,
&STATSDATA_TYPES_MAP,
&stats_jsonl_pathbuf,
)?;
}
}
}
if stdout_output_flag {
let currstats = fs::read_to_string(currstats_filename)?;
io::stdout().write_all(currstats.as_bytes())?;
io::stdout().flush()?;
} else if let Some(output) = args.flag_output {
if currstats_filename != output {
fs::copy(currstats_filename, output)?;
}
}
Ok(())
}
impl Args {
fn sequential_stats(&self, whitelist: &str) -> CliResult<(csv::ByteRecord, Vec<Stats>)> {
let mut rdr = self.rconfig().reader()?;
let full_headers = rdr.byte_headers()?.clone();
let (weight_col_idx, sel, headers) =
self.process_headers_with_weight_exclusion(&full_headers)?;
init_date_inference(self.flag_infer_dates, &headers, whitelist)?;
let stats = self.compute(&sel, rdr.byte_records(), weight_col_idx);
Ok((headers, stats))
}
fn parallel_stats(
&self,
whitelist: &str,
idx_count: u64,
) -> CliResult<(csv::ByteRecord, Vec<Stats>)> {
if idx_count == 0 {
return self.sequential_stats(whitelist);
}
let mut rdr = self.rconfig().reader()?;
let full_headers = rdr.byte_headers()?.clone();
let (weight_col_idx, sel, headers) =
self.process_headers_with_weight_exclusion(&full_headers)?;
init_date_inference(self.flag_infer_dates, &headers, whitelist)?;
let njobs = util::njobs(self.flag_jobs);
let max_chunk_memory_mb = if let Ok(val) = std::env::var("QSV_STATS_CHUNK_MEMORY_MB") {
atoi_simd::parse::<u64>(val.as_bytes()).ok()
} else {
Some(0) };
let which_stats = self.which_stats();
let needs_memory_aware_chunking =
which_stats.needs_memory_aware_chunking() && max_chunk_memory_mb.is_some();
let (chunking_mode_info, chunk_size) =
if max_chunk_memory_mb.is_some() || needs_memory_aware_chunking {
let sample_records = util::sample_records(&self.rconfig(), 1000);
let chunk_size = calculate_memory_aware_chunk_size(
idx_count,
njobs,
max_chunk_memory_mb,
&which_stats,
sample_records.as_deref(),
);
let avg_record_size = if let Some(samples) = sample_records {
calculate_avg_record_size(&samples, &which_stats)
} else {
1024
};
let estimated_memory_mb =
estimate_chunk_memory(chunk_size, avg_record_size, &which_stats, headers.len())
/ (1024 * 1024);
let chunking_mode = if let Some(limit_mb) = max_chunk_memory_mb {
if limit_mb == 0 {
"dynamic (auto)"
} else {
"fixed limit"
}
} else {
"dynamic (auto)"
};
(
format!(
"Memory-aware chunking ({chunking_mode}): chunk_size={chunk_size}, \
estimated_memory_mb={estimated_memory_mb:.2}"
),
chunk_size,
)
} else {
let chunk_size = util::chunk_size(idx_count as usize, njobs);
(
format!("CPU-based chunking: chunk_size={chunk_size}"),
chunk_size,
)
};
let nchunks = util::num_of_chunks(idx_count as usize, chunk_size);
log::info!("({chunking_mode_info}) nchunks={nchunks}");
let pool = ThreadPool::new(njobs);
let (send, recv) = crossbeam_channel::bounded(nchunks);
for i in 0..nchunks {
let (send, args, sel) = (send.clone(), self.clone(), sel.clone());
let weight_idx: Option<usize> = weight_col_idx;
pool.execute(move || {
let mut idx = unsafe {
args.rconfig()
.indexed()
.unwrap_unchecked()
.unwrap_unchecked()
};
idx.seek((i * chunk_size) as u64)
.expect("Index seek failed.");
let it = idx.byte_records().take(chunk_size);
unsafe {
send.send(args.compute(&sel, it, weight_idx))
.unwrap_unchecked();
}
});
}
drop(send);
Ok((headers, merge_all(recv.iter()).unwrap_or_default()))
}
fn stats_to_records(&self, stats: Vec<Stats>, visualize_ws: bool) -> Vec<csv::StringRecord> {
let round_places = self.flag_round;
let infer_boolean = self.flag_infer_boolean;
let mut records = Vec::with_capacity(stats.len());
records.extend(repeat_n(csv::StringRecord::new(), stats.len()));
let pool = ThreadPool::new(util::njobs(self.flag_jobs));
let mut results = Vec::with_capacity(stats.len());
for mut stat in stats {
let (send, recv) = crossbeam_channel::bounded(0);
results.push(recv);
pool.execute(move || {
send.send(stat.to_record(round_places, infer_boolean, visualize_ws))
.unwrap();
});
}
for (i, recv) in results.into_iter().enumerate() {
unsafe {
*records.get_unchecked_mut(i) = recv.recv().unwrap();
}
}
records
}
#[inline]
fn compute<I>(&self, sel: &Selection, it: I, weight_col_idx: Option<usize>) -> Vec<Stats>
where
I: Iterator<Item = csv::Result<csv::ByteRecord>>,
{
let sel_len = sel.len();
let mut stats = self.new_stats(sel_len);
let infer_date_flags = INFER_DATE_FLAGS.get().unwrap();
let infer_boolean = self.flag_infer_boolean;
let prefer_dmy = self.flag_prefer_dmy;
let mut i;
for row in it {
i = 0;
let row_result: csv::ByteRecord = unsafe { row.unwrap_unchecked() };
let weight = if let Some(widx) = weight_col_idx {
if widx < row_result.len() {
fast_float2::parse::<f64, &[u8]>(row_result.get(widx).unwrap_or(b"1.0"))
.unwrap_or(1.0)
} else {
1.0
}
} else {
1.0
};
debug_assert_eq!(infer_date_flags.len(), sel_len);
unsafe {
for field in sel.select(&row_result) {
stats.get_unchecked_mut(i).add(
field,
weight,
*infer_date_flags.get_unchecked(i),
infer_boolean,
prefer_dmy,
);
i += 1;
}
}
}
stats
}
fn process_headers_with_weight_exclusion(
&self,
full_headers: &csv::ByteRecord,
) -> CliResult<(Option<usize>, Selection, csv::ByteRecord)> {
if let Some(ref weight_col) = self.flag_weight {
let weight_idx = full_headers
.iter()
.position(|h| {
let h_str = String::from_utf8_lossy(h);
h_str.trim().eq_ignore_ascii_case(weight_col.trim())
})
.ok_or_else(|| {
CliError::Other(format!(
"Weight column '{weight_col}' not found in CSV headers"
))
})?;
let sel = self.rconfig().selection(full_headers)?;
let sel_vec: Vec<usize> = sel
.iter()
.copied()
.filter(|&idx| idx != weight_idx)
.collect();
if sel_vec.is_empty() {
return Err(CliError::Other(format!(
"After excluding weight column '{weight_col}', no columns remain for \
statistics computation"
)));
}
let modified_sel = unsafe { std::mem::transmute::<Vec<usize>, Selection>(sel_vec) };
let selected_headers: csv::ByteRecord = modified_sel.select(full_headers).collect();
Ok((Some(weight_idx), modified_sel, selected_headers))
} else {
let sel = self.rconfig().selection(full_headers)?;
let headers: csv::ByteRecord = sel.select(full_headers).collect();
Ok((None, sel, headers))
}
}
#[inline]
fn rconfig(&self) -> Config {
Config::new(self.arg_input.as_ref())
.delimiter(self.flag_delimiter)
.no_headers_flag(self.flag_no_headers)
.select(self.flag_select.clone())
}
#[inline]
fn which_stats(&self) -> WhichStats {
WhichStats {
include_nulls: self.flag_nulls,
sum: !self.flag_typesonly || self.flag_infer_boolean,
range: !self.flag_typesonly || self.flag_infer_boolean,
dist: !self.flag_typesonly || self.flag_infer_boolean,
cardinality: self.flag_everything || self.flag_cardinality,
median: !self.flag_everything && self.flag_median && !self.flag_quartiles,
mad: self.flag_everything || self.flag_mad,
quartiles: self.flag_everything || self.flag_quartiles,
mode: self.flag_everything || self.flag_mode,
typesonly: self.flag_typesonly,
percentiles: self.flag_everything || self.flag_percentiles,
percentile_list: self.flag_percentile_list.clone(),
}
}
#[inline]
fn new_stats(&self, record_len: usize) -> Vec<Stats> {
let mut stats: Vec<Stats> = Vec::with_capacity(record_len);
let use_weights = self.flag_weight.is_some();
stats.extend(repeat_n(
Stats::new(self.which_stats(), use_weights),
record_len,
));
stats
}
pub fn stats_headers(&self) -> csv::StringRecord {
if self.flag_typesonly {
return csv::StringRecord::from(vec!["field", "type"]);
}
let mut fields = Vec::with_capacity(MAX_STAT_COLUMNS);
fields.extend_from_slice(&[
"field",
"type",
"is_ascii",
"sum",
"min",
"max",
"range",
"sort_order",
"sortiness",
"min_length",
"max_length",
"sum_length",
"avg_length",
"stddev_length",
"variance_length",
"cv_length",
"mean",
"sem",
"geometric_mean",
"harmonic_mean",
"stddev",
"variance",
"cv",
"nullcount",
"n_negative",
"n_zero",
"n_positive",
"max_precision",
"sparsity",
]);
let everything = self.flag_everything;
if self.flag_median && !self.flag_quartiles && !everything {
fields.push("median");
}
if self.flag_mad || everything {
fields.push("mad");
}
if self.flag_quartiles || everything {
fields.extend_from_slice(&[
"lower_outer_fence",
"lower_inner_fence",
"q1",
"q2_median",
"q3",
"iqr",
"upper_inner_fence",
"upper_outer_fence",
"skewness",
]);
}
if self.flag_cardinality || everything {
fields.extend_from_slice(&["cardinality", "uniqueness_ratio"]);
}
if self.flag_mode || everything {
fields.extend_from_slice(&[
"mode",
"mode_count",
"mode_occurrences",
"antimode",
"antimode_count",
"antimode_occurrences",
]);
}
if self.flag_percentiles || everything {
fields.push("percentiles");
}
csv::StringRecord::from(fields)
}
}
fn calculate_avg_record_size(samples: &[csv::ByteRecord], which_stats: &WhichStats) -> usize {
if samples.is_empty() {
1024 } else {
let total_size: usize = samples
.iter()
.map(|record| estimate_record_memory(record, which_stats))
.sum();
(total_size / samples.len()).max(1024)
}
}
fn estimate_record_memory(record: &csv::ByteRecord, which_stats: &WhichStats) -> usize {
let base_size: usize = record.iter().map(<[u8]>::len).sum();
let mut additional_memory = 0;
if which_stats.quartiles || which_stats.median || which_stats.mad || which_stats.percentiles {
additional_memory += (record.len() / 2) * 8;
}
if which_stats.mode || which_stats.cardinality {
additional_memory += base_size; }
let overhead = usize::midpoint(base_size, additional_memory);
base_size + additional_memory + overhead
}
const fn estimate_chunk_memory(
record_count: usize,
avg_record_size: usize,
which_stats: &WhichStats,
field_count: usize,
) -> usize {
let base_memory = record_count.saturating_mul(avg_record_size);
let mut additional_memory = 0;
if which_stats.quartiles || which_stats.median || which_stats.mad || which_stats.percentiles {
additional_memory += record_count.saturating_mul((field_count / 2).saturating_mul(8));
}
if which_stats.mode || which_stats.cardinality {
additional_memory += record_count.saturating_mul(avg_record_size);
}
let overhead = (base_memory + additional_memory) / 5;
base_memory
.saturating_add(additional_memory)
.saturating_add(overhead)
}
fn calculate_memory_aware_chunk_size(
idx_count: u64,
njobs: usize,
max_chunk_memory_mb: Option<u64>,
which_stats: &WhichStats,
sample_records: Option<&[csv::ByteRecord]>,
) -> usize {
let needs_memory_aware_chunking = which_stats.needs_memory_aware_chunking();
match max_chunk_memory_mb {
None => {
if needs_memory_aware_chunking {
util::calculate_dynamic_chunk_size(idx_count, njobs, sample_records, |record| {
estimate_record_memory(record, which_stats)
})
} else {
util::chunk_size(idx_count as usize, njobs)
}
},
Some(0) => {
util::calculate_dynamic_chunk_size(idx_count, njobs, sample_records, |record| {
estimate_record_memory(record, which_stats)
})
},
Some(limit_mb) => {
#[allow(clippy::cast_precision_loss)]
let max_memory_bytes = (limit_mb as usize * 1024 * 1024) as f64 * util::SAFETY_MARGIN;
let avg_record_size = if let Some(samples) = sample_records {
if samples.is_empty() {
1024 } else {
let total_size: usize = samples
.iter()
.map(|record| estimate_record_memory(record, which_stats))
.sum();
debug_assert!(total_size > 0, "total_size should be positive here");
total_size / samples.len() }
} else {
1024 };
#[allow(clippy::cast_precision_loss)]
let chunk_size = (max_memory_bytes / (avg_record_size as f64).max(1.0)) as usize;
chunk_size.max(1).min(idx_count as usize)
},
}
}
fn stats_path(stats_csv_path: &Path, stdin_flag: bool, weighted: bool) -> io::Result<PathBuf> {
let parent = stats_csv_path
.parent()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Invalid path"))?;
let fstem = stats_csv_path
.file_stem()
.ok_or_else(|| io::Error::new(io::ErrorKind::InvalidInput, "Invalid file name"))?;
let new_fname = if stdin_flag {
if weighted {
"stdin.stats.weighted.csv".to_string()
} else {
"stdin.stats.csv".to_string()
}
} else if weighted {
format!("{}.stats.weighted.csv", fstem.to_string_lossy())
} else {
format!("{}.stats.csv", fstem.to_string_lossy())
};
Ok(parent.join(new_fname))
}
fn init_date_inference(
infer_dates: bool,
headers: &csv::ByteRecord,
flag_whitelist: &str,
) -> Result<(), String> {
if !infer_dates {
INFER_DATE_FLAGS
.set(SmallVec::from_elem(false, headers.len()))
.map_err(|_| "Cannot init empty date inference flags".to_string())?;
return Ok(());
}
let infer_date_flags = if flag_whitelist.eq_ignore_ascii_case("all") {
log::info!("inferring dates for ALL fields");
SmallVec::from_elem(true, headers.len())
} else {
let mut header_str = String::new();
let whitelist_lower = flag_whitelist.to_lowercase();
log::info!("inferring dates with date-whitelist: {whitelist_lower}");
let whitelist: SmallVec<[&str; 8]> = whitelist_lower.split(',').map(str::trim).collect();
headers
.iter()
.map(|header| {
util::to_lowercase_into(
simdutf8::basic::from_utf8(header).unwrap_or_default(),
&mut header_str,
);
whitelist
.iter()
.any(|whitelist_item| header_str.contains(whitelist_item))
})
.collect()
};
INFER_DATE_FLAGS
.set(infer_date_flags)
.map_err(|e| format!("Cannot init date inference flags: {e:?}"))?;
Ok(())
}
#[derive(Deserialize)]
struct SniffResult {
fields: Vec<String>,
types: Vec<String>,
}
fn resolve_sniff_whitelist(input_path: &std::path::Path) -> CliResult<String> {
let qsv_bin = util::current_exe()?;
let output = std::process::Command::new(qsv_bin)
.args(["sniff", "--json", "--stats-types"])
.arg(input_path)
.output()?;
if !output.status.success() {
let stderr = String::from_utf8_lossy(&output.stderr);
return fail_clierror!("Failed to sniff file for date columns: {}", stderr.trim());
}
#[cfg(target_endian = "little")]
let sniff_result: SniffResult = {
let mut json_bytes = output.stdout;
simd_json::from_slice(&mut json_bytes)
.map_err(|e| CliError::Other(format!("Failed to parse sniff JSON: {e}")))?
};
#[cfg(target_endian = "big")]
let sniff_result: SniffResult = serde_json::from_slice(&output.stdout)
.map_err(|e| CliError::Other(format!("Failed to parse sniff JSON: {e}")))?;
let date_columns: Vec<&str> = sniff_result
.fields
.iter()
.zip(sniff_result.types.iter())
.filter_map(|(field, typ)| {
if typ == "Date" || typ == "DateTime" {
Some(field.as_str())
} else {
None
}
})
.collect();
if date_columns.is_empty() {
log::info!("sniff: no Date/DateTime columns found");
Ok("_qsv_no_date_columns_found".to_string())
} else {
log::info!(
"sniff: found Date/DateTime columns: {}",
date_columns.join(", ")
);
Ok(date_columns.join(","))
}
}
#[derive(Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
struct WhichStats {
include_nulls: bool,
sum: bool,
range: bool,
dist: bool,
cardinality: bool,
median: bool,
mad: bool,
quartiles: bool,
mode: bool,
typesonly: bool,
percentiles: bool,
percentile_list: String,
}
impl Commute for WhichStats {
#[inline]
fn merge(&mut self, other: WhichStats) {
assert_eq!(*self, other);
}
}
impl WhichStats {
const fn needs_memory_aware_chunking(&self) -> bool {
self.quartiles
|| self.median
|| self.mad
|| self.percentiles
|| self.mode
|| self.cardinality
}
}
#[allow(clippy::unsafe_derive_deserialize)]
#[allow(clippy::struct_field_names)]
#[repr(C, align(64))] #[derive(Clone, Serialize, Deserialize, PartialEq)]
struct Stats {
typ: FieldType, is_ascii: bool, max_precision: u16,
nullcount: u64, sum_stotlen: u64, total_weight: f64,
sum: Option<TypedSum>,
which: WhichStats,
online: Option<OnlineStats>, online_len: Option<OnlineStats>, weighted_online: Option<WeightedOnlineStats>,
modes: Option<Unsorted<Vec<u8>>>, weighted_modes: Option<HashMap<Vec<u8>, f64>>,
#[allow(clippy::struct_field_names)]
unsorted_stats: Option<Unsorted<f64>>, weighted_unsorted_stats: Option<Vec<(f64, f64)>>,
minmax: Option<TypedMinMax>, }
#[derive(Clone, Default, Serialize, Deserialize, PartialEq)]
struct WeightedOnlineStats {
sum_weights: f64,
weighted_mean: f64,
sum_squared_diffs: f64,
sum_weighted_logs: f64,
sum_weights_positive: f64,
sum_weighted_reciprocals: f64,
sum_weights_nonzero: f64,
count: usize,
}
impl WeightedOnlineStats {
const fn new() -> Self {
Self {
sum_weights: 0.0,
weighted_mean: 0.0,
sum_squared_diffs: 0.0,
sum_weighted_logs: 0.0,
sum_weights_positive: 0.0,
sum_weighted_reciprocals: 0.0,
sum_weights_nonzero: 0.0,
count: 0,
}
}
#[inline]
fn add_weighted(&mut self, x: f64, w: f64) {
if w <= 0.0 {
return;
}
self.count += 1;
self.sum_weights += w;
if self.sum_weights == 0.0 {
return;
}
let delta = x - self.weighted_mean;
self.weighted_mean += (w / self.sum_weights) * delta;
let delta2 = x - self.weighted_mean;
self.sum_squared_diffs += w * delta * delta2;
if x > 0.0 {
self.sum_weighted_logs += w * x.ln();
self.sum_weights_positive += w;
}
if x != 0.0 {
self.sum_weighted_reciprocals += w / x;
self.sum_weights_nonzero += w;
}
}
#[inline]
const fn mean(&self) -> f64 {
self.weighted_mean
}
#[inline]
fn variance(&self) -> f64 {
if self.sum_weights <= 1.0 {
return 0.0;
}
self.sum_squared_diffs / (self.sum_weights - 1.0)
}
#[inline]
fn stddev(&self) -> f64 {
self.variance().sqrt()
}
#[inline]
fn geometric_mean(&self) -> f64 {
if self.sum_weights_positive <= 0.0 || self.sum_weighted_logs.is_nan() {
return f64::NAN;
}
(self.sum_weighted_logs / self.sum_weights_positive).exp()
}
#[inline]
fn harmonic_mean(&self) -> f64 {
if self.sum_weights_nonzero <= 0.0 || self.sum_weighted_reciprocals <= 0.0 {
return f64::NAN;
}
self.sum_weights_nonzero / self.sum_weighted_reciprocals
}
#[inline]
const fn len(&self) -> usize {
self.count
}
fn merge(&mut self, other: &WeightedOnlineStats) {
if other.sum_weights == 0.0 {
return;
}
if self.sum_weights == 0.0 {
*self = other.clone();
return;
}
let total_weights = self.sum_weights + other.sum_weights;
let delta = other.weighted_mean - self.weighted_mean;
self.sum_squared_diffs += delta.mul_add(
delta * (self.sum_weights * other.sum_weights / total_weights),
other.sum_squared_diffs,
);
self.weighted_mean = self
.sum_weights
.mul_add(self.weighted_mean, other.sum_weights * other.weighted_mean)
/ total_weights;
self.sum_weighted_logs += other.sum_weighted_logs;
self.sum_weights_positive += other.sum_weights_positive;
self.sum_weighted_reciprocals += other.sum_weighted_reciprocals;
self.sum_weights_nonzero += other.sum_weights_nonzero;
self.sum_weights = total_weights;
self.count += other.count;
}
}
fn weighted_quantile(data: &[(f64, f64)], total_weight: f64, percentile: f64) -> Option<f64> {
if data.is_empty() || total_weight <= 0.0 {
return None;
}
let target_weight = percentile * total_weight;
let mut cum_weight = 0.0;
for &(value, weight) in data {
cum_weight += weight;
if cum_weight >= target_weight {
return Some(value);
}
}
data.last().map(|(v, _)| *v)
}
fn weighted_quartiles(data: &[(f64, f64)], total_weight: f64) -> Option<(f64, f64, f64)> {
if data.is_empty() || total_weight <= 0.0 {
return None;
}
let thresholds = [
0.25_f64 * total_weight,
0.5_f64 * total_weight,
0.75_f64 * total_weight,
];
let mut results: [Option<f64>; 3] = [None, None, None];
let mut cumulative_weight = 0.0_f64;
let mut t_idx = 0_usize;
for (value, weight) in data {
cumulative_weight += *weight;
while t_idx < thresholds.len() && cumulative_weight >= thresholds[t_idx] {
if results[t_idx].is_none() {
results[t_idx] = Some(*value);
}
t_idx += 1;
}
if t_idx >= thresholds.len() {
break;
}
}
if let (Some(q1), Some(q2), Some(q3)) = results.into() {
Some((q1, q2, q3))
} else {
None
}
}
fn weighted_median(data: &[(f64, f64)], total_weight: f64) -> Option<f64> {
weighted_quantile(data, total_weight, 0.5)
}
fn weighted_mad(data: &[(f64, f64)], total_weight: f64, median: f64) -> Option<f64> {
if data.is_empty() || total_weight <= 0.0 {
return None;
}
let mut abs_deviations: Vec<(f64, f64)> = data
.par_iter()
.map(|&(value, weight)| ((value - median).abs(), weight))
.collect();
abs_deviations
.par_sort_unstable_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
weighted_median(&abs_deviations, total_weight)
}
fn weighted_percentiles(
data: &[(f64, f64)],
total_weight: f64,
percentile_list: &[u8],
) -> Option<Vec<f64>> {
if data.is_empty() || total_weight <= 0.0 {
return None;
}
let mut targets: Vec<(f64, usize)> = percentile_list
.iter()
.enumerate()
.map(|(idx, &p)| {
let percentile_f64 = p as f64 / 100.0;
let target_cum_weight = percentile_f64 * total_weight;
(target_cum_weight, idx)
})
.collect();
targets.par_sort_unstable_by(|a, b| a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal));
let mut results = vec![0.0; percentile_list.len()];
let mut cum_weight = 0.0;
let mut target_idx = 0;
for &(value, weight) in data {
cum_weight += weight;
while target_idx < targets.len() && cum_weight >= targets[target_idx].0 {
let original_idx = targets[target_idx].1;
results[original_idx] = value;
target_idx += 1;
}
if target_idx == targets.len() {
break;
}
}
Some(results)
}
#[inline]
fn timestamp_ms_to_rfc3339(timestamp: i64, typ: FieldType) -> String {
let date_val = chrono::DateTime::from_timestamp_millis(timestamp)
.unwrap_or_default()
.to_rfc3339();
if typ == TDate {
return date_val[..10].to_string();
}
date_val
}
impl Stats {
fn new(which: WhichStats, use_weights: bool) -> Stats {
let (mut sum, mut minmax, mut online, mut online_len, mut modes, mut unsorted_stats) =
(None, None, None, None, None, None);
let mut weighted_online = None;
let mut weighted_unsorted_stats = None;
let mut weighted_modes = None;
if which.sum {
sum = Some(TypedSum::default());
}
if which.range {
minmax = Some(TypedMinMax::default());
}
if which.dist {
online = Some(stats::OnlineStats::default());
online_len = Some(stats::OnlineStats::default());
if use_weights {
weighted_online = Some(WeightedOnlineStats::new());
}
}
let record_count = *RECORD_COUNT.get().unwrap_or(&10_000) as usize;
if which.mode || which.cardinality {
if use_weights {
weighted_modes = Some(HashMap::with_capacity((record_count / 10).max(16)));
} else {
modes = Some(stats::Unsorted::with_capacity(record_count));
}
}
if which.quartiles || which.median || which.mad || which.percentiles {
unsorted_stats = Some(stats::Unsorted::with_capacity(record_count));
if use_weights {
weighted_unsorted_stats = Some(Vec::with_capacity(record_count));
}
}
Stats {
typ: FieldType::default(),
is_ascii: true,
max_precision: 0,
nullcount: 0,
sum_stotlen: 0,
total_weight: 0.0,
sum,
which,
online,
online_len,
weighted_online,
modes,
weighted_modes,
unsorted_stats,
weighted_unsorted_stats,
minmax,
}
}
#[allow(clippy::inline_always)]
#[inline(always)]
fn add(
&mut self,
sample: &[u8],
weight: f64,
infer_dates: bool,
infer_boolean: bool,
prefer_dmy: bool,
) {
let (sample_type, int_val, float_val) =
FieldType::from_sample(infer_dates, prefer_dmy, sample, self.typ);
self.typ.merge(sample_type);
if self.which.typesonly && !infer_boolean {
return;
}
let t = self.typ;
if weight > 0.0 {
self.total_weight += weight;
}
if b"" != sample {
unsafe {
self.sum
.as_mut()
.unwrap_unchecked()
.add_with_parsed(t, sample, float_val, int_val);
}
}
unsafe {
self.minmax
.as_mut()
.unwrap_unchecked()
.add_with_parsed(t, sample, float_val, int_val);
};
if let Some(ref mut wm) = self.weighted_modes {
*wm.entry(sample.to_vec()).or_insert(0.0) += weight;
} else if let Some(v) = self.modes.as_mut() {
v.add(sample.to_vec());
}
if t == TString {
unsafe {
self.online_len
.as_mut()
.unwrap_unchecked()
.add(&sample.len());
}
if self.is_ascii {
self.is_ascii = sample.is_ascii();
}
if sample_type == TNull {
self.nullcount += 1;
}
return; }
if sample_type == TNull {
self.nullcount += 1;
if self.which.include_nulls {
unsafe {
self.online.as_mut().unwrap_unchecked().add_null();
}
}
return; }
match t {
TInteger | TFloat => {
if let Some(v) = self.unsorted_stats.as_mut() {
v.add(float_val);
}
if let Some(v) = self.weighted_unsorted_stats.as_mut() {
if weight > 0.0 {
v.push((float_val, weight));
}
}
unsafe {
self.online.as_mut().unwrap_unchecked().add_f64(float_val);
}
if let Some(ref mut wos) = self.weighted_online {
wos.add_weighted(float_val, weight);
}
if t == TFloat {
let precision = if float_val == 0.0 {
0
} else {
unsafe {
zmij::Buffer::new()
.format_finite(float_val)
.split('.')
.next_back()
.unwrap_unchecked()
.len() as u16
}
};
self.max_precision = std::cmp::max(self.max_precision, precision);
}
},
TDateTime | TDate => {
#[allow(clippy::cast_precision_loss)]
let timestamp = int_val as f64;
if let Some(v) = self.unsorted_stats.as_mut() {
v.add(timestamp);
}
if let Some(v) = self.weighted_unsorted_stats.as_mut() {
if weight > 0.0 {
v.push((timestamp, weight));
}
}
unsafe {
self.online.as_mut().unwrap_unchecked().add_f64(timestamp);
}
if let Some(ref mut wos) = self.weighted_online {
wos.add_weighted(timestamp, weight);
}
},
_ => {},
}
}
#[allow(clippy::wrong_self_convention)]
pub fn to_record(
&mut self,
round_places: u32,
infer_boolean: bool,
visualize_ws: bool,
) -> csv::StringRecord {
const EMPTY_STR: &str = "";
const EMPTY_STRING: String = String::new();
if self.which.typesonly && !infer_boolean {
return csv::StringRecord::from(vec![self.typ.to_string()]);
}
let typ = self.typ;
let mut record = csv::StringRecord::with_capacity(512, MAX_STAT_COLUMNS);
let minmax_range_sortorder_pieces: Vec<String>;
let mut minval = String::new();
let mut maxval = String::new();
let mut column_sorted = false;
if let Some(mm) = self
.minmax
.as_ref()
.and_then(|mm| mm.show(typ, round_places, visualize_ws))
{
minval.clone_from(&mm.0);
maxval.clone_from(&mm.1);
if mm.3.starts_with("Ascending") {
column_sorted = true;
}
minmax_range_sortorder_pieces = vec![mm.0, mm.1, mm.2, mm.3, mm.4];
} else {
minmax_range_sortorder_pieces = vec![EMPTY_STRING; 5];
}
let record_count = *RECORD_COUNT.get().unwrap_or(&1);
let stats_separator = STATS_SEPARATOR.get_or_init(|| {
if self.which.mode || self.which.percentiles {
std::env::var("QSV_STATS_SEPARATOR")
.unwrap_or_else(|_| DEFAULT_STATS_SEPARATOR.to_string())
} else {
DEFAULT_STATS_SEPARATOR.to_string()
}
});
let mut cardinality = 0;
let mut mc_pieces: Vec<String> = Vec::new();
if let Some(ref weighted_modes_map) = self.weighted_modes {
mc_pieces.reserve(8);
if self.which.cardinality {
cardinality = weighted_modes_map.len() as u64;
mc_pieces.push(itoa::Buffer::new().format(cardinality).to_owned());
#[allow(clippy::cast_precision_loss)]
mc_pieces.push(util::round_num(
(cardinality as f64) / (record_count as f64),
round_places,
));
}
if self.which.mode {
if weighted_modes_map.is_empty() {
mc_pieces.extend_from_slice(&[
EMPTY_STRING,
"0".to_string(),
"0".to_string(),
EMPTY_STRING,
"0".to_string(),
"0".to_string(),
]);
} else {
let unique_count = weighted_modes_map.len() as u64;
if unique_count == record_count {
mc_pieces.extend_from_slice(
&[
EMPTY_STRING,
"0".to_string(),
"0".to_string(),
"*ALL".to_string(),
"0".to_string(),
"1".to_string(),
],
);
} else {
let max_weight = weighted_modes_map.values().copied().fold(0.0, f64::max);
let min_weight = weighted_modes_map
.values()
.copied()
.fold(f64::INFINITY, f64::min);
let mut modes_keys: Vec<&Vec<u8>> = weighted_modes_map
.iter()
.filter(|&(_, &weight)| (weight - max_weight).abs() < 1e-10)
.map(|(value, _)| value)
.collect();
if modes_keys.len() > PAR_SORT_THRESHOLD {
modes_keys.par_sort_unstable();
} else {
modes_keys.sort_unstable();
}
let modes_result: Vec<Vec<u8>> = modes_keys.into_iter().cloned().collect();
let antimodes_all: Vec<&Vec<u8>> = weighted_modes_map
.iter()
.filter(|&(_, &weight)| (weight - min_weight).abs() < 1e-10)
.map(|(value, _)| value)
.collect();
let antimodes_count = antimodes_all.len();
let mut antimodes_keys: Vec<&Vec<u8>> = antimodes_all;
if antimodes_keys.len() > PAR_SORT_THRESHOLD {
antimodes_keys.par_sort_unstable();
} else {
antimodes_keys.sort_unstable();
}
antimodes_keys.truncate(MAX_ANTIMODES);
let antimodes_result: Vec<Vec<u8>> =
antimodes_keys.into_iter().cloned().collect();
let modes_count = modes_result.len();
let modes_list = if visualize_ws {
modes_result
.iter()
.map(|c| util::visualize_whitespace(&String::from_utf8_lossy(c)))
.join(stats_separator)
} else {
modes_result
.iter()
.map(|c| String::from_utf8_lossy(c))
.join(stats_separator)
};
let antimodes_len = ANTIMODES_LEN.get_or_init(|| {
std::env::var("QSV_ANTIMODES_LEN").map_or(
DEFAULT_ANTIMODES_LEN,
|val| {
let parsed = atoi_simd::parse::<usize>(val.as_bytes())
.unwrap_or(DEFAULT_ANTIMODES_LEN);
if parsed == 0 { usize::MAX } else { parsed }
},
)
});
let mut antimodes_list = String::with_capacity(*antimodes_len);
if antimodes_count > MAX_ANTIMODES {
antimodes_list.push_str("*PREVIEW: ");
}
let antimodes_vals = &antimodes_result
.iter()
.map(|c| String::from_utf8_lossy(c))
.join(stats_separator);
if antimodes_vals.starts_with(stats_separator) {
antimodes_list.push_str("NULL");
}
antimodes_list.push_str(antimodes_vals);
if antimodes_list.len() > *antimodes_len {
util::utf8_truncate(&mut antimodes_list, *antimodes_len + 1);
antimodes_list.push_str("...");
}
#[allow(clippy::cast_possible_truncation)]
let mode_occurrences = max_weight.round() as u32;
#[allow(clippy::cast_possible_truncation)]
let antimode_occurrences = min_weight.round() as u32;
mc_pieces.extend_from_slice(&[
modes_list,
itoa::Buffer::new().format(modes_count).to_owned(),
itoa::Buffer::new().format(mode_occurrences).to_owned(),
if visualize_ws {
util::visualize_whitespace(&antimodes_list)
} else {
antimodes_list
},
itoa::Buffer::new().format(antimodes_count).to_owned(),
itoa::Buffer::new().format(antimode_occurrences).to_owned(),
]);
}
}
}
} else {
match self.modes.as_mut() {
None => {
if self.which.cardinality {
mc_pieces = vec![EMPTY_STRING; 2];
}
if self.which.mode {
mc_pieces = vec![EMPTY_STRING; 6];
}
},
Some(v) => {
mc_pieces.reserve(8);
if self.which.cardinality {
cardinality = v.cardinality(column_sorted, 1);
mc_pieces.push(itoa::Buffer::new().format(cardinality).to_owned());
#[allow(clippy::cast_precision_loss)]
mc_pieces.push(util::round_num(
(cardinality as f64) / (record_count as f64),
round_places,
));
}
if self.which.mode {
if cardinality == record_count {
mc_pieces.extend_from_slice(
&[
EMPTY_STRING,
"0".to_string(),
"0".to_string(),
"*ALL".to_string(),
"0".to_string(),
"1".to_string(),
],
);
} else {
let (
(modes_result, modes_count, mode_occurrences),
(antimodes_result, antimodes_count, antimode_occurrences),
) = v.modes_antimodes();
let modes_list = if visualize_ws {
modes_result
.iter()
.map(|c| {
util::visualize_whitespace(&String::from_utf8_lossy(c))
})
.join(stats_separator)
} else {
modes_result
.iter()
.map(|c| String::from_utf8_lossy(c))
.join(stats_separator)
};
let antimodes_len = ANTIMODES_LEN.get_or_init(|| {
std::env::var("QSV_ANTIMODES_LEN").map_or(
DEFAULT_ANTIMODES_LEN,
|val| {
let parsed = atoi_simd::parse::<usize>(val.as_bytes())
.unwrap_or(DEFAULT_ANTIMODES_LEN);
if parsed == 0 { usize::MAX } else { parsed }
},
)
});
let mut antimodes_list = String::with_capacity(*antimodes_len);
if antimodes_count > MAX_ANTIMODES {
antimodes_list.push_str("*PREVIEW: ");
}
let antimodes_vals = &antimodes_result
.iter()
.map(|c| String::from_utf8_lossy(c))
.join(stats_separator);
if antimodes_vals.starts_with(stats_separator) {
antimodes_list.push_str("NULL");
}
antimodes_list.push_str(antimodes_vals);
if antimodes_list.len() > *antimodes_len {
util::utf8_truncate(&mut antimodes_list, *antimodes_len + 1);
antimodes_list.push_str("...");
}
mc_pieces.extend_from_slice(&[
modes_list,
itoa::Buffer::new().format(modes_count).to_owned(),
itoa::Buffer::new().format(mode_occurrences).to_owned(),
if visualize_ws {
util::visualize_whitespace(&antimodes_list)
} else {
antimodes_list
},
itoa::Buffer::new().format(antimodes_count).to_owned(),
itoa::Buffer::new().format(antimode_occurrences).to_owned(),
]);
}
}
},
}
}
if cardinality == 2 && infer_boolean {
let patterns = BOOLEAN_PATTERNS.get();
if let Some(patterns) = patterns {
let mut is_boolean = false;
for pattern in patterns {
if pattern.matches(&minval).is_some() && pattern.matches(&maxval).is_some() {
record.push_field("Boolean");
is_boolean = true;
break;
}
}
if !is_boolean {
record.push_field(typ.as_str());
}
} else {
record.push_field(typ.as_str());
}
} else {
record.push_field(typ.as_str());
}
if self.which.typesonly && infer_boolean {
return record;
}
if typ == FieldType::TString {
record.push_field(&self.is_ascii.to_string());
} else {
record.push_field(EMPTY_STR);
}
let stotlen =
if let Some((stotlen_work, sum)) = self.sum.as_ref().and_then(|sum| sum.show(typ)) {
if typ == FieldType::TFloat {
if let Ok(f64_val) = fast_float2::parse::<f64, &[u8]>(sum.as_bytes()) {
record.push_field(&util::round_num(f64_val, round_places));
} else {
record.push_field(&format!("ERROR: Cannot convert {sum} to a float."));
}
} else {
record.push_field(&sum);
}
stotlen_work
} else {
record.push_field(EMPTY_STR);
0
};
for field in &minmax_range_sortorder_pieces {
record.push_field(field);
}
if typ != FieldType::TString {
for _ in 0..7 {
record.push_field(EMPTY_STR);
}
} else if let Some(mm) = self.minmax.as_ref().and_then(TypedMinMax::len_range) {
record.push_field(&mm.0);
record.push_field(&mm.1);
if stotlen < u64::MAX {
record.push_field(itoa::Buffer::new().format(stotlen));
#[allow(clippy::cast_precision_loss)]
let avg_len = stotlen as f64 / record_count as f64;
record.push_field(&util::round_num(avg_len, round_places));
if let Some(vl) = self.online_len.as_ref() {
let vlen_stddev = vl.stddev();
record.push_field(&util::round_num(vlen_stddev, round_places));
record.push_field(&util::round_num(vl.variance(), round_places));
record.push_field(&util::round_num(vlen_stddev / avg_len, round_places));
} else {
for _ in 0..3 {
record.push_field(EMPTY_STR);
}
}
} else {
for _ in 0..5 {
record.push_field(OVERFLOW_STRING);
}
}
} else {
for _ in 0..7 {
record.push_field(EMPTY_STR);
}
}
if typ == TString || typ == TNull {
for _ in 0..7 {
record.push_field(EMPTY_STR);
}
} else if let Some(ref wos) = self.weighted_online {
let std_dev = wos.stddev();
#[allow(clippy::cast_precision_loss)]
let sem = std_dev / (wos.len() as f64).sqrt();
let mean = wos.mean();
let mean_string = util::round_num(mean, round_places);
let cv = if mean_string == "0" {
f64::NAN
} else {
(std_dev / mean) * 100.0_f64
};
let geometric_mean = wos.geometric_mean();
let harmonic_mean = wos.harmonic_mean();
if self.typ == TFloat || self.typ == TInteger {
record.push_field(&mean_string);
record.push_field(&util::round_num(sem, round_places));
record.push_field(&util::round_num(geometric_mean, round_places));
record.push_field(&util::round_num(harmonic_mean, round_places));
record.push_field(&util::round_num(std_dev, round_places));
record.push_field(&util::round_num(wos.variance(), round_places));
} else {
record.push_field(×tamp_ms_to_rfc3339(mean as i64, typ));
record.push_field(&util::round_num(
sem / MS_IN_DAY,
u32::max(round_places, DAY_DECIMAL_PLACES),
));
record.push_field(&util::round_num(
geometric_mean / MS_IN_DAY,
u32::max(round_places, DAY_DECIMAL_PLACES),
));
record.push_field(&util::round_num(
harmonic_mean / MS_IN_DAY,
u32::max(round_places, DAY_DECIMAL_PLACES),
));
record.push_field(&util::round_num(
std_dev / MS_IN_DAY,
u32::max(round_places, DAY_DECIMAL_PLACES),
));
record.push_field(&util::round_num(
wos.variance() / (MS_IN_DAY * MS_IN_DAY),
u32::max(round_places, DAY_DECIMAL_PLACES),
));
}
record.push_field(&util::round_num(cv, round_places));
} else if let Some(ref v) = self.online {
let std_dev = v.stddev();
#[allow(clippy::cast_precision_loss)]
let sem = std_dev / (v.len() as f64).sqrt();
let mean = v.mean();
let mean_string = util::round_num(mean, round_places);
let cv = if mean_string == "0" {
f64::NAN
} else {
(std_dev / mean) * 100.0_f64
};
let geometric_mean = v.geometric_mean();
let harmonic_mean = v.harmonic_mean();
if self.typ == TFloat || self.typ == TInteger {
record.push_field(&mean_string);
record.push_field(&util::round_num(sem, round_places));
record.push_field(&util::round_num(geometric_mean, round_places));
record.push_field(&util::round_num(harmonic_mean, round_places));
record.push_field(&util::round_num(std_dev, round_places));
record.push_field(&util::round_num(v.variance(), round_places));
} else {
record.push_field(×tamp_ms_to_rfc3339(mean as i64, typ));
record.push_field(&util::round_num(
sem / MS_IN_DAY,
u32::max(round_places, DAY_DECIMAL_PLACES),
));
record.push_field(&util::round_num(
geometric_mean / MS_IN_DAY,
u32::max(round_places, DAY_DECIMAL_PLACES),
));
record.push_field(&util::round_num(
harmonic_mean / MS_IN_DAY,
u32::max(round_places, DAY_DECIMAL_PLACES),
));
record.push_field(&util::round_num(
std_dev / MS_IN_DAY,
u32::max(round_places, DAY_DECIMAL_PLACES),
));
record.push_field(&util::round_num(
v.variance() / (MS_IN_DAY * MS_IN_DAY),
u32::max(round_places, DAY_DECIMAL_PLACES),
));
}
record.push_field(&util::round_num(cv, round_places));
} else {
for _ in 0..7 {
record.push_field(EMPTY_STR);
}
}
record.push_field(itoa::Buffer::new().format(self.nullcount));
if typ == TInteger || typ == TFloat {
if let Some(ref v) = self.online {
let (n_negative, n_zero, n_positive) = v.n_counts();
record.push_field(itoa::Buffer::new().format(n_negative));
record.push_field(itoa::Buffer::new().format(n_zero));
record.push_field(itoa::Buffer::new().format(n_positive));
} else {
for _ in 0..3 {
record.push_field(EMPTY_STR);
}
}
} else {
for _ in 0..3 {
record.push_field(EMPTY_STR);
}
}
if typ == TFloat {
record.push_field(itoa::Buffer::new().format(self.max_precision));
} else {
record.push_field(EMPTY_STR);
}
#[allow(clippy::cast_precision_loss)]
record.push_field(&util::round_num(
self.nullcount as f64 / record_count as f64,
round_places,
));
let mut existing_median = None;
let mut quartile_pieces: Vec<String> = if self.which.quartiles {
vec![EMPTY_STRING; 9]
} else {
Vec::new()
};
let sorted_weighted_data: Option<Vec<(f64, f64)>> =
if let Some(mut weighted_data) = self.weighted_unsorted_stats.take() {
if weighted_data.is_empty() {
None
} else {
weighted_data.par_sort_unstable_by(|a, b| {
a.0.partial_cmp(&b.0).unwrap_or(std::cmp::Ordering::Equal)
});
Some(weighted_data)
}
} else {
None
};
let quartiles_result = if let Some(weighted_data) = sorted_weighted_data.as_ref() {
match typ {
TInteger | TFloat | TDate | TDateTime => {
if self.which.quartiles {
weighted_quartiles(weighted_data, self.total_weight)
} else {
None
}
},
_ => None,
}
} else {
self.unsorted_stats.as_mut().and_then(|v| match typ {
TInteger | TFloat | TDate | TDateTime => {
if self.which.quartiles {
v.quartiles()
} else {
None
}
},
_ => None,
})
};
match quartiles_result {
None => {
},
Some((q1, q2, q3)) => {
existing_median = Some(q2);
let iqr = q3 - q1;
let lof = 3.0f64.mul_add(-iqr, q1);
let lif = 1.5f64.mul_add(-iqr, q1);
let uif = 1.5_f64.mul_add(iqr, q3);
let uof = 3.0_f64.mul_add(iqr, q3);
let skewness = (2.0f64.mul_add(-q2, q3) + q1) / iqr;
quartile_pieces.clear();
quartile_pieces.reserve(9);
if typ == TDateTime || typ == TDate {
quartile_pieces.extend_from_slice(&[
timestamp_ms_to_rfc3339(lof as i64, typ),
timestamp_ms_to_rfc3339(lif as i64, typ),
timestamp_ms_to_rfc3339(q1 as i64, typ),
timestamp_ms_to_rfc3339(q2 as i64, typ), timestamp_ms_to_rfc3339(q3 as i64, typ),
util::round_num(
(q3 - q1) / MS_IN_DAY,
u32::max(round_places, DAY_DECIMAL_PLACES),
),
timestamp_ms_to_rfc3339(uif as i64, typ),
timestamp_ms_to_rfc3339(uof as i64, typ),
]);
} else {
quartile_pieces.extend_from_slice(&[
util::round_num(lof, round_places),
util::round_num(lif, round_places),
util::round_num(q1, round_places),
util::round_num(q2, round_places), util::round_num(q3, round_places),
util::round_num(iqr, round_places),
util::round_num(uif, round_places),
util::round_num(uof, round_places),
]);
}
quartile_pieces.push(util::round_num(skewness, round_places));
},
}
if self.which.median {
let median_value = if let Some(weighted_data) = sorted_weighted_data.as_ref() {
match typ {
TNull | TString => None,
_ => weighted_median(weighted_data, self.total_weight),
}
} else {
self.unsorted_stats.as_mut().and_then(|v| {
if let TNull | TString = typ {
None
} else {
v.median()
}
})
};
if median_value.is_some() {
existing_median = median_value;
}
if let Some(v) = median_value {
if typ == TDateTime || typ == TDate {
record.push_field(×tamp_ms_to_rfc3339(v as i64, typ));
} else {
record.push_field(&util::round_num(v, round_places));
}
} else {
record.push_field(EMPTY_STR);
}
}
if self.which.mad {
let mad_value = if let Some(weighted_data) = sorted_weighted_data.as_ref() {
match typ {
TNull | TString => None,
_ => {
existing_median
.or_else(|| weighted_median(weighted_data, self.total_weight))
.and_then(|weighted_median_val| {
weighted_mad(weighted_data, self.total_weight, weighted_median_val)
})
},
}
} else {
self.unsorted_stats.as_mut().and_then(|v| {
if let TNull | TString = typ {
None
} else {
v.mad(existing_median)
}
})
};
if let Some(v) = mad_value {
if typ == TDateTime || typ == TDate {
record.push_field(&util::round_num(
v / MS_IN_DAY,
u32::max(round_places, DAY_DECIMAL_PLACES),
));
} else {
record.push_field(&util::round_num(v, round_places));
}
} else {
record.push_field(EMPTY_STR);
}
}
for field in &quartile_pieces {
record.push_field(field);
}
for field in &mc_pieces {
record.push_field(field);
}
if self.which.percentiles {
match typ {
TInteger | TFloat | TDate | TDateTime => {
let (percentile_labels, percentile_list): (Vec<String>, Vec<u8>) = self
.which
.percentile_list
.split(',')
.filter_map(|p: &str| {
fast_float2::parse(p.trim())
.ok()
.map(|p_val: f64| (p.trim().to_string(), p_val as u8))
})
.unzip();
let percentile_values =
if let Some(weighted_data) = sorted_weighted_data.as_ref() {
weighted_percentiles(weighted_data, self.total_weight, &percentile_list)
} else {
self.unsorted_stats
.as_mut()
.and_then(|v| v.custom_percentiles(&percentile_list))
};
if let Some(percentile_vals) = percentile_values {
let formatted_values = if typ == TDateTime || typ == TDate {
percentile_labels
.iter()
.zip(percentile_vals.iter())
.map(|(label, p)| {
#[allow(clippy::cast_possible_truncation)]
let ts = p.round() as i64;
let formatted_value = timestamp_ms_to_rfc3339(ts, typ);
format!("{label}: {formatted_value}")
})
.collect::<Vec<_>>()
} else {
percentile_labels
.iter()
.zip(percentile_vals.iter())
.map(|(label, p)| {
let formatted_value = util::round_num(*p, round_places);
format!("{label}: {formatted_value}")
})
.collect::<Vec<_>>()
};
record.push_field(&formatted_values.join(stats_separator));
} else {
record.push_field(EMPTY_STR);
}
},
_ => record.push_field(EMPTY_STR),
}
}
record
}
}
impl Commute for Stats {
#[inline]
fn merge(&mut self, other: Stats) {
self.typ.merge(other.typ);
self.is_ascii &= other.is_ascii;
self.max_precision = self.max_precision.max(other.max_precision);
self.which.merge(other.which);
self.nullcount += other.nullcount;
self.sum_stotlen = self.sum_stotlen.saturating_add(other.sum_stotlen);
self.sum.merge(other.sum);
self.modes.merge(other.modes);
self.unsorted_stats.merge(other.unsorted_stats);
self.online.merge(other.online);
self.online_len.merge(other.online_len);
self.minmax.merge(other.minmax);
if let Some(ref mut wos) = self.weighted_online {
if let Some(ref other_wos) = other.weighted_online {
wos.merge(other_wos);
}
} else if other.weighted_online.is_some() {
self.weighted_online = other.weighted_online;
}
if let Some(ref mut wus) = self.weighted_unsorted_stats {
if let Some(mut other_wus) = other.weighted_unsorted_stats {
wus.append(&mut other_wus);
}
} else if other.weighted_unsorted_stats.is_some() {
self.weighted_unsorted_stats = other.weighted_unsorted_stats;
}
if let Some(ref mut wm) = self.weighted_modes {
if let Some(other_wm) = other.weighted_modes {
for (value, weight) in other_wm {
*wm.entry(value).or_insert(0.0) += weight;
}
}
} else if other.weighted_modes.is_some() {
self.weighted_modes = other.weighted_modes;
}
self.total_weight += other.total_weight;
}
}
#[allow(clippy::enum_variant_names)]
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Clone, Copy, PartialEq, Default, Serialize, Deserialize)]
enum FieldType {
#[default]
TNull,
TString,
TFloat,
TInteger,
TDate,
TDateTime,
}
impl FieldType {
#[allow(clippy::inline_always)]
#[inline(always)]
pub fn from_sample(
infer_dates: bool,
prefer_dmy: bool,
sample: &[u8],
current_type: FieldType,
) -> (FieldType, i64, f64) {
if b"" == sample {
return (FieldType::TNull, 0, 0.0);
}
if current_type == FieldType::TString {
return (FieldType::TString, 0, 0.0);
}
if current_type != FieldType::TFloat
&& let Ok(samp_int) = atoi_simd::parse::<i64>(sample)
{
if samp_int == 0 || unsafe { *sample.get_unchecked(0) != b'0' } {
#[allow(clippy::cast_precision_loss)]
return (FieldType::TInteger, samp_int, samp_int as f64);
}
return (FieldType::TString, 0, 0.0);
}
if let Ok(float_sample) = fast_float2::parse::<f64, &[u8]>(sample) {
return (FieldType::TFloat, 0, float_sample);
}
if !infer_dates {
return (FieldType::TString, 0, 0.0);
}
if let Ok(s) = simdutf8::basic::from_utf8(sample) {
if let Ok(parsed_date) = parse_with_preference(s, prefer_dmy) {
let ts_val = parsed_date.timestamp_millis();
return if ts_val % MS_IN_DAY_INT == 0 {
(FieldType::TDate, ts_val, 0.0)
} else {
(FieldType::TDateTime, ts_val, 0.0)
};
}
} else {
return (FieldType::TString, 0, 0.0);
}
(FieldType::TString, 0, 0.0)
}
}
impl Commute for FieldType {
#[inline]
#[allow(clippy::match_same_arms)]
fn merge(&mut self, other: FieldType) {
*self = match (*self, other) {
(TString, TString) => TString,
(TFloat, TFloat) => TFloat,
(TInteger, TInteger) => TInteger,
(TNull, any) | (any, TNull) => any,
(TFloat, TInteger) | (TInteger, TFloat) => TFloat,
(TDate, TDate) => TDate,
(TDateTime | TDate, TDateTime) | (TDateTime, TDate) => TDateTime,
(_, _) => TString,
};
}
}
const NULL_FTYPE: &str = "NULL";
const STRING_FTYPE: &str = "String";
const FLOAT_FTYPE: &str = "Float";
const INTEGER_FTYPE: &str = "Integer";
const DATE_FTYPE: &str = "Date";
const DATETIME_FTYPE: &str = "DateTime";
impl fmt::Display for FieldType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TNull => write!(f, "{NULL_FTYPE}"),
TString => write!(f, "{STRING_FTYPE}"),
TFloat => write!(f, "{FLOAT_FTYPE}"),
TInteger => write!(f, "{INTEGER_FTYPE}"),
TDate => write!(f, "{DATE_FTYPE}"),
TDateTime => write!(f, "{DATETIME_FTYPE}"),
}
}
}
impl fmt::Debug for FieldType {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TNull => write!(f, "{NULL_FTYPE}"),
TString => write!(f, "{STRING_FTYPE}"),
TFloat => write!(f, "{FLOAT_FTYPE}"),
TInteger => write!(f, "{INTEGER_FTYPE}"),
TDate => write!(f, "{DATE_FTYPE}"),
TDateTime => write!(f, "{DATETIME_FTYPE}"),
}
}
}
impl FieldType {
pub const fn as_str(&self) -> &str {
match self {
TNull => NULL_FTYPE,
TString => STRING_FTYPE,
TFloat => FLOAT_FTYPE,
TInteger => INTEGER_FTYPE,
TDate => DATE_FTYPE,
TDateTime => DATETIME_FTYPE,
}
}
}
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Clone, Default, Serialize, Deserialize, PartialEq)]
struct TypedSum {
float: Option<f64>,
integer: i64,
stotlen: u64, }
impl TypedSum {
#[allow(clippy::inline_always)]
#[inline(always)]
fn add_with_parsed(&mut self, typ: FieldType, sample: &[u8], float_val: f64, int_val: i64) {
#[allow(clippy::cast_precision_loss)]
match typ {
TInteger => {
if let Some(ref mut f) = self.float {
*f += float_val;
} else {
self.integer = self.integer.saturating_add(int_val);
}
},
TFloat => {
if let Some(ref mut f) = self.float {
*f += float_val;
} else {
self.float = Some((self.integer as f64) + float_val);
}
},
TString => {
self.stotlen = self.stotlen.saturating_add(sample.len() as u64);
},
_ => {},
}
}
fn show(&self, typ: FieldType) -> Option<(u64, String)> {
match typ {
TNull | TDate | TDateTime => None,
TInteger => {
match self.integer {
i64::MAX => Some((self.stotlen, OVERFLOW_STRING.to_string())),
i64::MIN => Some((self.stotlen, UNDERFLOW_STRING.to_string())),
_ => Some((
self.stotlen,
itoa::Buffer::new().format(self.integer).to_owned(),
)),
}
},
TFloat => Some((
self.stotlen,
zmij::Buffer::new()
.format(self.float.unwrap_or(0.0))
.to_owned(),
)),
TString => Some((self.stotlen, String::new())),
}
}
}
impl Commute for TypedSum {
#[inline]
fn merge(&mut self, other: TypedSum) {
#[allow(clippy::cast_precision_loss)]
match (self.float, other.float) {
(Some(f1), Some(f2)) => self.float = Some(f1 + f2),
(Some(f1), None) => self.float = Some(f1 + (other.integer as f64)),
(None, Some(f2)) => self.float = Some((self.integer as f64) + f2),
(None, None) => self.integer = self.integer.saturating_add(other.integer),
}
self.stotlen = self.stotlen.saturating_add(other.stotlen);
}
}
#[allow(clippy::unsafe_derive_deserialize)]
#[derive(Clone, Default, Serialize, Deserialize, PartialEq)]
struct TypedMinMax {
floats: MinMax<f64>,
integers: MinMax<i64>,
dates: MinMax<i64>,
strings: MinMax<Vec<u8>>,
str_len: MinMax<usize>,
}
impl TypedMinMax {
#[inline]
fn add_with_parsed(&mut self, typ: FieldType, sample: &[u8], float_val: f64, int_val: i64) {
let sample_len = sample.len();
if sample_len == 0 {
self.str_len.add(0);
return;
}
match typ {
TInteger => {
self.integers.add(int_val);
self.floats.add(float_val);
},
TFloat => {
self.floats.add(float_val);
},
TString => {
self.str_len.add(sample_len);
self.strings.add(sample.to_vec());
},
TNull => {},
_ => {
if int_val != 0 {
self.dates.add(int_val);
}
},
}
}
fn len_range(&self) -> Option<(String, String)> {
if let (Some(min), Some(max)) = (self.str_len.min(), self.str_len.max()) {
Some((
itoa::Buffer::new().format(*min).to_owned(),
itoa::Buffer::new().format(*max).to_owned(),
))
} else {
None
}
}
#[inline]
fn show(
&self,
typ: FieldType,
round_places: u32,
visualize_ws: bool,
) -> Option<(String, String, String, String, String)> {
match typ {
TNull => None,
TString => {
if let (Some(min), Some(max), sort_order, sortiness) = (
self.strings.min(),
self.strings.max(),
self.strings.sort_order(),
self.strings.sortiness(),
) {
let min_str = String::from_utf8_lossy(min).to_string();
let max_str = String::from_utf8_lossy(max).to_string();
let max_length = STATS_STRING_MAX_LENGTH.get_or_init(|| {
std::env::var("QSV_STATS_STRING_MAX_LENGTH")
.ok()
.and_then(|s| atoi_simd::parse::<usize>(s.as_bytes()).ok())
});
let (min_str, max_str) = if let Some(max_len) = *max_length {
(
if min_str.len() > max_len {
format!("{}...", &min_str[..max_len])
} else {
min_str
},
if max_str.len() > max_len {
format!("{}...", &max_str[..max_len])
} else {
max_str
},
)
} else {
(min_str, max_str)
};
let (min_display, max_display) = if visualize_ws {
(
util::visualize_whitespace(&min_str),
util::visualize_whitespace(&max_str),
)
} else {
(min_str, max_str)
};
Some((
min_display,
max_display,
String::new(),
sort_order.to_string(),
util::round_num(sortiness, round_places),
))
} else {
None
}
},
TInteger => {
if let (Some(min), Some(max), sort_order, sortiness) = (
self.integers.min(),
self.integers.max(),
self.integers.sort_order(),
self.integers.sortiness(),
) {
Some((
itoa::Buffer::new().format(*min).to_owned(),
itoa::Buffer::new().format(*max).to_owned(),
itoa::Buffer::new().format(*max - *min).to_owned(),
sort_order.to_string(),
util::round_num(sortiness, round_places),
))
} else {
None
}
},
TFloat => {
if let (Some(min), Some(max), sort_order, sortiness) = (
self.floats.min(),
self.floats.max(),
self.floats.sort_order(),
self.floats.sortiness(),
) {
Some((
zmij::Buffer::new().format(*min).to_owned(),
zmij::Buffer::new().format(*max).to_owned(),
util::round_num(*max - *min, round_places),
sort_order.to_string(),
util::round_num(sortiness, round_places),
))
} else {
None
}
},
TDateTime | TDate => {
if let (Some(min), Some(max), sort_order, sortiness) = (
self.dates.min(),
self.dates.max(),
self.dates.sort_order(),
self.dates.sortiness(),
) {
Some((
timestamp_ms_to_rfc3339(*min, typ),
timestamp_ms_to_rfc3339(*max, typ),
#[allow(clippy::cast_precision_loss)]
util::round_num(
(*max - *min) as f64 / MS_IN_DAY,
u32::max(round_places, 5),
),
sort_order.to_string(),
util::round_num(sortiness, round_places),
))
} else {
None
}
},
}
}
}
impl Commute for TypedMinMax {
#[inline]
fn merge(&mut self, other: TypedMinMax) {
self.floats.merge(other.floats);
self.integers.merge(other.integers);
self.dates.merge(other.dates);
self.strings.merge(other.strings);
self.str_len.merge(other.str_len);
}
}