Skip to content

Commit

Permalink
add geometry data type
Browse files Browse the repository at this point in the history
  • Loading branch information
ariesdevil committed Feb 1, 2024
1 parent 5263fff commit 1abe057
Show file tree
Hide file tree
Showing 58 changed files with 1,148 additions and 52 deletions.
92 changes: 84 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@ derive_more = "0.99.17"
enumflags2 = { version = "0.7.7", features = ["serde"] }
ethnum = { git = "https://github.com/ariesdevil/ethnum-rs", rev = "4cb05f1" }
feature-set = { version = "0.1.1" }
geo = { version = "0.27.0", features = ["use-serde"] }
geozero = { version = "0.11.0", features = ["default", "with-wkb"] }
itertools = "0.10.5"
log = { version = "0.4.19", features = ["serde", "kv_unstable_std"] }
logcall = "0.1.5"
Expand Down Expand Up @@ -259,3 +261,4 @@ sentry = { git = "https://github.com/getsentry/sentry-rust", rev = "6ef6d97" }
micromarshal = { git = "https://github.com/ariesdevil/opensrv", rev = "6c96813" }
async-backtrace = { git = "https://github.com/zhang2014/async-backtrace.git", rev = "e7e1b5f" }
opendal = { git = "https://github.com/apache/opendal", rev = "e94f068" }
geozero = { git = "https://github.com/georust/geozero", rev = "1d78b36" }
1 change: 1 addition & 0 deletions src/common/exception/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ arrow-schema = { workspace = true }
backtrace = { git = "https://github.com/rust-lang/backtrace-rs.git", rev = "6145fe6bac65c38375f1216a565a6cc7deb89a2d" }
#backtrace = "0.3.69"
bincode = { workspace = true }
geozero = { workspace = true }
http = "0.2"
opendal = { workspace = true }
parquet = { workspace = true }
Expand Down
5 changes: 4 additions & 1 deletion src/common/exception/src/exception_code.rs
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,10 @@ build_exceptions! {
// Cloud control error codes
CloudControlConnectError(1701),
CloudControlNotEnabled(1702),
IllegalCloudControlMessageFormat(1703)
IllegalCloudControlMessageFormat(1703),

// Geometry errors.
GeometryError(1801)
}

// Meta service errors [2001, 3000].
Expand Down
8 changes: 8 additions & 0 deletions src/common/exception/src/exception_into.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ use std::fmt::Display;
use std::fmt::Formatter;
use std::sync::Arc;

use geozero::error::GeozeroError;

use crate::exception::ErrorCodeBacktrace;
use crate::exception_backtrace::capture;
use crate::ErrorCode;
Expand Down Expand Up @@ -229,6 +231,12 @@ impl From<std::string::FromUtf8Error> for ErrorCode {
}
}

impl From<GeozeroError> for ErrorCode {
fn from(value: GeozeroError) -> Self {
ErrorCode::GeometryError(value.to_string())
}
}

// === prost error ===
impl From<prost::EncodeError> for ErrorCode {
fn from(error: prost::EncodeError) -> Self {
Expand Down
3 changes: 3 additions & 0 deletions src/common/io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,11 +23,14 @@ bytes = { workspace = true }
chrono = { workspace = true }
chrono-tz = { workspace = true }
ethnum = { workspace = true }
geo = { workspace = true }
geozero = { workspace = true }
lexical-core = "0.8.5"
micromarshal = "0.5.0"
ordered-float = { workspace = true }
roaring = { version = "0.10.1", features = ["serde"] }
serde = { workspace = true }
wkt = "0.10.3"

[dev-dependencies]
aho-corasick = { version = "1.0.1" }
Expand Down
40 changes: 40 additions & 0 deletions src/common/io/src/geometry.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
// Copyright 2021 Datafuse Labs
//
// Licensed 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.

use databend_common_exception::ErrorCode;
use databend_common_exception::Result;
use geo::Geometry;
use geozero::CoordDimensions;
use geozero::ToWkb;
use wkt::TryFromWkt;

pub fn parse_to_ewkb(buf: &[u8]) -> Result<Vec<u8>> {
let wkt = std::str::from_utf8(buf).map_err(|e| ErrorCode::GeometryError(e.to_string()))?;
let mut srid: Option<i32> = None;
let input_wkt = wkt.trim().to_ascii_uppercase();

let parts: Vec<&str> = input_wkt.split(';').collect();

if input_wkt.starts_with("SRID=") && parts.len() == 2 {
srid = Some(parts[0].replace("SRID=", "").parse()?);
}

let geo_part = if parts.len() == 2 { parts[1] } else { parts[0] };

let geom: Geometry<f64> = Geometry::try_from_wkt_str(geo_part)
.map_err(|e| ErrorCode::GeometryError(e.to_string()))?;

geom.to_ewkb(CoordDimensions::xy(), srid)
.map_err(ErrorCode::from)
}
2 changes: 2 additions & 0 deletions src/common/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ pub mod cursor_ext;
mod decimal;
mod escape;
mod format_settings;
mod geometry;
mod position;
mod stat_buffer;

Expand All @@ -46,3 +47,4 @@ pub use decimal::display_decimal_128;
pub use decimal::display_decimal_256;
pub use escape::escape_string;
pub use escape::escape_string_with_quote;
pub use geometry::parse_to_ewkb;
2 changes: 2 additions & 0 deletions src/meta/proto-conv/src/schema_from_to_protobuf_impl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -257,6 +257,7 @@ impl FromToProto for ex::TableDataType {
}
}
Dt24::VariantT(_) => ex::TableDataType::Variant,
Dt24::GeometryT(_) => ex::TableDataType::Geometry,
Dt24::DecimalT(x) => {
ex::TableDataType::Decimal(ex::types::decimal::DecimalDataType::from_pb(x)?)
}
Expand Down Expand Up @@ -322,6 +323,7 @@ impl FromToProto for ex::TableDataType {
new_pb_dt24(Dt24::TupleT(x))
}
TableDataType::Variant => new_pb_dt24(Dt24::VariantT(pb::Empty {})),
TableDataType::Geometry => new_pb_dt24(Dt24::GeometryT(pb::Empty {})),
};
Ok(x)
}
Expand Down
1 change: 1 addition & 0 deletions src/meta/proto-conv/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ const META_CHANGE_LOG: &[(u64, &str)] = &[
(77, "2024-01-22: Remove: allow_anonymous in S3 Config", ),
(78, "2024-01-29: Refactor: GrantEntry::UserPrivilegeType and ShareGrantEntry::ShareGrantObjectPrivilege use from_bits_truncate deserialize", ),
(79, "2024-01-31: Add: udf.proto/UserDefinedFunction add created_on field", ),
(80, "2024-02-01: Add: Add: datatype.proto/DataType Geometry type")
// Dear developer:
// If you're gonna add a new metadata version, you'll have to add a test for it.
// You could just copy an existing test file(e.g., `../tests/it/v024_table_meta.rs`)
Expand Down
1 change: 1 addition & 0 deletions src/meta/proto-conv/tests/it/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,4 @@ mod v076_role_ownership_info;
mod v077_s3_remove_allow_anonymous;
mod v078_grantentry;
mod v079_udf_created_on;
mod v080_geometry_datatype;
3 changes: 3 additions & 0 deletions src/meta/proto-conv/tests/it/proto_conv.rs
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,8 @@ fn new_table_meta() -> mt::TableMeta {
ce::TableField::new("variant_object", ce::TableDataType::Variant),
// NOTE: It is safe to convert Interval to NULL, because `Interval` is never really used.
ce::TableField::new("interval", ce::TableDataType::Null),
ce::TableField::new("bitmap", ce::TableDataType::Bitmap),
ce::TableField::new("geom", ce::TableDataType::Geometry),
],
btreemap! {s("a") => s("b")},
)),
Expand Down Expand Up @@ -256,6 +258,7 @@ pub(crate) fn new_latest_schema() -> TableSchema {
),
TableField::new("empty_map", TableDataType::EmptyMap),
TableField::new("bitmap", TableDataType::Bitmap),
TableField::new("geom", TableDataType::Geometry),
];
TableSchema::new(fields)
}
Expand Down
Loading

0 comments on commit 1abe057

Please sign in to comment.