Skip to content

Commit

Permalink
support geometry type for create table
Browse files Browse the repository at this point in the history
  • Loading branch information
ariesdevil committed Feb 8, 2024
1 parent 3382953 commit 0d921db
Show file tree
Hide file tree
Showing 25 changed files with 659 additions and 67 deletions.
103 changes: 80 additions & 23 deletions Cargo.lock

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

58 changes: 58 additions & 0 deletions scripts/setup/dev_setup.sh
Original file line number Diff line number Diff line change
Expand Up @@ -327,6 +327,62 @@ function install_mysql_client {
esac
}

function install_sqlite3 {
PACKAGE_MANAGER=$1

echo "==> installing sqlite3..."

case "$PACKAGE_MANAGER" in
apt-get)
install_pkg libsqlite3-dev "$PACKAGE_MANAGER"
;;
pacman)
install_pkg sqlite "$PACKAGE_MANAGER"
;;
apk)
install_pkg sqlite-dev "$PACKAGE_MANAGER"
;;
yum | dnf)
install_pkg sqlite-devel "$PACKAGE_MANAGER"
;;
brew)
install_pkg sqlite "$PACKAGE_MANAGER"
;;
*)
echo "Unable to install sqlite3 with package manager: $PACKAGE_MANAGER"
exit 1
;;
esac
}

function install_libtiff {
PACKAGE_MANAGER=$1

echo "==> installing libtiff..."

case "$PACKAGE_MANAGER" in
apt-get)
install_pkg libtiff-dev "$PACKAGE_MANAGER"
;;
pacman)
install_pkg libtiff "$PACKAGE_MANAGER"
;;
apk)
install_pkg tiff-dev "$PACKAGE_MANAGER"
;;
yum | dnf)
install_pkg libtiff-devel "$PACKAGE_MANAGER"
;;
brew)
install_pkg libtiff "$PACKAGE_MANAGER"
;;
*)
echo "Unable to install libtiff with package manager: $PACKAGE_MANAGER"
exit 1
;;
esac
}

function install_rustup {
RUST_TOOLCHAIN=$1

Expand Down Expand Up @@ -556,6 +612,8 @@ if [[ "$INSTALL_BUILD_TOOLS" == "true" ]]; then
install_pkg llvm "$PACKAGE_MANAGER"
install_ziglang "$PACKAGE_MANAGER"
install_python3 "$PACKAGE_MANAGER"
install_sqlite3 "$PACKAGE_MANAGER"
install_libtiff "$PACKAGE_MANAGER"

# Any call to cargo will make rustup install the correct toolchain
cargo version
Expand Down
15 changes: 9 additions & 6 deletions src/common/io/src/geometry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,22 +19,25 @@ use geozero::CoordDimensions;
use geozero::ToWkb;
use wkt::TryFromWkt;

pub fn parse_to_ewkb(buf: &[u8]) -> Result<Vec<u8>> {
pub fn parse_to_ewkb(buf: &[u8], srid: Option<i32>) -> 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 parsed_srid: Option<i32> = srid.or_else(|| {
if input_wkt.starts_with("SRID=") && parts.len() == 2 {
parts[0].replace("SRID=", "").parse().ok()
} else {
None
}
});

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)
geom.to_ewkb(CoordDimensions::xy(), parsed_srid)
.map_err(ErrorCode::from)
}
4 changes: 4 additions & 0 deletions src/query/ast/src/ast/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,7 @@ pub enum TypeName {
fields_type: Vec<TypeName>,
},
Variant,
Geometry,
Nullable(Box<TypeName>),
NotNull(Box<TypeName>),
}
Expand Down Expand Up @@ -899,6 +900,9 @@ impl Display for TypeName {
TypeName::Variant => {
write!(f, "VARIANT")?;
}
TypeName::Geometry => {
write!(f, "GEOMETRY")?;
}
TypeName::Nullable(ty) => {
write!(f, "{} NULL", ty)?;
}
Expand Down
2 changes: 2 additions & 0 deletions src/query/ast/src/parser/expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1586,6 +1586,7 @@ pub fn type_name(i: Input) -> IResult<TypeName> {
rule! { ( STRING | VARCHAR | CHAR | CHARACTER | TEXT ) ~ ( "(" ~ ^#literal_u64 ~ ^")" )? },
);
let ty_variant = value(TypeName::Variant, rule! { VARIANT | JSON });
let ty_geometry = value(TypeName::Geometry, rule! { GEOMETRY });
map_res(
alt((
rule! {
Expand Down Expand Up @@ -1614,6 +1615,7 @@ pub fn type_name(i: Input) -> IResult<TypeName> {
| #ty_binary
| #ty_string
| #ty_variant
| #ty_geometry
| #ty_nullable
) ~ #nullable? : "type name" },
)),
Expand Down
2 changes: 2 additions & 0 deletions src/query/ast/src/parser/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -597,6 +597,8 @@ pub enum TokenKind {
FUSE,
#[token("GENERATED", ignore(ascii_case))]
GENERATED,
#[token("GEOMETRY", ignore(ascii_case))]
GEOMETRY,
#[token("GLOBAL", ignore(ascii_case))]
GLOBAL,
#[token("GRAPH", ignore(ascii_case))]
Expand Down
2 changes: 1 addition & 1 deletion src/query/formats/src/field_decoder/fast_values.rs
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ impl FastFieldDecoderValues {
) -> Result<()> {
let mut buf = Vec::new();
self.read_string_inner(reader, &mut buf, positions)?;
let geom = parse_to_ewkb(&buf)?;
let geom = parse_to_ewkb(&buf, None)?;
column.put_slice(geom.as_bytes());
column.commit_row();
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/query/formats/src/field_decoder/json_ast.rs
Original file line number Diff line number Diff line change
Expand Up @@ -330,7 +330,7 @@ impl FieldJsonAstDecoder {
fn read_geometry(&self, column: &mut BinaryColumnBuilder, value: &Value) -> Result<()> {
match value {
Value::String(v) => {
let geom = parse_to_ewkb(v.as_bytes())?;
let geom = parse_to_ewkb(v.as_bytes(), None)?;
column.put_slice(&geom);
column.commit_row();
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/query/formats/src/field_decoder/nested.rs
Original file line number Diff line number Diff line change
Expand Up @@ -327,7 +327,7 @@ impl NestedValues {
) -> Result<()> {
let mut buf = Vec::new();
self.read_string_inner(reader, &mut buf)?;
let geom = parse_to_ewkb(&buf)?;
let geom = parse_to_ewkb(&buf, None)?;
column.put_slice(geom.as_bytes());
column.commit_row();
Ok(())
Expand Down
2 changes: 1 addition & 1 deletion src/query/formats/src/field_decoder/separated_text.rs
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,7 @@ impl SeparatedTextDecoder {
}

fn read_geometry(&self, column: &mut BinaryColumnBuilder, data: &[u8]) -> Result<()> {
let geom = parse_to_ewkb(data)?;
let geom = parse_to_ewkb(data, None)?;
column.put_slice(geom.as_bytes());
column.commit_row();
Ok(())
Expand Down
Loading

0 comments on commit 0d921db

Please sign in to comment.