Skip to content

Commit

Permalink
format
Browse files Browse the repository at this point in the history
  • Loading branch information
Maxxen committed Nov 7, 2023
1 parent fca445c commit d0d35f9
Show file tree
Hide file tree
Showing 9 changed files with 83 additions and 87 deletions.
30 changes: 14 additions & 16 deletions spatial/src/spatial/core/functions/scalar/st_dump.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ static void DumpFunction(DataChunk &args, ExpressionState &state, Vector &result
idx_t total_geom_count = 0;
idx_t total_path_count = 0;

for(idx_t out_row_idx = 0; out_row_idx < count; out_row_idx++) {
for (idx_t out_row_idx = 0; out_row_idx < count; out_row_idx++) {
auto in_row_idx = geom_format.sel->get_index(out_row_idx);

if (!geom_format.validity.RowIsValid(in_row_idx)) {
Expand All @@ -39,38 +39,35 @@ static void DumpFunction(DataChunk &args, ExpressionState &state, Vector &result

stack.emplace_back(geometry, vector<int32_t>());

while(!stack.empty()) {
while (!stack.empty()) {
auto current = stack.back();
auto current_geom = std::get<0>(current);
auto current_path = std::get<1>(current);

stack.pop_back();

if(current_geom.Type() == GeometryType::MULTIPOINT) {
if (current_geom.Type() == GeometryType::MULTIPOINT) {
auto mpoint = current_geom.GetMultiPoint();
for (int32_t i = 0; i < mpoint.Count(); i++) {
auto path = current_path;
path.push_back(i + 1); // path is 1-indexed
stack.emplace_back(mpoint[i], path);
}
}
else if(current_geom.Type() == GeometryType::MULTILINESTRING) {
} else if (current_geom.Type() == GeometryType::MULTILINESTRING) {
auto mline = current_geom.GetMultiLineString();
for (int32_t i = 0; i < mline.Count(); i++) {
auto path = current_path;
path.push_back(i + 1);
stack.emplace_back(mline[i], path);
}
}
else if(current_geom.Type() == GeometryType::MULTIPOLYGON) {
} else if (current_geom.Type() == GeometryType::MULTIPOLYGON) {
auto mpoly = current_geom.GetMultiPolygon();
for (int32_t i = 0; i < mpoly.Count(); i++) {
auto path = current_path;
path.push_back(i + 1);
stack.emplace_back(mpoly[i], path);
}
}
else if (current_geom.Type() == GeometryType::GEOMETRYCOLLECTION) {
} else if (current_geom.Type() == GeometryType::GEOMETRYCOLLECTION) {
auto collection = current_geom.GetGeometryCollection();
for (int32_t i = 0; i < collection.Count(); i++) {
auto path = current_path;
Expand Down Expand Up @@ -105,7 +102,7 @@ static void DumpFunction(DataChunk &args, ExpressionState &state, Vector &result
auto &result_path_vec = result_list_children[1];

auto geom_data = FlatVector::GetData<string_t>(*result_geom_vec);
for(idx_t i = 0; i < geom_length; i++) {
for (idx_t i = 0; i < geom_length; i++) {
// Write the geometry
auto &item_blob = std::get<0>(items[i]);
geom_data[geom_offset + i] = lstate.factory.Serialize(*result_geom_vec, item_blob);
Expand All @@ -128,24 +125,25 @@ static void DumpFunction(DataChunk &args, ExpressionState &state, Vector &result
auto &path_data_vec = ListVector::GetEntry(*result_path_vec);
auto path_data = FlatVector::GetData<int32_t>(path_data_vec);

for(idx_t j = 0; j < path_length; j++) {
for (idx_t j = 0; j < path_length; j++) {
path_data[path_offset + j] = path[j];
}
}
}

if(count == 1) {
if (count == 1) {
result.SetVectorType(VectorType::CONSTANT_VECTOR);
}
}

void CoreScalarFunctions::RegisterStDump(DatabaseInstance &db) {
ScalarFunctionSet set("ST_Dump");

set.AddFunction(ScalarFunction({GeoTypes::GEOMETRY()},
LogicalType::LIST(LogicalType::STRUCT({{"geom", GeoTypes::GEOMETRY()}, {"path", LogicalType::LIST(LogicalType::INTEGER)}})),
DumpFunction,
nullptr, nullptr, nullptr, GeometryFunctionLocalState::Init));
set.AddFunction(
ScalarFunction({GeoTypes::GEOMETRY()},
LogicalType::LIST(LogicalType::STRUCT(
{{"geom", GeoTypes::GEOMETRY()}, {"path", LogicalType::LIST(LogicalType::INTEGER)}})),
DumpFunction, nullptr, nullptr, nullptr, GeometryFunctionLocalState::Init));

ExtensionUtil::RegisterFunction(db, set);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ static void MakePolygonFromRingsFunction(DataChunk &args, ExpressionState &state
auto &new_ring = rings[ring_idx];
auto &poly_ring = polygon.Ring(ring_idx);

for(auto i = 0; i < new_ring.Vertices().Count(); i++) {
for (auto i = 0; i < new_ring.Vertices().Count(); i++) {
poly_ring.Add(new_ring.Vertices().Get(i));
}
}
Expand Down
2 changes: 1 addition & 1 deletion spatial/src/spatial/core/geometry/wkb_writer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ void WKBWriter::Write(const LineString &line, data_ptr_t &ptr) {

auto num_points = line.Count();
WriteInt(num_points, ptr);
for(uint32_t i = 0; i < line.Vertices().Count(); i++) {
for (uint32_t i = 0; i < line.Vertices().Count(); i++) {
auto vertex = line.Vertices().Get(i);
WriteDouble(vertex.x, ptr);
WriteDouble(vertex.y, ptr);
Expand Down
77 changes: 36 additions & 41 deletions spatial/src/spatial/core/optimizer_rules.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,35 +44,32 @@ class RangeJoinSpatialPredicateRewriter : public OptimizerExtension {
join->conditions.push_back(std::move(cmp));
}


static bool IsTableRefsDisjoint(unordered_set<idx_t> &left_table_indexes,
unordered_set<idx_t> &right_table_indexes,
unordered_set<idx_t> &left_bindings,
unordered_set<idx_t> &right_bindings) {
static bool IsTableRefsDisjoint(unordered_set<idx_t> &left_table_indexes, unordered_set<idx_t> &right_table_indexes,
unordered_set<idx_t> &left_bindings, unordered_set<idx_t> &right_bindings) {

// Check that all the left-side bindings reference the left-side tables of the join,
// as well as that all the right-side bindings reference the right-side tables of the join.
// and that the left and right side bindings are disjoint.

for(auto &left_binding : left_bindings) {
if(right_bindings.find(left_binding) != right_bindings.end()) {
for (auto &left_binding : left_bindings) {
if (right_bindings.find(left_binding) != right_bindings.end()) {
// The left side bindings reference the right side tables of the join.
return false;
}
// Also check that the left side bindings are on the left side of the join
if(left_table_indexes.find(left_binding) == left_table_indexes.end()) {
if (left_table_indexes.find(left_binding) == left_table_indexes.end()) {
// The left side bindings are not on the left side of the join.
return false;
}
}

for(auto &right_binding : right_bindings) {
if(left_bindings.find(right_binding) != left_bindings.end()) {
for (auto &right_binding : right_bindings) {
if (left_bindings.find(right_binding) != left_bindings.end()) {
// The right side bindings reference the left side tables of the join.
return false;
}
// Also check that the right side bindings are on the right side of the join
if(right_table_indexes.find(right_binding) == right_table_indexes.end()) {
if (right_table_indexes.find(right_binding) == right_table_indexes.end()) {
// The right side bindings are not on the right side of the join.
return false;
}
Expand Down Expand Up @@ -128,14 +125,16 @@ class RangeJoinSpatialPredicateRewriter : public OptimizerExtension {
// a JOIN b ON st_intersects(a.geom, b.geom) => OK
// a JOIN b ON st_intersects(b.geom, a.geom) => OK
// a JOIN b ON st_intersects(a.geom, st_union(a.geom, b.geom)) => NOT OK
auto can_split = IsTableRefsDisjoint(left_table_indexes, right_table_indexes, left_pred_bindings, right_pred_bindings);
if(!can_split) {
auto can_split = IsTableRefsDisjoint(left_table_indexes, right_table_indexes, left_pred_bindings,
right_pred_bindings);
if (!can_split) {
// Try again with the left and right side of the predicate swapped
// We can safely swap because the intersection operation we encode with the comparison join
// is symmetric, so the order of the arguments wont matter in the "new" join condition we're
// about to create.
can_split = IsTableRefsDisjoint(left_table_indexes, right_table_indexes, right_pred_bindings, left_pred_bindings);
if(!can_split) {
can_split = IsTableRefsDisjoint(left_table_indexes, right_table_indexes, right_pred_bindings,
left_pred_bindings);
if (!can_split) {
// We cant optimize this join
return;
}
Expand All @@ -145,35 +144,31 @@ class RangeJoinSpatialPredicateRewriter : public OptimizerExtension {

// Lookup the st_xmin, st_xmax, st_ymin, st_ymax functions in the catalog
auto &catalog = Catalog::GetSystemCatalog(context);
auto &xmin_func_set = catalog.GetEntry(context, CatalogType::SCALAR_FUNCTION_ENTRY, DEFAULT_SCHEMA, "st_xmin")
.Cast<ScalarFunctionCatalogEntry>();
auto &xmax_func_set = catalog.GetEntry(context, CatalogType::SCALAR_FUNCTION_ENTRY, DEFAULT_SCHEMA, "st_xmax")
.Cast<ScalarFunctionCatalogEntry>();
auto &ymin_func_set = catalog.GetEntry(context, CatalogType::SCALAR_FUNCTION_ENTRY, DEFAULT_SCHEMA, "st_ymin")
.Cast<ScalarFunctionCatalogEntry>();
auto &ymax_func_set = catalog.GetEntry(context, CatalogType::SCALAR_FUNCTION_ENTRY, DEFAULT_SCHEMA, "st_ymax")
.Cast<ScalarFunctionCatalogEntry>();
auto &xmin_func_set =
catalog.GetEntry(context, CatalogType::SCALAR_FUNCTION_ENTRY, DEFAULT_SCHEMA, "st_xmin")
.Cast<ScalarFunctionCatalogEntry>();
auto &xmax_func_set =
catalog.GetEntry(context, CatalogType::SCALAR_FUNCTION_ENTRY, DEFAULT_SCHEMA, "st_xmax")
.Cast<ScalarFunctionCatalogEntry>();
auto &ymin_func_set =
catalog.GetEntry(context, CatalogType::SCALAR_FUNCTION_ENTRY, DEFAULT_SCHEMA, "st_ymin")
.Cast<ScalarFunctionCatalogEntry>();
auto &ymax_func_set =
catalog.GetEntry(context, CatalogType::SCALAR_FUNCTION_ENTRY, DEFAULT_SCHEMA, "st_ymax")
.Cast<ScalarFunctionCatalogEntry>();

auto &left_arg_type = left_pred_expr->return_type;
auto &right_arg_type = right_pred_expr->return_type;

auto xmin_func_left =
xmin_func_set.functions.GetFunctionByArguments(context, {left_arg_type});
auto xmax_func_left =
xmax_func_set.functions.GetFunctionByArguments(context, {left_arg_type});
auto ymin_func_left =
ymin_func_set.functions.GetFunctionByArguments(context, {left_arg_type});
auto ymax_func_left =
ymax_func_set.functions.GetFunctionByArguments(context, {left_arg_type});

auto xmin_func_right =
xmin_func_set.functions.GetFunctionByArguments(context, {right_arg_type});
auto xmax_func_right =
xmax_func_set.functions.GetFunctionByArguments(context, {right_arg_type});
auto ymin_func_right =
ymin_func_set.functions.GetFunctionByArguments(context, {right_arg_type});
auto ymax_func_right =
ymax_func_set.functions.GetFunctionByArguments(context, {right_arg_type});
auto xmin_func_left = xmin_func_set.functions.GetFunctionByArguments(context, {left_arg_type});
auto xmax_func_left = xmax_func_set.functions.GetFunctionByArguments(context, {left_arg_type});
auto ymin_func_left = ymin_func_set.functions.GetFunctionByArguments(context, {left_arg_type});
auto ymax_func_left = ymax_func_set.functions.GetFunctionByArguments(context, {left_arg_type});

auto xmin_func_right = xmin_func_set.functions.GetFunctionByArguments(context, {right_arg_type});
auto xmax_func_right = xmax_func_set.functions.GetFunctionByArguments(context, {right_arg_type});
auto ymin_func_right = ymin_func_set.functions.GetFunctionByArguments(context, {right_arg_type});
auto ymax_func_right = ymax_func_set.functions.GetFunctionByArguments(context, {right_arg_type});

// Create the new join condition

Expand Down Expand Up @@ -231,7 +226,7 @@ class RangeJoinSpatialPredicateRewriter : public OptimizerExtension {
ExpressionType::COMPARE_GREATERTHANOREQUALTO);

new_join->children = std::move(any_join.children);
if(any_join.has_estimated_cardinality) {
if (any_join.has_estimated_cardinality) {
new_join->estimated_cardinality = any_join.estimated_cardinality;
new_join->has_estimated_cardinality = true;
}
Expand Down
1 change: 0 additions & 1 deletion spatial/src/spatial/gdal/file_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,6 @@ static int DuckDBStatCallback(void *userData, const char *filename, VSIStatBufL
throw IOException("Unknown file type");
}


/* DuckDB doesnt have anything equivalent to these yet... Hopefully thats ok?
pstatbuf->st_mtime = file->GetLastModifiedTime();
pstatbuf->st_uid = file->GetFileOwner();
Expand Down
4 changes: 2 additions & 2 deletions spatial/src/spatial/gdal/functions/st_write.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,10 @@ struct GlobalState : public GlobalFunctionData {
//===--------------------------------------------------------------------===//
// The parameters are const in duckdb > 0.9.1, ifdef so we can build for both versions for now.
#if DUCKDB_PATCH_VERSION == 1
static unique_ptr<FunctionData> Bind(ClientContext &context, CopyInfo &info, vector<string> &names,
static unique_ptr<FunctionData> Bind(ClientContext &context, CopyInfo &info, vector<string> &names,
vector<LogicalType> &sql_types) {
#else
static unique_ptr<FunctionData> Bind(ClientContext &context, const CopyInfo &info, const vector<string> &names,
static unique_ptr<FunctionData> Bind(ClientContext &context, const CopyInfo &info, const vector<string> &names,
const vector<LogicalType> &sql_types) {
#endif
GdalFileHandler::SetLocalClientContext(context);
Expand Down
3 changes: 2 additions & 1 deletion spatial/src/spatial/geos/functions/cast.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,8 @@ void GeosCastFunctions::Register(DatabaseInstance &db) {
ExtensionUtil::RegisterCastFunction(db, core::GeoTypes::GEOMETRY(), LogicalType::VARCHAR,
BoundCastInfo(GeometryToTextCast, nullptr, GEOSFunctionLocalState::InitCast));

ExtensionUtil::RegisterCastFunction(db, LogicalType::VARCHAR, core::GeoTypes::GEOMETRY(), BoundCastInfo(TextToGeometryCast, nullptr, GEOSFunctionLocalState::InitCast));
ExtensionUtil::RegisterCastFunction(db, LogicalType::VARCHAR, core::GeoTypes::GEOMETRY(),
BoundCastInfo(TextToGeometryCast, nullptr, GEOSFunctionLocalState::InitCast));
};

} // namespace geos
Expand Down
29 changes: 14 additions & 15 deletions spatial/src/spatial/geos/functions/scalar/st_geomfromtext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,20 @@ static void GeometryFromWKTFunction(DataChunk &args, ExpressionState &state, Vec

UnaryExecutor::ExecuteWithNulls<string_t, string_t>(
input, result, count, [&](string_t &wkt, ValidityMask &mask, idx_t idx) {
try {
auto geos_geom = reader.Read(wkt);
auto multidimensional = (GEOSHasZ_r(lstate.ctx.GetCtx(), geos_geom.get()) == 1);
if (multidimensional) {
throw InvalidInputException("3D/4D geometries are not supported");
}
return lstate.ctx.Serialize(result, geos_geom);
}
catch (InvalidInputException &error) {
if(!info.ignore_invalid) {
throw;
}
mask.SetInvalid(idx);
return string_t();
}
try {
auto geos_geom = reader.Read(wkt);
auto multidimensional = (GEOSHasZ_r(lstate.ctx.GetCtx(), geos_geom.get()) == 1);
if (multidimensional) {
throw InvalidInputException("3D/4D geometries are not supported");
}
return lstate.ctx.Serialize(result, geos_geom);
} catch (InvalidInputException &error) {
if (!info.ignore_invalid) {
throw;
}
mask.SetInvalid(idx);
return string_t();
}
});
}

Expand Down
22 changes: 13 additions & 9 deletions spatial/src/spatial/geos/functions/scalar/st_linemerge.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -27,21 +27,25 @@ static void LineMergeFunction(DataChunk &args, ExpressionState &state, Vector &r
static void LineMergeFunctionWithDirected(DataChunk &args, ExpressionState &state, Vector &result) {
auto &lstate = GEOSFunctionLocalState::ResetAndGet(state);
auto &ctx = lstate.ctx.GetCtx();
BinaryExecutor::Execute<string_t, bool, string_t>(args.data[0], args.data[1], result, args.size(), [&](string_t &geometry_blob, bool directed) {
auto geometry = lstate.ctx.Deserialize(geometry_blob);
auto convex_hull_geometry = directed ? make_uniq_geos(ctx, GEOSLineMergeDirected_r(ctx, geometry.get()))
: make_uniq_geos(ctx, GEOSLineMerge_r(ctx, geometry.get()));

return lstate.ctx.Serialize(result, convex_hull_geometry);
});
BinaryExecutor::Execute<string_t, bool, string_t>(
args.data[0], args.data[1], result, args.size(), [&](string_t &geometry_blob, bool directed) {
auto geometry = lstate.ctx.Deserialize(geometry_blob);
auto convex_hull_geometry = directed ? make_uniq_geos(ctx, GEOSLineMergeDirected_r(ctx, geometry.get()))
: make_uniq_geos(ctx, GEOSLineMerge_r(ctx, geometry.get()));

return lstate.ctx.Serialize(result, convex_hull_geometry);
});
}

void GEOSScalarFunctions::RegisterStLineMerge(DatabaseInstance &db) {

ScalarFunctionSet set("ST_LineMerge");

set.AddFunction(ScalarFunction({GeoTypes::GEOMETRY()}, GeoTypes::GEOMETRY(), LineMergeFunction, nullptr, nullptr, nullptr, GEOSFunctionLocalState::Init));
set.AddFunction(ScalarFunction({GeoTypes::GEOMETRY(), LogicalType::BOOLEAN}, GeoTypes::GEOMETRY(), LineMergeFunctionWithDirected, nullptr, nullptr, nullptr, GEOSFunctionLocalState::Init));
set.AddFunction(ScalarFunction({GeoTypes::GEOMETRY()}, GeoTypes::GEOMETRY(), LineMergeFunction, nullptr, nullptr,
nullptr, GEOSFunctionLocalState::Init));
set.AddFunction(ScalarFunction({GeoTypes::GEOMETRY(), LogicalType::BOOLEAN}, GeoTypes::GEOMETRY(),
LineMergeFunctionWithDirected, nullptr, nullptr, nullptr,
GEOSFunctionLocalState::Init));

ExtensionUtil::RegisterFunction(db, set);
}
Expand Down

0 comments on commit d0d35f9

Please sign in to comment.