@@ -30,6 +30,7 @@ namespace duckdb {
30
30
31
31
static DefaultMacro chsql_macros[] = {
32
32
{DEFAULT_SCHEMA, " times_two" , {" x" , nullptr }, R"( x*2)" },
33
+ // -- Type conversion macros
33
34
{DEFAULT_SCHEMA, " toString" , {" x" , nullptr }, R"( CAST(x AS VARCHAR))" },
34
35
{DEFAULT_SCHEMA, " toInt8" , {" x" , nullptr }, R"( CAST(x AS INT8))" },
35
36
{DEFAULT_SCHEMA, " toInt16" , {" x" , nullptr }, R"( CAST(x AS INT16))" },
@@ -49,6 +50,7 @@ static DefaultMacro chsql_macros[] = {
49
50
{DEFAULT_SCHEMA, " toInt64OrNull" , {" x" , nullptr }, R"( TRY_CAST(x AS INT64))" },
50
51
{DEFAULT_SCHEMA, " toInt128OrNull" , {" x" , nullptr }, R"( TRY_CAST(x AS INT128))" },
51
52
{DEFAULT_SCHEMA, " toInt256OrNull" , {" x" , nullptr }, R"( TRY_CAST(x AS HUGEINT))" },
53
+ // -- Unsigned integer conversion macros
52
54
{DEFAULT_SCHEMA, " toUInt8" , {" x" , nullptr }, R"( CAST(x AS UTINYINT))" },
53
55
{DEFAULT_SCHEMA, " toUInt16" , {" x" , nullptr }, R"( CAST(x AS USMALLINT))" },
54
56
{DEFAULT_SCHEMA, " toUInt32" , {" x" , nullptr }, R"( CAST(x AS UINTEGER))" },
@@ -61,16 +63,22 @@ static DefaultMacro chsql_macros[] = {
61
63
{DEFAULT_SCHEMA, " toUInt16OrNull" , {" x" , nullptr }, R"( TRY_CAST(x AS USMALLINT))" }, // And here
62
64
{DEFAULT_SCHEMA, " toUInt32OrNull" , {" x" , nullptr }, R"( TRY_CAST(x AS UINTEGER))" }, // Also here
63
65
{DEFAULT_SCHEMA, " toUInt64OrNull" , {" x" , nullptr }, R"( TRY_CAST(x AS UBIGINT))" }, // And here
66
+ // -- Floating-point conversion macros
64
67
{DEFAULT_SCHEMA, " toFloat" , {" x" , nullptr }, R"( CAST(x AS DOUBLE))" },
65
68
{DEFAULT_SCHEMA, " toFloatOrNull" , {" x" , nullptr }, R"( TRY_CAST(x AS DOUBLE))" },
66
69
{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
67
71
{DEFAULT_SCHEMA, " intDiv" , {" a" , " b" }, R"( (CAST(a AS BIGINT) / CAST(b AS BIGINT)))" },
72
+ // -- String matching macros
68
73
{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)))" },
69
77
{nullptr , nullptr , {nullptr }, nullptr }};
70
78
71
79
// 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.
74
82
// (essentially, leave the last entry in the array as {nullptr, nullptr, {nullptr}, nullptr})
75
83
76
84
// Keep the DEFAULT_SCHEMA (no change needed)
@@ -80,7 +88,7 @@ static DefaultMacro chsql_macros[] = {
80
88
// If your function has parameters with default values, add their names and values in quotes inside of {}'s inside of the {}.
81
89
// Be sure to keep {nullptr, nullptr} at the end
82
90
// 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; )"
84
92
85
93
// clang-format off
86
94
static const DefaultTableMacro chsql_table_macros[] = {
0 commit comments