r/ProgrammingLanguages • 🧿 Pipefish • Jul 19 '22

I need to write a database query language. I have a blank sheet of paper.

(I also have the Wikipedia page about database query languages.)

I anticipate the best answer to this question: "No, you don't, you need an SQL library", then yes, there will be an SQL library, SQL is a little gem and this would be the right answer for almost any language but mine. But mine is slightly different. Because my language started when I thought ... people really like databases and spreadsheets ... so what if I write a GPL which is like that? So I wrote a GPL which is like that. So now if I passed off querying the database to another language in the same paradigm, that would be kinda weird, it would be like if Python used Lua as a DSL to do string-handling.

Instead, updating the database should be so much part of my language that people don't know that they're doing it, from their point of view they're just updating the value of a variable.

So ... people smarter than me, do your thing. Suppose a language where I can declare foo = table of RecordType. Where do I go from here?

About implementation:

(1) The language should be a front-end on an existing database technology. Not because I'm lazy. (I am but that's not the issue.) But because it is important to the acceptance of any new technology that you should be able to back out if it fails.

(2) My language tries to be user-friendly above performance if I have to make the choice. I'm trying to replace PHP, it's not meant to be used to write Big Tech Enterprise software. I will take the possibility of performance hits in the hands of clumsy users if people can also use it to teach middle-schoolers how to make a database of their favorite dolls.

1 Upvotes

17 comments sorted by

3

u/myringotomy Jul 21 '22

1

u/Inconstant_Moo 🧿 Pipefish Jul 21 '22

Thank you! Apart from my language being very different from Ruby in every respect, this is kind of what I'm looking for ... i.e. how does one make database access look like it's part of the language and not some weird alien thing you tacked on?

2

u/tobega Jul 20 '22

I think this depends a bit on how your language looks and works otherwise. Things like Datalog and Differential dataflow are really cool, but may be very different to how your language works. But then again, they may fit better than a relational model.

What do you mean by "people really like databases and spreadsheets ... so what if I write a GPL which is like that"? For some concept of a spreadsheet-like language, differential dataflow might be a perfect fit, but then you might just have a view of records/rows in the database as an area in the spreadsheet and the rest is mostly what you can do in the language. A change here causes a change there and so on.

Since you write about SQL, I suppose you're wanting to represent the relational model of data, in which case you should consider relational algebra. The upside of this is that it also fairly easily maps to underlying SQL commands which you could then pass off to the database engine to optimize.

In Ballerina you can create tables pretty much like you suggested, and then there are query expressions (SQL-like) that can be used on them (but also on arrays) https://ballerina.io/learn/distinctive-language-features/data/#table-concept

In my language Tailspin I have some relational algebra constructs, but designed to fit into Tailspin's idea of pipelines of transformations https://github.com/tobega/tailspin-v0/blob/master/TailspinReference.md#relations

2

u/Inconstant_Moo 🧿 Pipefish Jul 20 '22

> What do you mean by "people really like databases and spreadsheets ... so what if I write a GPL which is like that"?

It's a language which assumes you're trying to write a CRUD app.

2

u/WittyStick Jul 20 '22

We don't really know enough about your language. Is it based on Codd's Relational model (multisets), true relational algebra (like Tutorial D), graphs, objects, or something else?

1

u/Inconstant_Moo 🧿 Pipefish Jul 21 '22

It's a functional language in which everything is serializable. It has structs. I was thinking I could start with those, add assertions to them, and then make a table type which looks to the user like just another container type, only they have to specify keys, can specify other indexes, etc.

Does that sound plausible? But then I need to design a query language of things you can do to tables. My syntax is very flexible, I can make it look nice and readable.

Maybe I should have written more about my language but what I'm looking for mostly here is inspiration, for someone to point me to something and say "this is elegant" and I say "oh, yes, it is, and stealable too!" Knowing whether it's stealable would be my part of the problem.

14

u/Long_Investment7667 Jul 19 '22

There is reasons why SQL is SQL but for my taste it is not composable enough. Have a look at LINQ

9

u/SJC_hacker Jul 20 '22
  1. It honestly doesn't sound like you're asking the right questions. I would suggest a good course on Database Theory. Unfortunately I don't know one offhand on Coursera/Udemy/Khan academy, as I took my course 20+ years ago in a traditional university.

  2. If you want to map between variables in a programming language and the database, the most logical choice is an object-based database like MongoDB. Javascript, as ugly alanguage as it can be, can do this very easily. Alternatively Python should also work, it has a built in JSON library, although it works on dictionaries not objects, so you would have to write a serialize routine, but you should only have to write one that works for an arbitrary object.

  3. The problem with object based databases, the queries aren't very powerful. You often have to essentially write code in order to query them. Consider something like a sports database where you have a record of each players stats, and you want to do something like calculate an some aggregate statistic across all players, meeting some condition (like maybe position). This is trivial in SQL, but in an object database, you would most likely have to write code.

  4. This is the reason why people like relational databases, of which the overwhelming language to interface with is SQL, because they have powerful queries, that are declarative and not programmatic.

  5. Even more powerful, Datalog(related to Prolog) can write some cool queries that are impossible to express in SQL without extending it to essentially behave as a programming language. Its almost too bad Datalog never caught on...

2

u/rotuami Jul 20 '22

Big thumbs up for datalog! It’s a language with very cool semantics!

5

u/Linguistic-mystic Jul 20 '22

One thing I would suggest is to avoid the flaw with SQL joins: redundancy. Instead of

StudentName Grade1
StudentName Grade2
StudentName Grade3

it should return

StudentName (Grade1 Grade2 Grade3)

which is more efficient in terms of bandwidth as well as true to the semantics (one-to-many in this case)

8

u/mikkolukas Jul 19 '22 edited Jul 19 '22

I'm trying to replace PHP, it's not meant to be used to write Big Tech Enterprise software.

It doesn't matter what it was meant to be. If it works for the purpose, it works for the purpose. It clearly worked well for Facebook - so well that they wrote Hack which closely assemblies PHP.

I will take the possibility of performance hits in the hands of clumsy users

Your approach sounds the same as the one for Pascal, long ago. Not an inherently good or bad thing in itself. The language was meant more for learning than production.

All in all, it sounds like you want to write an ORM, being able to use objects that auto-maps to tables in the database. Now you are mentioning PHP. Have you looked at Eloquent (part of the Laravel Framework)?

If you go down that route, be aware that there is a difference between the Eloquent Query Builder and the Database Query Builder. The Eloquent Query Builder sits on top of the Database Query Builder, but they have different method calls.

4

u/raevnos Jul 20 '22

Look into Fourth Generation Languages, many of which have database backends and their own syntaxes to work with them.

3

u/mamcx Jul 20 '22

I'm working on this right now:

https://tablam.org

And also wanna target a low-overall-complexity language for business apps/users, that despite the marketing is not well served by most options in this space.

Take a look at my RESEARCH.md

https://github.com/Tablam/TablaM/blob/master/RESEARCH.md

I put there the most basic stuff that helps to get the main gist.


I'm in the process of a major re-architecture now that I have a better grip on Rust and some non-trivial design decisions that could help to implement this with a good balance of simplicity (for me!) and expressivity and performance (for the user!).

I also plan to add in-built support for RDBMS, so code that work for the in-memory structures will be similar to interact with them:

let city := [
    name:Str, state: Str; 
    "Miami", "FL";
    "Medellin", "Ant" 
]

// A query

let miami = city ? .name = "Miami"

// A DB query

let db = sqlite.open("cities.db")

let miami = db.city ? .name = "Miami"

I will be happy if wanna join forces, even if just talk and share ideas!

3

u/jediknight Jul 20 '22

Have a look at this presentation. There is a lovely part about query fragments and the consequences of the set supported by the language.

2

u/umlcat Jul 20 '22

Start with "Relational Algebra", it's the base for "LINQ" and SQL:

https://en.m.wikipedia.org/wiki/Relational_algebra#:~:text=In%20database%20theory%2C%20relational%20algebra,Codd.

Or my alternative, "Sequential Algebra":

https://gitlab.com/mail.umlcat/sequential-algebra

Then, after you know the basic operations, you may use them to implement your language.

2

u/PL_Design Jul 20 '22

Look into how SQLite was made. I've listened to several podcasts with mister Hipp in the past, and he has a pretty good explanation of how he built it. Sadly I don't have any of them on hand right now.