How Query Engines Work
How Query Engines Work
An Introductory Guide
About the Book
Andy Grove has worked on numerous projects that required custom query engines or integrations with existing query engines and this book provides an approachable introduction to the topic.
The book provides an introduction to the high-level concepts behind query engines and walks through every step of building a SQL query engine in Kotlin with full source code available in a companion github repository. Most of the book is programming language agnostic and Kotlin was chosen for the code examples due to its conciseness and readability. The concepts should be easily translatable to other programming languages.
Andy is a PMC member of Apache Arrow where he donated the initial Rust implementation and later donated the DataFusion query engine.
Please note that this is a short introductory book (around 100 pages). Around 4% of readers ask for a refund because they were expecting something far more comprehensive.
Table of Contents
- Acknowledgments
-
Preface
- Feedback
-
Introduction
- Who This Book Is For
- What You Will Learn
- How This Book Is Organized
-
The KQuery Project
- Why Kotlin?
- Repository Structure
- Building the Project
- Running Examples
-
1 What Is a Query Engine?
- 1.1 From Code to Queries
- 1.2 Anatomy of a Query Engine
- 1.3 A Concrete Example
- 1.4 SQL: The Universal Query Language
- 1.5 Beyond SQL: DataFrame APIs
- 1.6 Why Build a Query Engine?
- 1.7 What This Book Covers
-
2 Apache Arrow
- 2.1 Why Columnar?
- 2.2 What Is Apache Arrow?
- 2.3 Arrow Memory Layout
- 2.4 Record Batches
- 2.5 Schemas and Types
- 2.6 Language Implementations
- 2.7 Why Arrow for Our Query Engine?
- 2.8 Further Reading
-
3 Type System
- 3.1 Why Types Matter
- 3.2 Building on Arrow
- 3.3 Schemas and Fields
- 3.4 Column Vectors
- 3.5 Literal Values
- 3.6 Record Batches
- 3.7 Type Coercion
- 3.8 Putting It Together
-
4 Data Sources
- 4.1 Why Abstract Data Sources?
- 4.2 The DataSource Interface
- 4.3 CSV Data Source
- 4.4 Parquet Data Source
- 4.5 In-Memory Data Source
- 4.6 Other Data Sources
- 4.7 Schema-less Sources
- 4.8 Connecting Data Sources to the Query Engine
-
5 Logical Plans and Expressions
- 5.1 Why Separate Logical from Physical?
- 5.2 The LogicalPlan Interface
- 5.3 Printing Logical Plans
- 5.4 Logical Expressions
- 5.5 The LogicalExpr Interface
- 5.6 Column Expressions
- 5.7 Literal Expressions
- 5.8 Binary Expressions
- 5.9 Aggregate Expressions
- 5.10 Aliased Expressions
- 5.11 Logical Plans
- 5.12 Putting It Together
- 5.13 Serialization
-
6 DataFrame API
- 6.1 Building Plans The Hard Way
- 6.2 The DataFrame Approach
- 6.3 The DataFrame Interface
- 6.4 Implementation
- 6.5 Execution Context
- 6.6 Convenience Methods
- 6.7 DataFrames vs SQL
- 6.8 The Underlying Plan
-
7 SQL Support
- 7.1 The Journey from SQL to Logical Plan
- 7.2 Tokenizing
- 7.3 Parsing with Pratt Parsers
- 7.4 SQL Expressions
- 7.5 Precedence in Action
- 7.6 Parsing SELECT Statements
- 7.7 SQL Planning: The Hard Part
- 7.8 Aggregate Queries
- 7.9 Why Build Your Own Parser?
-
8 Physical Plans and Expressions
- 8.1 Why Separate Physical from Logical?
- 8.2 The PhysicalPlan Interface
- 8.3 Physical Expressions
- 8.4 Physical Plans
- 8.5 Execution Model
- 8.6 Next Steps
-
9 Query Planner
- 9.1 What the Query Planner Does
- 9.2 The QueryPlanner Class
- 9.3 Translating Expressions
- 9.4 Translating Plans
- 9.5 A Complete Example
- 9.6 Where Optimization Fits
- 9.7 Error Handling
-
10 Joins
- 10.1 Join Types
- 10.2 Join Conditions
- 10.3 Join Algorithms
- 10.4 Hash Join in Detail
- 10.5 Join Ordering
- 10.6 Bloom Filters
- 10.7 Summary
-
11 Subqueries
- 11.1 Types of Subqueries
- 11.2 Planning Subqueries
- 11.3 Implementation Complexity
- 11.4 When Decorrelation Is Not Possible
-
12 Query Optimizations
- 12.1 Why Optimize?
- 12.2 Rule-Based Optimization
- 12.3 Projection Push-Down
- 12.4 Predicate Push-Down
- 12.5 Eliminate Common Subexpressions
- 12.6 Cost-Based Optimization
- 12.7 Other Optimizations
-
13 Query Execution
- 13.1 The Execution Context
- 13.2 The Execution Pipeline
- 13.3 Running a Query
- 13.4 Lazy Evaluation
- 13.5 Consuming Results
- 13.6 Example: NYC Taxi Data
- 13.7 The Impact of Optimization
- 13.8 Comparison with Apache Spark
- 13.9 Error Handling
- 13.10 What We Have Built
-
14 Parallel Query Execution
- 14.1 Why Parallelism Helps
- 14.2 Data Parallelism
- 14.3 A Practical Example
- 14.4 Combining Results
- 14.5 Partitioning Strategies
- 14.6 Partition Pruning
- 14.7 Parallel Joins
- 14.8 Repartitioning and Exchange
- 14.9 Limits of Parallelism
-
15 Distributed Query Execution
- 15.1 When to Go Distributed
- 15.2 Architecture Overview
- 15.3 Embarrassingly Parallel Operators
- 15.4 Distributed Aggregates
- 15.5 Distributed Joins
- 15.6 Query Stages
- 15.7 Producing a Distributed Query Plan
- 15.8 Serializing a Query Plan
- 15.9 Serializing Data
- 15.10 Choosing a Protocol
- 15.11 Streaming vs Blocking Operators
- 15.12 Data Locality
- 15.13 Fault Tolerance
- 15.14 Custom Code
- 15.15 Distributed Query Optimizations
-
16 Testing
- 16.1 Unit Testing
- 16.2 Integration Testing
- 16.3 Fuzzing
-
17 Benchmarks
- 17.1 Measuring Performance
- 17.2 Measuring Scalability
- 17.3 Concurrency
- 17.4 Automation
- 17.5 Comparing Benchmarks
- 17.6 Publishing Benchmark Results
- 17.7 Transaction Processing Council (TPC) Benchmarks
-
Further Resources
- Open-Source Projects
- YouTube
- Sample Data
The Leanpub 60 Day 100% Happiness Guarantee
Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.
Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.
You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!
So, there's no reason not to click the Add to Cart button, is there?
See full terms...
Earn $8 on a $10 Purchase, and $16 on a $20 Purchase
We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.
(Yes, some authors have already earned much more than that on Leanpub.)
In fact, authors have earnedover $14 millionwriting, publishing and selling on Leanpub.
Learn more about writing on Leanpub
Free Updates. DRM Free.
If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).
Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.
Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.
Learn more about Leanpub's ebook formats and where to read them