Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 15 additions & 0 deletions src/expr/src/scalar/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2507,6 +2507,7 @@ pub enum BinaryFunc {
ConvertFrom(ConvertFrom),
Left(Left),
Position(Position),
Strpos(Strpos),
Right(Right),
RepeatString,
Normalize,
Expand Down Expand Up @@ -2789,6 +2790,7 @@ impl BinaryFunc {
BinaryFunc::Decode(s) => return s.eval(datums, temp_storage, a_expr, b_expr),
BinaryFunc::Left(s) => return s.eval(datums, temp_storage, a_expr, b_expr),
BinaryFunc::Position(s) => return s.eval(datums, temp_storage, a_expr, b_expr),
BinaryFunc::Strpos(s) => return s.eval(datums, temp_storage, a_expr, b_expr),
BinaryFunc::Right(s) => return s.eval(datums, temp_storage, a_expr, b_expr),
BinaryFunc::Trim(s) => return s.eval(datums, temp_storage, a_expr, b_expr),
BinaryFunc::TrimLeading(s) => return s.eval(datums, temp_storage, a_expr, b_expr),
Expand Down Expand Up @@ -3133,6 +3135,7 @@ impl BinaryFunc {
DigestString(s) => s.output_type(input1_type, input2_type),
DigestBytes(s) => s.output_type(input1_type, input2_type),
Position(s) => s.output_type(input1_type, input2_type),
Strpos(s) => s.output_type(input1_type, input2_type),
Encode(s) => s.output_type(input1_type, input2_type),
Decode(s) => s.output_type(input1_type, input2_type),
Power(s) => s.output_type(input1_type, input2_type),
Expand Down Expand Up @@ -3330,6 +3333,7 @@ impl BinaryFunc {
BinaryFunc::NotEq(s) => s.propagates_nulls(),
BinaryFunc::ParseIdent(s) => s.propagates_nulls(),
BinaryFunc::Position(s) => s.propagates_nulls(),
BinaryFunc::Strpos(s) => s.propagates_nulls(),
BinaryFunc::Power(s) => s.propagates_nulls(),
BinaryFunc::PowerNumeric(s) => s.propagates_nulls(),
BinaryFunc::PrettySql(s) => s.propagates_nulls(),
Expand Down Expand Up @@ -3526,6 +3530,7 @@ impl BinaryFunc {
NotEq(s) => s.introduces_nulls(),
ParseIdent(s) => s.introduces_nulls(),
Position(s) => s.introduces_nulls(),
Strpos(s) => s.introduces_nulls(),
Power(s) => s.introduces_nulls(),
PowerNumeric(s) => s.introduces_nulls(),
PrettySql(s) => s.introduces_nulls(),
Expand Down Expand Up @@ -3778,6 +3783,7 @@ impl BinaryFunc {
Normalize => false,
ParseIdent(s) => s.is_infix_op(),
Position(s) => s.is_infix_op(),
Strpos(s) => s.is_infix_op(),
Power(s) => s.is_infix_op(),
PowerNumeric(s) => s.is_infix_op(),
PrettySql(s) => s.is_infix_op(),
Expand Down Expand Up @@ -3952,6 +3958,7 @@ impl BinaryFunc {
BinaryFunc::NotEq(s) => s.negate(),
BinaryFunc::ParseIdent(s) => s.negate(),
BinaryFunc::Position(s) => s.negate(),
BinaryFunc::Strpos(s) => s.negate(),
BinaryFunc::Power(s) => s.negate(),
BinaryFunc::PowerNumeric(s) => s.negate(),
BinaryFunc::PrettySql(s) => s.negate(),
Expand Down Expand Up @@ -4181,6 +4188,7 @@ impl BinaryFunc {
BinaryFunc::ConvertFrom(s) => s.could_error(),
BinaryFunc::Left(s) => s.could_error(),
BinaryFunc::Position(s) => s.could_error(),
BinaryFunc::Strpos(s) => s.could_error(),
BinaryFunc::Right(s) => s.could_error(),
BinaryFunc::RepeatString => true,
BinaryFunc::Normalize => true,
Expand Down Expand Up @@ -4379,6 +4387,7 @@ impl BinaryFunc {
BinaryFunc::MapContainsMap(s) => s.is_monotone(),
BinaryFunc::ConvertFrom(s) => s.is_monotone(),
BinaryFunc::Position(s) => s.is_monotone(),
BinaryFunc::Strpos(s) => s.is_monotone(),
BinaryFunc::Right(s) => s.is_monotone(),
BinaryFunc::RepeatString => (false, false),
BinaryFunc::Trim(s) => s.is_monotone(),
Expand Down Expand Up @@ -4591,6 +4600,7 @@ impl fmt::Display for BinaryFunc {
BinaryFunc::ConvertFrom(s) => s.fmt(f),
BinaryFunc::Left(s) => s.fmt(f),
BinaryFunc::Position(s) => s.fmt(f),
BinaryFunc::Strpos(s) => s.fmt(f),
BinaryFunc::Right(s) => s.fmt(f),
BinaryFunc::Trim(s) => s.fmt(f),
BinaryFunc::TrimLeading(s) => s.fmt(f),
Expand Down Expand Up @@ -4943,6 +4953,11 @@ fn position(substring: &str, string: &str) -> Result<i32, EvalError> {
}
}

#[sqlfunc(propagates_nulls = true)]
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
#[sqlfunc(propagates_nulls = true)]
#[sqlfunc]

propagates_nulls is implied from arguments that cannot represent NULL.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please add a doc comment to the function (not used right now, but I hope it'll be eventually.

fn strpos(string: &str, substring: &str) -> Result<i32, EvalError> {
position(substring, string)
}

#[sqlfunc(
propagates_nulls = true,
// `left` is unfortunately not monotonic (at least for negative second arguments),
Expand Down
1 change: 1 addition & 0 deletions src/expr/src/scalar/func/binary.rs
Original file line number Diff line number Diff line change
Expand Up @@ -329,6 +329,7 @@ mod derive {
NotEq,
ParseIdent,
Position,
Strpos,
Power,
PowerNumeric,
PrettySql,
Expand Down
3 changes: 3 additions & 0 deletions src/sql/src/func.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2562,6 +2562,9 @@ pub static PG_CATALOG_BUILTINS: LazyLock<BTreeMap<&'static str, Func>> = LazyLoc
"position" => Scalar {
params!(String, String) => BinaryFunc::from(func::Position) => Int32, 849;
},
"strpos" => Scalar {
params!(String, String) => BinaryFunc::from(func::Strpos) => Int32, 868;
},
"pow" => Scalar {
params!(Float64, Float64) => Operation::nullary(|_ecx| catalog_name_only!("pow")) => Float64, 1346;
},
Expand Down
16 changes: 16 additions & 0 deletions test/sqllogictest/string.slt
Original file line number Diff line number Diff line change
Expand Up @@ -1095,6 +1095,22 @@ NULL
1
0

query I rowsort
SELECT strpos(strcol2, strcol1) FROM positiontest;
----
0
0
1
3
3
NULL
NULL

query I
SELECT strpos('high', 'ig')
----
3

# NULL inputs
query I
SELECT position(NULL IN 'str')
Expand Down