Skip to content

Commit

Permalink
JSON Macros
Browse files Browse the repository at this point in the history
  • Loading branch information
lmangani authored Oct 23, 2024
1 parent a000d4f commit 22c4dec
Showing 1 changed file with 8 additions and 0 deletions.
8 changes: 8 additions & 0 deletions chsql/src/chsql_extension.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,14 @@ static DefaultMacro chsql_macros[] = {
// IP Address Functions
{DEFAULT_SCHEMA, "IPv4NumToString", {"num", nullptr}, {{nullptr, nullptr}}, R"(CONCAT(CAST((num >> 24) & 255 AS VARCHAR), '.', CAST((num >> 16) & 255 AS VARCHAR), '.', CAST((num >> 8) & 255 AS VARCHAR), '.', CAST(num & 255 AS VARCHAR)))"},
{DEFAULT_SCHEMA, "IPv4StringToNum", {"ip", nullptr}, {{nullptr, nullptr}}, R"(CAST(SPLIT_PART(ip, '.', 1) AS INTEGER) * 256 * 256 * 256 + CAST(SPLIT_PART(ip, '.', 2) AS INTEGER) * 256 * 256 + CAST(SPLIT_PART(ip, '.', 3) AS INTEGER) * 256 + CAST(SPLIT_PART(ip, '.', 4) AS INTEGER))"},
// -- JSON Macros
{DEFAULT_SCHEMA, "JSONExtract", {"json", "key", "type"}, {{nullptr, nullptr}}, R"(CAST(json_extract(json, key) AS type))"},
{DEFAULT_SCHEMA, "JSONExtractRaw", {"json", "key"}, {{nullptr, nullptr}}, R"(json_extract(json, key))"},
{DEFAULT_SCHEMA, "JSONHas", {"json", "key"}, {{nullptr, nullptr}}, R"(json_extract(json, key) IS NOT NULL)"},
{DEFAULT_SCHEMA, "JSONLength", {"json"}, {{nullptr, nullptr}}, R"(json_array_length(json))"},
{DEFAULT_SCHEMA, "JSONType", {"json"}, {{nullptr, nullptr}}, R"(json_each(json))"},
{DEFAULT_SCHEMA, "JSONExtractKeys", {"json"}, {{nullptr, nullptr}}, R"(json_object_keys(json))"},
{DEFAULT_SCHEMA, "JSONExtractValues", {"json"}, {{nullptr, nullptr}}, R"(json_each_text(json))"},
// -- Misc macros
{DEFAULT_SCHEMA, "generateUUIDv4", {nullptr}, {{nullptr, nullptr}}, R"(toString(uuid()))"},
{DEFAULT_SCHEMA, "parseURL", {"url", "part", nullptr}, {{nullptr, nullptr}}, R"(CASE part WHEN 'protocol' THEN REGEXP_EXTRACT(url, '^(\w+)://') WHEN 'domain' THEN REGEXP_EXTRACT(url, '://([^/:]+)') WHEN 'port' THEN REGEXP_EXTRACT(url, ':(\d+)') WHEN 'path' THEN REGEXP_EXTRACT(url, '://[^/]+(/.+?)(\?|#|$)') WHEN 'query' THEN REGEXP_EXTRACT(url, '\?([^#]+)') WHEN 'fragment' THEN REGEXP_EXTRACT(url, '#(.+)$') END)"},
Expand Down

0 comments on commit 22c4dec

Please sign in to comment.