Skip to content

Commit 0d48335

Browse files
author
root
committed
resync
1 parent b7234ed commit 0d48335

File tree

1 file changed

+11
-3
lines changed

1 file changed

+11
-3
lines changed

src/chsql_extension.cpp

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ namespace duckdb {
3030

3131
static DefaultMacro chsql_macros[] = {
3232
{DEFAULT_SCHEMA, "times_two", {"x", nullptr}, R"(x*2)"},
33+
// -- Type conversion macros
3334
{DEFAULT_SCHEMA, "toString", {"x", nullptr}, R"(CAST(x AS VARCHAR))"},
3435
{DEFAULT_SCHEMA, "toInt8", {"x", nullptr}, R"(CAST(x AS INT8))"},
3536
{DEFAULT_SCHEMA, "toInt16", {"x", nullptr}, R"(CAST(x AS INT16))"},
@@ -49,6 +50,7 @@ static DefaultMacro chsql_macros[] = {
4950
{DEFAULT_SCHEMA, "toInt64OrNull", {"x", nullptr}, R"(TRY_CAST(x AS INT64))"},
5051
{DEFAULT_SCHEMA, "toInt128OrNull", {"x", nullptr}, R"(TRY_CAST(x AS INT128))"},
5152
{DEFAULT_SCHEMA, "toInt256OrNull", {"x", nullptr}, R"(TRY_CAST(x AS HUGEINT))"},
53+
// -- Unsigned integer conversion macros
5254
{DEFAULT_SCHEMA, "toUInt8", {"x", nullptr}, R"(CAST(x AS UTINYINT))"},
5355
{DEFAULT_SCHEMA, "toUInt16", {"x", nullptr}, R"(CAST(x AS USMALLINT))"},
5456
{DEFAULT_SCHEMA, "toUInt32", {"x", nullptr}, R"(CAST(x AS UINTEGER))"},
@@ -61,16 +63,22 @@ static DefaultMacro chsql_macros[] = {
6163
{DEFAULT_SCHEMA, "toUInt16OrNull", {"x", nullptr}, R"(TRY_CAST(x AS USMALLINT))"}, // And here
6264
{DEFAULT_SCHEMA, "toUInt32OrNull", {"x", nullptr}, R"(TRY_CAST(x AS UINTEGER))"}, // Also here
6365
{DEFAULT_SCHEMA, "toUInt64OrNull", {"x", nullptr}, R"(TRY_CAST(x AS UBIGINT))"}, // And here
66+
// -- Floating-point conversion macros
6467
{DEFAULT_SCHEMA, "toFloat", {"x", nullptr}, R"(CAST(x AS DOUBLE))"},
6568
{DEFAULT_SCHEMA, "toFloatOrNull", {"x", nullptr}, R"(TRY_CAST(x AS DOUBLE))"},
6669
{DEFAULT_SCHEMA, "toFloatOrZero", {"x", nullptr}, R"(CASE WHEN TRY_CAST(x AS DOUBLE) IS NOT NULL THEN CAST(x AS DOUBLE) ELSE 0 END)"},
70+
// -- Arithmetic macros
6771
{DEFAULT_SCHEMA, "intDiv", {"a", "b"}, R"((CAST(a AS BIGINT) / CAST(b AS BIGINT)))"},
72+
// -- String matching macros
6873
{DEFAULT_SCHEMA, "match", {"string", "token"}, R"(string LIKE token)"},
74+
// -- Array macros
75+
{DEFAULT_SCHEMA, "arrayExists", {"needle", "haystack"}, R"(haystack @> ARRAY[needle])"},
76+
{DEFAULT_SCHEMA, "arrayMap", {"e", "arr"}, R"(array_transform(arr, e -> (e * e)))"},
6977
{nullptr, nullptr, {nullptr}, nullptr}};
7078

7179
// To add a new table SQL macro, add a new macro to this array!
72-
// Copy and paste the top item in the array into the
73-
// second-to-last position and make some modifications.
80+
// Copy and paste the top item in the array into the
81+
// second-to-last position and make some modifications.
7482
// (essentially, leave the last entry in the array as {nullptr, nullptr, {nullptr}, nullptr})
7583

7684
// Keep the DEFAULT_SCHEMA (no change needed)
@@ -80,7 +88,7 @@ static DefaultMacro chsql_macros[] = {
8088
// If your function has parameters with default values, add their names and values in quotes inside of {}'s inside of the {}.
8189
// Be sure to keep {nullptr, nullptr} at the end
8290
// If you do not have parameters with default values, simplify to {nullptr, nullptr}
83-
// Add the text of your SQL macro as a raw string with the format R"( select 42; )"
91+
// Add the text of your SQL macro as a raw string with the format R"( select 42; )"
8492

8593
// clang-format off
8694
static const DefaultTableMacro chsql_table_macros[] = {

0 commit comments

Comments
 (0)