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

HIVE-27488: HPL/SQL Quote literals not behaving as expected #4476

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions hplsql/src/main/java/org/apache/hive/hplsql/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,18 +32,22 @@ public static String unquoteString(String s) {
}

int len = s.length();
StringBuilder s2 = new StringBuilder(len);
StringBuilder s2 = new StringBuilder(len);
boolean isEscape = true;
zabetak marked this conversation as resolved.
Show resolved Hide resolved

for (int i = 0; i < len; i++) {
char ch = s.charAt(i);
char ch2 = (i < len - 1) ? s.charAt(i+1) : 0;

if((i == 0 || i == len -1) && (ch == '\'' || ch == '"'))
if((i == 0 || i == len - 1) && (ch == '\'' || ch == '"'))
continue;
else
// \' and '' escape sequences
if((ch == '\\' && ch2 == '\'') || (ch == '\'' && ch2 == '\''))
continue;
// \' and '' escape sequences and include ' if last two characters are ''
if((ch == '\\' && ch2 == '\'') || (ch == '\'' && ch2 == '\'' && isEscape && i != len - 2)) {
isEscape = false;
continue;
}
isEscape = true;

s2.append(ch);
}
Expand Down
35 changes: 35 additions & 0 deletions hplsql/src/test/java/org/apache/hive/hplsql/TestUtils.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package org.apache.hive.hplsql;

import org.junit.Assert;
import org.junit.Test;

public class TestUtils {

@Test
public void testShouldRemoveEscapeCharactersFromTheGivenInput() {
Copy link
Contributor

Choose a reason for hiding this comment

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

We should include the name of the method that is tested. Something like:
testUnquoteStringRemovesOneQuoteWhenTwoConsecutive

Assert.assertEquals("a'a", Utils.unquoteString("'a''a'"));
Assert.assertEquals("'a", Utils.unquoteString("'''a'"));
Assert.assertEquals("a'", Utils.unquoteString("'a'''"));
Assert.assertEquals("''aa", Utils.unquoteString("'''''aa'"));
Assert.assertEquals("a''a", Utils.unquoteString("'a''''a'"));
Assert.assertEquals("aa''", Utils.unquoteString("'aa'''''"));
}
}
6 changes: 6 additions & 0 deletions hplsql/src/test/queries/local/dbms_output.sql
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,10 @@ DECLARE
BEGIN
DBMS_OUTPUT.PUT_LINE('Hello, world!');
DBMS_OUTPUT.PUT_LINE(str);
DBMS_OUTPUT.PUT_LINE('a''a');
DBMS_OUTPUT.PUT_LINE('''a');
DBMS_OUTPUT.PUT_LINE('a''');
DBMS_OUTPUT.PUT_LINE('''''aa');
DBMS_OUTPUT.PUT_LINE('a''''a');
DBMS_OUTPUT.PUT_LINE('aa''''');
END;
6 changes: 6 additions & 0 deletions hplsql/src/test/results/local/dbms_output.out.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,9 @@
Ln:2 DECLARE str VARCHAR = 'Hello, world!'
Hello, world!
Hello, world!
a'a
'a
a'
''aa
a''a
aa''
Loading