Synthesizing Document Database Queries Using Collection Abstractions

Qikang Liu, Yang He, Yunyun Cai, Byeongguk Kwak, Yuepeng Wang

TL;DR

This paper introduces NOSDAQ, an automated system that synthesizes complex document database queries (like those used in MongoDB) from just a few input-output examples. It achieves this by developing a novel "collection abstraction" that captures document types and collection sizes, enabling efficient pruning of the search space and significantly speeding up query generation.

What Problem Does This Paper Address?

Document databases (e.g., MongoDB, CouchDB) are widely used due to their flexible, semi-structured data models. However, this flexibility makes writing queries challenging, especially for complex, nested data structures. Existing query synthesis techniques, often developed for structured relational databases, struggle with the unique challenges of document databases. This paper aims to bridge this gap by providing an automated way to generate these complex queries, making document databases more accessible to users.

Methods

The core of NOSDAQ is a synthesis technique that takes input-output examples and automatically generates a query. Key innovations include:

  • Domain-Specific Language (DSL): A new algebraic-style DSL is designed to express a representative set of document database queries, mirroring MongoDB's aggregation pipeline.
  • Collection Abstraction: This is the paper's main contribution. It represents a collection not just by its documents but by two key pieces of information:
  • Document Type: The structure and types of documents within the collection, potentially including placeholders for unknown attributes.
  • Collection Size Formula: A logical formula describing constraints on the collection's size.
  • Deduction Engine: This engine uses the collection abstraction to efficiently prune "infeasible" query sketches. Instead of trying to complete every partial query, it checks if the sketch could lead to a correct query by evaluating it against abstract collections. This significantly reduces the search space.
  • Iterative Synthesis: The process is iterative, involving proposing query "sketches" (partial queries), checking their feasibility via deduction, and then completing feasible sketches into full queries.

Datasets

The authors evaluated NOSDAQ on 110 benchmarks collected from four sources:

  • StackOverflow: Adapted from real-world questions and answers, focusing on common query patterns.
  • MongoDB Official Documents: Extracted from official documentation examples.
  • Twitter API Documents: Benchmarks related to tweet statistics and reply analysis.
  • Kaggle: Datasets related to scientific research, such as satellite image analysis.

The paper notes that the Twitter API and Kaggle datasets tend to be more complex due to more attributes and deeper nesting.

Key Insights and Ideas

  • Abstraction is Key to Efficiency: The most significant insight is that abstracting collections by their type and size is crucial for pruning the search space. This allows the system to quickly discard incorrect query paths without fully evaluating them.
  • Type Information is More Impactful than Size: The ablation study showed that while both type and size information in the abstraction improve efficiency, the document type information has a more substantial impact on reducing synthesis time.
  • Document Databases Present Unique Synthesis Challenges: The hierarchical and nested nature of document data, along with specialized query operators (like Group, Unwind, Lookup), makes synthesizing queries for them fundamentally different and more complex than for relational databases.
  • Small Input Sets are Sufficient: The system demonstrates effectiveness with a small number of input-output examples (1-3), making it practical for users.

Key Results

  • High Success Rate: NOSDAQ successfully synthesized 108 out of 110 benchmarks within a 5-minute time limit.
  • Efficiency: The average synthesis time was a mere 25.4 seconds (later refined to 14.2 seconds in Table III), demonstrating remarkable efficiency.
  • Comparison to Baselines: NOSDAQ significantly outperformed EUSOLVER (solving 108 vs. 25 benchmarks) and ChatGPT-4o (solving 108 vs. 77 benchmarks, with a higher rate of desired queries).
  • Robustness to Collection Size: Synthesis time was largely insensitive to input collection sizes (1-10 documents), though the rate of generating desired queries increased with slightly larger input sets (1-3 documents).
  • Qualitative Success: 107 out of 108 synthesized queries were found to be equivalent to the ground truth, with only one benchmark yielding a plausible but undesired query due to predicate complexity.

Limitations and Future Work

  • Dataset Representativeness: The evaluation is limited to the collected datasets, and performance might vary on other, unseen datasets.
  • DSL Scope: The current DSL covers a core subset of MongoDB aggregation operators. Extending it to more operators could impact performance.
  • Experimental Environment: All experiments were run on a specific machine configuration, and results might differ on other hardware.
  • Future Work: The authors suggest extending the DSL to cover more operators and exploring the impact of different machine environments. The paper also implicitly points towards the need for handling even more complex predicates and potentially incorporating more sophisticated techniques for those specific cases.

Summary updated

Abstract

Document databases are increasingly popular in various applications, but their queries are challenging to write due to the flexible and complex data model underlying document databases. This paper presents a synthesis technique that aims to generate document database queries from input-output examples automatically. A new domain-specific language is designed to express a representative set of document database queries in an algebraic style. Furthermore, the synthesis technique leverages a novel abstraction of collections for deduction to efficiently prune the search space and quickly generate the target query. An evaluation of 110 benchmarks from various sources shows that the proposed technique can synthesize 108 benchmarks successfully. On average, the synthesizer can generate document database queries from a small number of input-output examples within tens of seconds.

Empirical ResultsBrowse all benchmarks →

BenchmarkMethodMetricScoreProtocol / Evidence
Total: 110 benchmarks from StackOverflow, MongoDB Document, Twitter API, and KaggleNosdaqbenchmarks solved108 benchmarkstime-limit=5 minutes · benchmark-count=110
Source quote
L630: \multirow{4}{=}{Total} │ \multirow{4}{*}{110} │ \multirow{4}{*}{108} │ avg │ 14.2 │ 175 │ 57 │ 13
Total: 110 benchmarks from StackOverflow, MongoDB Document, Twitter API, and KaggleNosdaqaverage synthesis time14.2 stime-limit=5 minutes · benchmark-count=110
Source quote
L630: \multirow{4}{=}{Total} │ \multirow{4}{*}{110} │ \multirow{4}{*}{108} │ avg │ 14.2 │ 175 │ 57 │ 13
Synthesized queries from the 110-benchmark evaluationNosdaqqueries equivalent to the desired ones107 queriesinspection=manual · benchmark-count=110
Source quote
L647: To understand if Nosdaq\xspace can synthesize desired queries, we have manually inspected all 108 synthesized queries and found 107 of them are equivalent to the desired ones. There is only one benchmark (from StackOverflow) where Nosdaq\xspace synthesized a plausible query in terms of the example but the query is not desired.
Reported baseline / comparator measurements (2)
BenchmarkMethodMetricScoreProtocol / Evidence
110 document database query benchmarksEUSolver baselinebenchmarks solved25 benchmarkstime-limit=5 minutes · benchmark-count=110
Source quote
L669: As shown in Figure~\ref{fig:baseline}, as opposed to 108 benchmarks solved by Nosdaq\xspace, EUSolver\xspace can only solve {25} benchmarks within the 5-minute time limit due to the large search space of document database queries in general.
110 document database query benchmarksChatGPT (gpt-4o-2024-08-06) baselinebenchmarks for which the generated query was desired53 benchmarksinput=input-output examples and constants; no natural-language description · model=gpt-4o-2024-08-06 · benchmark-count=110
Source quote
L674: To compare Nosdaq\xspace with the LLM-based approach, we have used ChatGPT (version gpt-4o-2024-08-06) to synthesize all of our 110 benchmarks. Specifically, we have used the same set of input-output examples and constants in each benchmark and asked ChatGPT to generate MongoDB queries. To make fair comparisons, we did not provide additional natural language descriptions about what the query should do. The evaluation shows that GPT can only generate the desired query for 53 out of 110 benchmarks. For 24 benchmarks, the generated query is plausible but undesired, i.e., it is consistent with the examples but not equivalent to the desired one. For the remaining 33 benchmarks, the generated query is inconsistent with the input-output examples. The errors made by GPT include misunderstanding the semantics of operators, missing predicates, etc. Recall that Nosdaq\xspace can synthesize desired queries for 107 benchmarks and plausible but undesired query for 1 benchmark. We believe our synthesis technique is more effective and generalizable than GPT to synthesize document database queries from examples.
BibTeX
@misc{liu2024synthesizing,
  author = {Qikang Liu and Yang He and Yunyun Cai and Byeongguk Kwak and Yuepeng Wang},
  title = {{Synthesizing Document Database Queries Using Collection Abstractions}},
  year = {2024},
  eprint = {2412.06102},
  archivePrefix = {arXiv},
  url = {https://arxiv.org/abs/2412.06102},
}

Loading related content...

Figure