Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

SQL Calcite #3630

Merged
merged 25 commits into from
Jun 2, 2023
Merged

SQL Calcite #3630

merged 25 commits into from
Jun 2, 2023

Conversation

devinrsmith
Copy link
Member

@devinrsmith devinrsmith commented Mar 30, 2023

This is a large, self-contained PR that adds server-side SQL parsing and execution support.

The SQL implementation logic is largely self-contained in the sql package, and the public interface it exposes is very small. Its job is to translate SQL strings into TableSpec, but not to execute it. This keeps dependencies at a minimum, and allows for the usage of sql by java clients if they so desire.

The server-side SQL execution is the responsibility of the engine-sql package. The package is also wrapped in the deephaven.experimental.sql python module:

from deephaven import time_table
from deephaven.experimental.sql import execute_sql

my_table = time_table("00:00:01")

sql = """
SELECT 
  count(*) as my_count 
FROM 
  my_table
"""

out = execute_sql(sql)

This should be considered an experimental / minimial-viable-SQL offering - there is a lot of opportunity for improvements and optimizations in the future. Some of these have been captured with "SQLTODO"s with descriptions inline, as well as listed in sql/DEVELOPMENT.md.

@devinrsmith devinrsmith self-assigned this Mar 30, 2023
Copy link
Member

@niloc132 niloc132 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

There are many TODOs that need to either be fixed, made into SQLTODOs, or reference bugs. Also, System.out.printlns.

Many classes could use javadoc, if only to briefly describe what they are doing to deal with their calcite counterparts - I think the VC summary you gave me would probably do the job for most of these.

I'm not sure that either project specifically belongs where it is - would these make more sense in extensions or java-client?

sql/build.gradle Outdated
useJUnitPlatform()
}

// todo: should this be a java 8 project?
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

if so, is this intended to be part of java-client, rather than a new top-level project?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

No; it's not meant to be part of the java-client. Ultimately, we are targetting server side SQL. That said, the conversion from String -> TableSpec is completely devoid of server side code, so it is a natural fit if the java client wants to make use out of it.

I'm going to make a better note.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This note was more about "Why would this be java 8, if it isnt meant to be designed for clients that can't update"

@Override
public Table of(TicketTable ticketTable) {
final String scanTicket = new String(ticketTable.ticket(), StandardCharsets.UTF_8);
if (!scanTicket.startsWith("scan/")) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

here an TableScanAdapter should find a way to have this be a constant - for my money, i'm not sure this class belongs in engine itself.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've updated the interface to give clear ownership to the calling side; the SqlAdapter layer won't be creating any TicketTables that the caller needs to know the format of.

engine/sql/src/main/java/io/deephaven/engine/sql/Sql.java Outdated Show resolved Hide resolved
sql/src/main/java/io/deephaven/sql/SqlFunctions.java Outdated Show resolved Hide resolved
@devinrsmith devinrsmith marked this pull request as ready for review May 19, 2023 19:23
@devinrsmith devinrsmith added this to the May 2023 milestone May 19, 2023
@devinrsmith devinrsmith changed the base branch from devin-tmp/filter-condition-expression to main May 19, 2023 19:24
engine/sql/src/main/java/io/deephaven/engine/sql/Sql.java Outdated Show resolved Hide resolved
server/jetty-app/src/main/resources/logback-minimal.xml Outdated Show resolved Hide resolved
server/jetty-app/src/main/resources/logback.xml Outdated Show resolved Hide resolved
sql/IMPLEMENTATION.md Outdated Show resolved Hide resolved
import java.nio.file.Path;
import java.util.List;

import static org.assertj.core.api.Assertions.assertThat;
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Moving from numbers to descriptive names would make this better and more maintainable.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

or maybe named constants in the test class

Copy link
Member

@rcaudy rcaudy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Finished re-review of files I looked at on Tuesday

@rcaudy
Copy link
Member

rcaudy commented May 25, 2023

I think it's very important that we write a comprehensive, user-facing document explaining what we support, what we want to, what we never will, etc.

@devinrsmith devinrsmith requested a review from rcaudy May 26, 2023 00:07

@Override
public void visit(FloatType floatType) {
out = create(SqlTypeName.FLOAT);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this correct?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Should be REAL.

@devinrsmith devinrsmith requested a review from rcaudy May 30, 2023 16:31
result_table = sql.eval("SELECT CURRENT_TIMESTAMP")
self.assertEqual(result_table.size, 1)


Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We tested a few other scoping scenarios for the table methods. Consider testing some of those here as well. See for example the stuff around this line in the test file.
https://github.com/deephaven/deephaven-core/blob/main/py/server/tests/test_table.py#L730

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The scoping for SQL is a bit more limited, as it's limited to only referencing table names. Custom functions are on the roadmap, and we'd be able to get more tests of this kind then.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The test cases I'm referring to as examples make sure that query scope is properly obtained from the correct Python scope.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've added a new test, but it's not really testing anything new - is there another specific SQL test you want to see?

py/server/deephaven/experimental/sql.py Outdated Show resolved Hide resolved
py/server/deephaven/experimental/sql.py Outdated Show resolved Hide resolved
j_py_script_session.popScope()


def eval(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

eval is a built-in python function, so we are better off renaming this to sql_eval or something else.

Copy link
Member Author

@devinrsmith devinrsmith May 30, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The naming was intentional, as I think the built-in eval is very similar in spirit to evaluating SQL (thus, also why I exposed globals/locals as params). I'm hoping we always reference it qualified by the sql module:

from deephaven.experimental import sql

sql.eval(...)

Or, I could get behind the more explicit name evaluate(...) if we prefer it to be "unique".

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The user can always do that themselves:
from deephaven.experimental.sql import eval as sql_eval

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overwriting a built-in function is generally considered to be bad practice.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've changed the function name to "evaluate"

py/server/deephaven/experimental/sql.py Outdated Show resolved Hide resolved
@devinrsmith devinrsmith requested a review from chipkent May 30, 2023 23:19
rcaudy
rcaudy previously approved these changes May 31, 2023
Copy link
Member

@rcaudy rcaudy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm content to merge this, once the Python crowd is on board.

jmao-denver
jmao-denver previously approved these changes Jun 1, 2023
Copy link
Contributor

@jmao-denver jmao-denver left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@devinrsmith devinrsmith dismissed stale reviews from jmao-denver and rcaudy via 8800b35 June 1, 2023 22:31
@devinrsmith devinrsmith merged commit 38b2e1c into deephaven:main Jun 2, 2023
@devinrsmith devinrsmith deleted the calcite branch June 2, 2023 16:16
@github-actions github-actions bot locked and limited conversation to collaborators Jun 2, 2023
@deephaven-internal
Copy link
Contributor

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants