-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Introduce FieldPath abstraction, restrict predicates to Field, Op, (F…
…ield|Scalar) (#324) We need an abstraction to handle addressing nested fields, hence FieldPath. Also disallows scalar-scalar comparisons, which are useless.
- Loading branch information
Showing
7 changed files
with
255 additions
and
60 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
use core::fmt; | ||
use std::fmt::{Display, Formatter}; | ||
|
||
use vortex_error::{vortex_bail, VortexResult}; | ||
|
||
#[derive(Clone, Debug, PartialEq)] | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
pub struct FieldPath { | ||
field_names: Vec<FieldIdentifier>, | ||
} | ||
|
||
impl FieldPath { | ||
pub fn builder() -> FieldPathBuilder { | ||
FieldPathBuilder::default() | ||
} | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq)] | ||
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] | ||
pub enum FieldIdentifier { | ||
Name(String), | ||
ListIndex(u64), | ||
} | ||
|
||
pub struct FieldPathBuilder { | ||
field_names: Vec<FieldIdentifier>, | ||
} | ||
|
||
impl FieldPathBuilder { | ||
pub fn new() -> Self { | ||
Self { | ||
field_names: Vec::new(), | ||
} | ||
} | ||
|
||
pub fn join<T: Into<FieldIdentifier>>(mut self, identifier: T) -> Self { | ||
self.field_names.push(identifier.into()); | ||
self | ||
} | ||
|
||
pub fn build(self) -> VortexResult<FieldPath> { | ||
if self.field_names.is_empty() { | ||
vortex_bail!("Cannot build empty path"); | ||
} | ||
Ok(FieldPath { | ||
field_names: self.field_names, | ||
}) | ||
} | ||
} | ||
|
||
impl Default for FieldPathBuilder { | ||
fn default() -> Self { | ||
Self::new() | ||
} | ||
} | ||
|
||
pub fn field(x: impl Into<FieldIdentifier>) -> FieldPath { | ||
x.into().into() | ||
} | ||
|
||
impl From<FieldIdentifier> for FieldPath { | ||
fn from(value: FieldIdentifier) -> Self { | ||
FieldPath { | ||
field_names: vec![value], | ||
} | ||
} | ||
} | ||
|
||
impl From<&str> for FieldIdentifier { | ||
fn from(value: &str) -> Self { | ||
FieldIdentifier::Name(value.to_string()) | ||
} | ||
} | ||
|
||
impl From<u64> for FieldIdentifier { | ||
fn from(value: u64) -> Self { | ||
FieldIdentifier::ListIndex(value) | ||
} | ||
} | ||
|
||
impl Display for FieldIdentifier { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
match self { | ||
FieldIdentifier::Name(name) => write!(f, "${name}"), | ||
FieldIdentifier::ListIndex(idx) => write!(f, "[{idx}]"), | ||
} | ||
} | ||
} | ||
|
||
impl Display for FieldPath { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
let formatted = self | ||
.field_names | ||
.iter() | ||
.map(|fid| format!("{fid}")) | ||
.collect::<Vec<_>>() | ||
.join("."); | ||
write!(f, "{}", formatted) | ||
} | ||
} |
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
Oops, something went wrong.