agentm / project-m36

Project: M36 Relational Algebra Engine
The Unlicense
876 stars 47 forks source link

implement subrelation relational expression evaluation #354

Closed agentm closed 3 weeks ago

agentm commented 1 year ago

Problem Definition

TutorialD as defined by C.J. Date includes the group and ungroup operators to generate and collapse relation-valued attributes (RVAs), but does not include any means of processing RVAs. This ticket proposes to remedy that.

Firstly, Project:M36 does support atom functions which can operate on RVAs which are used for aggregations:

TutorialD (master/main): :showexpr (s group ({all but city} as x)) : {y := count(@x)}
┌──────────┬──────────────────────────────────────────────────┬──────────┐
│city::Text│x::relation {s#::Text,sname::Text,status::Integer}│y::Integer│
├──────────┼──────────────────────────────────────────────────┼──────────┤
│"Athens"  │┌────────┬───────────┬───────────────┐            │1         │
│          ││s#::Text│sname::Text│status::Integer│            │          │
│          │├────────┼───────────┼───────────────┤            │          │
│          ││"S5"    │"Adams"    │30             │            │          │
│          │└────────┴───────────┴───────────────┘            │          │
│"Paris"   │┌────────┬───────────┬───────────────┐            │2         │
│          ││s#::Text│sname::Text│status::Integer│            │          │
│          │├────────┼───────────┼───────────────┤            │          │
│          ││"S2"    │"Jones"    │10             │            │          │
│          ││"S3"    │"Blake"    │30             │            │          │
│          │└────────┴───────────┴───────────────┘            │          │
│"London"  │┌────────┬───────────┬───────────────┐            │2         │
│          ││s#::Text│sname::Text│status::Integer│            │          │
│          │├────────┼───────────┼───────────────┤            │          │
│          ││"S1"    │"Smith"    │20             │            │          │
│          ││"S4"    │"Clark"    │20             │            │          │
│          │└────────┴───────────┴───────────────┘            │          │
└──────────┴──────────────────────────────────────────────────┴──────────┘

But what about if I want to add another attribute which projects on the s# attribute- well, that can only be done when the result is completely flattened. That seems like a limitation which could be lifted. In addition, since ungroup is not

Proposed TutorialD Syntax

The new syntax will enable the current AtomExpr context to contain a relational expression which to evaluate on RVAs. For example, the expression:

(s group ({all but city} as x)) : {y := @x{s#}}

where @x references the RVA would be equivalent to:

TutorialD (master/main): :showexpr (s{city,s#} group ({all but city} as x))
┌──────────┬──────────────────────┐
│city::Text│x::relation {s#::Text}│
├──────────┼──────────────────────┤
│"Paris"   │┌────────┐            │
│          ││s#::Text│            │
│          │├────────┤            │
│          ││"S3"    │            │
│          ││"S2"    │            │
│          │└────────┘            │
│"London"  │┌────────┐            │
│          ││s#::Text│            │
│          │├────────┤            │
│          ││"S1"    │            │
│          ││"S4"    │            │
│          │└────────┘            │
│"Athens"  │┌────────┐            │
│          ││s#::Text│            │
│          │├────────┤            │
│          ││"S5"    │            │
│          │└────────┘            │
└──────────┴──────────────────────┘

The TutorialD will become slightly more complicated but I don't see any immediate disqualifying flaw or ambiguity in the syntax. We already support recursive tuple context when extending.