• Stars
    star
    915
  • Rank 49,917 (Top 1.0 %)
  • Language
    Python
  • Created over 5 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

inferring sql queries from plain-text questions about tables

Infer SQL queries from plain-text questions and table headers.

Requirements:

  • install docker
  • install curl (or, if you're feeling brave, asql)
  • Make sure docker allows at least 3GB of RAM (see Docker>Preferences>Advanced or equivalent) for SQLova, or 5GB for IRNet or ValueNet.

I take pretrained models published along with academic papers, and do whatever it takes to make them testable on fresh data (academic work often omits that, with code tied to a particular benchmark dataset). I spend days tracking down and patching obscure data preprocessing steps so you don't have to.

ValueNet example

So far I've packaged three models:

  • SQLova. Works on single tables.
  • ValueNet. Works on multiple tables, and makes an effort to predict parameters.
  • IRNet. Works on multiple tables, but doesn't predict parameters.

In each case, I've mangled the original network somewhat, so if they interest you do follow up with the original sources.

SQLova

This wraps up a published pretrained model for SQLova (https://github.com/naver/sqlova/).

Fetch and start SQLova running as an api server on port 5050:

docker run --name sqlova -d -p 5050:5050 paulfitz/sqlova

Be patient, the image is about 4.2GB. Once it is running, it'll take a few seconds to load models and then you can start asking questions about CSV tables. For example:

curl -F "[email protected]" -F "q=how long is throgs neck" localhost:5050
# {"answer":[1800],"params":["throgs neck"],"sql":"SELECT (length) FROM bridges WHERE bridge = ?"}

This is using the sample bridges.csv included in this repo.

bridge designer length
Brooklyn J. A. Roebling 1595
Manhattan G. Lindenthal 1470
Williamsburg L. L. Buck 1600
Queensborough Palmer & Hornbostel 1182
Triborough O. H. Ammann 1380,383
Bronx Whitestone O. H. Ammann 2300
Throgs Neck O. H. Ammann 1800
George Washington O. H. Ammann 3500

Here are some examples of the answers and sql inferred for plain-text questions about this table:

question answer sql
how long is throgs neck 1800 SELECT (length) FROM bridges WHERE bridge = ? ['throgs neck']
who designed the george washington O. H. Ammann SELECT (designer) FROM bridges WHERE bridge = ? ['george washington']
how many bridges are there 8 SELECT count(bridge) FROM bridges
how many bridges are designed by O. H. Ammann 4 SELECT count(bridge) FROM bridges WHERE designer = ? ['O. H. Ammann']
which bridge are longer than 2000 Bronx Whitestone, George Washington SELECT (bridge) FROM bridges WHERE length > ? ['2000']
how many bridges are longer than 2000 2 SELECT count(bridge) FROM bridges WHERE length > ? ['2000']
what is the shortest length 1182 SELECT min(length) FROM bridges

With the players.csv sample from WikiSQL:

Player No. Nationality Position Years in Toronto School/Club Team
Antonio Lang 21 United States Guard-Forward 1999-2000 Duke
Voshon Lenard 2 United States Guard 2002-03 Minnesota
Martin Lewis 32, 44 United States Guard-Forward 1996-97 Butler CC (KS)
Brad Lohaus 33 United States Forward-Center 1996 Iowa
Art Long 42 United States Forward-Center 2002-03 Cincinnati
John Long 25 United States Guard 1996-97 Detroit
Kyle Lowry 3 United States Guard 2012-present Villanova
question answer sql
What number did the person playing for Duke wear? 21 SELECT (No.) FROM players WHERE School/Club Team = ? ['duke']
Who is the player that wears number 42? Art Long SELECT (Player) FROM players WHERE No. = ? ['42']
What year did Brad Lohaus play? 1996 SELECT (Years in Toronto) FROM players WHERE Player = ? ['brad lohaus']
What country is Voshon Lenard from? United States SELECT (Nationality) FROM players WHERE Player = ? ['voshon lenard']

Some questions about iris.csv:

question answer sql
what is the average petal width for virginica 2.026 SELECT avg(Petal.Width) FROM iris WHERE Species = ? ['virginica']
what is the longest sepal for versicolor 7.0 SELECT max(Sepal.Length) FROM iris WHERE Species = ? ['versicolor']
how many setosa rows are there 50 SELECT count(col0) FROM iris WHERE Species = ? ['setosa']

There are plenty of types of questions this model cannot answer (and that aren't covered in the dataset it is trained on, or in the sql it is permitted to generate).

ValueNet

This wraps up a published pretrained model for ValueNet (https://github.com/brunnurs/valuenet).

Fetch and start ValueNet running as an api server on port 5050:

docker run --name valuenet -d -p 5050:5050 paulfitz/valuenet

You can then ask questions of individual csv files as before, or several csv files (just repeat -F "[email protected]") or a simple sqlite db with tables related by foreign keys. In this last case, the model can answer using joins.

curl -F "[email protected]" -F "q=who is the CEO of Omni Cooperative" localhost:5050
# {"answer":[["Dracula"]], "sql":"SELECT T1.name FROM people AS T1 JOIN organizations AS T2 \
#   ON T1.id = T2.ceo_id WHERE T2.company = 'Omni Cooperative'"}
curl -F "[email protected]" -F "q=how many designers are there?" localhost:5050
# {"answer":[[5]],"sql":"SELECT DISTINCT count(DISTINCT T1.designer) FROM bridges AS T1"}
curl -F "[email protected]" -F "[email protected]" -F "q=how many designers are there?" localhost:5050
# same answer
curl -F "[email protected]" -F "[email protected]" -F "q=what is the name of the airport with the highest latitude?" localhost:5050
# {"answer":[["Disraeli Inlet Water Aerodrome"]],
#  "sql":"SELECT T1.name FROM airports AS T1 ORDER BY T1.latitude_deg DESC LIMIT 1"}

I've includes material to convert user tables into the form needed to query them. Don't judge the network by its quality here, go do a deep dive with the original - I've deviated from the original in important respects, including how named entity recognition is done.

I've written up some experiments with ValueNet.

IRNet

This wraps up a published pretrained model for IRNet (https://github.com/microsoft/IRNet). Upstream released a better model after I packaged this, so don't judge the model by playing with it here.

Fetch and start IRNet running as an api server on port 5050:

docker run --name irnet -d -p 5050:5050 -v $PWD/cache:/cache paulfitz/irnet

Be super patient! Especially on the first run, when a few large models need to be downloaded and unpacked.

You can then ask questions of individual csv files as before, or several csv files (just repeat -F "[email protected]") or a simple sqlite db with tables related by foreign keys. In this last case, the model can answer using joins.

curl -F "[email protected]" -F "q=what city is The Firm headquartered in?" localhost:5050
# Answer: SELECT T1.city FROM locations AS T1 JOIN organizations AS T2 WHERE T2.company = 1
curl -F "[email protected]" -F "q=who is the CEO of Omni Cooperative" localhost:5050
# Answer: SELECT T1.name FROM people AS T1 JOIN organizations AS T2 WHERE T2.company = 1
curl -F "[email protected]" -F "q=what company has Dracula as CEO" localhost:5050
# Answer: SELECT T1.company FROM organizations AS T1 JOIN people AS T2 WHERE T2.name = 1

(Note there's no value prediction, so e.g. the where clauses are = 1 rather than something more useful).

Postman users

Curl can be replaced by Postman for those who like that. Here's a working set-up: Postman version

Other models

I hope to track research in the area and substitute in models as they become available:

Live demoes

  • Photon by the SalesForce group is a good live demo of text-to-SQL.

More Repositories

1

daff

align and compare tables
Java
800
star
2

cosmicos

Sending the lambda calculus into deep space
Java
135
star
3

coopy

distributed spreadsheets with intelligent merges
C
95
star
4

catsql

cat for sql dbs
Python
73
star
5

visql

edit slices of SQL databases in vi
Python
48
star
6

sheetsite

sync a website or local spreadsheet with a google sheet
Python
34
star
7

makesweet

Put pictures into animations from the command line.
C++
21
star
8

deepmoon

the deep learning framework from beyond the moon
Python
11
star
9

emcycles

Compiling cycles renderer in javascript
C++
11
star
10

ucanvcam

Automatically exported from code.google.com/p/ucanvcam
C++
8
star
11

emacsql

edit a slice of a SQL database in emacs
Python
8
star
12

daff-php

php version of daff
PHP
7
star
13

html2video

JavaScript
6
star
14

segmenty

training convnets to segment visual patterns without annotated data
Python
5
star
15

blender2haxe

a fork of rozengain.com's great AS3Export tool, to support haxe.
Python
5
star
16

exoplanets

calculate exoplanet birthdays
JavaScript
4
star
17

zify

add depth to an icon using blender cycles
Shell
3
star
18

asql

Query a database in natural language
Python
3
star
19

gnumeric

notes on building gnumeric on windows
C
2
star
20

pixplz

need some training images in a hurry? pixplz!
Python
2
star
21

daff-doc

Documentation for daff
CSS
2
star
22

cycles_hack

Blender cycles renderer hacked to remove ... absolutely everything. You don't want this.
C++
2
star
23

coopyhx_gem

wraps up coopyhx for ruby
C++
2
star
24

chumby

Shell
1
star
25

url2020

Enter a URL in mobile chrome in 2020
HTML
1
star
26

glig

All About Gligs
HTML
1
star
27

data_commons

CDI DCP tools
1
star
28

dearet

Scheme
1
star
29

winarm_cmake

C
1
star
30

filabel

file labels for quick image classification projects
Python
1
star
31

grist_quiz

JavaScript
1
star
32

keras_cli

keras cli
Python
1
star
33

icub_skin

C++
1
star
34

daff-web

compare tables, producing a human and machine readable diff that is itself tabular
HTML
1
star
35

robobo

Random robotics
C++
1
star
36

grist-plugin-api-docs

HTML
1
star
37

spelling_bee_prep

spell time
JavaScript
1
star
38

poker

1
star
39

stonesouper

A backend for stonesoup-style directories
TypeScript
1
star
40

yarp_thrift

Use Thrift with YARP
C++
1
star
41

dylan

Extends the lyrics of Bob Dylan's A Hard Rain's A-Gonna Fall
Python
1
star
42

rb_coopy

native ruby implementation of coopy data diff tools
Ruby
1
star
43

grist-widget

JavaScript
1
star