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


##########
datafusion/proto/src/lib.rs:
##########
@@ -252,6 +252,34 @@ mod roundtrip_tests {
         Ok(())
     }
 
+    #[tokio::test]
+    async fn roundtrip_single_count_distinct() -> Result<(), DataFusionError> {
+        let ctx = SessionContext::new();
+
+        let schema = Schema::new(vec![
+            Field::new("a", DataType::Int64, true),
+            Field::new("b", DataType::Decimal128(15, 2), true),
+        ]);
+
+        ctx.register_csv(
+            "t1",
+            "testdata/test.csv",
+            CsvReadOptions::default().schema(&schema),
+        )
+        .await?;
+
+        let query = "SELECT a, COUNT(DISTINCT b) as b_cd FROM t1 GROUP BY a";
+        let plan = ctx.sql(query).await?.to_logical_plan()?;
+
+        println!("{:?}", plan);
+
+        let bytes = logical_plan_to_bytes(&plan)?;
+        let logical_round_trip = logical_plan_from_bytes(&bytes, &ctx)?;
+        assert_eq!(format!("{:?}", plan), format!("{:?}", logical_round_trip));

Review Comment:
   👍 



##########
datafusion/common/src/dfschema.rs:
##########
@@ -203,7 +203,20 @@ impl DFSchema {
                 // qualifier and name.
                 (Some(q), Some(field_q)) => q == field_q && field.name() == 
name,
                 // field to lookup is qualified but current field is 
unqualified.
-                (Some(_), None) => false,
+                (Some(qq), None) => {
+                    // the original field may now be aliased with a name that 
matches the
+                    // original qualified name
+                    let table_ref: TableReference = 
field.name().as_str().into();
+                    match table_ref {
+                        TableReference::Partial { schema, table } => {
+                            schema == qq && table == name
+                        }
+                        TableReference::Full { schema, table, .. } => {
+                            schema == qq && table == name
+                        }
+                        _ => false,
+                    }

Review Comment:
   I don't think alias's can be fully qualified identifiers
   
   I think the core disconnect is that datafusion 'flattens' compound 
identifiers into strings for column names which can then be referred to by 
anything that reads the plan that makes them
   
   Postgres deals with this type of issue by using indexes to identify columns, 
but that ends up with its own problems as the indexes invariably get mixed up 
sometimes and debugging it is super tricky.



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to