Latest / Talk Python To Me / #491: DuckDB and Python: Ducks and Snakes living together
Transcript
- 0:00Have you heard about DuckDB?
- 0:01Well, join me for an insightful conversation with Alex Monahan,
- 0:05who works on documentation tutorials and training at DuckDB Labs.
- 0:09We explore why DuckDB is gaining momentum among Python and data enthusiasts
- 0:14from its in-process database design to its blazing fast columnar architecture.
- 0:19We also dive into the indexing strategies, concurrency considerations,
- 0:24and fascinating way that Mother Duck, the cloud companion to DuckDB,
- 0:28handles large-scale data seamlessly.
- 0:30Don't miss this chance to learn how a single pip install
- 0:33could totally transform your Python data workflow.
- 0:36This is Talk Python To Me, episode 491, recorded December 10th, 2024.
- 0:41Are you ready for your host, please?
- 0:43You're listening to Michael Kennedy on Talk Python To Me.
- 0:47Live from Portland, Oregon, and this segment was made with Python.
- 0:51Welcome to Talk Python To Me, a weekly podcast on Python.
- 0:57This is your host, Michael Kennedy.
- 0:59Follow me on Mastodon, where I'm @mkennedy,
- 1:02and follow the podcast using @talkpython, both accounts over at fosstodon.org,
- 1:07and keep up with the show and listen to over nine years of episodes at talkpython.fm.
- 1:12If you want to be part of our live episodes, you can find the live streams over on YouTube.
- 1:17Subscribe to our YouTube channel over at talkpython.fm/youtube,
- 1:21and get notified about upcoming shows.
- 1:23This episode is brought to you by Sentry.
- 1:25Don't let those errors go unnoticed.
- 1:27Use Sentry like we do here at Talk Python.
- 1:29Sign up at talkpython.fm/sentry.
- 1:32And it's brought to you by the Data Citizens Dialogues podcast from Colibra.
- 1:36If you're ready for a deeper dive into the latest hot topics in data,
- 1:40listen to an episode at talkpython.fm/citizens.
- 1:43Alex, welcome to Talk Python To Me.
- 1:46Howdy, thanks so much for having me.
- 1:47Yeah, it's fabulous to have you here.
- 1:49I'm really excited to talk about databases.
- 1:51Databases are so important for making your app work well.
- 1:55Making them fast surprises me how many people have slow databases.
- 2:00It doesn't make sense.
- 2:01We're pretty big fans of databases around here, for sure.
- 2:03And fast ones as well, right?
- 2:04As best we can.
- 2:06That's right.
- 2:07Well, DuckDB is certainly on the rise in terms of me hearing people talk about like,
- 2:13oh, have you heard DuckDB?
- 2:14Have you seen DuckDB?
- 2:16Have you worked with it?
- 2:17And I think it continues an interesting tradition of lots of low stress near your application type
- 2:24of databases.
- 2:25So it's going to be fun to dive into that for sure.
- 2:27Before we do, tell us about yourself.
- 2:29Sure.
- 2:29Well, thanks so much for having me.
- 2:30Huge fan of the show.
- 2:31Really excited to be here.
- 2:32So I appreciate that.
- 2:34I work at MotherDuck.
- 2:35I'm a forward deployed software engineer, which is kind of a made up title.
- 2:39A couple of companies use it, but it's really a customer facing software engineer.
- 2:42So working closely with folks to see how MotherDuck fits in with all the rest of their stack.
- 2:47So MotherDuck is a cloud data warehouse with DuckDB at its core.
- 2:50So absolutely a huge part of what we do.
- 2:53I also work part time for DuckDB Labs, which is our partner organization.
- 2:57And I do blogging and some developer advocacy types of things for them.
- 3:02And so it's a separate company based in Amsterdam, the Netherlands.
- 3:06And they are database experts and researchers.
- 3:09And they're kind of the stewards of the open source project.
- 3:11Yeah.
- 3:11Awesome.
- 3:12What is a forward deployed engineer do?
- 3:15Do you get called into big companies who want to adopt the technology?
- 3:19And they say, hey, could you help us do this quicker?
- 3:21Or are they running in trouble?
- 3:23And they're like, you said it was fast.
- 3:25It's not fast.
- 3:26Help.
- 3:26Well, don't copy this every query.
- 3:28So you'll be better.
- 3:30There's quite a lot of variety.
- 3:31I think there's definitely some aspects of, you know, hey, can you help me with this?
- 3:36There's also aspects of as folks are getting started, they're kind of picking out the rest of the tools around the database.
- 3:40So what BI tool should I use?
- 3:43What orchestrator should I use?
- 3:44And then, you know, we do get into some SQL optimization, stuff like that.
- 3:48So that's kind of the customer facing side.
- 3:49That's the majority of the role.
- 3:50There's also troubleshooting that goes along with it, as well as kind of feeding things back into the company, being the voice of the customer saying, hey, we've had three people hit this.
- 3:58Is there a way we can build something different?
- 4:01Yeah.
- 4:01Yeah.
- 4:02Like everyone thinks they should do it this way.
- 4:04We need to change our docs or they want this particular feature and we don't have it.
- 4:08Yeah.
- 4:09Yep.
- 4:10Yep.
- 4:10It's a good cycle that we've got going.
- 4:12It sounds really fun.
- 4:13It's a lot of fun.
- 4:14The variety is my favorite part.
- 4:16And, you know, a lot of fun investigation, troubleshooting, you know, got to reframe it as a scavenger hunt.
- 4:21You know, it's fun.
- 4:22Yeah.
- 4:23Every day is like a box of chocolates, huh?
- 4:25Oh, yeah.
- 4:26A little different all the time.
- 4:27You know, you don't know what you're going to get.
- 4:29I used to do in-person professional training.
- 4:33You know, I'd travel to Chicago or New York or Beijing and I would show up Monday morning.
- 4:39I mean, all right, folks, what are we, you know, what's the situation?
- 4:43Obviously, I didn't know what we were going to talk about, but it was still, it was different every time.
- 4:46Every week was different.
- 4:48Sounds like it might be not so different from what you're doing.
- 4:50Are a lot of these in-person things or are they mostly Zoom style?
- 4:55It's Zoom style.
- 4:56You know, just we work with a variety of folks, not, you know, like a couple of really giant customers.
- 5:01So with that variety means that I couldn't fly fast enough.
- 5:04Yeah, for sure.
- 5:06Or, you know, issues I've had as well as like three people on the team are in this city.
- 5:10Three people are in that other city and one is here.
- 5:12It's like even if you flew somewhere, you would only be, you'd still be on Zoom.
- 5:15So you might as well just be on Zoom.
- 5:16Definitely, definitely.
- 5:18Absolutely.
- 5:18The world we live in.
- 5:19Well, I mean, that's a blessing, honestly, to not have to fly to all these places as well as just be able to pop in and out, right?
- 5:27I mean, this work from home story.
- 5:29It's amazing.
- 5:29It's only getting better all the time.
- 5:31So let's talk DuckDB and databases.
- 5:35I think maybe setting a bit of a mental model.
- 5:38A lot of folks are familiar with SQLite, right?
- 5:43Yep.
- 5:43Absolutely.
- 5:43And so SQLite is a library, not a server that you can use that then plugs into your application, works on a local file rather than a certain thing you run in another VM or however you, you know, there's not a managed SQLite.
- 5:58Not that I know, I know maybe there's a managed SQLite, I don't know of one.
- 6:01There are.
- 6:01There are.
- 6:02Are there?
- 6:02Incredible.
- 6:03Yeah, there's a bunch of cool stuff that you can do if you have a database that's just part of your application instead of somewhere else.
- 6:10You know, another one that I just ran across is Mocha-py or there's no doubt, there's Dashpy, which is a Python wrapper around the, I think it's Rust Mocha caching thing.
- 6:21But instead of being Redis or something like that, it's like an in-memory thing that you can run for sort of the same purpose.
- 6:27So DuckDB sort of falls into that realm, right?
- 6:31Yes.
- 6:31I think it's an in-process database, just like you said.
- 6:34So it's definitely a library.
- 6:35You know, it's a import DuckDB.
- 6:37So it's not in the Python standard library, but it is on PyPI, just pip install DuckDB.
- 6:42It's pre-compiled, zero dependencies.
- 6:45So it is super easy.
- 6:46Basically, anywhere you can get to pip, you can get DuckDB run in.
- 6:49So that could be your laptop.
- 6:50That could be a big, beefy server.
- 6:52It could be a Lambda function.
- 6:54It could be inside of your orchestrator, you know, in a GitHub action.
- 6:58Kind of slots in everywhere.
- 6:59That's part of the magic.
- 7:00Presumably, when you pip install DuckDB, it's just downloading a wheel for your architecture, for your platform?
- 7:06Yep.
- 7:06Pre-compiled all the combinations.
- 7:08Yeah, that's really good.
- 7:09Yeah, if you just sit on the DuckDB.org homepage, you can learn about it as it animates different messages to you.
- 7:16It says DuckDB is a fast analytical database system, open source database system, in-memory database system.
- 7:22We'll see what else.
- 7:23Yeah.
- 7:23Portable.
- 7:24So, yeah, it works on most of the places, the main operating systems, right?
- 7:29Yes.
- 7:29Yep, it's all over.
- 7:30Can I Raspberry Pi it?
- 7:32I'd imagine the answer is yes.
- 7:33Can I CircuitPython it?
- 7:35Probably not, right?
- 7:36I haven't checked that.
- 7:37You can do iPhone and Android.
- 7:38There's a Swift client, so you can build iPhone apps and Android works as well.
- 7:43So, yeah, you could do that.
- 7:44Works in your browser with WebAssembly.
- 7:46It'll work with Pyodide in the browser as well.
- 7:49So, it really goes all over.
- 7:50Oh, that's really cool.
- 7:51Yeah, one of the things that I said people are doing interesting stuff with, I had in mind, with SQLite is I saw some folks suggesting using SQLite in the browser instead of local storage and like the local, like all the JavaScript-y things.
- 8:06And could I do that with DuckDB?
- 8:07You could definitely do that with DuckDB.
- 8:10I think the use case is slightly different, but that's sort of the benefit is you can use both.
- 8:15So, definitely could talk a bit more about the differences, but I'd say in general, a good mental model for DuckDB is kind of a from scratch separate project from SQLite, but taking a lot of the lessons and learnings about what makes people love SQLite and bringing that into the analytical realm.
- 8:30And that's really, instead of inserting and updating tons of tiny transactions all the time really fast, it's big bulk operations.
- 8:37Aggregate a billion rows, join a billion rows to a billion rows, that type of thing.
- 8:42Yeah, doing that in memory in JavaScript is not that great.
- 8:45Doing that in memory in Python is not that great.
- 8:47But it sounds a little bit more like vector programming in the sense of kind of like you would do with pandas or Polars, right?
- 8:56You wouldn't loop over, you shouldn't loop over a pandas data frame processing each item.
- 9:02You should issue vector operations that apply to every row, like multiply this column by two rather than for every C in column, you know, whatever, like looping it over.
- 9:11Yes.
- 9:12So, it is exactly what you said.
- 9:14It's designed for that.
- 9:15It's vectorized execution.
- 9:17So, it's a little different than pandas.
- 9:18It's, you know, Polars is a bit more similar.
- 9:21Where instead, pandas will process a whole column at a time.
- 9:25And that works a lot better than one row at a time until your columns get too big and then it crashes.
- 9:30Yeah.
- 9:30So, what DuckDB does is it does it in chunks of about 2,000 rows at a time.
- 9:34And so, that way you get all the benefits of your modern CPU, you know, hierarchy, your caches, all that, where it's just pumping data through.
- 9:41But you also conserve memory and you can not get into issues with that.
- 9:46I will also say it's technically we are an in-process database, not an in-memory database.
- 9:51Oh, sorry.
- 9:52If I said in-memory, I meant in-process.
- 9:54Yeah.
- 9:54It is, you know, very much an interchangeable word like in English.
- 9:58We just are very specific about it in database land.
- 10:00No, you do.
- 10:01And you do support in-memory databases.
- 10:04Yes.
- 10:04But that's not the same as in-process.
- 10:06Yeah, sorry, in-process is what I meant.
- 10:07Oh, no problem.
- 10:08All I mean to say there is, you know, you don't need to limit yourself to your size of RAM.
- 10:12You know, you can solve one terabyte problems on your laptop with DuckDB, no problem.
- 10:16Really?
- 10:17Yes, it handles larger than memory data, both streaming from disk.
- 10:21So, it'll just read just the pieces it needs as it goes and even in the intermediates.
- 10:25So, let's say you join two tables together and you, you know, multiply the number of rows.
- 10:30It'll send things to disk as it gets memory pressure close to your memory limit.
- 10:34So, it's a lot of effort has been put into that in the last year or so.
- 10:38So, it's working pretty smooth now.
- 10:40Yeah, I can imagine that that's super important.
- 10:42So, I'm really looking forward to talking about schemas, indexes, those kinds of things,
- 10:48which sound like they may play important roles here.
- 10:50But you talked about these different ways in which you can use DuckDB.
- 10:55Looking through the documentation, it looks like it works with not just Python, but quite a few platforms or languages.
- 11:00Yes, somewhere north of 15.
- 11:03So, Python is the most popular way to use DuckDB.
- 11:05We've got some really tight integrations with data frame libraries, which we can definitely talk more about.
- 11:09There's a lot of Java use out there.
- 11:12So, JDBC, we've got a JDBC driver.
- 11:13You can use it in JavaScript, both in the browser with Wasm or on the server with Node, popular Node package.
- 11:21You can also do Go, Rust, C++, C.
- 11:25I think there's like a community-driven Ruby client, you know, really all over the place.
- 11:30So, .NET, C#, you can use it from .NET, all over.
- 11:34Yeah.
- 11:34Yeah, that's awesome.
- 11:35And I'm guessing it's interchangeable, the file?
- 11:38Yes.
- 11:39So, just like SQLite files are designed to be read for a long time, they're very portable.
- 11:45DuckDB just kind of reached the 1.0 milestone in June.
- 11:49So, super excited about that.
- 11:50And with that, you know, promised stability with the file format.
- 11:54And that means that we expect for the next somewhere around five years to have your DuckDB files be totally compatible,
- 12:01where you can read a five-year-old file five years from now type thing.
- 12:04Yeah.
- 12:04Which means you can, it's like SQLite in that you can fit all your tables and all the relationships between them all in one file.
- 12:11So, that's one nice thing about SQLite.
- 12:14You can email the whole database.
- 12:15You can do the same with DuckDB.
- 12:17But DuckDB is going to store it a lot more compactly if you've got a lot of data.
- 12:21Oh, really?
- 12:22Okay.
- 12:22You guys have worked a lot on the file format.
- 12:24It's, again, taking inspiration from SQLite, the format is very separate.
- 12:29And in databases, you kind of have a pretty hard fork in the road pretty early on in your architecture,
- 12:34which is you want to be row-based or do you want to be column-based?
- 12:37Right.
- 12:38And row-based is SQLite and column-based would be your pandas, your Polars,
- 12:42and most of your cloud data warehouses, your snowflakes, that type of thing.
- 12:46And once you go column-based, suddenly your compression is amazing because you store data that's very similar right next to each other.
- 12:53So, you can really compress it.
- 12:54You know, your dates are back-to-back.
- 12:56Instead of, you know, a date column, string column, integer column in a row-based store, they don't compress well.
- 13:01So, we can get somewhere around 5x the compression typically.
- 13:04Okay.
- 13:04That's really great.
- 13:06And you could take this file and you could put it into S3 or use it with some sort of Jupyter Notebook, right?
- 13:13And just, they could just grab the file and run with it.
- 13:15And it's all the data, yeah?
- 13:16Yes.
- 13:17Object stores are really the name of the game there.
- 13:20You can even work with a file on Object Store and read it without having to download the whole file first.
- 13:26So, DuckDB supports something like SQLite in that it's got this attach syntax.
- 13:30So, you can say, hey, I want you to attach this file and then just read the pieces of it that I actually query, which is pretty slick.
- 13:37Yeah, that is super slick.
- 13:38All right.
- 13:38Let's, let me scroll down here.
- 13:41And let's talk about maybe using it from Python.
- 13:45Give us a sense of, like, what working with DuckDB is like here.
- 13:49You bet.
- 13:50So, you've got your pip install DuckDB, which will download that pre-compiled binary.
- 13:54It's like 20 or 30 megabytes.
- 13:55So, pretty close to instant.
- 13:56Then import DuckDB, and then you can do DuckDB.SQL.
- 14:00And then within that function, you just pass in a string of your SQL statement and it'll execute.
- 14:05It's really that simple.
- 14:06And so, it follows the DB API if you want.
- 14:09So, you can get back your typical suples.
- 14:11Or you can say .df at the end and get back a Pandas data frame.
- 14:15Or .pl and get colors.
- 14:17All kinds of stuff.
- 14:18Yeah.
- 14:19I saw that there's a lot of to and from the popular data science libraries, which seems really nice.
- 14:24You work in your data science library or maybe run a query and get a subset of your data.
- 14:29And then maybe plot that with PlotLayer, Altair, or pass it along to other things that expect the data frame, right?
- 14:36Yes, exactly.
- 14:37A lot of the DuckDB ethos is don't ask folks to ship their data necessarily somewhere else.
- 14:42Don't ask them to change their workflow dramatically to use a database.
- 14:45Meet folks right where they are.
- 14:47And that means you can have one line of code be in your data frame library of choice.
- 14:51Pandas, Polar, or Arrow.
- 14:53And then a line of code in DuckDB.
- 14:55And then you're right back in your data frame in the next line.
- 14:58So, it fits really wherever you want.
- 14:59It's a great fit if you want to throw some SQL at a problem.
- 15:02Some problems are easier to formulate in SQL, I think, than in data frame libraries.
- 15:06And vice versa.
- 15:06But also, some libraries, it's harder to scale larger than memory.
- 15:11And DuckDB does have a lot of nice performance under the hood.
- 15:14So, maybe the hardest part of your workflow, you can slide in DuckDB and make the minimal change to get it working.
- 15:23This portion of Talk Python To Me is brought to you by Sentry.
- 15:26Code breaks.
- 15:27It's a fact of life.
- 15:29With Sentry, you can fix it faster.
- 15:31As I've told you all before, we use Sentry on many of our apps and APIs here at Talk Python.
- 15:37I recently used Sentry to help me track down one of the weirdest bugs I've run into in a long time.
- 15:42Here's what happened.
- 15:43When signing up for our mailing list, it would crash under a non-common execution pass.
- 15:49Like, situations where someone was already subscribed or entered an invalid email address or something like this.
- 15:55The bizarre part was that our logging of that unusual condition itself was crashing.
- 16:01How is it possible for our log to crash?
- 16:04It's basically a glorified print statement.
- 16:07Well, Sentry to the rescue.
- 16:08I'm looking at the crash report right now, and I see way more information than you'd expect to find in any log statement.
- 16:15And because it's production, debuggers are out of the question.
- 16:19I see the traceback, of course, but also the browser version, client OS, server OS, server OS version, whether it's production or Q&A, the email and name of the person signing up.
- 16:30That's the person who actually experienced the crash.
- 16:32Dictionaries of data on the call stack and so much more.
- 16:35What was the problem?
- 16:36I initialized the logger with the string info for the level rather than the enumeration dot info, which was an integer-based enum.
- 16:45So the logging statement would crash, saying that I could not use less than or equal to between strings and ints.
- 16:52Crazy town.
- 16:53But with Sentry, I captured it, fixed it, and I even helped the user who experienced that crash.
- 17:00Don't fly blind.
- 17:01Fix code faster with Sentry.
- 17:03Create your Sentry account now at talkpython.fm/sentry.
- 17:07And if you sign up with the code TALKPYTHON, all capital, no spaces, it's good for two free months of Sentry's business plan, which will give you up to 20 times as many monthly events as well as other features.
- 17:20It seems to me like maybe you put all of the data into DuckDB, and then you can ask questions which result in Python data science objects that then you can work on, where those questions result in, give me the stuff for this state or this time period or something like that where it's a reasonable amount of data.
- 17:37But if you tried to just load it all, then it would say no.
- 17:41I think that's a great way to go about it for sure.
- 17:43You know, another common workflow is to do some, you know, similar things, but with Parquet files or CSVs or JSONs.
- 17:51So in the same way you can store a lot of your data in DuckDB, you can periodically write chunks out to Parquet or read in from Parquet, all that type of stuff too.
- 17:59Yeah.
- 17:59Tell people what Parquet is.
- 18:00I know it comes from PyArrow or the Arrow project, right?
- 18:04Where does it come from?
- 18:05I know they use it.
- 18:06I don't know if they came up with it, but yeah.
- 18:08They definitely collaborated and were a big part of it.
- 18:11So it's a community developed open format.
- 18:14It's Apache Parquet.
- 18:15So it's a open source community driven and it's a columnar format.
- 18:21So instead of storing things by rows, it's columnar storage and it's chunked columnar storage.
- 18:26So it's sort of like somewhere around every million rows or so you kind of write out another chunk.
- 18:30And it ends up being a really good format to store large amounts of data in the cloud.
- 18:36And it's typically what's underneath a data lake or a data lake house.
- 18:40The vast majority of those have Parquet under the hood.
- 18:43I see.
- 18:44We love the Parquet format at DuckDB.
- 18:46We read it and write it.
- 18:46We have a custom from scratch reader, custom from scratch writer for it.
- 18:49We actually prefer our own format given the choice as well.
- 18:53Parquet has been around for a decade or so at this point.
- 18:56And we've added a few things over time into DuckDB that are new.
- 19:00Yeah.
- 19:00Parquet is one of those things that Pandas 2.0 supports it, right?
- 19:04Polar supports it.
- 19:05It's pretty universal.
- 19:07So it might be a good way to get data from that you've worked on before into DuckDB.
- 19:12Yes, it's great.
- 19:13I think, you know, most universal CSV, most difficult, also CSV.
- 19:17Parquet is a really great compromise there.
- 19:20Yeah.
- 19:21Hold on.
- 19:24My computer is lagging.
- 19:25It's distracting me.
- 19:26I apologize.
- 19:27One sec.
- 19:29Scroll, scroll.
- 19:31So DuckDB works well, it sounds like for the data science story, like it's built for the data science story.
- 19:36If I was wanting to store relational data, you know, maybe I've already got DuckDB working for something.
- 19:42I'm like, you know, do I necessarily want to bring SQLite into the story as well and have these two things?
- 19:48Is it possible to, say, do relational type things with DuckDB?
- 19:53Yes, that's a great question.
- 19:55So I think DuckDB is trying to bring the best of both.
- 19:58It is a full relational database.
- 20:00And as well as supporting some of the, you know, data science workflows that you typically in the past would have been doing in a data frame.
- 20:06So DuckDB is an analytical system, not transactional.
- 20:10So it's not going to be the fastest at doing individual row operations, but it supports them all.
- 20:15And it supports them with full asset transactions that you'd expect from a database where you won't get database corruption, even if your computer cuts out.
- 20:23You know, it's got that kind of robustness that you expect from a database.
- 20:27So part of the story, though, is that with an analytical database, it's typically not your only database unless you're doing data science work.
- 20:34If you're building an application, you're going to want a transactional database for, you know, saving your, you know, when a customer places an order, you know, all that type of transactional stuff is still critical.
- 20:44So typically DuckDB is going to be layered in addition.
- 20:46And the interoperability story there with DuckDB is pretty, pretty fantastic.
- 20:50You can actually read SQLite files directly with DuckDB.
- 20:54And it's going to be, if you're doing any sort of advanced processing on them, it's going to be a lot quicker in DuckDB.
- 21:00You can even read Postgres databases.
- 21:02You can read MySQL databases.
- 21:04Oh, wow.
- 21:04There's a community extension to read BigQuery.
- 21:06So it's really a universal Swiss Army knife in a lot of ways to read from those operational data stores, but to do those analytical tasks.
- 21:14You know, look at all my orders.
- 21:17What was the most popular five products?
- 21:19You know, that's going to be a tough query for a transactional system, but bread and butter for DuckDB.
- 21:23So when you say read from, let's say, Postgres, does that import it into a table in DuckDB or does it translate the queries over to the underlying data system?
- 21:35What's happening there?
- 21:36It's a great question.
- 21:37We are communicating with Postgres, so we're not importing everything.
- 21:42We're sending the query to it and we're reading all the data from Postgres.
- 21:46Typically, that's going to be sparing the table or forming a view from Postgres.
- 21:49And then after that point, bring it into the DuckDB engine for subsequent processing.
- 21:54But you don't have to migrate all your data.
- 21:56It's kind of a data virtualization is one of the buzzwords.
- 22:00It's been around a little bit.
- 22:01But, you know, it's getting to get some of the benefits of analytical performance without having to move your data first.
- 22:08It'll definitely be more performant if you put it in an analytical format like Parquet, like DuckDB, like an analytical database.
- 22:14But you get a big fraction of that benefit with the engine part, which is the vectorized engine of doing things 2,000 rows at a time instead of one row at a time.
- 22:23That's a lot of it.
- 22:25Yeah, that's a huge benefit.
- 22:25It's almost like microservices for databases.
- 22:29You can click the things together.
- 22:31But the benefit would be, you know, that if you have an operational database, that is, it's getting read to and written to and read from in real time.
- 22:40When you ask questions about it, it's using that real time data.
- 22:43It's not, well, we did an export last night and here's what we know.
- 22:47You can just get more live data, I guess.
- 22:50Yes, that's definitely a use case for that.
- 22:52You can kind of go either end of that spectrum.
- 22:54You can use that as an easy way to import or you can pull it live or you can kind of do midway and say, for any data older than an hour, use what I extracted.
- 23:04But grab the last hour right out of the operational database and get the best of both.
- 23:08Oh, wow.
- 23:08Okay.
- 23:09Yeah, that'd be pretty wild, wouldn't it?
- 23:10So you say that you all have the simplest or friendliest SQL syntax.
- 23:17What's the story with that?
- 23:18Sure.
- 23:18I think a lot of that is around part of the origin story of DuckDB is reaching out to the data science community and hearing feedback that we've invented this entire other thing called data frames because we didn't love databases.
- 23:30Yeah, exactly.
- 23:31They didn't answer the questions we wanted or whatever.
- 23:35Yeah.
- 23:35Right.
- 23:36It was, there was a lot of friction.
- 23:37And, you know, if you've, you know, folks have installed Postgres and set it up, there's a lot more to it than, you know, import pandas as PD.
- 23:45It's just a different level of hassle that you have to do.
- 23:49The SQL syntax is no different.
- 23:51You know, SQL is an old language, much older than Python.
- 23:54And it's got a lot of very interesting behavior that, you know, by modern standards is very different.
- 24:00So with DuckDB, we've really pushed the envelope in a lot of ways.
- 24:04Nice.
- 24:04Well, you know, many of the things that relational databases were built for and when they're designed, this was the 70s and they had different, you know, memory was expensive.
- 24:14Disk was expensive.
- 24:15So they made these trade-offs in that world, right?
- 24:18Yes.
- 24:19And it's absolutely.
- 24:21There's definitely some aspects of SQL where it's been around a long time, so it'll be around forever.
- 24:25But what DuckDB is trying to do is have it not be frozen in time forever and really push the whole language forward.
- 24:31So there's a couple of cool ways.
- 24:33The syntax itself is designed to be both easier to use and also push it into areas that are new and different for databases.
- 24:40So easier to use, for example, in SQL, if you want to aggregate, you kind of typically need two ingredients.
- 24:46You need an aggregation function like a sum or a max or an average.
- 24:49And then you want to group by clause to say, what's the level of detail I want to group at?
- 24:55Do I want to group by customer or customer and order?
- 24:58You know, what level of detail am I looking at?
- 25:00DuckDB can actually infer that level of detail with group by all and just say, whichever columns you're looking at, if you're looking at the customer ID and you're getting an average,
- 25:09okay, I'll just aggregate it up at customer ID level for you.
- 25:11And so it cuts out a whole clause of SQL.
- 25:13You basically never have to worry about the group by clause ever again.
- 25:17Just put the keyword all, you're good to go.
- 25:19I love it because the group by clause is hard.
- 25:21It's anytime you have to edit something in two places, just you're already in trouble, right?
- 25:25I mean, just, you know, the don't repeat yourself is very valuable.
- 25:29Yeah.
- 25:29Yeah.
- 25:30The joins and the group buys and the whole aggregation pipeline stuff, which is incredibly powerful,
- 25:35especially in analytical databases like DuckDB, but also hard.
- 25:38Yes, absolutely.
- 25:40And I think obviously anything we can do to reduce the friction there is helpful.
- 25:43So we are talking about the friendly SQL dialect.
- 25:46I'll throw a teaser in there to say there are ways to use DuckDB without SQL as well in very Pythonic ways.
- 25:51So you really can have it both ways if you want.
- 25:54Okay.
- 25:54What about ORMs?
- 25:55Is there a concept of a ORM for DuckDB?
- 25:59I'd have to check exactly which ones support it.
- 26:01But I know at MotherDuck, we're working on a TypeScript ORM that'll support DuckDB SQLize.
- 26:10I'd have to check which other ones are out there that support it.
- 26:12But nothing in principle really stops it.
- 26:14We support the fundamental things you need, like transactions, like, you know, all the other pieces you need.
- 26:21Yeah.
- 26:21Basically, if it runs on pure SQL, then, you know, you should...
- 26:25ORMs are mostly about I'm querying against classes, and then I'm going to turn that into SQL for you.
- 26:31And then the things that come back, I'm going to turn them into objects, right?
- 26:34Like, I feel like both sides of that would be fine.
- 26:37Yes.
- 26:38And it is, you know, it's a relational database.
- 26:41So it's not...
- 26:42There are many...
- 26:43It'll look a lot very similar to the ORMs themselves, so it should be very compatible.
- 26:47Yeah, it seems possible.
- 26:49It seems totally possible.
- 26:50All right.
- 26:50So maybe let's talk through a little bit of the ways of working with DuckDB through, maybe connecting, running some queries, updating.
- 27:00But let me first ask you, what does a schema look like?
- 27:05If, for example, if I'm going to work with Postgres or SQLite, I need to run create scripts to create the tables and the indices and that kind of stuff before I can even talk to it, generally speaking.
- 27:16Is that needed for DuckDB?
- 27:18It doesn't look like it from the examples that I've seen, but how does that work?
- 27:21I think in those other databases, there are some ways where you don't necessarily have to do that, and DuckDB follows that same approach where it can automatically create tables for you based on the query that you pipe into it.
- 27:33So you create a table as the result of another SQL statement, and it will just automatically create that table with the data types that you pass through.
- 27:42So one key note, if folks really know a lot about SQLite, one of the defining characteristics of it is that it is very much non-typed as much as possible.
- 27:50VectDB is very much more traditional in that it does have typing.
- 27:55It does take full advantage of having nice numeric data types, date times that are true date times, not strings.
- 28:03It'll do its best to auto-convert for you, but it really does make it very easy.
- 28:07You can just create a table as, for example, create table as select star from this parquet file.
- 28:12It can really be that easy.
- 28:14It'll just auto-create the table based on the parquet file.
- 28:16Okay.
- 28:16Yeah, that's really neat.
- 28:18And a lot of those have their columns have data types and stuff, right?
- 28:21Yes.
- 28:21And we have also invested quite a lot in our CSV reader as well.
- 28:26So the CSV reader we consider to be up there with the best on the planet.
- 28:31It auto-infers the data types by sniffing the CSV to really deduce it.
- 28:37And it handles some seriously wacky CSV files.
- 28:41Okay.
- 28:42CSV files.
- 28:43I mean, whatever you're imagining, it's worse.
- 28:46It's worse than that.
- 28:49But DuckDB is okay and ready to handle it and making it easy.
- 28:54Because it really aligns with the DuckDB ethos, which is a database shouldn't be extra work.
- 28:58It should be saving you work.
- 28:59Yeah.
- 29:00And most of the time, step one of a database is import your data.
- 29:04And a lot of databases, that's not that easy to just import your data.
- 29:08Especially if it's not exactly the format of that database, right?
- 29:12It's like, well, okay, I have a .SQL file.
- 29:15It's a bunch of update statements.
- 29:16Sure, that's easy to import.
- 29:18But here's my CSV and actually you need to transform it or whatever.
- 29:21Yeah.
- 29:22Yes, there's a long tail of complexity there, but DuckDB is really trying to handle that
- 29:27and just make that first experience using a database just straight out of the box.
- 29:31Really easy.
- 29:32This portion of Talk Python To Me is brought to you by the Data Citizens Dialogues podcast.
- 29:38If you're ready for a deeper dive into the latest hot topics in data,
- 29:42you need to listen to the Data Citizens Dialogues podcast.
- 29:45Brought to you by Colibra, the leader in data intelligence.
- 29:49In every episode of Data Citizens Dialogues, industry leaders unpack data's impact on the world.
- 29:55From big picture questions like AI governance and data sharing to more nuanced questions like,
- 30:01how do we balance offense and defense in data management?
- 30:04You'll hear firsthand insights about the data conversations affecting all kinds of industries.
- 30:10With guests sharing unique stories from some of the world's largest companies,
- 30:14such as Adobe, Fidelity, Deloitte, Hewlett-Packard, McDonald's, and even the United States Coast Guard,
- 30:19you'll get an amazing look inside how these organizations handle their data.
- 30:22My favorite episode is Solving Data Discovery with a self-service approach.
- 30:26It's an interesting look inside creating a single source of truth at an online university.
- 30:32Check them out and try an episode for yourself.
- 30:34Find Data Citizens Dialogues at talkpython.fm/citizens.
- 30:39That's talkpython.fm/citizens.
- 30:41The link is in your podcast player's show notes.
- 30:43Or just follow Data Citizens Dialogues on Apple, Spotify, YouTube, or wherever you get your podcasts.
- 30:51Thank you to the Data Citizens Dialogues podcast for supporting the show.
- 30:54So you have some interesting data ingestion.
- 30:59This is a good thing, not a bad thing.
- 31:01Data ingestion options.
- 31:04You have a, it feels a little bit Pandas-like.
- 31:06You have a read CSV, read Parquet, read JSON.
- 31:09And then it's just, as you sort of hinted there, it's like part of the database now and you can ask it questions.
- 31:16Yes, so you could absolutely do that just with the read CSV and it'll import there.
- 31:21You can also even just read it without importing.
- 31:25So for example, when we're running that read Parquet, it's lazily evaluated, much like Polars or some of the other more modern data frame libraries are,
- 31:34where it's building up this relation object that says, once you look at the results, start by reading the Parquet file and then process the next few steps.
- 31:42So you could do something like read Parquet, filter to these things, aggregate up on these columns.
- 31:47And it will actually do all those operations together.
- 31:50It'll optimize them to do them in the right order to pull the least amount of data.
- 31:54And then only at that point will it, you know, materialize what it needs to.
- 31:57So the examples that you're looking at on screen are, you know, DuckDB.SQL select star from a Parquet file.
- 32:04You don't ever even have to read that part, you know, write that Parquet file into DuckDB format.
- 32:08It's just going to read it and return the results to you straight as a pass through, as an engine.
- 32:13Oh, that's really cool.
- 32:15So if it were a million lines of that, I could just iteratively pull through it.
- 32:21Yes.
- 32:21It'll just chunk through it at nice low memory usage and just, you know, pass those results out to you.
- 32:26And then if you process them also in batches, you can keep your memory, you're super low and take advantage of DuckDB being a streaming engine and not bring it on to memory.
- 32:35Right.
- 32:35Well, let's say I want to read a CSV file, but then put it into a persistent table in DuckDB.
- 32:41What do I do for this?
- 32:42Yes.
- 32:43You could say create table as my improved CSV table as select star from path to my CSV file.
- 32:51Will it copy over the schema?
- 32:53It will infer the schema as best as it possibly can, and it will automatically define the schema for you.
- 32:59Nice.
- 32:59What's the nullability story in your schema?
- 33:02I think...
- 33:03Do you support nullability or are there things that cannot be null?
- 33:07Yes.
- 33:07It's more traditional in the database in that everything can have a null value in the column.
- 33:12I think in Pandas, that was a little bit complicated by the fact that they were using NumPy in some cases, but extending beyond NumPy in others.
- 33:20But in DuckDB, it's more of a traditional database story in that you can have a null value wherever you choose unless you define a table to say this column doesn't allow nulls.
- 33:29Right.
- 33:29You can do that if you want.
- 33:30Yeah.
- 33:31I remember that was one of the things that caught me out with SQLite.
- 33:33I think it doesn't support...
- 33:35I can't remember which way it went.
- 33:36It's been a while.
- 33:37It doesn't support non-nullable columns.
- 33:40Like, everything is nullable or something like that.
- 33:42And it was like, wait, why is this not failing when I put in this wrong data or something along those lines?
- 33:47You know, SQLite is incredibly flexible and there's a lot of benefits to it.
- 33:51At times, that can mean that things pop up later in the lifecycle than at the beginning, like you said.
- 33:56So with SQLite, you can put a number in a string column and a string column and a number column, you know, and it'll mostly work until it doesn't work.
- 34:04DuckDB will be much more explicit about that, but it offers a lot of tools where you can auto-convert or it'll auto-convert for you.
- 34:11So you kind of get the benefits without the cost.
- 34:13And it actually has what's called a union type where you can actually have one column that can have strings and numbers in it if you know that you're going to have a little bit of both.
- 34:21So really, best of all worlds, I'd say.
- 34:24Yeah, I think that's more common in the data science, data lake space.
- 34:27You've got a bunch of data, you pull it in, and then you're going to clean it up and work on it.
- 34:31Whereas in maybe a relational database powering a web API, like, nope, users always have an email.
- 34:37That's just how it is.
- 34:39Right.
- 34:39Yes.
- 34:40Okay.
- 34:41So I'm looking at some code on your documentation here about getting started with Python for pandas.
- 34:46And I need some help.
- 34:48I need some help.
- 34:49It says import DuckDB, import pandas as pd.
- 34:52This is all normal.
- 34:53And then it says pandas df, it's a variable, equals pd.dataframes.
- 34:57So it creates one.
- 34:57And here's where I need the help.
- 34:59The next line is duckdb.sql, select star, in the quotes, pandas df.
- 35:05How do those two things connect?
- 35:08That's a great question.
- 35:09Because you never, like, take the actual object of the data frame and show it to DuckDB in any way.
- 35:14Like, you don't pass it to a function or set a property or nothing.
- 35:17Exactly.
- 35:18And this is the benefit of being an in-process database.
- 35:20We are in the same memory space as Python, which means we can actually read the Python locals for its local variables.
- 35:28And DuckDB has been built from the very beginning with this concept of interchangeability of storage format.
- 35:34That's why you can read parquet files or read CSV files, because we treat them as first-class citizens in our database.
- 35:40And data frames are no different.
- 35:41What we'll do first when we see a select statement come in and it's got a table in there.
- 35:45In this case, it's pandas df is the name of the table.
- 35:47First thing we'll do is, hey, do we have a physical DuckDB table like that?
- 35:50Is there a table of that name?
- 35:51And if we don't find one, we don't give up.
- 35:54We keep looking.
- 35:54We say, okay, is there another object we know how to read with that name?
- 35:58And so we look in Python's local variables.
- 36:00There's a variable named pandas df.
- 36:02And we check the class name.
- 36:04What class is this?
- 36:05Is it pandas?
- 36:06Is it polars?
- 36:06Is it Apache arrow?
- 36:07And if it's one of those, we can treat it just like a table and then run SQL right on top of it.
- 36:13Pretty wild.
- 36:14It's pretty wild.
- 36:15I think one of the real values there is that you don't have to do, presumably, I'm presuming, you don't have to do a data conversion from pandas into whatever in-memory thing DuckDB thinks is ideal and then back.
- 36:30That's a great question.
- 36:31So you definitely don't have to do it all at once.
- 36:33So pandas, sometimes it is Python objects under the hood, whereas DuckDB is written in C++.
- 36:37So it will change some things, but it'll do it in chunks like we're talking about.
- 36:41So it won't be all up front.
- 36:43But for things like polars and arrow, in most data types, it's zero copy where it's the same format.
- 36:51DuckDB has slightly different data formats.
- 36:54And Apache arrow is actually adjusted to align closer to DuckDB in some of their format, like how they store strings.
- 37:00So DuckDB has really been one of the leading databases in terms of how it handles strings.
- 37:04And Apache arrow is also making those changes.
- 37:08Yeah, that's super cool.
- 37:09You can't write back to these, can you?
- 37:11You can't say, wait, you can't.
- 37:13You can't.
- 37:13You can say update, like the data frame variable, PD data frame set, whatever.
- 37:19Yeah.
- 37:20So if you're updating the data frame, you will overwrite the whole data frame.
- 37:24But DuckDB table, you can run updates on that object for sure.
- 37:29Yeah.
- 37:30But you can write data frames for sure.
- 37:33So you can replace the whole thing.
- 37:34Right.
- 37:34But I couldn't change the 172nd row of that data frame with a SQL query.
- 37:39Or could I?
- 37:40You would just be making a copy of the object.
- 37:44Got it.
- 37:44Got it.
- 37:45Yeah, that's what I thought.
- 37:46You would make the changes, but then you would return as data frame and make a copy of it.
- 37:50Correct.
- 37:50We would return a different object in memory.
- 37:52But functionally, it would be the same.
- 37:54And DuckDB is fast enough where it would probably be quite performant still.
- 37:57Yeah, probably fine.
- 37:58Yeah.
- 37:58Okay.
- 37:59I'm not saying that it should be able to do.
- 38:00I'm just trying to figure out how far down this.
- 38:02How far down?
- 38:03Yeah.
- 38:03Yeah.
- 38:05How far down does it go?
- 38:07Yeah.
- 38:07So it does the same kind of magic with the locals, with Pyro and Polars and so on.
- 38:13Mm-hmm.
- 38:13So there's different ways to get data back.
- 38:16And it doesn't really matter where it came from, if it came from Pandas or DuckDB files itself,
- 38:21or even just hard-coded SQL.
- 38:23But you can say things like fetch all, which will return you Python objects, or .df will return
- 38:29you a Pandas data frame or different things, right?
- 38:31Yes.
- 38:32Yep, we've got .pl for polars, .aero.
- 38:35You could do NumPy arrays if you'd like.
- 38:37We also support outputting to TensorFlow tensors and PyTorch tensors.
- 38:43Wow.
- 38:43Because those are NumPy under the hood in a lot of ways.
- 38:46So we can output to that as well.
- 38:48Okay.
- 38:48That's super cool.
- 38:49Do I need to be worried about little Bobby tables, SQL injections?
- 38:53We could definitely pass in parameters that will get escaped properly and all that.
- 38:58You know, with DuckDB, I think that in general, we take the approach of being
- 39:02super flexible, but there's a lot of options to lock it down should you choose.
- 39:06So the select star from any CSV file on my hard drive, you could turn that off if you want.
- 39:12For example, likewise with the data frames, you can disable that also.
- 39:16Yeah.
- 39:17I guess what I'm asking more, is there a concept of a parameterized query or something like that?
- 39:22Yes.
- 39:23Yes.
- 39:23So you can follow the same Python DB 2.0 kind of API spec.
- 39:27You could do .execute and pass in a query with placeholder parameters and then pass in those as arguments.
- 39:33So you can absolutely do that.
- 39:34It tends to be not good for bulk ingestion.
- 39:38The reason for that is Python objects just have a lot more.
- 39:40Oh, well, I lost your audio.
- 39:43Did you possibly hit something that hit a mute button?
- 39:46I did.
- 39:47Sorry.
- 39:47Okay.
- 39:48Sorry.
- 39:48Give us the...
- 39:50You said it's not good for bulk ingestion.
- 39:52Let's go from there.
- 39:54Yep.
- 39:55Sorry.
- 39:55Oh, all good.
- 39:56That's on me.
- 39:57So you can insert straight from Python objects with parameters.
- 40:02It works really great.
- 40:03It's very convenient, but it's not great for bulk ingestion.
- 40:06Inserting a million rows that way versus a million rows in a Pandas data frame, it's going to be a couple orders of magnitude faster to do it through a data frame.
- 40:14Yeah.
- 40:14Yeah, absolutely.
- 40:15So if by default, if I just do WDB.sql, this is working in this correctly this time, in memory database.
- 40:25But if you create a connection to a file, then that where you just call it whatever you want, right?
- 40:31Like in your examples, you got file.db.
- 40:33That will then start persisting to that file, right?
- 40:37But it's pretty simple to do either.
- 40:39Yes.
- 40:40You're just changing the connection path.
- 40:42And if you pass in no path, it'll be in memory.
- 40:46Otherwise, you can point to a file.
- 40:47And it's really that straightforward.
- 40:50It'll save all your tables into that same file, nice and compressed in columnar format.
- 40:54And then now the format is all stabilized.
- 40:57You can throw that on an object store, share it with anybody, put it in your data lake, anywhere you want it to go.
- 41:01Can I read from objects or can I give it an S3 connection?
- 41:04Yes, indeed.
- 41:05You can read from any of the major clouds.
- 41:08So AWS, Google, Azure, you can read individual files or actually it'll understand hierarchies of files.
- 41:15So if you have like a top level folder for a year and a folder for month and day, if you filter for I only want three days worth of data, DuckDB will only read three files.
- 41:24So it's pretty slick.
- 41:25Oh, wow.
- 41:25That's wild.
- 41:25Okay.
- 41:26Yeah, that is really slick.
- 41:27And you can even write out to those as well.
- 41:29So you can actually write on object stores too.
- 41:31That's amazing, actually.
- 41:33And a lot of less hyperscale clouds, I don't know, I was almost calling them lesser clouds, but I think of almost as like even better clouds.
- 41:40You know, DigitalOcean, Hetzner, Leno, some of these smaller places, a lot of them have their own object store.
- 41:47And the object store API is just a different connection string for the S3 API, right?
- 41:54So it sounds like you could probably get that to work as well.
- 41:57Let's pass that.
- 41:58I believe I've seen MinIO used, which is even like self-hosted if you want.
- 42:01So if it handles MinIO, you can kind of put it wherever you want it.
- 42:04Yeah, yeah, that's for sure.
- 42:05MinIO is awesome.
- 42:06I haven't got a chance to put MinIO into any useful work, but it's like a self-hosted S3 basically, right?
- 42:16Which is really cool.
- 42:16Yes.
- 42:17Yeah.
- 42:17So I don't know.
- 42:18It seems like a really neat system.
- 42:20But it's also, MinIO is complicated.
- 42:22You know, it's not easy to set up and run.
- 42:26It seems like there's a lot going on there.
- 42:28So that's why I haven't done it yet.
- 42:30I'm like, this looks amazing.
- 42:30Oh, this looks complicated.
- 42:32What else am I going to do today?
- 42:33I think S3 is pretty great.
- 42:36Drop a file, read a file.
- 42:38So it's not a bad starting point for quite a while.
- 42:41Yeah, absolutely.
- 42:41So with this connection object, like if you want to have a persistent database, right?
- 42:46You create a connection object and then it has basically the same API as the DuckDB itself.
- 42:52So you can run SQL queries.
- 42:53You can do table stuff on it and so on.
- 42:54What's the concurrency story with this thing?
- 42:58Like, so for example, what if I open, what if I try to use DuckDB in Flask and I have Flask running in Gunicorn and I say, Gunicorn, please use four worker processes.
- 43:10And I try to talk to it.
- 43:11It's a great question.
- 43:12So am I going to be having a bad time or having a good time?
- 43:15It depends a little bit.
- 43:16It depends.
- 43:16It depends a little bit.
- 43:17So, but yes, it's, there's some trade-offs.
- 43:19There's reasons that there are databases that are in process and that there are databases that are client server.
- 43:24And so whenever you want to write data to DuckDB, you can only write data from within a single process.
- 43:30And you'll open up a read-write connection and it will actually disallow any access from any other process.
- 43:36And it'll be single process access while you're doing reads and writes.
- 43:39It does handle multi-threading just fine.
- 43:42So you can have multiple Python threads.
- 43:43You get nice parallel performance that way.
- 43:46No problem.
- 43:46But it's all within the same memory space because you're using threads.
- 43:49So we're pretty excited about free threading coming soon in Python.
- 43:52Yes.
- 43:52Yeah.
- 43:53Well, that's going to knock out the need for the web, the web garden, not the web farm, the web garden,
- 43:59where you've got a bunch of these copies of the web server running in parallel because they're all trying to have separate gills so they can be more concurrent.
- 44:07That's not the only reason, but that's a big reason in Python.
- 44:10That's definitely a big reason.
- 44:11So that we're excited about.
- 44:12If you want to be read-only, so since DuckDB is an analytical database and since we can read from files,
- 44:19there are a lot of use cases that are read-only where if I'm reading from parquet files on an object store,
- 44:24I might not be doing any write operations at all, at which point multi-process,
- 44:29access is totally fine as long as you're in read-only mode for all of those processes.
- 44:33I will add one big other thing to look into, though, is where I work, which is Mother Duck.
- 44:39So we're a cloud serverless data warehouse built around DuckDB and we'll handle the concurrency side.
- 44:45So it's really taking this amazing single-player DuckDB experience and bringing it into the cloud as a full multiplayer cloud data warehouse experience.
- 44:53So you get concurrency, you get automatic managed storage, it's very efficient, and you also get access control and some of the things you associate with a large-scale database.
- 45:03That's the story. If you need concurrency but you're using DuckDB, I think Mother Duck is a great option.
- 45:09Right. So this is kind of DuckDB as a service. Is that right?
- 45:13That's definitely a key piece of it. I think we have a... DuckDB is a huge part of what we do and it is the engine under the hood.
- 45:21I think we are using that to really be meeting the use case of being not just sort of a cloud hosting for DuckDB, it's to really be a full data warehouse with DuckDB as the engine.
- 45:33So a little bit of a distinction there, but just as a whole organization could kind of use it as opposed to just one person, you know, cloud hosting it, something like that.
- 45:41Yeah. Well, it seems like a really, really great service. And I guess it's worth calling out that DuckDB, the thing that runs in your process, is available on as an MIT licensed thing, right? Which is, that's pretty amazing.
- 45:54And you, I saw somewhere, I can't remember where I saw it, but it's something like, you'll promise to be MIT, to stay MIT. No rug pulling, the rugs will stay.
- 46:02Yes. So it's a pretty cool kind of corporate structure. And if you can say a cool corporate structure, we'll see if we can get away with that. But as far as corporate structures go, it's pretty cool, right?
- 46:12So there's actually three organizations at play. And one of them is a nonprofit foundation called the DuckDB Foundation. And they actually own the intellectual property for DuckDB.
- 46:22And that is what ensures that it'll be MIT licensed forever, because it is not a company that owns DuckDB, which is amazing.
- 46:29So there is like, it's not even possible to do a rug pull, even if somebody wanted, which nobody does, right? It's not even possible. It's against the law, which is pretty cool.
- 46:37So yeah.
- 46:38I guess maybe give people, I threw that out there. There might be people who don't know what that means.
- 46:43Oh, what a rug pull is?
- 46:44Yes, exactly. Yeah. What is that?
- 46:46You know, certain products, once they're open source, they begin to, they get funded in certain ways to where they need to be, you know, selling it as a product.
- 46:56And at times that means that either they don't always put everything back in open source, or there's different licensing models that get changed,
- 47:04so that the license is a little bit more restrictive than it was in the past.
- 47:07Kind of the typical maneuver there for a couple different companies has been around Amazon will just host your product.
- 47:14And then it's very hard to have your own business if Amazon just hosts it.
- 47:18So we're not in that situation with DuckDB because it's open source forever.
- 47:24But also Mother Duck is not just wrapping DuckDB.
- 47:27We're innovating at the, at that service layer, where with Mother Duck, one of its secret sauce ingredients is it's not just a cloud service.
- 47:36When you connect to Mother Duck, you have DuckDB running locally and in the cloud.
- 47:40And one query, one Python statement can actually run partially in the cloud and partially on your laptop.
- 47:45Oh, wow. Okay.
- 47:46And we actually can, we can choose the optimal location for that to run based on your query plan.
- 47:51We actually do that as a part of the optimization to say, where's the most efficient place for this to run?
- 47:55And that's some serious secret sauce that, you know, it's not just, just wrapping, you know, an open source solution and hosting it.
- 48:02That gives some really nice benefits for things like the local development experience.
- 48:05You can develop locally at light speed and then push to the cloud at the final step.
- 48:10Or alternatively, kind of reverse the polarity.
- 48:13Maybe your first step is to bring everything down locally in a cache and then do all kinds of data science processing on it.
- 48:19So it's, it's very convenient to have both local and cloud.
- 48:23I see.
- 48:23Kind of like we talked about writing a query and then turn it into a data science object and then asking questions.
- 48:29This is a little bit like distributed versus local, but staying within DuckDB.
- 48:33It's definitely, you get to use server side computer, local compute, however you'd like.
- 48:38Yeah.
- 48:39Super cool.
- 48:40I love the idea of it.
- 48:42It's a lot of fun.
- 48:42We're, we're, we're doing a lot of fun things and we, we, you gotta have fun with it as well.
- 48:47You know, what do you call someone who works at mother duck?
- 48:49A duckling.
- 48:50That's the second most popular one.
- 48:53We actually were mother duckers, Michael.
- 48:55Oh, okay.
- 48:56Mother duckers.
- 48:57All right.
- 48:57You gotta have fun with it.
- 48:59Right.
- 48:59Otherwise it's all just ones and zeros at the end of the day.
- 49:01Right.
- 49:01You have to have fun with it.
- 49:02Yeah.
- 49:03You have to have fun with it.
- 49:04You should.
- 49:04I don't know.
- 49:05Ducklings.
- 49:05But mother duckers is good.
- 49:07Still.
- 49:07I guess ducklings might send the wrong message.
- 49:09They're all good.
- 49:10Do you want to, do you want to like sort of a tough, tough persona or facade, or do you want
- 49:17to like a, you know, coddled, whatever?
- 49:19No, it's a great name.
- 49:21It's, you know, like if you look at the branding on our website, you know, we're not going for
- 49:25like, we're not like a super muscly duck, you know, at our level ranges.
- 49:29We're definitely in the playful side.
- 49:30And part of it is because we really think that back in the big data era, we believe the big
- 49:36data era is over here at Mother Duck.
- 49:38And the reason for that is you no longer need 50 servers to answer your questions.
- 49:43It turns out laptops are pretty daggone fast now.
- 49:45Yeah.
- 49:46And you can do a lot.
- 49:47Even, even if you're not able to handle it on a laptop, how about one giant node on AWS?
- 49:52You can rent a node with multiple terabytes of RAM, of RAM.
- 49:57I mean, you could do a lot, right?
- 49:58So at that point, what is a single node?
- 50:01You can kind of do, do almost anything that you need.
- 50:04So as a result.
- 50:05And single server versus distributed is way easier.
- 50:09Yes.
- 50:09It allows you to innovate a lot faster.
- 50:10You can use better algorithms.
- 50:12So in many cases, it's actually just plain old faster to do it that way.
- 50:15A lot of benefits.
- 50:17But what we realized fundamentally at Mother Duck is, in a lot of ways, DuckDB as well.
- 50:21The scale of data shouldn't be the most important thing anymore.
- 50:24It should be how easy it is to use.
- 50:26You should be able to do select star from a CSV.
- 50:29You know, it doesn't take scale to parse a CSV file in an intelligent way.
- 50:33It takes elbow grease.
- 50:34It takes innovation in terms of the algorithm.
- 50:37But at the end of the day, it's not about how big is your CSV file.
- 50:40It's about how long did it take me to get my job done.
- 50:42And in Mother Duck, it's a similar kind of pragmatism.
- 50:45And our branding, I think, does a really good job of showing that.
- 50:47I think so.
- 50:48Well, I asked you about half of what I said I was looking forward to asking about, which
- 50:53was the table schemas, how to do that.
- 50:55However, what about indexes?
- 50:57Sure.
- 50:58I think indexes are a great question.
- 51:00So DuckDB does support indexes.
- 51:02But in many cases, it's not necessarily something that you need.
- 51:06And the reason for that is because of the workloads that it's best for, but also because of columnar
- 51:11databases.
- 51:11So when you have a columnar database, by default, it's going to create some rough indexes for you
- 51:18automatically.
- 51:19So every about 100,000 rows in DuckDB, we create an index there that says, what's the minimum value
- 51:25in this chunk and then the maximum value in this chunk.
- 51:28And what we'll do is then if you run a query that says select data from this week, we're going to check,
- 51:32hey, which blocks have any data from this week?
- 51:36And we can skip 90% of your data because we know that it doesn't match that filter.
- 51:41But that's not like an individual row index.
- 51:44It's an index for every 100,000 rows.
- 51:47And it's for the analytical queries where you're looking at trends, where you're analyzing
- 51:51large amounts of data, it tends to be really the optimal way to go about it.
- 51:54And that's the way most systems do it.
- 51:56There is an external index you can add.
- 51:58It's an art index, an adaptive radix tri index, which is a pretty cool way of doing it.
- 52:04It's a little different than the B tree in SQLite, but it allows for those vast point lookups
- 52:09where if you want to grab one row, you can get a very quick mapping to that individual row.
- 52:14It tends to be just less necessary in DuckDB to do that, but we have it if you need it.
- 52:19I'm realizing that I need to learn more about databases, especially columnar ones, because
- 52:25they're not matching my mental model as much as, say, for relational or document ones.
- 52:29It's definitely a different world.
- 52:32Different world.
- 52:32And that's, I think, the fun of it is I think of databases as an infinitely deep rabbit hole
- 52:38of really just fun optimizations, fun things to learn about.
- 52:43So a lot of exciting stuff.
- 52:44Yeah.
- 52:44When you get that stuff right, it's like magic.
- 52:46It's like, how could it possibly answer that so quickly?
- 52:49At the same time, when it's wrong, it's really frustrating.
- 52:52You know, you go to a website and you don't say, oh, you know, click this thing here.
- 52:57And then you try to, and it just sits there and spins.
- 52:59Like, I know this is happening without an index.
- 53:01And I know this could be so easily fixed.
- 53:03And it's just not.
- 53:04Why, why is it so bad?
- 53:06But, you know, you can make your stuff fast.
- 53:08You can't make other people's stuff fast.
- 53:09So I guess it's an advantage.
- 53:10Makes you look good.
- 53:11Yes.
- 53:12So I think that the workloads are just so different, but the mental model, it does open doors in
- 53:18a lot of ways.
- 53:18So for example, if you want to get the average of like a billion rows at DuckDB, it's a couple
- 53:23seconds.
- 53:24Wow.
- 53:24It's that fast.
- 53:25You know, so just the things that you can do with that kind of tool, an analytical oriented
- 53:31tool, really unlocks things you can do in an application sense.
- 53:34So data apps, we think are a really interesting market for both DuckDB and MotherDuck, where,
- 53:40you know, it's not just show me a little bit of data.
- 53:43Maybe it's an event tracker, like a fitness tracker.
- 53:46You know, I want to see the history of my bike rides for the last month.
- 53:50And I want to see the trends, my moving averages, you know, highlight the outliers for me.
- 53:54You know, there's a lot of heavy analytical work to be done in data apps that an analytical
- 54:00database is really good for.
- 54:00Yeah.
- 54:01Give us some, I don't know how much you can share.
- 54:04I know you interact with these customers quite a bit.
- 54:06So, you know, but I don't know how much you can share.
- 54:08Give us some senses of what people are doing.
- 54:10Sure.
- 54:11On the MotherDuck side or the DuckDB side?
- 54:13Well, you know, give us a little example from each, maybe that you think is representative.
- 54:17You bet.
- 54:18I think on the DuckDB side, I think some of the very interesting things are when you use DuckDB
- 54:24as a data processing engine, where each individual processing task is, you know, one node, but you're
- 54:30doing a ton of them in parallel.
- 54:31So some companies will actually take DuckDB and use it to transform one Parquet file into
- 54:36an aggregated Parquet file.
- 54:38And they'll just set it up on a trigger.
- 54:39So when a Parquet file gets added, they'll automatically just create a little pipeline with DuckDB and
- 54:44convert it into a much, you know, cleaner, more aggregated form.
- 54:47And you could do that at huge scale.
- 54:49So there's some companies that are doing huge amounts of data processing, you know, with
- 54:53DuckDB.
- 54:54Tons of tiny DuckDBs, a flock of tiny DuckDBs running behind the scenes.
- 54:59So that's an interesting use case that, you know, DuckDB originated as a data science tool,
- 55:04but I think the data engineering side of DuckDB is also very exciting, where it fits really well
- 55:09in those pipelines.
- 55:10On the MotherDuck side, I think what we see is a lot of cases where the transactional database,
- 55:15you know, a lot of cases, it's Postgres.
- 55:18It's got a really friendly SQL syntax, and it aligns very closely with DuckDB's SQL syntax,
- 55:23where Postgres just is too slow for those kind of trend-oriented aggregates or join queries
- 55:28that you want to run.
- 55:30And MotherDuck ends up being a really great way to very easily get a lot more speed in those cases.
- 55:35And that could be speed for data engineering tasks.
- 55:37Like some folks are running data processing jobs with this tool called DBT, stands for Data Build
- 55:43Tool, and it's a Python library.
- 55:46And they were running these, you know, sets of computation and aggregations, and they would
- 55:51take, you know, eight hours.
- 55:52And if it starts running, you know, 2 a.m., by the time it errors out, you've lost your whole
- 55:57day of your data analysis being up to date.
- 56:00Yeah, you better get it right, right?
- 56:02Better get it right.
- 56:03Unfortunately, DBT does also stand for, you know, behavioral therapy of a certain kind.
- 56:07So you've got to search for DBT data.
- 56:09It's a tough acronym.
- 56:11Yeah, it's a little short.
- 56:13Yeah, DBT Python, that'll get you there too, yeah.
- 56:16With MotherDuck and with DuckDB, you can take that, and instead of being eight hours, you can
- 56:21run it in, you know, 15 or 30 minutes.
- 56:23And just that scale of change means that, you know, it's just a far more delightful experience
- 56:29to do those kind of data pipelines on an analytical tool as opposed to a transaction.
- 56:33So that's one.
- 56:34The other one is business intelligence tools.
- 56:36In a lot of cases, those start to get really slow on a transactional database because if
- 56:40you're looking at a graph, it's by most of the time, it's a trend.
- 56:43And most of the time, it's looking at a lot of data to plot.
- 56:46Even if it's just plotting a few points, it's looking at a lot of data to plot it.
- 56:49And DuckDB is really excellent for that, and MotherDuck as well.
- 56:52There's actually a lot of business intelligence tools where they are powered by DuckDB.
- 56:56So Mode was acquired by ThoughtSpot.
- 56:59They're powered by DuckDB.
- 57:00Hex is a data science notebook.
- 57:02They have DuckDB as a part of their architecture.
- 57:05There's a couple others.
- 57:06We talked about some of the data types.
- 57:08What about JSON or document type of things, right, where you've got a little hierarchical
- 57:14data?
- 57:14You know, there's on one end of that spectrum, we have MongoDB.
- 57:18That's all it does.
- 57:18And on the other, you've got relational databases like Postgres that say, well, this column can
- 57:23just be JSON arbitrary stuff.
- 57:26Yes.
- 57:27And we're in that space where we absolutely believe that's how most things are, a little
- 57:31bit of both.
- 57:32So there is a full JSON data type in DuckDB.
- 57:35And so you can store any arbitrary document in there and then process it with some really
- 57:40fast JSON processing.
- 57:41We can also take JSON data and automatically split it out into columns if you want to unnest
- 57:47it.
- 57:48So you can kind of go back and forth there.
- 57:49Oh, nice.
- 57:49There are also specific DuckDB nested types if you want to enforce the typing at the column
- 57:56level, but you want to be nested.
- 57:57So you could say, I know these are going to be my keys.
- 58:00I know this will be my structure.
- 58:01You can store it all in one column and it'll be very, very quick to work with.
- 58:06So you kind of have both.
- 58:07You have the full flexibility with JSON and then you also can have nested types.
- 58:11Can you query into them?
- 58:12So if I have a JSON thing that's got like purchases and then it's got a bunch of purchase objects
- 58:17and then like values, can I say, give me the ones that have over $100 purchases?
- 58:21You can do much like Postgres and SQLite have the ability to extract pieces of JSON with
- 58:27the path syntax of like go to this path and pull that value out.
- 58:30You absolutely can do that in DuckDB as well and say, okay, navigate to the customer object
- 58:37and then inside the customer object, go to this object and absolutely.
- 58:41Yeah.
- 58:41Super cool.
- 58:42All right.
- 58:42Well, I think there's probably more to dive into, honestly, but we've covered it.
- 58:47I think we've covered it pretty well.
- 58:48I think DuckDB is super exciting for the possibilities that it opens up.
- 58:52You know, there might be a ton of people out there who haven't heard of it or really just
- 58:56heard of it in passing and didn't really know.
- 58:58This in process stuff that you can do, it really makes it quite accessible, quite easy,
- 59:03lowers the bar to getting started, right?
- 59:05You don't have to understand connections, security, servers, daemons, all of that.
- 59:10So I'm excited to see where this goes.
- 59:12It's already got a ton of traction.
- 59:13So final call to action, maybe tell people who are in that group, what do they do?
- 59:18You bet.
- 59:18I think, you know, obviously, pip install DuckDB, give it a try.
- 59:21It's anywhere you can use it.
- 59:23MIT license, it really can fit anywhere that you're running Python.
- 59:27A whole lot of other fun things you could do on top of it.
- 59:30There's a new extension ecosystem.
- 59:31So if it doesn't exist in DuckDB, but you'd like it to, you can actually build an extension
- 59:36for DuckDB in a variety of languages.
- 59:38And so we can all together make DuckDB into whatever we'd like.
- 59:42So it's pretty neat.
- 59:43Yeah.
- 59:44Well, good work.
- 59:45And I love to see the open source side and then maybe a strong company that's built around
- 59:53it in a way that doesn't really undermine the open source value.
- 59:56So MotherDuck, DuckDB, it looks like a really good relationship there.
- 59:59So that's nice.
- 1:00:00Yeah.
- 1:00:01We even have two companies.
- 1:00:02We got DuckDB Labs and MotherDuck.
- 1:00:04Yeah, that's right.
- 1:00:05Got the foundational side as well.
- 1:00:07Yeah.
- 1:00:07Awesome.
- 1:00:08Well, Alex, thanks for being on the show.
- 1:00:09And yeah, thanks for sharing all this stuff with us.
- 1:00:12It's great.
- 1:00:12Thanks for having me.
- 1:00:13Cheers, folks.
- 1:00:14Happy analyzing.
- 1:00:14Yep.
- 1:00:15Bye.
- 1:00:15Bye.
- 1:00:16This has been another episode of Talk Python To Me.
- 1:00:19Thank you to our sponsors.
- 1:00:21Be sure to check out what they're offering.
- 1:00:22It really helps support the show.
- 1:00:24Take some stress out of your life.
- 1:00:26Get notified immediately about errors and performance issues in your web or mobile applications with
- 1:00:31Sentry.
- 1:00:32Just visit talkpython.fm/sentry and get started for free.
- 1:00:37And be sure to use the promo code talkpython, all one word.
- 1:00:40And it's brought to you by the Data Citizens Dialogues podcast from Colibra.
- 1:00:44If you're ready for a deeper dive into the latest hot topics and data, listen to an episode
- 1:00:49at talkpython.fm/citizens.
- 1:00:51Want to level up your Python?
- 1:00:53We have one of the largest catalogs of Python video courses over at Talk Python.
- 1:00:57Our content ranges from true beginners to deeply advanced topics like memory and async.
- 1:01:02And best of all, there's not a subscription in sight.
- 1:01:05Check it out for yourself at training.talkpython.fm.
- 1:01:08Be sure to subscribe to the show, open your favorite podcast app, and search for Python.
- 1:01:13We should be right at the top.
- 1:01:14You can also find the iTunes feed at /itunes, the Google Play feed at /play, and the
- 1:01:20direct RSS feed at /rss on talkpython.fm.
- 1:01:24We're live streaming most of our recordings these days.
- 1:01:26If you want to be part of the show and have your comments featured on the air, be sure to
- 1:01:30subscribe to our YouTube channel at talkpython.fm/youtube.
- 1:01:35This is your host, Michael Kennedy.
- 1:01:36Thanks so much for listening.
- 1:01:38I really appreciate it.
- 1:01:39Now get out there and write some Python code.
- 1:01:41You
- 1:01:57You
- 1:01:59you
- 1:01:59you
- 1:02:00Thank you.