-
Notifications
You must be signed in to change notification settings - Fork 1.1k
SQL query interface (very experimental) #6172
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
Open
lutter
wants to merge
17
commits into
master
Choose a base branch
from
lutter/sql
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,794
−31
Open
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
c7d8f5b
store: Make Layout.table a little easier to use
lutter 261bc4a
graph, store: Create database sql executor
gusinacio 79dc39b
store: Use Layout for schema information
lutter 3574aea
store: Rewrite table names in from clauses
lutter 37dafa8
store: Revamp query rewriting
lutter 5df824a
store: Extract SQL parsing tests into a YAML file
lutter b113c15
graph, graphql, server: Add rudimentary SQL query support
lutter 4b498fa
store: Restrict the selectable columns to actual attributes
lutter 4988fa6
store: Defer wrapping of SQL queries until execution
lutter 09823bc
store: Move SQL validation tests to YAML test file
lutter 0fca92b
store: Limit maximum execution time for SQL queries
lutter 069a1ef
docs: Add some details on the SQL interface
lutter c313b1d
store: Enforce existing GraphQL first/skip limits for SQL queries
lutter adc0d5f
store: Add SQL query tests
lutter 6417279
graph, graphql, store: Make SQL queries configurable (default: off)
lutter 1125d3a
store: Quote column names in the generated part of SQL queries
lutter 6df4992
graph, store: Require function syntax for aggregations
lutter File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
# SQL Queries | ||
|
||
**This interface is extremely experimental. There is no guarantee that this | ||
interface will ever be brought to production use. It's solely here to help | ||
evaluate the utility of such an interface** | ||
|
||
**The interface is only available if the environment variable `GRAPH_ENABLE_SQL_QUERIES` is set to `true`** | ||
|
||
SQL queries can be issued by posting a JSON document to | ||
`/subgraphs/sql`. The server will respond with a JSON response that | ||
contains the records matching the query in JSON form. | ||
|
||
The body of the request must contain the following keys: | ||
|
||
* `deployment`: the hash of the deployment against which the query should | ||
be run | ||
* `query`: the SQL query | ||
* `mode`: either `info` or `data`. When the mode is `info` only some | ||
information of the response is reported, with a mode of `data` the query | ||
result is sent in the response | ||
|
||
The SQL query can use all the tables of the given subgraph. Table and | ||
attribute names for normal `@entity` types are snake-cased from their form | ||
in the GraphQL schema, so that data for `SomeDailyStuff` is stored in a | ||
table `some_daily_stuff`. For `@aggregation` types, the table can be | ||
accessed as `<aggregation>(<interval>)`, for example, `my_stats('hour')` for | ||
`type MyStats @aggregation(..) { .. }` | ||
|
||
The query can use fairly arbitrary SQL, including aggregations and most | ||
functions built into PostgreSQL. | ||
|
||
## Example | ||
|
||
For a subgraph whose schema defines an entity `Block`, the following query | ||
```json | ||
{ | ||
"query": "select number, hash, parent_hash, timestamp from block order by number desc limit 2", | ||
"deployment": "QmSoMeThInG", | ||
"mode": "data" | ||
} | ||
``` | ||
|
||
might result in this response | ||
```json | ||
{ | ||
"data": [ | ||
{ | ||
"hash": "\\x5f91e535ee4d328725b869dd96f4c42059e3f2728dfc452c32e5597b28ce68d6", | ||
"number": 5000, | ||
"parent_hash": "\\x82e95c1ee3a98cd0646225b5ae6afc0b0229367b992df97aeb669c898657a4bb", | ||
"timestamp": "2015-07-30T20:07:44+00:00" | ||
}, | ||
{ | ||
"hash": "\\x82e95c1ee3a98cd0646225b5ae6afc0b0229367b992df97aeb669c898657a4bb", | ||
"number": 4999, | ||
"parent_hash": "\\x875c9a0f8215258c3b17fd5af5127541121cca1f594515aae4fbe5a7fbef8389", | ||
"timestamp": "2015-07-30T20:07:36+00:00" | ||
} | ||
] | ||
} | ||
``` | ||
|
||
## Limitations/Ideas/Disclaimers | ||
|
||
Most of these are fairly easy to address: | ||
|
||
- bind variables/query parameters are not supported, only literal SQL | ||
queries | ||
* queries must finish within `GRAPH_SQL_STATEMENT_TIMEOUT` (unlimited by | ||
default) | ||
* queries are always executed at the subgraph head. It would be easy to add | ||
a way to specify a block at which the query should be executed | ||
* the interface right now pretty much exposes the raw SQL schema for a | ||
subgraph, though system columns like `vid` or `block_range` are made | ||
inaccessible. | ||
* it is not possible to join across subgraphs, though it would be possible | ||
to add that. Implenting that would require some additional plumbing that | ||
hides the effects of sharding. | ||
* JSON as the response format is pretty terrible, and we should change that | ||
to something that isn't so inefficient | ||
* the response contains data that's pretty raw; as the example shows, | ||
binary data uses Postgres' notation for hex strings | ||
* because of how broad the supported SQL is, it is pretty easy to issue | ||
queries that take a very long time. It will therefore not be hard to take | ||
down a `graph-node`, especially when no query timeout is set | ||
|
||
Most importantly: while quite a bit of effort has been put into making this | ||
interface safe, in particular, making sure it's not possible to write | ||
through this interface, there's no guarantee that this works without bugs. |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The latest version (0.59.0) has an additional variant for
SetExpr
for DELETE statements. It's probably worth updating and covering the new variant in the validator.