Using Bmg when coming from SQL, a cheatsheet
This pages summarizes usual SQL queries and their equivalent in Bmg. Please carefully read the notes below each example, as subtle semantics differences exist between Bmg and SQL regarding: duplicate tuples, duplicate attribute names, tuple ordering and null.
And remember: Bmg truly composes. You can for instance use union
or restrict
after a group by (summarize
), use restrict
multiple times in a row, and make any other composition that would fail in SQL or with query composer libraries.
SELECT
DISTINCT
Unlike SQL, Bmg never returns duplicates, hence the absence of a .distinct
operator. Bmg’s SQL compiler will automatically introduce DISTINCT
if needed to filter duplicates.
Renaming…
Unlike SQL, Bmg does not support multiple attributes having the same name (aka duplicate attributes). Bmg’s .prefix
and .suffix
have no equivalent in SQL: you’ll have to do the job manually, as in the example above.
All but an attribute…
SQL does not support an ALLBUT
keyword. You’ll have to select all attributes but those that you want to throw away, as in the example above.
WHERE
or
or
or
Please refer to Predicate’s documentation or Bmg’s README for the full list of supported predicates.
EXISTS / IN
We discourage the usage of WHERE (NOT) IN
in SQL since its semantics in presence of NULL
may generate subtle bugs. Bmg’s matching
and not_matching
can be used everytime you’d be using IN
in SQL.
JOIN
CROSS
or
Please note that Bmg does not allow duplicate attribute names. Hence, the examples above are only equivalent if suppliers
and parts
have no attribute in common. In practice, Bmg simply ignores attributes of the right operand that already exist on the left operand, in the join result.
In other words, the following compilation actually applies:
EQUI
The same remark about duplicate attribute names applies here. In presence of conflictual attributes, the following compilation applies:
THETA
The same remark about duplicate attribute names applies here.
LEFT
Bmg allows providing an optional ‘default’ tuple for missing ones on right operand. Not using it generates NULL
/nil
like SQL, which may expose subtle bugs further down in your data transformation.
UNION
MINUS
GROUP BY
HAVING
Bmg has no special HAVING
construct, it’s as simple as using restrict
(WHERE
)
like SQL should do.
LIMIT / OFFSET
Unlike SQL, Bmg does not support limit/offset without specifying an ordering relation (SQL’s ORDER BY
), since the result is non deterministic.