Compiling Mappings to Bridge Applications and Databases��SIGMOD 2007, Beijing
Sergey Melnik, Atul Adya, Phil Bernstein
{melnik | adya | philbe}@microsoft.com
Microsoft Corporation, USA
Outline
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
2
Motivation
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
3
Objects
Relationaldatabase
Business logic
O/R Mismatch: State of the Art
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
4
Data Mapping Layers
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
5
Sample Scenario
Client
Id Name Score ...
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
Person
Id
Name
HR
Id Name
Empl
Id Dept
6
6
Employee
Dept
Customer
BillingAddr
CreditScore
People = { }
var result =� from p in People
where p.Name == "Alice"
select p;
E.g., LINQ�in .NET:
Application schema�≠ DB schema
Assembling Entities from Tables
People = � SELECT VALUE � CASE� WHEN (T5._from2 AND NOT T5._from1) THEN Person(T5.Id, T5.Name) � WHEN (T5._from1 AND T5._from2) � THEN Employee(T5.Id, T5.Name, T5.Dept)� ELSE Customer(T5.Id, T5.Name, T5.CreditScore, T5.BillingAddr)� END� FROM ( (SELECT T1.Id, T1.Name, T2.Dept, � CAST (NULL AS int) AS CreditScore, � CAST (NULL AS nvarchar) AS BillingAddr, False AS _from0, � (T2._from1 AND T2._from1 IS NOT NULL) AS _from1, T1._from2� FROM ( SELECT T.Id, T.Name, True AS _from2� FROM HR AS T) AS T1� LEFT OUTER JOIN (� SELECT T.Id, T.Dept, True AS _from1� FROM Empl AS T) AS T2� ON T1.Id = T2.Id )� UNION ALL (� SELECT T.Id, T.Name, � CAST (NULL AS nvarchar) AS Dept, � T.Score AS CreditScore, T.Addr AS BillingAddr, � True AS _from0, False AS _from1, False AS _from2� FROM Client AS T)� ) AS T5
7
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
Problem 1: Update Execution
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
8
Insight: Update Mechanism
∆Entities
Entities
Entities + ∆Entities
Query views
Update views
virtual state
persistent�data
∆Tables ?
Tables
Tables + ∆Tables
9
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
Example: Update Translation
10
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
Client
Id Name Score ...
HR
Id Name
Empl
Id Dept
U
HR = SELECT p.Id, p.Name FROM People p� WHERE p IS OF (ONLY Person) OR� p IS OF (ONLY Employee)
Empl = SELECT e.Id, e.Dept FROM People e
WHERE e IS OF Employee
Client = SELECT c.Id, c.Name, ... FROM People c
WHERE c IS OF Customer
Person
Id
Name
Employee
Dept
Customer
BillingAddr
CreditScore
People = { }
Example: Update Translation
11
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
∆People =�{� Person(1, "Alice"),� Employee(2, "Bob", "R&D")�}
∆Tables
∆Entities
= { (1, "Alice"),� (2, "Bob") }
= { (2, "R&D") }
Client
Id Name Score ...
HR
Id Name
Empl
Id Dept
U
HR = SELECT p.Id, p.Name FROM People p� WHERE p IS OF (ONLY Person) OR� p IS OF (ONLY Employee)
Empl = SELECT e.Id, e.Dept FROM People e
WHERE e IS OF Employee
Client = SELECT c.Id, c.Name, ... FROM People c
WHERE c IS OF Customer
= { }
∆U
∆HR = SELECT p.Id, p.Name FROM ∆People p� WHERE p IS OF (ONLY Person) OR� p IS OF (ONLY Employee)
∆Empl = SELECT e.Id, e.Dept FROM ∆People e
WHERE e IS OF Employee
∆Client = SELECT c.Id, c.Name, ... FROM ∆People c
WHERE c IS OF Customer
Problem 2: Specifying Mappings
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
12
@Entity
@Table(name="HR")
@SecondaryTables({
@SecondaryTable(name="Empl",
pkJoinColumns=@PrimaryKeyJoinColumn(name="Id")) })
public class Employee : Person { ... }
@Entity ...
public class Customer : Person { ... }
@Entity ...
public class Person { ... }
"Specifying one or more secondary tables indicates that the data for the entity class is stored across multiple tables."
"If the mapping is incompletely specified, the JDO implementation might silently use mapping defaults or throw an exception."
UNSUPPORTED
Insight: �Declarative Mapping Language
13
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
Example: Mapping Fragments
Client
Id Name Score ...
SELECT p.Id, p.Name FROM People p�WHERE p IS OF (ONLY Person) OR� p IS OF (ONLY Employee)
SELECT Id, Name
FROM HR
=
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
Person
Id
Name
HR
Id Name
Empl
Id Dept
14
14
SELECT e.Id, e.Dept FROM People e
WHERE e IS OF Employee
SELECT Id, Dept
FROM Empl
=
Employee
Dept
Customer
BillingAddr
CreditScore
SELECT c.Id, c.Name, ... FROM People c
WHERE c IS OF Customer
SELECT Id, Name, ...
FROM Client
=
People = { }
QE1
QE2
QE3
QT1
QT2
QT3
Fragment views
Problem 3:�Tie Specification to Execution
Query views
Update views
Mapping
?
15
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
Outline
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
16
Insight: Mapping Compilation
17
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
Query views
Update views
Mapping
compile
Problem Statement
For a given map, construct views Q and U expressed in some language L, such that U ⊆ map ⊆ Q-1, or show that such views do not exist
18
map
Entities
Tables
Query views
Update views
Bipartite Mappings
19
f
g
map
f′
g′
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
People = Rewriting(QE1, QE2, QE3)
= Rewriting(QT1, QT2, QT3)
Entities
Tables
f′:
f′ ◦ g:
= f ◦ g-1
f
g
Rewriting Algorithm
20
P1 : σe IS OF (ONLY Person) ∪a
P2 : σe IS OF (ONLY Customer) ∪a
P3 : σe IS OF (ONLY Employee)
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
⋈
⊐
⊏
⋈
⊐
⋈
⊐
⊏
⋉
Outline
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
21
ADO.NET Entity Framework
Entities�(conceptual schema based on EER)
Relationaldatabase
Mapping
var recentHires =� from p in db.SalesPeople
where p.HireDate.Year > 2000
select p;
"SELECT p
FROM SalesPeople AS p
WHERE p.HireDate.Year > 2000"
Objects
(C#, VB)
Users
Applications
Tools
22
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
Architecture
Merge views
23
Mapping
Query pipeline
Update pipeline
Mapping compiler
Assemble �entities
Object services
Apply view maintenance to bidirectional views
Language integration
Runtime
Design time
Data providers
∆Entities
∆Tables
Entity SQL
Algebraic trees
Entities
Tuples
Database system
API
generator
UI Tools
Query
views
Update
views
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
Adya, Blakeley, Melnik, Muralidhar & ADO.NET Team (SIGMOD'07)
Evaluation
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
24
Related Work
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
25
Surveys
Contributions: Principled Approach
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
26
Mapping
compile
Bidirectional�views
Research Challenges (see Bernstein's keynote)
27
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
More Information (SIGMOD'07)
S. Melnik, A. Adya, P. A. Bernstein: Compiling Mappings to Bridge Applications and Databases, SIGMOD 2007
28