SlideShare a Scribd company logo
Queries in general-purpose
  programming languages
Languages
Theory
  Relational algebra
  Monads and comprehensions

Haskell and Links
  Monad comprehensions
  Comprehensions with group and order

C# and F#
  Language integrated query (LINQ)
  Extensible query syntax in F# 3.0
Problems
Expressing queries
  What operations can we use?
  What is the right syntax for queries?
  Should the syntax be extensible?

Compiling queries
  How to turn normal code to SQL?
  How to turn normal code to efficient SQL?
  Dealing with functions in SQL code
Theory
Monads and monad
comprehensions in Haskell
Monad comprehensions

[ p.Name | p <- db.Products, p.Price > 10 ]




 db.Products `bind` (p ->
   if (p.Price > 10) then return p.Name
   else zero)
Monadic bind and joins
Join using comprehensions
  [ (p.Name, c.CategoryName)
       | p <- db.Products, c <- db.Categories
       , p.CategoryID = c.ID ]

Translation using monads
   db.Products `bind` (p ->
     db.Categories `bind` (c ->
       if (p.CategoryID = c.ID) then
         return (p.Name, c.CategoryName)
       else zero))

  Multiple uses of bind are nested
  Second data source may depend on values from first!
Comparing bind and join
Standard join
  Two independent data sources
  Equality condition on some projected values
    [ (p, c) | p <- db.Products, c <- db.Categories
             , p.CategoryID = c.ID ]

But bind allows more
  Second data source may depend on first
  Can use arbitrary conditions
    [ (p1.Name, p2.Name)
       | p1 <- Products
       , p2 <- [ p | p <- Products, p.Price > p1.Price ] ]
Adding ordering and grouping
Adding ordering and grouping

[ p | p <- db.Products, then reverse ]
[ p | p <- db.Products, then order by p.Price ]




[ p | …, then group by p.CategoryID ]
[ p | …, then group by p.CategoryID using groupAdj ]
SQL-style relational
queries in LINQ using C#
LINQ Overview
Query example
  from p in db.Products
  join c in db.Categories on p.CategoryID equals c.ID
  select new { p.Name, c.CategoryName }

Translating to SQL
  All operations have a corresponding method
   db.Products.Where(p => p.Price > 10)
   db.Products.Select(p => p.Name)

  Lambda function translated as expression tree
  Expression trees composed and processed
Expressing queries
Selection and projection
  from p in db.Products where p.Price > 10 select p
  from p in db.Products select p.Name

Ordering
  from p in db.Products orderby p.Price select p

Grouping
  from p in db.Products group p by p.CategoryID

Joins (products with selection)
  from p in db.Products
  join c in db.Categories on p.CategoryID equals c.ID
  select new { ... }
Customizing LINQ operators
Operations depend on the data source
  Syntax is translated to method calls
    from p in db.Products group p by p.CategoryID
    db.Products.GroupBy(p => p.CategoryID)
  Wrap collection and then use different operator
    from p in db.Products.WithAdjacentGrouping()
    group p by p.CategoryID

    WithAdjacentGrouping : IE<T> -> IEWithGroup<T>
    GroupBy : IEWithGroup<T> -> IEWithGroup<IEWithGroup<T>>


Various applications
  LINQ over events, Parallel implementation
Writing joins in LINQ
Syntax for joins
  Independent sources; equality condition only
    from p in db.Products
    join c in db.Categories on p.CategoryID equals c.ID
    select …

Using multiple from clauses
  Later from can depend on previous variables
  We can write arbitrary conditions
    from p in db.Products
    from c in db.Categories.Where(c => p.CategoryID == c.ID)
    where c.Name != "Stuff"
Joins and theory
Multiple from clauses
  Uses an operation similar to Bind
   SelectMany : M<S> -> (S -> M<C>) -> (S * C -> R) -> M<R>

  Can be implemented using Bind
   Bind      : M<S> -> (S -> M<R>) -> M<R>
   StrengthL : V * M<S> -> M<V * S>


Syntax for joins
  Can be defined using SelectMany, but not vice versa
    Join : M<A> -> M<B> -> (A -> K) -> (B -> K)
                -> (A * B -> R) -> M<R>

  Resembles operation of “applicative functors”
More flexible
queries in F# 3.0
Queries in F# 3.0
Design between Haskell and C#
  Can define custom operations
    query { for p in db.Products do
            orderBy p.Price
            takeWhile (p.Price > 100) }

  Here orderBy and takeWhile are not built-in

Two kinds of custom operations
  Preserve variable scope (orderBy, where, …)
  Require re-binding (groupBy)
What can be done in F# 3.0?
Better queries for OData
  Open standard for REST based queries
  Supports only subset of query operations

Expanding dependent tables explicitly
   query { for title in db.Movies do
           select title.Awards }

  Wrong! We did not say we will use Awards
   query { for title in db.Movies do
           expand title.Awards
           select title.Awards }


Other interesting areas: NoSQL, etc.
Some questions about queries
    that I’m interested in…
Possible questions
Advanced type checking
  Translating custom functions to SQL
  Limiting operations and allowed patterns (OData)

Expressivity
    What are other join-like operations?
    Aren’t monads and bind too powerful?
    Are we missing other important operations?
    What else is there than just lists…

Classical problems
  Optimizing queries, efficient compilation
References
Haskell and comprehensions
  Comprehensive Comprehensions:
   comprehensions with "Order by" and "Group by"
  Bringing Back Monad Comprehensions
  Monad Comprehensions:
   A Versatile Representation for Queries

LINQ, C# and F#
  C# Language Specification
  msdn.microsoft.com/en-us/library/bb397676.aspx
  tomasp.net/blog/idioms-in-linq.aspx

More Related Content

What's hot

Grouping & Summarizing Data in R
Grouping & Summarizing Data in RGrouping & Summarizing Data in R
Grouping & Summarizing Data in R
Jeffrey Breen
 
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Serban Tanasa
 
Pig statements
Pig statementsPig statements
Pig statements
Ganesh Sanap
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
Piyush rai
 
Basic Tutorial of Association Mapping by Avjinder Kaler
Basic Tutorial of Association Mapping by Avjinder KalerBasic Tutorial of Association Mapping by Avjinder Kaler
Basic Tutorial of Association Mapping by Avjinder Kaler
Avjinder (Avi) Kaler
 
Data Analysis in Python
Data Analysis in PythonData Analysis in Python
Data Analysis in Python
Richard Herrell
 
Text Mining with R
Text Mining with RText Mining with R
Text Mining with R
Sanjay Mishra
 
Coding T-SQL in a Team Environment
Coding T-SQL in a Team EnvironmentCoding T-SQL in a Team Environment
Coding T-SQL in a Team Environment
Mark Ginnebaugh
 
Working with Complex Types in DataFrames: Optics to the Rescue
Working with Complex Types in DataFrames: Optics to the RescueWorking with Complex Types in DataFrames: Optics to the Rescue
Working with Complex Types in DataFrames: Optics to the Rescue
Databricks
 
Manipulating Data using DPLYR in R Studio
Manipulating Data using DPLYR in R StudioManipulating Data using DPLYR in R Studio
Manipulating Data using DPLYR in R Studio
Rupak Roy
 
Genomic Selection with Bayesian Generalized Linear Regression model using R
Genomic Selection with Bayesian Generalized Linear Regression model using RGenomic Selection with Bayesian Generalized Linear Regression model using R
Genomic Selection with Bayesian Generalized Linear Regression model using R
Avjinder (Avi) Kaler
 
Reading Data into R
Reading Data into RReading Data into R
Reading Data into R
Kazuki Yoshida
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
izahn
 
R + 15 minutes = Hadoop cluster
R + 15 minutes = Hadoop clusterR + 15 minutes = Hadoop cluster
R + 15 minutes = Hadoop cluster
Jeffrey Breen
 
Python Pandas
Python PandasPython Pandas
Python Pandas
Sunil OS
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python Pandas
Neeru Mittal
 
Introduction to MongoDB at IGDTUW
Introduction to MongoDB at IGDTUWIntroduction to MongoDB at IGDTUW
Introduction to MongoDB at IGDTUW
Ankur Raina
 
Introduction to TDD (PHPunit examples)
Introduction to TDD (PHPunit examples)Introduction to TDD (PHPunit examples)
Introduction to TDD (PHPunit examples)
Matheus Marabesi
 
Introduction to Data Mining with R and Data Import/Export in R
Introduction to Data Mining with R and Data Import/Export in RIntroduction to Data Mining with R and Data Import/Export in R
Introduction to Data Mining with R and Data Import/Export in R
Yanchang Zhao
 

What's hot (19)

Grouping & Summarizing Data in R
Grouping & Summarizing Data in RGrouping & Summarizing Data in R
Grouping & Summarizing Data in R
 
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
Get up to Speed (Quick Guide to data.table in R and Pentaho PDI)
 
Pig statements
Pig statementsPig statements
Pig statements
 
Introduction to pandas
Introduction to pandasIntroduction to pandas
Introduction to pandas
 
Basic Tutorial of Association Mapping by Avjinder Kaler
Basic Tutorial of Association Mapping by Avjinder KalerBasic Tutorial of Association Mapping by Avjinder Kaler
Basic Tutorial of Association Mapping by Avjinder Kaler
 
Data Analysis in Python
Data Analysis in PythonData Analysis in Python
Data Analysis in Python
 
Text Mining with R
Text Mining with RText Mining with R
Text Mining with R
 
Coding T-SQL in a Team Environment
Coding T-SQL in a Team EnvironmentCoding T-SQL in a Team Environment
Coding T-SQL in a Team Environment
 
Working with Complex Types in DataFrames: Optics to the Rescue
Working with Complex Types in DataFrames: Optics to the RescueWorking with Complex Types in DataFrames: Optics to the Rescue
Working with Complex Types in DataFrames: Optics to the Rescue
 
Manipulating Data using DPLYR in R Studio
Manipulating Data using DPLYR in R StudioManipulating Data using DPLYR in R Studio
Manipulating Data using DPLYR in R Studio
 
Genomic Selection with Bayesian Generalized Linear Regression model using R
Genomic Selection with Bayesian Generalized Linear Regression model using RGenomic Selection with Bayesian Generalized Linear Regression model using R
Genomic Selection with Bayesian Generalized Linear Regression model using R
 
Reading Data into R
Reading Data into RReading Data into R
Reading Data into R
 
Introduction to R Programming
Introduction to R ProgrammingIntroduction to R Programming
Introduction to R Programming
 
R + 15 minutes = Hadoop cluster
R + 15 minutes = Hadoop clusterR + 15 minutes = Hadoop cluster
R + 15 minutes = Hadoop cluster
 
Python Pandas
Python PandasPython Pandas
Python Pandas
 
Data Analysis with Python Pandas
Data Analysis with Python PandasData Analysis with Python Pandas
Data Analysis with Python Pandas
 
Introduction to MongoDB at IGDTUW
Introduction to MongoDB at IGDTUWIntroduction to MongoDB at IGDTUW
Introduction to MongoDB at IGDTUW
 
Introduction to TDD (PHPunit examples)
Introduction to TDD (PHPunit examples)Introduction to TDD (PHPunit examples)
Introduction to TDD (PHPunit examples)
 
Introduction to Data Mining with R and Data Import/Export in R
Introduction to Data Mining with R and Data Import/Export in RIntroduction to Data Mining with R and Data Import/Export in R
Introduction to Data Mining with R and Data Import/Export in R
 

Viewers also liked

Docase notation for Haskell
Docase notation for HaskellDocase notation for Haskell
Docase notation for Haskell
Tomas Petricek
 
Doing data science with F#
Doing data science with F#Doing data science with F#
Doing data science with F#
Tomas Petricek
 
F# Tutorial @ QCon
F# Tutorial @ QConF# Tutorial @ QCon
F# Tutorial @ QCon
Tomas Petricek
 
Presentation Earth Saver Canvas Bags
Presentation Earth Saver Canvas BagsPresentation Earth Saver Canvas Bags
Presentation Earth Saver Canvas Bags
mcoprean
 
F# on the Server-Side
F# on the Server-SideF# on the Server-Side
F# on the Server-Side
Tomas Petricek
 
Academia
AcademiaAcademia
Academia
Tomas Petricek
 
Social Media in Small Business is Anything But Small
Social Media in Small Business is Anything But SmallSocial Media in Small Business is Anything But Small
Social Media in Small Business is Anything But Small
Mark Schmulen
 

Viewers also liked (8)

Tics
TicsTics
Tics
 
Docase notation for Haskell
Docase notation for HaskellDocase notation for Haskell
Docase notation for Haskell
 
Doing data science with F#
Doing data science with F#Doing data science with F#
Doing data science with F#
 
F# Tutorial @ QCon
F# Tutorial @ QConF# Tutorial @ QCon
F# Tutorial @ QCon
 
Presentation Earth Saver Canvas Bags
Presentation Earth Saver Canvas BagsPresentation Earth Saver Canvas Bags
Presentation Earth Saver Canvas Bags
 
F# on the Server-Side
F# on the Server-SideF# on the Server-Side
F# on the Server-Side
 
Academia
AcademiaAcademia
Academia
 
Social Media in Small Business is Anything But Small
Social Media in Small Business is Anything But SmallSocial Media in Small Business is Anything But Small
Social Media in Small Business is Anything But Small
 

Similar to Queries in general purpose languages

Document databases
Document databasesDocument databases
Document databases
Qframe
 
MongoDB
MongoDBMongoDB
MongoDB
kesavan N B
 
MongoDB World 2018: Keynote
MongoDB World 2018: KeynoteMongoDB World 2018: Keynote
MongoDB World 2018: Keynote
MongoDB
 
SF Elixir Meetup - RethinkDB
SF Elixir Meetup - RethinkDBSF Elixir Meetup - RethinkDB
SF Elixir Meetup - RethinkDB
Peter Hamilton
 
GraphQL in Symfony
GraphQL in SymfonyGraphQL in Symfony
GraphQL in Symfony
Bernd Alter
 
MongoDB Aggregation MongoSF May 2011
MongoDB Aggregation MongoSF May 2011MongoDB Aggregation MongoSF May 2011
MongoDB Aggregation MongoSF May 2011
Chris Westin
 
Introduction to MongoDB and Workshop
Introduction to MongoDB and WorkshopIntroduction to MongoDB and Workshop
Introduction to MongoDB and Workshop
AhmedabadJavaMeetup
 
Adventures in TclOO
Adventures in TclOOAdventures in TclOO
Adventures in TclOO
Donal Fellows
 
AIA101.2.Access Queries Accelerated
AIA101.2.Access Queries AcceleratedAIA101.2.Access Queries Accelerated
AIA101.2.Access Queries Accelerated
Dan D'Urso
 
Agile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tddAgile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tdd
Srinivasa GV
 
Building DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language WorkbenchBuilding DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language Workbench
Eelco Visser
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
Raghunath A
 
MongoDB World 2019: Fast Machine Learning Development with MongoDB
MongoDB World 2019: Fast Machine Learning Development with MongoDBMongoDB World 2019: Fast Machine Learning Development with MongoDB
MongoDB World 2019: Fast Machine Learning Development with MongoDB
MongoDB
 
Apache Calcite Tutorial - BOSS 21
Apache Calcite Tutorial - BOSS 21Apache Calcite Tutorial - BOSS 21
Apache Calcite Tutorial - BOSS 21
Stamatis Zampetakis
 
Functions and Arguments in Python
Functions and Arguments in PythonFunctions and Arguments in Python
Functions and Arguments in Python
Mars Devs
 
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDBMongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
Lisa Roth, PMP
 
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDBMongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB
 
MongoDB - A next-generation database that lets you create applications never ...
MongoDB - A next-generation database that lets you create applications never ...MongoDB - A next-generation database that lets you create applications never ...
MongoDB - A next-generation database that lets you create applications never ...
Ram Murat Sharma
 
Mongo learning series
Mongo learning series Mongo learning series
Mongo learning series
Prashanth Panduranga
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introduction
Tse-Ching Ho
 

Similar to Queries in general purpose languages (20)

Document databases
Document databasesDocument databases
Document databases
 
MongoDB
MongoDBMongoDB
MongoDB
 
MongoDB World 2018: Keynote
MongoDB World 2018: KeynoteMongoDB World 2018: Keynote
MongoDB World 2018: Keynote
 
SF Elixir Meetup - RethinkDB
SF Elixir Meetup - RethinkDBSF Elixir Meetup - RethinkDB
SF Elixir Meetup - RethinkDB
 
GraphQL in Symfony
GraphQL in SymfonyGraphQL in Symfony
GraphQL in Symfony
 
MongoDB Aggregation MongoSF May 2011
MongoDB Aggregation MongoSF May 2011MongoDB Aggregation MongoSF May 2011
MongoDB Aggregation MongoSF May 2011
 
Introduction to MongoDB and Workshop
Introduction to MongoDB and WorkshopIntroduction to MongoDB and Workshop
Introduction to MongoDB and Workshop
 
Adventures in TclOO
Adventures in TclOOAdventures in TclOO
Adventures in TclOO
 
AIA101.2.Access Queries Accelerated
AIA101.2.Access Queries AcceleratedAIA101.2.Access Queries Accelerated
AIA101.2.Access Queries Accelerated
 
Agile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tddAgile_goa_2013_clean_code_tdd
Agile_goa_2013_clean_code_tdd
 
Building DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language WorkbenchBuilding DSLs with the Spoofax Language Workbench
Building DSLs with the Spoofax Language Workbench
 
Introduction to MongoDB
Introduction to MongoDBIntroduction to MongoDB
Introduction to MongoDB
 
MongoDB World 2019: Fast Machine Learning Development with MongoDB
MongoDB World 2019: Fast Machine Learning Development with MongoDBMongoDB World 2019: Fast Machine Learning Development with MongoDB
MongoDB World 2019: Fast Machine Learning Development with MongoDB
 
Apache Calcite Tutorial - BOSS 21
Apache Calcite Tutorial - BOSS 21Apache Calcite Tutorial - BOSS 21
Apache Calcite Tutorial - BOSS 21
 
Functions and Arguments in Python
Functions and Arguments in PythonFunctions and Arguments in Python
Functions and Arguments in Python
 
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDBMongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
 
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDBMongoDB .local London 2019: Fast Machine Learning Development with MongoDB
MongoDB .local London 2019: Fast Machine Learning Development with MongoDB
 
MongoDB - A next-generation database that lets you create applications never ...
MongoDB - A next-generation database that lets you create applications never ...MongoDB - A next-generation database that lets you create applications never ...
MongoDB - A next-generation database that lets you create applications never ...
 
Mongo learning series
Mongo learning series Mongo learning series
Mongo learning series
 
mongodb-introduction
mongodb-introductionmongodb-introduction
mongodb-introduction
 

More from Tomas Petricek

Coeffects: A Calculus of Context-Dependent Computation
Coeffects: A Calculus of Context-Dependent ComputationCoeffects: A Calculus of Context-Dependent Computation
Coeffects: A Calculus of Context-Dependent Computation
Tomas Petricek
 
Domain Specific Languages: The Functional Way
Domain Specific Languages: The Functional WayDomain Specific Languages: The Functional Way
Domain Specific Languages: The Functional Way
Tomas Petricek
 
F# Data: Making structured data first class citizens
F# Data: Making structured data first class citizensF# Data: Making structured data first class citizens
F# Data: Making structured data first class citizens
Tomas Petricek
 
Doing data science with F# (BuildStuff)
Doing data science with F# (BuildStuff)Doing data science with F# (BuildStuff)
Doing data science with F# (BuildStuff)
Tomas Petricek
 
F# and Financial Data Making Data Analysis Simple
F# and Financial Data Making Data Analysis SimpleF# and Financial Data Making Data Analysis Simple
F# and Financial Data Making Data Analysis Simple
Tomas Petricek
 
Creating Domain Specific Languages in F#
Creating Domain Specific Languages in F#Creating Domain Specific Languages in F#
Creating Domain Specific Languages in F#
Tomas Petricek
 
How F# Learned to Stop Worrying and Love the Data
How F# Learned to Stop Worrying and Love the DataHow F# Learned to Stop Worrying and Love the Data
How F# Learned to Stop Worrying and Love the Data
Tomas Petricek
 
Information-rich programming in F# (ML Workshop 2012)
Information-rich programming in F# (ML Workshop 2012)Information-rich programming in F# (ML Workshop 2012)
Information-rich programming in F# (ML Workshop 2012)
Tomas Petricek
 
F# Type Providers in Depth
F# Type Providers in DepthF# Type Providers in Depth
F# Type Providers in Depth
Tomas Petricek
 
Asynchronous programming in F# (QCon 2012)
Asynchronous programming in F# (QCon 2012)Asynchronous programming in F# (QCon 2012)
Asynchronous programming in F# (QCon 2012)
Tomas Petricek
 
Accessing loosely structured data from F# and C#
Accessing loosely structured data from F# and C#Accessing loosely structured data from F# and C#
Accessing loosely structured data from F# and C#
Tomas Petricek
 
Teaching F#
Teaching F#Teaching F#
Teaching F#
Tomas Petricek
 
F# in MonoDevelop
F# in MonoDevelopF# in MonoDevelop
F# in MonoDevelop
Tomas Petricek
 
Concurrent programming with Agents
Concurrent programming with AgentsConcurrent programming with Agents
Concurrent programming with Agents
Tomas Petricek
 

More from Tomas Petricek (14)

Coeffects: A Calculus of Context-Dependent Computation
Coeffects: A Calculus of Context-Dependent ComputationCoeffects: A Calculus of Context-Dependent Computation
Coeffects: A Calculus of Context-Dependent Computation
 
Domain Specific Languages: The Functional Way
Domain Specific Languages: The Functional WayDomain Specific Languages: The Functional Way
Domain Specific Languages: The Functional Way
 
F# Data: Making structured data first class citizens
F# Data: Making structured data first class citizensF# Data: Making structured data first class citizens
F# Data: Making structured data first class citizens
 
Doing data science with F# (BuildStuff)
Doing data science with F# (BuildStuff)Doing data science with F# (BuildStuff)
Doing data science with F# (BuildStuff)
 
F# and Financial Data Making Data Analysis Simple
F# and Financial Data Making Data Analysis SimpleF# and Financial Data Making Data Analysis Simple
F# and Financial Data Making Data Analysis Simple
 
Creating Domain Specific Languages in F#
Creating Domain Specific Languages in F#Creating Domain Specific Languages in F#
Creating Domain Specific Languages in F#
 
How F# Learned to Stop Worrying and Love the Data
How F# Learned to Stop Worrying and Love the DataHow F# Learned to Stop Worrying and Love the Data
How F# Learned to Stop Worrying and Love the Data
 
Information-rich programming in F# (ML Workshop 2012)
Information-rich programming in F# (ML Workshop 2012)Information-rich programming in F# (ML Workshop 2012)
Information-rich programming in F# (ML Workshop 2012)
 
F# Type Providers in Depth
F# Type Providers in DepthF# Type Providers in Depth
F# Type Providers in Depth
 
Asynchronous programming in F# (QCon 2012)
Asynchronous programming in F# (QCon 2012)Asynchronous programming in F# (QCon 2012)
Asynchronous programming in F# (QCon 2012)
 
Accessing loosely structured data from F# and C#
Accessing loosely structured data from F# and C#Accessing loosely structured data from F# and C#
Accessing loosely structured data from F# and C#
 
Teaching F#
Teaching F#Teaching F#
Teaching F#
 
F# in MonoDevelop
F# in MonoDevelopF# in MonoDevelop
F# in MonoDevelop
 
Concurrent programming with Agents
Concurrent programming with AgentsConcurrent programming with Agents
Concurrent programming with Agents
 

Recently uploaded

Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
SitimaJohn
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
HarisZaheer8
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
Zilliz
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
akankshawande
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
Zilliz
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Jeffrey Haguewood
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Jeffrey Haguewood
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
Dinusha Kumarasiri
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
saastr
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
Shinana2
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
Octavian Nadolu
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Safe Software
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
MichaelKnudsen27
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
Jakub Marek
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
Pixlogix Infotech
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
innovationoecd
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
Intelisync
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
LucaBarbaro3
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Tosin Akinosho
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
Tomaz Bratanic
 

Recently uploaded (20)

Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptxOcean lotus Threat actors project by John Sitima 2024 (1).pptx
Ocean lotus Threat actors project by John Sitima 2024 (1).pptx
 
AWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptxAWS Cloud Cost Optimization Presentation.pptx
AWS Cloud Cost Optimization Presentation.pptx
 
Generating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and MilvusGenerating privacy-protected synthetic data using Secludy and Milvus
Generating privacy-protected synthetic data using Secludy and Milvus
 
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development ProvidersYour One-Stop Shop for Python Success: Top 10 US Python Development Providers
Your One-Stop Shop for Python Success: Top 10 US Python Development Providers
 
Programming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup SlidesProgramming Foundation Models with DSPy - Meetup Slides
Programming Foundation Models with DSPy - Meetup Slides
 
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
Letter and Document Automation for Bonterra Impact Management (fka Social Sol...
 
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
Salesforce Integration for Bonterra Impact Management (fka Social Solutions A...
 
Azure API Management to expose backend services securely
Azure API Management to expose backend services securelyAzure API Management to expose backend services securely
Azure API Management to expose backend services securely
 
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
Overcoming the PLG Trap: Lessons from Canva's Head of Sales & Head of EMEA Da...
 
dbms calicut university B. sc Cs 4th sem.pdf
dbms  calicut university B. sc Cs 4th sem.pdfdbms  calicut university B. sc Cs 4th sem.pdf
dbms calicut university B. sc Cs 4th sem.pdf
 
Artificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopmentArtificial Intelligence for XMLDevelopment
Artificial Intelligence for XMLDevelopment
 
Driving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success StoryDriving Business Innovation: Latest Generative AI Advancements & Success Story
Driving Business Innovation: Latest Generative AI Advancements & Success Story
 
Nordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptxNordic Marketo Engage User Group_June 13_ 2024.pptx
Nordic Marketo Engage User Group_June 13_ 2024.pptx
 
Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)Main news related to the CCS TSI 2023 (2023/1695)
Main news related to the CCS TSI 2023 (2023/1695)
 
Best 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERPBest 20 SEO Techniques To Improve Website Visibility In SERP
Best 20 SEO Techniques To Improve Website Visibility In SERP
 
Presentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of GermanyPresentation of the OECD Artificial Intelligence Review of Germany
Presentation of the OECD Artificial Intelligence Review of Germany
 
A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024A Comprehensive Guide to DeFi Development Services in 2024
A Comprehensive Guide to DeFi Development Services in 2024
 
Trusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process MiningTrusted Execution Environment for Decentralized Process Mining
Trusted Execution Environment for Decentralized Process Mining
 
Monitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdfMonitoring and Managing Anomaly Detection on OpenShift.pdf
Monitoring and Managing Anomaly Detection on OpenShift.pdf
 
GraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracyGraphRAG for Life Science to increase LLM accuracy
GraphRAG for Life Science to increase LLM accuracy
 

Queries in general purpose languages

  • 1. Queries in general-purpose programming languages
  • 2. Languages Theory  Relational algebra  Monads and comprehensions Haskell and Links  Monad comprehensions  Comprehensions with group and order C# and F#  Language integrated query (LINQ)  Extensible query syntax in F# 3.0
  • 3. Problems Expressing queries  What operations can we use?  What is the right syntax for queries?  Should the syntax be extensible? Compiling queries  How to turn normal code to SQL?  How to turn normal code to efficient SQL?  Dealing with functions in SQL code
  • 6. Monad comprehensions [ p.Name | p <- db.Products, p.Price > 10 ] db.Products `bind` (p -> if (p.Price > 10) then return p.Name else zero)
  • 7. Monadic bind and joins Join using comprehensions [ (p.Name, c.CategoryName) | p <- db.Products, c <- db.Categories , p.CategoryID = c.ID ] Translation using monads db.Products `bind` (p -> db.Categories `bind` (c -> if (p.CategoryID = c.ID) then return (p.Name, c.CategoryName) else zero))  Multiple uses of bind are nested  Second data source may depend on values from first!
  • 8. Comparing bind and join Standard join  Two independent data sources  Equality condition on some projected values [ (p, c) | p <- db.Products, c <- db.Categories , p.CategoryID = c.ID ] But bind allows more  Second data source may depend on first  Can use arbitrary conditions [ (p1.Name, p2.Name) | p1 <- Products , p2 <- [ p | p <- Products, p.Price > p1.Price ] ]
  • 10. Adding ordering and grouping [ p | p <- db.Products, then reverse ] [ p | p <- db.Products, then order by p.Price ] [ p | …, then group by p.CategoryID ] [ p | …, then group by p.CategoryID using groupAdj ]
  • 12. LINQ Overview Query example from p in db.Products join c in db.Categories on p.CategoryID equals c.ID select new { p.Name, c.CategoryName } Translating to SQL  All operations have a corresponding method db.Products.Where(p => p.Price > 10) db.Products.Select(p => p.Name)  Lambda function translated as expression tree  Expression trees composed and processed
  • 13. Expressing queries Selection and projection from p in db.Products where p.Price > 10 select p from p in db.Products select p.Name Ordering from p in db.Products orderby p.Price select p Grouping from p in db.Products group p by p.CategoryID Joins (products with selection) from p in db.Products join c in db.Categories on p.CategoryID equals c.ID select new { ... }
  • 14. Customizing LINQ operators Operations depend on the data source  Syntax is translated to method calls from p in db.Products group p by p.CategoryID db.Products.GroupBy(p => p.CategoryID)  Wrap collection and then use different operator from p in db.Products.WithAdjacentGrouping() group p by p.CategoryID WithAdjacentGrouping : IE<T> -> IEWithGroup<T> GroupBy : IEWithGroup<T> -> IEWithGroup<IEWithGroup<T>> Various applications  LINQ over events, Parallel implementation
  • 15. Writing joins in LINQ Syntax for joins  Independent sources; equality condition only from p in db.Products join c in db.Categories on p.CategoryID equals c.ID select … Using multiple from clauses  Later from can depend on previous variables  We can write arbitrary conditions from p in db.Products from c in db.Categories.Where(c => p.CategoryID == c.ID) where c.Name != "Stuff"
  • 16. Joins and theory Multiple from clauses  Uses an operation similar to Bind SelectMany : M<S> -> (S -> M<C>) -> (S * C -> R) -> M<R>  Can be implemented using Bind Bind : M<S> -> (S -> M<R>) -> M<R> StrengthL : V * M<S> -> M<V * S> Syntax for joins  Can be defined using SelectMany, but not vice versa Join : M<A> -> M<B> -> (A -> K) -> (B -> K) -> (A * B -> R) -> M<R>  Resembles operation of “applicative functors”
  • 18. Queries in F# 3.0 Design between Haskell and C#  Can define custom operations query { for p in db.Products do orderBy p.Price takeWhile (p.Price > 100) }  Here orderBy and takeWhile are not built-in Two kinds of custom operations  Preserve variable scope (orderBy, where, …)  Require re-binding (groupBy)
  • 19. What can be done in F# 3.0? Better queries for OData  Open standard for REST based queries  Supports only subset of query operations Expanding dependent tables explicitly query { for title in db.Movies do select title.Awards }  Wrong! We did not say we will use Awards query { for title in db.Movies do expand title.Awards select title.Awards } Other interesting areas: NoSQL, etc.
  • 20. Some questions about queries that I’m interested in…
  • 21. Possible questions Advanced type checking  Translating custom functions to SQL  Limiting operations and allowed patterns (OData) Expressivity  What are other join-like operations?  Aren’t monads and bind too powerful?  Are we missing other important operations?  What else is there than just lists… Classical problems  Optimizing queries, efficient compilation
  • 22. References Haskell and comprehensions  Comprehensive Comprehensions: comprehensions with "Order by" and "Group by"  Bringing Back Monad Comprehensions  Monad Comprehensions: A Versatile Representation for Queries LINQ, C# and F#  C# Language Specification  msdn.microsoft.com/en-us/library/bb397676.aspx  tomasp.net/blog/idioms-in-linq.aspx