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

[WIP] sql: refactor the statement pretty-printer and implement EXPLAIN(TYPES) #6406

Closed
wants to merge 10 commits into from

Conversation

knz
Copy link
Contributor

@knz knz commented Apr 28, 2016

This commit relies on #6358 being merged first. All the commits prior to the last (where the work of this PR is being done) can be ignored, as they really belong to the other PR.


This change is Reviewable

This is one of the cornerstones of the Summer typing algorithm.
Type checking now takes a "desired" type, which is a hint to `Expr`s to
resolve themselves as the type, if possible. It it up to the caller to
assert that the type returned if correct, if it is actually required.
This commit adds a `TypedExpr` interface which is a superset of Expr but
can also perform expression evaluation and can return type annotations.
This change creates the types `NumVal` and `StrVal`, which both
implement the new `Constant` interface. The interface is a super-set of
`Expr`, but includes extra methods for literal values.
This commit creates a new error type to hold commonly seen errors. It
then uses this error in a few previously ambiguous situations.
Previously `(1) + 2` would not have folded to `3`, now it does.
This change pulls the constant related visitors into constant.go and
cleans up a few tests.
This change creates the types `unaryOpOverload`, `binOpOverload`, and
`cmpOpOverload`, each with a lookup method for an overload
implementation for a given set of argument types. It also changes
`ComparisonOp` to `ComparisonOperator` to stay consistent.
This commit cleans up a number of uses of the new `TypedExpr` interface,
improves the type checking system, added new test cases, and adds in a few
TODOs for future work.
@RaduBerinde
Copy link
Member

Hi Raphael,

This is awesome, thanks for working on this! I have a couple of suggestions, but nothing that needs to be handled in this PR necessarily.

BTW I found a decent way to post PRs on top of not-yet-merged commits - I push those other commits into a temporary branch in the main cockroach repository, and then generate a PR between that branch and my work branch.

:lgtm:


Review status: 0 of 79 files reviewed at latest revision, 2 unresolved discussions, all commit checks successful.


sql/parser/expr.go, line 89 [r9] (raw file):
This is a prime candidate for something that could make use of new infrastructure. Instead of a separate function, we would have a flag that indicates that this expression is preceded or followed by an operator and thus we need to add a paren.

Later if we want to implement operator precedence into account the "flags" could keep track of the priorities of the preceding and following operators to determine if parens are really needed.OR


sql/parser/format.go, line 32 [r9] (raw file):
So the plan here is that we will OR flags together? That is a bit tedious, it would be cleaner and more extensible to pass a FormatterFlags struct which can contain bools and even other arbitrary stuff. I realize this implies a huge refactoring, maybe just add a comment to consider it in the future.

Even if we keep it as an int, I would define a FormatterFlags type as it will make the implementations more readable (when looking at the implementations it's not obvious what f int is).


Comments from Reviewable

This patch replaces the pretty-printing using a tree of String() calls
by a Formattable interface common to all syntax object. This interface
allows caller code to pass formatting flags throughout the pretty
printing. This can be used later to implement conditional
printing of node details (e.g. typing information).

A side effect is a performance improvement: prior to this patch String
and bytes.Buffer objects would be created and destroyed throughout the
recursion, causing pressure on memory allocation. With this patch a
single Buffer object is used per top-level request to the
pretty-printer.
This patch introduces a new features: the statement `EXPLAIN (TYPES)`
to inspect the types of intermediate results in a query plan.

Known limitation: `EXPLAIN(TYPES)` cannot be applied on statements
containing placeholders ($xxx).

As a side effect this patch also fixes a bug, where EXPLAIN(PLAN)
would cause a panic when invoked on an INSERT statement.
@knz
Copy link
Contributor Author

knz commented Apr 30, 2016

Replaced by #6424 to ease reviewing.

@knz knz closed this Apr 30, 2016
@knz
Copy link
Contributor Author

knz commented Apr 30, 2016 via email

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

Successfully merging this pull request may close these issues.

3 participants