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

feat(query): implement ST_YMAX/ST_XMIN/ST_YMIN #15594

Merged
merged 2 commits into from
May 27, 2024
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
36 changes: 22 additions & 14 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -162,7 +162,7 @@ ethnum = { version = "1.5.0" }
feature-set = { version = "0.1.1" }
geo = { version = "0.27.0", features = ["use-serde"] }
geos = { version = "8.3", features = ["static", "geo", "geo-types"] }
geozero = { version = "0.12.0", features = ["default", "with-wkb", "with-geos", "with-geojson"] }
geozero = { version = "0.13.0", features = ["default", "with-wkb", "with-geos", "with-geojson"] }
itertools = "0.10.5"
match-template = "0.0.1"
mysql_async = { version = "0.34", default-features = false, features = ["rustls-tls"] }
Expand Down
1 change: 0 additions & 1 deletion src/common/io/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,6 @@ lexical-core = "0.8.5"
micromarshal = "0.5.0"
ordered-float = { workspace = true }
roaring = { version = "0.10.1", features = ["serde"] }
scroll = "0.12.0"
serde = { workspace = true }
wkt = "0.10.3"

Expand Down
33 changes: 12 additions & 21 deletions src/common/io/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,6 @@
// limitations under the License.

use std::fmt::Display;
use std::io;
use std::io::Read;
use std::str::FromStr;

use databend_common_exception::ErrorCode;
Expand All @@ -26,14 +24,10 @@ use geozero::GeozeroGeometry;
use geozero::ToJson;
use geozero::ToWkb;
use geozero::ToWkt;
use scroll::Endian;
use scroll::IOread;
use serde::Deserialize;
use serde::Serialize;
use wkt::TryFromWkt;

const GEO_TYPE_ID_MASK: u32 = 0x2000_0000;

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Default, Serialize, Deserialize)]
pub enum GeometryDataType {
WKB,
Expand All @@ -44,6 +38,18 @@ pub enum GeometryDataType {
GEOJSON,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Axis {
X,
Y,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Extremum {
Max,
Min,
}

impl FromStr for GeometryDataType {
type Err = ErrorCode;

Expand Down Expand Up @@ -168,21 +174,6 @@ pub fn parse_to_subtype(buf: &[u8]) -> Result<GeometryDataType> {
}
}
}

pub fn read_ewkb_srid<R: Read>(raw: &mut R) -> std::result::Result<Option<i32>, io::Error> {
let byte_order = raw.ioread::<u8>()?;
let is_little_endian = byte_order != 0;
let endian = Endian::from(is_little_endian);
let type_id = raw.ioread_with::<u32>(endian)?;
let srid = if type_id & GEO_TYPE_ID_MASK == GEO_TYPE_ID_MASK {
Some(raw.ioread_with::<i32>(endian)?)
} else {
None
};

Ok(srid)
}

pub trait GeometryFormatOutput {
fn format(self, data_type: GeometryDataType) -> Result<String>;
}
Expand Down
3 changes: 2 additions & 1 deletion src/common/io/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,5 +50,6 @@ pub use escape::escape_string_with_quote;
pub use geometry::geometry_format;
pub use geometry::parse_to_ewkb;
pub use geometry::parse_to_subtype;
pub use geometry::read_ewkb_srid;
pub use geometry::Axis;
pub use geometry::Extremum;
pub use geometry::GeometryDataType;
16 changes: 8 additions & 8 deletions src/query/expression/src/utils/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,15 +15,15 @@
use std::fmt::Debug;
use std::fmt::Display;
use std::fmt::Formatter;
use std::io;

use chrono_tz::Tz;
use comfy_table::Cell;
use comfy_table::Table;
use databend_common_io::display_decimal_128;
use databend_common_io::display_decimal_256;
use databend_common_io::read_ewkb_srid;
use geozero::wkb::Ewkb;
use geozero::GeozeroGeometry;
use geozero::ToGeos;
use geozero::ToWkt;
use itertools::Itertools;
use num_traits::FromPrimitive;
Expand Down Expand Up @@ -167,9 +167,9 @@ impl<'a> Debug for ScalarRef<'a> {
Ok(())
}
ScalarRef::Geometry(s) => {
let geom = Ewkb(s.to_vec())
.to_ewkt(read_ewkb_srid(&mut io::Cursor::new(s)).unwrap())
.unwrap();
let ewkb = Ewkb(s.to_vec());
let geos = ewkb.to_geos().unwrap();
let geom = geos.to_ewkt(geos.srid()).unwrap();
write!(f, "{geom:?}")
}
}
Expand Down Expand Up @@ -258,9 +258,9 @@ impl<'a> Display for ScalarRef<'a> {
write!(f, "'{value}'")
}
ScalarRef::Geometry(s) => {
let geom = Ewkb(s.to_vec())
.to_ewkt(read_ewkb_srid(&mut io::Cursor::new(s)).unwrap())
.unwrap();
let ewkb = Ewkb(s.to_vec());
let geos = ewkb.to_geos().unwrap();
let geom = geos.to_ewkt(geos.srid()).unwrap();
write!(f, "'{geom}'")
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/query/formats/src/field_encoder/values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,6 @@
// See the License for the specific language governing permissions and
// limitations under the License.

use std::io;

use bstr::ByteSlice;
use chrono_tz::Tz;
use databend_common_arrow::arrow::bitmap::Bitmap;
Expand All @@ -35,10 +33,11 @@ use databend_common_io::constants::NAN_BYTES_LOWER;
use databend_common_io::constants::NAN_BYTES_SNAKE;
use databend_common_io::constants::NULL_BYTES_UPPER;
use databend_common_io::constants::TRUE_BYTES_NUM;
use databend_common_io::read_ewkb_srid;
use databend_common_io::GeometryDataType;
use geozero::wkb::Ewkb;
use geozero::CoordDimensions;
use geozero::GeozeroGeometry;
use geozero::ToGeos;
use geozero::ToJson;
use geozero::ToWkb;
use geozero::ToWkt;
Expand Down Expand Up @@ -318,11 +317,11 @@ impl FieldEncoderValues {
.to_vec(),
GeometryDataType::WKT => Ewkb(v.to_vec()).to_wkt().unwrap().as_bytes().to_vec(),
GeometryDataType::EWKB => hex::encode_upper(v).as_bytes().to_vec(),
GeometryDataType::EWKT => Ewkb(v.to_vec())
.to_ewkt(read_ewkb_srid(&mut io::Cursor::new(&v)).unwrap())
.unwrap()
.as_bytes()
.to_vec(),
GeometryDataType::EWKT => {
let ewkb = Ewkb(v.to_vec());
let geos = ewkb.to_geos().unwrap();
geos.to_ewkt(geos.srid()).unwrap().as_bytes().to_vec()
}
GeometryDataType::GEOJSON => Ewkb(v.to_vec()).to_json().unwrap().as_bytes().to_vec(),
};

Expand Down
Loading
Loading