AnzoGraph中关系Double属性过滤异常问题求助
问题
开发应用时需要从AnzoGraph中获取带属性的关系数据,通过过滤关系上的Double类型属性筛选数据(部分关系可能无该属性),使用OPTIONAL查询时遇到异常:
数据准备步骤
- 插入两个顶点:
PREFIX ex: <http://example.org/> INSERT DATA { GRAPH <http://anzograph.com/graphN> { ex:obj1 a ex:Object . ex:obj1 ex:Name "obj1"^^xsd:string . ex:obj1 ex:s_id "18"^^xsd:integer . } } PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX ex: <http://example.org/> INSERT DATA { GRAPH <http://anzograph.com/graphN> { ex:obj2 a ex:Object . ex:obj2 ex:Name "obj2"^^xsd:string . ex:obj2 ex:s_id "19"^^xsd:integer . } }
- 创建带属性的关系:
PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX ex: <http://example.org/> INSERT DATA { GRAPH <http://anzograph.com/graphN> { <<<ex:obj1> <edgeSample> <ex:obj2>>> ex:Name "edgeobj1"^^xsd:string . <<<ex:obj1> <edgeSample> <ex:obj2>>> ex:Double "47.1234"^^xsd:double . <<<ex:obj1> <edgeSample> <ex:obj2>>> ex:Int "123"^^xsd:int . <<<ex:obj1> <edgeSample> <ex:obj2>>> ex:Name "41"^^xsd:float . } }
异常场景
场景1:直接过滤Double属性
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX ex: <http://example.org/> SELECT ?start ?end (IRI(<edgeSample>) AS ?labels) ?attr1 ?attr2 ?attr3 ?attr4 FROM <http://anzograph.com/graphN> WHERE { ?start <edgeSample> ?end . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Name ?attr1 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Double ?attr2 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Int ?attr3 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Name ?attr4 } . FILTER(?attr2 = "47.1234"^^xsd:double) }
错误输出:m_type == row->a_field_type(m_colnum):Invalid system state type mismatch for var(?attr2) vartype(19): rowtype(4).
场景2:使用str()函数过滤
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX ex: <http://example.org/> SELECT ?start ?end (IRI(<edgeSample>) AS ?labels) ?attr1 ?attr2 ?attr3 ?attr4 FROM <http://anzograph.com/graphN> WHERE { ?start <edgeSample> ?end . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Name ?attr1 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Double ?attr2 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Int ?attr3 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Name ?attr4 } . FILTER(STR(?attr2) = "47.1234") }
输出:无查询结果
询问如何正确查询带有Double类型属性(该属性可能仅存在于部分关系)的关系数据。
解决方案
方法1:将过滤条件嵌入OPTIONAL块
把针对?attr2的FILTER直接放到对应的OPTIONAL子句中,仅当ex:Double属性存在时执行过滤,避免类型不匹配问题:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX ex: <http://example.org/> SELECT ?start ?end (IRI(<edgeSample>) AS ?labels) ?attr1 ?attr2 ?attr3 ?attr4 FROM <http://anzograph.com/graphN> WHERE { ?start <edgeSample> ?end . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Name ?attr1 } . OPTIONAL{ << ?start <edgeSample> ?end >> ex:Double ?attr2 . FILTER(?attr2 = "47.1234"^^xsd:double) } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Int ?attr3 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Name ?attr4 } . }
方法2:使用EXISTS判断属性存在性后过滤
先通过EXISTS确认关系存在ex:Double属性,再进行值过滤,同时保留其他OPTIONAL属性的查询:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX ex: <http://example.org/> SELECT ?start ?end (IRI(<edgeSample>) AS ?labels) ?attr1 ?attr2 ?attr3 ?attr4 FROM <http://anzograph.com/graphN> WHERE { ?start <edgeSample> ?end . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Name ?attr1 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Double ?attr2 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Int ?attr3 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Name ?attr4 } . EXISTS { << ?start <edgeSample> ?end >> ex:Double ?temp } FILTER(?attr2 = "47.1234"^^xsd:double) }
方法3:处理类型转换(针对场景2优化)
场景2无结果是因为AnzoGraph中Double类型的字符串表示可能为科学计数法格式(如47.1234e0),转换回Double类型再比较即可解决:
PREFIX rdf: <http://www.w3.org/1999/02/22-rdf-syntax-ns#> PREFIX xsd: <http://www.w3.org/2001/XMLSchema#> PREFIX ex: <http://example.org/> SELECT ?start ?end (IRI(<edgeSample>) AS ?labels) ?attr1 ?attr2 ?attr3 ?attr4 FROM <http://anzograph.com/graphN> WHERE { ?start <edgeSample> ?end . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Name ?attr1 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Double ?attr2 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Int ?attr3 } . OPTIONAL{<< ?start <edgeSample> ?end >> ex:Name ?attr4 } . FILTER(xsd:double(STR(?attr2)) = "47.1234"^^xsd:double) }
核心说明
- 场景1的错误源于OPTIONAL变量
?attr2可能为null,AnzoGraph无法确定其类型,直接进行类型匹配会触发类型不匹配错误。将过滤嵌入OPTIONAL后,仅在属性存在时执行过滤,避免了null值的类型问题。 - 场景2无结果是因为Double类型的字符串序列化形式与预期的
"47.1234"不一致,转换回Double类型再比较即可匹配。
内容的提问来源于stack exchange,提问作者Arpit Dongaonkar
相关产品推荐
相关产品推荐

