Skip to content

Commit

Permalink
change keyword to SKIPPING INDEX
Browse files Browse the repository at this point in the history
Signed-off-by: Ruihang Xia <[email protected]>
  • Loading branch information
waynexia committed Dec 16, 2024
1 parent 1bb4203 commit 8315e74
Show file tree
Hide file tree
Showing 13 changed files with 108 additions and 94 deletions.
12 changes: 6 additions & 6 deletions src/api/src/v1/column_def.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ use std::collections::HashMap;

use datatypes::schema::{
ColumnDefaultConstraint, ColumnSchema, FulltextAnalyzer, FulltextOptions, COMMENT_KEY,
FULLTEXT_KEY, INVERTED_INDEX_KEY, SKIP_KEY,
FULLTEXT_KEY, INVERTED_INDEX_KEY, SKIPPING_INDEX_KEY,
};
use greptime_proto::v1::Analyzer;
use snafu::ResultExt;
Expand All @@ -30,7 +30,7 @@ const FULLTEXT_GRPC_KEY: &str = "fulltext";
/// Key used to store inverted index options in gRPC column options.
const INVERTED_INDEX_GRPC_KEY: &str = "inverted_index";
/// Key used to store skip index options in gRPC column options.
const SKIP_INDEX_GRPC_KEY: &str = "skip_index";
const SKIPPING_INDEX_GRPC_KEY: &str = "skipping_index";

/// Tries to construct a `ColumnSchema` from the given `ColumnDef`.
pub fn try_as_column_schema(column_def: &ColumnDef) -> Result<ColumnSchema> {
Expand Down Expand Up @@ -62,8 +62,8 @@ pub fn try_as_column_schema(column_def: &ColumnDef) -> Result<ColumnSchema> {
if let Some(inverted_index) = options.options.get(INVERTED_INDEX_GRPC_KEY) {
metadata.insert(INVERTED_INDEX_KEY.to_string(), inverted_index.clone());
}
if let Some(skip_index) = options.options.get(SKIP_INDEX_GRPC_KEY) {
metadata.insert(SKIP_KEY.to_string(), skip_index.clone());
if let Some(skip_index) = options.options.get(SKIPPING_INDEX_GRPC_KEY) {
metadata.insert(SKIPPING_INDEX_KEY.to_string(), skip_index.clone());
}
}

Expand All @@ -89,10 +89,10 @@ pub fn options_from_column_schema(column_schema: &ColumnSchema) -> Option<Column
.options
.insert(INVERTED_INDEX_GRPC_KEY.to_string(), inverted_index.clone());
}
if let Some(skip_index) = column_schema.metadata().get(SKIP_KEY) {
if let Some(skip_index) = column_schema.metadata().get(SKIPPING_INDEX_KEY) {
options
.options
.insert(SKIP_INDEX_GRPC_KEY.to_string(), skip_index.clone());
.insert(SKIPPING_INDEX_GRPC_KEY.to_string(), skip_index.clone());
}

(!options.options.is_empty()).then_some(options)
Expand Down
6 changes: 3 additions & 3 deletions src/datatypes/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,8 +232,8 @@ pub enum Error {
#[snafu(implicit)]
location: Location,
},
#[snafu(display("Invalid skip index option: {}", msg))]
InvalidSkipIndexOption {
#[snafu(display("Invalid skipping index option: {}", msg))]
InvalidSkippingIndexOption {
msg: String,
#[snafu(implicit)]
location: Location,
Expand All @@ -259,7 +259,7 @@ impl ErrorExt for Error {
| InvalidJson { .. }
| InvalidVector { .. }
| InvalidFulltextOption { .. }
| InvalidSkipIndexOption { .. } => StatusCode::InvalidArguments,
| InvalidSkippingIndexOption { .. } => StatusCode::InvalidArguments,

ValueExceedsPrecision { .. }
| CastType { .. }
Expand Down
8 changes: 4 additions & 4 deletions src/datatypes/src/schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,11 @@ use snafu::{ensure, ResultExt};
use crate::error::{self, DuplicateColumnSnafu, Error, ProjectArrowSchemaSnafu, Result};
use crate::prelude::ConcreteDataType;
pub use crate::schema::column_schema::{
ColumnSchema, FulltextAnalyzer, FulltextOptions, Metadata, SkipIndexOptions,
ColumnSchema, FulltextAnalyzer, FulltextOptions, Metadata, SkippingIndexOptions,
COLUMN_FULLTEXT_CHANGE_OPT_KEY_ENABLE, COLUMN_FULLTEXT_OPT_KEY_ANALYZER,
COLUMN_FULLTEXT_OPT_KEY_CASE_SENSITIVE, COLUMN_SKIP_INDEX_OPT_KEY_GRANULARITY,
COLUMN_SKIP_INDEX_OPT_KEY_TYPE, COMMENT_KEY, FULLTEXT_KEY, INVERTED_INDEX_KEY, SKIP_KEY,
TIME_INDEX_KEY,
COLUMN_FULLTEXT_OPT_KEY_CASE_SENSITIVE, COLUMN_SKIPPING_INDEX_OPT_KEY_GRANULARITY,
COLUMN_SKIPPING_INDEX_OPT_KEY_TYPE, COMMENT_KEY, FULLTEXT_KEY, INVERTED_INDEX_KEY,
SKIPPING_INDEX_KEY, TIME_INDEX_KEY,
};
pub use crate::schema::constraint::ColumnDefaultConstraint;
pub use crate::schema::raw::RawSchema;
Expand Down
40 changes: 20 additions & 20 deletions src/datatypes/src/schema/column_schema.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,16 +40,16 @@ pub const FULLTEXT_KEY: &str = "greptime:fulltext";
/// Key used to store whether the column has inverted index in arrow field's metadata.
pub const INVERTED_INDEX_KEY: &str = "greptime:inverted_index";
/// Key used to store skip options in arrow field's metadata.
pub const SKIP_KEY: &str = "greptime:skip";
pub const SKIPPING_INDEX_KEY: &str = "greptime:skipping_index";

/// Keys used in fulltext options
pub const COLUMN_FULLTEXT_CHANGE_OPT_KEY_ENABLE: &str = "enable";
pub const COLUMN_FULLTEXT_OPT_KEY_ANALYZER: &str = "analyzer";
pub const COLUMN_FULLTEXT_OPT_KEY_CASE_SENSITIVE: &str = "case_sensitive";

/// Keys used in SKIP index options
pub const COLUMN_SKIP_INDEX_OPT_KEY_GRANULARITY: &str = "granularity";
pub const COLUMN_SKIP_INDEX_OPT_KEY_TYPE: &str = "type";
/// Keys used in SKIPPING index options
pub const COLUMN_SKIPPING_INDEX_OPT_KEY_GRANULARITY: &str = "granularity";
pub const COLUMN_SKIPPING_INDEX_OPT_KEY_TYPE: &str = "type";

pub const DEFAULT_GRANULARITY: u32 = 10240;

Expand Down Expand Up @@ -307,9 +307,9 @@ impl ColumnSchema {
Ok(())
}

/// Retrieves the skip options for the column.
pub fn skip_options(&self) -> Result<Option<SkipIndexOptions>> {
match self.metadata.get(SKIP_KEY) {
/// Retrieves the skipping index options for the column.
pub fn skipping_index_options(&self) -> Result<Option<SkippingIndexOptions>> {
match self.metadata.get(SKIPPING_INDEX_KEY) {
None => Ok(None),
Some(json) => {
let options =
Expand All @@ -319,17 +319,17 @@ impl ColumnSchema {
}
}

pub fn with_skip_options(mut self, options: SkipIndexOptions) -> Result<Self> {
pub fn with_skipping_options(mut self, options: SkippingIndexOptions) -> Result<Self> {
self.metadata.insert(
SKIP_KEY.to_string(),
SKIPPING_INDEX_KEY.to_string(),
serde_json::to_string(&options).context(error::SerializeSnafu)?,
);
Ok(self)
}

pub fn set_skip_options(&mut self, options: &SkipIndexOptions) -> Result<()> {
pub fn set_skipping_options(&mut self, options: &SkippingIndexOptions) -> Result<()> {
self.metadata.insert(
SKIP_KEY.to_string(),
SKIPPING_INDEX_KEY.to_string(),
serde_json::to_string(options).context(error::SerializeSnafu)?,
);
Ok(())
Expand Down Expand Up @@ -531,18 +531,18 @@ impl fmt::Display for FulltextAnalyzer {
}
}

/// Skip options for a column.
/// Skipping options for a column.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default, Visit, VisitMut)]
#[serde(rename_all = "kebab-case")]
pub struct SkipIndexOptions {
pub struct SkippingIndexOptions {
/// The granularity of the skip index.
pub granularity: u32,
/// The type of the skip index.
#[serde(default)]
pub index_type: SkipIndexType,
}

impl fmt::Display for SkipIndexOptions {
impl fmt::Display for SkippingIndexOptions {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "granularity={}", self.granularity)?;
write!(f, ", index_type={}", self.index_type)?;
Expand All @@ -565,14 +565,14 @@ impl fmt::Display for SkipIndexType {
}
}

impl TryFrom<HashMap<String, String>> for SkipIndexOptions {
impl TryFrom<HashMap<String, String>> for SkippingIndexOptions {
type Error = Error;

fn try_from(options: HashMap<String, String>) -> Result<Self> {
// Parse granularity with default value 1
let granularity = match options.get(COLUMN_SKIP_INDEX_OPT_KEY_GRANULARITY) {
let granularity = match options.get(COLUMN_SKIPPING_INDEX_OPT_KEY_GRANULARITY) {
Some(value) => value.parse::<u32>().map_err(|_| {
error::InvalidSkipIndexOptionSnafu {
error::InvalidSkippingIndexOptionSnafu {
msg: format!("Invalid granularity: {value}, expected: positive integer"),
}
.build()
Expand All @@ -581,11 +581,11 @@ impl TryFrom<HashMap<String, String>> for SkipIndexOptions {
};

// Parse index type with default value BloomFilter
let index_type = match options.get(COLUMN_SKIP_INDEX_OPT_KEY_TYPE) {
let index_type = match options.get(COLUMN_SKIPPING_INDEX_OPT_KEY_TYPE) {
Some(typ) => match typ.to_ascii_uppercase().as_str() {
"BLOOM" => SkipIndexType::BloomFilter,
_ => {
return error::InvalidSkipIndexOptionSnafu {
return error::InvalidSkippingIndexOptionSnafu {
msg: format!("Invalid index type: {typ}, expected: 'BLOOM'"),
}
.fail();
Expand All @@ -594,7 +594,7 @@ impl TryFrom<HashMap<String, String>> for SkipIndexOptions {
None => SkipIndexType::default(),
};

Ok(SkipIndexOptions {
Ok(SkippingIndexOptions {
granularity,
index_type,
})
Expand Down
6 changes: 3 additions & 3 deletions src/query/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ pub enum Error {
location: Location,
},

#[snafu(display("Failed to get skip inde options"))]
GetSkipIndexOptions {
#[snafu(display("Failed to get SKIPPING index options"))]
GetSkippingIndexOptions {
source: datatypes::error::Error,
#[snafu(implicit)]
location: Location,
Expand Down Expand Up @@ -373,7 +373,7 @@ impl ErrorExt for Error {
MissingTableMutationHandler { .. } => StatusCode::Unexpected,
GetRegionMetadata { .. } => StatusCode::RegionNotReady,
TableReadOnly { .. } => StatusCode::Unsupported,
GetFulltextOptions { source, .. } | GetSkipIndexOptions { source, .. } => {
GetFulltextOptions { source, .. } | GetSkippingIndexOptions { source, .. } => {
source.status_code()
}
}
Expand Down
24 changes: 12 additions & 12 deletions src/query/src/sql/show_create_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,8 @@ use std::collections::HashMap;
use common_meta::SchemaOptions;
use datatypes::schema::{
ColumnDefaultConstraint, ColumnSchema, SchemaRef, COLUMN_FULLTEXT_OPT_KEY_ANALYZER,
COLUMN_FULLTEXT_OPT_KEY_CASE_SENSITIVE, COLUMN_SKIP_INDEX_OPT_KEY_GRANULARITY,
COLUMN_SKIP_INDEX_OPT_KEY_TYPE, COMMENT_KEY,
COLUMN_FULLTEXT_OPT_KEY_CASE_SENSITIVE, COLUMN_SKIPPING_INDEX_OPT_KEY_GRANULARITY,
COLUMN_SKIPPING_INDEX_OPT_KEY_TYPE, COMMENT_KEY,
};
use snafu::ResultExt;
use sql::ast::{ColumnDef, ColumnOption, ColumnOptionDef, Expr, Ident, ObjectName};
Expand All @@ -33,8 +33,8 @@ use table::metadata::{TableInfoRef, TableMeta};
use table::requests::{FILE_TABLE_META_KEY, TTL_KEY, WRITE_BUFFER_SIZE_KEY};

use crate::error::{
ConvertSqlTypeSnafu, ConvertSqlValueSnafu, GetFulltextOptionsSnafu, GetSkipIndexOptionsSnafu,
Result, SqlSnafu,
ConvertSqlTypeSnafu, ConvertSqlValueSnafu, GetFulltextOptionsSnafu,
GetSkippingIndexOptionsSnafu, Result, SqlSnafu,
};

/// Generates CREATE TABLE options from given table metadata and schema-level options.
Expand Down Expand Up @@ -118,20 +118,20 @@ fn create_column(column_schema: &ColumnSchema, quote_style: char) -> Result<Colu
}

if let Some(opt) = column_schema
.skip_options()
.context(GetSkipIndexOptionsSnafu)?
.skipping_index_options()
.context(GetSkippingIndexOptionsSnafu)?
{
let map = HashMap::from([
(
COLUMN_SKIP_INDEX_OPT_KEY_GRANULARITY.to_string(),
COLUMN_SKIPPING_INDEX_OPT_KEY_GRANULARITY.to_string(),
opt.granularity.to_string(),
),
(
COLUMN_SKIP_INDEX_OPT_KEY_TYPE.to_string(),
COLUMN_SKIPPING_INDEX_OPT_KEY_TYPE.to_string(),
opt.index_type.to_string(),
),
]);
extensions.skip_index_options = Some(map.into());
extensions.skipping_index_options = Some(map.into());
}

Ok(Column {
Expand Down Expand Up @@ -238,7 +238,7 @@ mod tests {

use common_time::timestamp::TimeUnit;
use datatypes::prelude::ConcreteDataType;
use datatypes::schema::{FulltextOptions, Schema, SchemaRef, SkipIndexOptions};
use datatypes::schema::{FulltextOptions, Schema, SchemaRef, SkippingIndexOptions};
use table::metadata::*;
use table::requests::{
TableOptions, FILE_TABLE_FORMAT_KEY, FILE_TABLE_LOCATION_KEY, FILE_TABLE_META_KEY,
Expand All @@ -250,7 +250,7 @@ mod tests {
fn test_show_create_table_sql() {
let schema = vec![
ColumnSchema::new("id", ConcreteDataType::uint32_datatype(), true)
.with_skip_options(SkipIndexOptions {
.with_skipping_options(SkippingIndexOptions {
granularity: 4096,
..Default::default()
})
Expand Down Expand Up @@ -324,7 +324,7 @@ mod tests {
assert_eq!(
r#"
CREATE TABLE IF NOT EXISTS "system_metrics" (
"id" INT UNSIGNED NULL SKIP WITH(granularity = '4096', type = 'BLOOM'),
"id" INT UNSIGNED NULL SKIPPING INDEX WITH(granularity = '4096', type = 'BLOOM'),
"host" STRING NULL,
"cpu" DOUBLE NULL,
"disk" FLOAT NULL,
Expand Down
6 changes: 3 additions & 3 deletions src/sql/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,8 +326,8 @@ pub enum Error {
location: Location,
},

#[snafu(display("Failed to set skip index option"))]
SetSkipIndexOption {
#[snafu(display("Failed to set SKIPPING index option"))]
SetSkippingIndexOption {
source: datatypes::error::Error,
#[snafu(implicit)]
location: Location,
Expand Down Expand Up @@ -382,7 +382,7 @@ impl ErrorExt for Error {
ConvertSqlValue { .. } | ConvertValue { .. } => StatusCode::Unsupported,

PermissionDenied { .. } => StatusCode::PermissionDenied,
SetFulltextOption { .. } | SetSkipIndexOption { .. } => StatusCode::Unexpected,
SetFulltextOption { .. } | SetSkippingIndexOption { .. } => StatusCode::Unexpected,
}
}

Expand Down
28 changes: 20 additions & 8 deletions src/sql/src/parsers/create_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ pub const SINK: &str = "SINK";
pub const EXPIRE: &str = "EXPIRE";
pub const AFTER: &str = "AFTER";
pub const INVERTED: &str = "INVERTED";
pub const SKIPPING: &str = "SKIPPING";

const DB_OPT_KEY_TTL: &str = "ttl";

Expand Down Expand Up @@ -705,12 +706,23 @@ impl<'a> ParserContext<'a> {

let mut is_index_declared = false;

if parser.parse_keyword(Keyword::SKIP) {
if let Token::Word(word) = parser.peek_token().token
&& word.value.eq_ignore_ascii_case(SKIPPING)
{
parser.next_token();
// Consume `INDEX` keyword
ensure!(
parser.parse_keyword(Keyword::INDEX),
InvalidColumnOptionSnafu {
name: column_name.to_string(),
msg: "expect INDEX after SKIPPING keyword",
}
);
ensure!(
column_extensions.skip_index_options.is_none(),
column_extensions.skipping_index_options.is_none(),
InvalidColumnOptionSnafu {
name: column_name.to_string(),
msg: "duplicated SKIP index option",
msg: "duplicated SKIPPING index option",
}
);

Expand All @@ -731,7 +743,7 @@ impl<'a> ParserContext<'a> {
);
}

column_extensions.skip_index_options = Some(options.into());
column_extensions.skipping_index_options = Some(options.into());
is_index_declared |= true;
}

Expand Down Expand Up @@ -2142,7 +2154,7 @@ CREATE TABLE log (
let sql = r"
CREATE TABLE log (
ts TIMESTAMP TIME INDEX,
msg INT SKIP WITH (granularity='8192', type='bloom'),
msg INT SKIPPING INDEX WITH (granularity='8192', type='bloom'),
)";
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
Expand All @@ -2153,7 +2165,7 @@ CREATE TABLE log (
if col.name().value == "msg" {
assert!(!col
.extensions
.skip_index_options
.skipping_index_options
.as_ref()
.unwrap()
.is_empty());
Expand All @@ -2166,7 +2178,7 @@ CREATE TABLE log (
let sql = r"
CREATE TABLE log (
ts TIMESTAMP TIME INDEX,
msg INT SKIP,
msg INT SKIPPING INDEX,
)";
let result =
ParserContext::create_with_dialect(sql, &GreptimeDbDialect {}, ParseOptions::default())
Expand All @@ -2177,7 +2189,7 @@ CREATE TABLE log (
if col.name().value == "msg" {
assert!(col
.extensions
.skip_index_options
.skipping_index_options
.as_ref()
.unwrap()
.is_empty());
Expand Down
Loading

0 comments on commit 8315e74

Please sign in to comment.