Skip to main content
NIHPA Author Manuscripts logoLink to NIHPA Author Manuscripts
. Author manuscript; available in PMC: 2019 Aug 6.
Published in final edited form as: Proc ACM SIGMOD Int Conf Manag Data. 2019 Jun-Jul;2019:1961–1964. doi: 10.1145/3299869.3320236

RATest: Explaining Wrong Relational Queries Using Small Examples

Zhengjie Miao 1, Sudeepa Roy 1, Jun Yang 1
PMCID: PMC6684020  NIHMSID: NIHMS1031597  PMID: 31388247

Abstract

We present a system called RATEST, designed to help debug relational queries against reference queries and test database instances. In many applications, e.g., classroom learning and regression testing, we test the correctness of a user query Q by evaluating it over a test database instance D and comparing its result with that of evaluating a reference (correct) query Q0 over D. If Q(D) differs from Q0(D), the user knows Q is incorrect. However, D can be large (often by design), which makes debugging Q difficult. The key idea behind RATEST is to show the user a much smaller database instance D’ ⊆ D, which we call a counterexample, such that Q(D’) ≠ Q0(D’). RATEST builds on data provenance and constraint solving, and employs a suite of techniques to support, at interactive speed, complex queries involving differences and group-by aggregation. We demonstrate an application of RATEST in learning: it has been used successfully by a large undergraduate database course in a university to help students with a relational algebra assignment.

1. INTRODUCTION

In many applications, we test correctness of database queries—which we call candidate queries—by comparing their results with those of reference queries (which are deemed correct) over some reference database instance. For example, during software development, when changes are made to queries used by applications, we use regression testing to verify new queries by comparing their results against those of original queries (presumed correct) over test databases. In a classroom setting, where students learn to write relational queries, we can evaluate a student query and compare its result with that of the correct solution query over a test database instance. In these applications, a key to debugging candidate queries is the reference database instance D, which serves as a counterexample that illustrates the difference in the results of candidate query Q and reference query Q0.

Unfortunately, in practice, D is often large, either because it is based on real or production data, or it is synthesized to cover numerous corner cases and to stress-test the scalability of the queries. Such a large counterexample would take too much effort to understand where the inequivalence of the queries comes from. For example, in a classroom setting, if we want to use real data from the DBLP publication database in an assignment, D would contain millions of tuples, and the difference in query results may contain many tuples as well, easily overwhelming students.

The key idea behind our approach, implemented in a system we call RATEST, is to show the user a much smaller counterexample database instance D’ ⊆ D, such that Q(D’) ≠ Q0(D’), i.e., still enough to illustrate the difference between Q and Q0. D’ would also satisfy the same database constraints as D. Besides the small size of D’, which makes it easier to work with, this approach has a number of other advantages. First, while it is possible to generate a counterexample D’ completely unrelated to D (like [4]), constraining D’ to be a subset of D helps preserve some context for users by using the same data values and relationships from D. Also, technically, knowing that D itself is a counterexample (albeit large) helps us find small counterexamples more easily. For example, without access to D, it would be difficult to automatically generate values that satisfy one predicate involving complex SQL functions but not another. Second, our counterexample can choose to illustrate just one particular result tuple that differentiates Q and Q0, which allows users to focus on identifying and fixing one issue with their query at a time. Third, users get enough help in debugging just by seeing D’, Q(D’), and Q0(D’) (along with a description of what the query is intended to return). There is no need to reveal the reference query Q0 or the entire test database instance D; this feature is particularly useful in the classroom setting.1

We illustrate the problem of generating a small counterexample with an example.

EXAMPLE 1. Consider two relations storing information about students and the courses they took: S(name, major) and R(name, number, dept, grade). Suppose a user is asked to write a relational algebra query to find student records for those who took exactly one Computer Science (CS) course. One correct solution (reference query) Q0 is the following:

S⋈(πnameσdept=,CS,(R)−πR1.name((σdept=,CS,ρR1(R))⋈R1.name=R2.name∧R1.number≠R2.number(σdept=,CS,ρR2(R)))).

An incorrect candidate query may be the following, which actually finds students who took one or more CS courses:

Q:πname,major(S⋈σdept=,cs,R).

A reference database instance and the results of these queries over this instance are shown in Figure 1. The tuples r2 = (Mary, CS) and r3 = (Jesse, CS) are in the result of Q but not in the result of Q0. To convince the user that Q is wrong, we could provide the entirety of S and R as a counterexample comprising 11 tuples. However, a smaller counterexample simply contains three tuples, t1 t4, t5 (a subset of tuples pertaining to Mary), over which Q0 would return an empty result while Q would return just r2 (Mary). This counterexample is not only smaller, but also helps “pinpoint” the problem. For a real data-base with tens of thousands of students, there can be a vast difference between the size of the entire database and that of the smallest counterexample (which remains at 3).

Figure 1:

Figure 1:

Example relations with tuples identifiers.

In our recent work [7], we solve the problem of finding small counterexamples by building on the work in data provenance and constraint solving. We identify connections to known problems, such as minimal witness, and show why and how to adapt our problem definition for complex cases such as when queries involve aggregation. We tackle the challenges of finding the smallest counterexample at interactive speed and supporting complex queries involving differences, group-by aggregation, as well as SQL functions and operators in selection and join conditions. A detailed discussion on related work can be found in [7].

In this demonstration, we showcase a representative application of RATEST in a classroom setting, which we deployed earlier in a large undergraduate database course at Duke University. We set up RATEST for a relational algebra assignment, with a large test database and the correct solution queries, all of which are hidden from students. A student can submit candidate queries to RATEST. If a query returns an incorrect result, RATEST will show a counterexample with a small number of tuples drawn from the test instance, and show how the result of the submitted query differs from the correct one on the counterexample. The student can revise the query and get further feedback that may help reveal additional bugs, until the query returns the same result as the hidden solution query on the full hidden test instance.

2. BACKGROUND AND PROBLEM

Consider a database instance D where D |= Γ for a given set Γ of integrity constraints, and two queries Q1 and Q2 such that Q1(D) ≠ Q2(D). The smallest counterexample problem (SCP for short) is to find a D’ ⊂ D with minimum number of tuples such that D’ |= Γ and Q1(D’) ≠ Q2(D’), i.e., D’ also differentiates Q1 and Q2.2

As stated in Section 1, we connected SCP to data provenance: if Q1 and Q2 return different results over a D, then there must exist one tuple t ∈ Q1(D) \ Q2(D) or t ∈ Q2(D) \ Q1(D). We refer to the concept of witnesses [3]: a witness for a tuple t w.r.t. a query Q and database instance D is a subinstance D’ ⊂ D where t ∈ Q(D’). Therefore, for any counterexample D’ ⊆ D for which Q1(D’) ≠ Q2(D’), there exists some tuple t in (Q1 – Q2)(D’) or (Q2 – Q1)(D’), i.e., D’ is a witness of t w.r.t. Q1 – Q2 or Q2 – Q1 and D.

Thus, a reasonable approach to SCP is to consider each tuple in Q1(D) \ Q2(D) (or Q2(D) \ Q1(D)), find its smallest witness w.r.t. Q1 – Q2 (or Q2 – Q1, resp.) and D; we then pick the smallest witness overall. If Q1 and Q2 are monotone, this approach always yields the smallest counterexample [7]. In [7], we also obtained complexity results for SCP for different classes of queries. For queries involving projection, join, and difference, it is noteworthy that finding the smallest witness for a result tuple is already NP-hard in data complexity, even when the queries are of bounded sizes.

Aggregate Queries.

More challenges arise once we consider group-by aggregation queries. First, the approach of finding minimal witnesses for result tuples over D will unlikely give us the smallest counterexample. The reason is that removing a tuple from a group may change the aggregate result (e.g., of SUM); a witness that tries to preserve the original aggregate result may not be able to remove any tuple. Hence, instead of finding minimal witnesses for result tuples over D, we want a counterexample D’ that still shows some difference between Q1(D’) and Q2(D’), but the result values do not need to the same as those in Q1(D) or Q2(D).

A second challenge requires further modifying the definition of SCP, because there are cases when no counterexamples are small in size even if they do not need to preserve aggregate values. For example, suppose both Q1 and Q2 return (different) tuples from groups that pass a HAVING condition stipulating that the group size must be more than a million. Any counterexample would necessarily have to produce a group containing more than a million tuples. Nonetheless, we show in [7] how to adapt the problem definition by allowing the queries to be parameterized: e.g., the query constant “one million” in the example earlier would be replaced with a parameter λ. Then, we allow a counterexample to show difference between Q1 and Q2 for some setting of the parameter λ, e.g., a small group size threshold that does not require a large database instance to achieve. For details, see [7].

3. TECHNIQUES AND IMPLEMENTATION

Capturing Provenance.

The smallest witness of a tuple t w.r.t. a query Q and a database instance D can be obtained with how-provenance or lineage [6]. The how-provenance of aresulttuple t ∈ Q(D),denotedbyPrvQ,D(t)(orPrv(t)when the context is clear), is a formula involving Boolean variables annotating tuples in the input relations, which encodes how t is derived from input tuples in D. These Boolean variables indicate whether the corresponding tuples are present in the input, and Prv(t) correctly computes whether tuple t is in the query result given the setting of these Boolean variables. Therefore, one can map the the problem of finding the smallest witness into the min-ones satisfiability problem: find a satisfying model for Prv(t) with the least number of variables set to true; the true variables in this model would give us the tuples in a smallest witness.

We implemented RATEST based on RA(radb) [7], a relational algebra (extended with group-by and aggregation) interpreter that works by translating relational algebra queries into SQL (using WITH to build up complex queries one relational operator at a time). Taking two queries as input, RATEST uses RA(radb) to translate them into SQL, but further rewrites the SQL queries to compute provenance. Specifically, we add an extra string-valued prv column for all input and intermediate result relations to store the provenance expression. For each relational operator, we rewrite the SQL fragment generated for this operator with logic to derive output provenance expressions from input ones. All expressions are in the SMT-LIB format [2]. As an example, for the difference operator, following is the rewritten SQL query for R - S (assuming columns A and B):

(SELECT R.A, R.B, ‘(and␣ ‘ ||R.prv|| ‘␣(not␣’ | |S.prv| | ‘ )) ‘ FROM R, S WHERE R.A=S.A AND R.B=S.B) UNION (SELECT A, B, prv FROM R WHERE NOT EXISTS (SELECT * FROM S WHERE S.A=R.A AND S.B=R.B));

Note that this query computes all “potential” result tuples (that may arise when some input relation tuples are deleted).

For aggregation, we further apply provenance for aggregate queries [1]. We encode result aggregate values as symbolic expressions involving values from input relations. Then, we can express Q1(D) ≠ Q2(D) symbolically: assert that a group only exists in one of the query results (PrvQ1,D(t)⊕PrvQ2,D(t) is true), or the group exists in both results but the aggregate values differ (PrvQ1,D(t)∧PrvQ2,D(t)∧AgvQ1,D(t)≠AgvQ2,D(t), where Agv denotes the symbolic expression computing a given group’s aggregate value). This approach allows RATEST to handle SPJUDA (Select-Project-Join-Union-Difference-Aggregate) queries where there is no further grouping using aggregate values and no difference after aggregation within Q1 and Q2.

Finding Counterexample with a Solver.

Using the provenance information obtained by executing the rewritten SQL queries, we can then take a tuple in the symmetric difference of Q1(D) and Q2(D) and formulate an SMT (satisfiability modulo theories) problem to find a smallest witness. RATEST uses the Z3 SMT Solver [5], with the objective function minimizing the number of variables set to true. The satisfying model returned by the solver represents a counterexample.

EXAMPLE 2. Consider again Example 1. (Mary, CS) and (Jesse, CS) are returned by Q but not by Q0, and their how-provenance w.r.t. Q - Q0 and D can be computed, e.g.,

PrvQ−Q0,D(〈Jesse,CS〉)=PrvQ,D(〈Jesse,CS〉)∧¬PrvQ0,D(〈Jesse,CS〉)=(t3(t9+t10+t11))t3(t9+t10+t11)t9t10+t9t11+t10t11__=t3t9t10+t3t9t11+t3t10t11,

where it is clear that any one of t3t9t10, t3t9t11, and t3t10t11 is a smallest witness for (Jesse, CS). By asserting this formula to be true, we can use the SMT solver to find such a witness.

A basic approach would be to call the solver on each tuple in the symmetric difference of Q1(D) and Q2(D), and then pick the overall smallest counterexample. However, computing provenance on all result tuples incurs high overhead. Instead, we can pick one tuple t in the difference, and compute provenance just for t, by adding a selection using t’s value on top of the difference query. Pushing this selection down as much as possible through the difference query drastically reduces the cost of provenance computation. Although this approach may not give us the smallest counterexample, it can always find a counterexample, and in practice it works well: it can reduce the running time significantly (up to 42× in our experiments) while still finding counterexamples that are almost as small as the smallest possible.

Note that since the counterexample contains a subset of D’s tuples, it trivially satisfies all keys, functional dependencies, and NOT NULL constraints. However, referential constraints need explicit enforcement. We capture such constraints using Boolean formulas (involving Boolean variables corresponding to D’s tuples), and additionally assert these constraints when invoking the solver.

4. DEMONSTRATION

Figure 2 shows the web interface of RATEST. The demonstration goes through a use case of RATEST in an educational setting, where students are asked to write relational algebra queries to answer questions against a database about bars, beers, drinkers, and their relationships (bars serve beers; drinkers like beers and frequent bars); see bottom left of Figure 2. Each table contains hundreds to thousands of tuples, and the whole database contains 100,000 tuples.

Figure 2:

Figure 2:

Overview of RATEST interface.

As an example, consider one of the hardest problems in the assignment: “Find all drinkers who frequent only those bars that serve only beers they like.” The solution requires non-trivial uses of joins and differences:

πnamedrinker−πdrinker(πdrinker,beer(frequents⋈serves)−likes)

Now consider a student query

πnamedrinker−(πdrinker(frequents⋈serves)−πdrinkerlikes),

where the second input to the first difference operator is incorrect—this input would find “drinkers who frequent some bars and like no beers.” RATEST shows a small counterexample explaining this mistake (Figure 3): drinker “Frances” likes “Bender Beer” and frequents “Pizzeria Bistro Leonardo,” where only another beer “Yeti Special Export” is served. Thus, “Frances” should not be in the answer.

Figure 3:

Figure 3:

Counterexample for the query in Figure 2.

The user can revise the query and get further feedback that may reveal more bugs; a history feature allows convenient access to queries attempted recently. We also walk through the backend of RATEST for those interested.

Overall, this demonstration shows how RATEST uses small counterexamples to help users understand why their queries are wrong. We are also releasing RATEST to the public to encourage adoption by database courses at other universities.

ACKNOWLEDGMENTS

This work is supported in part by NSF Awards IIS-1408846, IIS-1552538, IIS-1703431, IIS-1718398, IIS-1814493, and by NIH award 1R01EB025021–01.

Footnotes

1

It is conceivable that the system can be abused to recover D with some effort. We do not address this issue in our current system and have not observed any such abuses in a real classroom deployment, but the problem can be an interesting direction for future work.

2

Without loss of generality, we assume that Q1 and Q2 have the same result schema (otherwise, we do not need an instance to illustrate their difference).

REFERENCES

  • [1].Amsterdamer Yael, Deutch Daniel, and Tannen Val. 2011. Provenance for aggregate queries. In PODS. 153–164. [Google Scholar]
  • [2].Barrett Clark, Stump Aaron, Tinelli Cesare, et al. 2010. The SMT-LIB standard: Version 2.0. In Proceedings of the 8th International Workshop on Satisfiability Modulo Theories, Vol. 13 14. [Google Scholar]
  • [3].Buneman Peter, Khanna Sanjeev, and Wang-Chiew Tan. 2001. Why and where: A characterization of data provenance. In ICDT. 316–330. [Google Scholar]
  • [4].Chu Shumo, Wang Chenglong, Weitz Konstantin, and Cheung Alvin. 2017. Cosette: An Automated Prover for SQL. In CIDR. [Google Scholar]
  • [5].Moura Leonardo De and Bj0rner Nikolaj. 2008. Z3: An efficient SMT solver. In International conference on Tools and Algorithms for the Construction and Analysis of Systems. 337–340. [Google Scholar]
  • [6].Green Todd J, Karvounarakis Grigoris, and Tannen Val. 2007. Provenance semirings. In PODS. 31–40. [Google Scholar]
  • [7].Miao Zhengjie, Roy Sudeepa, and Yang Jun. 2019. Explaining Wrong Queries Using Small Examples. In SIGMOD. [DOI] [PMC free article] [PubMed] [Google Scholar]

RESOURCES