|
| 1 | +/******************************************************************************* |
| 2 | + * Copyright (c) 2026 Eclipse RDF4J contributors. |
| 3 | + * |
| 4 | + * All rights reserved. This program and the accompanying materials |
| 5 | + * are made available under the terms of the Eclipse Distribution License v1.0 |
| 6 | + * which accompanies this distribution, and is available at |
| 7 | + * http://www.eclipse.org/org/documents/edl-v10.php. |
| 8 | + * |
| 9 | + * SPDX-License-Identifier: BSD-3-Clause |
| 10 | + *******************************************************************************/ |
| 11 | +// Some portions generated by Codex |
| 12 | +package org.eclipse.rdf4j.sail.nativerdf.benchmark; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | + |
| 16 | +import org.eclipse.rdf4j.benchmark.common.ThemeQueryCatalog; |
| 17 | +import org.eclipse.rdf4j.benchmark.rio.util.ThemeDataSetGenerator; |
| 18 | +import org.eclipse.rdf4j.benchmark.rio.util.ThemeDataSetGenerator.Theme; |
| 19 | +import org.eclipse.rdf4j.common.transaction.IsolationLevels; |
| 20 | +import org.eclipse.rdf4j.query.explanation.Explanation; |
| 21 | +import org.eclipse.rdf4j.repository.sail.SailRepository; |
| 22 | +import org.eclipse.rdf4j.repository.sail.SailRepositoryConnection; |
| 23 | +import org.eclipse.rdf4j.repository.util.RDFInserter; |
| 24 | +import org.eclipse.rdf4j.sail.nativerdf.NativeStore; |
| 25 | +import org.junit.jupiter.api.Test; |
| 26 | +import org.junit.jupiter.api.io.TempDir; |
| 27 | + |
| 28 | +class NativeThemeQueryRegressionTest { |
| 29 | + |
| 30 | + private static final Theme THEME = Theme.PHARMA; |
| 31 | + private static final int[] QUERY_INDEXES = { 8, 10 }; |
| 32 | + private static final int ITERATIONS = 3; |
| 33 | + |
| 34 | + @Test |
| 35 | + void pharmaQueriesMatchExpectedCounts(@TempDir java.nio.file.Path dataDir) throws IOException { |
| 36 | + NativeStore sail = new NativeStore(dataDir.toFile(), "spoc,ospc,psoc"); |
| 37 | + SailRepository repository = new SailRepository(sail); |
| 38 | + try { |
| 39 | + loadData(repository); |
| 40 | + for (int iteration = 0; iteration < ITERATIONS; iteration++) { |
| 41 | + for (int queryIndex : QUERY_INDEXES) { |
| 42 | + String query = ThemeQueryCatalog.queryFor(THEME, queryIndex); |
| 43 | + long expected = ThemeQueryCatalog.expectedCountFor(THEME, queryIndex); |
| 44 | + long actual = executeQuery(repository, query); |
| 45 | + if (actual != expected) { |
| 46 | + throw new AssertionError( |
| 47 | + mismatchMessage(repository, queryIndex, iteration, expected, actual, query)); |
| 48 | + } |
| 49 | + } |
| 50 | + } |
| 51 | + } finally { |
| 52 | + repository.shutDown(); |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + private static void loadData(SailRepository repository) throws IOException { |
| 57 | + try (SailRepositoryConnection connection = repository.getConnection()) { |
| 58 | + connection.begin(IsolationLevels.NONE); |
| 59 | + RDFInserter inserter = new RDFInserter(connection); |
| 60 | + ThemeDataSetGenerator.generate(THEME, inserter); |
| 61 | + connection.commit(); |
| 62 | + } |
| 63 | + } |
| 64 | + |
| 65 | + private static long executeQuery(SailRepository repository, String query) { |
| 66 | + try (SailRepositoryConnection connection = repository.getConnection()) { |
| 67 | + return connection.prepareTupleQuery(query) |
| 68 | + .evaluate() |
| 69 | + .stream() |
| 70 | + .count(); |
| 71 | + } |
| 72 | + } |
| 73 | + |
| 74 | + private static String mismatchMessage(SailRepository repository, int queryIndex, int iteration, long expected, |
| 75 | + long actual, String query) { |
| 76 | + String explanation = "<explain failed>"; |
| 77 | + try (SailRepositoryConnection connection = repository.getConnection()) { |
| 78 | + explanation = connection.prepareTupleQuery(query) |
| 79 | + .explain(Explanation.Level.Optimized) |
| 80 | + .toString(); |
| 81 | + } catch (Exception ignored) { |
| 82 | + // Best-effort explanation for diagnosis. |
| 83 | + } |
| 84 | + return "NativeStore theme query mismatch: theme=" + THEME + ", queryIndex=" + queryIndex |
| 85 | + + ", iteration=" + iteration + ", expected=" + expected + ", actual=" + actual + "\n" + explanation; |
| 86 | + } |
| 87 | +} |
0 commit comments