Liking cljdoc? Tell your friends :D

datahike.pg.sql.fns

SQL → Clojure function wrappers.

Every named function the translator can emit at query time lives here, behind a stable (fully-qualified) symbol that Datahike's resolve-fn looks up at execute time:

  • Aggregate wrappers (filter-sum / filter-avg / …) — variants of the core Datahike aggregates that skip the :__null__ sentinel and treat nil as SQL NULL. Needed because core's sum/avg throw on keyword arithmetic and min/max mis-order keyword vs number.

  • null-safe — wraps a scalar fn so either-operand-is-NULL returns NULL. Used to adapt Clojure string/math fns (upper, length, arithmetic) to SQL 3-valued logic.

  • SQL string fns (sql-lpad, sql-position, …) — thin wrappers with standard SQL semantics (1-based positions, padding behavior).

  • PG-catalog-function stubs (pg-table-is-visible, pg-format-type, pg-get-expr) — minimum viable to make JDBC drivers and ORMs that probe the catalog not trip over missing functions.

  • Lookup tables:

    • sql-aggregate->datalog — SQL agg name → fully-qualified symbol. Emitted symbol resolves via datahike.pg.sql/filter-* which re-exports from this ns.
    • sql-fn->clj-fn — SQL fn name → actual IFn value. Used by translate-function-call at emit time to wrap in null-safe.

Runtime symbols re-exported from datahike.pg.sql so the translator's emitted forms keep the old qualified path ('datahike.pg.sql/filter-count, 'datahike.pg.sql/sql-+, …) for backward compat with already-prepared statements cached on clients.

SQL → Clojure function wrappers.

Every named function the translator can emit at query time lives
here, behind a stable (fully-qualified) symbol that Datahike's
`resolve-fn` looks up at execute time:

- Aggregate wrappers (`filter-sum` / `filter-avg` / …) — variants of
  the core Datahike aggregates that skip the `:__null__` sentinel and
  treat nil as SQL NULL. Needed because core's `sum`/`avg` throw on
  keyword arithmetic and `min`/`max` mis-order keyword vs number.

- `null-safe` — wraps a scalar fn so either-operand-is-NULL returns
  NULL. Used to adapt Clojure string/math fns (upper, length,
  arithmetic) to SQL 3-valued logic.

- SQL string fns (`sql-lpad`, `sql-position`, …) — thin wrappers
  with standard SQL semantics (1-based positions, padding behavior).

- PG-catalog-function stubs (`pg-table-is-visible`, `pg-format-type`,
  `pg-get-expr`) — minimum viable to make JDBC drivers and ORMs
  that probe the catalog not trip over missing functions.

- Lookup tables:
    * `sql-aggregate->datalog` — SQL agg name → fully-qualified
      symbol. Emitted symbol resolves via
      `datahike.pg.sql/filter-*` which re-exports from this ns.
    * `sql-fn->clj-fn` — SQL fn name → actual IFn value. Used by
      `translate-function-call` at emit time to wrap in `null-safe`.

Runtime symbols re-exported from `datahike.pg.sql` so the
translator's emitted forms keep the old qualified path
(`'datahike.pg.sql/filter-count`, `'datahike.pg.sql/sql-+`, …)
for backward compat with already-prepared statements cached on
clients.
raw docstring

aggregate-function?clj

(aggregate-function? fname)

filter-avgclj

(filter-avg coll)

AVG that ignores :null sentinel values. Returns :null if all filtered.

AVG that ignores :__null__ sentinel values. Returns :__null__ if all filtered.
raw docstring

filter-corrclj

(filter-corr pairs)

SQL CORR(y, x) — Pearson correlation. Input is a collection of [x y] pairs (as produced by translate-select for two-arg CORR). Pairs where either element is :null/nil are dropped. Returns :null when fewer than 2 pairs remain or the denominator is zero (one side is a constant).

SQL CORR(y, x) — Pearson correlation. Input is a collection of [x y]
pairs (as produced by translate-select for two-arg CORR). Pairs where
either element is :__null__/nil are dropped. Returns :__null__ when
fewer than 2 pairs remain or the denominator is zero (one side is a
constant).
raw docstring

filter-countclj

(filter-count coll)

SQL COUNT(col) — counts non-NULL values. Unlike COUNT(*), which counts all rows, COUNT(col) skips rows where col IS NULL. Returns a long (never NULL — COUNT of empty is 0).

SQL COUNT(col) — counts non-NULL values. Unlike COUNT(*), which counts
all rows, COUNT(col) skips rows where col IS NULL. Returns a long
(never NULL — COUNT of empty is 0).
raw docstring

filter-count-distinctclj

(filter-count-distinct coll)

SQL COUNT(DISTINCT col) — counts distinct non-NULL values.

SQL COUNT(DISTINCT col) — counts distinct non-NULL values.
raw docstring

filter-maxclj

(filter-max coll)

MAX that ignores :null sentinel values. Returns :null if all filtered.

MAX that ignores :__null__ sentinel values. Returns :__null__ if all filtered.
raw docstring

filter-minclj

(filter-min coll)

MIN that ignores :null sentinel values. Returns :null if all filtered.

MIN that ignores :__null__ sentinel values. Returns :__null__ if all filtered.
raw docstring

filter-stddev-sampclj

(filter-stddev-samp coll)

SQL STDDEV_SAMP(x) — sample standard deviation. See filter-variance-samp.

SQL STDDEV_SAMP(x) — sample standard deviation. See filter-variance-samp.
raw docstring

filter-sumclj

(filter-sum coll)

SUM that ignores :null sentinel values. Returns :null if all filtered.

SUM that ignores :__null__ sentinel values. Returns :__null__ if all filtered.
raw docstring

filter-variance-sampclj

(filter-variance-samp coll)

SQL VAR_SAMP(x) — sample variance, ignores :null/nil. Returns :null when fewer than 2 non-null values remain (matches PG).

SQL VAR_SAMP(x) — sample variance, ignores :__null__/nil. Returns
:__null__ when fewer than 2 non-null values remain (matches PG).
raw docstring

null-safeclj

(null-safe f)

Wrap a scalar function f to propagate SQL NULL: if any argument is the :__null__ sentinel (or Clojure nil), return :__null__ without calling f. Otherwise apply f to the arguments.

Used to make scalar SQL functions (upper, lower, substring, length, abs, arithmetic, ...) match SQL semantics: UPPER(NULL) = NULL, LENGTH(NULL) = NULL, 1 + NULL = NULL. Without this guard, Clojure string/numeric functions throw on :__null__ (a Keyword, not a String/Number).

Wrap a scalar function `f` to propagate SQL NULL: if any argument is the
`:__null__` sentinel (or Clojure nil), return `:__null__` without calling
`f`. Otherwise apply `f` to the arguments.

Used to make scalar SQL functions (upper, lower, substring, length, abs,
arithmetic, ...) match SQL semantics: `UPPER(NULL) = NULL`, `LENGTH(NULL)
= NULL`, `1 + NULL = NULL`. Without this guard, Clojure string/numeric
functions throw on `:__null__` (a Keyword, not a String/Number).
raw docstring

pg-format-typeclj

(pg-format-type type-oid _typmod)

Convert a type OID + type modifier to a type name string. Delegates to the centralized type registry.

Convert a type OID + type modifier to a type name string.
Delegates to the centralized type registry.
raw docstring

pg-get-exprclj

(pg-get-expr expr-text _relation-oid)

Return the expression text as-is (CockroachDB's approach).

Return the expression text as-is (CockroachDB's approach).
raw docstring

pg-table-is-visibleclj

(pg-table-is-visible _oid)

Stub: always returns true (all tables are visible in the default schema).

Stub: always returns true (all tables are visible in the default schema).
raw docstring

sql-*clj


sql-+clj


sql--clj


sql-aggregate->datalogclj

Map SQL aggregate function names (lowercased) to Datalog aggregate symbols. SUM/AVG/MIN/MAX route to our filter-* variants so they skip the :__null__ sentinel that col-var! emits for missing attribute values — Datahike's raw sum/avg would throw trying (+ :__null__ 0), and raw min/max would mis-order a keyword against a number.

COUNT(*) is handled specially in translate-select (counts entities, always produces a long). COUNT(col) / COUNT(DISTINCT col) route to filter-count / filter-count-distinct which skip NULLs per SQL spec (PG: 'count(expression) returns the number of non-null input rows').

Map SQL aggregate function names (lowercased) to Datalog aggregate
symbols. SUM/AVG/MIN/MAX route to our `filter-*` variants so they
skip the `:__null__` sentinel that col-var! emits for missing
attribute values — Datahike's raw `sum`/`avg` would throw trying
`(+ :__null__ 0)`, and raw `min`/`max` would mis-order a keyword
against a number.

COUNT(*) is handled specially in translate-select (counts entities,
always produces a long). COUNT(col) / COUNT(DISTINCT col) route to
`filter-count` / `filter-count-distinct` which skip NULLs per SQL
spec (PG: 'count(expression) returns the number of non-null input
rows').
raw docstring

sql-divclj


sql-fn->clj-fnclj

Map of SQL function names (lowercased) to Clojure fn values.

Values are actual IFn objects (not symbols) so we can wrap them in null-safe at emit time. Java static methods are wrapped in thin fns so they're callable through the same path.

Map of SQL function names (lowercased) to Clojure fn values.

Values are actual `IFn` objects (not symbols) so we can wrap them in
`null-safe` at emit time. Java static methods are wrapped in thin fns
so they're callable through the same path.
raw docstring

sql-initcapclj

(sql-initcap s)

Capitalize first letter of each word.

Capitalize first letter of each word.
raw docstring

sql-leftclj

(sql-left s n)

Return first n characters of string.

Return first n characters of string.
raw docstring

sql-lpadclj

(sql-lpad s n)
(sql-lpad s n fill)

Left-pad string s to length n with fill character/string.

Left-pad string s to length n with fill character/string.
raw docstring

sql-modclj


sql-positionclj

(sql-position substring string)

Return 1-based position of substring in string, 0 if not found.

Return 1-based position of substring in string, 0 if not found.
raw docstring

sql-repeatclj

(sql-repeat s n)

Repeat string s n times.

Repeat string s n times.
raw docstring

sql-rightclj

(sql-right s n)

Return last n characters of string.

Return last n characters of string.
raw docstring

sql-rpadclj

(sql-rpad s n)
(sql-rpad s n fill)

Right-pad string s to length n with fill character/string.

Right-pad string s to length n with fill character/string.
raw docstring

cljdoc builds & hosts documentation for Clojure/Script libraries

Keyboard shortcuts
Ctrl+kJump to recent docs
Move to previous article
Move to next article
Ctrl+/Jump to the search field
× close