Skip to content

Conversation

@jonathanc-n
Copy link
Contributor

Which issue does this PR close?

Build on #1116

What changes are included in this PR?

Adds IndexByName and index by id to name mapping

Are these changes tested?

Yes, unit tests

@jonathanc-n
Copy link
Contributor Author

cc @jdockerty @liurenjie1024

Copy link
Contributor

@jdockerty jdockerty left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

A question and a clarification 💯

Copy link
Contributor

@liurenjie1024 liurenjie1024 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jonathanc-n , just finished first round of review, generally looks good.

@jonathanc-n
Copy link
Contributor Author

@jdockerty @liurenjie1024 Thanks for the reviews. Should be fixed now!

Xuanwo
Xuanwo previously approved these changes May 10, 2025
Copy link
Member

@Xuanwo Xuanwo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @jonathanc-n for working on this and thank you @liurenjie1024 for the review.

Wait for @liurenjie1024 for another look.

Copy link
Contributor

@liurenjie1024 liurenjie1024 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jonathanc-n for this pr, just finished second round of review. There are still some problems to fix.

}

/// Recursively visits the entire name mapping using visitor
fn visit_name_mapping<V>(namespace: &Vec<Arc<MappedField>>, visitor: &V) -> V::S
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
fn visit_name_mapping<V>(namespace: &Vec<Arc<MappedField>>, visitor: &V) -> V::S
fn visit_name_mapping<V>(namespace: &NameMapping, visitor: &V) -> V::S

It's odd that the name is visit_name_mapping while accepts a list of fields.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also the return value should be a Result

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is because when creating a new NameMapping we call visit_schema which creates an array of mapped fields which are then called on by index_by_id + index_by_name

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@liurenjie1024 This should be good for another look, thanks!

Comment on lines +21 to +30
pub trait NameMappingVisitor {
/// Aggregated result of `MappedField`s
type S;

/// Handles entire `NameMapping` field
fn mapping(&self, field_result: Self::S) -> Self::S;

/// Handles a single `MappedField`
fn field(&self, field: &MappedField, field_result: Self::S) -> Self::S;
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
pub trait NameMappingVisitor {
/// Aggregated result of `MappedField`s
type S;
/// Handles entire `NameMapping` field
fn mapping(&self, field_result: Self::S) -> Self::S;
/// Handles a single `MappedField`
fn field(&self, field: &MappedField, field_result: Self::S) -> Self::S;
}
pub trait NameMappingVisitor {
type T;
/// Aggregated result of `MappedField`s
type S;
/// Handles entire `NameMapping` field
fn mapping(&mut self, mapping: &NameMapping, field_result: Vec<Self::T>) -> Result<Self::S>;
/// Handles a single `MappedField`
fn field(&mut self, field: &MappedField, field_result: Vec<Self::T>) -> Result<Self::T>;
}

Comment on lines +134 to +147
fn visit_name_mapping<V>(name_mapping: &Vec<Arc<MappedField>>, visitor: &V) -> Result<V::S>
where
V: NameMappingVisitor,
V::S: IntoIterator + FromIterator<<V::S as IntoIterator>::Item>,
{
let root_result = visit_fields(name_mapping, visitor);
Ok(visitor.mapping(root_result))
}

/// Recursively visits a slice of mapped fields using visitor
fn visit_fields<V>(fields: &Vec<Arc<MappedField>>, visitor: &V) -> V::S
where
V: NameMappingVisitor,
V::S: IntoIterator + FromIterator<<V::S as IntoIterator>::Item>,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please refine the visiting logic with suggested visitor.

/// Creates new `NameMapping` instance
pub fn new(schema: &Schema) -> Self {
// Create `MappedField` instances by visiting schema.
let root = visit_schema(schema, &mut CreateMapping).unwrap();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can construct a NameMapping without index here, and then assign the indcies to it before returning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants