Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: fix point_in_polygon function panic if a tuple has only one field #12948

Merged
merged 1 commit into from
Sep 21, 2023
Merged
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
55 changes: 41 additions & 14 deletions src/query/functions/src/scalars/geo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,8 @@ pub fn register(registry: &mut FunctionRegistry) {

// point in ellipses
registry.register_function_factory("point_in_ellipses", |_, args_type| {
if args_type.len() < 6 {
// The input parameters must be 2+4*n, where n is the number of ellipses.
if args_type.len() < 6 || (args_type.len() - 2) % 4 != 0 {
return None;
}
Some(Arc::new(Function {
Expand All @@ -221,20 +222,28 @@ pub fn register(registry: &mut FunctionRegistry) {
return None;
}

let (arg1, arg2) = if args_type.len() == 2 {
let (arg1, arg2) = {
let arg1 = match args_type.get(0)? {
DataType::Tuple(tys) => vec![DataType::Number(NumberDataType::Float64); tys.len()],
DataType::Tuple(tys) => {
if tys.len() == 2 {
vec![DataType::Number(NumberDataType::Float64); tys.len()]
} else {
return None;
}
}
_ => return None,
};
let arg2 = match args_type.get(1)? {
DataType::Array(box DataType::Tuple(tys)) => {
vec![DataType::Number(NumberDataType::Float64); tys.len()]
if tys.len() == 2 {
vec![DataType::Number(NumberDataType::Float64); tys.len()]
} else {
return None;
}
}
_ => return None,
};
(arg1, arg2)
} else {
(vec![], vec![])
};

Some(Arc::new(Function {
Expand All @@ -260,20 +269,28 @@ pub fn register(registry: &mut FunctionRegistry) {
return None;
}

let (arg1, arg2) = if args_type.len() == 2 {
let (arg1, arg2) = {
let arg1 = match args_type.get(0)? {
DataType::Tuple(tys) => vec![DataType::Number(NumberDataType::Float64); tys.len()],
DataType::Tuple(tys) => {
if tys.len() == 2 {
vec![DataType::Number(NumberDataType::Float64); tys.len()]
} else {
return None;
}
}
_ => return None,
};
let arg2 = match args_type.get(1)? {
DataType::Array(box DataType::Array(box DataType::Tuple(tys))) => {
vec![DataType::Number(NumberDataType::Float64); tys.len()]
if tys.len() == 2 {
vec![DataType::Number(NumberDataType::Float64); tys.len()]
} else {
return None;
}
}
_ => return None,
};
(arg1, arg2)
} else {
(vec![], vec![])
};

Some(Arc::new(Function {
Expand Down Expand Up @@ -302,20 +319,30 @@ pub fn register(registry: &mut FunctionRegistry) {
let mut args = vec![];

let arg1 = match args_type.get(0)? {
DataType::Tuple(tys) => vec![DataType::Number(NumberDataType::Float64); tys.len()],
DataType::Tuple(tys) => {
if tys.len() == 2 {
vec![DataType::Number(NumberDataType::Float64); tys.len()]
} else {
return None;
}
}
_ => return None,
};
args.push(DataType::Tuple(arg1));

let arg2: Vec<DataType> = match args_type.get(1)? {
DataType::Array(box DataType::Tuple(tys)) => {
vec![DataType::Number(NumberDataType::Float64); tys.len()]
if tys.len() == 2 {
vec![DataType::Number(NumberDataType::Float64); tys.len()]
} else {
return None;
}
}

_ => return None,
};

(0..args_type.len() - 1)
(1..args_type.len())
.for_each(|_| args.push(DataType::Array(Box::new(DataType::Tuple(arg2.clone())))));

Some(Arc::new(Function {
Expand Down
22 changes: 22 additions & 0 deletions tests/sqllogictests/suites/query/02_function/02_0060_function_geo
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,33 @@ select geohash_encode(-5.60302734375, 42.593994140625)
----
ezs42d000000

# form 1
query T
select point_in_polygon((3., 3.), [(6, 0), (8, 4), (5, 8), (0, 2)])
----
1

# form 2
query T
select point_in_polygon((1., 1.), [[(4., 0.), (8., 4.), (4., 8.), (0., 4.)], [(3., 3.), (3., 5.), (5., 5.), (5., 3.)]])
----
0

# form 3
query T
select point_in_polygon((2.5, 2.5), [(4., 0.), (8., 4.), (4., 8.), (0., 4.)], [(3., 3.), (3., 5.), (5., 5.), (5., 3.)])
----
1

statement error 1065
select point_in_polygon((3,), [(6, 0), (8, 4)])
ariesdevil marked this conversation as resolved.
Show resolved Hide resolved

statement error 1065
select point_in_polygon((1.,), [[(4., 0.), (8., 4.), (4., 8.), (0., 4.)], [(3., 3.), (3., 5.), (5., 5.), (5., 3.)]])

statement error 1065
select point_in_polygon((2.5,), [(4., 0.), (8., 4.), (4., 8.), (0., 4.)], [(3., 3.), (3., 5.), (5., 5.), (5., 3.)])

query T
select great_circle_angle(-2181569507.9714413, 15253014773.129665, 0.5823419941455749, 0.5823419941455749)
----
Expand Down