Skip to content

Commit

Permalink
clean hiereachy file
Browse files Browse the repository at this point in the history
  • Loading branch information
andicuko committed Mar 19, 2024
1 parent c31086a commit c3f307f
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 56 deletions.
46 changes: 0 additions & 46 deletions src/hierarchy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,21 +211,6 @@ impl<T: Clone> Hierarchy<T> {
.filter_map(|(p, o)| Some((p.clone(), f(o)?)))
.collect()
}

/// It creates a new hierarchy with elements for which the tail of their
/// path is not ambiguous. In the new hierarchy, only the tails of the original
/// path are used as a path.
pub fn non_ambiguous_tails(&self) -> Hierarchy<T> {
self
.iter()
.filter_map(|(path, _)|{
let path_tail = path.last().unwrap().clone();
self
.get(&[path_tail.clone()])
.and_then( |t| Some(([path_tail], t.clone())) )
})
.collect()
}
}

impl<P: Path> Hierarchy<P> {
Expand Down Expand Up @@ -483,35 +468,4 @@ mod tests {
))
);
}

#[test]
fn test_non_ambiguous() {
let values = Hierarchy::from([
(vec!["a", "b", "c"], 1),
(vec!["a", "b", "d"], 2),
(vec!["a", "c"], 3),
(vec!["a", "e"], 4),
(vec!["a", "e", "f"], 5),
(vec!["b", "c"], 6),
]);
let expected = Hierarchy::from([
(vec!["d"], 2),
(vec!["e"], 4),
(vec!["f"], 5),
]);
let non_ambiguous = values.non_ambiguous_tails();
assert_eq!(non_ambiguous, expected);
println!("{}", non_ambiguous);

let values = Hierarchy::from([
(vec!["t1", "x"], 1),
(vec!["x"], 2),
]);
let expected = Hierarchy::from([
(vec!["x"], 2),
]);
let non_ambiguous = values.non_ambiguous_tails();
assert_eq!(non_ambiguous, expected);
println!("{}", non_ambiguous);
}
}
30 changes: 20 additions & 10 deletions src/sql/relation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use super::{
Error, Result,
};
use crate::{
ast, builder::{Ready, With, WithIterator, WithoutContext}, data_type::injection::Composed, dialect::{Dialect, GenericDialect}, dialect_translation::{postgresql::PostgreSqlTranslator, QueryToRelationTranslator}, display::Dot, expr::{Expr, Identifier, Reduce, Split}, hierarchy::{Hierarchy, Path}, namer::{self, FIELD}, parser::Parser, relation::{
ast, builder::{Ready, With, WithIterator, WithoutContext}, dialect::{Dialect, GenericDialect}, dialect_translation::{postgresql::PostgreSqlTranslator, QueryToRelationTranslator}, display::Dot, expr::{Expr, Identifier, Reduce, Split}, hierarchy::{Hierarchy, Path}, namer::{self, FIELD}, parser::Parser, relation::{
Join, JoinOperator, MapBuilder, Relation, SetOperator, SetQuantifier,
Variant as _, WithInput,
LEFT_INPUT_NAME, RIGHT_INPUT_NAME
Expand All @@ -17,12 +17,7 @@ use crate::{
use dot::Id;
use itertools::Itertools;
use std::{
convert::TryFrom,
iter::{once, Iterator},
result,
str::FromStr,
sync::Arc,
ops::Deref
collections::HashMap, convert::TryFrom, iter::{once, Iterator}, ops::Deref, result, str::FromStr, sync::Arc
};

/*
Expand Down Expand Up @@ -384,21 +379,21 @@ impl<'a, T: QueryToRelationTranslator + Copy + Clone> VisitedQueryRelations<'a,
// push all names that are present in the from into named_exprs.
// for non ambiguous col names preserve the input name
// for the ambiguous ones used the name present in the relation.
let non_ambiguous_cols = columns.non_ambiguous_tails();
let non_ambiguous_cols = last(columns);
// Invert mapping of non_ambiguous_cols
let new_aliases: Hierarchy<String> = non_ambiguous_cols.iter()
.map(|(p, i)|(i.deref(), p.last().unwrap().clone()))
.collect();

for field in from.schema().iter() {
let field_name = field.name().to_string();
let new_alias = new_aliases
let alias = new_aliases
.get_key_value(&[field.name().to_string()])
.and_then(|(k, v)|{
renamed_columns.push((k.to_vec().into(), v.clone().into()));
Some(v.clone())
} );
named_exprs.push((new_alias.unwrap_or(field_name), Expr::col(field.name())));
named_exprs.push((alias.unwrap_or(field_name), Expr::col(field.name())));
}
}
}
Expand Down Expand Up @@ -768,6 +763,21 @@ impl<'a, T: QueryToRelationTranslator + Copy + Clone> TryFrom<(QueryWithRelation
}
}

/// It creates a new hierarchy with Identifier for which the last part of their
/// path is not ambiguous. The new hierarchy will contain one-element paths
fn last(columns: &Hierarchy<Identifier>) -> Hierarchy<Identifier> {
columns
.iter()
.filter_map(|(path, _)|{
let path_last = path.last().unwrap().clone();
columns
.get(&[path_last.clone()])
.and_then( |t| Some((path_last, t.clone())) )
})
.collect()
}


/// A simple SQL query parser with dialect
pub fn parse_with_dialect<D: Dialect>(query: &str, dialect: D) -> Result<ast::Query> {
let mut tokenizer = Tokenizer::new(&dialect, query);
Expand Down

0 comments on commit c3f307f

Please sign in to comment.