Skip to content

Commit

Permalink
feat: issue #8969 adding position function (#8988)
Browse files Browse the repository at this point in the history
* feat: issue #8969 adding position function

* reuse Instr
  • Loading branch information
Lordworms committed Feb 2, 2024
1 parent f203d86 commit 7641a32
Show file tree
Hide file tree
Showing 4 changed files with 75 additions and 3 deletions.
2 changes: 1 addition & 1 deletion datafusion/expr/src/built_in_function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1463,7 +1463,7 @@ impl BuiltinScalarFunction {
BuiltinScalarFunction::Chr => &["chr"],
BuiltinScalarFunction::EndsWith => &["ends_with"],
BuiltinScalarFunction::InitCap => &["initcap"],
BuiltinScalarFunction::InStr => &["instr"],
BuiltinScalarFunction::InStr => &["instr", "position"],
BuiltinScalarFunction::Left => &["left"],
BuiltinScalarFunction::Lower => &["lower"],
BuiltinScalarFunction::Lpad => &["lpad"],
Expand Down
19 changes: 17 additions & 2 deletions datafusion/sql/src/expr/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -514,7 +514,9 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
SQLExpr::Struct { values, fields } => {
self.parse_struct(values, fields, schema, planner_context)
}

SQLExpr::Position { expr, r#in } => {
self.sql_position_to_expr(*expr, *r#in, schema, planner_context)
}
_ => not_impl_err!("Unsupported ast node in sqltorel: {sql:?}"),
}
}
Expand Down Expand Up @@ -704,7 +706,20 @@ impl<'a, S: ContextProvider> SqlToRel<'a, S> {
};
Ok(Expr::ScalarFunction(ScalarFunction::new(fun, args)))
}

fn sql_position_to_expr(
&self,
substr_expr: SQLExpr,
str_expr: SQLExpr,
schema: &DFSchema,
planner_context: &mut PlannerContext,
) -> Result<Expr> {
let fun = BuiltinScalarFunction::InStr;
let substr =
self.sql_expr_to_logical_expr(substr_expr, schema, planner_context)?;
let fullstr = self.sql_expr_to_logical_expr(str_expr, schema, planner_context)?;
let args = vec![fullstr, substr];
Ok(Expr::ScalarFunction(ScalarFunction::new(fun, args)))
}
fn sql_agg_with_filter_to_expr(
&self,
expr: SQLExpr,
Expand Down
43 changes: 43 additions & 0 deletions datafusion/sqllogictest/test_files/position.slt
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
# Licensed to the Apache Software Foundation (ASF) under one
# or more contributor license agreements. See the NOTICE file
# distributed with this work for additional information
# regarding copyright ownership. The ASF licenses this file
# to you under the Apache License, Version 2.0 (the
# "License"); you may not use this file except in compliance
# with the License. You may obtain a copy of the License at

# http://www.apache.org/licenses/LICENSE-2.0

# Unless required by applicable law or agreed to in writing,
# software distributed under the License is distributed on an
# "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
# KIND, either express or implied. See the License for the
# specific language governing permissions and limitations
# under the License.

# test position in select
query I
select position('world' in 'hello world');
----
7



# test in expression
query I
select 1000 where position('world' in 'hello world') != 100;
----
1000


# test in expression
query I
select 100000 where position('legend' in 'league of legend') = 11;
----
100000


# test in expression
query I
select 100000 where position('legend' in 'league of legend') != 11;
----
14 changes: 14 additions & 0 deletions docs/source/user-guide/sql/scalar_functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -641,6 +641,7 @@ nullif(expression1, expression2)
- [levenshtein](#levenshtein)
- [substr_index](#substr_index)
- [find_in_set](#find_in_set)
- [position](#position)

### `ascii`

Expand Down Expand Up @@ -1300,6 +1301,19 @@ regexp_replace(str, regexp, replacement, flags)
- **g**: (global) Search globally and don't return after the first match.
- **i**: (insensitive) Ignore case when matching.

### `position`

Returns the position of substr in orig_str

```
position(substr in origstr)
```

#### Arguments

- **substr**: he pattern string.
- **origstr**: The model string.

## Time and Date Functions

- [now](#now)
Expand Down

0 comments on commit 7641a32

Please sign in to comment.