ChatGPT Is Now Finding Bugs in Databases
ChatGPT has dominated the public’s attention over the last few months, creating entirely new industries, tools, and changes to how we work almost overnight. Millions of users across the globe have put ChatGPT’s AI power to the test, and while there’s no shortage of limitations with the software, some fairly amazing use cases are starting to show up.
One such use case is ChatGPT’s ability to make database testing more efficient. Recently, contributors to the open-source Linux project StarRocks put ChatGPT to the test against several popular automated testing tools to see what advantages, if any, the AI-powered chat platform could offer.
ChatGPT vs. Popular Database Testing Tools
The results of the StarRocks community’s test were impressive. ChatGPT was able to hold its own when put up against other existing and popular, automated testing tools including SQLsmith and SQLancer. The results of the tests were enlightening, and with ChatGPT’s ability to learn and adapt, it seems likely that this is only the beginning of what ChatGPT is capable of for improving database testing.
ChatGPT vs. SQLsmith
SQLsmith is an automated database testing tool that generates random SQL queries based on a fixed table schema and executes the queries against the database to test its performance and robustness.
ChatGPT is well-positioned to replace the work of SQLsmith, and can generate random, complex SQL queries based on natural language inputs with ease. While it’s too early to tell if ChatGPT is ready to completely eliminate the need for SQLsmith, it’s certainly worth evaluating for anyone already using the tool.
The following figure offers a great example of how ChatGPT constructs a complex SQL query based on a user’s input:
ChatGPT : Great for Maintaining SQLsmith
While testing ChatGPT against SQLsmith, another valuable use case presented itself: ChatGPT could be great for maintaining automated database testing tools. Anyone who has used these tools recognizes this is a major pain point. Every time a new function, operator, or data type is added, engineers need to update the source code of their tools like SQLsmith and SQLancer. This is both tedious and time-consuming.
To validate whether ChatGPT could effectively take on this task, StarRocks developers asked ChatGPT to write a query using StarRocks JSON functions. Initially, ChatGPT replied that StarRocks didn’t support JSON functions, but after feeding StarRocks’ JSON docs to ChatGPT, ChatGPT was able to learn from the docs and successfully generates an SQL query with StarRocks JSON functions.
The following figures show this learning process in action:
It’s clear that ChatGPT may not be ready to go right out of the box, but it demonstrates an amazing ability to quickly learn and adapt when the necessary learning materials are provided.
ChatGPT vs. SQLancer
Seeing how well ChatGPT fared versus SQLsmith, it made sense to see how ChatGPT compared to SQLancer by testing against SQLancer’s Ternary Logic Partitioning (TLP) principle.
The preceding figure is a schematic of the TLP principle, in which a random SQL query (Q) and a predicate (P) are provided, with three predicates (P, not P, and P is null) being applied to the query. A Union All is then performed on the results of the three queries. The checkmark in the diagram means the result is consistent with the original SQL query and the bug means the opposite.
Initially, this was not something ChatGPT could handle, but after nearly a dozen training sessions, ChatGPT developed the ability to generate an equivalent SQL based on the TLP principle.
This, however, doesn’t prove by itself that ChatGPT can act as a replacement to SQLancer. Since a complete SQLancer TLP test involves two additional steps: random table creation and data insertion, ChatGPT needs to hand this as well. Fortunately, this is very simple for ChatGPT. You can see the process below:
ChatGPT for Query-Aware Table Creation Statements
By this point, the StarRocks community was able to confirm that ChatGPT could (to a reasonable degree):
- Automate table creation and data loading.
- Automatically generate test SQL.
- Automatically generate a test oracle (confirm whether the SQL execution results are as expected).
And while these initial results from the ChatGPT vs. SQLancer experiments were exciting, they didn’t completely answer the question as to whether or not ChatGPT can act as a replacement for these testing tools. In order to better answer this question, ChatGPT needs to be able to prove that it can handle four additional requirements. Those are:
- Automatically generated SQL must be syntactically and semantically correct.
- Automatically generated schema and datasets must be valid.
- Automatically generated SQL code can cover a wide variety of scenarios.
- There is an efficient way to verify that the SQL execution result is correct.
Query-Aware table creation statements are actually a great solution for addressing the requirements and challenges listed above. This made testing ChatGPT’s ability to generate Query-Aware table creation statements a reliable way to evaluate ChatGPT’s effectiveness as a testing tool.
Query-Aware means that table creation and data loading are oriented for SQL queries and guarantee that the test query can get the desired query results as defined in a given test case. It turns out that ChatGPT can handle this with ease.
ChatGPT: A More Efficient Way To Test Databases
The StarRocks community’s tests show that ChatGPT has great potential for improving automated testing efficiency, but it still has some work to do. ChatGPT still requires additional support to fully understand the basic logic of automated testing, such as the basic rewrite principles of SQLancer’s TLP.
After dozens of hours of training, ChatGPT still does not fully understand the TLP principle, and the predicates in some of its rewritten SQL are wrong. All things considered, however, sufficient training may help ChatGPT fully understand this logic at some point in the near future.
This doesn’t seem like such a high bar for ChatGPT to clear, as ChatGPT’s biggest advantage is its amazing (and scary) ability to learn quickly. Even if it may be a while before ChatGPT is able to write test cases for new features more efficiently than humans, there’s no doubt that ChatGPT will help humans improve the efficiency and accuracy of the work they’re doing today.
Built on the Open Source database StarRocks, CelerData has a performance 3 to 5 times faster than any other solutions on the market, demanding only 1/3 of the hardware resources, and can reduce your operating costs by up to 80%.
As an open-source solution, StarRocks can be downloaded for free from starrocks.io and used in production environments without any performance or capacity limit. We also encourage database developers and users to join the hundreds of contributors worldwide on our GitHub repository and participate in discussions in our Slack channel.
But if you are looking for enterprise-standard features, dedicated support, or just want these benefits on the cloud, we recommend checking out our StarRocks-powered CelerData Enterprise and CelerData Cloud products.
We encourage you to take a look at these solutions to see which is right for your business. If you have any questions, please reach out to one of our engineers here.
What You Should Know Before Using ClickBench
Evaluating and Implementing Analytics Platforms
StarRocks outperforms ClickHouse, Apache Druid, and Trino with excellent wide-table and multi-table query performance