Skip to content

Commit

Permalink
H-3376: Infer data type IDs if only abstract parents exists (#5272)
Browse files Browse the repository at this point in the history
  • Loading branch information
TimDiekmann authored Sep 29, 2024
1 parent bf74d01 commit bbd8886
Show file tree
Hide file tree
Showing 5 changed files with 52 additions and 3 deletions.
26 changes: 26 additions & 0 deletions apps/hash-graph/libs/graph/src/store/validation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -250,6 +250,32 @@ where
.get(0))
}

#[expect(refining_impl_trait)]
async fn has_non_abstract_parents(
&self,
data_type: &VersionedUrl,
) -> Result<bool, Report<QueryError>> {
let client = self.store.as_client().client();

Ok(client
.query_one(
"
SELECT EXISTS (
SELECT 1
FROM data_type_inherits_from
JOIN data_types
ON data_types.ontology_id = target_data_type_ontology_id
WHERE source_data_type_ontology_id = $1
AND data_types.schema->>'abstract' = 'false'
);
",
&[&DataTypeId::from_url(data_type)],
)
.await
.change_context(QueryError)?
.get(0))
}

#[expect(refining_impl_trait)]
async fn find_conversion(
&self,
Expand Down
4 changes: 4 additions & 0 deletions apps/hash-graph/libs/store/src/filter/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -536,6 +536,10 @@ mod tests {
unimplemented!()
}

async fn has_non_abstract_parents(&self, _: &VersionedUrl) -> Result<bool, Report<!>> {
unimplemented!()
}

async fn find_conversion(
&self,
_: &VersionedUrl,
Expand Down
9 changes: 9 additions & 0 deletions libs/@local/hash-graph-types/rust/src/ontology/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -184,11 +184,20 @@ pub trait DataTypeProvider: OntologyTypeProvider<DataTypeWithMetadata> {
parent: &BaseUrl,
) -> impl Future<Output = Result<bool, Report<impl Context>>> + Send;

// TODO: Remove when the data type ID is forced to be passed
// see https://linear.app/hash/issue/H-2800/validate-that-a-data-type-id-is-always-specified
fn has_children(
&self,
data_type: &VersionedUrl,
) -> impl Future<Output = Result<bool, Report<impl Context>>> + Send;

// TODO: Remove when the data type ID is forced to be passed
// see https://linear.app/hash/issue/H-2800/validate-that-a-data-type-id-is-always-specified
fn has_non_abstract_parents(
&self,
data_type: &VersionedUrl,
) -> impl Future<Output = Result<bool, Report<impl Context>>> + Send;

fn find_conversion(
&self,
source_data_type_id: &VersionedUrl,
Expand Down
8 changes: 5 additions & 3 deletions libs/@local/hash-validation/src/entity_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -377,6 +377,8 @@ impl EntityVisitor for EntityPreprocessor {
let mut status = ReportSink::new();

// We try to infer the data type ID
// TODO: Remove when the data type ID is forced to be passed
// see https://linear.app/hash/issue/H-2800/validate-that-a-data-type-id-is-always-specified
if property.metadata.data_type_id.is_none() {
let mut possible_data_types = HashSet::new();

Expand All @@ -394,14 +396,14 @@ impl EntityVisitor for EntityPreprocessor {
break;
}

let data_type = type_provider
.provide_type(&data_type_ref.url)
let has_non_abstract_parents = type_provider
.has_non_abstract_parents(&data_type_ref.url)
.await
.change_context_lazy(|| TraversalError::DataTypeRetrieval {
id: data_type_ref.clone(),
})?;

if !data_type.borrow().schema.all_of.is_empty() {
if has_non_abstract_parents {
status.capture(TraversalError::AmbiguousDataType);
possible_data_types.clear();
break;
Expand Down
8 changes: 8 additions & 0 deletions libs/@local/hash-validation/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -323,6 +323,14 @@ mod tests {
Ok(false)
}

#[expect(refining_impl_trait)]
async fn has_non_abstract_parents(
&self,
_data_type: &VersionedUrl,
) -> Result<bool, Report<InvalidDataType>> {
Ok(false)
}

#[expect(refining_impl_trait)]
async fn find_conversion(
&self,
Expand Down

0 comments on commit bbd8886

Please sign in to comment.