Skip to main content

uqa_sql/ast/
indexes.rs

1//
2// Unified Query Algebra
3//
4// Copyright (c) 2023-2026 Cognica, Inc.
5//
6
7//! Typed index keys retain the legacy string encoding for ordinary columns.
8
9use std::borrow::Cow;
10
11use serde::{Deserialize, Serialize};
12
13use super::Expr;
14
15#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
16#[serde(untagged)]
17pub enum IndexKey {
18    Column(String),
19    Expression(Box<Expr>),
20}
21
22impl IndexKey {
23    #[must_use]
24    pub fn from_expression(expression: Expr) -> Self {
25        match expression {
26            Expr::Column(column) => Self::Column(column),
27            other => Self::Expression(Box::new(other)),
28        }
29    }
30
31    #[must_use]
32    pub fn column(&self) -> Option<&str> {
33        match self {
34            Self::Column(column) => Some(column),
35            Self::Expression(_) => None,
36        }
37    }
38
39    #[must_use]
40    pub fn expression(&self) -> Cow<'_, Expr> {
41        match self {
42            Self::Column(column) => Cow::Owned(Expr::Column(column.clone())),
43            Self::Expression(expression) => Cow::Borrowed(expression),
44        }
45    }
46}
47
48impl From<String> for IndexKey {
49    fn from(column: String) -> Self {
50        Self::Column(column)
51    }
52}
53
54impl From<&str> for IndexKey {
55    fn from(column: &str) -> Self {
56        Self::Column(column.into())
57    }
58}