risingwave_frontend/expr/function_impl/pg_index_column_has_property.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 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,
})
}