alamb commented on code in PR #23548:
URL: https://github.com/apache/datafusion/pull/23548#discussion_r3591808385
##########
datafusion/sqllogictest/test_files/group_by.slt:
##########
@@ -5641,3 +5641,56 @@ set datafusion.execution.target_partitions = 4;
statement count 0
drop table t;
+
+# DISTINCT must not be removed when a unique key is downgraded to a
+# non-unique functional dependency by a join: `u.id` is a primary key, but
+# after the LEFT JOIN each `u` row can occur once per matching order.
+statement ok
+CREATE TABLE users_with_pk (id INT, name VARCHAR, primary key(id)) AS VALUES
+ (1, 'alice'),
+ (2, 'bob');
+
+statement ok
+CREATE TABLE user_orders (user_id INT, amount INT) AS VALUES
+ (1, 10),
+ (1, 20),
+ (2, 30);
+
+query I
+SELECT DISTINCT u.id
Review Comment:
I verified this fails without the fix
```
1. query result mismatch:
[SQL] SELECT DISTINCT u.id
FROM users_with_pk u
LEFT JOIN user_orders o ON u.id = o.user_id
ORDER BY u.id;
[Diff] (-expected|+actual)
1
+ 1
2
at .../group_by.slt:5659
2. query result mismatch:
[SQL] EXPLAIN SELECT DISTINCT u.id ...
[Diff] (-expected|+actual)
logical_plan
- 01)Aggregate: groupBy=[[u.id]], aggr=[[]]
- 02)--Projection: u.id
- 03)----Left Join: u.id = o.user_id
...
+ 01)Projection: u.id
+ 02)--Left Join: u.id = o.user_id
...
Error: Execution("1 failures")
```
##########
datafusion/common/src/functional_dependencies.rs:
##########
@@ -151,8 +151,10 @@ pub struct FunctionalDependence {
/// Describes functional dependency mode.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Dependency {
- Single, // A determinant key may occur only once.
- Multi, // A determinant key may occur multiple times (in multiple rows).
+ /// A determinant key may occur only once.
Review Comment:
this is a nice drive by change
--
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]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]