Skip to content

Commit

Permalink
Better detecting invalid figures
Browse files Browse the repository at this point in the history
  • Loading branch information
nikitamikhaylov committed Jul 18, 2023
1 parent 58c5e91 commit a06631f
Show file tree
Hide file tree
Showing 9 changed files with 42 additions and 24 deletions.
7 changes: 7 additions & 0 deletions src/Functions/geoToS2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ namespace ErrorCodes
{
extern const int ILLEGAL_TYPE_OF_ARGUMENT;
extern const int ILLEGAL_COLUMN;
extern const int BAD_ARGUMENTS;
}

namespace
Expand Down Expand Up @@ -108,6 +109,12 @@ class FunctionGeoToS2 : public IFunction

/// S2 acceptes point as (latitude, longitude)
S2LatLng lat_lng = S2LatLng::FromDegrees(lat, lon);

if (!lat_lng.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Point is invalid. For valid point the latitude is between -90 and 90 degrees inclusive"
"and the longitude is between -180 and 180 degrees inclusive.");

S2CellId id(lat_lng);

dst_data[row] = id.id();
Expand Down
15 changes: 10 additions & 5 deletions src/Functions/s2RectAdd.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,18 @@ class FunctionS2RectAdd : public IFunction
const auto hi = S2CellId(data_hi[row]);
const auto point = S2CellId(data_point[row]);

if (!lo.is_valid() || !hi.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Rectangle is not valid");
S2LatLngRect rect(lo.ToLatLng(), hi.ToLatLng());

if (!point.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Point is not valid");

S2LatLngRect rect(lo.ToLatLng(), hi.ToLatLng());
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Point is invalid. For valid point the latitude is between -90 and 90 degrees inclusive"
"and the longitude is between -180 and 180 degrees inclusive.");

if (!rect.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Rectangle is invalid. For valid rectangles the latitude bounds do not exceed"
"Pi/2 in absolute value and the longitude bounds do not exceed Pi in absolute value."
"Also, if either the latitude or longitude bound is empty then both must be.");

rect.AddPoint(point.ToPoint());

Expand Down
15 changes: 10 additions & 5 deletions src/Functions/s2RectContains.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -107,13 +107,18 @@ class FunctionS2RectContains : public IFunction
const auto hi = S2CellId(data_hi[row]);
const auto point = S2CellId(data_point[row]);

if (!lo.is_valid() || !hi.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Rectangle is not valid");
S2LatLngRect rect(lo.ToLatLng(), hi.ToLatLng());

if (!point.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Point is not valid");

S2LatLngRect rect(lo.ToLatLng(), hi.ToLatLng());
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Point is invalid. For valid point the latitude is between -90 and 90 degrees inclusive"
"and the longitude is between -180 and 180 degrees inclusive.");

if (!rect.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Rectangle is invalid. For valid rectangles the latitude bounds do not exceed"
"Pi/2 in absolute value and the longitude bounds do not exceed Pi in absolute value."
"Also, if either the latitude or longitude bound is empty then both must be.");

dst_data.emplace_back(rect.Contains(point.ToLatLng()));
}
Expand Down
12 changes: 6 additions & 6 deletions src/Functions/s2RectIntersection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -128,15 +128,15 @@ class FunctionS2RectIntersection : public IFunction
const auto lo2 = S2CellId(data_lo2[row]);
const auto hi2 = S2CellId(data_hi2[row]);

if (!lo1.is_valid() || !hi1.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "First rectangle is not valid");

if (!lo2.is_valid() || !hi2.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Second rectangle is not valid");

S2LatLngRect rect1(lo1.ToLatLng(), hi1.ToLatLng());
S2LatLngRect rect2(lo2.ToLatLng(), hi2.ToLatLng());

if (!rect1.is_valid() || !rect2.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Rectangle is invalid. For valid rectangles the latitude bounds do not exceed"
"Pi/2 in absolute value and the longitude bounds do not exceed Pi in absolute value."
"Also, if either the latitude or longitude bound is empty then both must be.");

S2LatLngRect rect_intersection = rect1.Intersection(rect2);

vec_res_first.emplace_back(S2CellId(rect_intersection.lo()).id());
Expand Down
12 changes: 6 additions & 6 deletions src/Functions/s2RectUnion.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -126,15 +126,15 @@ class FunctionS2RectUnion : public IFunction
const auto lo2 = S2CellId(data_lo2[row]);
const auto hi2 = S2CellId(data_hi2[row]);

if (!lo1.is_valid() || !hi1.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "First rectangle is not valid");

if (!lo2.is_valid() || !hi2.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Second rectangle is not valid");

S2LatLngRect rect1(lo1.ToLatLng(), hi1.ToLatLng());
S2LatLngRect rect2(lo2.ToLatLng(), hi2.ToLatLng());

if (!rect1.is_valid() || !rect2.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS,
"Rectangle is invalid. For valid rectangles the latitude bounds do not exceed"
"Pi/2 in absolute value and the longitude bounds do not exceed Pi in absolute value."
"Also, if either the latitude or longitude bound is empty then both must be.");

S2LatLngRect rect_union = rect1.Union(rect2);

vec_res_first.emplace_back(S2CellId(rect_union.lo()).id());
Expand Down
2 changes: 1 addition & 1 deletion src/Functions/s2ToGeo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ class FunctionS2ToGeo : public IFunction
const auto id = S2CellId(data_id[row]);

if (!id.is_valid())
throw Exception(ErrorCodes::BAD_ARGUMENTS, "Point is not valid");
throw Exception(ErrorCodes::BAD_ARGUMENTS, "CellId is invalid.");

S2Point point = id.ToPoint();
S2LatLng ll(point);
Expand Down
2 changes: 1 addition & 1 deletion src/Interpreters/Cache/Metadata.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ friend struct CacheMetadata;
{
{
std::lock_guard lock(mutex);
queue.emplace(file_segment->key(), file_segment->offset(), file_segment);
queue.push(DownloadInfo{file_segment->key(), file_segment->offset(), file_segment});
}

CurrentMetrics::add(CurrentMetrics::FilesystemCacheDownloadQueueElements);
Expand Down
Empty file.
1 change: 1 addition & 0 deletions tests/queries/0_stateless/02816_s2_invalid_point.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
SELECT geoToS2(toFloat64(toUInt64(-1)), toFloat64(toUInt64(-1))); -- { serverError BAD_ARGUMENTS }

0 comments on commit a06631f

Please sign in to comment.