Erlang: объединить две таблицы не по шаблону равенства - PullRequest
2 голосов
/ 01 апреля 2011

Есть ли способ реализовать

SELECT *
FROM pattern p
  JOIN tag t ON t.tag LIKE CONCAT(p.pattern, '%') AND t.type = p.type

в терминах erlang qlc поверх двух ets:

  1. [{{Pattern, Type}, Id}]
  2. [{{Tag, Type}, Id}]

I. е. Значение шаблона неравенства означает, что тег начинается с Pattern и Type = Type.

Заранее спасибо.

1 Ответ

2 голосов
/ 01 апреля 2011

Довольно просто:

1> ets:new(a,[named_table]),[ets:insert(a,X) || X<-[{{"foo", bar},12},{{"baz", quux},11}]].
[true,true]
2> ets:new(b,[named_table]),[ets:insert(b,X) || X<-[{{"foobar", bar},12},{{"bazquux", quux},11},{{"fooxxx", bar},1},{{"bazbaz", baz},11}]].
[true,true,true,true]
3> qlc:e(qlc:q([{Pattern, Type1, Tag, Id1, Id2} || {{Pattern, Type1}, Id1} <- ets:table(a), {{Tag, Type2}, Id2} <- ets:table(b), Type1 =:= Type2, lists:prefix(Pattern, Tag)])).
[{"baz",quux,"bazquux",11,11},
 {"foo",bar,"foobar",12,12},
 {"foo",bar,"fooxxx",12,1}]
...