jupyter-xeus / xeus-sql

Jupyter kernel for SQL databases
https://xeus-sql.readthedocs.io/en/latest/
BSD 3-Clause "New" or "Revised" License
163 stars 22 forks source link

About vega plot command #17

Closed wangfenjin closed 3 years ago

wangfenjin commented 3 years ago

Hi team,

I like this project, especially the idea integrate vega into the kernel.

The command is %XVEGA_PLOT X_FIELD EmployeeId GRID false Y_FIELD ReportsTo MARK area COLOR pink WIDTH 200 HEIGHT 200 <> SELECT EmployeeId, ReportsTo FROM employees like this I think it's two complated, we need to write a lot of code to parse the user input and user need to learn new syntax.

So I suggest we follow the style I implement in here:

%VEGA-LITE specs/arc_pie.vl.json 
SELECT 
    g.Name as category, 
    SUM(il.Quantity) as b, 
    ROUND((CAST(SUM(il.Quantity) as float) / (SELECT 
        SUM(Quantity) total
    FROM Chinook.InvoiceLine il
    INNER JOIN Chinook.Invoice i ON i.InvoiceId = il.InvoiceId
    WHERE i.BillingCountry = 'USA')) * 100, 2) value 
FROM Chinook.InvoiceLine il
INNER JOIN Chinook.Invoice i ON i.InvoiceId = il.InvoiceId 
INNER JOIN Chinook.Track t ON il.TrackId = t.TrackId 
INNER JOIN Chinook.Genre g ON t.GenreId = g.GenreId 
WHERE i.BillingCountry = 'USA' 
GROUP BY 1 
ORDER BY 2 DESC
LIMIT 10

This is a project I fork from this repo to verify my ideas and used to participants a hackathon, but I would like to contribute back and archive my repo.

And if we want to support define specs on the fly, and can add some command like:

%VEGA-LITE SET my_spec.vl.json
{
  "data": {"url": "data/seattle-weather.csv"},
  "mark": "bar",
  "encoding": {
    "x": {"timeUnit": "month", "field": "date", "type": "ordinal"},
    "y": {"aggregate": "mean", "field": "precipitation"}
  }
}

%VEGA-LITE PLOT my_spec.vl.json
select ...

What do you think?

marimeireles commented 3 years ago

Hi @wangfenjin, first of all, amazing work! Also, super cool examples. I absolutely love them!

I never thought about this idea, tbh, because I think we have this strong culture of using jupyter magics never occurred to me we could use the raw json. What do you think of this idea @SylvainCorlay ?

Thank you for opening an issue to discuss it :)

marimeireles commented 3 years ago

That actually allows the user to use all xvega implementation. I think we should at least support both.

marimeireles commented 3 years ago

@wangfenjin feel free to open a PR adding the possibility to load by json, I also saw you took care of some edge cases in your code that I'm not taking into account, it'd be super cool if you could contribute these too. :)

I'm not sure if I still have a lot of time to work in this project, but I'll gladly review a PR!

wangfenjin commented 3 years ago

Yes, I'll create a separate PR for the corner case, but it may take time as I think we may need to support conditional compile and make it work with CMake. For example, if we want to catch error from mysql, then we need to include the mysql header, and we can't just catch all exception as explained here https://stackoverflow.com/a/50133665/1203241

But we can only include mysql header file if user compile with mysql support. I'm not so familiar with all of this.

Anyway, I'll try to contribute. Thanks.

SylvainCorlay commented 3 years ago

@wangfenjin I think this is a brilliant idea. Awesome!

I wonder how we could define the vega specs inline with another magics.

marimeireles commented 3 years ago

For example, if we want to catch error from mysql, then we need to include the mysql header

Hum, I see. Yeah, we can worry about this later. I've considered it before, let me know if you want some input on this, we can discuss on a separate issue. What it'd be super cool to have is the json input anyways :)

I wonder how we could define the vega specs inline with another magics.

I think we could use what we're using for xvega-bindings?