-
Notifications
You must be signed in to change notification settings - Fork 804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
SQLite support #161
Comments
Agreed! I'd love to add SQLite support. The biggest blocker right now is access to a high-quality SQLite query parser. The SQLite C API doesn't expose a parsing function as far as I can tell, which means we'll probably need to create a project similar to pg_query_go. We'll make this the official issue for tracking SQLite. |
Just a suggestion. The amalgamated sqlite source file can be used as a static or dynamic library. If you wanted to write to the C interface, you could get a parser for free if you're willing to track their development changes over time. Even if all you did was separate out the lemon parser generator and the generated parser, you'd have a leg up on writing your own, and a simpler path to future compliance with their changes. |
I spent a few days looking into the feasibility of reusing the parser present in the sqlite source code. I don't think it's going to work. The parse.y file is littered with C / C++ calls, making using the existing parser very difficult. The internal structs are not cleaning exposed during parsing. Instead, my current plan is to use the ANTLR, as they have an existing SQLite grammar. I played around with the generated parser today. It's not perfect, but it should fit the bill. |
Hi @kyleconroy, has there been any progress on this? I'm close to publishing build rules for sqlc in bazel, and it would be useful for writing and running tests that don't require a backend. |
In case it helps, note that someone has a pretty advanced cgo-free implementation of sqlite at https://pkg.go.dev/modernc.org/sqlite. I seem to recall that it's translated from C, so the internal APIs will likely not be nicer than the C ones, but perhaps you can reuse some of the code at least. The author is not on GitHub, but they are on Gitlab if you want to start a conversation: https://gitlab.com/cznic/sqlite |
In particular, the internal parser seems to be at https://pkg.go.dev/modernc.org/sqlite/lib#Xsqlite3Parser. |
I did a little investigation of using the internal parser from modernc's sqlite implementation. I wasn't able to get anything working that could be used in sqlc, but I wanted to share my progress in case this helps someone else. https://gist.github.com/maxhawkins/f162eaacddabbe380c7fb75791d85a56
This program uses the A better approach may be using the tokenizer ( I won't have a chunk of time to dive into this for a few weeks, but I'm hoping this inspires someone else to poke around with modernc.org/sqlite and maybe get a parser working. |
@maxhawkins I don't know if you had any further explorations, but I summarized this issue and included your investigations in https://gitlab.com/cznic/sqlite/-/issues/75 |
Current state is: #1260 (comment)
|
Update from @cznic - https://gitlab.com/cznic/sqlite/-/issues/75#note_720410495 :
|
Maybe this will help: |
It looks like there's a more maintained and up-to-date version of the antlr grammar for SQLite here: I started playing with migrating sqlc to use this. There are some naming differences that will require a bit of work to figure out, but I'm interested in doing this work if the maintainers think it would be useful? Is the antlr parser thought of as a dead end for this, or is it worth investing in improving it? Let me know and I'll see if I can get a PR together this week. |
FWIW, I just completed a port of pikchr to Go: gopikchr. As part of that effort, I also ported the Lemon Parser to Go: golemon. In all, it was almost 12,000 lines of code, hand-ported from C to Go. After that, the 849 lines in Anyway, just a thought. I'd do the port myself, but I've just exhausted my quota of idiotic-hand-porting-to-Go energy 🙂 |
I understand that hand porting is tedious, but what's the difficulty here? I have pretty limited c exposure but write enough go in the day to day. I have many personal projects that could benefit from generating sqlite and am willing to contribute. |
You're right, it's not difficult, just tedious. After playing around with |
I wonder how easy it is to run ccgo/v3 on just the 849 lines in |
ccgo/v3 does not understand |
Alright. I will update here if I end up taking a crack at this. |
One other lesson: where a dynamically sized array is only ever cleanly appended to, you can replace the pointer/current-index/current-size triple with a simple slice in Go. If the current index moves around, or the code frequently references |
I've been chipping away at implementing support using the antlr generated parser. Latest work here: #1410 — I can write up some issues if anyone else wants to jump in, otherwise I'll keep going on this for a few hours / week. |
Would love to see this soon! |
It looks like benbjohnson created a Pure Go sqlite parser here: https://github.com/benbjohnson/sql that is not currently maintained, but caught my eye. @PadraigK it looks like your PRs have been merged so #1397 #1410 #1443 #1447 #1455 how is the current support looking? |
@zellyn @jmillerv I attempted to port over |
I'm still interested in continuing on the path I'm on, I was just on vacation for a few weeks and haven't found time to get back into it yet. The support is coming along well, but not finished — the main missing thing is support for where clauses, but a lot of the pieces for that are in place. After that, I think it's just small details to handle more obscure SQL, so it may even be beta-testable by then. As a heads up, it is possible my work situation will change in the next month or so, and in that case I might not get to finish, but I'll let you know @kyleconroy. For what it's worth, I don't have any concerns about the ANTLR parsing side — 95% of the work is in mapping the ANTLR output into My process has been to work down through the existing tests and make SQLite versions of them where appropriate then I examine the output structs/functions and fix any bugs. This makes it quite easy to jump in and get something impactful done without much ramp up time. The test suite will also be useful to test other parsers / mapping approaches, so even if the native SQLite parser gets done, some of the work should still be useful at least :D |
@PadraigK I'm unsure that my experiment with golemon will work, so I still think the ANTLR based approach has the best chance to succeed. |
@PadraigK @kyleconroy I'm not sure what is the best SQLite parser for sqlc, but I can write code #1687 to support more SQL Syntax using current ANTLR based parser. I also added some examples and many end to end test data for SQLite in the PR. I think these examples and tests could help us for check backward compatibility, even if you decided changed parse implementation in the future. |
We now have beta support for sqlite! Gonna test it out this weekend. |
Looks good, thank you for the amazing work! Is there any issue summarizing what is supported and what is not supported on sqlite? (I am personally missing |
@oliverpool FTR I'm not a maintainer of sqlc, just a very happy user that is eager to get SQLite support :) |
Great work on the project and perfect timing on SQLite support, thank you! :) I gave it a go (latest commit on main: 8618c39) and it works nicely. As a feedback, there was one minor issue along the way... the project no longer works with Go 1.17:
I was able to work around it (built a Docker container and used sqlc inside it) so it is not a big issue for me, just wanted to share. Otherwise works great. |
Amazing work, I can't thank you enough! |
Do you want issues with the SQLite beta registered on the issue tracker? |
Yep! |
SQLite will be in beta for 1.15.0, so I'm going to close this out :) |
Hi all, great works on this project. Is it possible to add SQLite support ? Right now I'm working on embedded device and often uses SQLite, so it will be awesome for me.
The text was updated successfully, but these errors were encountered: