You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
However, we don't have a corresponding example for writing a user defined AnalyzerRule which also means the APIs for using them are complicated (see #10849 for example)
However, it only works with the analyzer and the optimizer. Maybe we can enhance it after #10849 to show how to apply the custom rules for the end-to-end DataFusion query flow.
Currently, in my personal work, I just use it like
let ctx = SessionContext::new();let new_state = ctx
.state().add_analyzer_rule(Arc::new(ModelAnalyzeRule::new())).add_analyzer_rule(Arc::new(ModelGenerationRule::new()));let new_ctx = SessionContext::new_with_state(new_state);// create a plan to run a SQL querylet df = new_ctx.sql("SELECT * FROM datafusion.default.orders").await?;
After the API updated, I guess it would be
let ctx = SessionContext::new();
ctx.add_analyzer_rule(Arc::new(ModelAnalyzeRule::new())).add_analyzer_rule(Arc::new(ModelGenerationRule::new()));// create a plan to run a SQL querylet df = ctx.sql("SELECT * FROM datafusion.default.orders").await?;
After #10849 is done, if no one else is working on it, I think I can help with it.
Hi @alamb, I just want to share how I use user-defined AnalyzerRule. As you know, I'm working on reimplementing the semantic layer engine, wren-engine, for LLM using DataFusion. The project is still a work in progress. However, I think I have finished the part of integration with DataFusion. I think it could be a nice use case for AnalyzerRule.
The basic concept of wren-engine is that user can define a virtual modeling layer to apply on their physical data.
For example, given a csv file registered in DataFusion context called, datafusion.public.customers. We can wrap a virtual table for it called, wrenai.default.customers_model.
User can query it like
select*fromwrenai.default.customers_model
Then , wren engine translate it to a physical query:
It's a simple use case to show how it works. We have other features, such as relationship between models or calculated fields in the model. I don't go into detail about them here. However, I implemented all of them through AnalyzerRule and UserDefinedLogicalNode.
The related PR, Canner/wren-engine#613, is still under review. However, you can find the usage of AnalyzerRule in rule.rs and the plan node in plan.rs. I also added some examples to show how to use the API or integrate with the DataFusion query flow.
Thanks to DataFusion for providing such amazing features, allowing me to implement a more stable and structured converter for the semantic layer.
Thank you @goldmedal - this is a great hint. I have some basic analyzer rule coded up but maybe your example is a better idea. I have a PR in progress that I need to polish up and then will ping you for review
Activity
alamb commentedon Jun 10, 2024
Perhaps @goldmedal is interested in this / has some example code to share
goldmedal commentedon Jun 10, 2024
I think we have an existing example showing how to create an AnalyzerRule in
datafusion/datafusion-examples/examples/rewrite_expr.rs
Line 82 in 3773fb7
However, it only works with the analyzer and the optimizer. Maybe we can enhance it after #10849 to show how to apply the custom rules for the end-to-end DataFusion query flow.
goldmedal commentedon Jun 10, 2024
Currently, in my personal work, I just use it like
After the API updated, I guess it would be
After #10849 is done, if no one else is working on it, I think I can help with it.
[-]Add example for writing an `AnalyzerRule[/-][+]Add example for writing an `AnalyzerRule`[/+]alamb commentedon Jun 15, 2024
I have some time on a plane today that I may use to try and write up this example. I was inspired by some discussion I had this week
goldmedal commentedon Jun 16, 2024
Hi @alamb, I just want to share how I use user-defined AnalyzerRule. As you know, I'm working on reimplementing the semantic layer engine, wren-engine, for LLM using DataFusion. The project is still a work in progress. However, I think I have finished the part of integration with DataFusion. I think it could be a nice use case for
AnalyzerRule.The basic concept of wren-engine is that user can define a virtual modeling layer to apply on their physical data.
For example, given a csv file registered in DataFusion context called,
datafusion.public.customers. We can wrap a virtual table for it called,wrenai.default.customers_model.User can query it like
Then , wren engine translate it to a physical query:
It's a simple use case to show how it works. We have other features, such as
relationshipbetween models or calculated fields in the model. I don't go into detail about them here. However, I implemented all of them throughAnalyzerRuleandUserDefinedLogicalNode.The related PR, Canner/wren-engine#613, is still under review. However, you can find the usage of
AnalyzerRulein rule.rs and the plan node in plan.rs. I also added some examples to show how to use the API or integrate with the DataFusion query flow.Thanks to DataFusion for providing such amazing features, allowing me to implement a more stable and structured converter for the semantic layer.
alamb commentedon Jun 17, 2024
Thank you @goldmedal - this is a great hint. I have some basic analyzer rule coded up but maybe your example is a better idea. I have a PR in progress that I need to polish up and then will ping you for review
alamb commentedon Jun 24, 2024
Here is my proposed analyzer rule example: #11089
It isn't quite as fancy as the wren example, but I think it is an understandable enough example