enspirit / bmg

Bmg, Alf's successor, A Ruby Relational Algebra
MIT License
234 stars 8 forks source link

Add Relation#transform, for easier attribute transformations than extend #20

Closed blambeau closed 4 years ago

blambeau commented 4 years ago

The commit introduces a new operator to apply shared transformations to all attributes (selective transformation is also possible, a bit like extend). The operator is inspired by Tutorial D's UPDATE op, yet using update clashes here with relvar/SQL update intuition.

TRANSFORM uses ruby semantics for now, is not compiled to SQL, and provides no optimization so far. It makes various transformation much easier than before:

# Will apply attr.to_s on every attribute
relation.transform(:to_s)
relation.transform(&:to_s)

# Will apply attr.to_s.upcase on every tuple attribute
relation.transform([:to_s, :upcase])

# Will selectively apply on attributes
relation.transform(:foo => :upcase, :bar => ->(bar){ bar*2 })

EXTEND is supposed to be used for adding attributes only, not transforming existing ones. The introduction of TRANSFORM make this clearer by providing an official alternative. The aim is to make formal logic (e.g. optimizer) slightly more powerful, through PRE strengtening (in 1.0) along those rules.

blambeau commented 4 years ago

@felixyz written with Klaro's Board2CSV/XLS improvement in mind. Strictly speaking, I tend to see it like this:

board_stories
  .project([...])         # only those wanted by the user
  .transform(:to_human)   # all dimension values presented as human readable
  .to_csv

WDYT?