Skip to content

Scoped queries with association comparison in filter breaks transformation #935

@patricebender

Description

@patricebender

Given this model:

entity Bar {
  key ID   : Integer;
      baz  : Association to Baz;
      hurz : Association to Hurz;
}

entity Baz {
  key ID : Integer;
}

entity Hurz {
  key ID : Integer;
}

the where clause of the exists subquery is broken:

> q = SELECT.from('Bar[baz = 1 and baz = ID]:hurz')
Query {
  SELECT: {
    from: {
      ref: [
        {
          id: 'Bar',
          where: [
            { ref: [ 'baz' ] },
            '=',
            { val: 1 },
            'and',
            { ref: [ 'baz' ] },
            '=',
            { ref: [ 'ID' ] }
          ]
        },
        'hurz'
      ]
    }
  }
}
> q.forSQL()
Query {
  SELECT: {
    from: { ref: [ 'Hurz' ], as: 'hurz' },
    where: [
      'exists',
      {
        SELECT: {
          from: { ref: [ 'Bar' ], as: 'Bar' },
          columns: [ { val: 1 } ],
          where: [
            { ref: [ 'Bar', 'hurz_ID' ] },
            '=',
            { ref: [ 'hurz', 'ID' ] },
            'and',
            { xpr: [ { ref: [ 'Bar', 'baz_ID' ] }, '=', { val: 1 } ] },
            'and', // ❌❌❌❌
            '=',
            { ref: [ 'Bar', 'ID' ] }
          ]
        }
      }
    ],
    columns: [ { ref: [ 'hurz', 'ID' ] } ]
  }
}

also broken in a different way if the condition is flipped:

> q = SELECT.from('Bar[baz = 1 and ID = baz]:hurz')
Query {
  SELECT: {
    from: {
      ref: [
        {
          id: 'Bar',
          where: [
            { ref: [ 'baz' ] },
            '=',
            { val: 1 },
            'and',
            { ref: [ 'ID' ] },
            '=',
            { ref: [ 'baz' ] }
          ]
        },
        'hurz'
      ]
    }
  }
}
> q.forSQL()
Query {
  SELECT: {
    from: { ref: [ 'Hurz' ], as: 'hurz' },
    where: [
      'exists',
      {
        SELECT: {
          from: { ref: [ 'Bar' ], as: 'Bar' },
          columns: [ { val: 1 } ],
          where: [
            { ref: [ 'Bar', 'hurz_ID' ] },
            '=',
            { ref: [ 'hurz', 'ID' ] },
            'and',
            { xpr: [ { ref: [ 'Bar', 'baz_ID' ] }, '=', { val: 1 } ] },
            'and',
            { ref: [ 'Bar', 'ID' ] },
            '=',
            { ref: [ 'Bar', 'baz' ] } // ❌❌❌❌
          ]
        }
      }
    ],
    columns: [ { ref: [ 'hurz', 'ID' ] } ]
  }
}

Metadata

Metadata

Assignees

Labels

bugSomething isn't working

Type

No type

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions