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: implement IFERROR and ISERROR #25304
Conversation
cc @andreimatei |
Note: I made a mistake in the definition of iserror -- my explanation about "either/or code" is wrong. |
Fixed, PTAL |
Reviewed 9 of 9 files at r1. pkg/sql/sem/tree/expr.go, line 542 at r1 (raw file):
pkg/sql/sem/tree/expr.go, line 546 at r1 (raw file):
I would rename pkg/sql/sem/tree/expr.go, line 579 at r1 (raw file):
pkg/sql/sem/tree/expr.go, line 580 at r1 (raw file):
Do you think it's worth folding this into pkg/sql/sem/tree/type_check.go, line 621 at r1 (raw file):
Comments from Reviewable |
Review status: all files reviewed at latest revision, 5 unresolved discussions, some commit checks failed. pkg/sql/sem/tree/expr.go, line 542 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/sem/tree/expr.go, line 546 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/sem/tree/expr.go, line 579 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. pkg/sql/sem/tree/expr.go, line 580 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
do you mean that pkg/sql/sem/tree/type_check.go, line 621 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. Comments from Reviewable |
Review status: 4 of 9 files reviewed at latest revision, 1 unresolved discussion, some commit checks failed. pkg/sql/sem/tree/expr.go, line 580 at r1 (raw file): Previously, knz (kena) wrote…
No sorry, I just meant folding these two expression types into a single AST node. Comments from Reviewable |
409fcb8
to
0acdf3d
Compare
Review status: 4 of 9 files reviewed at latest revision, 1 unresolved discussion. pkg/sql/sem/tree/expr.go, line 580 at r1 (raw file): Previously, nvanbenschoten (Nathan VanBenschoten) wrote…
Done. RFAL Comments from Reviewable |
ef40e39
to
fb16165
Compare
Reviewed 6 of 6 files at r2. Comments from Reviewable |
The SQL scalar operators `IFERROR` and `ISERROR` are inspired from the MSSQL feature of the same name. In their simple form: - `ISERROR(Expr)` returns false if `Expr` evaluates without error; true otherwise. - `IFERROR(Expr, Val)` returns the value of `Expr` if `Expr` evaluates without error, otherwise it evaluates and returns `Val`. In order to avoid catching too many errors, they exist also in an "advanced" mode: - `ISERROR(Expr, Code)` returns false if `Expr` evaluates without error; otherwise, it evaluates `Code`, and if `Code` evaluates without error it checks whether the error produced for `Expr` has pg error code that matches `Code`. If `Code` is NULL or the pg error code matches, `ISERROR` returns true, otherwise `ISERROR` returns `Expr`'s evaluation error. For example, `ISERROR(1/x, '22012') returns true iff Expr evaluates with a "divide by zero" error. - `IFERROR(Expr, Val, Code)` evaluates `Expr` and returns its value if no error. If there is an error, it evaluates `Code` and checks the `Expr` eval error with `Code` as for `ISERROR` above. If the error code matches, it evaluates and returns `Val`. Note: this only covers the evaluation of scalar expressions that do not require access to data in the underlying KV store. - If ISERROR/IFERROR is applied to a subquery and the subquery errors out, the entire query fails and ISERROR/IFERROR are ineffective. - In general if the scalar expression causes a KV operation and the KV operation fails (e.g. `nextval()` on a sequence), again the entire query fails. - Some very specific uses of KV may be caught (e.g. ::REGCLASS casts) but this is neither tested nor guaranteed. Release note (sql change): the two experimental scalar operators `IFERROR()` and `ISERROR()` have been introduced. They may be documented for public use in the future.
Thanks! bors r+ |
25304: sql: implement IFERROR and ISERROR r=knz a=knz Fixes #9760. The SQL scalar operators `IFERROR` and `ISERROR` are inspired from the MSSQL feature of the same name. In their simple form: - `ISERROR(Expr)` returns false if `Expr` evaluates without error; true otherwise. - `IFERROR(Expr, Val)` returns the value of `Expr` if `Expr` evaluates without error, otherwise it evaluates and returns `Val`. In order to avoid catching too many errors, they exist also in an "advanced" mode: - `ISERROR(Expr, Code)` returns false if `Expr` evaluates without error; otherwise, it evaluates `Code`, and if `Code` evaluates without error it checks whether the error produced for `Expr` has pg error code that matches `Code`. If `Code` is NULL or the pg error code matches, `ISERROR` returns true, otherwise `ISERROR` returns `Expr`'s evaluation error. For example, `ISERROR(1/x, '22012') returns true iff Expr evaluates with a "divide by zero" error. - `IFERROR(Expr, Val, Code)` evaluates `Expr` and returns its value if no error. If there is an error, it evaluates `Code` and checks the `Expr` eval error with `Code` as for `ISERROR` above. If the error code matches, it evaluates and returns `Val`. Note: this only covers the evaluation of scalar expressions that do not require access to data in the underlying KV store. - If ISERROR/IFERROR is applied to a subquery and the subquery errors out, the entire query fails and ISERROR/IFERROR are ineffective. - In general if the scalar expression causes a KV operation and the KV operation fails (e.g. `nextval()` on a sequence), again the entire query fails. - Some very specific uses of KV may be caught (e.g. ::REGCLASS casts) but this is neither tested nor guaranteed. Release note (sql change): the two experimental scalar operators `IFERROR()` and `ISERROR()` have been introduced. They may be documented for public use in the future. Co-authored-by: Raphael 'kena' Poss <knz@cockroachlabs.com>
Build succeeded |
This commit introduces the IFERROR/ISERROR scalar operators. See cockroachdb#25304 for more background on these operators. Release note: None
This commit introduces the IFERROR/ISERROR scalar operators. See cockroachdb#25304 for more background on these operators. Release note: None
This commit introduces the IFERROR/ISERROR scalar operators. See cockroachdb#25304 for more background on these operators. Release note: None
This commit introduces the IFERROR/ISERROR scalar operators. See cockroachdb#25304 for more background on these operators. Release note: None
Fixes #9760.
The SQL scalar operators
IFERROR
andISERROR
are inspired from theMSSQL feature of the same name. In their simple form:
ISERROR(Expr)
returns false ifExpr
evaluateswithout error; true otherwise.
IFERROR(Expr, Val)
returns the value ofExpr
ifExpr
evaluateswithout error, otherwise it evaluates and returns
Val
.In order to avoid catching too many errors, they exist also in an
"advanced" mode:
ISERROR(Expr, Code)
returns false ifExpr
evaluates withouterror; otherwise, it evaluates
Code
, and ifCode
evaluateswithout error it checks whether the error produced for
Expr
has pgerror code that matches
Code
. IfCode
is NULL or the pg errorcode matches,
ISERROR
returns true, otherwiseISERROR
returnsExpr
's evaluation error. For example, `ISERROR(1/x, '22012')returns true iff Expr evaluates with a "divide by zero" error.
IFERROR(Expr, Val, Code)
evaluatesExpr
and returns its value ifno error. If there is an error, it evaluates
Code
and checks theExpr
eval error withCode
as forISERROR
above. If the errorcode matches, it evaluates and returns
Val
.Note: this only covers the evaluation of scalar expressions that
do not require access to data in the underlying KV store.
out, the entire query fails and ISERROR/IFERROR are ineffective.
operation fails (e.g.
nextval()
on a sequence), again the entirequery fails.
but this is neither tested nor guaranteed.
Release note (sql change): the two experimental scalar operators
IFERROR()
andISERROR()
have been introduced. They may bedocumented for public use in the future.