RDFSharp NuGet
Query

Tips and tricks

How the engine works and how to help it

The engine is straightforward once you know a few of its habits. Here is what it reorders by itself, what it cannot guess from your data and how to keep your queries light and readable.

The cooperative’s questions have grown and one of them misbehaves: an average of prices that comes out empty. It is not a bug in the query. It comes from a habit of the engine that is worth knowing, like the others on this page.

This page works on vallerosa-catalog.ttl, loaded as in Getting started with RDFGraph.FromFileAsync.

using System.Data;
using RDFSharp.Model;   // resources, literals, graphs
using RDFSharp.Query;   // variables, patterns, queries

RDFNamespace vr = new RDFNamespace("vr", "https://vallerosa.example/vocab#");
RDFNamespace vrd = new RDFNamespace("vrd", "https://vallerosa.example/id/");
RDFGraph catalog = await RDFGraph.FromFileAsync(RDFModelEnums.RDFFormats.Turtle, "vallerosa-catalog.ttl");

RDFResource producedBy = new RDFResource(vr + "producedBy");

RDFResource rosso2021 = new RDFResource(vrd + "rosso-2021");

RDFVariable wine = new RDFVariable("wine");
RDFVariable label = new RDFVariable("label");
RDFVariable price = new RDFVariable("price");
RDFVariable producer = new RDFVariable("producer");

The order of your patterns

The engine answers a pattern group by evaluating its patterns and joining the results. The order of the joins decides how big the intermediate tables become. A bad order can multiply rows that a good one never creates. On a graph or on a memory store you do not have to care: before the evaluation the optimizer reorders the patterns by itself. It follows three rules, in this order:

  1. patterns that share a variable with the ones already placed come first, so that no join multiplies every row by every row
  2. among those, the pattern that brings in fewer new variables comes first, because it behaves like a filter
  3. on a tie, the pattern with fewer matching triples comes first, estimated from the indexes of the data source

A pattern that begins with a constant, such as one naming vrd:poggio-alto, is usually the most selective one and ends up first whatever its position in your query. The reordering never changes the answer: it changes only the way the engine gets there.

RDFResource hasPrice = new RDFResource(vr + "price");

RDFResource poggioAlto = new RDFResource(vrd + "poggio-alto");

RDFPattern wineLabel = new RDFPattern(wine, RDFVocabulary.RDFS.LABEL, label);
RDFPattern winePrice = new RDFPattern(wine, hasPrice, price);
RDFPattern wineOfPoggio = new RDFPattern(wine, producedBy, poggioAlto);

RDFSelectQuery broadFirst = new RDFSelectQuery()
    .AddPatternGroup(new RDFPatternGroup()
        .AddPattern(winePrice)
        .AddPattern(wineLabel)
        .AddPattern(wineOfPoggio))
    .AddProjectionVariable(wine);
RDFSelectQuery narrowFirst = new RDFSelectQuery()
    .AddPatternGroup(new RDFPatternGroup()
        .AddPattern(wineOfPoggio)
        .AddPattern(winePrice)
        .AddPattern(wineLabel))
    .AddProjectionVariable(wine);

Console.WriteLine((await broadFirst.ApplyToGraphAsync(catalog)).SelectResultsCount);
Console.WriteLine((await narrowFirst.ApplyToGraphAsync(catalog)).SelectResultsCount);
4
4

Two wines of Poggio Alto with two labels each: four rows in both orders. The optimizer has limits that you should know:

So on a federation or an endpoint the order is yours to choose: write first the patterns with constants and then the ones that connect to them. The habit costs nothing on a graph either.

Project only what you need

A SELECT without projection variables behaves like SELECT *: every variable that takes part in the patterns becomes a column of the result.

RDFSelectQuery withoutProjection = new RDFSelectQuery()
    .AddPatternGroup(new RDFPatternGroup()
        .AddPattern(wineLabel)
        .AddPattern(new RDFPattern(wine, producedBy, producer)));
RDFSelectQueryResult wide = await withoutProjection.ApplyToGraphAsync(catalog);
foreach (DataColumn column in wide.SelectResults.Columns)
    Console.WriteLine(column.ColumnName);
?WINE
?PRODUCER
?LABEL

Adding the projection of the one variable you want leaves a table with one column:

RDFSelectQuery withProjection = new RDFSelectQuery()
    .AddPatternGroup(new RDFPatternGroup()
        .AddPattern(wineLabel)
        .AddPattern(new RDFPattern(wine, producedBy, producer)))
    .AddProjectionVariable(label);
RDFSelectQueryResult narrow = await withProjection.ApplyToGraphAsync(catalog);
foreach (DataColumn column in narrow.SelectResults.Columns)
    Console.WriteLine(column.ColumnName);
?LABEL

At the top level of a query the projection is applied after the joins, so it makes the result smaller and does not make the joins cheaper. It matters most in a subquery, which hands to the outer query only the columns it projects. Here the inner query uses a price pattern to keep the wines that have one and projects only ?wine: the outer query receives a single column to join with the labels.

RDFSelectQuery poggioWinesWithPrice = new RDFSelectQuery()
    .AddPatternGroup(new RDFPatternGroup()
        .AddPattern(wineOfPoggio)
        .AddPattern(winePrice))
    .AddProjectionVariable(wine);
RDFSelectQuery labelledPoggioWines = new RDFSelectQuery()
    .AddPatternGroup(new RDFPatternGroup()
        .AddPattern(wineLabel))
    .AddSubQuery(poggioWinesWithPrice);
RDFSelectQueryResult labelled = await labelledPoggioWines.ApplyToGraphAsync(catalog);
foreach (DataColumn column in labelled.SelectResults.Columns)
    Console.WriteLine(column.ColumnName);
?WINE
?LABEL

The price never reaches the outer table. See Subqueries for the rest of the story.

Avoid the pattern that matches everything

?s ?p ?o has a variable in every position, so every triple of the data source matches it. On the catalog these are 143 rows. On a larger source they are all its triples.

RDFVariable anySubject = new RDFVariable("s");
RDFVariable anyPredicate = new RDFVariable("p");
RDFVariable anyObject = new RDFVariable("o");

RDFSelectQuery everything = new RDFSelectQuery()
    .AddPatternGroup(new RDFPatternGroup()
        .AddPattern(new RDFPattern(anySubject, anyPredicate, anyObject)));
Console.WriteLine(everything);
Console.WriteLine((await everything.ApplyToGraphAsync(catalog)).SelectResultsCount);
SELECT *
WHERE {
  {
    ?S ?P ?O .
  }
}

143

On a federation the damage grows, because every pattern is asked of every member on its own, as the federations page explains. The query above would send exactly this text to each endpoint of the federation and wait for all the triples that it holds. Give every pattern a constant in at least one position and pair a poorly selective pattern with more selective ones.

Type your literals

The engine computes with the datatype of a literal. A price written as a typed xsd:decimal can be averaged and compared. A price written as a plain literal is text and arithmetic on text finds nothing to work with. A partner shop has returned the prices of three wines as plain text. The catalog holds them as decimals:

RDFResource riserva2019 = new RDFResource(vrd + "riserva-2019");
RDFResource bianco2023 = new RDFResource(vrd + "bianco-2023");

RDFPlainLiteral rossoPrice = new RDFPlainLiteral("14.50");
RDFPlainLiteral riservaPrice = new RDFPlainLiteral("32.00");
RDFPlainLiteral biancoPrice = new RDFPlainLiteral("11.00");

RDFVariable average = new RDFVariable("average");

RDFGraph importedPrices = new RDFGraph();
importedPrices.AddTriple(new RDFTriple(rosso2021, hasPrice, rossoPrice));
importedPrices.AddTriple(new RDFTriple(riserva2019, hasPrice, riservaPrice));
importedPrices.AddTriple(new RDFTriple(bianco2023, hasPrice, biancoPrice));

RDFSelectQuery averagePrice = new RDFSelectQuery()
    .AddPatternGroup(new RDFPatternGroup().AddPattern(winePrice))
    .AddModifier(new RDFGroupByModifier().AddAggregator(new RDFAvgAggregator(price, average)));

Console.WriteLine("typed: " + (await averagePrice.ApplyToGraphAsync(catalog)).SelectResults.Rows[0]["?AVERAGE"]);
Console.WriteLine("plain: " + (await averagePrice.ApplyToGraphAsync(importedPrices)).SelectResults.Rows[0]["?AVERAGE"]);
typed: 19.75^^http://www.w3.org/2001/XMLSchema#decimal
plain: 

The average of the plain prices is empty, with no error to tell you why. The same happens with comparisons and with computed values: a filter for prices below 13 keeps two wines of the catalog and none of the import. The price with VAT of an imported wine is unbound.

RDFTypedLiteral priceCap = new RDFTypedLiteral("13", RDFModelEnums.RDFDatatypes.XSD_DECIMAL);
RDFTypedLiteral vatMultiplier = new RDFTypedLiteral("1.22", RDFModelEnums.RDFDatatypes.XSD_DECIMAL);

RDFVariable withVat = new RDFVariable("withVat");

RDFSelectQuery under13 = new RDFSelectQuery()
    .AddPatternGroup(new RDFPatternGroup()
        .AddPattern(winePrice)
        .AddFilter(new RDFFilter(new RDFComparisonExpression(
            RDFQueryEnums.RDFComparisonFlavors.LessThan, price,
            new RDFConstantExpression(priceCap)))))
    .AddProjectionVariable(wine);
Console.WriteLine("typed: " + (await under13.ApplyToGraphAsync(catalog)).SelectResultsCount);
Console.WriteLine("plain: " + (await under13.ApplyToGraphAsync(importedPrices)).SelectResultsCount);

RDFSelectQuery priceWithVat = new RDFSelectQuery()
    .AddPatternGroup(new RDFPatternGroup()
        .AddPattern(winePrice)
        .AddBind(new RDFBind(new RDFMultiplyExpression(price, vatMultiplier), withVat)))
    .AddProjectionVariable(wine)
    .AddProjectionVariable(withVat);
foreach (DataRow row in (await priceWithVat.ApplyToGraphAsync(importedPrices)).SelectResults.Rows)
    Console.WriteLine($"{row["?WINE"]} | {row["?WITHVAT"]}");
typed: 2
plain: 0
https://vallerosa.example/id/rosso-2021 | 
https://vallerosa.example/id/riserva-2019 | 
https://vallerosa.example/id/bianco-2023 | 

The rule has two halves. Use typed literals for what you will count, compare, add or measure: prices, years, alcohol degrees, coordinates. Use plain literals for text that you will search, match with a regular expression or show: labels and descriptions. When data comes from outside, check its datatypes before you trust an aggregate over it: the SHACL validation of the Model chapter can do it for you.

Readable queries

Prefixes do not change what a query means or how fast it runs, only how it reads when you print it. Add the namespaces you use with AddPrefix and the printed query shortens its IRIs:

RDFSelectQuery prefixed = new RDFSelectQuery()
    .AddPrefix(vr)
    .AddPrefix(vrd)
    .AddPrefix(RDFNamespaceRegister.GetByPrefix("rdfs"))
    .AddPatternGroup(new RDFPatternGroup()
        .AddPattern(wineLabel)
        .AddPattern(wineOfPoggio))
    .AddProjectionVariable(label);
Console.WriteLine(prefixed);
PREFIX vr: <https://vallerosa.example/vocab#>
PREFIX vrd: <https://vallerosa.example/id/>
PREFIX rdfs: <http://www.w3.org/2000/01/rdf-schema#>

SELECT ?LABEL
WHERE {
  {
    ?WINE rdfs:label ?LABEL .
    ?WINE vr:producedBy vrd:poggio-alto .
  }
}

The prefixes of the namespace register such as rdfs are one call away. Your own come from the RDFNamespace objects you already have.

Improve this page