You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@calcite.apache.org by Eugen Stan <eu...@netdava.com> on 2021/12/27 23:35:11 UTC

Calcite-Clojure integration - use Clojure to implement calcite back-ends

Hello,

I would like to announce that I have ported the Calcite CSV example 
(scannable table only) from Java to Clojure.

I did it mainly as an exercise: to learn how Calicte works and how it 
would look with Clojure.

I learn a lot in the process and also discovered a Calcite bug 
https://issues.apache.org/jira/browse/CALCITE-4966 .

If anyone is interested, my code is available here: 
https://github.com/ieugen/clojure-training/tree/main/csv-clojure .

Id does need a version of Calcite with the fix for 
https://issues.apache.org/jira/browse/CALCITE-4966  (I built and 
deployed a local version).

It also needs a small 1 class library used as a bridge (build and 
install locally): 
https://github.com/ieugen/clojure-training/blob/main/csv-clojure/calcite-clj/src/main/java/ro.ieugen.calcite.clj/SchemaFactory.java 
.

With the above code I was able to call code like this:
(let [db {:jdbcUrl "jdbc:calcite:model=resources/model.json"
             :user "admin"
             :password "admin"}
         ds (jdbc/get-datasource db)]
     (jdbc/execute! ds ["select * from emps where age is null or age >= 
40"])))

and get back SQL results.

Part of the magic is in model.json file.
It uses ro.ieugen.calcite.clj.SchemaFactory and it has an operand property
  "clojure-clj.schema-factory": "ro.ieugen.calcite-csv/csv-schema" .

This property is a reference to the Clojure namespace 
(ro.ieugen.calcite-csv) and function (csv-schema) to call for generating 
the Schema.

The schema factory is generic and if there is interest I would like to 
contribute it upstream.

It allows the use of Clojure functions to be used as Schema factories 
thus creating a bridge to Clojure in a seamless way.


Full model.json bellow:

```
{
     "version": "1.0",
     "defaultSchema": "SALES",
     "schemas": [
       {
         "name": "SALES",
         "type": "custom",
         "factory": "ro.ieugen.calcite.clj.SchemaFactory",
         "operand": {
           "clojure-clj.schema-factory": "ro.ieugen.calcite-csv/csv-schema",
           "directory": "resources/sales"
         }
       }
     ]
   }

```
-- 
Eugen Stan

+40770 941 271  / https://www.netdava.com