Skip to content

Commit

Permalink
HIVE-27488: Incorrect escaping of quotes in HPL/SQL literals (Dayakar…
Browse files Browse the repository at this point in the history
… M reviewed by Attila Turoczy, Stamatis Zampetakis)

Closes apache#4476
  • Loading branch information
DayaCloudera authored and yeahyung committed Jul 20, 2023
1 parent c7419e9 commit 4ff6de2
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
12 changes: 8 additions & 4 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;

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 == '\''))
// \' 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 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''

0 comments on commit 4ff6de2

Please sign in to comment.