Skip to content

Commit 7007eaf

Browse files
committed
Handle null input in the presence of errors
Signed-off-by: Moritz Hoffmann <mh@materialize.com>
1 parent 4add5b3 commit 7007eaf

File tree

2 files changed

+19
-6
lines changed

2 files changed

+19
-6
lines changed

src/expr/src/scalar.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1241,10 +1241,18 @@ impl MirScalarExpr {
12411241
BinaryFunc::RegexpReplace { regex, limit },
12421242
)
12431243
}
1244-
Err(err) => MirScalarExpr::literal(
1245-
Err(err),
1246-
e.typ(column_types).scalar_type,
1247-
),
1244+
Err(err) => {
1245+
let mut exprs = mem::take(exprs);
1246+
let source = exprs.swap_remove(0);
1247+
let scalar_type = e.typ(column_types).scalar_type;
1248+
// We need to return `NULL` on `NULL` input, and error otherwise.
1249+
source
1250+
.call_is_null()
1251+
.if_then_else(
1252+
MirScalarExpr::literal_null(scalar_type.clone()),
1253+
MirScalarExpr::literal(Err(err), scalar_type),
1254+
)
1255+
}
12481256
};
12491257
} else if *func == VariadicFunc::RegexpSplitToArray
12501258
&& exprs[1].is_literal()

test/sqllogictest/regex.slt

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -717,6 +717,11 @@ Target cluster: multiprocess
717717

718718
EOF
719719

720+
query T
721+
SELECT regexp_replace(null, 'b(..', 'X', 'ig');
722+
----
723+
NULL
724+
720725
query error invalid regular expression: regex parse error:
721726
SELECT regexp_replace('foobarbaz', 'b(..', 'X', 'ig');
722727

@@ -734,8 +739,8 @@ EXPLAIN OPTIMIZED PLAN WITH (humanized expressions) AS VERBOSE TEXT FOR
734739
SELECT s, regexp_replace(s, 'b(..', replacement, 'ig') FROM e;
735740
----
736741
Explained Query:
737-
Map (error("invalid regular expression: regex parse error:\n b(..\n ^\nerror: unclosed group"))
738-
Project (#0{s})
742+
Project (#0{s}, #3)
743+
Map (case when (#0{s}) IS NULL then null else error("invalid regular expression: regex parse error:\n b(..\n ^\nerror: unclosed group") end)
739744
ReadStorage materialize.public.e
740745

741746
Source materialize.public.e

0 commit comments

Comments
 (0)