text:
const FiniteArrMissing{N} = Union{
    AbstractArray{<:Union{Missing,Multiclass{N}}},
    AbstractArray{<:Union{Missing,OrderedFactor{N}}}}

# ---------------------------------------------------
# misclassification rate

struct MisclassificationRate <: Aggregated end

metadata_measure(MisclassificationRate;
                 instances  = ["misclassification_rate", "mcr"],
                 target_scitype           = FiniteArrMissing,
                 prediction_type          = :deterministic,
                 orientation              = :loss)

const MCR = MisclassificationRate
@create_aliases MCR

@create_docs(MisclassificationRate,
body=
"""
A confusion matrix can also be passed as argument.
$INVARIANT_LABEL
""",
scitype=DOC_FINITE)

# calling behaviour:
call(::MCR, ŷ, y) where {V,N} = (y .!= ŷ) |> Mean()
call(::MCR, ŷ, y, w) where {V,N} = (y .!= ŷ) .* w |> Mean()
(::MCR)(cm::ConfusionMatrixObject) = 1.0 - sum(diag(cm.mat)) / sum(cm.mat)

# -------------------------------------------------------------
# accuracy

struct Accuracy <: Aggregated end

metadata_measure(Accuracy;
                 instances = ["accuracy",],
                 target_scitype           = FiniteArrMissing,
                 prediction_type          = :deterministic,
                 orientation              = :score)

@create_aliases Accuracy

@create_docs(Accuracy,
body=
"""
Accuracy is proportion of correct predictions `ŷ[i]` that match the
ground truth `y[i]` observations. $INVARIANT_LABEL
""",
scitype=DOC_FINITE)

# calling behaviour:
call(::Accuracy, args...) = 1.0 - call(misclassification_rate, args...)
(::Accuracy)(m::ConfusionMatrixObject) = sum(diag(m.mat)) / sum(m.mat)

# -----------------------------------------------------------
# balanced accuracy

struct BalancedAccuracy <: Aggregated
    adjusted::Bool
end
BalancedAccuracy(; adjusted=false) = BalancedAccuracy(adjusted)

metadata_measure(BalancedAccuracy;
                 instances = ["balanced_accuracy", "bacc", "bac"],
                 target_scitype           = FiniteArrMissing,
                 prediction_type          = :deterministic,
                 orientation              = :score)

const BACC = BalancedAccuracy
@create_aliases BACC

@create_docs(BalancedAccuracy,
body=
"""
Balanced accuracy compensates standard [`Accuracy`](@ref) for class imbalance.
See [https://en.wikipedia.org/wiki/Precision_and_recall#Imbalanced_data](https://en.wikipedia.org/wiki/Precision_and_recall#Imbalanced_data).

Setting `adjusted=true` rescales the score in the way prescribed in
[L. Mosley (2013): A balanced approach to the multi-class imbalance
problem. PhD thesis, Iowa State
University](https://lib.dr.iastate.edu/etd/13537/). In the binary
case, the adjusted balanced accuracy is also known as *Youden’s J
statistic*, or *informedness*.

$INVARIANT_LABEL
""",
scitype=DOC_FINITE)

function call(m::BACC, ŷm, ym, wm::Union{Nothing,Arr{<:Real}}=nothing)

    ŷ, y, w = _skipinvalid(ŷm, ym, wm)

    if w === nothing
        n_given_class = StatsBase.countmap(y)
        freq(i) = @inbounds n_given_class[y[i]]
        ŵ = 1 ./ freq.(eachindex(y))
    else # following sklearn, which is non-linear
        ŵ = similar(w)
        @inbounds for i in eachindex(w)
            ŵ[i] = w[i] / sum(w .* (y .== y[i]))
        end
    end
    s = sum(ŵ)
    score = sum((ŷ .== y) .* ŵ) / sum(ŵ)
    if m.adjusted
        n_classes = length(levels(y))
        chance = 1 / n_classes
        score -= chance
        score /= 1 - chance
    end
    return score
end

# ==================================================================
## DETERMINISTIC BINARY PREDICTIONS - ORDER-INDEPENDENT

# ------------------------------------------------------------------
# Matthew's correlation

struct MatthewsCorrelation <: Aggregated end

metadata_measure(MatthewsCorrelation;
                 instances = ["matthews_correlation", "mcc"],
                 target_scitype           = FiniteArrMissing{2},
                 prediction_type          = :deterministic,
                 orientation              = :score,
                 supports_weights         = false)
const MCC = MatthewsCorrelation
@create_aliases MCC

@create_docs(MatthewsCorrelation,
body=
"""
[https://en.wikipedia.org/wiki/Matthews_correlation_coefficient](https://en.wikipedia.org/wiki/Matthews_correlation_coefficient)
$INVARIANT_LABEL
""",
scitype=DOC_FINITE_BINARY)

# calling behaviour:
function (::MCC)(cm::ConfusionMatrixObject{C}) where C
    # http://rk.kvl.dk/introduction/index.html
    # NOTE: this is O(C^3), there may be a clever way to
    # speed this up though in general this is only used for low  C
    num = 0
    @inbounds for k in 1:C, l in 1:C, m in 1:C
        num += cm[k,k] * cm[l,m] - cm[k,l] * cm[m,k]
    end
    den1 = 0
    den2 = 0
    @inbounds for k in 1:C
        a = sum(cm[k, :])
        b = sum(cm[setdiff(1:C, k), :])
        den1 += a * b
        a = sum(cm[:, k])
        b = sum(cm[:, setdiff(1:C, k)])
        den2 += a * b
    end
    mcc = num / sqrt(float(den1) * float(den2))

    isnan(mcc) && return 0
    return mcc
end

call(m::MCC, ŷ, y) = _confmat(ŷ, y, warn=false) |> m

# ==========================================================================
# DETERMINISTIC BINARY PREDICTIONS - ORDER DEPENDENT

const CM2 = ConfusionMatrixObject{2}

# --------------------------------------------------------------------------
# FScore

struct FScore{T<:Real} <: Aggregated
    β::T
    rev::Union{Nothing,Bool}
end

FScore(; β=1.0, rev=nothing) = FScore(β, rev)

metadata_measure(FScore;
                 human_name = "F-Score",
                 instances = ["f1score",],
                 target_scitype           = FiniteArrMissing{2},
                 prediction_type          = :deterministic,
                 orientation              = :score,
                 supports_weights         = false)

@create_aliases FScore

@create_docs(FScore,
body=
"""
This is the one-parameter generalization, ``F_β``, of the F-measure or
balanced F-score.

[https://en.wikipedia.org/wiki/F1_score](https://en.wikipedia.org/wiki/F1_score)

Constructor signature: `FScore(; β=1.0, rev=true)`.

By default, the second element of `levels(y)` is designated as
`true`. To reverse roles, specify `rev=true`.
""",
scitype=DOC_ORDERED_FACTOR_BINARY,
footer="Constructor signature: `FScore(β=1.0, rev=false)`. ")

# calling on conf matrix:
function (score::FScore)(m::CM2)
    β = score.β
    β2   = β^2
    tp = _tp(m)
    fn = _fn(m)
    fp = _fp(m)
    return (1 + β2) * tp / ((1 + β2)*tp + β2*fn + fp)
end

# calling on arrays:
call(m::FScore, ŷ, y) = _confmat(ŷ, y; rev=m.rev) |> m

# -------------------------------------------------------------------------
# TruePositive and its cousins - struct and metadata declerations

const TRUE_POSITIVE_AND_COUSINS =
    (:TruePositive, :TrueNegative, :FalsePositive, :FalseNegative,
     :TruePositiveRate, :TrueNegativeRate, :FalsePositiveRate,
     :FalseNegativeRate, :FalseDiscoveryRate, :Precision,
     :NegativePredictiveValue)

for M in TRUE_POSITIVE_AND_COUSINS
    ex = quote
        struct $M <: Aggregated rev::Union{Nothing,Bool} end
        $M(; rev=nothing) = $M(rev)
    end
    eval(ex)
end

metadata_measure.((FalsePositive, FalseNegative);
    target_scitype           = FiniteArrMissing{2},
    prediction_type          = :deterministic,
    orientation              = :loss,
    aggregation              = Sum(),
    supports_weights         = false)

metadata_measure.((FalsePositiveRate, FalseNegativeRate, FalseDiscoveryRate);
    target_scitype           = FiniteArrMissing{2},
    prediction_type          = :deterministic,
    orientation              = :loss,
    supports_weights         = false)

metadata_measure.((TruePositive, TrueNegative);
    target_scitype           = FiniteArrMissing{2},
    prediction_type          = :deterministic,
    orientation              = :score,
    aggregation              = Sum(),
    supports_weights         = false)

metadata_measure.((TruePositiveRate, TrueNegativeRate, Precision,
                   NegativePredictiveValue);
    target_scitype           = FiniteArrMissing{2},
    prediction_type          = :deterministic,
    orientation              = :score,
    supports_weights         = false)

# adjustments:
instances(::Type{<:TruePositive}) = ["true_positive", "truepositive"]
human_name(::Type{<:TruePositive})  = "number of true positives"

instances(::Type{<:TrueNegative}) = ["true_negative", "truenegative"]
human_name(::Type{<:TrueNegative}) = "number of true negatives"

instances(::Type{<:FalsePositive}) = ["false_positive", "falsepositive"]
human_name(::Type{<:FalsePositive}) = "number of false positives"

instances(::Type{<:FalseNegative}) = ["false_negative", "falsenegative"]
human_name(::Type{<:FalseNegative}) = "number of false negatives"

instances(::Type{<:TruePositiveRate}) =
    ["true_positive_rate", "truepositive_rate",
     "tpr", "sensitivity", "recall", "hit_rate"]
human_name(::Type{<:TruePositiveRate}) =
    "true positive rate (a.k.a recall)"

instances(::Type{<:TrueNegativeRate}) =
    ["true_negative_rate", "truenegative_rate", "tnr",
     "specificity", "selectivity"]

instances(::Type{<:FalsePositiveRate}) =
    ["false_positive_rate", "falsepositive_rate",
     "fpr", "fallout"]
                               "."
instances(::Type{<:FalseNegativeRate}) =
    ["false_negative_rate", "falsenegative_rate", "fnr", "miss_rate"]
                               "."
instances(::Type{<:FalseDiscoveryRate}) =
    ["false_discovery_rate", "falsediscovery_rate", "fdr"]

instances(::Type{<:NegativePredictiveValue}) =
    ["negative_predictive_value", "negativepredictive_value", "npv"]

instances(::Type{<:Precision}) =
    ["positive_predictive_value", "ppv", "positivepredictive_value", "precision"]
human_name(::Type{<:Precision}) =
    "precision (a.k.a. positive predictive value)"

# ---------------------------------------------------------------------
# TruePositive and its cousins - doc-string building and alias creation

for M in TRUE_POSITIVE_AND_COUSINS
    eval(quote
         $M == Precision || @create_aliases $M # precision handled separately

         @create_docs($M,
         body=
         """
         Assigns `false` to first element of `levels(y)`. To reverse roles,
         use `$(name($M))(rev=true)`.
         """,
         scitype=DOC_ORDERED_FACTOR_BINARY)
         end)
end

# type aliases:
const TNR = TrueNegativeRate
const Specificity = TrueNegativeRate
const TPR = TruePositiveRate
const Recall = TPR
const FPR = FalsePositiveRate
const FNR = FalseNegativeRate
const FDR = FalseDiscoveryRate
const NPV = NegativePredictiveValue
const PPV = Precision

# special case of precision; cannot generate alias's automatically due
# to conflict with Base.precision:
const positive_predictive_value = Precision()
const ppv = Precision()
const positivepredictive_value = Precision()

# ----------------------------------------------------------------------
# TruePositive and its cousins - helper functions for confusion matrices

_tp(m::CM2) = m[2,2]
_tn(m::CM2) = m[1,1]
_fp(m::CM2) = m[2,1]
_fn(m::CM2) = m[1,2]

_tpr(m::CM2) = _tp(m) / (_tp(m) + _fn(m))
_tnr(m::CM2) = _tn(m) / (_tn(m) + _fp(m))
_fpr(m::CM2) = 1 - _tnr(m)
_fnr(m::CM2) = 1 - _tpr(m)

_fdr(m::CM2) = _fp(m) / (_tp(m) + _fp(m))
_npv(m::CM2) = _tn(m) / (_tn(m) + _fn(m))

# ----------------------------------------------------------------------
# TruePositive and its cousins - calling behaviour

# NOTE: here we assume the CM was constructed a priori with the
# proper ordering so the field `rev` in the measure is ignored

# on confusion matrices:
(::TruePositive)(m::CM2)  = _tp(m)
(::TrueNegative)(m::CM2)  = _tn(m)
(::FalsePositive)(m::CM2) = _fp(m)
(::FalseNegative)(m::CM2) = _fn(m)
(::TPR)(m::CM2) = _tpr(m)
(::TNR)(m::CM2) = _tnr(m)
(::FPR)(m::CM2) = _fpr(m)
(::FNR)(m::CM2) = _fnr(m)
(::FDR)(m::CM2) = _fdr(m)
(::NPV)(m::CM2) = _npv(m)
(::Precision)(m::CM2) = 1.0 - _fdr(m)

# on arrays (ŷ, y):
for M_ex in TRUE_POSITIVE_AND_COUSINS
    @eval call(m::$M_ex, ŷ, y) = _confmat(ŷ, y; rev=m.rev) |> m
end

# since Base.precision exists (as single argument function) we
# manually overload Base.precision:
Base.precision(m::CM2) = m |> Precision()
function Base.precision(ŷ, y)
    _check(Precision(), ŷ, y)
    call(Precision(), ŷ, y)
end

# =================================================================
# MULTICLASS AND ORDER INDEPENDENT

const CM = ConfusionMatrixObject{N} where N

abstract type MulticlassAvg end
struct MacroAvg <: MulticlassAvg end
struct MicroAvg <: MulticlassAvg end
struct NoAvg <: MulticlassAvg end

const macro_avg = MacroAvg()
const micro_avg = MicroAvg()
const no_avg    = NoAvg()

const DS_AVG_RET = "Options for `average` are: `no_avg`, `macro_avg` "*
    "(default) and `micro_avg`. Options for `return_type`, "*
    "applying in the `no_avg` case, are: `LittleDict` (default) or "*
    "`Vector`. "

const DS_RET = "Options for `return_type` are: "*
    "`LittleDict`(default) or "*
    "`Vector`. "

const CLASS_W = "An optional `AbstractDict`, denoted `class_w` above, "*
    "keyed on `levels(y)`, specifies class weights. It applies if "*
    "`average=macro_avg` or `average=no_avg`."

"""
    MulticlassFScore(; β=1.0, average=macro_avg, return_type=LittleDict)

One-parameter generalization, ``F_β``, of the F-measure or balanced F-score for
multiclass observations.

    MulticlassFScore()(ŷ, y)
    MulticlassFScore()(ŷ, y, class_w)

Evaluate the default score on multiclass observations, `ŷ`, given
ground truth values, `y`. $DS_AVG_RET $CLASS_W

For more information, run `info(MulticlassFScore)`.

"""
struct MulticlassFScore{T<:Real,
                        M<:MulticlassAvg,
                        U<:Union{Vector, LittleDict}} <:Aggregated
    β::T
    average::M
    return_type::Type{U}
end

MulticlassFScore(; β=1.0, average=macro_avg, return_type=LittleDict) =
    MulticlassFScore(β, average, return_type)

metadata_measure(MulticlassFScore;
                 instances = ["macro_f1score", "micro_f1score",
                              "multiclass_f1score"],
                 target_scitype           = FiniteArrMissing,
                 prediction_type          = :deterministic,
                 orientation              = :score,
                 supports_weights         = false,
                 supports_class_weights   = true)

MLJModelInterface.docstring(::Type{<:MulticlassFScore}) =
    "Multiclass F_β score; aliases: " *
    "`macro_f1score=MulticlassFScore()`, "*
    "`multiclass_f1score=MulticlassFScore()` " *
    "`micro_f1score=MulticlassFScore(average=micro_avg)`."

const micro_f1score      = MulticlassFScore(average=micro_avg)
const macro_f1score      = MulticlassFScore(average=macro_avg)
const multiclass_f1score = MulticlassFScore(average=macro_avg)

for M in (:MulticlassTruePositive, :MulticlassTrueNegative,
          :MulticlassFalsePositive, :MulticlassFalseNegative)
    ex = quote
        struct $M{U<:Union{Vector, LittleDict}} <: Aggregated
            return_type::Type{U}
        end
#        $M(return_type::Type{U}) where {U} = $M(return_type)
        $M(; return_type=LittleDict) = $M(return_type)
    end
    eval(ex)
end

const _mtp_vec = MulticlassTruePositive(return_type=Vector)
const _mfn_vec = MulticlassFalseNegative(return_type=Vector)
const _mfp_vec = MulticlassFalsePositive(return_type=Vector)
const _mtn_vec = MulticlassTrueNegative(return_type=Vector)

for M in (:MulticlassTruePositiveRate, :MulticlassTrueNegativeRate,
          :MulticlassFalsePositiveRate, :MulticlassFalseNegativeRate,
          :MulticlassFalseDiscoveryRate, :MulticlassPrecision,
          :MulticlassNegativePredictiveValue)
    ex = quote
        struct $M{T<:MulticlassAvg, U<:Union{Vector, LittleDict}} <: Aggregated
            average::T
            return_type::Type{U}
        end
        # @ablaom says next line looks redundant:
        $M(average::T, return_type::Type{U}) where {T, U} =
            $M(average, return_type)
        $M(; average=macro_avg,
           return_type=LittleDict) where T<:MulticlassAvg where
           U<:Union{Vector, LittleDict} = $M(average, return_type)
    end
    eval(ex)
end

metadata_measure.((MulticlassFalsePositive, MulticlassFalseNegative);
    target_scitype           = FiniteArrMissing,
    prediction_type          = :deterministic,
    orientation              = :loss,
    aggregation               = Sum(),
    is_feature_dependent     = false,
    supports_weights         = false,
    supports_class_weights   = false)

metadata_measure.((MulticlassFalsePositiveRate, MulticlassFalseNegativeRate,
                   MulticlassFalseDiscoveryRate);
    target_scitype           = FiniteArrMissing,
    prediction_type          = :deterministic,
    orientation              = :loss,
    is_feature_dependent     = false,
    supports_weights         = false,
    supports_class_weights   = true)

metadata_measure.((MulticlassTruePositive, MulticlassTrueNegative);
    target_scitype           = FiniteArrMissing,
    prediction_type          = :deterministic,
    orientation              = :score,
    aggregation              = Sum(),
    is_feature_dependent     = false,
    supports_weights         = false,
    supports_class_weights   = false)

metadata_measure.((MulticlassTrueNegativeRate, MulticlassNegativePredictiveValue);
    target_scitype           = FiniteArrMissing,
    prediction_type          = :deterministic,
    orientation              = :score,
    is_feature_dependent     = false,
    supports_weights         = false,
    supports_class_weights   = true)

metadata_measure.((MulticlassTruePositiveRate, MulticlassPrecision);
    target_scitype           = FiniteArrMissing,
    prediction_type          = :deterministic,
    orientation              = :score,
    is_feature_dependent     = false,
    supports_weights         = false,
    supports_class_weights   = true)

MMI.docstring(::Type{<:MulticlassTruePositive})  =
    "Number of true positives; " *
    "aliases: `multiclass_true_positive`, `multiclass_truepositive`."
instances(::Type{<:MulticlassTruePositive})  =
    ["multiclass_true_positive", "multiclass_truepositive"]
MMI.docstring(::Type{<:MulticlassTrueNegative})  =
    "Number of true negatives; " *
    "aliases: `multiclass_true_negative`, `multiclass_truenegative`."
instances(::Type{<:MulticlassTrueNegative})  =
    ["multiclass_true_negative", "multiclass_truenegative"]
MMI.docstring(::Type{<:MulticlassFalsePositive}) =
    "Number of false positives; " *
    "aliases: `multiclass_false_positive`, `multiclass_falsepositive`."
instances(::Type{<:MulticlassFalsePositive}) =
    ["multiclass_false_positive", "multiclass_falsepositive"]
MMI.docstring(::Type{<:MulticlassFalseNegative}) =
    "Number of false negatives; " *
    "aliases: `multiclass_false_negative`, `multiclass_falsenegative`."
instances(::Type{<:MulticlassFalseNegative}) =
    ["multiclass_false_negative", "multiclass_falsenegative"]

MMI.docstring(::Type{<:MulticlassTruePositiveRate}) =
    "multiclass true positive rate; aliases: " *
    "`multiclass_true_positive_rate`, `multiclass_tpr`, " *
    "`multiclass_sensitivity`, `multiclass_recall`, " *
    "`multiclass_hit_rate`, `multiclass_truepositive_rate`, "
instances(::Type{<:MulticlassTruePositiveRate}) =
    ["multiclass_true_positive_rate", "multiclass_tpr",
    "multiclass_sensitivity", "multiclass_recall",
    "multiclass_hit_rate", "multiclass_truepositive_rate"]
MMI.docstring(::Type{<:MulticlassTrueNegativeRate}) =
    "multiclass true negative rate; aliases: " *
    "`multiclass_true_negative_rate`, `multiclass_tnr` " *
    " `multiclass_specificity`, `multiclass_selectivity`, " *
    "`multiclass_truenegative_rate`."
instances(::Type{<:MulticlassTrueNegativeRate}) =
    ["multiclass_true_negative_rate", "multiclass_tnr",
    "multiclass_specificity", "multiclass_selectivity",
    "multiclass_truenegative_rate"]
MMI.docstring(::Type{<:MulticlassFalsePositiveRate}) =
                       "multiclass false positive rate; aliases: " *
                       "`multiclass_false_positive_rate`, `multiclass_fpr` " *
                       "`multiclass_fallout`, `multiclass_falsepositive_rate`."
instances(::Type{<:MulticlassFalsePositiveRate}) =
    ["multiclass_false_positive_rate", "multiclass_fpr",
     "multiclass_fallout", "multiclass_falsepositive_rate"]
MMI.docstring(::Type{<:MulticlassFalseNegativeRate}) =
    "multiclass false negative rate; aliases: " *
    "`multiclass_false_negative_rate`, `multiclass_fnr`, " *
    "`multiclass_miss_rate`, `multiclass_falsenegative_rate`."
instances(::Type{<:MulticlassFalseNegativeRate}) =
    ["multiclass_false_negative_rate", "multiclass_fnr",
    "multiclass_miss_rate", "multiclass_falsenegative_rate"]
MMI.docstring(::Type{<:MulticlassFalseDiscoveryRate}) =
    "multiclass false discovery rate; "*
    "aliases: `multiclass_false_discovery_rate`, " *
    "`multiclass_falsediscovery_rate`, `multiclass_fdr`."
instances(::Type{<:MulticlassFalseDiscoveryRate}) =
    ["multiclass_falsediscovery_rate", "multiclass_fdr",
     "multiclass_false_discovery_rate"]
MMI.docstring(::Type{<:MulticlassNegativePredictiveValue}) =
    "multiclass negative predictive value; aliases: " *
    "`multiclass_negative_predictive_value`, " *
    "`multiclass_negativepredictive_value`, `multiclass_npv`."
instances(::Type{<:MulticlassNegativePredictiveValue}) =
    ["multiclass_negative_predictive_value",
    "multiclass_negativepredictive_value", "multiclass_npv"]
MMI.docstring(::Type{<:MulticlassPrecision}) =
  "multiclass positive predictive value (aka precision);"*
  " aliases: `multiclass_positive_predictive_value`, `multiclass_ppv`, " *
  "`multiclass_positivepredictive_value`, " *
  "`multiclass_precision`."
instances(::Type{<:MulticlassPrecision}) =
    ["multiclass_positive_predictive_value", "multiclass_ppv",
     "multiclass_positivepredictive_value", "multiclass_precision"]

const W_KEY_MISMATCH = "Encountered target with levels different from the " *
                       "keys of user-specified dictionary of class weights."
const W_PROMOTE_WARN = "Using macro averaging instead of micro averaging, as "*
    "class weights specified. "

# ----------------------------------------------------
# MulticlassTruePositive

"""
    MulticlassTruePositive(; return_type=LittleDict)

$(docstring(MulticlassTruePositive()))

    MulticlassTruePositive()(ŷ, y)

Number of true positives for multiclass observations `ŷ` and ground
truth `y`, using default return type. $DS_RET

For more information, run `info(MulticlassTruePositive)`.

"""
function MulticlassTruePositive end
const multiclass_true_positive  = MulticlassTruePositive()
const multiclass_truepositive   = MulticlassTruePositive()
const mtp = MulticlassTruePositive()

# ----------------------------------------------------
# MulticlassTrueNegative

"""
    MulticlassTrueNegative(; return_type=LittleDict)

$(docstring(MulticlassTrueNegative()))

    MulticlassTrueNegative()(ŷ, y)

Number of true negatives for multiclass observations `ŷ` and ground truth
`y`, using default return type. $DS_RET

For more information, run `info(MulticlassTrueNegative)`.

"""
function MulticlassTrueNegative end
const multiclass_true_negative  = MulticlassTrueNegative()
const multiclass_truenegative   = MulticlassTrueNegative()
const mtn = MulticlassTrueNegative()

# ----------------------------------------------------
# MulticlassFalsePositive

"""
    MulticlassFalsePositive(; return_type=LittleDict)

$(docstring(MulticlassFalsePositive()))

    MulticlassFalsePositive()(ŷ, y)

Number of false positives for multiclass observations `ŷ` and ground
truth `y`, using default return type. $DS_RET

For more information, run `info(MulticlassFalsePositive)`.

"""
function MulticlassPositive end
const multiclass_false_positive = MulticlassFalsePositive()
const multiclass_falsepositive  = MulticlassFalsePositive()
const mfp = MulticlassFalsePositive()

# ----------------------------------------------------
# MulticlassFalseNegative

"""
    MulticlassFalseNegative(; return_type=LittleDict)

$(docstring(MulticlassFalseNegative()))

    MulticlassFalseNegative()(ŷ, y)

Number of false negatives for multiclass observations `ŷ` and ground
truth `y`, using default return type. $DS_RET

For more information, run `info(MulticlassFalseNegative)`.

"""
function MulticlassNegative end
const multiclass_false_negative = MulticlassFalseNegative()
const multiclass_falsenegative  = MulticlassFalseNegative()
const mfn = MulticlassFalseNegative()

# ----------------------------------------------------
# MulticlassTruePositiveRate

"""
    MulticlassTruePositiveRate(; average=macro_avg, return_type=LittleDict)

$(docstring(MulticlassTruePositiveRate()))

    MulticlassTruePositiveRate(ŷ, y)
    MulticlassTruePositiveRate(ŷ, y, class_w)

True positive rate (a.k.a. sensitivity, recall, hit rate) for
multiclass observations `ŷ` and ground truth `y`, using default
averaging and return type. $DS_AVG_RET $CLASS_W

For more information, run `info(MulticlassTruePositiveRate)`.

"""
function MulticlassTruePositiveRate end
const multiclass_true_positive_rate = MulticlassTruePositiveRate()
const multiclass_truepositive_rate  = MulticlassTruePositiveRate()
const multiclass_tpr                = MulticlassTruePositiveRate()
const multiclass_sensitivity        = MulticlassTruePositiveRate()
const multiclass_hit_rate           = MulticlassTruePositiveRate()
const MTPR                          = MulticlassTruePositiveRate
const multiclass_recall             = MulticlassTruePositiveRate()
const MulticlassRecall              = MulticlassTruePositiveRate

# ----------------------------------------------------
# MulticlassTrueNegativeRate

"""
    MulticlassTrueNegativeRate(; average=macro_avg, return_type=LittleDict)

$(docstring(MulticlassTrueNegativeRate()))

    MulticlassTrueNegativeRate()(ŷ, y)
    MulticlassTrueNegativeRate()(ŷ, y, class_w)

True negative rate for multiclass observations `ŷ` and ground truth
`y`, using default averaging and return type. $DS_AVG_RET $CLASS_W

For more information, run `info(MulticlassTrueNegativeRate)`.

"""
function MulticlassTrueNegativeRate end
const multiclass_true_negative_rate = MulticlassTrueNegativeRate()
const multiclass_truenegative_rate  = MulticlassTrueNegativeRate()
const multiclass_tnr                = MulticlassTrueNegativeRate()
const multiclass_specificity        = MulticlassTrueNegativeRate()
const multiclass_selectivity        = MulticlassTrueNegativeRate()
const MulticlassSpecificity         = MulticlassTrueNegativeRate
const MTNR                          = MulticlassTrueNegativeRate

# ----------------------------------------------------
# MulticlassFalsePositiveRate

"""
    MulticlassFalsePositiveRate(; average=macro_avg, return_type=LittleDict)

$(docstring(MulticlassFalsePositiveRate()))

    MulticlassFalsePositiveRate()(ŷ, y)
    MulticlassFalsePositiveRate()(ŷ, y, class_w)

False positive rate for multiclass observations `ŷ` and ground truth
`y`, using default averaging and return type.  $DS_AVG_RET $CLASS_W

For more information, run `info(MulticlassFalsePositiveRate)`.

"""
function MulticlassFalsePositiveRate end
const multiclass_false_positive_rate = MulticlassFalsePositiveRate()
const multiclass_falsepositive_rate  = MulticlassFalsePositiveRate()
const multiclass_fpr                 = MulticlassFalsePositiveRate()
const MFPR                           = MulticlassFalsePositiveRate
const multiclass_fallout             = MFPR()

# ----------------------------------------------------
# MulticlassFalseNegativeRate

"""
    MulticlassFalseNegativeRate(; average=macro_avg, return_type=LittleDict)

$(docstring(MulticlassFalseNegativeRate()))

    MulticlassFalseNegativeRate()(ŷ, y)
    MulticlassFalseNegativeRate()(ŷ, y, class_w)

False negative rate for multiclass observations `ŷ` and ground truth
`y`, using default averaging and return type.  $DS_AVG_RET $CLASS_W

For more information, run `info(MulticlassFalseNegativeRate)`.

"""
function MulticlassFalseNegativeRate end
const multiclass_false_negative_rate = MulticlassFalseNegativeRate()
const multiclass_falsenegative_rate  = MulticlassFalseNegativeRate()
const multiclass_fnr                 = MulticlassFalseNegativeRate()
const MFNR                           = MulticlassFalseNegativeRate
const multiclass_miss_rate           = MFNR()

# ----------------------------------------------------
# MulticlassFalseDiscoveryRate

"""
    MulticlassFalseDiscoveryRate(; average=macro_avg, return_type=LittleDict)

$(docstring(MulticlassFalseDiscoveryRate()))

    MulticlassFalseDiscoveryRate()(ŷ, y)
    MulticlassFalseDiscoveryRate()(ŷ, y, class_w)

False discovery rate for multiclass observations `ŷ` and ground truth
`y`, using default averaging and return type.  $DS_AVG_RET $CLASS_W

For more information, run `info(MulticlassFalseDiscoveryRate)`.

"""
function MulticlassFalseDiscoveryRate end
const multiclass_false_discovery_rate = MulticlassFalseDiscoveryRate()
const multiclass_falsediscovery_rate  = MulticlassFalseDiscoveryRate()
const multiclass_fdr                  = MulticlassFalseDiscoveryRate()
const MFDR                            = MulticlassFalseDiscoveryRate

# ----------------------------------------------------
# MulticlassPrecision

"""
    MulticlassPrecision(; average=macro_avg, return_type=LittleDict)

$(docstring(MulticlassPrecision()))

    MulticlassPrecision()(ŷ, y)
    MulticlassPrecision()(ŷ, y, class_w)

Precision for multiclass observations `ŷ` and ground truth `y`, using
default averaging and return type. $DS_AVG_RET $CLASS_W

For more information, run `info(MulticlassPrecision)`.

"""
function MulticlassPrecision end
const multiclass_precision                 = MulticlassPrecision()
const multiclass_ppv                       = MulticlassPrecision()
const multiclass_positive_predictive_value = MulticlassPrecision()
const multiclass_positivepredictive_value  = MulticlassPrecision()
const MPPV                                 = MulticlassPrecision

# ----------------------------------------------------
# MulticlassNegativePredictiveValue

"""
    MulticlassNegativePredictiveValue(; average=macro_avg, return_type=LittleDict)

$(docstring(MulticlassNegativePredictiveValue()))

    MulticlassNegativePredictiveValue()(ŷ, y)
    MulticlassNegativePredictiveValue()(ŷ, y, class_w)

Negative predictive value for multiclass observations `ŷ` and ground truth
`y`, using default averaging and return type. $DS_AVG_RET $CLASS_W

For more information, run `info(MulticlassNegativePredictiveValue)`.

"""
function MulticlassNegativePredictiveValue end
const multiclass_npv                       = MulticlassNegativePredictiveValue()
const multiclass_negative_predictive_value = MulticlassNegativePredictiveValue()
const multiclass_negativepredictive_value  = MulticlassNegativePredictiveValue()
const MNPV                                 = MulticlassNegativePredictiveValue

# -----------------------------------------------------
## INTERNAL FUNCTIONS ON MULTICLASS CONFUSION MATRIX

_mtp(m::CM, return_type::Type{Vector}) = diag(m.mat)
_mtp(m::CM, return_type::Type{LittleDict}) =
    LittleDict(m.labels, diag(m.mat))

_mfp(m::CM, return_type::Type{Vector}) =
    (col_sum = vec(sum(m.mat, dims=2)); col_sum .-= diag(m.mat))

_mfp(m::CM, return_type::Type{LittleDict}) =
    (col_sum = vec(sum(m.mat, dims=2)); col_sum .-= diag(m.mat);
     LittleDict(m.labels, col_sum))

_mfn(m::CM, return_type::Type{Vector}) =
    (row_sum = vec(collect(transpose(sum(m.mat, dims=1))));
     row_sum .-= diag(m.mat))

_mfn(m::CM, return_type::Type{LittleDict}) =
    (row_sum = vec(collect(transpose(sum(m.mat, dims=1))));
     row_sum .-= diag(m.mat); LittleDict(m.labels, row_sum))

function _mtn(m::CM, return_type::Type{Vector})
    _sum = sum(m.mat, dims=2)
    _sum .= sum(m.mat) .- (_sum .+= sum(m.mat, dims=1)'.- diag(m.mat))
    return vec(_sum)
end

function _mtn(m::CM, return_type::Type{LittleDict})
    _sum = sum(m.mat, dims=2)
    _sum .= sum(m.mat) .- (_sum .+= sum(m.mat, dims=1)'.- diag(m.mat))
    return LittleDict(m.labels, vec(_sum))
end

@inline function _mean(x::Arr{<:Real})
    for i in eachindex(x)
        @inbounds x[i] = ifelse(isnan(x[i]), zero(eltype(x)), x[i])
    end
    return mean(x)
end

@inline function _class_w(level_m::Arr{<:String},
                          class_w::AbstractDict{<:Any, <:Real})
    class_w_labels = levels(keys(class_w))
    string.(class_w_labels) == level_m || throw(ArgumentError(W_KEY_MISMATCH))
    return [class_w[l] for l in class_w_labels]
end

@inline function _mc_helper(m::CM, a::Arr{<:Real}, b::Arr{<:Real},
                            average::NoAvg, return_type::Type{Vector})
    return vec(a ./ (a + b))
end

@inline function _mc_helper(m::CM, a::Arr{<:Real}, b::Arr{<:Real},
                            average::NoAvg, return_type::Type{LittleDict})
    return LittleDict(m.labels, _mc_helper(m, a, b, average, Vector))
end

@inline function _mc_helper(m::CM, a::Arr{<:Real}, b::Arr{<:Real},
                            average::MacroAvg, return_type)
    return _mean(_mc_helper(m, a, b, no_avg, Vector))
end

@inline function _mc_helper(m::CM, a::Arr{<:Real}, b::Arr{<:Real},
                            average::MicroAvg, return_type)
    a_sum, b_sum = sum(a), sum(b)
    return a_sum / (a_sum + b_sum)
end

@inline function _mc_helper(m::CM, a::Arr{<:Real}, b::Arr{<:Real},
                            class_w::AbstractDict{<:Any, <:Real},
                            average::NoAvg, return_type::Type{Vector})
    level_w = _class_w(m.labels, class_w)
    return _mc_helper(m, a, b, no_avg, return_type) .* level_w
end

@inline function _mc_helper(m::CM, a::Arr{<:Real}, b::Arr{<:Real},
                            class_w::AbstractDict{<:Any, <:Real},
                            average::MacroAvg, return_type::Type{Vector})
    return _mean(_mc_helper(m, a, b, class_w, no_avg, return_type))
end

@inline function _mc_helper(m::CM, a::Arr{<:Real}, b::Arr{<:Real},
                            class_w::AbstractDict{<:Any, <:Real},
                            average::MicroAvg, return_type)
    @warn W_PROMOTE_WARN
    return _mc_helper(m, a, b, class_w, macro_avg, Vector)
end

@inline function _mc_helper_b(m::CM, helper_name,
                              class_w::AbstractDict{<:Any, <:Real},
                              average::NoAvg, return_type::Type{Vector})
    level_w = _class_w(m.labels, class_w)
    return (1 .- helper_name(m, no_avg, return_type)) .* level_w
end

@inline function _mc_helper_b(m::CM, helper_name,
                              class_w::AbstractDict{<:Any, <:Real},
                              average::NoAvg, return_type::Type{LittleDict})
    level_w = _class_w(m.labels, class_w)
    return LittleDict(m.labels, ((1 .- helper_name(m, no_avg, Vector)) .* level_w))
end

@inline function _mc_helper_b(m::CM, helper_name,
                              class_w::AbstractDict{<:Any, <:Real},
                              average::MacroAvg, return_type)
    return _mean(_mc_helper_b(m, helper_name, class_w, no_avg, Vector))
end

@inline function _mc_helper_b(m::CM, helper_name,
                              class_w::AbstractDict{<:Any, <:Real},
                              average::MicroAvg, return_type)
    @warn W_PROMOTE_WARN
    return _mc_helper_b(m, helper_name, class_w, macro_avg, Vector)
end

@inline function _mc_helper_b(m::CM, helper_name, average::NoAvg,
                              return_type::Type{LittleDict})
    return LittleDict(m.labels, 1.0 .- helper_name(m, average, Vector))
end

@inline function _mc_helper_b(m::CM, helper_name, average::NoAvg,
                              return_type::Type{Vector})
    return 1.0 .- helper_name(m, average, Vector)
end

@inline function _mc_helper_b(m::CM, helper_name, average::MacroAvg,
                              return_type)
    return 1.0 .- helper_name(m, average, Vector)
end

@inline function _mc_helper_b(m::CM, helper_name, average::MicroAvg,
                              return_type)
    return 1.0 .- helper_name(m, average, Vector)
end

@inline function _mc_helper(m::CM, a::Arr{<:Real}, b::Arr{<:Real},
                            class_w::AbstractDict{<:Any, <:Real},
                            average::NoAvg, return_type::Type{LittleDict})
    level_w = _class_w(m.labels, class_w)
    return LittleDict(m.labels, _mc_helper(m, a, b, class_w, no_avg, Vector))
end

@inline function _mc_helper(m::CM, a::Arr{<:Real}, b::Arr{<:Real},
                            class_w::AbstractDict{<:Any, <:Real},
                            average::MacroAvg, return_type::Type{U}) where U
    return _mean(_mc_helper(m, a, b, class_w, no_avg, Vector))
end

@inline function _mc_helper(m::CM, a::Arr{<:Real}, b::Arr{<:Real},
                            class_w::AbstractDict{<:Any, <:Real},
                            average::MicroAvg, return_type::Type{U}) where U
    @warn W_PROMOTE_WARN
    return _mc_helper(m, a, b, class_w, macro_avg, return_type)
end

function _mtpr(m::CM, average::A, return_type::Type{U}) where {A, U}
    mtp_val, mfn_val = _mtp_vec(m), _mfn_vec(m)
    return _mc_helper(m, mtp_val, mfn_val, average, return_type)
end

function _mtpr(m::CM, class_w::AbstractDict{<:Any, <:Real}, average::A,
               return_type::Type{U}) where {A, U}
    mtp_val, mfn_val = _mtp_vec(m), _mfn_vec(m)
    return _mc_helper(m, mtp_val, mfn_val, class_w, average, return_type)
end

function _mtnr(m::CM, average::A, return_type::Type{U}) where {A, U}
    mtn_val, mfp_val = _mtn_vec(m), _mfp_vec(m)
    return _mc_helper(m, mtn_val, mfp_val, average, return_type)
end

function _mtnr(m::CM, class_w::AbstractDict{<:Any, <:Real}, average::A,
               return_type::Type{U}) where {A, U}
    mtn_val, mfp_val = _mtn_vec(m), _mfp_vec(m)
    return _mc_helper(m, mtn_val, mfp_val, class_w, average, return_type)
end

_mfpr(m::CM, average::A, return_type::Type{U}) where {A, U} =
    _mc_helper_b(m, _mtnr, average, return_type)

function _mfpr(m::CM, class_w::AbstractDict{<:Any, <:Real}, average::A,
               return_type::Type{U}) where {A, U}
    return _mc_helper_b(m, _mtnr, class_w, average, return_type)
end

_mfnr(m::CM, average::A, return_type::Type{U}) where {A, U} =
    _mc_helper_b(m, _mtpr, average, return_type)

function _mfnr(m::CM, class_w::AbstractDict{<:Any, <:Real}, average::A,
               return_type::Type{U}) where {A, U}
    return _mc_helper_b(m, _mtpr, class_w, average, return_type)
end

function _mfdr(m::CM, average::A, return_type::Type{U}) where {A, U}
    mfp_val, mtp_val = _mfp_vec(m), _mtp_vec(m)
    return _mc_helper(m, mfp_val, mtp_val, average, return_type)
end

function _mfdr(m::CM, class_w::AbstractDict{<:Any, <:Real}, average::A,
               return_type::Type{U}) where {A, U}
    mfp_val, mtp_val = _mfp_vec(m), _mtp_vec(m)
    return _mc_helper(m, mfp_val, mtp_val, class_w, average, return_type)
end

function _mnpv(m::CM, average::A, return_type::Type{U}) where {A, U}
    mtn_val, mfn_val = _mtn_vec(m), _mfn_vec(m)
    return _mc_helper(m, mtn_val, mfn_val, average, return_type)
end

function _mnpv(m::CM, class_w::AbstractDict{<:Any, <:Real}, average::A,
               return_type::Type{U}) where {A, U}
    mtn_val, mfn_val = _mtn_vec(m), _mfn_vec(m)
    return _mc_helper(m, mtn_val, mfn_val, class_w, average, return_type)
end

## CALLABLES ON MULTICLASS CONFUSION MATRIX

(p::MulticlassTruePositive)(m::CM)  = _mtp(m, p.return_type)
(n::MulticlassTrueNegative)(m::CM)  = _mtn(m, n.return_type)
(p::MulticlassFalsePositive)(m::CM) = _mfp(m, p.return_type)
(n::MulticlassFalseNegative)(m::CM) = _mfn(m, n.return_type)

(r::MTPR)(m::CM) = _mtpr(m, r.average, r.return_type)
(r::MTPR)(m::CM, w::AbstractDict{<:Any, <:Real}) =
    _mtpr(m, w, r.average, r.return_type)

(r::MTNR)(m::CM) = _mtnr(m, r.average, r.return_type)
(r::MTNR)(m::CM, w::AbstractDict{<:Any, <:Real}) =
    _mtnr(m, w, r.average, r.return_type)

(r::MFPR)(m::CM) = _mfpr(m, r.average, r.return_type)
(r::MFPR)(m::CM, w::AbstractDict{<:Any, <:Real}) =
    _mfpr(m, w, r.average, r.return_type)

(r::MFNR)(m::CM) = _mfnr(m, r.average, r.return_type)
(r::MFNR)(m::CM, w::AbstractDict{<:Any, <:Real}) =
    _mfnr(m, w, r.average, r.return_type)

(r::MFDR)(m::CM) = _mfdr(m, r.average, r.return_type)
(r::MFDR)(m::CM, w::AbstractDict{<:Any, <:Real}) =
    _mfdr(m, w, r.average, r.return_type)

(v::MNPV)(m::CM) = _mnpv(m, v.average, v.return_type)
(v::MNPV)(m::CM, w::AbstractDict{<:Any, <:Real}) =
    _mnpv(m, w, v.average, v.return_type)

(p::MulticlassPrecision)(m::CM) =
    _mc_helper_b(m, _mfdr, p.average, p.return_type)
(p::MulticlassPrecision)(m::CM, class_w::AbstractDict{<:Any, <:Real}) =
    _mc_helper_b(m, _mfdr, class_w, p.average, p.return_type)

@inline function _fs_helper(m::CM, β::Real, mtp_val::Arr{<:Real}, mfp_val::Arr{<:Real}, mfn_val::Arr{<:Real},
                    average::NoAvg, return_type::Type{LittleDict})
    β2 = β^2
    return LittleDict(m.labels, (1 + β2) * mtp_val ./ ((1 + β2) * mtp_val + β2 * mfn_val + mfp_val))
end

@inline function _fs_helper(m::CM, β::Real, mtp_val::Arr{<:Real}, mfp_val::Arr{<:Real}, mfn_val::Arr{<:Real},
                    average::NoAvg, return_type::Type{Vector})
    β2 = β^2
    return (1 + β2) * mtp_val ./ ((1 + β2) * mtp_val + β2 * mfn_val + mfp_val)
end

@inline function _fs_helper(m::CM, β::Real, mtp_val::Arr{<:Real}, mfp_val::Arr{<:Real}, mfn_val::Arr{<:Real},
                            average::MacroAvg, return_type::Type{U}) where U
    return _mean(_fs_helper(m, β, mtp_val, mfp_val, mfn_val, no_avg, Vector))
end

function (f::MulticlassFScore)(m::CM)
    f.average == micro_avg && return MulticlassRecall(; average=micro_avg, return_type=f.return_type)(m)
    mtp_val = _mtp(m, Vector)
    mfp_val = _mfp(m, Vector)
    mfn_val = _mfn(m, Vector)
    return _fs_helper(m, f.β, mtp_val, mfp_val, mfn_val, f.average, f.return_type)
end

@inline function _fs_helper(m::CM, w::AbstractDict{<:Any, <:Real}, β::Real,
                    average::NoAvg, return_type::Type{LittleDict})
    level_w = _class_w(m.labels, w)
    return LittleDict(m.labels,
                      MulticlassFScore(β=β,
                                       average=no_avg,
                                       return_type=Vector)(m) .* level_w)
end

@inline function _fs_helper(m::CM, w::AbstractDict{<:Any, <:Real}, β::Real,
                    average::NoAvg, return_type::Type{Vector})
    level_w = _class_w(m.labels, w)
    return MulticlassFScore(β=β,
                            average=no_avg,
                            return_type=Vector)(m) .* level_w
end

@inline function _fs_helper(m::CM, w::AbstractDict{<:Any, <:Real}, β::Real,
                            average::MacroAvg, return_type::Type{U}) where U
    return _mean(_fs_helper(m, w, β, no_avg, Vector))
end

@inline function _fs_helper(m::CM, w::AbstractDict{<:Any, <:Real}, β::Real,
                            average::MicroAvg, return_type::Type{U}) where U
    @warn W_PROMOTE_WARN
    return _fs_helper(m, w, β, macro_avg, return_type)
end

function (f::MulticlassFScore)(m::CM, class_w::AbstractDict{<:Any, <:Real})
    return _fs_helper(m, class_w, f.β, f.average, f.return_type)
end

## Callables on arrays

for M_ex in (:MulticlassTruePositive, :MulticlassTrueNegative,
          :MulticlassFalsePositive, :MulticlassFalseNegative)
    @eval call(m::$M_ex, ŷ, y) = m(_confmat(ŷ, y, warn=false))
end

for M_ex in (:MTPR, :MTNR, :MFPR, :MFNR, :MFDR, :MulticlassPrecision, :MNPV,
          :MulticlassFScore)
    @eval call(m::$M_ex, ŷ, y) = m(_confmat(ŷ, y, warn=false))
    @eval call(m::$M_ex, ŷ, y, class_w::AbstractDict{<:Any, <:Real}) =
        m(_confmat(ŷ, y, warn=false), class_w)
end


text:
# This file is auto-generated by AWSMetadata.jl
using AWS
using AWS.AWSServices: athena
using AWS.Compat
using AWS.UUIDs

"""
    batch_get_named_query(named_query_ids)
    batch_get_named_query(named_query_ids, params::Dict{String,<:Any})

Returns the details of a single named query or a list of up to 50 queries, which you
provide as an array of query ID strings. Requires you to have access to the workgroup in
which the queries were saved. Use ListNamedQueriesInput to get the list of named query IDs
in the specified workgroup. If information could not be retrieved for a submitted query ID,
information about the query ID submitted is listed under UnprocessedNamedQueryId. Named
queries differ from executed queries. Use BatchGetQueryExecutionInput to get details about
each unique query execution, and ListQueryExecutionsInput to get a list of query execution
IDs.

# Arguments
- `named_query_ids`: An array of query IDs.

"""
batch_get_named_query(NamedQueryIds; aws_config::AbstractAWSConfig=global_aws_config()) = athena("BatchGetNamedQuery", Dict{String, Any}("NamedQueryIds"=>NamedQueryIds); aws_config=aws_config)
batch_get_named_query(NamedQueryIds, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("BatchGetNamedQuery", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("NamedQueryIds"=>NamedQueryIds), params)); aws_config=aws_config)

"""
    batch_get_query_execution(query_execution_ids)
    batch_get_query_execution(query_execution_ids, params::Dict{String,<:Any})

Returns the details of a single query execution or a list of up to 50 query executions,
which you provide as an array of query execution ID strings. Requires you to have access to
the workgroup in which the queries ran. To get a list of query execution IDs, use
ListQueryExecutionsInputWorkGroup. Query executions differ from named (saved) queries. Use
BatchGetNamedQueryInput to get details about named queries.

# Arguments
- `query_execution_ids`: An array of query execution IDs.

"""
batch_get_query_execution(QueryExecutionIds; aws_config::AbstractAWSConfig=global_aws_config()) = athena("BatchGetQueryExecution", Dict{String, Any}("QueryExecutionIds"=>QueryExecutionIds); aws_config=aws_config)
batch_get_query_execution(QueryExecutionIds, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("BatchGetQueryExecution", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("QueryExecutionIds"=>QueryExecutionIds), params)); aws_config=aws_config)

"""
    create_data_catalog(name, type)
    create_data_catalog(name, type, params::Dict{String,<:Any})

Creates (registers) a data catalog with the specified name and properties. Catalogs created
are visible to all users of the same AWS account.

# Arguments
- `name`: The name of the data catalog to create. The catalog name must be unique for the
  AWS account and can use a maximum of 128 alphanumeric, underscore, at sign, or hyphen
  characters.
- `type`: The type of data catalog to create: LAMBDA for a federated catalog or HIVE for an
  external hive metastore.  Do not use the GLUE type. This refers to the AwsDataCatalog that
  already exists in your account, of which you can have only one. Specifying the GLUE type
  will result in an INVALID_INPUT error.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Description"`: A description of the data catalog to be created.
- `"Parameters"`: Specifies the Lambda function or functions to use for creating the data
  catalog. This is a mapping whose values depend on the catalog type.    For the HIVE data
  catalog type, use the following syntax. The metadata-function parameter is required. The
  sdk-version parameter is optional and defaults to the currently supported version.
  metadata-function=lambda_arn, sdk-version=version_number     For the LAMBDA data catalog
  type, use one of the following sets of required parameters, but not both.   If you have one
  Lambda function that processes metadata and another for reading the actual data, use the
  following syntax. Both parameters are required.  metadata-function=lambda_arn,
  record-function=lambda_arn      If you have a composite Lambda function that processes both
  metadata and data, use the following syntax to specify your Lambda function.
  function=lambda_arn
- `"Tags"`: A list of comma separated tags to add to the data catalog that is created.
"""
create_data_catalog(Name, Type; aws_config::AbstractAWSConfig=global_aws_config()) = athena("CreateDataCatalog", Dict{String, Any}("Name"=>Name, "Type"=>Type); aws_config=aws_config)
create_data_catalog(Name, Type, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("CreateDataCatalog", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Name"=>Name, "Type"=>Type), params)); aws_config=aws_config)

"""
    create_named_query(database, name, query_string)
    create_named_query(database, name, query_string, params::Dict{String,<:Any})

Creates a named query in the specified workgroup. Requires that you have access to the
workgroup. For code samples using the AWS SDK for Java, see Examples and Code Samples in
the Amazon Athena User Guide.

# Arguments
- `database`: The database to which the query belongs.
- `name`: The query name.
- `query_string`: The contents of the query with all query statements.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"ClientRequestToken"`: A unique case-sensitive string used to ensure the request to
  create the query is idempotent (executes only once). If another CreateNamedQuery request is
  received, the same response is returned and another query is not created. If a parameter
  has changed, for example, the QueryString, an error is returned.  This token is listed as
  not required because AWS SDKs (for example the AWS SDK for Java) auto-generate the token
  for users. If you are not using the AWS SDK or the AWS CLI, you must provide this token or
  the action will fail.
- `"Description"`: The query description.
- `"WorkGroup"`: The name of the workgroup in which the named query is being created.
"""
create_named_query(Database, Name, QueryString; aws_config::AbstractAWSConfig=global_aws_config()) = athena("CreateNamedQuery", Dict{String, Any}("Database"=>Database, "Name"=>Name, "QueryString"=>QueryString, "ClientRequestToken"=>string(uuid4())); aws_config=aws_config)
create_named_query(Database, Name, QueryString, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("CreateNamedQuery", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Database"=>Database, "Name"=>Name, "QueryString"=>QueryString, "ClientRequestToken"=>string(uuid4())), params)); aws_config=aws_config)

"""
    create_prepared_statement(query_statement, statement_name, work_group)
    create_prepared_statement(query_statement, statement_name, work_group, params::Dict{String,<:Any})

Creates a prepared statement for use with SQL queries in Athena.

# Arguments
- `query_statement`: The query string for the prepared statement.
- `statement_name`: The name of the prepared statement.
- `work_group`: The name of the workgroup to which the prepared statement belongs.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Description"`: The description of the prepared statement.
"""
create_prepared_statement(QueryStatement, StatementName, WorkGroup; aws_config::AbstractAWSConfig=global_aws_config()) = athena("CreatePreparedStatement", Dict{String, Any}("QueryStatement"=>QueryStatement, "StatementName"=>StatementName, "WorkGroup"=>WorkGroup); aws_config=aws_config)
create_prepared_statement(QueryStatement, StatementName, WorkGroup, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("CreatePreparedStatement", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("QueryStatement"=>QueryStatement, "StatementName"=>StatementName, "WorkGroup"=>WorkGroup), params)); aws_config=aws_config)

"""
    create_work_group(name)
    create_work_group(name, params::Dict{String,<:Any})

Creates a workgroup with the specified name.

# Arguments
- `name`: The workgroup name.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Configuration"`: The configuration for the workgroup, which includes the location in
  Amazon S3 where query results are stored, the encryption configuration, if any, used for
  encrypting query results, whether the Amazon CloudWatch Metrics are enabled for the
  workgroup, the limit for the amount of bytes scanned (cutoff) per query, if it is
  specified, and whether workgroup's settings (specified with EnforceWorkGroupConfiguration)
  in the WorkGroupConfiguration override client-side settings. See
  WorkGroupConfigurationEnforceWorkGroupConfiguration.
- `"Description"`: The workgroup description.
- `"Tags"`: A list of comma separated tags to add to the workgroup that is created.
"""
create_work_group(Name; aws_config::AbstractAWSConfig=global_aws_config()) = athena("CreateWorkGroup", Dict{String, Any}("Name"=>Name); aws_config=aws_config)
create_work_group(Name, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("CreateWorkGroup", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Name"=>Name), params)); aws_config=aws_config)

"""
    delete_data_catalog(name)
    delete_data_catalog(name, params::Dict{String,<:Any})

Deletes a data catalog.

# Arguments
- `name`: The name of the data catalog to delete.

"""
delete_data_catalog(Name; aws_config::AbstractAWSConfig=global_aws_config()) = athena("DeleteDataCatalog", Dict{String, Any}("Name"=>Name); aws_config=aws_config)
delete_data_catalog(Name, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("DeleteDataCatalog", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Name"=>Name), params)); aws_config=aws_config)

"""
    delete_named_query(named_query_id)
    delete_named_query(named_query_id, params::Dict{String,<:Any})

Deletes the named query if you have access to the workgroup in which the query was saved.
For code samples using the AWS SDK for Java, see Examples and Code Samples in the Amazon
Athena User Guide.

# Arguments
- `named_query_id`: The unique ID of the query to delete.

"""
delete_named_query(NamedQueryId; aws_config::AbstractAWSConfig=global_aws_config()) = athena("DeleteNamedQuery", Dict{String, Any}("NamedQueryId"=>NamedQueryId); aws_config=aws_config)
delete_named_query(NamedQueryId, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("DeleteNamedQuery", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("NamedQueryId"=>NamedQueryId), params)); aws_config=aws_config)

"""
    delete_prepared_statement(statement_name, work_group)
    delete_prepared_statement(statement_name, work_group, params::Dict{String,<:Any})

Deletes the prepared statement with the specified name from the specified workgroup.

# Arguments
- `statement_name`: The name of the prepared statement to delete.
- `work_group`: The workgroup to which the statement to be deleted belongs.

"""
delete_prepared_statement(StatementName, WorkGroup; aws_config::AbstractAWSConfig=global_aws_config()) = athena("DeletePreparedStatement", Dict{String, Any}("StatementName"=>StatementName, "WorkGroup"=>WorkGroup); aws_config=aws_config)
delete_prepared_statement(StatementName, WorkGroup, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("DeletePreparedStatement", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("StatementName"=>StatementName, "WorkGroup"=>WorkGroup), params)); aws_config=aws_config)

"""
    delete_work_group(work_group)
    delete_work_group(work_group, params::Dict{String,<:Any})

Deletes the workgroup with the specified name. The primary workgroup cannot be deleted.

# Arguments
- `work_group`: The unique name of the workgroup to delete.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"RecursiveDeleteOption"`: The option to delete the workgroup and its contents even if
  the workgroup contains any named queries or query executions.
"""
delete_work_group(WorkGroup; aws_config::AbstractAWSConfig=global_aws_config()) = athena("DeleteWorkGroup", Dict{String, Any}("WorkGroup"=>WorkGroup); aws_config=aws_config)
delete_work_group(WorkGroup, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("DeleteWorkGroup", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("WorkGroup"=>WorkGroup), params)); aws_config=aws_config)

"""
    get_data_catalog(name)
    get_data_catalog(name, params::Dict{String,<:Any})

Returns the specified data catalog.

# Arguments
- `name`: The name of the data catalog to return.

"""
get_data_catalog(Name; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetDataCatalog", Dict{String, Any}("Name"=>Name); aws_config=aws_config)
get_data_catalog(Name, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetDataCatalog", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Name"=>Name), params)); aws_config=aws_config)

"""
    get_database(catalog_name, database_name)
    get_database(catalog_name, database_name, params::Dict{String,<:Any})

Returns a database object for the specified database and data catalog.

# Arguments
- `catalog_name`: The name of the data catalog that contains the database to return.
- `database_name`: The name of the database to return.

"""
get_database(CatalogName, DatabaseName; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetDatabase", Dict{String, Any}("CatalogName"=>CatalogName, "DatabaseName"=>DatabaseName); aws_config=aws_config)
get_database(CatalogName, DatabaseName, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetDatabase", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("CatalogName"=>CatalogName, "DatabaseName"=>DatabaseName), params)); aws_config=aws_config)

"""
    get_named_query(named_query_id)
    get_named_query(named_query_id, params::Dict{String,<:Any})

Returns information about a single query. Requires that you have access to the workgroup in
which the query was saved.

# Arguments
- `named_query_id`: The unique ID of the query. Use ListNamedQueries to get query IDs.

"""
get_named_query(NamedQueryId; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetNamedQuery", Dict{String, Any}("NamedQueryId"=>NamedQueryId); aws_config=aws_config)
get_named_query(NamedQueryId, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetNamedQuery", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("NamedQueryId"=>NamedQueryId), params)); aws_config=aws_config)

"""
    get_prepared_statement(statement_name, work_group)
    get_prepared_statement(statement_name, work_group, params::Dict{String,<:Any})

Retrieves the prepared statement with the specified name from the specified workgroup.

# Arguments
- `statement_name`: The name of the prepared statement to retrieve.
- `work_group`: The workgroup to which the statement to be retrieved belongs.

"""
get_prepared_statement(StatementName, WorkGroup; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetPreparedStatement", Dict{String, Any}("StatementName"=>StatementName, "WorkGroup"=>WorkGroup); aws_config=aws_config)
get_prepared_statement(StatementName, WorkGroup, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetPreparedStatement", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("StatementName"=>StatementName, "WorkGroup"=>WorkGroup), params)); aws_config=aws_config)

"""
    get_query_execution(query_execution_id)
    get_query_execution(query_execution_id, params::Dict{String,<:Any})

Returns information about a single execution of a query if you have access to the workgroup
in which the query ran. Each time a query executes, information about the query execution
is saved with a unique ID.

# Arguments
- `query_execution_id`: The unique ID of the query execution.

"""
get_query_execution(QueryExecutionId; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetQueryExecution", Dict{String, Any}("QueryExecutionId"=>QueryExecutionId); aws_config=aws_config)
get_query_execution(QueryExecutionId, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetQueryExecution", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("QueryExecutionId"=>QueryExecutionId), params)); aws_config=aws_config)

"""
    get_query_results(query_execution_id)
    get_query_results(query_execution_id, params::Dict{String,<:Any})

Streams the results of a single query execution specified by QueryExecutionId from the
Athena query results location in Amazon S3. For more information, see Query Results in the
Amazon Athena User Guide. This request does not execute the query but returns results. Use
StartQueryExecution to run a query. To stream query results successfully, the IAM principal
with permission to call GetQueryResults also must have permissions to the Amazon S3
GetObject action for the Athena query results location.  IAM principals with permission to
the Amazon S3 GetObject action for the query results location are able to retrieve query
results from Amazon S3 even if permission to the GetQueryResults action is denied. To
restrict user or role access, ensure that Amazon S3 permissions to the Athena query
location are denied.

# Arguments
- `query_execution_id`: The unique ID of the query execution.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of results (rows) to return in this request.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
get_query_results(QueryExecutionId; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetQueryResults", Dict{String, Any}("QueryExecutionId"=>QueryExecutionId); aws_config=aws_config)
get_query_results(QueryExecutionId, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetQueryResults", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("QueryExecutionId"=>QueryExecutionId), params)); aws_config=aws_config)

"""
    get_table_metadata(catalog_name, database_name, table_name)
    get_table_metadata(catalog_name, database_name, table_name, params::Dict{String,<:Any})

Returns table metadata for the specified catalog, database, and table.

# Arguments
- `catalog_name`: The name of the data catalog that contains the database and table
  metadata to return.
- `database_name`: The name of the database that contains the table metadata to return.
- `table_name`: The name of the table for which metadata is returned.

"""
get_table_metadata(CatalogName, DatabaseName, TableName; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetTableMetadata", Dict{String, Any}("CatalogName"=>CatalogName, "DatabaseName"=>DatabaseName, "TableName"=>TableName); aws_config=aws_config)
get_table_metadata(CatalogName, DatabaseName, TableName, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetTableMetadata", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("CatalogName"=>CatalogName, "DatabaseName"=>DatabaseName, "TableName"=>TableName), params)); aws_config=aws_config)

"""
    get_work_group(work_group)
    get_work_group(work_group, params::Dict{String,<:Any})

Returns information about the workgroup with the specified name.

# Arguments
- `work_group`: The name of the workgroup.

"""
get_work_group(WorkGroup; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetWorkGroup", Dict{String, Any}("WorkGroup"=>WorkGroup); aws_config=aws_config)
get_work_group(WorkGroup, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("GetWorkGroup", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("WorkGroup"=>WorkGroup), params)); aws_config=aws_config)

"""
    list_data_catalogs()
    list_data_catalogs(params::Dict{String,<:Any})

Lists the data catalogs in the current AWS account.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: Specifies the maximum number of data catalogs to return.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
list_data_catalogs(; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListDataCatalogs"; aws_config=aws_config)
list_data_catalogs(params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListDataCatalogs", params; aws_config=aws_config)

"""
    list_databases(catalog_name)
    list_databases(catalog_name, params::Dict{String,<:Any})

Lists the databases in the specified data catalog.

# Arguments
- `catalog_name`: The name of the data catalog that contains the databases to return.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: Specifies the maximum number of results to return.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
list_databases(CatalogName; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListDatabases", Dict{String, Any}("CatalogName"=>CatalogName); aws_config=aws_config)
list_databases(CatalogName, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListDatabases", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("CatalogName"=>CatalogName), params)); aws_config=aws_config)

"""
    list_engine_versions()
    list_engine_versions(params::Dict{String,<:Any})

Returns a list of engine versions that are available to choose from, including the Auto
option.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of engine versions to return in this request.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
list_engine_versions(; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListEngineVersions"; aws_config=aws_config)
list_engine_versions(params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListEngineVersions", params; aws_config=aws_config)

"""
    list_named_queries()
    list_named_queries(params::Dict{String,<:Any})

Provides a list of available query IDs only for queries saved in the specified workgroup.
Requires that you have access to the specified workgroup. If a workgroup is not specified,
lists the saved queries for the primary workgroup. For code samples using the AWS SDK for
Java, see Examples and Code Samples in the Amazon Athena User Guide.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of queries to return in this request.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
- `"WorkGroup"`: The name of the workgroup from which the named queries are being returned.
  If a workgroup is not specified, the saved queries for the primary workgroup are returned.
"""
list_named_queries(; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListNamedQueries"; aws_config=aws_config)
list_named_queries(params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListNamedQueries", params; aws_config=aws_config)

"""
    list_prepared_statements(work_group)
    list_prepared_statements(work_group, params::Dict{String,<:Any})

Lists the prepared statements in the specfied workgroup.

# Arguments
- `work_group`: The workgroup to list the prepared statements for.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of results to return in this request.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
list_prepared_statements(WorkGroup; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListPreparedStatements", Dict{String, Any}("WorkGroup"=>WorkGroup); aws_config=aws_config)
list_prepared_statements(WorkGroup, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListPreparedStatements", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("WorkGroup"=>WorkGroup), params)); aws_config=aws_config)

"""
    list_query_executions()
    list_query_executions(params::Dict{String,<:Any})

Provides a list of available query execution IDs for the queries in the specified
workgroup. If a workgroup is not specified, returns a list of query execution IDs for the
primary workgroup. Requires you to have access to the workgroup in which the queries ran.
For code samples using the AWS SDK for Java, see Examples and Code Samples in the Amazon
Athena User Guide.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of query executions to return in this request.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
- `"WorkGroup"`: The name of the workgroup from which queries are being returned. If a
  workgroup is not specified, a list of available query execution IDs for the queries in the
  primary workgroup is returned.
"""
list_query_executions(; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListQueryExecutions"; aws_config=aws_config)
list_query_executions(params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListQueryExecutions", params; aws_config=aws_config)

"""
    list_table_metadata(catalog_name, database_name)
    list_table_metadata(catalog_name, database_name, params::Dict{String,<:Any})

Lists the metadata for the tables in the specified data catalog database.

# Arguments
- `catalog_name`: The name of the data catalog for which table metadata should be returned.
- `database_name`: The name of the database for which table metadata should be returned.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Expression"`: A regex filter that pattern-matches table names. If no expression is
  supplied, metadata for all tables are listed.
- `"MaxResults"`: Specifies the maximum number of results to return.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
list_table_metadata(CatalogName, DatabaseName; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListTableMetadata", Dict{String, Any}("CatalogName"=>CatalogName, "DatabaseName"=>DatabaseName); aws_config=aws_config)
list_table_metadata(CatalogName, DatabaseName, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListTableMetadata", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("CatalogName"=>CatalogName, "DatabaseName"=>DatabaseName), params)); aws_config=aws_config)

"""
    list_tags_for_resource(resource_arn)
    list_tags_for_resource(resource_arn, params::Dict{String,<:Any})

Lists the tags associated with an Athena workgroup or data catalog resource.

# Arguments
- `resource_arn`: Lists the tags for the resource with the specified ARN.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of results to be returned per request that lists the
  tags for the resource.
- `"NextToken"`: The token for the next set of results, or null if there are no additional
  results for this request, where the request lists the tags for the resource with the
  specified ARN.
"""
list_tags_for_resource(ResourceARN; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListTagsForResource", Dict{String, Any}("ResourceARN"=>ResourceARN); aws_config=aws_config)
list_tags_for_resource(ResourceARN, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListTagsForResource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ResourceARN"=>ResourceARN), params)); aws_config=aws_config)

"""
    list_work_groups()
    list_work_groups(params::Dict{String,<:Any})

Lists available workgroups for the account.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"MaxResults"`: The maximum number of workgroups to return in this request.
- `"NextToken"`: A token generated by the Athena service that specifies where to continue
  pagination if a previous request was truncated. To obtain the next set of pages, pass in
  the NextToken from the response object of the previous page call.
"""
list_work_groups(; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListWorkGroups"; aws_config=aws_config)
list_work_groups(params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("ListWorkGroups", params; aws_config=aws_config)

"""
    start_query_execution(query_string)
    start_query_execution(query_string, params::Dict{String,<:Any})

Runs the SQL query statements contained in the Query. Requires you to have access to the
workgroup in which the query ran. Running queries against an external catalog requires
GetDataCatalog permission to the catalog. For code samples using the AWS SDK for Java, see
Examples and Code Samples in the Amazon Athena User Guide.

# Arguments
- `query_string`: The SQL query statements to be executed.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"ClientRequestToken"`: A unique case-sensitive string used to ensure the request to
  create the query is idempotent (executes only once). If another StartQueryExecution request
  is received, the same response is returned and another query is not created. If a parameter
  has changed, for example, the QueryString, an error is returned.  This token is listed as
  not required because AWS SDKs (for example the AWS SDK for Java) auto-generate the token
  for users. If you are not using the AWS SDK or the AWS CLI, you must provide this token or
  the action will fail.
- `"QueryExecutionContext"`: The database within which the query executes.
- `"ResultConfiguration"`: Specifies information about where and how to save the results of
  the query execution. If the query runs in a workgroup, then workgroup's settings may
  override query settings. This affects the query results location. The workgroup settings
  override is specified in EnforceWorkGroupConfiguration (true/false) in the
  WorkGroupConfiguration. See WorkGroupConfigurationEnforceWorkGroupConfiguration.
- `"WorkGroup"`: The name of the workgroup in which the query is being started.
"""
start_query_execution(QueryString; aws_config::AbstractAWSConfig=global_aws_config()) = athena("StartQueryExecution", Dict{String, Any}("QueryString"=>QueryString, "ClientRequestToken"=>string(uuid4())); aws_config=aws_config)
start_query_execution(QueryString, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("StartQueryExecution", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("QueryString"=>QueryString, "ClientRequestToken"=>string(uuid4())), params)); aws_config=aws_config)

"""
    stop_query_execution(query_execution_id)
    stop_query_execution(query_execution_id, params::Dict{String,<:Any})

Stops a query execution. Requires you to have access to the workgroup in which the query
ran. For code samples using the AWS SDK for Java, see Examples and Code Samples in the
Amazon Athena User Guide.

# Arguments
- `query_execution_id`: The unique ID of the query execution to stop.

"""
stop_query_execution(QueryExecutionId; aws_config::AbstractAWSConfig=global_aws_config()) = athena("StopQueryExecution", Dict{String, Any}("QueryExecutionId"=>QueryExecutionId); aws_config=aws_config)
stop_query_execution(QueryExecutionId, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("StopQueryExecution", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("QueryExecutionId"=>QueryExecutionId), params)); aws_config=aws_config)

"""
    tag_resource(resource_arn, tags)
    tag_resource(resource_arn, tags, params::Dict{String,<:Any})

Adds one or more tags to an Athena resource. A tag is a label that you assign to a
resource. In Athena, a resource can be a workgroup or data catalog. Each tag consists of a
key and an optional value, both of which you define. For example, you can use tags to
categorize Athena workgroups or data catalogs by purpose, owner, or environment. Use a
consistent set of tag keys to make it easier to search and filter workgroups or data
catalogs in your account. For best practices, see Tagging Best Practices. Tag keys can be
from 1 to 128 UTF-8 Unicode characters, and tag values can be from 0 to 256 UTF-8 Unicode
characters. Tags can use letters and numbers representable in UTF-8, and the following
characters: + - = . _ : / @. Tag keys and values are case-sensitive. Tag keys must be
unique per resource. If you specify more than one tag, separate them by commas.

# Arguments
- `resource_arn`: Specifies the ARN of the Athena resource (workgroup or data catalog) to
  which tags are to be added.
- `tags`: A collection of one or more tags, separated by commas, to be added to an Athena
  workgroup or data catalog resource.

"""
tag_resource(ResourceARN, Tags; aws_config::AbstractAWSConfig=global_aws_config()) = athena("TagResource", Dict{String, Any}("ResourceARN"=>ResourceARN, "Tags"=>Tags); aws_config=aws_config)
tag_resource(ResourceARN, Tags, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("TagResource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ResourceARN"=>ResourceARN, "Tags"=>Tags), params)); aws_config=aws_config)

"""
    untag_resource(resource_arn, tag_keys)
    untag_resource(resource_arn, tag_keys, params::Dict{String,<:Any})

Removes one or more tags from a data catalog or workgroup resource.

# Arguments
- `resource_arn`: Specifies the ARN of the resource from which tags are to be removed.
- `tag_keys`: A comma-separated list of one or more tag keys whose tags are to be removed
  from the specified resource.

"""
untag_resource(ResourceARN, TagKeys; aws_config::AbstractAWSConfig=global_aws_config()) = athena("UntagResource", Dict{String, Any}("ResourceARN"=>ResourceARN, "TagKeys"=>TagKeys); aws_config=aws_config)
untag_resource(ResourceARN, TagKeys, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("UntagResource", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("ResourceARN"=>ResourceARN, "TagKeys"=>TagKeys), params)); aws_config=aws_config)

"""
    update_data_catalog(name, type)
    update_data_catalog(name, type, params::Dict{String,<:Any})

Updates the data catalog that has the specified name.

# Arguments
- `name`: The name of the data catalog to update. The catalog name must be unique for the
  AWS account and can use a maximum of 128 alphanumeric, underscore, at sign, or hyphen
  characters.
- `type`: Specifies the type of data catalog to update. Specify LAMBDA for a federated
  catalog or HIVE for an external hive metastore.  Do not use the GLUE type. This refers to
  the AwsDataCatalog that already exists in your account, of which you can have only one.
  Specifying the GLUE type will result in an INVALID_INPUT error.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Description"`: New or modified text that describes the data catalog.
- `"Parameters"`: Specifies the Lambda function or functions to use for updating the data
  catalog. This is a mapping whose values depend on the catalog type.    For the HIVE data
  catalog type, use the following syntax. The metadata-function parameter is required. The
  sdk-version parameter is optional and defaults to the currently supported version.
  metadata-function=lambda_arn, sdk-version=version_number     For the LAMBDA data catalog
  type, use one of the following sets of required parameters, but not both.   If you have one
  Lambda function that processes metadata and another for reading the actual data, use the
  following syntax. Both parameters are required.  metadata-function=lambda_arn,
  record-function=lambda_arn      If you have a composite Lambda function that processes both
  metadata and data, use the following syntax to specify your Lambda function.
  function=lambda_arn
"""
update_data_catalog(Name, Type; aws_config::AbstractAWSConfig=global_aws_config()) = athena("UpdateDataCatalog", Dict{String, Any}("Name"=>Name, "Type"=>Type); aws_config=aws_config)
update_data_catalog(Name, Type, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("UpdateDataCatalog", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("Name"=>Name, "Type"=>Type), params)); aws_config=aws_config)

"""
    update_prepared_statement(query_statement, statement_name, work_group)
    update_prepared_statement(query_statement, statement_name, work_group, params::Dict{String,<:Any})

Updates a prepared statement.

# Arguments
- `query_statement`: The query string for the prepared statement.
- `statement_name`: The name of the prepared statement.
- `work_group`: The workgroup for the prepared statement.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"Description"`: The description of the prepared statement.
"""
update_prepared_statement(QueryStatement, StatementName, WorkGroup; aws_config::AbstractAWSConfig=global_aws_config()) = athena("UpdatePreparedStatement", Dict{String, Any}("QueryStatement"=>QueryStatement, "StatementName"=>StatementName, "WorkGroup"=>WorkGroup); aws_config=aws_config)
update_prepared_statement(QueryStatement, StatementName, WorkGroup, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("UpdatePreparedStatement", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("QueryStatement"=>QueryStatement, "StatementName"=>StatementName, "WorkGroup"=>WorkGroup), params)); aws_config=aws_config)

"""
    update_work_group(work_group)
    update_work_group(work_group, params::Dict{String,<:Any})

Updates the workgroup with the specified name. The workgroup's name cannot be changed.

# Arguments
- `work_group`: The specified workgroup that will be updated.

# Optional Parameters
Optional parameters can be passed as a `params::Dict{String,<:Any}`. Valid keys are:
- `"ConfigurationUpdates"`: The workgroup configuration that will be updated for the given
  workgroup.
- `"Description"`: The workgroup description.
- `"State"`: The workgroup state that will be updated for the given workgroup.
"""
update_work_group(WorkGroup; aws_config::AbstractAWSConfig=global_aws_config()) = athena("UpdateWorkGroup", Dict{String, Any}("WorkGroup"=>WorkGroup); aws_config=aws_config)
update_work_group(WorkGroup, params::AbstractDict{String, <:Any}; aws_config::AbstractAWSConfig=global_aws_config()) = athena("UpdateWorkGroup", Dict{String, Any}(mergewith(_merge, Dict{String, Any}("WorkGroup"=>WorkGroup), params)); aws_config=aws_config)

ext: jl


text:
A = Variable("WildA", Sets)
B = Variable("WildB", Sets)
f = FuncVariable("Wildf", Statements, Statements)

P = Variable("WildP", Statements)
Q = Variable("WildQ", Statements)
R = Variable("WildR", Statements)

x = Variable("Wildx", Ω)
y = Variable("Wildy", Ω)
z = Variable("Wildz", Ω)

abstract type ATruth end

struct Axiom <: ATruth  # its id is the position in the truth table
    name::String
    statement::Term
end
struct Definition <: ATruth
    name::String
    statement::Term
end

struct Proof
    lines::Array{Tuple{Term,Int64,Array{Int64}}}
end
lines(x::Proof) = x.lines
steps(x::Proof) = [line[1] for line in x.lines]
function english(x::Proof)
    result = ""
    for line in lines(x)
        result *= "$(line[1]) because $(name(truths[line[2]]))\n"
    end
    return result
end
Base.hash(x::Proof, h::UInt) = hash(steps(x)[end], h)
Base.:(==)(x::Proof, y::Proof) = steps(x)[end] == steps(y)[end]
extensions(x::Proof, truth_table=truths) = [Proof(push!(copy(lines(x)), re)) for re in rewrites_wp(steps(x)[end], Int64[], truth_table)]

struct Theorem <: ATruth
    name::String
    statement::Term
    proof::Proof
end
name(x::ATruth) = x.name

truths = ATruth[
    Axiom("Given", P ⩶ P),
]

include("Properties.jl")

include("Logic.jl")
include("SetTheory.jl")


text:
@testset "match" begin
    @testset "literal match" begin
        simple_match(x) = @match x begin
            1  => "wrong!"
            2  => "wrong!"
            10 => "right!"
            _  => "None" end
        @test simple_match(10) == "right!"
        @test simple_match(1) == "wrong!"
        @test simple_match(0) == "None"
    end
    @testset "capture match with guard" begin
        capture_match(x) = @match x begin
            x && if x > 0 end => x + 1
            x && if x < 0 end => x - 1
            _ => 0
        end
        @test_skip capture_match(0) == 0
        @test_skip capture_match(1) == 2
        @test_skip capture_match(-1) == -2
    end
    @testset "type match" begin
        type_match(x) = @match x begin
            ::Float64  => nothing
            b :: Int   => b
            _          => nothing
        end
        @test type_match(3.0) == nothing
        @test type_match(3) == 3
        @test type_match("3") == nothing
    end
    @testset "as match" begin
        as_match(x) = @match x begin
            (a, b) && c => c[1] == a && c[2] == b
        end
        @test as_match((:a, :b)) == true
        @test as_match((1, 2)) == true
    end
    @testset "guard match" begin
        guard_match(x) = @match x begin
            x && if x > 5 end => 5 - x # only succeed when x > 5
            _        => 1
        end
        @test_skip guard_match(0) == 1
        @test_skip guard_match(10) == -5
    end
    # Personally I think range match is not that useful
    # since we already have guard
    # A: not only for range, it's enumerable..

    @testset "range match" begin
        range_match(x) = @match x begin
            1:10  && x => "$x in [1, 10]"
            11:20 && x => "$x in [11, 20]"
            21:30 && x => "$x in [21, 30]"
        end
        @test range_match(3) == "3 in [1, 10]"
        @test range_match(13) == "13 in [11, 20]"
        @test range_match(23) == "23 in [21, 30]"
    end

    @testset "reference match" begin
        c = "abc"
        ref_match(x,y) = @match (x, y) begin
            (&c, _)  => "x equals to c!"
            (_,  &c) => "y equals to c!"
            _        => "none of x and y equal to c"
        end
        @test ref_match("abc", "def") == "x equals to c!"
        @test ref_match("def", "abc") == "y equals to c!"
        @test ref_match(0, 0) == "none of x and y equal to c"
    end
    # TODO: custom pattern is not fully understood yet...
    @testset "dict match" begin
        dict_match(dict) = @match dict begin
            Dict("3" => four::Int,
                 5   => Dict(6 => sev)) && if four < sev end => sev
        end
        @test dict_match(Dict(1 => 2, "3" => 4, 5 => Dict(6 => 7))) == 7
    end
    @testset "tuple match" begin
        @test (1, 2, 3, 4) == @match (1, 2, (3, 4, (5, ))) begin
            (a, b, (c, d, (5, ))) => (a, b, c, d)
        end
    end
    @testset "array match" begin
        @test ([2, 3], 4) == @match [1, 2, 3, 4] begin
            [1, pack..., a] => (pack, a)
            end
        # @test ([3, 2], 4) == @match [1 2; 3 4] begin
        #     [1, pack..., a] => (pack, a)
        #     end
        @test ([2, 3], 4) == @match [1, 2, 3, 4] begin
            [1, pack..., a] => (pack, a)
            end
    end
    @testset "AST match" begin
        function ast_match(a)
            @match a begin
            :x       => 0
            :(x + 1) => 1
            :(x + 2) => 2
            _        => 3
            end
        end
        @test ast_match(:(x + 1)) === 1
        @test ast_match(:(x + 2)) === 2
        @test ast_match(:x)       === 0
        @test ast_match(:(x + 5)) === 3
    end

end


text:
@compile reference_dummy """
__global__ void reference_dummy()
{
}
"""

@target ptx function kernel_dummy()
    return nothing
end


text:
# ---
# title: 725. Split Linked List in Parts
# id: problem725
# author: Indigo
# date: 2021-09-22
# difficulty: Medium
# categories: Linked List
# link: <https://leetcode.com/problems/split-linked-list-in-parts/description/>
# hidden: true
# ---
#
# Given a (singly) linked list with head node `root`, write a function to split
# the linked list into `k` consecutive linked list "parts".
#
# The length of each part should be as equal as possible: no two parts should
# have a size differing by more than 1. This may lead to some parts being null.
#
# The parts should be in order of occurrence in the input list, and parts
# occurring earlier should always have a size greater than or equal parts
# occurring later.
#
# Return a List of ListNode's representing the linked list parts that are
# formed.
#
# Examples 1->2->3->4, k = 5 // 5 equal parts [ [1], [2], [3], [4], null ]
#
# **Example 1:**
#
#
#
#     Input:
#     root = [1, 2, 3], k = 5
#     Output: [[1],[2],[3],[],[]]
#     Explanation:
#     The input and each element of the output are ListNodes, not arrays.
#     For example, the input root has root.val = 1, root.next.val = 2, \root.next.next.val = 3, and root.next.next.next = null.
#     The first element output[0] has output[0].val = 1, output[0].next = null.
#     The last element output[4] is null, but it's string representation as a ListNode is [].
#
#
# **Example 2:**
#
#
#
#     Input:
#     root = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10], k = 3
#     Output: [[1, 2, 3, 4], [5, 6, 7], [8, 9, 10]]
#     Explanation:
#     The input has been split into consecutive parts with size difference at most 1, and earlier parts are a larger size than the later parts.
#
#
# **Note:**
#
# * The length of `root` will be in the range `[0, 1000]`.
# * Each value of a node in the input will be an integer in the range `[0, 999]`.
# * `k` will be an integer in the range `[1, 50]`.
#
#
## @lc code=start
using LeetCode

split_list_to_parts(::Nothing, k::Int)::Vector{Union{ListNode{Int},Nothing}} =
    fill(nothing, k)

function split_list_to_parts(
    head::ListNode{Int}, k::Int
)::Vector{Union{ListNode{Int},Nothing}}
    p = head
    len = length_of_list(head)
    first_m = mod(len, k)
    base_n = len ÷ k
    res = Vector{Union{ListNode{Int},Nothing}}(undef, k)
    for i in 1:k
        for _ in 1:(base_n - (i > first_m))
            p = p.next
        end
        res[i] = head
        if !isnothing(p)
            head = p.next
            p.next = nothing
        end
        p = head
    end
    return res
end
## @lc code=end


text:
using BladeRF
d = open()
# enable( d, TX )
# disable( d, TX )
config( d, RX )
enable( d, RX )
samplerate( d, RX )
# data = receive( d, 1_000_000 )
# disable( d, RX )
# close( d )