Compare Two DBQC Queries

DBQC can compare two query definitions without connecting to your database. Use it to turn a refactor question into testable evidence before execution: check whether behavior is preserved, compare generated queries, or produce a small counterexample for a code review.

Query comparison comes after DBQC's single-query safety checks: safety checks validate one query, while comparison asks whether two valid queries always return the same result.

The result has three possible verdicts:

Verdict Meaning
equivalent DBQC found a sound proof that the queries return the same result.
not_equivalent DBQC found a concrete database instance on which the results differ.
undecidable DBQC found neither a proof nor a counterexample.

undecidable is a normal result. It does not mean that the queries differ, and it must not be treated as equivalent.

A large query graph passes through a validation gateway that captures risky changes before a controlled database deployment.

Endpoint and Authentication

Send a POST request to:

https://api.dbqc.volvwealth.com/compare

Use the same x-api-key as the compile endpoint. Comparison requests use the same API Gateway usage plan, throttling, and quota as compilation requests.

The request body contains two DBQC Source trees named left and right:

{
  "left": {
    "type": "table",
    "name": "items",
    "heading": [
      {"name": "id", "type": "int4"}
    ],
    "dbSchema": "public"
  },
  "right": {
    "type": "distinct",
    "input": {
      "type": "table",
      "name": "items",
      "heading": [
        {"name": "id", "type": "int4"}
      ],
      "dbSchema": "public"
    },
    "on": []
  }
}

Save that body as comparison.json, then call the API:

curl -X POST https://api.dbqc.volvwealth.com/compare \
  -H "Content-Type: application/json" \
  -H "x-api-key: $DBQC_KEY" \
  --data @comparison.json

Both sides use the same JSON format documented in the API Guide. Send raw Source trees, not compile-request wrappers with a target dialect.

Reading the Response

For a valid comparison, comparison contains the verdict, a summary, supporting evidence, and an optional counterexample. errors is empty:

{
  "comparison": {
    "verdict": "not_equivalent",
    "summary": "a generated database instance produces different results",
    "evidence": [
      "bounded relational-model search found a concrete witness"
    ],
    "counterexample": {
      "relations": [
        {
          "dbSchema": "public",
          "name": "items",
          "schema": ["id:int4"],
          "rows": [["-1"], ["-1"]]
        }
      ],
      "leftResult": {
        "schema": ["id:int4"],
        "rows": [["-1"], ["-1"]]
      },
      "rightResult": {
        "schema": ["id:int4"],
        "rows": [["-1"]]
      }
    }
  },
  "errors": []
}

This counterexample shows why items and DISTINCT(items) are not equivalent under SQL bag semantics: when the table contains a duplicate row, the left query keeps both rows and the right query removes one.

Every not_equivalent result includes a counterexample. Its relations are the synthetic input tables, and leftResult and rightResult show the differing outputs. Values are rendered as strings so the fixture is readable and portable across clients.

For equivalent and undecidable, counterexample is null. Read summary and evidence to see which proof succeeded or why no decision was possible.

Errors and HTTP Status

After API-key authentication succeeds, the endpoint follows the compile API's always-200 response convention. A body that cannot be decoded as two DBQC Source trees returns:

{
  "comparison": null,
  "errors": ["description of the invalid request"]
}

Always inspect errors before reading comparison. A missing or invalid API key is rejected by API Gateway with HTTP 403 before the request reaches DBQC.

What DBQC Can Prove

The comparator currently proves equivalence using conservative rules including:

It proves non-equivalence using:

The counterexample search supports tables, literal relations, filters, projections, full-row distinct, union and union-all, transparent named and locked wrappers, and conservative scalar comparisons. It includes duplicate rows so it can expose differences between bag and set semantics.

Limits and Soundness

Query equivalence is undecidable in general, so DBQC deliberately refuses to guess. Unsupported operations, ambiguous scalar types, or a bounded search that finds no witness produce undecidable.

In particular:

This conservative contract makes the verdicts safe to automate: accept equivalent as a proof, use not_equivalent as a reproducible failure, and route undecidable to review or additional testing.

Local Command-Line Use

Repository contributors can run QueryComparatorMain with two JSON files. Each file may contain a raw DBQC Source or a CompileRequest wrapper. The command prints the verdict, evidence, and any counterexample fixture with both outputs.

Exit status is 0 for equivalent, 1 for not equivalent, 2 for undecidable, and 64 for invalid input.

Design Background

The design follows the proof-oriented approach in Ryan Wisnesky et al.'s Toward a Verified Relational Database Management System: translate queries to a small typed relational representation and justify equivalence rules explicitly. New equivalent outcomes are added only with a sound rule and regression tests.

Future versions can extend the decidable fragment with additional proven identities or a U-semiring and SMT layer. A broader counterexample search can find more differences, but exhausting any finite search must still return undecidable, never equivalent.