You are viewing a plain text version of this content. The canonical link for it is here.
Posted to github@arrow.apache.org by GitBox <gi...@apache.org> on 2022/04/28 15:19:18 UTC

[GitHub] [arrow-datafusion] andygrove opened a new pull request, #2369: Stop optimizing queries twice

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

   # 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 https://github.com/apache/arrow-datafusion/issues/2368
   
    # Rationale for this change
   <!--
    Why are you proposing this change? If this is already explained clearly in the issue then this section is not needed.
    Explaining clearly why changes are proposed helps reviewers understand your changes and offer better suggestions for fixes.  
   -->
   
   Why do something twice when you can do it once.
   
   I see speedup of 8% - 11% in the included criterion benchmark for SQL planning.
   
   # 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.
   -->
   
   - SQL execution no longer optimizes the logical plan before creating the physical plan
   - DataFrame execution no longer optimizes the logical plan before creating the physical plan
   - Optimization happens once when creating physical plan
   - Criterion bench added for SQL planning
   
   
   # Are there any user-facing changes?
   <!--
   If there are user-facing changes then we may require documentation to be updated before approving the PR.
   -->
   
   Yes. Users will now see unoptimized plans and maybe we will need to make changes to EXPLAIN before we merge this so that they still have a way to see the optimized plan before execution?
   
   <!--
   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] andygrove commented on a diff in pull request #2369: Stop optimizing queries twice

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #2369:
URL: https://github.com/apache/arrow-datafusion/pull/2369#discussion_r861842848


##########
datafusion/core/src/execution/context.rs:
##########
@@ -1887,26 +1885,6 @@ mod tests {
         Ok(())
     }
 
-    #[tokio::test]
-    async fn ctx_sql_should_optimize_plan() -> Result<()> {
-        let ctx = SessionContext::new();
-        let plan1 = ctx
-            .create_logical_plan("SELECT * FROM (SELECT 1) AS one WHERE TRUE AND TRUE")?;
-
-        let opt_plan1 = ctx.optimize(&plan1)?;
-
-        let plan2 = ctx
-            .sql("SELECT * FROM (SELECT 1) AS one WHERE TRUE AND TRUE")
-            .await?;
-
-        assert_eq!(

Review Comment:
   @alamb I made that change and I think that worked out well. The last commit is quite large because I had to make `to_logical_plan` return a `Result` now so had to update the call sites and I also stopped using this method internally within `DataFrame` and replaced those calls with `self.plan.clone()` instead (since that is what `to_logical_plan` was originally doing).
   
   



-- 
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 #2369: Stop optimizing queries twice

Posted by GitBox <gi...@apache.org>.
alamb commented on code in PR #2369:
URL: https://github.com/apache/arrow-datafusion/pull/2369#discussion_r861314004


##########
datafusion/core/src/execution/context.rs:
##########
@@ -1887,26 +1885,6 @@ mod tests {
         Ok(())
     }
 
-    #[tokio::test]
-    async fn ctx_sql_should_optimize_plan() -> Result<()> {
-        let ctx = SessionContext::new();
-        let plan1 = ctx
-            .create_logical_plan("SELECT * FROM (SELECT 1) AS one WHERE TRUE AND TRUE")?;
-
-        let opt_plan1 = ctx.optimize(&plan1)?;
-
-        let plan2 = ctx
-            .sql("SELECT * FROM (SELECT 1) AS one WHERE TRUE AND TRUE")
-            .await?;
-
-        assert_eq!(

Review Comment:
   Maybe we could address the UX by making `to_logical_plan` call optimize?



##########
datafusion/core/benches/sql_planner.rs:
##########
@@ -0,0 +1,83 @@
+// Licensed to the Apache Software Foundation (ASF) under one
+// or more contributor license agreements.  See the NOTICE file
+// distributed with this work for additional information
+// regarding copyright ownership.  The ASF licenses this file
+// to you under the Apache License, Version 2.0 (the
+// "License"); you may not use this file except in compliance
+// with the License.  You may obtain a copy of the License at
+//
+//   http://www.apache.org/licenses/LICENSE-2.0
+//
+// Unless required by applicable law or agreed to in writing,
+// software distributed under the License is distributed on an
+// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+// KIND, either express or implied.  See the License for the
+// specific language governing permissions and limitations
+// under the License.
+
+#[macro_use]
+extern crate criterion;
+extern crate arrow;
+extern crate datafusion;
+
+mod data_utils;
+use crate::criterion::Criterion;
+use arrow::datatypes::{DataType, Field, Schema};
+use datafusion::datasource::MemTable;
+use datafusion::error::Result;
+use datafusion::execution::context::SessionContext;
+use parking_lot::Mutex;
+use std::sync::Arc;
+use tokio::runtime::Runtime;
+
+fn plan(ctx: Arc<Mutex<SessionContext>>, sql: &str) {
+    let rt = Runtime::new().unwrap();
+    criterion::black_box(rt.block_on(ctx.lock().sql(sql)).unwrap());
+}
+
+/// Create schema representing a large table
+pub fn create_schema(column_prefix: &str) -> Schema {
+    let fields = (0..200)
+        .map(|i| Field::new(&format!("{}{}", column_prefix, i), DataType::Int32, true))
+        .collect();
+    Schema::new(fields)
+}
+
+pub fn create_table_provider(column_prefix: &str) -> Result<Arc<MemTable>> {
+    let schema = Arc::new(create_schema(column_prefix));
+    MemTable::try_new(schema, vec![]).map(Arc::new)
+}
+
+fn create_context() -> Result<Arc<Mutex<SessionContext>>> {
+    let ctx = SessionContext::new();
+    ctx.register_table("t1", create_table_provider("a")?)?;
+    ctx.register_table("t2", create_table_provider("b")?)?;
+    Ok(Arc::new(Mutex::new(ctx)))
+}
+
+fn criterion_benchmark(c: &mut Criterion) {
+    let ctx = create_context().unwrap();
+
+    c.bench_function("trivial join early columns", |b| {
+        b.iter(|| {
+            plan(
+                ctx.clone(),
+                "SELECT t1.a2, t2.b2  \
+                 FROM t1, t2 WHERE a1 = b1",
+            )
+        })
+    });
+
+    c.bench_function("trivial join late columns", |b| {
+        b.iter(|| {
+            plan(
+                ctx.clone(),
+                "SELECT t1.a99, t2.b99  \
+                 FROM t1, t2 WHERE a199 = b199",

Review Comment:
   maybe would be nice to add a `GROUP BY` as well :)
   
   It is great to start getting benchmarks on the planner 👍 



-- 
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] andygrove commented on a diff in pull request #2369: Stop optimizing queries twice

Posted by GitBox <gi...@apache.org>.
andygrove commented on code in PR #2369:
URL: https://github.com/apache/arrow-datafusion/pull/2369#discussion_r861388120


##########
datafusion/core/src/execution/context.rs:
##########
@@ -1887,26 +1885,6 @@ mod tests {
         Ok(())
     }
 
-    #[tokio::test]
-    async fn ctx_sql_should_optimize_plan() -> Result<()> {
-        let ctx = SessionContext::new();
-        let plan1 = ctx
-            .create_logical_plan("SELECT * FROM (SELECT 1) AS one WHERE TRUE AND TRUE")?;
-
-        let opt_plan1 = ctx.optimize(&plan1)?;
-
-        let plan2 = ctx
-            .sql("SELECT * FROM (SELECT 1) AS one WHERE TRUE AND TRUE")
-            .await?;
-
-        assert_eq!(

Review Comment:
   Ah, yes, I like that idea.



-- 
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] andygrove commented on pull request #2369: Stop optimizing queries twice

Posted by GitBox <gi...@apache.org>.
andygrove commented on PR #2369:
URL: https://github.com/apache/arrow-datafusion/pull/2369#issuecomment-1112339900

   @alamb @Dandandan @matthewmturner This might be a step backward in UX so I left this as a draft while we discuss.


-- 
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] andygrove merged pull request #2369: Stop optimizing queries twice

Posted by GitBox <gi...@apache.org>.
andygrove merged PR #2369:
URL: https://github.com/apache/arrow-datafusion/pull/2369


-- 
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