QuackDB.Ecto.Analytics (quackdb v0.7.0)

Copy Markdown View Source

DuckDB analytical expression helpers for Ecto queries.

These macros wrap DuckDB-specific SQL functions in Ecto.Query.API.fragment/1 so analytical queries can stay in Ecto's query DSL instead of scattering raw fragment strings across application code.

import Ecto.Query
import QuackDB.Ecto.Analytics

from(event in "events",
  group_by: event.category,
  select: %{
    category: event.category,
    median_score: median(event.score),
    p95_score: quantile_cont(event.score, 0.95),
    scores: list(event.score)
  }
)

Helpers only build expressions. DuckDB clauses that are not representable in Ecto's AST, such as PIVOT, UNPIVOT, QUALIFY, and GROUPING SETS, should still be sent as raw SQL.

Summary

Functions

Builds json_insert(json, path, value), which only adds a missing value.

Builds json_remove(json, path).

Builds json_replace(json, path, value), which only changes an existing value.

Builds DuckDB 2.0's json_set(json, path, value), which creates or replaces the value at path. The value is converted with to_json, so Elixir strings set JSON strings. Paths are JSON path strings or lists such as [:address, :city].

Runs DuckDB SUMMARIZE for an Ecto queryable using the :all query operation.

Runs DuckDB SUMMARIZE for an Ecto queryable using the given Ecto SQL operation.

Runs DuckDB SUMMARIZE for an Ecto queryable and raises on error.

Runs DuckDB SUMMARIZE for an Ecto queryable using the given Ecto SQL operation and raises on error.

Reads a DuckDB variable set with SET VARIABLE, using DuckDB 2.0's $name shorthand. Pass a pinned value to read a variable named at runtime through getvariable/1.

Functions

any_value(arg1)

(macro)

approx_count_distinct(arg1)

(macro)

approx_quantile(arg1, arg2)

(macro)

approx_top_k(arg1, arg2)

(macro)

arg_max(arg1, arg2)

(macro)

arg_max(arg1, arg2, arg3)

(macro)

arg_min(arg1, arg2)

(macro)

arg_min(arg1, arg2, arg3)

(macro)

band(arg1)

(macro)

bitstring_agg(arg1)

(macro)

bitstring_agg(arg1, arg2, arg3)

(macro)

bool_and(arg1)

(macro)

bool_or(arg1)

(macro)

bor(arg1)

(macro)

bxor(arg1)

(macro)

corr(arg1, arg2)

(macro)

covar_pop(arg1, arg2)

(macro)

covar_samp(arg1, arg2)

(macro)

date_part(part, timestamp)

(macro)

date_trunc(part, timestamp)

(macro)

entropy(arg1)

(macro)

equi_width_bins(min, max, bin_count, nice \\ true)

(macro)

favg(arg1)

(macro)

fsum(arg1)

(macro)

geometric_mean(arg1)

(macro)

histogram(arg1)

(macro)

histogram_exact(arg1, arg2)

(macro)

json_contains(expression, value)

(macro)

json_exists(expression, path)

(macro)

json_extract(expression, path)

(macro)

json_extract_string(expression, path)

(macro)

json_insert(expression, path, value)

(macro)

Builds json_insert(json, path, value), which only adds a missing value.

json_remove(expression, path)

(macro)

Builds json_remove(json, path).

json_replace(expression, path, value)

(macro)

Builds json_replace(json, path, value), which only changes an existing value.

json_set(expression, path, value)

(macro)

Builds DuckDB 2.0's json_set(json, path, value), which creates or replaces the value at path. The value is converted with to_json, so Elixir strings set JSON strings. Paths are JSON path strings or lists such as [:address, :city].

kurtosis(arg1)

(macro)

kurtosis_pop(arg1)

(macro)

list(expression)

(macro)

list(expression, options)

(macro)

mad(arg1)

(macro)

median(arg1)

(macro)

mode(arg1)

(macro)

product(arg1)

(macro)

quantile_cont(arg1, arg2)

(macro)

quantile_disc(arg1, arg2)

(macro)

regr_count(arg1, arg2)

(macro)

regr_intercept(arg1, arg2)

(macro)

regr_r2(arg1, arg2)

(macro)

regr_slope(arg1, arg2)

(macro)

regr_sxx(arg1, arg2)

(macro)

regr_sxy(arg1, arg2)

(macro)

regr_syy(arg1, arg2)

(macro)

reservoir_quantile(arg1, arg2)

(macro)

reservoir_quantile(arg1, arg2, arg3)

(macro)

sem(arg1)

(macro)

skewness(arg1)

(macro)

stddev(arg1)

(macro)

stddev_pop(arg1)

(macro)

string_agg(expression, separator)

(macro)

string_agg(expression, separator, options)

(macro)

summarize(repo, queryable)

Runs DuckDB SUMMARIZE for an Ecto queryable using the :all query operation.

summarize(repo, queryable, options)

Runs DuckDB SUMMARIZE for an Ecto queryable using the given Ecto SQL operation.

summarize(repo, operation, queryable, options)

summarize!(repo, queryable)

Runs DuckDB SUMMARIZE for an Ecto queryable and raises on error.

summarize!(repo, queryable, options)

Runs DuckDB SUMMARIZE for an Ecto queryable using the given Ecto SQL operation and raises on error.

summarize!(repo, operation, queryable, options)

time_bucket(interval, timestamp)

(macro)

time_bucket(interval, timestamp, options)

(macro)

var_pop(arg1)

(macro)

var_samp(arg1)

(macro)

variable(name)

(macro)

Reads a DuckDB variable set with SET VARIABLE, using DuckDB 2.0's $name shorthand. Pass a pinned value to read a variable named at runtime through getvariable/1.

variance(arg1)

(macro)

weighted_avg(arg1, arg2)

(macro)