Skip to content

Handles Escape Characters in String Literals - #81

Closed
MitchellGale wants to merge 23 commits into
integ-escapecharacterfrom
dev-escapecharacter
Closed

MitchellGale wants to merge 23 commits into
integ-escapecharacterfrom
dev-escapecharacter

Conversation

@MitchellGale

Copy link
Copy Markdown

Description

Correctly handles escape characters in string literals. No longer returns all escape characters passed in.

I\'m       -> I'm
I\\\'m     -> I\'m
I\\\\\'m   -> I\\'m
I\\\\\\\'m -> I\\\'m
I''m       -> I'm
fetched rows / total rows = 1/1
+----------+
| 'I''m'   |
|----------|
| I'm      |
+----------+
opensearchsql> SELECT 'I\'m';                                                                                                                                                                                                                               
fetched rows / total rows = 1/1
+----------+
| 'I\'m'   |
|----------|
| I'm      |
+----------+
opensearchsql> SELECT 'I\\\'m';                                                                                                                                                                                                                             
fetched rows / total rows = 1/1
+------------+
| 'I\\\'m'   |
|------------|
| I\'m       |
+------------+

Issues Resolved

Resolves Issue 297(Unable to escape characters for string literals[BUG]).

Check List

  • New functionality includes testing.
    • All tests pass, including unit test, integration test and doctest
  • New functionality has been documented.
    • New functionality has javadoc added
    • New functionality has user manual doc added
  • Commits are signed per the DCO using --signoff

By submitting this pull request, I confirm that my contribution is made under the terms of the Apache 2.0 license.
For more information on following Developer Certificate of Origin and signing off your commits, please check here.

@MaxKsyunz MaxKsyunz left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please make sure all the PR checks pass [*].

[*] Integration tests against ODFE 1.13 are an exception. They are very flaky and are more likely to fail than not.

@acarbonetto acarbonetto left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

a couple of comments

if (isQuoted(text, "`")) {
return text.substring(1, text.length() - 1).replace("\\`", "`").replace("\\\\", "\\");
}
return text;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Is it possible to reach this line?
Maybe we need to throw an error instead (RuntimeError?)

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

It'll reach it in the case where unquoteText is called when text doesn't need to be unquoted (or using quotes not supported, in which case it won't "unquote it"). I think it's a needed case.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

So we're calling unquote string on a string that doesn't need unquoting...? That sounds wrong ;)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It is being called on every thing. String that needed to be unquoted would be unquoted, the rest should left unchanged.

@Test
void unquoteTest() {
assertEquals("test\\'s", unquoteText("'test\\\\'s'"));
assertEquals("test\\'s", unquoteText("'test\\\\\\'s'"));

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

shouldn't this throw an error?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Unsure... so what is happening is java is reducing the four \ into two, and the six \ into three.
Once it's inside the function, it's a string that has 'test\\'s', which it converts into test's, no obligation for there to be an odd number of escape characters because it doesn't need to use them at least..
Once it's inside the function, it's a string that has 'test\\\'s', which it converts into test\'s.

I think it's behaving appropriately.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

It seems like ' by itself won't throw an error. Maybe this is also because the parser will throw a syntax error before ever reaching this staging in the code... so practically speaking, this is fine.
But... then this should be properly described in the method documentation. Otherwise, this function may be misused.

… Added case for double, double quote being converted to single, double quote.

Signed-off-by: mitchellg <[email protected]>
…gle of their respective type when the string is quoted by the same type. Added some unit tests to test for that condition in StringUtilsTest.java.

Signed-off-by: mitchellg <[email protected]>

while (plan.hasNext()) {
result.add(plan.next());
ExprValue resultItem = plan.next();

@MaxKsyunz MaxKsyunz Jun 28, 2022 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Why is this block of code necessary? (lines 44-58)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Probably needs a comment

Comment on lines 37 to 45
if (isQuoted(text, "\"")) {
text = text.replace("\"\"", "\"");
text = text.substring(1, text.length() - 1).replace("\\\"", "\"").replace("\\\\", "\\");
return text;
}
if (isQuoted(text, "'")) {
text = text.replace("''", "'");
text = text.substring(1, text.length() - 1).replace("\\'", "'").replace("\\\\", "\\");
return text;

@Yury-Fridlyand Yury-Fridlyand Jun 29, 2022 •

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Strings like these: ""the cake" is lie", ''the cake' is lie' would be converted into the cake" is lie/the cake' is lie. I think expected output there is "the cake" is lie/'the cake' is lie.

We shouldn't collapse two '/" chars in the beginning and in the end into '/" char.
@MaxKsyunz, what do you think?


// Tests case of unquoted string sent to unquoteText function
assertEquals("test", unquoteText("test"));
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

More tests for god of tests!
Tests with backtick `, tests with combined ' and ", mixes of '/"/\ and '/"/\ in the beginning and in the end of line wrapped by '/"/`/none and so on.

Comment on lines +78 to +80
return explain(node, context, explainNode ->
explainNode.setDescription(ImmutableMap.of("request", node.explain()))
);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Do we need these changes?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

Regarding the switch from
return explain(node, context, explainNode -> { explainNode.setDescription(ImmutableMap.of("request", node.explain())); });
to return explain(node, context, explainNode -> explainNode.setDescription(ImmutableMap.of("request", node.explain())) );
It was giving me a warning about "Warning:(60, 58) Statement lambda can be replaced with expression lambda".

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

That is an IDEA warning/suggestion. We can ignore them unless they in code we're working on.

@codecov

codecov Bot commented Jun 29, 2022 •

Copy link
Copy Markdown

Codecov Report

Merging #81 (683a9a6) into integ-escapecharacter (ab7337c) will decrease coverage by 0.00%.
The diff coverage is 100.00%.

❗ Current head 683a9a6 differs from pull request most recent head 6835f4f. Consider uploading reports for the commit 6835f4f to get more accurate results

@@                     Coverage Diff                     @@
##             integ-escapecharacter      #81      +/-   ##
===========================================================
- Coverage                    97.66%   97.66%   -0.01%     
  Complexity                    2743     2743              
===========================================================
  Files                          268      268              
  Lines                         6772     6771       -1     
  Branches                       435      435              
===========================================================
- Hits                          6614     6613       -1     
  Misses                         157      157              
  Partials                         1        1              
Flag Coverage Δ
sql-engine 97.66% <100.00%> (-0.01%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

Impacted Files Coverage Δ
...ensearch/sql/planner/physical/ProjectOperator.java 100.00% <ø> (ø)
...opensearch/executor/OpenSearchExecutionEngine.java 100.00% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ab7337c...6835f4f. Read the comment docs.

if (isQuoted(text, "\"") || isQuoted(text, "'") || isQuoted(text, "`")) {
return text.substring(1, text.length() - 1);
} else {

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

A few things can be improved here (lines 36-50).

Currently, the input string is scanned multiple times (3) and temporary strings (3) are allocated in each scenario.

A more efficient implementation is possible by scanning the input string once and using StringBuilder to allocate only one string. In pseudocode it is something like:

IF input_string IsQuoteBy $QUOTE
FOREACH char in input_string[1, LENGTH - 1):
   IF char is '\' ADD next char to StringBuilder
   ELSE ADD char to StringBuilder
       

I don't think we even need to check which quote character the string is surrounded by.

See this StackOverflow post and this method for reference.

This would also avoid the repeated code pattern -- if quoted by $QUOTE then ... .

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

@MaxKsyunz Can you please review my updated code for StringUtils.java? It does not use StringBuilder, but it resolves other issues. If you still recommend I use StringBuilder, I will figure it out and use it (thanks for the documentation).

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

If you want to reduce the number of temporary strings, we should consider using match replaceAll with a pattern. https://www.geeksforgeeks.org/matcher-replaceallstring-method-in-java-with-examples/

If we only want a single isQuoted call (which returns the quoted character) we could reduce this from 3 IF statements, to 2. But the backticks is a unique case that doesn't reduce double-backticks to a single backtick.


if (isQuoted(text, "\"")) {
return text.substring(1, text.length() - 1)
.replace("\\\"", "\"")

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Please consider using something like replaceAll instead of trying to cover each of these cases individually. We shouldn't need to consider both \\ and \\\\ and \\\\\\ and ...etc

@MitchellGale

Copy link
Copy Markdown
Author

Closed until consideration for 3.0 release.

andy-k-improving pushed a commit that referenced this pull request Nov 16, 2024
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.

4 participants