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

Parentheses are incorrectly removed for type arrays in default expressions #63097

Closed
the-ericwang35 opened this issue Apr 5, 2021 · 2 comments · Fixed by #63103
Closed

Parentheses are incorrectly removed for type arrays in default expressions #63097

the-ericwang35 opened this issue Apr 5, 2021 · 2 comments · Fixed by #63103
Assignees
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.

Comments

@the-ericwang35
Copy link
Contributor

the-ericwang35 commented Apr 5, 2021

Describe the problem

The way we currently serialize type arrays in default expressions removes parentheses in the expression. As a result, the expressions becomes invalid and results in an error.

To Reproduce

CREATE TYPE default_abc AS ENUM('a', 'b', 'c');
CREATE TABLE t (i STRING DEFAULT ('{a}'::_default_abc)[1]::STRING);

ERROR: at or near ":": syntax error
SQLSTATE: 42601
DETAIL: source SQL:
SET ROW ('{a}':::STRING::@100060[1:::INT8]::STRING)
                                  ^
HINT: try \h SET SESSION

Expected behavior
This syntax should be valid and not result in an error.
The expression should instead be serialized as ('{a}':::STRING::@100060)[1:::INT8]::STRING (notice the parentheses around '{a}':::STRING::@100060, which are currently being dropped).

@the-ericwang35 the-ericwang35 added the C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior. label Apr 5, 2021
@the-ericwang35 the-ericwang35 self-assigned this Apr 5, 2021
@ajwerner
Copy link
Contributor

ajwerner commented Apr 5, 2021

Eric points out that the problem is that we drop all parents in type checking:

I think it happens here, during the type checking of ParenExpr:

// TypeCheck implements the Expr interface.
func (expr *ParenExpr) TypeCheck(
ctx context.Context, semaCtx *SemaContext, desired *types.T,
) (TypedExpr, error) {
exprTyped, err := expr.Expr.TypeCheck(ctx, semaCtx, desired)
if err != nil {
return nil, err
}
// Parentheses are semantically unimportant and can be removed/replaced
// with its nested expression in our plan. This makes type checking cleaner.
return exprTyped, nil
}

It basically just drops the parentheses and only returns the inner expr. There’s even a comment that says Parentheses are semantically unimportant and can be removed/replace

git blame says that it used to return the entire expression (including the parentheses if i’m not mistaken)
7a02e3a#diff-b2a517db9d4d185d9e0258c%5B%E2%80%A6%5D33c9277cdc6ba71ebbd95071L606

Unfortunately this seems too simplistic. It seems like any [...] tokens after a type get interpreted as part of the type. This is true in postgres too:

postgres=# CREATE TYPE typ AS ENUM ('a');
CREATE TYPE
postgres=# SELECT '{a}'::VARCHAR::_typ[1]::VARCHAR;
ERROR:  type "_typ[]" does not exist
postgres=# SELECT ('{a}'::VARCHAR::_typ)[1]::VARCHAR;
 varchar 
---------
 a

In short, it does seem like these parens are meaningful in this case. I wonder if there are other cases we haven't thought of? For now, one thing we can do is look and see if the outermost expression is a cast and retain the parens.

@ajwerner
Copy link
Contributor

ajwerner commented Apr 5, 2021

Another option is to let the ParensExpr remove itself but then inject parens in the case where you've got a cast expr in an indirection expr then you need to wrap it in a ParensExpr

craig bot pushed a commit that referenced this issue Apr 7, 2021
63103: sql: wrap CastExpr within IndirectionExpr in a ParenExpr r=ajwerner,rafiss a=the-ericwang35

Fixes #63097.

When we type check ParenExpr, we remove the parentheses.
This can cause issues for IndirectionExprs.

For example, something like `('{a}'::_typ)[1]` would
turn into `'{a}'::_typ[1]` after type checking, resulting
in the `[1]` being interpreted as part of the type.
This would result in errors when trying to use these
expressions in default expressions.
This patch checks for this specific case, and if
it finds that there is a CastExpr within an IndirecionExpr,
it will wrap it in a ParenExpr.

Release note: None

63108: colexec: wrap DrainMeta with panic-catcher and protect columnarizer r=yuzefovich a=yuzefovich

**colexec: wrap DrainMeta with panic-catcher and protect columnarizer**

Previously, in some edge cases (like when a panic is encountered during
`Operator.Init`) the metadata sources could have been uninitialized, so
when we tried to drain them, we'd encounter a crash. In order to avoid
that in the future, now all root components will wrap the draining with
the panic-catcher. Additionally, we now protect the columnarizer in this
case explicitly - if it wasn't initialized, it won't drain the wrapped
processor in `DrainMeta`.

Fixes: #62514.

Release note: None

**rowexec: remove redundant implementations of MetadataSource interface**

Previously, some row-by-row processors implemented
`execinfra.MetadataSource` interface. The idea behind that originally
was to allow for wrapped processors to return their metadata in the
vectorized flow, but nothing explicit is actually needed because every
wrapped processor has a columnarizer after it which will drain the
processor according to row-by-row model (by moving into draining state
and exhausting the trailing meta). This commit removes those redundant
implementations.

This allows us to move the interface into `colexecop` package where it
belongs.

Release note: None

63169: README.md: fix indentation for Deployment section r=ajwerner a=ajwerner

It was formatted as preformatted as opposed to the list it was trying to be.

Release note: None

Co-authored-by: Eric Wang <[email protected]>
Co-authored-by: Yahor Yuzefovich <[email protected]>
Co-authored-by: Andrew Werner <[email protected]>
@craig craig bot closed this as completed in 9b62b56 Apr 7, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-bug Code not up to spec/doc, specs & docs deemed correct. Solution expected to change code/behavior.
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants