May 18, 2024
Pinterest Engineering
Pinterest Engineering Blog

Adam Obeng | Knowledge Scientist, Knowledge Platform Science; J.C. Zhong | Tech Lead, Analytics Platform; Charlie Gu | Sr. Supervisor, Engineering

Writing queries to resolve analytical issues is the core job for Pinterest’s information customers. Nevertheless, discovering the suitable information and translating an analytical downside into appropriate and environment friendly SQL code might be difficult duties in a fast-paced setting with vital quantities of information unfold throughout totally different domains.

We took the rise in availability of Giant Language Fashions (LLMs) as a chance to discover whether or not we might help our information customers with this job by growing a Textual content-to-SQL function which transforms these analytical questions straight into code.

Most information evaluation at Pinterest occurs via Querybook, our in–home open supply massive information SQL question instrument. This instrument is the pure place for us to develop and deploy new options to help our information customers, together with Textual content-to-SQL.

The Preliminary Model: A Textual content-to-SQL Resolution Utilizing an LLM

The primary model included a simple Textual content-to-SQL answer using an LLM. Let’s take a better have a look at its structure:

The person asks an analytical query, selecting the tables for use.

  1. The related desk schemas are retrieved from the desk metadata retailer.
  2. The query, chosen SQL dialect, and desk schemas are compiled right into a Textual content-to-SQL immediate.
  3. The immediate is fed into the LLM.
  4. A streaming response is generated and exhibited to the person.

Desk Schema

The desk schema acquired from the metadata retailer contains:

  • Desk identify
  • Desk description
  • Columns
  • Column identify
  • Column kind
  • Column description

Low-Cardinality Columns

Sure analytical queries, similar to “what number of lively customers are on the ‘net’ platform”, could generate SQL queries that don’t conform to the database’s precise values if generated naively. For instance, the the place clause within the response would possibly bewhere platform=’net’ versus the proper the place platform=’WEB’. To handle such points, distinctive values of low-cardinality columns which might continuously be used for this sort of filtering are processed and included into the desk schema, in order that the LLM could make use of this info to generate exact SQL queries.

Context Window Restrict

Extraordinarily giant desk schemas would possibly exceed the standard context window restrict. To handle this downside, we employed just a few strategies:

  • Diminished model of the desk schema: This contains solely essential parts such because the desk identify, column identify, and sort.
  • Column pruning: Columns are tagged within the metadata retailer, and we exclude sure ones from the desk schema based mostly on their tags.

Response Streaming

A full response from an LLM can take tens of seconds, so to keep away from customers having to attend, we employed WebSocket to stream the response. Given the requirement to return diversified info apart from the generated SQL, a correctly structured response format is essential. Though plain textual content is easy to stream, streaming JSON might be extra advanced. We adopted Langchain’s partial JSON parsing for the streaming on our server, after which the parsed JSON will probably be despatched again to the shopper via WebSocket.


Right here is the present prompt we’re utilizing for Text2SQL:

Analysis & Learnings

Our preliminary evaluations of Textual content-to-SQL efficiency have been principally performed to make sure that our implementation had comparable efficiency with outcomes reported within the literature, on condition that the implementation principally used off-the-shelf approaches. We discovered comparable outcomes to these reported elsewhere on the Spider dataset, though we famous that the duties on this benchmark have been considerably simpler than the issues our customers face, particularly that it considers a small variety of pre-specified tables with few and well-labeled columns.

As soon as our Textual content-to-SQL answer was in manufacturing, we have been additionally in a position to observe how customers interacted with the system. As our implementation improved and as customers turned extra acquainted with the function, our first-shot acceptance price for the generated SQL elevated from 20% to above 40%. In apply, most queries which might be generated require a number of iterations of human or AI era earlier than being finalized. To be able to decide how Textual content-to-SQL affected information person productiveness, essentially the most dependable methodology would have been to experiment. Utilizing such a way, earlier analysis has found that AI help improved job completion pace by over 50%. In our actual world information (which importantly doesn’t management for variations in duties), we discover a 35% enchancment in job completion pace for writing SQL queries utilizing AI help.

Whereas the primary model carried out decently — assuming the person is conscious of the tables to be employed — figuring out the proper tables amongst the a whole bunch of hundreds in our information warehouse is definitely a big problem for customers. To mitigate this, we built-in Retrieval Augmented Technology (RAG) to information customers in choosing the suitable tables for his or her duties. Right here’s a assessment of the refined infrastructure incorporating RAG:

  1. An offline job is employed to generate a vector index of tables’ summaries and historic queries towards them.
  2. If the person doesn’t specify any tables, their query is remodeled into embeddings, and a similarity search is performed towards the vector index to deduce the highest N appropriate tables.
  3. The highest N tables, together with the desk schema and analytical query, are compiled right into a immediate for LLM to pick the highest Ok most related tables.
  4. The highest Ok tables are returned to the person for validation or alteration.
  5. The usual Textual content-to-SQL course of is resumed with the user-confirmed tables.

Offline Vector Index Creation

There are two varieties of doc embeddings within the vector index:

  • Desk summarization
  • Question summarization

Desk Summarization

There’s an ongoing desk standardization effort at Pinterest so as to add tiering for the tables. We index solely top-tier tables, selling the usage of these higher-quality datasets. The desk summarization era course of entails the next steps:

  1. Retrieve the desk schema from the desk metadata retailer.
  2. Collect the latest pattern queries using the desk.
  3. Primarily based on the context window, incorporate as many pattern queries as attainable into the desk summarization immediate, together with the desk schema.
  4. Ahead the immediate to the LLM to create the abstract.
  5. Generate and retailer embeddings within the vector retailer.

The desk abstract contains description of the desk, the info it accommodates, in addition to potential use eventualities. Right here is the present prompt we’re utilizing for desk summarization:

Question Summarization

Apart from their position in desk summarization, pattern queries related to every desk are additionally summarized individually, together with particulars such because the question’s objective and utilized tables. Right here is the prompt we’re utilizing:

NLP Desk Search

When a person asks an analytical query, we convert it into embeddings utilizing the identical embedding mannequin. Then we conduct a search towards each desk and question vector indices. We’re utilizing OpenSearch because the vector retailer and utilizing its in-built similarity search capability.

Contemplating that a number of tables might be related to a question, a single desk might seem a number of instances within the similarity search outcomes. At the moment, we make the most of a simplified technique to combination and rating them. Desk summaries carry extra weight than question summaries, a scoring technique that may very well be adjusted sooner or later.

Aside from getting used within the Textual content-to-SQL, this NLP-based desk search can also be used within the normal desk search in Querybook.

Desk Re-selection

Upon retrieving the highest N tables from the vector index, we interact an LLM to decide on essentially the most related Ok tables by evaluating the query alongside the desk summaries. Relying on the context window, we embrace as many tables as attainable within the immediate. Right here is the prompt we’re utilizing for the desk re-selection:

As soon as the tables are re-selected, they’re returned to the person for validation earlier than transitioning to the precise SQL era stage.

Analysis & Learnings

We evaluated the desk retrieval element of our Textual content-to-SQL function utilizing offline information from earlier desk searches. This information was inadequate in a single vital respect: it captured person conduct earlier than they knew that NLP-based search was out there. Subsequently, this information was used principally to make sure that the embedding-based desk search didn’t carry out worse than the prevailing text-based search, somewhat than trying to measure enchancment. We used this analysis to pick a way and set weights for the embeddings utilized in desk retrieval. This method revealed to us that the desk metadata generated via our information governance efforts was of great significance to general efficiency: the search hit price with out desk documentation within the embeddings was 40%, however efficiency elevated linearly with the load positioned on desk documentation as much as 90%.

Whereas our currently-implemented Textual content-to-SQL has considerably enhanced our information analysts’ productiveness, there may be room for enhancements. Listed here are some potential areas of additional improvement:

NLP Desk Search

At the moment, our vector index solely associates with the desk abstract. One potential enchancment may very well be the inclusion of additional metadata similar to tiering, tags, domains, and so on., for extra refined filtering through the retrieval of comparable tables.

  • Scheduled or Actual-Time Index Replace

At the moment the vector index is generated manually. Implementing scheduled and even real-time updates every time new tables are created or queries executed would improve system effectivity.

  • Similarity Search and Scoring Technique Revision

Our present scoring technique to combination the similarity search outcomes is somewhat fundamental. Tremendous-tuning this facet might enhance the relevance of retrieved outcomes.

Question validation

At current, the SQL question generated by the LLM is straight returned to the person with out validation, leaving a possible danger that the question could not run as anticipated. Implementing question validation, maybe utilizing a constrained beam search, might present an additional layer of assurance.

Consumer suggestions

Introducing a person interface to effectively accumulate person suggestions on the desk search and question era outcomes might provide helpful insights for enhancements. Such suggestions may very well be processed and included into the vector index or desk metadata retailer, in the end boosting system efficiency.


Whereas engaged on this challenge, we realized that the efficiency of text-to-SQL in an actual world setting is considerably totally different to that in present benchmarks, which have a tendency to make use of a small variety of well-normalized tables (that are additionally prespecified). It could be useful for utilized researchers to supply extra life like benchmarks which embrace a bigger quantity of denormalized tables and deal with desk search as a core a part of the issue.

To study extra about engineering at Pinterest, take a look at the remainder of our Engineering Weblog and go to our Pinterest Labs website. To discover and apply to open roles, go to our Careers web page.