Safe Database Abstractions with Type-Level Record Computation Adam Chlipala RADICAL 2010.

Slides:



Advertisements
Similar presentations
Sml2java a source to source translator Justin Koser, Haakon Larsen, Jeffrey Vaughan PLI 2003 DP-COOL.
Advertisements

Introduction to Compilation of Functional Languages Wanhe Zhang Computing and Software Department McMaster University 16 th, March, 2004.
Type Checking, Inference, & Elaboration CS153: Compilers Greg Morrisett.
1 Mooly Sagiv and Greta Yorsh School of Computer Science Tel-Aviv University Modern Compiler Design.
Recap 1.Programmer enters expression 2.ML checks if expression is “well-typed” Using a precise set of rules, ML tries to find a unique type for the expression.
Winter Compiler Construction T7 – semantic analysis part II type-checking Mooly Sagiv and Roman Manevich School of Computer Science Tel-Aviv.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
INF 212 ANALYSIS OF PROG. LANGS Type Systems Instructors: Crista Lopes Copyright © Instructors.
Cse321, Programming Languages and Compilers 1 6/19/2015 Lecture #18, March 14, 2007 Syntax directed translations, Meanings of programs, Rules for writing.
1 Control Flow Analysis Mooly Sagiv Tel Aviv University Textbook Chapter 3
Introduction to ML Last time: Basics: integers, Booleans, tuples,... simple functions introduction to data types This time, we continue writing an evaluator.
Modules in UHC A proposal by: Tom Hofte & Eric Eijkelenboom.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 6: Higher-Order Functions, Polymorphism.
CSE 130 : Winter 2006 Programming Languages Ranjit Jhala UC San Diego Lecture 7: Polymorphism.
Higher Grade Computing Studies 2. Languages and Environments Higher Computing Software Development S. McCrossan 1 Classification of Languages 1. Procedural.
Chapter 7 Advanced SQL Database Systems: Design, Implementation, and Management, Sixth Edition, Rob and Coronel.
Putting it all together: LINQ as an Example. The Problem: SQL in Code Programs often connect to database servers. Database servers only “speak” SQL. Programs.
Using SAS® Information Map Studio
HSCI 709 SQL Data Definition Language. SQL Standard SQL-92 was developed by the INCITS Technical Committee H2 on Databases. SQL-92 was designed to be.
CSC 580 – Theory of Programming Languages, Spring, 2009 Week 9: Functional Languages ML and Haskell, Dr. Dale E. Parson.
8 1 Chapter 8 Advanced SQL Database Systems: Design, Implementation, and Management, Seventh Edition, Rob and Coronel.
Database Systems Design, Implementation, and Management Coronel | Morris 11e ©2015 Cengage Learning. All Rights Reserved. May not be scanned, copied or.
WEB SECURITY WEEK 2 Computer Security Group University of Texas at Dallas.
SQL DOM: Compile Time Checking of Dynamic SQL Statements Russel A. McClure Ingolf H. Krüger ICSE 2005 University of California, San Diego Department of.
QUERY PROCESSING RELATIONAL DATABASE KUSUMA AYU LAKSITOWENING
Joey Paquet, 2000, 2002, 2008, Lecture 8 Syntax-Directed Translation.
(SQL - Structured Query Language)
Error Example - 65/4; ! Toplevel input: ! 65/4; ! ^^ ! Type clash: expression of type ! int ! cannot have type ! real.
CSE 130 : Spring 2011 Programming Languages Ranjit Jhala UC San Diego Lecture 5: Functions and Closures.
In this session, you will learn to: Query data by using joins Query data by using subqueries Objectives.
The Execution System1. 2 Introduction Managed code and managed data qualify code or data that executes in cooperation with the execution engine The execution.
Select Complex Queries Database Management Fundamentals LESSON 3.1b.
Arvind Computer Science and Artificial Intelligence Laboratory M.I.T. L05-1 September 21, 2006http:// Types and Simple Type.
Dr. M. Al-Mulhem Introduction 1 Chapter 6 Type Systems.
Feature-Level Modularity in Ur/Web Adam Chlipala WG 2.16 meeting, August 2013.
Static Checking of Dynamically-Varying Security Policies in Database-Backed Applications Adam Chlipala OSDI 2010.
Ur/Web, a Domain-Specific Functional Programming Language for Modern Web Applications Adam Chlipala.
Heath Carroll Bill Hanczaryk Rich Porter.  A Theory of Type Polymorphism in Programming ◦ Robin Milner (1977)  Milner credited with introducing the.
Ur/Web: A Statically-Typed Language for Building Web Applications from Components Adam Chlipala Emerging Languages Camp 2010.
Ur: Statically-Typed Metaprogramming with Type-Level Record Computation Adam Chlipala PLDI 2010.
Chapter 1. Introduction.
Functional Programming
Type Checking and Type Inference
Chapter 2 .Computational Models
Table spaces.
Tuning Transact-SQL Queries
Generating Pieces of Web Applications with Type-Level Programming
PRINCIPLES OF COMPILER DESIGN
Prepared by : Moshira M. Ali CS490 Coordinator Arab Open University
6.001 SICP Compilation Context: special purpose vs. universal machines
Database Systems: Design, Implementation, and Management Tenth Edition
ITEC 313 Database Programming
Parameter Sniffing in SQL Server Stored Procedures
Static Checking of Dynamically-Varying Security Policies in Database-Backed Applications Adam Chlipala OSDI 2010.
Lec 3: Object-Oriented Data Modeling
Lecturer: Mukhtar Mohamed Ali “Hakaale”
FP Foundations, Scheme In Text: Chapter 14.
CS179G, Project In Computer Science
Database systems Lecture 3 – SQL + CRUD
SQL Fundamentals in Three Hours
Query Optimization CS 157B Ch. 14 Mien Siao.
CSE 341 Section 5 Winter 2018.
CMPT 354: Database System I
Contents Preface I Introduction Lesson Objectives I-2
Chapter 8 Advanced SQL.
Environments, Stores, and Interpreters
Dynamic Sql Not so scary?
Database Systems: Design, Implementation, and Management Tenth Edition
Topic 12 Lesson 2 – Retrieving Data with Queries
CSE 341 Lecture 11 b closures; scoping rules
Presentation transcript:

Safe Database Abstractions with Type-Level Record Computation Adam Chlipala RADICAL 2010

2 In the Wild... Written in a general- purpose programming language SELECT Id FROM User WHERE Name = 'foo' query(“SELECT Id FROM User WHERE Id = '“ + escape(name) + “'”)

3 Language-Integrated Query u <- User; filter(u.Name = name); return u.Id SELECT Id FROM User WHERE Name = 'foo' Query Compiler

4 Language or Library? u <- User; filter(u.Name = name); return u.Id SELECT Id FROM User WHERE Name = 'foo' Query Compi ler Query Compiler Library? ● Programmers can add support for new query languages, without touching the main compiler. ● Can take full advantage of the target language, by building new libraries as needed. Language? ● Static checking of query syntax ● Static guarantee that every query is compiled properly ● Compile-time optimization removes interpretation overhead. Bridging the gap? How do we know this is done right?

5 First-Class Queries in Ur/Web Ur A general-purpose language based on ML, Haskell, and Coq Expressive type system supporting type-level computation with records Ur/Web standard library Syntax and typing of SQL as a module system signature Ur/Web compiler SQL-specific optimization

6 Safe Abstractions via Types SELECT Id FROM User WHERE Name = {name} u <- User; filter(u.Name = name); return u.Id Comprehension Library u <- User; filter(u.Name = name); return u.Id bind [#U] (from [#U] user) (seq (filter (eq (field [#U] [#Name]) (const name)) (select {Id = field [#U] [#Id]}))

7 Optimization for Free SELECT Id FROM User WHERE Name = {name} Comprehension Library bind [#U] (from [#U] user) (seq (filter (eq (field [#U] [#Name]) (const name)) (select {Id = field [#U] [#Id]})) SELECT Id FROM User WHERE Name = $1 1. Compile prepared statement once $1 = “foo” 2. Execute with parameters 3. Optimized code iterates over results (often without any heap allocation)

8 Type Inference for Free SELECT Id FROM User WHERE Name = {name} select {From = {U = user}, Where = eq (field [#U] [#Name]) (const name), Select = pick {U = subset [[Id = _]]}} Syntax-directed translation Generic type inference engine Fully-annotated program

9 Typing SELECT val select : full :: {{Type}} -> keep :: {{Type}} -> {From : $(map sql_table full), Where : sql_exp full bool, Select : pick keep full} -> sql_query keep First class polymorphism with higher kinds The kind of records of records of types Type-level map over records Richly-typed abstract syntaxLightweight “proofs”

10 SQL Expression Syntax type sql_exp :: {{Type}} -> Type -> Type val const : ts :: {{Type}} -> t :: Type -> sql t -> t -> sql_exp ts t val eq : ts :: {{Type}} -> t :: Type -> sql_exp ts t -> sql_exp ts t -> sql_exp ts bool val field : tn :: Name -> nm :: Name -> t :: Type -> fs :: {Type} -> ts :: {{Type}} -> [[tn] ~ ts] => [[nm] ~ fs] => sql_exp ([tn = [nm = t] ++ fs] ++ ts) t Typing environment Expression type, according to SQL typing rules Type class witness Record disjointness constraints Type-level computation with records First-class, type-level names

11 Implementing Comprehensions type exp (env :: {{Type}}) (t :: Type) = ts :: {{Type}} -> $(map (fn r => $(map (sql_exp ts) r)) env) -> sql_exp ts t x1 <-....; x2 <-....;.... xn <-....; return.... from_table t SELECT * FROM..., t,... y1 <-....;.... ym <-....; return.... x1 x2... xn x1.A + 2 * x2.B t3.Xt3.Yt1.A t1.C t2.B + 6 t1.A1t2.B

12 Supported SQL Features ● Inner and outer joins ● Grouping and aggregation ● Relational operators (union, intersection,...) ● Subqueries ● Sorting of results (“ORDER BY”) ● Table constraints (foreign keys,...) ● Views ● Insert/update/delete

13 Checking Security Policies Table XTable Y Policy: SELECT Id, Name FROM X WHERE Public Public F T F T IdNameId Policy: SELECT Y.* FROM Y, Acl, User WHERE Y_id = Y.Id AND Usr = User.Id AND known(User.Pass) Access Control List UsrY_id Id User: 42 Password: foo known = {42, “foo”}* HTTP Request Static Verifier SMT Solver ● Equality ● Functions ● Datatypes ● “known” ● Functional dependencies

14 Ur/Web Available At: Including online demos with syntax-highlighted source code

15 Smart Type Inference fun foo [ts :: {{Type} * Type}] (fl : folder ts) (tabs : $(map (fn (fs, _) => sql_table fs) ts)) (funcs : $(map (fn (fs, t) => $fs -> t) ts)) : list $(map (fn (_, t) => t) ts) =.... select {From = (* build record from tabs *),...}.... $(map (fn (fs, _) => sql_table fs) ts) val select : full :: {{Type}} -> keep :: {{Type}} -> {From : $(map sql_table full),...} -> sql_query keep $(map sql_table (map (fn (fs, _) => fs) ts)) The Ur inference engine must apply a fusion law! Example usage: foo {X = t1, Y = t2} {X = fn r => r.A, Y = fn r => r.B + r.C}