OracleCompiler incorrectly quotes * inside JSON path literals
Environment
- SqlKata: 4.0.1
- Runtime: .NET 8
- Database: Oracle
- Compiler:
SqlKata.Compilers.OracleCompiler
Description
When using FromRaw with an Oracle JSON_TABLE expression, the Oracle compiler incorrectly rewrites the *
character inside a SQL string literal.
The JSON path:
is compiled as:
This produces the following Oracle error:
ORA-40597: JSON path expression syntax error ('$"*"')
JZN-00233: invalid character at the start of a step
Minimal reproduction
using SqlKata;
using SqlKata.Compilers;
var query = new Query()
.FromRaw("""
(
SELECT PKEY_ORDER.item
FROM SOME_TABLE S
CROSS JOIN JSON_TABLE(
S.SORTED,
'$[*]'
COLUMNS (
item VARCHAR2(200) PATH '$.PersonKey',
casePersonType VARCHAR2(200) PATH '$.Type',
ord FOR ORDINALITY
)
) PKEY_ORDER
) BASE_DATA
""")
.Select("BASE_DATA.item");
var sql = new OracleCompiler().Compile(query).Sql;
Console.WriteLine(sql);
Actual output
SELECT "BASE_DATA"."item"
FROM (
SELECT PKEY_ORDER.item
FROM SOME_TABLE S
CROSS JOIN JSON_TABLE(
S.SORTED,
'$"*"'
COLUMNS (
item VARCHAR2(200) PATH '$.PersonKey',
casePersonType VARCHAR2(200) PATH '$.Type',
ord FOR ORDINALITY
)
) PKEY_ORDER
) BASE_DATA
Expected output
The string literal should remain unchanged:
Expected compiled SQL:
SELECT "BASE_DATA"."item"
FROM (
SELECT PKEY_ORDER.item
FROM SOME_TABLE S
CROSS JOIN JSON_TABLE(
S.SORTED,
'$[*]'
COLUMNS (
item VARCHAR2(200) PATH '$.PersonKey',
casePersonType VARCHAR2(200) PATH '$.Type',
ord FOR ORDINALITY
)
) PKEY_ORDER
) BASE_DATA
Analysis
'$[*]' is an Oracle JSON path expression. The * is inside a single-quoted SQL string literal and should not be
treated as an SQL identifier or be passed through identifier quoting.
It appears that the Oracle compiler applies identifier quoting to the contents of a raw SQL expression without
excluding characters inside string literals.
Suggested fix
When compiling RawFromClause or other raw SQL expressions, string literals should be preserved verbatim.
Identifier quoting should not be applied to tokens inside single-quoted literals.
A regression test covering the following input would be useful:
JSON_TABLE(S.SORTED, '$[*]' COLUMNS (...))
Temporary workaround
The application currently replaces the * inside SQL string literals with a placeholder before compilation and
restores it after compilation.
OracleCompiler incorrectly quotes
*inside JSON path literalsEnvironment
SqlKata.Compilers.OracleCompilerDescription
When using
FromRawwith an OracleJSON_TABLEexpression, the Oracle compiler incorrectly rewrites the*character inside a SQL string literal.
The JSON path:
'$[*]'is compiled as:
This produces the following Oracle error:
Minimal reproduction
Actual output
Expected output
The string literal should remain unchanged:
'$[*]'Expected compiled SQL:
Analysis
'$[*]'is an Oracle JSON path expression. The*is inside a single-quoted SQL string literal and should not betreated as an SQL identifier or be passed through identifier quoting.
It appears that the Oracle compiler applies identifier quoting to the contents of a raw SQL expression without
excluding characters inside string literals.
Suggested fix
When compiling RawFromClause or other raw SQL expressions, string literals should be preserved verbatim.
Identifier quoting should not be applied to tokens inside single-quoted literals.
A regression test covering the following input would be useful:
Temporary workaround
The application currently replaces the
*inside SQL string literals with a placeholder before compilation andrestores it after compilation.