-
Notifications
You must be signed in to change notification settings - Fork 64
Expand file tree
/
Copy pathexample_3.bql
More file actions
107 lines (95 loc) · 3.45 KB
/
example_3.bql
File metadata and controls
107 lines (95 loc) · 3.45 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
# Copyright 2016 Google Inc. All rights reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# Create graphs.
CREATE GRAPH ?family, ?info;
# Insert some data into the ?family graph.
INSERT DATA INTO ?family {
/u<joe> "parent_of"@[] /u<mary> .
/u<joe> "parent_of"@[] /u<peter> .
/u<peter> "parent_of"@[] /u<eve> .
/u<peter> "parent_of"@[] /u<peter> .
/u<mary> "parent_of"@[] /u<john> .
/u<mary> "parent_of"@[] /u<anne> .
/u<mary> "parent_of"@[] /u<amy>
};
# Insert some data into the ?info graph.
INSERT DATA INTO ?info {
/u<joe> "bought"@[2016-01-01T00:00:00-08:00] /car<model x> .
/u<mary> "bought"@[2018-01-01T00:00:00-08:00] /car<model y> .
/u<joe> "bought"@[2020-01-01T00:00:00-08:00] /car<model z> .
/u<bob> "bought"@[2021-01-01T00:00:00-08:00] /car<model x> .
/u<joe> "height_cm"@[] "172"^^type:int64 .
/u<mary> "height_cm"@[] "174"^^type:int64 .
/u<bob> "height_cm"@[] "168"^^type:int64 .
/u<joe> "born_in"@[] /city<paris>
};
# Does any of Joe's grandchildren have the same name of his parent?
SELECT ?offspring, ?grandchildren
FROM ?family
WHERE {
/u<joe> AS ?grandparent ID ?grandparent_name "parent_of"@[] ?offspring .
?offspring "parent_of"@[] ?grandchildren
}
HAVING ?offspring = ?grandchildren;
# Who are Joe's grandchildren that do not have the same name of his parent?
SELECT ?offspring, ?grandchildren
FROM ?family
WHERE {
/u<joe> AS ?grandparent ID ?grandparent_name "parent_of"@[] ?offspring .
?offspring "parent_of"@[] ?grandchildren
}
HAVING NOT(?offspring = ?grandchildren);
# Which parents have name coming lexicographically after "joe"?
SELECT ?parent_name
FROM ?family
WHERE {
?parent ID ?parent_name "parent_of"@[] ?child
}
GROUP BY ?parent_name
HAVING ?parent_name > "joe"^^type:text;
# Who has height smaller than 173 cm?
SELECT ?person, ?height
FROM ?info
WHERE {
?person "height_cm"@[] ?height
}
HAVING ?height < "173"^^type:int64;
# Who has ever bought a car "model y" or "model z"?
SELECT ?person
FROM ?info
WHERE {
?person "bought"@[,] ?product
}
HAVING (?product = /car<model y>) OR (?product = /car<model z>);
# What are the triples in the ?info graph having either "height_cm"@[]
# or "born_in"@[] as predicate?
SELECT ?subj, ?pred, ?obj
FROM ?info
WHERE {
?subj ?pred ?obj
}
HAVING (?pred = "height_cm"@[]) OR (?pred = "born_in"@[]);
# N.B. for nodes and predicates you are allowed to use equalities inside the
# HAVING clause (as done in the two examples above), but if you use inequalities
# there for them you will receive an error prompting you to extract ID, TYPE or AT
# bindings and use those bindings to do these comparisons in a more explicit way.
# Who bought something before 2020?
SELECT ?person, ?time
FROM ?info
WHERE {
?person "bought"@[?time] ?product
}
HAVING ?time < 2020-01-01T00:00:00-08:00;
# Drop graphs.
DROP GRAPH ?family, ?info;