Live data from Hacker News

Ask HN: How do you test SQL?

news.ycombinator.com

281–290 of 322 posts

Re: Ask HN: How do you test SQL?

#281

So I'm not an expert, but for simplistic use cases I merely make use of https://github.com/oguimbal/pg-mem It's a lot faster and easier than dealing with containers and the like.

Same except we use a different in-memory database (H2). This makes it simple for CI systems like Jenkins to run the tests and you can have various degrees of integration. Really amazing that your comment was the first mention I saw of in-memory databases in this thread.

Re: Ask HN: How do you test SQL?

#282
post #263

Earlier quoted context omitted.

Spark makes it easy to wrap SQL in functions that are easy to test. I am the author of the popular Scala Spark (spark-fast-tests) and PySpark (chispa) testing libraries. Some additional tips to speed up Spark tests (can speed up tests between 70-90%): * reuse the same Spark session throughout the test suite * Set shuffle partitions to 2 (instead of default which is 200) * Use dependency injection to avoid disk I/O in…

That's super useful, thanks. Could you expand on the 'Use dependency injection to avoid disk I/O in the test suite' point please - I'm not sure I understand what it means but it sounds interesting!

Sure, this blog post explains the dependency injection design pattern with Spark: https://mrpowers.medium.com/dependency-injection-with-spark-...

You can structure your code to read from paths when run in the production environment, but inject DataFrames you build in memory for your test suite. Spark is designed to read multiple files in parallel, so it's not optimized to read a single tiny file. That's why it's best to avoid I/O in Spark test suites whenever possible.

Re: Ask HN: How do you test SQL?

#284
post #43

Try and write any complex SQL as a series of semantically meaningful CTEs. Test each part of the CTE pipeline with an in.parquet and an expected_out.parquet (or in.csv and out.csv if you have simple datatypes, so it works better with git). And similarly test larger parts of the pipeline with 'in' and 'expected_out' files. If you use DuckDB to run the tests, you can reference those files as if they were tables (select…

Decomposing a large query into smaller subsets is the right approach, but I would strongly suggest doing it with VIEWs rather than CTEs. CTEs are a useful tool, but come with their own performance profiles and often using them too much will lead to slower queries.

Re: Ask HN: How do you test SQL?

#285

Earlier quoted context omitted.

I’ve not encountered your definition of declarative before. SQL is often cited in cs texts as an example of a declarative language. That said, SQL does have a lot of imperative features, but those features are used to declare the result of the sql dml query.

SQL does not make sense as a declarative language to me. How you can say that SQL and HTML is the same by nature? Also I can't see why we can't argue about this against the literature. Saying "you are wrong because 20 years old textbook say you are wrong" is not an argument for me

"Declarative language" does not mean "free of computation". It does mean the language generally specifies an output. HTML does have computation in the form of tags. What makes HTML and SQL declarative instead of imperative is that in both SQL and HTML the document specifies an specific output that is wanted from the browser (in HTMLs case) or database (in SQLs case). Your earlier example of SELECT (1+3); is something that can be done in HTML (many different ways), too. Reality is that declarative languages would be little more than file formats without access to computation. For example, it would be impossible to write a query that always returned results for the interval of last week, without having to change the query every time you run it without the now and interval commands.

Re: Ask HN: How do you test SQL?

#286

Can you please expand on what you mean by DBT? DBT, specifically, DBT-2 is a suit of tests designed to benchmark a database system . These tests aren't interested in, eg. correctness of an application that is using the database. They are meant to be testing the system as a whole by modeling some sort of an "average business" and defining some sort of an "average business operation" and estimating how many of such ope…

Oh, someone else below suggested that DBT in OP may be this: https://docs.getdbt.com/ . Well... I don't know anything about this tool, but from cursory reading, this is the correctness kind of testing that is meant to examine whether the way you defined the schema actually leads to the results you expect in the data. Well... it's not an interesting kind of testing, at least not for me. So, I don't know much about it.…

Depends on what you mean by "the subject". dbt doesn't claim to be a testing tool. Personally I use dbt all the time, while I didn't know about the existence of DBT-2. Different worlds apparently.

Re: Ask HN: How do you test SQL?

#287
You say you are using dbt, so doesnt "dbt test" provide you with the functionality to test? I assume by testing sql pipelines, you want to test if the data written to intermediate stores conform to what you expect. You should be able to do that with dbt test. If you are using an analytical database like Snowflake you could direct the results of the dbt run and dbt test to a test database and do your testing there.

Re: Ask HN: How do you test SQL?

#288
This is a little off topic, but related. Does anyone know of any tools that would allow information about the size and shape of expected data to be provided along with a database schema so that developers could get instant feedback if queries were likely to perform poorly when run against production data sets. Or to perform poorly when a database grows to beyond a certain size. I have seen many instances of SQL going into production databases that works well for a while, but gets much slower as the database grows.

Re: Ask HN: How do you test SQL?

#290
post #27

Earlier quoted context omitted.

I agree about the limitations of ORMs. However I have had great luck with using an ORM to load up the database and data, and then having a unit test that calls the function which does raw SQL in the middle. And now the raw database tests are integrated with the unit tests for the rest of the environment in a way that keeps them synchronized with the application code that also interacts with the same database. And, of…

There’s ORMs and there’s ORMs - at one end you have the (reprehensible) Active Record anti-pattern, at the other end you have EF Core extended with one’s own build-time type generation - they’re both “ORMs” to everyone involved, but they’re totally incomparable. …not to say they that EF Core doesn’t have flaws (it does, and they’re legion) but the ORMs of today are nothing like the ORMs of the 1990s… or even like 201…

I agree, but I still haven't seen an ORM that handles analytical queries well. Which means that, no matter what the other merits of the ORMs may be, there are important use cases where raw SQL is the only realistic option. Which brings us back to how to test that code.
Post reply on HN