OpenMetadata icon indicating copy to clipboard operation
OpenMetadata copied to clipboard

RDF: RdfTagUpdater builds invalid SPARQL for tag FQNs with special characters and writes non-canonical tag triples to the default graph

Open fmcardoso opened this issue 2 months ago • 0 comments

Summary

Removing a tag or glossary term from an entity with RDF enabled logs org.apache.jena.query.QueryParseException: Encountered " "<" "< "" at line 1, column 125 from RdfTagUpdater.removeTagInline. The PATCH itself succeeds because the exception is caught and logged, but the RDF sync is wrong in more ways than the parse error.

Sentry: https://collate-3b.sentry.io/issues/7735060699/

Stack (trimmed)

org.apache.jena.query.QueryParseException: Encountered " "<" "< "" at line 1, column 125.
  at org.apache.jena.update.UpdateFactory.create(UpdateFactory.java:49)
  at o.o.service.rdf.storage.JenaFusekiStorage.executeSparqlUpdate(JenaFusekiStorage.java:1490)
  at o.o.service.rdf.rebuild.RebuildingRdfStorage.executeSparqlUpdate(RebuildingRdfStorage.java:115)
  at o.o.service.rdf.RdfRepository.executeSparqlUpdate(RdfRepository.java:3864)
  at o.o.service.rdf.RdfTagUpdater.removeTagInline(RdfTagUpdater.java:225)
  at o.o.service.rdf.RdfTagUpdater.removeTag(RdfTagUpdater.java:195)
  at o.o.service.jdbi3.EntityRepository$EntityUpdater.lambda$applyTagsDeleteInFlushAndDeferRdf$14(EntityRepository.java:9532)
  ...
  at o.o.service.resources.databases.TableResource.patch(TableResource.java:597)

Root cause

  1. Invalid IRI. RdfTagUpdater builds SPARQL with String.format and puts the tag FQN into an IRI without encoding it: baseUri + "tag/" + tagFQN.replace(".", "/"). A tag FQN with a space, ", <, >, {, }, |, ^, ` or \ (quoted FQN parts like PII."Sensitive Data", for example) makes the IRI invalid, and Jena falls back to reading < as the less-than operator. Column 125 is exactly where the tag IRI starts when the target is the hash-based entity/table/<fqn.hashCode()> URI.
  2. Wrong URIs. RdfPropertyMapper.resolveTagResource writes entity/tag/{uuid} / entity/glossaryTerm/{uuid} for tags and entity/{type}/{uuid} / entity/column/{urlEncodedFqn} for targets. RdfTagUpdater uses tag/<fqn path> and entity/<guessed type>/<fqn.hashCode()>, so even a query that parses never deletes the canonical triple.
  3. Wrong graph. Entity triples are stored in the named graph https://open-metadata.org/graph/knowledge. RdfTagUpdater runs INSERT DATA and DELETE WHERE against the default graph. Fuseki runs with tdb2:unionDefaultGraph true, so these orphan triples show up in queries that have no GRAPH clause (tag co-occurrence, communities, recommendations, inference rules) as duplicate or synthetic tag nodes.
  4. Duplicate work. postUpdate / postCreate already call RdfUpdater.updateEntity, and JenaFusekiStorage.storeEntity reconciles om:hasTag / om:hasGlossaryTerm (both are in TRANSLATOR_MANAGED_DIRECT_PREDICATES) for the entity and its columns with the canonical URIs. On the PATCH, PUT, create and import paths, RdfTagUpdater's writes add nothing correct.

Proposed fix

  • Stop issuing hand-built SPARQL from RdfTagUpdater. The entity snapshot write (RdfUpdater.updateEntity) should be the only thing that writes tag triples.
  • For tag-mutation paths with no snapshot write after them (bulk add/remove tags or glossary terms to assets, the TaskWorkflowHandler fallback / TierUpdate / tag-suggestion paths), call RdfUpdater.updateEntity for the owning entity.
  • Clean up orphan default-graph triples already written: an RDF rebuild, or a verified one-off cleanup.
  • Tests: an IT that adds and removes a tag whose FQN has a space/quote, checks the triple is gone in the union graph, and checks no synthetic tag/… or hash-based subjects exist; an IT for the bulk add-to-assets RDF sync.

Affected code (OSS main)

  • openmetadata-service/src/main/java/org/openmetadata/service/rdf/RdfTagUpdater.java
  • openmetadata-service/src/main/java/org/openmetadata/service/jdbi3/EntityRepository.java (applyTags*, applyTagsDelete*, *InFlushAndDeferRdf, applyTagsBatchWithRdf)
  • jdbi3/TagRepository.java, jdbi3/GlossaryTermRepository.java (bulk asset ops), tasks/TaskWorkflowHandler.java

fmcardoso avatar Sep 17 '26 08:09 fmcardoso