Skip to content

Direct serialization

For some mappings the CLI does not run the mapping engine at all. DuckDB reads the sources, builds the N-Triples or N-Quads lines in one SQL query, and writes the output file with COPY TO. No RDF terms are created in Java, which makes the run faster.

It is on by default and needs no configuration. This page explains when it applies, so you can tell why a run did or did not use it.

When it applies

The decision is all or nothing. If one triples map does not qualify, the whole run goes through the mapping engine.

The command line

All of these must hold:

  1. An output file is given with -o.
  2. The output format is nt or nq, without -P.
  3. There is no --limit.
  4. The evaluator is not -E reactive.
  5. No view needs deduplication. That rules out --dedup full, and --dedup view unless every view has a key that proves its rows distinct. With the default --dedup auto, plain logical sources and views without annotations qualify.
  6. There is no --strict. Only the mapping engine checks references.

The mapping

The mapping must not declare logical targets, and every term map must be one that DuckDB can render to exactly the string the engine would produce.

These qualify:

  • constants,
  • IRI templates that start with an absolute IRI, such as http://example.com/person/{id},
  • literals from a reference or a template. A language tag must be a constant. A datatype must be a constant too, and either xsd:string, xsd:integer or not an XSD type.
  • rml:class.

These do not:

  • rml:parentTriplesMap,
  • functions and rml:condition,
  • rml:gather (collections and containers),
  • blank nodes built from data,
  • an IRI taken from rml:reference, or from a template that relies on the base IRI,
  • a language tag or datatype taken from the data,
  • XSD datatypes other than xsd:string and xsd:integer,
  • more than one subject map, or more than one predicate map or graph map for a statement.

The data

Two checks need the data, so they run after the sources are opened:

  • Every column a term map reads must be a plain string. CSV columns are. Typed SQL columns are not. A JSON value can have a datatype of its own, which shows only per value, so a term map that reads a JSON field rules the direct path out.
  • Values that go into an IRI template may only contain A-Z, a-z, 0-9, ., _, ~ and -. A value typed as xsd:integer must already be in canonical form, such as 42 or -7.

If a check fails, nothing has been written yet and the run starts over in the mapping engine.

Why the restrictions

RML requires the canonical lexical form for a literal that comes from a typed value: 1.0E0 for a double, for example. SQL cannot produce those forms reliably, so CARML leaves typed values to the engine. A column of plain strings has nothing to canonicalize.

The same goes for percent-encoding in IRI templates. CARML only takes the direct path when no value needs encoding.

How the output differs

Both paths produce the same RDF. The files are not byte for byte the same:

  • The order of the lines differs, and neither order is guaranteed.
  • The direct path writes non-ASCII characters in literals as UTF-8. The engine writes them as \uXXXX escapes.

Seeing which path ran

Logging goes to standard error and is off by default.

  • -v prints Direct serialization used or Direct serialization not taken.
  • -vv adds the reason.
  • The plan command prints, per triples map, whether its shape qualifies. It cannot see the data or your map options, so "eligible" there can still fall back.
$ java -jar carml-jar-X.jar map -m mapping.ttl -of nt -vv
... Direct serialization not taken (run options): output goes to stdout

Switching it off

java -jar carml-jar-X.jar map -m mapping.ttl -of nt -o out.nt --direct-serialization=off

Do this when:

  • you need the engine's line order or escaping,
  • you collect metrics. A direct run reports no statement or iteration counts per triples map.

From the library

The library never takes the direct path by itself. Call tryMapDirectToFile and fall back if it declines:

var outcome = mapper.tryMapDirectToFile(Paths.get("out.nt"), OutputFormat.NTRIPLES);

if (!(outcome instanceof DirectSerializationOutcome.Written)) {
    // nothing was written; use mapToNTriplesBytes() instead
}

It needs a DuckDbLogicalViewEvaluatorFactory among the mapper's evaluators, and declines in strict mode.