-
Notifications
You must be signed in to change notification settings - Fork 2.4k
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
[CALCITE-6145] Function 'TRIM' without parameters throw NullPointerEx… #3869
base: main
Are you sure you want to change the base?
Changes from 2 commits
1bd8eca
295c069
ccd20a0
a3cb265
befabe0
344d70c
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -130,6 +130,9 @@ public SqlTrimFunction(String name, SqlKind kind, | |
if (operands[1] == null) { | ||
operands[1] = SqlLiteral.createCharString(" ", pos); | ||
} | ||
if (operands[2] == null) { | ||
throw new IllegalArgumentException("String to trim cannot be null"); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. "The second argument of trim cannot be null" There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it's either the first or the third: You can specify one argument (only the string to trim) or three (where to trim (leading, trailing both sides), what to trim from the string, the string to trim). There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This is not 100% correct I think as the string to trim from can be Ideally we should correctly fall through to the default case or handle the cases, where the string to trim is absent explicitly. |
||
} | ||
break; | ||
default: | ||
throw new IllegalArgumentException( | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -10843,6 +10843,7 @@ void assertSubFunReturns(boolean binary, String s, int start, | |
f.checkString("trim(trailing 'a' from 'aAa')", "aA", "VARCHAR(3) NOT NULL"); | ||
f.checkNull("trim(cast(null as varchar(1)) from 'a')"); | ||
f.checkNull("trim('a' from cast(null as varchar(1)))"); | ||
f.checkNull("trim(null)"); | ||
|
||
// SQL:2003 6.29.9: trim string must have length=1. Failure occurs | ||
// at runtime. | ||
|
@@ -10857,6 +10858,7 @@ void assertSubFunReturns(boolean binary, String s, int start, | |
f.checkFails("trim('' from 'abcde')", | ||
"Trim error: trim character must be exactly 1 character", | ||
true); | ||
f.checkFails("trim()", "String to trim cannot be null", false); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this the expected error for this function? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The creator of the original https://issues.apache.org/jira/browse/CALCITE-6145 suggested to throw an exception with an exact description. I've checked the behavior of PostgreSQL (v15): SELECT TRIM() AS trimmed_string; leads to a syntax error: SELECT TRIM("") AS trimmed_string; leads to an error: SELECT TRIM(null) AS trimmed_string; returns So I think this aligns with the behavior of PostgreSQL throwing an error during query parsing, but I think the message could be a bit better: I've added an explicit test for when the value is WDYT? @mihaibudiu There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. If the function trim() need to have arguments, then the type checker should have complained about not finding a function with a signature without arguments. I don't expect the parser to complain about it, that is strange. The parser should just accept the function, and the type checker should validate it. The original message is not good, since clearly there is no 'null' involved. However, if the error comes from the parser, there isn't much you can do about it. |
||
|
||
final SqlOperatorFixture f1 = f.withConformance(SqlConformanceEnum.MYSQL_5); | ||
f1.checkString("trim(leading 'eh' from 'hehe__hehe')", "__hehe", | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
something is wrong here. If you reached this point you have one argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think I've two (flag from which side(s) to trim, string to trim from the 3rd operand) right?
For
trim(both 'a' from 'aAa')
you've threeoperands
:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, and the message says that there are "no arguments"
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yup, currently adjusting this
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok so I've debugged through this:
If you call
trim()
the following happens:null
, not in the sense of their value, but of their absence)operands[0]
gets set toBOTH
operands[1]
gets so to" "
operands[2]
isnull
(indicating absence of the arg not that its actual value isnull
)So the actual method call from a user perspective was one without arguments (
trim()
).trim(" a ")
works finetrim(both " a ")
works finetrim("a" from)
syntax error (string to trim is absent)trim(both "a" from)
syntax error (string to trim is absent)So to handle the no-args case I think it's correct to check for
if (operands[2] == null)
in the block with3
operands. I'll add a comment explaining it, otherwise it's confusing I think.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
yes, the first two arguments have some default values
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@mihaibudiu @timgrein Perhaps we can refer to SparkSQL's exception information.
SparkSQL: