Andrew Kyle Purtell created PHOENIX-8092:
--------------------------------------------

             Summary: String literals in generated SQL do not escape backslashes
                 Key: PHOENIX-8092
                 URL: https://issues.apache.org/jira/browse/PHOENIX-8092
             Project: Phoenix
          Issue Type: Bug
          Components: core
    Affects Versions: 5.3.2
            Reporter: Andrew Kyle Purtell
            Assignee: Andrew Kyle Purtell
             Fix For: 5.4.0, 5.3.3


{{PartialIndexIT.testDDLWithAllDataTypes[local=false, uncovered=true, 
salted=true]}} fails:
{noformat}
java.lang.AssertionError
        at org.junit.Assert.fail(Assert.java:89)
        at 
org.apache.phoenix.end2end.index.PartialIndexIT.testDDLWithAllDataTypes(PartialIndexIT.java:210)
Caused by: org.apache.phoenix.exception.PhoenixParserException: ERROR 602 
(42P00): Syntax error. Missing "LPAREN" at line 1, column 650.
        at 
org.apache.phoenix.compile.CreateIndexCompiler.verifyIndexWhere(CreateIndexCompiler.java:2
{noformat}
The test creates a table with many column types, including {{{}Z VARCHAR 
ARRAY[10]{}}}. Then it runs {{{}CREATE UNCOVERED INDEX ... WHERE (kp > 
'5'){}}}. To check the WHERE clause, {{CreateIndexCompiler.verifyIndexWhere}} 
builds an UPSERT statement with one sample value for each column, and prepares 
it. The statement does not parse.

For array types other than the date family, {{CreateIndexCompiler.getValue}} 
returns {{{}"ARRAY" + type.getSampleValue().toString(){}}}. For VARCHAR and 
CHAR arrays, the sample element is one random character in the code points 1 to 
125. When the character is a backslash, the text is {{{}ARRAY['\']{}}}. The SQL 
lexer reads {{\'}} as an escaped quote, so the literal does not close. The 
parser reports the error at the {{ARRAY}} keyword of the column. The chance is 
1 in 125 for each column, about 3.2% for each run of the class (4 parameter 
sets).

{{StringUtil.escapeStringConstant}} doubles single quotes, but not backslashes. 
{{{}PVarchar.toStringLiteral{}}}, {{PChar}} and {{PDate}} use it to write 
string literals into SQL text. The lexer ({{{}PhoenixSQL.g{}}}, 
{{STRING_LITERAL}} and {{{}CHAR_ESC{}}}) reads a backslash as the start of an 
escape sequence:
||Text||Value||
|{{{}\n{}}}, {{{}\r{}}}, {{{}\t{}}}, {{{}\b{}}}, {{\f}}|a control character|
|{{{}\"{}}}, {{{}\'{}}}, {{
}}|{{{}"{}}}, {{{}'{}}}, \{{}}|
|{{{}_{}}}, {{%}}|{{{}_{}}}, {{%}} (kept, for LIKE)|
|{{\x}} (other)|{{\x}}|
|{{''}}|{{'}}|

Thus text that Phoenix generates does not always parse back to the same value. 
A value {{a\nb}} (backslash, {{{}n{}}}) is written as {{'a\nb'}} and read back 
as {{{}a{}}}, newline, {{{}b{}}}. A value that ends with a backslash gives a 
literal that does not close. This text is stored and parsed again: view 
statements, index WHERE clauses, column defaults, ARRAY values, EXPLAIN text.

Three other places depend on the old behavior:
 * {{MetaDataClient}} escaped backslashes in the whole functional index 
expression text. This compensated for the missing escape in string literals, 
but it also doubled backslashes in quoted identifiers.

 * {{PostIndexDDLCompiler}} escaped the stored expression text a second time 
for the index build SELECT. An index build by this path (a transactional data 
table, IndexTool with a snapshot, TransformTool) read a different value than 
index maintenance and query rewrite.

 * {{SchemaUtil.normalizeLiteral}} gave the SQL text of a literal without the 
outer quotes, not its value. SHOW TABLES/SCHEMAS LIKE patterns and GRANT/REVOKE 
names and permissions use it. A quote in the value thus came back doubled.



--
This message was sent by Atlassian Jira
(v10.3.26#10030026)

Reply via email to