duckdb / community-extensions

https://community-extensions.duckdb.org
113 stars 21 forks source link

Add chsql extension #43

Closed lmangani closed 2 months ago

lmangani commented 2 months ago

Adding chsql extension to Community 🤞

This extension provides a growing number of Clickhouse SQL Macros for DuckDB.

Special thanks @carlopi for all the support and assistance backporting the builders to v1.0.0 ⭐

lmangani commented 2 months ago

I gave a brief looks, we would need to write a proper template for SQL-only extensions with @Alex-Monahan, but that's for later.

Is anything out of place? I'd be curious to learn and improve and/or act as a guinea duck anytime

CREATE OR REPLACE MACRO intDiv(a, b) AS (a / b);

but // should be used for integer division, otherwise you will get a floating point number as result.

We don't actually use the aliases sql file, its purely a reference for testing and understanding the logic.

The actual code is as follows:

{DEFAULT_SCHEMA, "intDiv", {"a", "b"}, R"((CAST(a AS BIGINT) / CAST(b AS BIGINT)))"},

Does it look proper?

lmangani commented 2 months ago

Exciting and works out of the box 😂 image

carlopi commented 2 months ago

I think you still need a double //:

D INSTALL chsql FROM community;
D LOAD chsql;
D SELECT intDiv(5,2);
┌──────────────┐
│ intdiv(5, 2) │
│    double    │
├──────────────┤
│          2.5 │
└──────────────┘

see also https://duckdb.org/docs/sql/functions/numeric.html#division-and-modulo-operators

D SELECT 5/2;
┌─────────┐
│ (5 / 2) │
│ double  │
├─────────┤
│     2.5 │
└─────────┘
D SELECT 5//2;
┌──────────┐
│ (5 // 2) │
│  int32   │
├──────────┤
│        2 │
└──────────┘
lmangani commented 2 months ago

@carlopi totally missed that and wrongly assumed you were referring to casting. Great input! I will update the macro and experiment with our first extension upgrade :)

lmangani commented 2 months ago

Fixed in 17c249ba08a9b88338e77c7f2d6e5dd2040b4590 @carlopi should I just repoint the extension ref or is a minor version increase required to cycle?

carlopi commented 2 months ago

Truth is that version field is, at the moment, not really used, only relevant information, also used on extension install is the ref. In the run-up to 1.1 we will iterate and specify more precisely the meaning of the version.