@@ -90,6 +90,41 @@ class LmdbThemeQueryRegressionTest {
9090 "<http://example.com/theme/train/partOfLine> ?line" ),
9191 anchor (Theme .TRAIN , 7 , "<http://example.com/theme/train/name> ?name" ,
9292 "<http://example.com/theme/train/passesThrough> ?op" ));
93+ private static final String MEDICAL_Q1_FASTEST_RENDERED_QUERY = String .join ("\n " ,
94+ "SELECT (COUNT(DISTINCT ?entity) AS ?count) WHERE {" ,
95+ " {" ,
96+ " ?entity a <http://example.com/theme/medical/Condition> ." ,
97+ " ?entity <http://example.com/theme/medical/code> ?code ." ,
98+ " VALUES ?target { \" DX-200\" \" DX-201\" }" ,
99+ " FILTER ((?code = ?target) || (?code = \" DX-202\" ))" ,
100+ " }" ,
101+ " UNION" ,
102+ " {" ,
103+ " ?entity a <http://example.com/theme/medical/Medication> ." ,
104+ " ?entity <http://example.com/theme/medical/code> ?code ." ,
105+ " VALUES ?target { \" DX-200\" \" DX-201\" }" ,
106+ " FILTER ((?code = ?target) || (?code = \" DX-202\" ))" ,
107+ " }" ,
108+ " OPTIONAL {" ,
109+ " ?entity <http://example.com/theme/medical/code> ?alt ." ,
110+ " }" ,
111+ "}" );
112+ private static final String LIBRARY_Q9_FASTEST_RENDERED_QUERY = String .join ("\n " ,
113+ "SELECT (COUNT(DISTINCT ?member) AS ?count) WHERE {" ,
114+ " ?author <http://example.com/theme/library/name> ?authorName ." ,
115+ " FILTER ((?authorName = ?target) || (?authorName = \" Author 3\" ))" ,
116+ " ?book <http://example.com/theme/library/writtenBy> ?author ." ,
117+ " ?book <http://example.com/theme/library/hasCopy> ?copy ." ,
118+ " ?loan <http://example.com/theme/library/loanedCopy> ?copy ." ,
119+ " ?loan <http://example.com/theme/library/borrowedBy> ?member ." ,
120+ " ?member a <http://example.com/theme/library/Member> ." ,
121+ " ?loan a <http://example.com/theme/library/Loan> ." ,
122+ " VALUES ?target { \" Author 1\" \" Author 2\" }" ,
123+ " OPTIONAL {" ,
124+ " ?book <http://example.com/theme/library/title> ?optTitle ." ,
125+ " }" ,
126+ " FILTER ((?optTitle != \" \" ) && NOT EXISTS { ?loan <http://example.com/theme/library/dueDate> ?due . FILTER (?due < \" 2024-01-10\" ^^<http://www.w3.org/2001/XMLSchema#date>) })" ,
127+ "}" );
93128
94129 @ ParameterizedTest (name = "{0}" )
95130 @ MethodSource ("highValueThemes" )
@@ -243,6 +278,68 @@ void electricalGridGeneratorCapacityThresholdUsesFastestKnownShape(@TempDir Path
243278 }
244279 }
245280
281+ @ Test
282+ void medicalConditionsOrMedicationsByCodeUsesBranchLocalValuesAndFilter (@ TempDir Path dataDir ) throws Exception {
283+ Theme theme = Theme .MEDICAL_RECORDS ;
284+ Path themeDir = dataDir .resolve (theme .name ());
285+ LmdbStore store = new LmdbStore (themeDir .toFile (), ConfigUtil .createConfig ());
286+ SailRepository repository = new SailRepository (store );
287+ try {
288+ BenchmarkJoinEstimatorSupport .prepareEstimatorForBulkLoad (repository , store );
289+ loadData (repository , theme );
290+ persistEstimatorAfterBulkLoad (repository , store );
291+ primeLearnedFilterStats (repository , theme , 1 );
292+ BenchmarkJoinEstimatorSupport .persistStoreStatistics (store );
293+ } finally {
294+ shutdownAndRelease (repository , store );
295+ }
296+
297+ store = new LmdbStore (themeDir .toFile (), ConfigUtil .createConfig ());
298+ repository = new SailRepository (store );
299+ try {
300+ OptimizerSnapshot snapshot = explainOptimized (repository , theme , 1 );
301+ assertPlannerDiagnosticsPresent (theme , 1 , snapshot .plan ());
302+ if (!MEDICAL_Q1_FASTEST_RENDERED_QUERY .equals (snapshot .renderedQuery ().trim ())) {
303+ throw new AssertionError ("Medical q1 should match the fastest branch-local VALUES/filter shape\n "
304+ + "Expected:\n " + MEDICAL_Q1_FASTEST_RENDERED_QUERY + "\n Actual:\n "
305+ + snapshot .renderedQuery () + "\n Plan:\n " + snapshot .plan ());
306+ }
307+ } finally {
308+ shutdownAndRelease (repository , store );
309+ }
310+ }
311+
312+ @ Test
313+ void libraryMembersBorrowingBooksByAuthorsUsesFastestKnownShape (@ TempDir Path dataDir ) throws Exception {
314+ Theme theme = Theme .LIBRARY ;
315+ Path themeDir = dataDir .resolve (theme .name ());
316+ LmdbStore store = new LmdbStore (themeDir .toFile (), ConfigUtil .createConfig ());
317+ SailRepository repository = new SailRepository (store );
318+ try {
319+ BenchmarkJoinEstimatorSupport .prepareEstimatorForBulkLoad (repository , store );
320+ loadData (repository , theme );
321+ persistEstimatorAfterBulkLoad (repository , store );
322+ primeLearnedFilterStats (repository , theme , 9 );
323+ BenchmarkJoinEstimatorSupport .persistStoreStatistics (store );
324+ } finally {
325+ shutdownAndRelease (repository , store );
326+ }
327+
328+ store = new LmdbStore (themeDir .toFile (), ConfigUtil .createConfig ());
329+ repository = new SailRepository (store );
330+ try {
331+ OptimizerSnapshot snapshot = explainOptimized (repository , theme , 9 );
332+ assertPlannerDiagnosticsPresent (theme , 9 , snapshot .plan ());
333+ if (!LIBRARY_Q9_FASTEST_RENDERED_QUERY .equals (snapshot .renderedQuery ().trim ())) {
334+ throw new AssertionError ("Library q9 should match the fastest author/book/loan shape\n "
335+ + "Expected:\n " + LIBRARY_Q9_FASTEST_RENDERED_QUERY + "\n Actual:\n "
336+ + snapshot .renderedQuery () + "\n Plan:\n " + snapshot .plan ());
337+ }
338+ } finally {
339+ shutdownAndRelease (repository , store );
340+ }
341+ }
342+
246343 private static Stream <Theme > highValueThemes () {
247344 return Stream .of (Theme .PHARMA , Theme .LIBRARY , Theme .MEDICAL_RECORDS , Theme .ENGINEERING ,
248345 Theme .ELECTRICAL_GRID , Theme .TRAIN , Theme .SOCIAL_MEDIA );
0 commit comments