Unverified 提交 c794e1e5 authored 作者: Evgenij Ryazanov's avatar Evgenij Ryazanov 提交者: GitHub

Merge pull request #1555 from Catchwa/regexp_replace-ignore-global-flag

In the REGEXP_REPLACE function, when in PostgreSQL compatibility mode, accept the 'g' flag.
...@@ -950,7 +950,12 @@ or the SQL statement <code>SET MODE PostgreSQL</code>. ...@@ -950,7 +950,12 @@ or the SQL statement <code>SET MODE PostgreSQL</code>.
</li><li>The system columns <code>CTID</code> and </li><li>The system columns <code>CTID</code> and
<code>OID</code> are supported. <code>OID</code> are supported.
</li><li>LOG(x) is base 10 in this mode. </li><li>LOG(x) is base 10 in this mode.
</li><li>REGEXP_REPLACE() uses \ for back-references. </li><li>REGEXP_REPLACE():
<ul>
<li>Uses \ for back-references</li>
<li>Will not throw an exception when the <code>flagsString</code> parameter contains a 'g'</li>
<li>In the absence of the 'g' flag in the <code>flagsString</code> parameter, only the first-matched substring will be replaced</li>
</ul>
</li><li>Fixed-width strings are padded with spaces. </li><li>Fixed-width strings are padded with spaces.
</li><li>MONEY data type is treated like NUMERIC(19, 2) data type. </li><li>MONEY data type is treated like NUMERIC(19, 2) data type.
</li><li>Datetime value functions return the same value within a transaction. </li><li>Datetime value functions return the same value within a transaction.
......
...@@ -1370,12 +1370,20 @@ public class Function extends Expression implements FunctionCall { ...@@ -1370,12 +1370,20 @@ public class Function extends Expression implements FunctionCall {
} }
String regexpMode = v3 == null || v3.getString() == null ? "" : String regexpMode = v3 == null || v3.getString() == null ? "" :
v3.getString(); v3.getString();
int flags = makeRegexpFlags(regexpMode); boolean isInPostgreSqlMode = Mode.ModeEnum.PostgreSQL.equals(database.getMode().getEnum());
int flags = makeRegexpFlags(regexpMode, isInPostgreSqlMode);
try { try {
if(isInPostgreSqlMode && !regexpMode.contains("g")) {
result = ValueString.get(
Pattern.compile(regexp, flags).matcher(v0.getString())
.replaceFirst(replacement),
database.getMode().treatEmptyStringsAsNull);
} else {
result = ValueString.get( result = ValueString.get(
Pattern.compile(regexp, flags).matcher(v0.getString()) Pattern.compile(regexp, flags).matcher(v0.getString())
.replaceAll(replacement), .replaceAll(replacement),
database.getMode().treatEmptyStringsAsNull); database.getMode().treatEmptyStringsAsNull);
}
} catch (StringIndexOutOfBoundsException e) { } catch (StringIndexOutOfBoundsException e) {
throw DbException.get( throw DbException.get(
ErrorCode.LIKE_ESCAPE_ERROR_1, e, replacement); ErrorCode.LIKE_ESCAPE_ERROR_1, e, replacement);
...@@ -1650,7 +1658,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -1650,7 +1658,7 @@ public class Function extends Expression implements FunctionCall {
String regexp = v1.getString(); String regexp = v1.getString();
String regexpMode = v2 == null || v2.getString() == null ? "" : String regexpMode = v2 == null || v2.getString() == null ? "" :
v2.getString(); v2.getString();
int flags = makeRegexpFlags(regexpMode); int flags = makeRegexpFlags(regexpMode, false);
try { try {
result = ValueBoolean.get(Pattern.compile(regexp, flags) result = ValueBoolean.get(Pattern.compile(regexp, flags)
.matcher(v0.getString()).find()); .matcher(v0.getString()).find());
...@@ -2032,7 +2040,7 @@ public class Function extends Expression implements FunctionCall { ...@@ -2032,7 +2040,7 @@ public class Function extends Expression implements FunctionCall {
return md; return md;
} }
private static int makeRegexpFlags(String stringFlags) { private static int makeRegexpFlags(String stringFlags, boolean ignoreGlobalFlag) {
int flags = Pattern.UNICODE_CASE; int flags = Pattern.UNICODE_CASE;
if (stringFlags != null) { if (stringFlags != null) {
for (int i = 0; i < stringFlags.length(); ++i) { for (int i = 0; i < stringFlags.length(); ++i) {
...@@ -2049,6 +2057,11 @@ public class Function extends Expression implements FunctionCall { ...@@ -2049,6 +2057,11 @@ public class Function extends Expression implements FunctionCall {
case 'm': case 'm':
flags |= Pattern.MULTILINE; flags |= Pattern.MULTILINE;
break; break;
case 'g':
if (ignoreGlobalFlag) {
break;
}
//$FALL-THROUGH$
default: default:
throw DbException.get(ErrorCode.INVALID_VALUE_2, stringFlags); throw DbException.get(ErrorCode.INVALID_VALUE_2, stringFlags);
} }
......
...@@ -35,3 +35,21 @@ select regexp_replace('first last', '(\w+) (\w+)', '\2 \1'); ...@@ -35,3 +35,21 @@ select regexp_replace('first last', '(\w+) (\w+)', '\2 \1');
select regexp_replace('first last', '(\w+) (\w+)', '$2 $1'); select regexp_replace('first last', '(\w+) (\w+)', '$2 $1');
>> last first >> last first
select regexp_replace('AbcDef', '[^a-z]', '', 'g');
> exception INVALID_VALUE_2
select regexp_replace('First and Second', '[A-Z]', '');
>> irst and econd
set mode PostgreSQL;
> ok
select regexp_replace('AbcDef', '[^a-z]', '', 'g');
>> bcef
select regexp_replace('AbcDef123', '[a-z]', '!', 'gi');
>> !!!!!!123
select regexp_replace('First Only', '[A-Z]', '');
>> irst Only
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论