<?xml-model href='http://www.tei-c.org/release/xml/tei/custom/schema/relaxng/tei_all.rng' schematypens='http://relaxng.org/ns/structure/1.0'?><TEI xmlns="http://www.tei-c.org/ns/1.0">
	<teiHeader>
		<fileDesc>
			<titleStmt><title level='a'>RATest: Explaining Wrong Relational Queries Using Small Examples</title></titleStmt>
			<publicationStmt>
				<publisher></publisher>
				<date>2019 June</date>
			</publicationStmt>
			<sourceDesc>
				<bibl> 
					<idno type="par_id">10190788</idno>
					<idno type="doi">10.1145/3299869.3320236</idno>
					<title level='j'>SIGMOD '19: Proceedings of the 2019 International Conference on Management of Data</title>
<idno></idno>
<biblScope unit="volume"></biblScope>
<biblScope unit="issue"></biblScope>					

					<author>Zhengjie Miao</author><author>Sudeepa Roy</author><author>Jun Yang</author>
				</bibl>
			</sourceDesc>
		</fileDesc>
		<profileDesc>
			<abstract><ab><![CDATA[We present a system called RAT , 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 Q 0 over D. If Q(D) differs from Q 0 (D), the user knows Q is incorrect. However, D can be large (often by design), which makes debugging Q difficult. The key idea behind RAT is to show the user a much smaller database instance D ′ ⊆ D, which we call a counterexample, such thatbuilds 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 RAT in learning: it has been used successfully by a large undergraduate database course in a university to help students with a relational algebra assignment.
CCS CONCEPTS• Information systems → Database utilities and tools.]]></ab></abstract>
		</profileDesc>
	</teiHeader>
	<text><body xmlns="http://www.tei-c.org/ns/1.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xlink="http://www.w3.org/1999/xlink">
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="1">INTRODUCTION</head><p>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 Q 0 .</p><p>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.</p><p>The key idea behind our approach, implemented in a system we call RAT , is to show the user a much smaller counterexample database instance D &#8242; &#8838; D, such that Q(D &#8242; ) Q 0 (D &#8242; ), i.e., still enough to illustrate the difference between Q and Q 0 . D &#8242; would also satisfy the same database constraints as D. Besides the small size of D &#8242; , 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 &#8242; completely unrelated to D (like [4]), constraining D &#8242; 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 Q 0 , which allows users to focus on identifying and fixing one issue with their query at a time.</p><p>Third, users get enough help in debugging just by seeing D &#8242; , Q(D &#8242; ), and Q 0 (D &#8242; ) (along with a description of what the query is intended to return). There is no need to reveal the reference query Q 0 or the entire test database instance D; this feature is particularly useful in the classroom setting. <ref type="foot">1</ref>We illustrate the problem of generating a small counterexample with an example.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head>E</head><p>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) Q 0 is the following:</p><p>.</p><p>An incorrect candidate query may be the following, which actually finds students who took one or more CS courses:</p><p>A reference database instance and the results of these queries over this instance are shown in Figure <ref type="figure">1</ref>. The tuples r 2 = Mary, CS and r 3 = Jesse, CS are in the result of Q but not in the result of Q 0 . 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, t 1 , t 4 , t 5 (a subset of tuples pertaining to Mary), over which Q 0 would return an empty result while Q would return just r 2 (Mary). This counterexample is not only smaller, but also helps "pinpoint" the problem. For a real database 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).</p><p>In our recent work <ref type="bibr">[7]</ref>, 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 <ref type="bibr">[7]</ref>.</p><p>In this demonstration, we showcase a representative application of RAT in a classroom setting, which we deployed earlier in a large undergraduate database course at Duke University. We set up RAT 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 RAT . If a query returns an incorrect result, RAT 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.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="2">BACKGROUND AND PROBLEM</head><p>Consider a database instance D where D |= &#915; for a given set &#915; of integrity constraints, and two queries</p><p>As stated in Section 1, we connected SCP to data provenance: if Q 1 and Q 2 return different results over a D, then there must exist one tuple</p><p>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 &#8242; &#8838; D where t &#8712; Q(D &#8242; ). Therefore, for any counterexample</p><p>Thus, a reasonable approach to SCP is to consider each tuple in</p><p>) and D; we then pick the smallest witness overall. If Q 1 and Q 2 are monotone, this approach always yields the smallest counterexample <ref type="bibr">[7]</ref>.</p><p>In <ref type="bibr">[7]</ref>, 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.</p><p>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 &#8242; that still shows some difference between Q 1 (D &#8242; ) and Q 2 (D &#8242; ), but the result values do not need to the same as those in Q 1 (D) or Q 2 (D).</p><p>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 Q 1 and Q 2 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 <ref type="bibr">[7]</ref> 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 &#955;. Then, we allow a counterexample to show difference between Q 1 and Q 2 for some setting of the parameter &#955;, e.g., a small group size threshold that does not require a large database instance to achieve. For details, see <ref type="bibr">[7]</ref>.</p></div>
<div xmlns="http://www.tei-c.org/ns/1.0"><head n="3">TECHNIQUES AND IMPLEMENTATION</head><p>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 <ref type="bibr">[6]</ref>. The how-provenance of a result tuple t &#8712; Q(D), denoted by Prv Q, D (t) (or Prv(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.</p><p>We implemented RAT based on RA(radb) <ref type="bibr">[7]</ref>, 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, RAT 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 <ref type="bibr">[2]</ref>. As an example, for the difference operator, following is the rewritten SQL query for R -S (assuming columns A and B): Note that this query computes all "potential" result tuples (that may arise when some input relation tuples are deleted).</p><p>For aggregation, we further apply provenance for aggregate queries <ref type="bibr">[1]</ref>. We encode result aggregate values as symbolic expressions involving values from input relations. Then, we can express Q 1 (D)</p><p>Q 2 (D) symbolically: assert that a group only exists in one of the query results</p><p>, where Agv denotes the symbolic expression computing a given group's aggregate value). This approach allows RAT 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 Q 1 and Q 2 . 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 Q 1 (D) and Q 2 (D) and formulate an SMT (satisfiability modulo theories) problem to find a smallest witness. RAT 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. = t 3 (t 9 + t 10 + t 11 ) t 3 (t 9 + t 10 + t 11 ) t 3 (t 9 t 10 + t 9 t 11 + t 10 t 11 ) = t 3 t 9 t 10 + t 3 t 9 t 11 + t 3 t 10 t 11 , where it is clear that any one of t 3 t 9 t 10 , t 3 t 9 t 11 , and t 3 t 10 t 11 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.</p><p>A basic approach would be to call the solver on each tuple in the symmetric difference of Q 1 (D) and Q 2 (D), and then Demonstration SIGMOD '19, June 30-July 5, 2019, Amsterdam, Netherlands</p></div><note xmlns="http://www.tei-c.org/ns/1.0" place="foot" n="1" xml:id="foot_0"><p>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.</p></note>
			<note xmlns="http://www.tei-c.org/ns/1.0" place="foot" n="2" xml:id="foot_1"><p>Without loss of generality, we assume that Q 1 and Q 2 have the same result schema (otherwise, we do not need an instance to illustrate their difference).</p></note>
		</body>
		</text>
</TEI>
