-
Notifications
You must be signed in to change notification settings - Fork 590
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(expr): add function
pg_index_column_has_property
(#17275)
Signed-off-by: Runji Wang <[email protected]> Co-authored-by: August <[email protected]>
- Loading branch information
1 parent
9274ebc
commit e34c83b
Showing
15 changed files
with
210 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
128 changes: 128 additions & 0 deletions
128
src/frontend/src/expr/function_impl/pg_index_column_has_property.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,128 @@ | ||
// Copyright 2024 RisingWave 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 risingwave_expr::{capture_context, function, Result}; | ||
|
||
use super::context::{CATALOG_READER, DB_NAME}; | ||
use crate::catalog::CatalogReader; | ||
|
||
/// Tests whether an index column has the named property. | ||
/// | ||
/// `index` is the OID of the index. | ||
/// `column` is the column number (1-based) within the index. | ||
/// | ||
/// NULL is returned if the property name is not known or does not apply to the particular object, | ||
/// or if the OID or column number does not identify a valid object. | ||
/// | ||
/// # Supported Properties | ||
/// | ||
/// - `asc`: Does the column sort in ascending order on a forward scan? | ||
/// - `desc`: Does the column sort in descending order on a forward scan? | ||
/// - `nulls_first`: Does the column sort with nulls first on a forward scan? | ||
/// - `nulls_last`: Does the column sort with nulls last on a forward scan? | ||
/// | ||
/// # Examples | ||
/// | ||
/// ```slt | ||
/// statement ok | ||
/// create table t(a int, b int); | ||
/// | ||
/// statement ok | ||
/// create index i on t (a asc, b desc); | ||
/// | ||
/// query B | ||
/// select pg_index_column_has_property('i'::regclass, 1, 'asc'); | ||
/// ---- | ||
/// t | ||
/// | ||
/// query B | ||
/// select pg_index_column_has_property('i'::regclass, 1, 'DESC'); | ||
/// ---- | ||
/// f | ||
/// | ||
/// query B | ||
/// select pg_index_column_has_property('i'::regclass, 1, 'nulls_FIRST'); | ||
/// ---- | ||
/// f | ||
/// | ||
/// query B | ||
/// select pg_index_column_has_property('i'::regclass, 1, 'nulls_last'); | ||
/// ---- | ||
/// t | ||
/// | ||
/// query B | ||
/// select pg_index_column_has_property('i'::regclass, 2, 'asc'); | ||
/// ---- | ||
/// f | ||
/// | ||
/// query B | ||
/// select pg_index_column_has_property('i'::regclass, 2, 'desc'); | ||
/// ---- | ||
/// t | ||
/// | ||
/// query B | ||
/// select pg_index_column_has_property('i'::regclass, 2, 'nulls_first'); | ||
/// ---- | ||
/// t | ||
/// | ||
/// query B | ||
/// select pg_index_column_has_property('i'::regclass, 2, 'nulls_last'); | ||
/// ---- | ||
/// f | ||
/// | ||
/// query B | ||
/// select pg_index_column_has_property('i'::regclass, 1, 'gg'); -- invalid property | ||
/// ---- | ||
/// NULL | ||
/// | ||
/// query B | ||
/// select pg_index_column_has_property('i'::regclass, 0, 'asc'); -- column 0 does not exist | ||
/// ---- | ||
/// NULL | ||
/// | ||
/// statement ok | ||
/// drop index i; | ||
/// | ||
/// statement ok | ||
/// drop table t; | ||
/// ``` | ||
#[function("pg_index_column_has_property(int4, int4, varchar) -> boolean")] | ||
fn pg_index_column_has_property(index: i32, column: i32, property: &str) -> Result<Option<bool>> { | ||
pg_index_column_has_property_impl_captured(index, column, property) | ||
} | ||
|
||
#[capture_context(CATALOG_READER, DB_NAME)] | ||
fn pg_index_column_has_property_impl( | ||
catalog: &CatalogReader, | ||
db_name: &str, | ||
index_id: i32, | ||
column_idx: i32, | ||
property: &str, | ||
// `Result` is not necessary for this function, but it's required by `capture_context`. | ||
) -> Result<Option<bool>> { | ||
let catalog_reader = catalog.read_guard(); | ||
let Ok(index) = catalog_reader.get_index_by_id(db_name, index_id as u32) else { | ||
return Ok(None); | ||
}; | ||
let Some(properties) = index.get_column_properties((column_idx - 1) as usize) else { | ||
return Ok(None); | ||
}; | ||
Ok(match property.to_lowercase().as_str() { | ||
"asc" => Some(!properties.is_desc), | ||
"desc" => Some(properties.is_desc), | ||
"nulls_first" => Some(properties.nulls_first), | ||
"nulls_last" => Some(!properties.nulls_first), | ||
_ => None, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/meta/model_v2/migration/src/m20240617_070131_index_column_properties.rs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
use sea_orm_migration::prelude::*; | ||
|
||
#[derive(DeriveMigrationName)] | ||
pub struct Migration; | ||
|
||
#[async_trait::async_trait] | ||
impl MigrationTrait for Migration { | ||
async fn up(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(Index::Table) | ||
.add_column(ColumnDef::new(Index::IndexColumnProperties).binary()) | ||
.to_owned(), | ||
) | ||
.await | ||
} | ||
|
||
async fn down(&self, manager: &SchemaManager) -> Result<(), DbErr> { | ||
manager | ||
.alter_table( | ||
Table::alter() | ||
.table(Index::Table) | ||
.drop_column(Index::IndexColumnProperties) | ||
.to_owned(), | ||
) | ||
.await | ||
} | ||
} | ||
|
||
#[derive(DeriveIden)] | ||
enum Index { | ||
Table, | ||
IndexColumnProperties, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters