Modelling polymorphic relationships

Is there a pattern (is it possible?) to build a mapping for a many-to-many table where one side is a polymorphic relationship? This is from a Rails database where polymorphic relations are stored as <relation>_id and <relation>_type (type being the class name).

Something like this?

id type
1 Person
1 Employee
2 Product

There are solutions but the straightforward approach performs poorly at scale. How many distinct types are there? How many distinct types in the most frequent 95% of the distribution, or, what percent of the total do the, say, 20 most common types represent?

-Paul

In this example there are just 2 distinct types, mostly of 1 type. Do you have any links to how-tos or similar on an approach?

I don't fully understand the problem but here's a simple mapping for the above table. If you can give a fuller example and describe how what you're after is different from what this does I'll see if I can provide a better answer.

PREFIX inst: <tag:inst:>
PREFIX type: <tag:type:>

MAPPING <tag:types>
FROM SQL {
  SELECT "id", "type" FROM "types_table"
}
TO {
  ?inst rdf:type ?type .
}
WHERE {
  BIND(TEMPLATE("tag:inst:{id}") AS ?inst)
  BIND(TEMPLATE("tag:type:{type}") AS ?type)
}

With the sample table above, this will translate to these triples:

  inst:1 rdf:type type:Person .
  inst:1 rdf:type type:Employee .
  inst:2 rdf:type type:Product .

This doesn't create any type hierarchy or change the relationship type between different instances, which I'm unclear about from your question.

-Paul