jenkins-bot has submitted this change and it was merged.

Change subject: Add workaround for Blazegraph optimizer bug with labels 
explicitly specified.
......................................................................


Add workaround for Blazegraph optimizer bug with labels explicitly specified.

Change-Id: I475a9bfde4871a50b55f306a61fdf517baa15b69
Bug: T146921
---
M 
blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/WikibaseOptimizers.java
M 
blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/label/EmptyLabelServiceOptimizer.java
A 
blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/label/LabelServiceExtractOptimizer.java
3 files changed, 97 insertions(+), 0 deletions(-)

Approvals:
  Smalyshev: Looks good to me, approved
  jenkins-bot: Verified



diff --git 
a/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/WikibaseOptimizers.java
 
b/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/WikibaseOptimizers.java
index b96a817..c066a84 100644
--- 
a/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/WikibaseOptimizers.java
+++ 
b/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/WikibaseOptimizers.java
@@ -1,6 +1,7 @@
 package org.wikidata.query.rdf.blazegraph;
 
 import org.wikidata.query.rdf.blazegraph.label.EmptyLabelServiceOptimizer;
+import org.wikidata.query.rdf.blazegraph.label.LabelServiceExtractOptimizer;
 import org.wikidata.query.rdf.blazegraph.label.LabelServicePlacementOptimizer;
 
 import com.bigdata.rdf.sparql.ast.optimizers.DefaultOptimizerList;
@@ -12,6 +13,7 @@
     private static final long serialVersionUID = 2364845438265527328L;
 
     public WikibaseOptimizers() {
+        add(0, new LabelServiceExtractOptimizer());
         add(new EmptyLabelServiceOptimizer());
         add(new LabelServicePlacementOptimizer());
     }
diff --git 
a/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/label/EmptyLabelServiceOptimizer.java
 
b/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/label/EmptyLabelServiceOptimizer.java
index 3e28c0f..44cb389 100644
--- 
a/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/label/EmptyLabelServiceOptimizer.java
+++ 
b/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/label/EmptyLabelServiceOptimizer.java
@@ -1,5 +1,7 @@
 package org.wikidata.query.rdf.blazegraph.label;
 
+import java.util.List;
+
 import org.apache.log4j.Logger;
 import org.openrdf.model.URI;
 import org.openrdf.model.impl.URIImpl;
@@ -65,6 +67,9 @@
                 foundArg = true;
                 break;
             }
+
+            foundArg = restoreExtracted(service) || foundArg;
+
             if (foundArg) {
                 continue;
             }
@@ -76,6 +81,27 @@
     }
 
     /**
+     * Restore extracted statement from label service node.
+     * @param service
+     * @return
+     */
+    @SuppressWarnings("unchecked")
+    private boolean restoreExtracted(ServiceNode service) {
+        boolean found = false;
+        JoinGroupNode g = (JoinGroupNode) service.getGraphPattern();
+
+        final List<BOp> extractedList = 
(List<BOp>)service.annotations().get(LabelServiceExtractOptimizer.EXTRACTOR_ANNOTATION);
+        if (extractedList != null && !extractedList.isEmpty()) {
+            for (BOp st : extractedList) {
+                g.addArg(st);
+            }
+            found = true;
+        }
+        
service.annotations().remove(LabelServiceExtractOptimizer.EXTRACTOR_ANNOTATION);
+        return found;
+    }
+
+    /**
      * Infer that the user wanted to resolve some variables using the label
      * service.
      */
diff --git 
a/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/label/LabelServiceExtractOptimizer.java
 
b/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/label/LabelServiceExtractOptimizer.java
new file mode 100644
index 0000000..0a7f3f4
--- /dev/null
+++ 
b/blazegraph/src/main/java/org/wikidata/query/rdf/blazegraph/label/LabelServiceExtractOptimizer.java
@@ -0,0 +1,69 @@
+package org.wikidata.query.rdf.blazegraph.label;
+
+import java.util.ArrayList;
+import java.util.List;
+
+import org.wikidata.query.rdf.common.uri.Ontology;
+
+import com.bigdata.bop.BOp;
+import com.bigdata.bop.IBindingSet;
+import com.bigdata.rdf.model.BigdataValue;
+import com.bigdata.rdf.sparql.ast.JoinGroupNode;
+import com.bigdata.rdf.sparql.ast.QueryRoot;
+import com.bigdata.rdf.sparql.ast.QueryType;
+import com.bigdata.rdf.sparql.ast.StatementPatternNode;
+import com.bigdata.rdf.sparql.ast.StaticAnalysis;
+import com.bigdata.rdf.sparql.ast.eval.AST2BOpContext;
+import com.bigdata.rdf.sparql.ast.optimizers.AbstractJoinGroupOptimizer;
+import com.bigdata.rdf.sparql.ast.service.ServiceNode;
+import com.bigdata.rdf.store.BD;
+
+/**
+ * This class extracts label statements from label service's SERVICE clause.
+ * This is to work around bug: https://jira.blazegraph.com/browse/BLZG-2097
+ * EmptyLabelService will insert the statements back.
+ */
+public class LabelServiceExtractOptimizer extends AbstractJoinGroupOptimizer {
+
+    /**
+     * Annotation to store extracted nodes.
+     */
+    public static final String EXTRACTOR_ANNOTATION = 
LabelServiceExtractOptimizer.class.getName() + ".extractedStatements";
+
+    @Override
+    protected void optimizeJoinGroup(AST2BOpContext ctx, StaticAnalysis sa,
+            IBindingSet[] bSets, JoinGroupNode op) {
+        final QueryRoot root = sa.getQueryRoot();
+
+        if (root.getQueryType() == QueryType.ASK) {
+            return;
+        }
+        for (ServiceNode service : op.getServiceNodes()) {
+            BigdataValue serviceRef = service.getServiceRef().getValue();
+            if (serviceRef == null) {
+                continue;
+            }
+            if (!serviceRef.stringValue().startsWith(Ontology.LABEL)) {
+                continue;
+            }
+            JoinGroupNode g = (JoinGroupNode) service.getGraphPattern();
+            final List<BOp> extractedNodes = new ArrayList<BOp>();
+            for (BOp st : g.args()) {
+                StatementPatternNode sn = (StatementPatternNode) st;
+                if (sn.s().isConstant() && 
BD.SERVICE_PARAM.equals(sn.s().getValue())) {
+                    // skip parameters
+                    continue;
+                }
+                extractedNodes.add(st);
+            }
+
+            for (BOp node: extractedNodes) {
+                g.removeArg(node);
+            }
+
+            if (!extractedNodes.isEmpty()) {
+                service.annotations().put(EXTRACTOR_ANNOTATION, 
extractedNodes);
+            }
+        }
+    }
+}

-- 
To view, visit https://gerrit.wikimedia.org/r/315892
To unsubscribe, visit https://gerrit.wikimedia.org/r/settings

Gerrit-MessageType: merged
Gerrit-Change-Id: I475a9bfde4871a50b55f306a61fdf517baa15b69
Gerrit-PatchSet: 2
Gerrit-Project: wikidata/query/rdf
Gerrit-Branch: master
Gerrit-Owner: Smalyshev <smalys...@wikimedia.org>
Gerrit-Reviewer: Smalyshev <smalys...@wikimedia.org>
Gerrit-Reviewer: jenkins-bot <>

_______________________________________________
MediaWiki-commits mailing list
MediaWiki-commits@lists.wikimedia.org
https://lists.wikimedia.org/mailman/listinfo/mediawiki-commits

Reply via email to