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

fix(java client): allow java client to accept statements with more than one semicolon #7243

Merged
merged 5 commits into from
Mar 18, 2021

Conversation

jzaralim
Copy link
Contributor

Description

The executeStatement Java client method fails if the given statement contains a string with semicolons. This PR updates the validator logic to ignore semicolons in strings when counting semicolons.

Testing done

Added a unit test

Reviewer checklist

  • Ensure docs are updated if necessary. (eg. if a user visible feature is being added or changed).
  • Ensure relevant issues are linked (description should include text like "Fixes #")

@jzaralim jzaralim requested a review from a team as a code owner March 16, 2021 22:03
Copy link
Contributor

@vcrfxia vcrfxia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jzaralim -- great stuff! Comments inline.

@@ -34,7 +37,10 @@ static boolean validateExecuteStatementRequest(
return false;
}

if (sql.indexOf(";") != sql.lastIndexOf(";")) {
if (Arrays.stream(sql.split(QUOTED_STRING_OR_SEMICOLON))
.mapToInt(part -> part.split(";", -1).length - 1)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why is -1 being passed as the second argument rather than using the default 0? We shouldn't reject statements with extra white space after the semicolon. Might even be nice to trim the resulting pieces to see if there really are multiple pieces on either side of the semicolon.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This counts the number of semicolons in outside of strings. The -1 means that empty strings before or after a semicolon are counted. The name of the regex is a misnomer, which has been fixed.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks. To check my understanding (again): the purpose of the -1 is to ensure that there are always 2 pieces on either side of the semicolon, which means the length of the (split) array minus 1 is the number of semicolons in the statement? Can we add a short comment into the code to explain this for future readers?

Would be great to also add a short javadoc explaining exactly what this method checks. For example, this method does not validate that there are no hanging statements (text at the end not ended with a semicolon). This is fine since the client passes the original text to the server and the server will throw if such a statement is received, but it's not immediately clear to a reader that this is intentional rather than an oversight.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yup, that is correct. Will add a javadoc.

public void shouldExecuteSingleStatementWithMultipleSemicolons() throws Exception {
// Given
final CommandStatusEntity entity = new CommandStatusEntity(
"CREATE STREAM FOO AS CONCAT(A, `wow;`) FROM `BAR`; ",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's also update this example to reflect proper usage of quoted strings, to avoid confusion (contributed to my question above).

Suggested change
"CREATE STREAM FOO AS CONCAT(A, `wow;`) FROM `BAR`; ",
"CREATE STREAM FOO AS CONCAT(A, 'wow;') FROM `BAR`; ",

Copy link
Contributor

@vcrfxia vcrfxia left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM once the outstanding comments are addressed. Thanks for the fix, @jzaralim !

@jzaralim jzaralim merged commit 4086acb into confluentinc:master Mar 18, 2021
@jzaralim jzaralim deleted the semicolons branch March 18, 2021 20:24
jzaralim pushed a commit to jzaralim/ksql that referenced this pull request Mar 22, 2021
…an one semicolon (confluentinc#7243)

* fix(java client): allow java client to accept statements with more than one semicolon

* address review comments

* add javadoc

* address review comments

* Update DdlDmlRequestValidators.java
jzaralim pushed a commit to jzaralim/ksql that referenced this pull request Mar 22, 2021
…an one semicolon (confluentinc#7243)

* fix(java client): allow java client to accept statements with more than one semicolon

* address review comments

* add javadoc

* address review comments

* Update DdlDmlRequestValidators.java
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.

2 participants