BT

Facilitating the spread of knowledge and innovation in professional software development

Contribute

Topics

Choose your language

InfoQ Homepage News Google Logica Aims to Make SQL Queries More Reusable and Readable

Google Logica Aims to Make SQL Queries More Reusable and Readable

This item in japanese

Bookmarks

Logica is a Datalog-like declarative logic programming language for database querying. It supports the creation of reusable abstractions to build complex queries and compiles to SQL, thus making it suitable for wide application, in addition to Google's own BigQuery.

Writing SQL queries, explain Google engineers and Logica creators Konstantin Tretyakov and Evgeny Skvortsov, can be nightmarish:

Despite the widespread adoption, SQL is not flawless. Constructing statements from long chains of English words (which are often capitalized to keep the old-fashioned COBOL spirit of the 70s alive!) can be very verbose—a single query spanning hundreds of lines is a routine occurrence.

This state of things is the direct consequence of the lack of advanced abstraction mechanisms built into SQL, besides the very basic notions of views and functions. In particular, say Tretyakov and Skvortsov, SQL does not offer any facilities to define or import packages.

Logica attempts to provide an alternative to writing SQL using the principles of logic programming. In particular, it extends propositional logic with additional constructs, for example aggregation, which, as its name implies, makes it possible to aggregate the results of several queries together, similar to what you do with the SQL GROUP BY statement.

The basic notion in Logica is that of a logic predicate, which replaces SQL relations. In fact, Logica predicates describe the logical condition that rows of a relations must satisfy. Predicates can use structures, variables, negation, conjunction (join), disjunction (union), and, as mentioned, aggregation. Most importantly, Logica predicates can use functions, which do not add any query capabilities but make syntax cleaner and predicate composition easier.

Coming from Google, Logica is well integrated with its BigQuery database. Here is a short example of Logica syntax showing how you can use BigQuery to select popular baby names:

# Connecting predicate to a table.
BabyNames(..r) :- `bigquery-public-data.usa_names.usa_1910_current`(..r);

# Extracting an arbitrary sample from the table.
@Limit(BabyNamesSample, 11);
BabyNamesSample(..r) :- BabyNames(..r);  

NameCountByYear(name: , year:) += number :-
  BabyNames(name:, year:, number:);

# Finding most popular name for each year.
@OrderBy(TopNameByYear, "year");
TopNameByYear(year) ArgMax= name -> NameCountByYear(name:, year:);

Logica's syntax is strongly informed by Datalog's, hence Prolog's, which can make it appear rather abstruse. According to Tretyakov and Skvortsov, though, it can be a good option if you use complex SQL queries that end up being scarcely readable and would like to express them in a more manageable way. It must be noticed, though, that SQL is not just a query definition language, since it also includes data manipulation features to create new data properties, such as adding columns to a table, creating new tables, and so on.

As mentioned, currently Logica can be translated to BigQuery SQL, but support for PostgreSQL and SQLite is on the roadmap and already available experimentally.

We need your feedback

How might we improve InfoQ for you

Thank you for being an InfoQ reader.

Each year, we seek feedback from our readers to help us improve InfoQ. Would you mind spending 2 minutes to share your feedback in our short survey? Your feedback will directly help us continually evolve how we support you.

Take the Survey

Rate this Article

Adoption
Style

Hello stranger!

You need to Register an InfoQ account or or login to post comments. But there's so much more behind being registered.

Get the most out of the InfoQ experience.

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Community comments

  • abstruse

    by John Walker,

    Your message is awaiting moderation. Thank you for participating in the discussion.

    Ironically I needed to look up the definition of "abstruse"

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

Allowed html: a,b,br,blockquote,i,li,pre,u,ul,p

BT

Is your profile up-to-date? Please take a moment to review and update.

Note: If updating/changing your email, a validation request will be sent

Company name:
Company role:
Company size:
Country/Zone:
State/Province/Region:
You will be sent an email to validate the new email address. This pop-up will close itself in a few moments.