-
Notifications
You must be signed in to change notification settings - Fork 3.8k
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
TestLogic/testdata/views fails with COCKROACH_DISTSQL_MODE=on #12532
Comments
The problem can be reproduced without views, just with the statement
But this is a more tricky to fix than I thought. If I make things like
Note that this issue already exists on master with interval:
CC @a-robinson, @knz |
Interesting, thanks for all the details. I believe the redundant DATE/TIMESTAMP keywords when you add the type to I don't know much of anything about the distsql serialization, so I can't say for sure what the correct way to resolve this is. However, it seems more accurate to me to keep the printing of types in CastExpr rather than in the Datum types, such that we only pretty-print explicit types when the user explicitly typed a literal. That would mean removing the |
We aren't doing any specific serialization - we just Format the expression and expect it to parse back to the same thing. So for e.g. a DInterval in an expression, we need to format it as I don't understand why we have a |
We handle INTERVAL inconsistently in parsing, it turns out. In most uses, we parse it as a CastExpr (which leads to the double printing of the INTERVAL constant): cockroach/pkg/sql/parser/sql.y Line 4816 in 1bc5527
We handle it differently when it's part of a timezone value, though (which currently necessitates the inclusion of the INTERVAL constant in DInterval's Format method): cockroach/pkg/sql/parser/sql.y Line 1460 in 1bc5527
|
I'm still confused about something - in that case CastExpr.Expr is a StrVal, not a DInterval.. If at some point we form a DInterval, it should replace the entire CastExpr, not just CastExpr.Expr no? |
Yes, that would make more sense than keeping the CastExpr around. |
I've done a bunch of investigation and I think I got to the bottom of this. Take this statement: SELECT * FROM t WHERE d > d + INTERVAL '10h'; WHERE expression after parsing has a
WHERE expression in the scanNode after makePlan/expandPlan (as seen by distsql
Now for the view case: CREATE VIEW dt AS SELECT d, t FROM t WHERE d > d + INTERVAL '10h'; In
But after calling
What is happening is fairly subtle:
In the view case, the former "leaks" into the statement. The latter does not I see a bunch of potential fixes to
@knz, @nvanbenschoten I am inclined to do all of the above.. Thoughts? |
Nice job tracking this issue down @RaduBerinde. Before coming up with the exact fix, I think we first need to resolve the inconsistency you pointed out with timestamp and date datums formatting without their type in front, unlike interval datums. Clearly, these need to all act the same way, and we almost certainly want datums to roundtrip when printed and then parsed, so I think we'd want to go with the interval approach (ie. DDate formats as Once that issue is resolved, I think it would be sufficient to make a small modification to your third fix dealing with |
On the first part - of course. It just needs to be done in the same PR with the other fix (it would break an existing test mentioned above). I see the ambiguity problem. What about the other proposal - can't CastExpr.TypeCheck return a new CastExpr instead of modifying in place? (one that also has a "corrected" syntaxMode) |
Yes, CastExpr.TypeCheck can return a new CastExpr if necessary. That said,
the reason we try to modify in-place is for performance. We tested making a
full copy of the expression tree during type checking back when this
approach was going in and found the performance impact to be unacceptable.
Are there any correctness issues with modifying in-place to simply correct
the syntaxMode of the CastExpr in these cases?
…On Fri, Dec 30, 2016, 3:03 AM RaduBerinde ***@***.***> wrote:
On the first part - of course. It just needs to be done in the same PR
with the other fix (it would break an existing test mentioned above
<#12532 (comment)>
).
I see the ambiguity problem. What about the other proposal - can't
CastExpr.TypeCheck return a new CastExpr instead of modifying in place?
(one that also has a "corrected" syntaxMode)
—
You are receiving this because you were mentioned.
Reply to this email directly, view it on GitHub
<#12532 (comment)>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AFL7-Ehw7z6adcO7LdHdwxpVfHFaBzWsks5rNLrkgaJpZM4LTB3E>
.
|
No correctness issues, just form - the expression stored in the view will have this weird extra cast (I am guessing the user can list the views so it would be visible). |
Returning to the eliding the CastExpr idea for constants who can become the cast type, this would actually be completely possible without the added TypeAnnotation I mentioned earlier if we made DString and DBytes format with their types as well. I think this would also be beneficial for #12611, as it would remove any ambiguity introduced from serializing those types. |
I see. Another question I had is if these expressions should be CastExpr in the first place.. They are not intended to work in general, are they? Eg does postgres support 'INTERVAL (some expression returning a string)'? |
no it doesn't. Why not using TypeAnnotation again? It sounded to me like the right approach. |
Why do we need any enclosing expr - can't the parser when it encounters |
I am assuming that this |
Postgres expresses that syntax with a cast, which I assume is why we did it in the first place. However, I can't think of any pressing reasons why we need to use a |
Do we need to support a placeholder inside the constant part? My guess is no - that would normally be done with an explicit cast, not with an expression like |
I think that we can't do the cast at parse time because we need a I'm struggling to find the correct solution for this, the problem is pretty general: if we want any expression to not change semantically after a formatting and parsing cycle, it seems that we would need to add a type annotation to most datums (even So I'm thinking we should 1) add a type annotation to all datums that can result in ambiguous parsing and 2) retire the |
Removing the "prefix" syntax for `CastExpr`s; we now prepend the type as part of Datum formatting (conditional on a new `FmtFlag`). This ensures that expressions that contain `DDInterval`, `DDate` etc (without a `CastExpr`) can be formatted and re-parsed correctly. To avoid a redundant cast, we elide the `CastExpr` during type checking if the argument is a constant that can become the required type. See cockroachdb#12532 for more context. Fixed cockroachdb#12532.
Introducing a FmtFlag that disambiguates constants with a type prefix. This ensures that expressions that contain `DDInterval`, `DDate` etc (without a `CastExpr`) can be formatted and re-parsed correctly. To avoid a redundant cast, we elide the `CastExpr` during type checking if the argument is a constant that can become the required type. The new `FmtParsable` is used when storing expressions in view and when serializing expressions for distsql. Fixes cockroachdb#12532.
Introducing a FmtFlag that disambiguates constants with a type prefix. This ensures that expressions that contain `DDInterval`, `DDate` etc (without a `CastExpr`) can be formatted and re-parsed correctly. To avoid a redundant cast, we elide the `CastExpr` during type checking if the argument is a constant that can become the required type. The new `FmtParsable` is used when storing expressions in view and when serializing expressions for distsql. Fixes cockroachdb#12532.
Introducing a FmtFlag that disambiguates constants with a type prefix. This ensures that expressions that contain `DDInterval`, `DDate` etc (without a `CastExpr`) can be formatted and re-parsed correctly. To avoid a redundant cast, we elide the `CastExpr` during type checking if the argument is a constant that can become the required type. The new `FmtParsable` is used when storing expressions in view and when serializing expressions for distsql. Fixes cockroachdb#12532.
This change addresses a few related problems (described in more detail in cockroachdb#12532): - ambiguous datum types after serialization - invalid prefix casts after expression processing (like `INTERVAL INTERVAL `1.5s`'). Changes: - a new FmtFlag adds type annotations to datums and placeholders. This ensures that expressions that contain `DDInterval`, `DDate` can be formatted and re-parsed correctly. This replaces ad-hoc type prepending like `INTERVAL '..'` or `DATE '..'`. - `CastExpr.TypeCheck` now returns the underlying type-checked expression directly when it is a Datum that we know for sore is of identical type. - `castPrefix` (renamed to `castPrepend`) now only functions if the underlying expression is a string literal (preventing incorrect expressions like `DECIMAL '1.0':::string`). `castPrefixParens` is removed (it was incorrect). - `AnnotateTypeExpr.TypeCheck` now returns the underlying type-checked expression rather than changing the `AnnotateTypeExpr` in-place and returning it (which could result in double annotations with the new `FmtFlag`). `AnnotateTypeExpr` is no longer a `TypedExpr` so it is never part of a type-checked expression (which makes sense). - type checking tests are extended to verify the serliazed type-checked expression, which verifies both serialization and type resolution. Note that changing the prepend syntax to a `AnnotateTypeExpr` was considered but that doesn't work for things like `DECIMAL '2.0'`. Fixes cockroachdb#12532. Resolves some of the concerns in cockroachdb#12611.
This change addresses a few related problems (described in more detail in cockroachdb#12532): - ambiguous datum types after serialization - invalid prefix casts after expression processing (like `INTERVAL INTERVAL `1.5s`'). Changes: - a new FmtFlag adds type annotations to datums and placeholders. This ensures that expressions that contain `DDInterval`, `DDate` can be formatted and re-parsed correctly. This replaces ad-hoc type prepending like `INTERVAL '..'` or `DATE '..'`. - `CastExpr.TypeCheck` now returns the underlying type-checked expression directly when it is a Datum that we know for sure is of identical type. - `castPrefix` (renamed to `castPrepend`) now only functions if the underlying expression is a string literal (preventing incorrect expressions like `DECIMAL '1.0':::STRING`). `castPrefixParens` is removed (it was incorrect). - `AnnotateTypeExpr.TypeCheck` now returns the underlying type-checked expression rather than changing the `AnnotateTypeExpr` in-place and returning it (which could result in double annotations with the new `FmtFlag`). `AnnotateTypeExpr` is no longer a `TypedExpr` so it is never part of a type-checked expression (which makes sense). - type checking tests are extended to verify the serialized type-checked expression, which verifies both serialization and type resolution. Note that changing the prepend syntax to a `AnnotateTypeExpr` was considered but that doesn't work for things like `DECIMAL '2.0'`. Fixes cockroachdb#12532. Helps with cockroachdb#12611.
This change addresses a few related problems (described in more detail in cockroachdb#12532): - ambiguous datum types after serialization - invalid prefix casts after expression processing (like `INTERVAL INTERVAL `1.5s`'). Changes: - a new FmtFlag adds type annotations to datums and placeholders. This ensures that expressions that contain `DDInterval`, `DDate` can be formatted and re-parsed correctly. This replaces ad-hoc type prepending like `INTERVAL '..'` or `DATE '..'`. - `CastExpr.TypeCheck` now returns the underlying type-checked expression directly when it is a Datum that we know for sure is of identical type. - `castPrefix` (renamed to `castPrepend`) now only functions if the underlying expression is a string literal (preventing incorrect expressions like `DECIMAL '1.0':::STRING`). `castPrefixParens` is removed (it was incorrect). - `AnnotateTypeExpr.TypeCheck` now returns the underlying type-checked expression rather than changing the `AnnotateTypeExpr` in-place and returning it (which could result in double annotations with the new `FmtFlag`). `AnnotateTypeExpr` is no longer a `TypedExpr` so it is never part of a type-checked expression (which makes sense). - type checking tests are extended to verify the serialized type-checked expression, which verifies both serialization and type resolution. Note that changing the prepend syntax to a `AnnotateTypeExpr` was considered but that doesn't work for things like `DECIMAL '2.0'`. Fixes cockroachdb#12532. Helps with cockroachdb#12611.
In distsql mode, this pair of statements fails as below:
This likely has to do with the serialization of the expressions -
DATE
,TIMESTAMP
disappear:The text was updated successfully, but these errors were encountered: