adriank / ObjectPath

The agile query language for semi-structured data
http://objectpath.org
MIT License
380 stars 93 forks source link

Intersection and difference #55

Closed felixhao28 closed 6 years ago

felixhao28 commented 7 years ago

Currently we have + for union operation to combine two or more collections together. This is very helpful in the way you can split your desired results into sub-sections and write different query paths for each. In addition to +, in certain use cases, more collection operators will prove their usefulness (borrowing from set theory).

I am proposing 2 new collection operators: intersection and difference. These operations require operands to have non duplicate elements. So for these operations, there will be two modes: by-ref and by-value. A and B are by-ref equal if they have the excact same path. A and B are by-value equal if they have the same primitive value or all fields of them are by-value equal.

An array can be a set as long as there are no two elements equal to each other. If otherwise, duplicates are removed before performing any of the set operations.

Intersection ^ (by-ref) and .^ (by-value)

A ^ B is an intersection of A and A iff every element in A ^ B is contained by both A and B.

poems:
    - name: The Bronze Horseman
      author: Pushkin
    - name: Devils
      author: Pushkin
    - name: Oh, Laziness, Come...
      author: Pushkin
    - name: Gitanjali
      author: Tagore
    - name: Manasi
      author: Tagore
    - name: Sonar Tori
      author: Tagore
novels:
    - name: Gitanjali
      author: Tagore
    - name: Noukadubi
      author: Tagore
    - name: Les Misérables
      author: Victor Hugo

Difference/complement - and .-

A - B is a complement of B in A (or difference of A and B) iff every element in A - B is contained by A but not by B.

For performance concerns, A - B should only evaluate B within results of A.

adriank commented 6 years ago

I'm closing it because I didn't have time to think about it for a year. If you'd like to add this functionality to ObjectPath, create PR.

felixhao28 commented 6 years ago

I have created a completely new DSL to solve my problem and then moved on from the previous project. Now I don't have the incentive anymore.