Skip to content

Commit

Permalink
Tidy operator depgraph edges (#394)
Browse files Browse the repository at this point in the history
* Tidy operator depgraph edges

Signed-off-by: Ana Hobden <[email protected]>

* Prompt CI

Signed-off-by: Ana Hobden <[email protected]>
  • Loading branch information
Hoverbear authored Jan 26, 2022
1 parent 1328ee2 commit f3c1c25
Show file tree
Hide file tree
Showing 3 changed files with 72 additions and 14 deletions.
48 changes: 36 additions & 12 deletions pgx/src/datum/sql_entity_graph/pgx_sql.rs
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,7 @@ impl PgxSql {
}
}

#[tracing::instrument(level = "error", skip_all)]
fn build_base_edges(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
index: NodeIndex,
Expand All @@ -448,7 +449,7 @@ fn build_base_edges(
}
}

#[instrument(level = "error", skip(graph, root, extension_sqls))]
#[tracing::instrument(level = "error", skip_all)]
fn initialize_extension_sqls<'a>(
graph: &'a mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
root: NodeIndex,
Expand Down Expand Up @@ -505,6 +506,7 @@ fn initialize_extension_sqls<'a>(
Ok((mapped_extension_sqls, bootstrap, finalize))
}

#[tracing::instrument(level = "error", skip_all)]
/// A best effort attempt to find the related [`NodeIndex`] for some [`PositioningRef`].
pub fn find_positioning_ref_target<'a>(
positioning_ref: &'a PositioningRef,
Expand Down Expand Up @@ -556,6 +558,7 @@ pub fn find_positioning_ref_target<'a>(
None
}

#[tracing::instrument(level = "error", skip_all)]
fn connect_extension_sqls(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
extension_sqls: &HashMap<ExtensionSqlEntity, NodeIndex>,
Expand Down Expand Up @@ -605,6 +608,7 @@ fn connect_extension_sqls(
Ok(())
}

#[tracing::instrument(level = "error", skip_all)]
fn initialize_schemas(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
bootstrap: Option<NodeIndex>,
Expand All @@ -626,6 +630,7 @@ fn initialize_schemas(
Ok(mapped_schemas)
}

#[tracing::instrument(level = "error", skip_all)]
fn connect_schemas(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
schemas: &HashMap<SchemaEntity, NodeIndex>,
Expand All @@ -636,6 +641,7 @@ fn connect_schemas(
}
}

#[tracing::instrument(level = "error", skip_all)]
fn initialize_enums(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
root: NodeIndex,
Expand All @@ -653,6 +659,7 @@ fn initialize_enums(
Ok(mapped_enums)
}

#[tracing::instrument(level = "error", skip_all)]
fn connect_enums(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
enums: &HashMap<PostgresEnumEntity, NodeIndex>,
Expand All @@ -670,6 +677,7 @@ fn connect_enums(
}
}

#[tracing::instrument(level = "error", skip_all)]
fn initialize_types(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
root: NodeIndex,
Expand All @@ -687,6 +695,7 @@ fn initialize_types(
Ok(mapped_types)
}

#[tracing::instrument(level = "error", skip_all)]
fn connect_types(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
types: &HashMap<PostgresTypeEntity, NodeIndex>,
Expand All @@ -704,6 +713,7 @@ fn connect_types(
}
}

#[tracing::instrument(level = "error", skip_all)]
fn initialize_externs(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
root: NodeIndex,
Expand Down Expand Up @@ -803,6 +813,7 @@ fn initialize_externs(
Ok((mapped_externs, mapped_builtin_types))
}

#[tracing::instrument(level = "error", skip_all)]
fn connect_externs(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
externs: &HashMap<PgExternEntity, NodeIndex>,
Expand Down Expand Up @@ -952,7 +963,7 @@ fn connect_externs(
if !found {
for (ty_item, &ty_index) in enums {
if ty_item.id_matches(&iterated_return.0) {
tracing::debug!(from = %item.rust_identifier(), to = %ty_item.rust_identifier(), "Adding Extern after Enum (due to return) edge.");
tracing::debug!(from = %item.rust_identifier(), to = %ty_item.rust_identifier(), "Adding Extern after Enum (due to return) edge");
graph.add_edge(
ty_index,
index,
Expand Down Expand Up @@ -1007,6 +1018,7 @@ fn connect_externs(
Ok(())
}

#[tracing::instrument(level = "error", skip_all)]
fn initialize_ords(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
root: NodeIndex,
Expand All @@ -1024,6 +1036,7 @@ fn initialize_ords(
Ok(mapped_ords)
}

#[tracing::instrument(level = "error", skip_all)]
fn connect_ords(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
ords: &HashMap<PostgresOrdEntity, NodeIndex>,
Expand Down Expand Up @@ -1052,16 +1065,23 @@ fn connect_ords(
enums,
);

for (ty_item, &ty_index) in externs {
if ty_item.operator.is_some() {
tracing::debug!(from = ?item.full_path, to = ty_item.full_path, "Adding Hash after Operator edge.");
graph.add_edge(ty_index, index, SqlGraphRelationship::RequiredBy);
// NB: no break here. We need to be dependent on all externs that are operators
for (extern_item, &extern_index) in externs {
let fn_matches = |fn_name| item.module_path == extern_item.module_path && extern_item.name == fn_name;
let cmp_fn_matches = fn_matches(item.cmp_fn_name());
let lt_fn_matches = fn_matches(item.lt_fn_name());
let lte_fn_matches = fn_matches(item.le_fn_name());
let eq_fn_matches = fn_matches(item.eq_fn_name());
let gt_fn_matches = fn_matches(item.gt_fn_name());
let gte_fn_matches = fn_matches(item.ge_fn_name());
if cmp_fn_matches || lt_fn_matches || lte_fn_matches || eq_fn_matches || gt_fn_matches || gte_fn_matches {
tracing::debug!(from = ?item.full_path, to = extern_item.full_path, "Adding Ord after Extern edge");
graph.add_edge(extern_index, index, SqlGraphRelationship::RequiredBy);
}
}
}
}

#[tracing::instrument(level = "error", skip_all)]
fn initialize_hashes(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
root: NodeIndex,
Expand All @@ -1079,6 +1099,7 @@ fn initialize_hashes(
Ok(mapped_hashes)
}

#[tracing::instrument(level = "error", skip_all)]
fn connect_hashes(
graph: &mut StableGraph<SqlGraphEntity, SqlGraphRelationship>,
hashes: &HashMap<PostgresHashEntity, NodeIndex>,
Expand Down Expand Up @@ -1107,11 +1128,14 @@ fn connect_hashes(
enums,
);

for (ty_item, &ty_index) in externs {
if ty_item.operator.is_some() {
tracing::debug!(from = ?item.full_path, to = ty_item.full_path, "Adding Hash after Operator edge.");
graph.add_edge(ty_index, index, SqlGraphRelationship::RequiredBy);
// NB: no break here. We need to be dependent on all externs that are operators
for (extern_item, &extern_index) in externs {
let hash_fn_name = item.fn_name();
let hash_fn_matches = item.module_path == extern_item.module_path && extern_item.name == hash_fn_name;

if hash_fn_matches {
tracing::debug!(from = ?item.full_path, to = extern_item.full_path, "Adding Hash after Extern edge");
graph.add_edge(extern_index, index, SqlGraphRelationship::RequiredBy);
break;
}
}
}
Expand Down
9 changes: 8 additions & 1 deletion pgx/src/datum/sql_entity_graph/postgres_hash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,12 @@ pub struct PostgresHashEntity {
pub id: core::any::TypeId,
}

impl PostgresHashEntity {
pub(crate) fn fn_name(&self) -> String {
format!("{}_hash", self.name.to_lowercase())
}
}

impl Ord for PostgresHashEntity {
fn cmp(&self, other: &Self) -> Ordering {
self.file
Expand Down Expand Up @@ -58,12 +64,13 @@ impl ToSql for PostgresHashEntity {
CREATE OPERATOR FAMILY {name}_hash_ops USING hash;\n\
CREATE OPERATOR CLASS {name}_hash_ops DEFAULT FOR TYPE {name} USING hash FAMILY {name}_hash_ops AS\n\
\tOPERATOR 1 = ({name}, {name}),\n\
\tFUNCTION 1 {name}_hash({name});\
\tFUNCTION 1 {fn_name}({name});\
",
name = self.name,
full_path = self.full_path,
file = self.file,
line = self.line,
fn_name = self.fn_name(),
);
tracing::trace!(%sql);
Ok(sql)
Expand Down
29 changes: 28 additions & 1 deletion pgx/src/datum/sql_entity_graph/postgres_ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,32 @@ pub struct PostgresOrdEntity {
pub id: core::any::TypeId,
}

impl PostgresOrdEntity {
pub(crate) fn cmp_fn_name(&self) -> String {
format!("{}_cmp", self.name.to_lowercase())
}

pub(crate) fn lt_fn_name(&self) -> String {
format!("{}_lt", self.name.to_lowercase())
}

pub(crate) fn le_fn_name(&self) -> String {
format!("{}_le", self.name.to_lowercase())
}

pub(crate) fn eq_fn_name(&self) -> String {
format!("{}_eq", self.name.to_lowercase())
}

pub(crate) fn gt_fn_name(&self) -> String {
format!("{}_gt", self.name.to_lowercase())
}

pub(crate) fn ge_fn_name(&self) -> String {
format!("{}_ge", self.name.to_lowercase())
}
}

impl Ord for PostgresOrdEntity {
fn cmp(&self, other: &Self) -> Ordering {
self.file
Expand Down Expand Up @@ -62,12 +88,13 @@ impl ToSql for PostgresOrdEntity {
\tOPERATOR 3 =,\n\
\tOPERATOR 4 >=,\n\
\tOPERATOR 5 >,\n\
\tFUNCTION 1 {name}_cmp({name}, {name});\
\tFUNCTION 1 {cmp_fn_name}({name}, {name});\
",
name = self.name,
full_path = self.full_path,
file = self.file,
line = self.line,
cmp_fn_name = self.cmp_fn_name(),
);
tracing::trace!(%sql);
Ok(sql)
Expand Down

0 comments on commit f3c1c25

Please sign in to comment.