MCPcopy Create free account
hub / github.com/PostHog/duckgres / InitMacros

Function InitMacros

server/chsql/chsql.go:12–65  ·  view source on GitHub ↗

InitMacros registers ClickHouse SQL function macros so that ClickHouse-flavoured queries work out of the box against DuckDB. This is a subset of the chsql community extension's functions, implemented as plain SQL macros following the same pattern as initUtilityMacros.

(db *sql.DB)

Source from the content-addressed store, hash-verified

10// This is a subset of the chsql community extension's functions, implemented
11// as plain SQL macros following the same pattern as initUtilityMacros.
12func InitMacros(db *sql.DB) {
13 macros := []string{
14 // -- Type conversion --
15 `CREATE OR REPLACE MACRO toString(x) AS CAST(x AS VARCHAR)`,
16 `CREATE OR REPLACE MACRO toInt32(x) AS CAST(x AS INTEGER)`,
17 `CREATE OR REPLACE MACRO toInt64(x) AS CAST(x AS BIGINT)`,
18 `CREATE OR REPLACE MACRO toFloat(x) AS CAST(x AS DOUBLE)`,
19 `CREATE OR REPLACE MACRO toInt32OrNull(x) AS TRY_CAST(x AS INTEGER)`,
20 `CREATE OR REPLACE MACRO toInt32OrZero(x) AS COALESCE(TRY_CAST(x AS INTEGER), 0)`,
21
22 // -- Arithmetic --
23 `CREATE OR REPLACE MACRO intDiv(a, b) AS CAST(a / b AS BIGINT)`,
24 `CREATE OR REPLACE MACRO modulo(a, b) AS a % b`,
25
26 // -- String --
27 `CREATE OR REPLACE MACRO empty(s) AS (length(s) = 0)`,
28 `CREATE OR REPLACE MACRO notEmpty(s) AS (length(s) > 0)`,
29 `CREATE OR REPLACE MACRO splitByChar(sep, s) AS string_split(s, sep)`,
30 `CREATE OR REPLACE MACRO lengthUTF8(s) AS length(s)`,
31
32 // -- Date --
33 `CREATE OR REPLACE MACRO toYear(d) AS date_part('year', d)`,
34 `CREATE OR REPLACE MACRO toMonth(d) AS date_part('month', d)`,
35 `CREATE OR REPLACE MACRO toDayOfMonth(d) AS date_part('day', d)`,
36 `CREATE OR REPLACE MACRO toYYYYMMDD(d) AS CAST(strftime(CAST(d AS DATE), '%Y%m%d') AS INTEGER)`,
37 `CREATE OR REPLACE MACRO toYYYYMM(d) AS CAST(strftime(CAST(d AS DATE), '%Y%m') AS INTEGER)`,
38
39 // -- URL --
40 // protocol: extract scheme before "://"
41 `CREATE OR REPLACE MACRO protocol(url) AS CASE WHEN contains(url, '://') THEN split_part(url, '://', 1) ELSE '' END`,
42 // domain: extract host from URL
43 `CREATE OR REPLACE MACRO domain(url) AS split_part(split_part(split_part(url, '://', 2), '/', 1), ':', 1)`,
44 // topLevelDomain: last dot-separated part of the domain
45 `CREATE OR REPLACE MACRO topLevelDomain(url) AS split_part(split_part(split_part(split_part(url, '://', 2), '/', 1), ':', 1), '.', -1)`,
46
47 // -- IP --
48 // IPv4NumToString: convert a 32-bit integer to dotted-decimal notation
49 `CREATE OR REPLACE MACRO IPv4NumToString(n) AS concat_ws('.', (n >> 24) & 255, (n >> 16) & 255, (n >> 8) & 255, n & 255)`,
50
51 // -- JSON --
52 `CREATE OR REPLACE MACRO JSONExtractString(json, path) AS CAST(json_extract(json, path) AS VARCHAR)`,
53 `CREATE OR REPLACE MACRO JSONHas(json, path) AS (json_extract(json, path) IS NOT NULL)`,
54
55 // -- Misc --
56 `CREATE OR REPLACE MACRO generateUUIDv4() AS uuid()`,
57 `CREATE OR REPLACE MACRO ifNull(x, alt) AS COALESCE(x, alt)`,
58 }
59
60 for _, m := range macros {
61 if _, err := db.Exec(m); err != nil {
62 slog.Warn("Failed to create ClickHouse macro", "sql", m, "error", err)
63 }
64 }
65}

Calls 1

ExecMethod · 0.65

Tested by 3

TestClickHouseMacrosFunction · 0.74