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: Alter column fulltext option #4637

Open
wants to merge 20 commits into
base: main
Choose a base branch
from
Open
4 changes: 3 additions & 1 deletion Cargo.lock

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

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ etcd-client = { version = "0.13" }
fst = "0.4.7"
futures = "0.3"
futures-util = "0.3"
greptime-proto = { git = "https://github.com/GreptimeTeam/greptime-proto.git", rev = "c437b55725b7f5224fe9d46db21072b4a682ee4b" }
greptime-proto = { git = "https://github.com/irenjj/greptime-proto.git", rev = "ed13098712a87674e1d41be8c9a8ceb23585c009" }
humantime = "2.1"
humantime-serde = "1.1"
itertools = "0.10"
Expand Down Expand Up @@ -151,7 +151,7 @@ reqwest = { version = "0.12", default-features = false, features = [
"stream",
"multipart",
] }
# SCRAM-SHA-512 requires https://github.com/dequbed/rsasl/pull/48, https://github.com/influxdata/rskafka/pull/247
# SCRAM-SHA-512 requires https://github.com/dequbed/rsasl/pull/48, https://github.com/influxdata/rskafka/pull/247
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: No need to change this. The comments have been removed in the latest main.

rskafka = { git = "https://github.com/WenyXu/rskafka.git", rev = "940c6030012c5b746fad819fb72e3325b26e39de", features = [
"transport-tls",
] }
Expand Down
14 changes: 11 additions & 3 deletions src/api/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,15 +66,23 @@ pub enum Error {
#[snafu(implicit)]
location: Location,
},

#[snafu(display("Failed to decode proto"))]
DecodeProto {
#[snafu(implicit)]
location: Location,
#[snafu(source)]
error: prost::DecodeError,
},
}

impl ErrorExt for Error {
fn status_code(&self) -> StatusCode {
match self {
Error::UnknownColumnDataType { .. } => StatusCode::InvalidArguments,
Error::IntoColumnDataType { .. } | Error::SerializeJson { .. } => {
StatusCode::Unexpected
}
Error::IntoColumnDataType { .. }
| Error::SerializeJson { .. }
| Error::DecodeProto { .. } => StatusCode::Unexpected,
Error::ConvertColumnDefaultConstraint { source, .. }
| Error::InvalidColumnDefaultConstraint { source, .. } => source.status_code(),
}
Expand Down
34 changes: 32 additions & 2 deletions src/api/src/v1/column_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,10 @@
use std::collections::HashMap;

use datatypes::schema::{
ColumnDefaultConstraint, ColumnSchema, FulltextOptions, COMMENT_KEY, FULLTEXT_KEY,
ChangeFulltextOptions, ColumnDefaultConstraint, ColumnSchema, FulltextAnalyzer,
FulltextOptions, COMMENT_KEY, FULLTEXT_KEY,
};
use greptime_proto::v1::{Analyzer, ChangeFulltext};
use snafu::ResultExt;

use crate::error::{self, Result};
Expand Down Expand Up @@ -93,6 +95,34 @@ pub fn options_from_fulltext(fulltext: &FulltextOptions) -> Result<Option<Column
Ok((!options.options.is_empty()).then_some(options))
}

/// Tries to construct a `FulltextAnalyzer` from the given `Analyzer`.
pub fn try_as_fulltext_option(analyzer: Option<i32>) -> Result<Option<FulltextAnalyzer>> {
let analyzer = analyzer
.map(Analyzer::try_from)
.transpose()
.context(error::DecodeProtoSnafu)?;
Ok(analyzer.map(|a| match a {
Analyzer::English => FulltextAnalyzer::English,
Analyzer::Chinese => FulltextAnalyzer::Chinese,
}))
}
/// Tries to construct a `ChangeFulltextOptions` from the given `ChangeFulltext`.
pub fn try_as_change_fulltext_options(
ChangeFulltext {
enable,
analyzer,
case_sensitive,
..
}: &ChangeFulltext,
) -> Result<ChangeFulltextOptions> {
let analyzer = try_as_fulltext_option(*analyzer)?;
Ok(ChangeFulltextOptions {
enable: *enable,
analyzer,
case_sensitive: *case_sensitive,
})
}

#[cfg(test)]
mod tests {

Expand Down Expand Up @@ -148,7 +178,7 @@ mod tests {
assert!(options.is_none());

let schema = ColumnSchema::new("test", ConcreteDataType::string_datatype(), true)
.with_fulltext_options(FulltextOptions {
.with_fulltext_options(&FulltextOptions {
enable: true,
analyzer: FulltextAnalyzer::English,
case_sensitive: false,
Expand Down
9 changes: 7 additions & 2 deletions src/common/grpc-expr/src/alter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ use table::metadata::TableId;
use table::requests::{AddColumnRequest, AlterKind, AlterTableRequest, ChangeColumnTypeRequest};

use crate::error::{
InvalidColumnDefSnafu, MissingFieldSnafu, MissingTimestampColumnSnafu, Result,
UnknownLocationTypeSnafu,
DecodeProtoSnafu, InvalidColumnDefSnafu, MissingFieldSnafu, MissingTimestampColumnSnafu,
Result, UnknownLocationTypeSnafu,
};

const LOCATION_TYPE_FIRST: i32 = LocationType::First as i32;
Expand Down Expand Up @@ -92,6 +92,11 @@ pub fn alter_expr_to_request(table_id: TableId, expr: AlterExpr) -> Result<Alter
Kind::RenameTable(RenameTable { new_table_name }) => {
AlterKind::RenameTable { new_table_name }
}
Kind::ChangeFulltext(change) => AlterKind::ChangeFulltext {
column_name: change.column_name.clone(),
options: column_def::try_as_change_fulltext_options(&change)
.context(DecodeProtoSnafu)?,
},
};

let request = AlterTableRequest {
Expand Down
20 changes: 17 additions & 3 deletions src/common/grpc-expr/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -121,8 +121,21 @@ pub enum Error {
InvalidFulltextColumnType {
column_name: String,
column_type: ColumnDataType,
},

#[snafu(display("Invalid fulltext options"))]
InvalidFulltextOptions {
source: datatypes::error::Error,
#[snafu(implicit)]
location: Location,
},

#[snafu(display("Failed to decode proto"))]
DecodeProto {
#[snafu(implicit)]
location: Location,
#[snafu(source)]
error: api::error::Error,
},
}

Expand All @@ -145,9 +158,10 @@ impl ErrorExt for Error {
StatusCode::InvalidArguments
}

Error::UnknownColumnDataType { .. } | Error::InvalidFulltextColumnType { .. } => {
StatusCode::InvalidArguments
}
Error::UnknownColumnDataType { .. }
| Error::InvalidFulltextOptions { .. }
| Error::InvalidFulltextColumnType { .. }
| Error::DecodeProto { .. } => StatusCode::InvalidArguments,
}
}

Expand Down
1 change: 1 addition & 0 deletions src/common/meta/src/ddl/alter_table/region_request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,7 @@ fn create_proto_alter_kind(
})))
}
Kind::RenameTable(_) => Ok(None),
Kind::ChangeFulltext(x) => Ok(Some(alter_request::Kind::ChangeFulltext(x.clone()))),
}
}

Expand Down
1 change: 1 addition & 0 deletions src/common/meta/src/ddl/alter_table/update_metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ impl AlterTableProcedure {
new_info.name = new_table_name.to_string();
}
AlterKind::DropColumns { .. } | AlterKind::ChangeColumnTypes { .. } => {}
AlterKind::ChangeFulltext { .. } => {}
}

Ok(new_info)
Expand Down
2 changes: 2 additions & 0 deletions src/datatypes/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,5 @@ paste = "1.0"
serde.workspace = true
serde_json.workspace = true
snafu.workspace = true
sqlparser.workspace = true
sqlparser_derive = "0.1"
24 changes: 23 additions & 1 deletion src/datatypes/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -205,6 +205,26 @@ pub enum Error {
#[snafu(implicit)]
location: Location,
},

#[snafu(display(
"Invalid data type {} for fulltext in column {}",
data_type,
column_name
))]
InvalidFulltextDataType {
data_type: String,
column_name: String,
#[snafu(implicit)]
location: Location,
},

#[snafu(display("Invalid Fulltext Options, key: {}, value: {}", key, value))]
InvalidFulltextOptions {
key: String,
value: String,
#[snafu(implicit)]
location: Location,
},
}

impl ErrorExt for Error {
Expand All @@ -222,7 +242,9 @@ impl ErrorExt for Error {
| DefaultValueType { .. }
| DuplicateMeta { .. }
| InvalidTimestampPrecision { .. }
| InvalidPrecisionOrScale { .. } => StatusCode::InvalidArguments,
| InvalidPrecisionOrScale { .. }
| InvalidFulltextDataType { .. }
| InvalidFulltextOptions { .. } => StatusCode::InvalidArguments,

ValueExceedsPrecision { .. }
| CastType { .. }
Expand Down
7 changes: 5 additions & 2 deletions src/datatypes/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,15 @@ use snafu::{ensure, ResultExt};

use crate::error::{self, DuplicateColumnSnafu, Error, ProjectArrowSchemaSnafu, Result};
pub use crate::schema::column_schema::{
ColumnSchema, FulltextAnalyzer, FulltextOptions, Metadata, COMMENT_KEY, FULLTEXT_KEY,
TIME_INDEX_KEY,
ChangeFulltextOptions, ColumnSchema, FulltextAnalyzer, FulltextOptions, Metadata, COMMENT_KEY,
FULLTEXT_KEY, TIME_INDEX_KEY,
};
pub use crate::schema::constraint::ColumnDefaultConstraint;
pub use crate::schema::raw::RawSchema;

const COLUMN_FULLTEXT_OPT_KEY_ANALYZER: &str = "analyzer";
const COLUMN_FULLTEXT_OPT_KEY_CASE_SENSITIVE: &str = "case_sensitive";

/// Key used to store version number of the schema in metadata.
pub const VERSION_KEY: &str = "greptime:version";

Expand Down
Loading