Calcite already has something called macros.  They work like Lisp macros 
(substituting the AST) rather than C macros (your model of string replacement). 
See JdbcTest.testTableMacro [1].

Does this approach solve your problem?

Julian

[1] 
https://github.com/apache/calcite/blob/82dd78a14f6aef2eeec2f9c94978d04b4acc5359/core/src/test/java/org/apache/calcite/test/JdbcTest.java#L427
 

> On Nov 3, 2021, at 1:53 PM, Cyril Catheu <[email protected]> wrote:
> 
> Hey,
> 
> I'm working on a product that executes arbitrary SQL on a "data source".
> A data source implements a SQL language, for instance, it can be MySQL,
> Pinot, BigQuery, etc...
> I'd like to introduce a macro language on top of the SQL.
> Very similar to what is done in grafana:
> https://grafana.com/docs/grafana/latest/datasources/mysql/#macros
> These macros would be resolved before the SQL is executed on the data
> source.
> 
> A macro looks like a function but is a string replacement, for instance:
> __timeFilter(timeColumn, start, end) -->  timeColumn>=start and
> timeColumn<end
> 
> Grafana uses regex replacements, but I'm considering parsing the SQL with
> Calcite to apply the macros safely and manage nested macros/functions.
> 
> --> Is this a good use case for Calcite? What seemed interesting to me is
> that Calcite has all those SqlDialect implementations.
> 
> I had this flow in mind:
> 1. get the SqlDialect of my datasource
> *2.* add to this dialect my custom macros (add to an SqlFunction list? )
> 3. parse
> 4. replace
> 5. rebuild
> 
> I'm wondering if 2. is possible? I'm not sure to understand how I could add
> a list of SqlFunction to a dialect, or build a new dialect from an existing
> one dynamically.
> 
> If you've taken the time to read this message, thanks a lot!
> Have a nice day.
> 
> -- 
> [image: Startree] <https://www.startree.ai/>
> 
> Cyril de Catheu
> 
> Software Engineer, StarTree
> 
> [email protected] | +33 684-829-908
> 
> [image: Linkedin] <https://www.linkedin.com/in/cyril-de-catheu/>[image:
> Twitter] <https://twitter.com/deCatheu>

Reply via email to