Ways to Compute Pi in SQL

Clock Icon

2 min read

Pencil Icon

Mar 14, 2020

Ways to Compute Pi in SQL

In celebration of Pi Day, I asked SingleStore’s Engineering team to come up with clever ways to compute pi in SQL. Because that’s the kind of thing we engineers enjoy! And, given pi’s privileged status in mathematical history – and my own history of earning a bachelor’s degree in Mathematics – I just couldn’t resist.

Truth be told, in the early days of SingleStore, my fellow math geeks and I had some fun with Pi Day – yes, including eating actual pies – and we even had a colleague with the nickname of Pieguy. Pie, not Pi, btw. So, if anyone was going to put together a blog post on calculating pi in honor of Pi Day, it would be me.

How else is SingleStore Pi/Pie-forward? Here are a few fun facts:

  • All of our conference rooms in San Francisco are named after mathematicians and their accomplishments: (Thomas) Bayes, (Emmy) Noether, (Maryam) Mirzakhani, (Alan) Turing, and (John) Venn. (Oh, and the conference rooms in our Portugal engineering office are named for pastries. And, if you squint hard enough, pastel de nata – the name of a conference room – is a mini-pie!)
  • All of our product releases are named after pies. We used to eat pie for major releases. For instance, for icecreampie, we ate ice cream pie. But we haven’t eaten any fictional pies like KeyUsingClusteredColumnstorePie (lol). We’re on Kalm at the moment, which stands for keypie a la mode. I dare you to top that!

That being said, we had fun putting this post together, and we hope you have a great #piday.

lets-use-stored-proceduresLet’s Use Stored Procedures

Code is available in Github.

lets-generate-a-large-ish-dataset-with-some-random-dataLet’s Generate a Large(-ish) Dataset with Some Random Data

Code is available in Github.

From there, the conversation digressed into the quality of our RAND() implementation and our engineers started to compare it with loading data from /dev/urandom with data generated by calling RAND().

Code is available in Github.

we-also-got-an-alternative-solutionWe Also Got an Alternative Solution

Code is available in Github.

This one outputs pie – the author says it is a pecan pie, but it’s hard to tell for sure.

This is all fun and good, but of course you can just do:

memsql> select PI();
+-------------------+
| PI()             |
+-------------------+
|        3.141593 |
+-------------------+

1 row in set (0.00 sec)

Thank you all for reading! Now, time for some cherry pie.

With thanks to Carl Sverre for graphics and code storage.


Share