You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by "waynexia (via GitHub)" <gi...@apache.org> on 2023/04/11 13:16:07 UTC

[GitHub] [arrow-datafusion] waynexia opened a new pull request, #5963: feat: allow the customization of analyzer rules

waynexia opened a new pull request, #5963:
URL: https://github.com/apache/arrow-datafusion/pull/5963

   # Which issue does this PR close?
   
   <!--
   We generally require a GitHub issue to be filed for all bug fixes and enhancements and this helps us generate change logs for our releases. You can link an issue to this PR using the GitHub syntax. For example `Closes #123` indicates that this PR will close issue #123.
   -->
   
   Closes #.
   
   # Rationale for this change
   
   
   Like `Optimizer` allows to customize its rules, this patch adds a similar interface to `Analyzer`.
   
   # What changes are included in this PR?
   
   <!--
   There is no need to duplicate the description in the issue here but it is sometimes worth providing a summary of the individual changes in this PR.
   -->
   
   Allow users to insert their customized analyzer rules into the optimizer.
   
   # Are these changes tested?
   
   <!--
   We typically require tests for all PRs in order to:
   1. Prevent the code from being accidentally broken by subsequent changes
   2. Serve as another way to document the expected behavior of the code
   
   If tests are not included in your PR, please explain why (for example, are they covered by existing tests)?
   -->
   
   # Are there any user-facing changes?
   
   Yes, `Optimizer::with_rules()` now accepts two parameters, one for analyzer rule and one for optimizer rule.
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   <!--
   If there are any breaking changes to public APIs, please add the `api change` label.
   -->


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] waynexia commented on pull request #5963: feat: allow the customization of analyzer rules

Posted by "waynexia (via GitHub)" <gi...@apache.org>.
waynexia commented on PR #5963:
URL: https://github.com/apache/arrow-datafusion/pull/5963#issuecomment-1504566362

   >The rationale is both to document the API for potential users but also to help ensure that we don't accidentally remove / break this API in the future during a refactpor
   
   Great suggestion! 👍 I have expanded the example to illustrate how to create and implement a basic analyzer rule.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] waynexia commented on pull request #5963: feat: allow the customization of analyzer rules

Posted by "waynexia (via GitHub)" <gi...@apache.org>.
waynexia commented on PR #5963:
URL: https://github.com/apache/arrow-datafusion/pull/5963#issuecomment-1503369398

   > I think put `analyzer rule` in optimizer isn't a good idea. Because they are two part, and I think someday we may put analyzer into a independent part.
   
   Same concern. To avoid the second breaking change in the future we'd better separate them at the beginning.
   
   I'll change the config part, add `Analyzer` to the session context. But leave the code structure unchanged.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] waynexia commented on a diff in pull request #5963: feat: allow the customization of analyzer rules

Posted by "waynexia (via GitHub)" <gi...@apache.org>.
waynexia commented on code in PR #5963:
URL: https://github.com/apache/arrow-datafusion/pull/5963#discussion_r1162806690


##########
datafusion/optimizer/src/optimizer.rs:
##########
@@ -244,12 +256,18 @@ impl Optimizer {
             Arc::new(PushDownLimit::new()),
         ];
 
-        Self::with_rules(rules)
+        Self::with_rules(analyzer_rules, rules)
     }
 
     /// Create a new optimizer with the given rules
-    pub fn with_rules(rules: Vec<Arc<dyn OptimizerRule + Send + Sync>>) -> Self {
-        Self { rules }
+    pub fn with_rules(
+        analyzer_rules: Vec<Arc<dyn AnalyzerRule + Send + Sync>>,
+        rules: Vec<Arc<dyn OptimizerRule + Send + Sync>>,
+    ) -> Self {
+        Self {
+            analyzer_rules,
+            rules,
+        }
     }

Review Comment:
   I'm unsure if this is a proper place to customize analyzer rules. But from the code structure `Analyzer` is part of `Optimizer`. And only `Optimizer` is exposed to the context. Please let me know what you think @alamb @jackwener 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jackwener commented on a diff in pull request #5963: feat: allow the customization of analyzer rules

Posted by "jackwener (via GitHub)" <gi...@apache.org>.
jackwener commented on code in PR #5963:
URL: https://github.com/apache/arrow-datafusion/pull/5963#discussion_r1164098426


##########
datafusion-examples/examples/rewrite_expr.rs:
##########
@@ -66,11 +75,61 @@ fn observe(plan: &LogicalPlan, rule: &dyn OptimizerRule) {
     )
 }
 
-struct MyRule {}
+/// An example analyzer rule that changes Int64 literals to UInt64
+struct MyAnalyzerRule {}
+
+impl AnalyzerRule for MyAnalyzerRule {
+    fn analyze(&self, plan: LogicalPlan, _config: &ConfigOptions) -> Result<LogicalPlan> {
+        Self::analyze_internal(&plan)
+    }
+
+    fn name(&self) -> &str {
+        "my_analyzer_rule"
+    }
+}
+
+impl MyAnalyzerRule {
+    fn analyze_internal(plan: &LogicalPlan) -> Result<LogicalPlan> {
+        // optimize child plans first
+        let mut new_inputs = plan
+            .inputs()
+            .iter()
+            .map(|p| Self::analyze_internal(p))
+            .collect::<Result<Vec<_>>>()?;

Review Comment:
   we can use `transform_up`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jackwener commented on a diff in pull request #5963: feat: allow the customization of analyzer rules

Posted by "jackwener (via GitHub)" <gi...@apache.org>.
jackwener commented on code in PR #5963:
URL: https://github.com/apache/arrow-datafusion/pull/5963#discussion_r1164094681


##########
datafusion/optimizer/src/analyzer/count_wildcard_rule.rs:
##########
@@ -28,6 +28,12 @@ use crate::analyzer::AnalyzerRule;
 /// Resolve issue: https://github.com/apache/arrow-datafusion/issues/5473.
 pub struct CountWildcardRule {}
 
+impl Default for CountWildcardRule {
+    fn default() -> Self {
+        Self::new()
+    }
+}
+

Review Comment:
   we can use `#[derive(Default)]`



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jackwener commented on pull request #5963: feat: allow the customization of analyzer rules

Posted by "jackwener (via GitHub)" <gi...@apache.org>.
jackwener commented on PR #5963:
URL: https://github.com/apache/arrow-datafusion/pull/5963#issuecomment-1503351111

   I think put `analyzer rule` in optimizer isn't a good idea.
   Because they are two part, and I think someday we may put analyzer into a independent part.


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #5963: feat: allow the customization of analyzer rules

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb commented on code in PR #5963:
URL: https://github.com/apache/arrow-datafusion/pull/5963#discussion_r1164554403


##########
datafusion-examples/examples/rewrite_expr.rs:
##########
@@ -46,10 +47,18 @@ pub fn main() -> Result<()> {
         logical_plan.display_indent()
     );
 
-    // now run the optimizer with our custom rule
-    let optimizer = Optimizer::with_rules(vec![Arc::new(MyRule {})]);
+    // run the analyzer with our custom rule
     let config = OptimizerContext::default().with_skip_failing_rules(false);
-    let optimized_plan = optimizer.optimize(&logical_plan, &config, observe)?;
+    let analyzer = Analyzer::with_rules(vec![Arc::new(MyAnalyzerRule {})]);

Review Comment:
   👍 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] jackwener commented on a diff in pull request #5963: feat: allow the customization of analyzer rules

Posted by "jackwener (via GitHub)" <gi...@apache.org>.
jackwener commented on code in PR #5963:
URL: https://github.com/apache/arrow-datafusion/pull/5963#discussion_r1164091423


##########
datafusion/optimizer/src/optimizer.rs:
##########
@@ -332,7 +330,7 @@ impl Optimizer {
         }
         log_plan("Final optimized plan", &new_plan);
         debug!("Optimizer took {} ms", start_time.elapsed().as_millis());
-        Ok(new_plan)
+        Ok(new_plan.clone())

Review Comment:
   ```suggestion
           Ok(new_plan)
   ```



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] waynexia commented on a diff in pull request #5963: feat: allow the customization of analyzer rules

Posted by "waynexia (via GitHub)" <gi...@apache.org>.
waynexia commented on code in PR #5963:
URL: https://github.com/apache/arrow-datafusion/pull/5963#discussion_r1164181882


##########
datafusion-examples/examples/rewrite_expr.rs:
##########
@@ -66,11 +75,61 @@ fn observe(plan: &LogicalPlan, rule: &dyn OptimizerRule) {
     )
 }
 
-struct MyRule {}
+/// An example analyzer rule that changes Int64 literals to UInt64
+struct MyAnalyzerRule {}
+
+impl AnalyzerRule for MyAnalyzerRule {
+    fn analyze(&self, plan: LogicalPlan, _config: &ConfigOptions) -> Result<LogicalPlan> {
+        Self::analyze_internal(&plan)
+    }
+
+    fn name(&self) -> &str {
+        "my_analyzer_rule"
+    }
+}
+
+impl MyAnalyzerRule {
+    fn analyze_internal(plan: &LogicalPlan) -> Result<LogicalPlan> {
+        // optimize child plans first
+        let mut new_inputs = plan
+            .inputs()
+            .iter()
+            .map(|p| Self::analyze_internal(p))
+            .collect::<Result<Vec<_>>>()?;

Review Comment:
   TIL, it looks pretty :+1: 



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org


[GitHub] [arrow-datafusion] alamb merged pull request #5963: feat: allow the customization of analyzer rules

Posted by "alamb (via GitHub)" <gi...@apache.org>.
alamb merged PR #5963:
URL: https://github.com/apache/arrow-datafusion/pull/5963


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: github-unsubscribe@arrow.apache.org

For queries about this service, please contact Infrastructure at:
users@infra.apache.org