alamb commented on issue #12446:
URL: https://github.com/apache/datafusion/issues/12446#issuecomment-2361950461

   I am trying to figure out where the bug is (is it in the union equivalence 
properties calculation). I am not sure it is. 
   
   I added some debugging information to my small reproducer from 
https://github.com/apache/datafusion/issues/12446#issuecomment-2361896831 to 
try and figure out what the code should be doing
   
   The debugging (full output below) shows the input equivalence properties are:
   ```
   input1: [[a, c]], constants: [a0]
   input2: [[a0, c]], constants: [a]
   ```
   
   The current code computes the output equivalence properties as
   ```
   result: [a, a0], constants: []
   ```
   
   However, this seems correct to me. I worked an example to show this 🤔 
   
   # Worked example
   
   If `input1` is (sorted on `a`, `c,` constant `a0`):
   | a | c | a0 |
   |---|---|----|
   | 1 | 1 | N  |
   | 1 | 2 | N  |
   | 1 | 3 | N  |
   | 2 | 1 | N  |
   
   And `input2` is (sorted on `a0`, `c`, constant `a`):
   | a | c  | a0 |
   |---|----|----|
   | N | 10 | 10 |
   | N | 20 | 10 |
   | N | 30 | 10 |
   | N | 10 | 20 |
   
   There are at least two possible outputs
   
   Possible output 1: (sorted on `a` and `a0`)
   
   | a | c | a0 |
   |---|---|----|
   | 1 | 1 | N  |
   | 1 | 2 | N  |
   | 1 | 3 | N  |
   | 2 | 1 | N  |
   | N | 10 | 10 |
   | N | 20 | 10 |
   | N | 30 | 10 |
   | N | 10 | 20 |
   
   
   Possible output 2: ( also sorted on `a` and `a0`)
   
   | a | c | a0 |
   |---|---|----|
   | N | 10 | 10 |
   | N | 20 | 10 |
   | N | 30 | 10 |
   | N | 10 | 20 |
   | 1 | 1 | N  |
   | 1 | 2 | N  |
   | 1 | 3 | N  |
   | 2 | 1 | N  |
   
   
   
   
   
   
   <details><summary>Details</summary>
   <p>
   
   
    Here is the full output
   ```
   Calculating union of 2 equivalence properties
   ---------------
   EquivalenceProperties {
       eq_group: EquivalenceGroup {
           classes: [],
       },
       oeq_class: OrderingEquivalenceClass {
           orderings: [
               [
                   PhysicalSortExpr {
                       expr: Column {
                           name: "a",
                           index: 1,
                       },
                       options: SortOptions {
                           descending: false,
                           nulls_first: false,
                       },
                   },
                   PhysicalSortExpr {
                       expr: Column {
                           name: "c",
                           index: 0,
                       },
                       options: SortOptions {
                           descending: false,
                           nulls_first: false,
                       },
                   },
               ],
           ],
       },
       constants: [
           ConstExpr {
               expr: Column {
                   name: "a0",
                   index: 2,
               },
               across_partitions: true,
           },
       ],
       schema: Schema {
           fields: [
               Field {
                   name: "c",
                   data_type: Int32,
                   nullable: true,
                   dict_id: 0,
                   dict_is_ordered: false,
                   metadata: {},
               },
               Field {
                   name: "a",
                   data_type: Int32,
                   nullable: true,
                   dict_id: 0,
                   dict_is_ordered: false,
                   metadata: {},
               },
               Field {
                   name: "a0",
                   data_type: Int32,
                   nullable: true,
                   dict_id: 0,
                   dict_is_ordered: false,
                   metadata: {},
               },
           ],
           metadata: {},
       },
   }
   ---------------
   EquivalenceProperties {
       eq_group: EquivalenceGroup {
           classes: [],
       },
       oeq_class: OrderingEquivalenceClass {
           orderings: [
               [
                   PhysicalSortExpr {
                       expr: Column {
                           name: "a0",
                           index: 2,
                       },
                       options: SortOptions {
                           descending: false,
                           nulls_first: false,
                       },
                   },
                   PhysicalSortExpr {
                       expr: Column {
                           name: "c",
                           index: 0,
                       },
                       options: SortOptions {
                           descending: false,
                           nulls_first: false,
                       },
                   },
               ],
           ],
       },
       constants: [
           ConstExpr {
               expr: Column {
                   name: "a",
                   index: 1,
               },
               across_partitions: true,
           },
       ],
       schema: Schema {
           fields: [
               Field {
                   name: "c",
                   data_type: Int32,
                   nullable: true,
                   dict_id: 0,
                   dict_is_ordered: false,
                   metadata: {},
               },
               Field {
                   name: "a",
                   data_type: Int32,
                   nullable: true,
                   dict_id: 0,
                   dict_is_ordered: false,
                   metadata: {},
               },
               Field {
                   name: "a0",
                   data_type: Int32,
                   nullable: true,
                   dict_id: 0,
                   dict_is_ordered: false,
                   metadata: {},
               },
           ],
           metadata: {},
       },
   }
   Result:
   ---------------
   EquivalenceProperties {
       eq_group: EquivalenceGroup {
           classes: [],
       },
       oeq_class: OrderingEquivalenceClass {
           orderings: [
               [
                   PhysicalSortExpr {
                       expr: Column {
                           name: "a",
                           index: 1,
                       },
                       options: SortOptions {
                           descending: false,
                           nulls_first: false,
                       },
                   },
               ],
               [
                   PhysicalSortExpr {
                       expr: Column {
                           name: "a0",
                           index: 2,
                       },
                       options: SortOptions {
                           descending: false,
                           nulls_first: false,
                       },
                   },
               ],
           ],
       },
       constants: [],
       schema: Schema {
           fields: [
               Field {
                   name: "c",
                   data_type: Int32,
                   nullable: true,
                   dict_id: 0,
                   dict_is_ordered: false,
                   metadata: {},
               },
               Field {
                   name: "a",
                   data_type: Int32,
                   nullable: true,
                   dict_id: 0,
                   dict_is_ordered: false,
                   metadata: {},
               },
               Field {
                   name: "a0",
                   data_type: Int32,
                   nullable: true,
                   dict_id: 0,
                   dict_is_ordered: false,
                   metadata: {},
               },
           ],
           metadata: {},
       },
   }
   ```
   
   </p>
   </details> 
   
   


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

Reply via email to