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

[GitHub] [arrow-datafusion] alamb commented on a diff in pull request #5961: add an example of using DataFrame to create a subquery

alamb commented on code in PR #5961:
URL: https://github.com/apache/arrow-datafusion/pull/5961#discussion_r1163337939


##########
datafusion-examples/examples/dataframe_subquery.rs:
##########
@@ -0,0 +1,129 @@
+// 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.
+
+use std::sync::Arc;
+
+use datafusion::error::Result;
+use datafusion::prelude::*;
+use datafusion::test_util::arrow_test_data;
+use datafusion_common::ScalarValue;
+use datafusion_expr::Subquery;
+
+/// This example demonstrates how to use the DataFrame API to create a subquery.
+#[tokio::main]
+async fn main() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_aggregate_test_data("t1", &ctx).await?;
+    register_aggregate_test_data("t2", &ctx).await?;
+
+    where_scalar_subquery(&ctx).await?;
+
+    where_in_subquery(&ctx).await?;
+
+    where_exist_subquery(&ctx).await?;
+
+    Ok(())
+}
+
+//select c1,c2 from t1 where (select avg(t2.c2) from t2 where t1.c1 = t2.c1)>0 limit 10;
+async fn where_scalar_subquery(ctx: &SessionContext) -> Result<()> {
+    ctx.table("t1")
+        .await?
+        .filter(
+            Expr::ScalarSubquery(datafusion_expr::Subquery {

Review Comment:
   I agree this is 🤮  -- let me see if I can come up with some way to make this easier to construct



##########
datafusion-examples/examples/dataframe_subquery.rs:
##########
@@ -0,0 +1,129 @@
+// 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.
+
+use std::sync::Arc;
+
+use datafusion::error::Result;
+use datafusion::prelude::*;
+use datafusion::test_util::arrow_test_data;
+use datafusion_common::ScalarValue;
+use datafusion_expr::Subquery;
+
+/// This example demonstrates how to use the DataFrame API to create a subquery.
+#[tokio::main]
+async fn main() -> Result<()> {
+    let ctx = SessionContext::new();
+    register_aggregate_test_data("t1", &ctx).await?;
+    register_aggregate_test_data("t2", &ctx).await?;
+
+    where_scalar_subquery(&ctx).await?;
+
+    where_in_subquery(&ctx).await?;
+
+    where_exist_subquery(&ctx).await?;
+
+    Ok(())
+}
+
+//select c1,c2 from t1 where (select avg(t2.c2) from t2 where t1.c1 = t2.c1)>0 limit 10;
+async fn where_scalar_subquery(ctx: &SessionContext) -> Result<()> {
+    ctx.table("t1")
+        .await?
+        .filter(
+            Expr::ScalarSubquery(datafusion_expr::Subquery {
+                subquery: Arc::new(
+                    ctx.table("t2")
+                        .await?
+                        .filter(col("t1.c1").eq(col("t2.c1")))?
+                        .aggregate(vec![], vec![avg(col("t2.c2"))])?
+                        .select(vec![avg(col("t2.c2"))])?
+                        .into_unoptimized_plan(),
+                ),
+                outer_ref_columns: vec![],
+            })
+            .gt(lit(ScalarValue::UInt8(Some(0)))),

Review Comment:
   ```suggestion
               .gt(lit(0u8)),
   ```



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