Skip to content

Commit 3114592

Browse files
mdesalvoclaude
andcommitted
Evaluate in-memory subqueries straight to RDFTable
Extracts EvaluateSelectQueryToTable as the shared core that returns the query's internal RDFTable (body + modifiers, with the OPTIONAL/UNION/ MINUS flags carried on the table). The subquery branch now consumes it directly for in-memory data sources (graph/store/federation), skipping the RDFTable -> DataTable -> RDFTable round-trip that ApplyToDataSource imposed via the public result type. Remote SPARQL-endpoint subqueries still go through the DataTable, since their results arrive as one. Behavior-preserving; full test suite stays green. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
1 parent 997f27e commit 3114592

1 file changed

Lines changed: 33 additions & 11 deletions

File tree

‎RDFSharp/Query/Mirella/Engines/RDFQueryEngine.cs‎

Lines changed: 33 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -77,11 +77,8 @@ internal RDFQueryEngine()
7777
/// </summary>
7878
internal RDFSelectQueryResult EvaluateSelectQuery(RDFSelectQuery selectQuery, RDFDataSource datasource)
7979
{
80-
//Evaluate the body of the query
81-
RDFTable queryResultTable = EvaluateQuery(selectQuery, datasource);
82-
83-
//Evaluate the modifiers of the query
84-
RDFTable finalTable = ApplyModifiers(selectQuery, queryResultTable);
80+
//Evaluate the query down to its internal RDFTable (body + modifiers)
81+
RDFTable finalTable = EvaluateSelectQueryToTable(selectQuery, datasource);
8582

8683
//Expose the result of the query
8784
DataTable selectResults = finalTable.ToDataTable();
@@ -94,6 +91,21 @@ internal RDFSelectQueryResult EvaluateSelectQuery(RDFSelectQuery selectQuery, RD
9491
};
9592
}
9693

94+
/// <summary>
95+
/// Evaluates the given SPARQL SELECT query on the given RDF datasource down to its internal RDFTable
96+
/// (body + modifiers), carrying the OPTIONAL/UNION/MINUS join flags on the table itself. This is the
97+
/// shared core of EvaluateSelectQuery and lets in-memory subqueries be consumed directly as RDFTable,
98+
/// without the RDFTable -> DataTable -> RDFTable round-trip imposed by the public result type.
99+
/// </summary>
100+
internal RDFTable EvaluateSelectQueryToTable(RDFSelectQuery selectQuery, RDFDataSource datasource)
101+
{
102+
//Evaluate the body of the query
103+
RDFTable queryResultTable = EvaluateQuery(selectQuery, datasource);
104+
105+
//Evaluate the modifiers of the query
106+
return ApplyModifiers(selectQuery, queryResultTable);
107+
}
108+
97109
/// <summary>
98110
/// Evaluates the given SPARQL DESCRIBE query on the given RDF datasource
99111
/// </summary>
@@ -196,12 +208,22 @@ internal void EvaluateQueryMembers(List<RDFQueryMember> evaluableQueryMembers, R
196208
break;
197209

198210
case RDFSelectQuery subQuery:
199-
//Get the result table of the subquery
200-
RDFSelectQueryResult subQueryResult = subQuery.ApplyToDataSource(datasource);
201-
202-
//Expose it in internal format
203-
RDFTable subQueryTable = RDFTable.FromDataTable(subQueryResult.SelectResults);
204-
subQueryTable.IsOptional = subQuery.IsOptional || subQueryResult.SelectResults.ExtendedProperties[IsOptional] is true;
211+
RDFTable subQueryTable;
212+
if (datasource.IsSPARQLEndpoint())
213+
{
214+
//Remote subquery: results come back over the wire as a DataTable, so import them
215+
RDFSelectQueryResult subQueryResult = subQuery.ApplyToDataSource(datasource);
216+
subQueryTable = RDFTable.FromDataTable(subQueryResult.SelectResults);
217+
subQueryTable.IsOptional = subQuery.IsOptional || subQueryResult.SelectResults.ExtendedProperties[IsOptional] is true;
218+
}
219+
else
220+
{
221+
//In-memory subquery (graph/store/federation): evaluate straight to RDFTable on a
222+
//dedicated engine, skipping the RDFTable -> DataTable -> RDFTable round-trip
223+
//which we would have if we used the public query evaluation APIs
224+
subQueryTable = new RDFQueryEngine().EvaluateSelectQueryToTable(subQuery, datasource);
225+
subQueryTable.IsOptional = subQuery.IsOptional || subQueryTable.IsOptional;
226+
}
205227
subQueryTable.JoinAsUnion = subQuery.JoinAsUnion;
206228
subQueryTable.JoinAsMinus = subQuery.JoinAsMinus;
207229

0 commit comments

Comments
 (0)