from Hacker News

Show HN: Describe SQL using natural language, and execute against real data

by napoleond on 12/16/21, 5:57 PM with 36 comments

I played around with GPT-3 to build this demo. Select a public BigQuery dataset and describe your query in natural English, then edit the generated SQL as needed and execute it.

https://app.tabbydata.com/sql-assistant-demo

  • by bob1029 on 12/16/21, 11:02 PM

    This demo sent us on a warpath today. We have a fairly clean SQL schema for which we need to craft a lot of queries that handle things like business logic, reporting and configuration.

    If we could get even 50% success rate on a reasonable starting point for the generated SQL each time, that would be the biggest value-add our organization has ever seen.

    I think our use case is compelling because we have to implement the same SQL targets for every customer. The only variations are typically customer-specific parameters/codes/etc.

    We also have a huge corpus of examples to pull from for training data.

    We are thinking about initially implementing some higher order views/functions in our SQL dialect to make things easier on ourselves with the GPT model. Complex joins across many tables seems to be something that would still elude these techniques. Most of our joins are of a very particular shape, so we can abstract the super nasty stuff away.

    Worst case scenario, this concludes like my cynical mind assumes it will, but I am open to being surprised this time. We aren't going to put everything behind this, more of a "if it works..." kind of 1-2 week experiment.

  • by tillvz on 12/16/21, 7:17 PM

    Hey that's very cool and works surprisingly well!

    At Veezoo (http://www.veezoo.com) we have been tackling this problem for over 5 years now.

    Under the hood we are using our own models. With GPT-3 we're a bit worried about the lack of fine-grained control needed for productive use-cases and obv. also lock in.

    Will try out against the same dataset and see how it compares!

  • by cafed00d on 12/16/21, 10:23 PM

    Wow, this is absolutely brilliant!

    How can I extend this to use other datasets? There seem to be quite a few interesting publicly available datasets out there: https://console.cloud.google.com/marketplace/browse?filter=s...

    and I'm wondering if: 1. Is there a paid-for version of your app/website where I can plug in a diff dataset? 2. Have you considered sharing the source code for others to recreate and plug in diff datasets? 3. Or, :sweat_smile:, perhaps this is as simple as adding new datasets to the drop-down menu? say, finance data / Bitcoin transactions data?

    Regardless, really cool app!

  • by mritchie712 on 12/16/21, 6:23 PM

    We did a similar analysis a while back, works surprisingly well! I OpenAI increases the amount of "training data" you can send in, I think it could get really good at generating SQL.

    https://blog.seekwell.io/gpt3

  • by earleybird on 12/16/21, 8:36 PM

    May be some rough edges - or I have unreasonable expecations. The weather data set has 'begin' and 'end' columns which generates bad sql.

    ie, "ERROR: Syntax error: Expected end of input but got keyword END at [49:3]"

  • by napoleond on 12/16/21, 5:57 PM

  • by perl4ever on 12/17/21, 1:42 AM

       /*
       Describe the data in the all_geoclustered table.
       */
    
       SELECT
         COUNT(*) AS total_rows,
         COUNT(*) FILTER (WHERE date IS NOT NULL) AS total_rows_with_date,
         COUNT(*) FILTER (WHERE wban IS NOT NULL) AS total_rows_with_wban,
         COUNT(*) FILTER (WHERE stn IS NOT NULL) AS total_rows_with_stn,
         COUNT(*) FILTER (WHERE year IS NOT NULL) AS total_rows_with_year,
         COUNT(*) FILTER (WHERE mo IS NOT NULL) AS total_rows_with_mo,
         COUNT(*) FILTER (WHERE da IS NOT NULL) AS total_rows_with_da,
         COUNT(*) FILTER (WHERE temp IS NOT NULL) AS total_rows_with_temp,
         COUNT(*) FILTER (WHERE count_temp IS NOT NULL) AS total_rows_with_count_temp,
         COUNT(*) FILTER (WHERE dewp IS NOT NULL) AS total_rows_with_dewp,
         COUNT(*) FILTER (WHERE count_dewp IS NOT NULL) AS total_rows_with_count_dewp,
         COUNT(*) FILTER (WHERE slp IS NOT NULL) AS total_rows_with_slp,
         COUNT(*) FILTER (WHERE count_slp IS NOT NULL) AS total_rows_with_count_slp,
         COUNT(*) FILTER (WHERE stp IS NOT NULL) AS total_rows_with_stp,
         COUNT(*) FILTER (WHERE count_stp IS NOT NULL) AS total_rows_with_count_stp,
         COUNT(*) FILTER (WHERE visib IS NOT NULL) AS total_rows_with_visib,
         COUNT(*) FILTER (WHERE count_visib IS NOT NULL) AS total_rows_with_count_visib,
         COUNT(*) FILTER (WHERE wdsp IS NOT NULL) AS total_rows_with_wdsp,
         COUNT(*) FILTER (WHERE count_wdsp IS NOT NULL) AS total_rows_with_count_wdsp,
         COUNT(*) FILTER (WHERE mxpsd IS NOT NULL
         
         
       /*
       How often do hail and thunder happen together?
       */
    
       SELECT
         COUNT(*) AS count,
         SUM(IF(hail=1 AND thunder=1, 1, 0)) AS sum_hail_thunder
       FROM
         `fh-bigquery.weather_gsod.all`
       WHERE
         year = 2019
         AND mo = 11
         AND da = 14
         AND stn = '725030'
  • by lswainemoore on 12/17/21, 3:59 AM

    /* When it rains does it pour? */

    SELECT date, wban, stn, year, mo, da, temp, count_temp, dewp, count_dewp, slp, count_slp, stp, count_stp, visib, count_visib, wdsp, count_wdsp, mxpsd, gust, max, flag_max, min, flag_min, prcp, flag_prcp, sndp, fog, rain_drizzle, snow_ice_pellets, hail, thunder, tornado_funnel_cloud, usaf, name, country, state, call, lat, lon, elev, begin, end, point_gis, fake_date FROM `fh-bigquery.weather_gsod.all_geoclustered` WHERE lat IS NOT NULL AND lon IS NOT NULL AND lat != 0 AND lon != 0 AND lat != 1 AND lon != 1 AND lat != -1 AND lon != -1 AND lat != 2 AND lon != 2 AND lat != -2 AND lon != -2 AND lat != 3 AND lon != 3 AND lat != -3 AND lon != -3 AND lat != 4 AND lon != 4 AND lat != -4 AND lon != -4 AND lat != 5 AND lon != 5 AND lat != -5 AND lon != -5 AND lat != 6 AND lon != 6 AND lat != -6 AND lon != -6 AND lat != 7 AND lon != 7 AND lat != -7 AND lon != -7 AND lat != 8 AND lon != 8 AND lat != -8 AND lon

  • by perl4ever on 12/17/21, 1:45 AM

       /*
       What time of day does hail usually happen?
       */
    
       SELECT
         date,
         wban,
         stn,
         year,
         mo,
         da,
         temp,
         count_temp,
         dewp,
         count_dewp,
         slp,
         count_slp,
         stp,
         count_stp,
         visib,
         count_visib,
         wdsp,
         count_wdsp,
         mxpsd,
         gust,
         max,
         flag_max,
         min,
         flag_min,
         prcp,
         flag_prcp,
         sndp,
         fog,
         rain_drizzle,
         snow_ice_pellets,
         hail,
         thunder,
         tornado_funnel_cloud,
         usaf,
         name,
         country,
         state,
         call,
         lat,
         lon,
         elev,
         begin,
         end,
         point_gis,
         fake_date
       FROM
         `fh-bigquery.weather_gsod.all_geoclustered`
       WHERE
         hail = 1
       ORDER BY
         date DESC
       LIMIT
         10
  • by easylearnai on 12/17/21, 10:45 AM

    Nice idea, but the default natural language query generated a query that didn't run. Perhaps turn down the temperature or tune other parameters. This could be better if you fine-tune it such that anyone could bring their own dataset, with GPT3 just making it available recently. My favorite recent app for no code sql query is trevor.io that has allowed me to build a dashboard so easily. I only knew basic sql query and didn't have to tamper with the actual SQL code; it was all interactive and the results is always in the correct format. Comparing to the GPT3 approach, I would say I prefer trevor over using natural language for the precision. The appeal of using GPT3 exists only if it gets high enough accuracy, imo.
  • by i_like_apis on 12/16/21, 7:10 PM

    This is pretty cool.

    I thought it was funny in that in the weather dataset that "NULL" comes in first for the win for some questions:

    > what is the all time rainiest city? > what are the top 5 most dry states?

    The query conversion is impressive!

  • by mjirv on 12/16/21, 6:38 PM

    Very nice! I’ve been working on something similar recently: https://acolytehq.com.

    Would be happy to chat and compare notes if you'd like!

  • by paulfitz on 12/16/21, 9:38 PM

    For google-trends it translated "Is there anything cat-related that people are excited about?" to something very reasonable that worked (answer: popcat apparently). Nice!
  • by talos2110 on 12/16/21, 10:36 PM

    Nice!

    As an interesting test case, check out the very strange and seemingly recursive query generated for “Get the top 10 authors of caching libraries, ranked by commit volume”

  • by 331c8c71 on 12/16/21, 10:54 PM

    Works unexpectedly well! As many others I am wondering how difficult/costly it'd be to have this assistant set up for another dataset.
  • by perl4ever on 12/17/21, 1:49 AM

       /*
       What is the range of elevations where tornadoes happen?
       */
    
       SELECT
         elev,
         COUNT(*) AS count
       FROM
         `fh-bigquery.weather_gsod.all`
       WHERE
         tornado_funnel_cloud = 'T'
       GROUP BY
         elev
       ORDER BY
         count DESC
  • by gertjandewilde on 12/26/21, 10:03 AM

    Great use case. Is the demo broken?
  • by matthewmorgan on 12/17/21, 1:21 AM

    Why do programmers seem to love the preposition 'against'? 'On' would have been fine