Instantly share code, notes, and snippets.

# wey-gu/nebula-graph-pitfalls-to-avoid.md

Last active May 26, 2023 10:09
Star You must be signed in to star a gist
The pitfalls that we should avoid in Nebula Graph. 使用 Nebula Graph 时候我们要注意的陷阱

TOC

## Condition expressions on multiple hops in MATCH

https://discuss.nebula-graph.com.cn/t/topic/6625/5

``````nebula> MATCH p=(v:player{name:"Tim Duncan"})-[e:follow*2]->(v2) \
WHERE e.degree > 1 \
RETURN DISTINCT v2 AS Friends;
``````

``````nebula> MATCH p=(v:player{name:"Tim Duncan"})-[e:follow*2]->(v2) \
WHERE ALL(e_ in e WHERE e_.degree > 0) \
RETURN DISTINCT v2 AS Friends;
``````

``````nebula> MATCH p=(v:player{name:"Tim Duncan"})-[e:follow*2]->(v2) \
WHERE e[0].degree > 98 \
RETURN DISTINCT v2 AS Friends;
``````

``````(root@nebula) [basketballplayer]> match (n:player)-[e:follow{degree:95}*3]-(v) return n
[ERROR (-1004)]: SyntaxError: syntax error near `*3]-(v) '

Fri, 26 Nov 2021 17:11:34 CST

(root@nebula) [basketballplayer]> match (n:player)-[e:follow*3{degree:95}]-(v) return n
+-------------------------------------------------------+
| n                                                     |
+-------------------------------------------------------+
| ("player100" :player{age: 42, name: "Tim Duncan"})    |
+-------------------------------------------------------+
| ("player125" :player{age: 41, name: "Manu Ginobili"}) |
+-------------------------------------------------------+
``````

## FIND PATH

https://discuss.nebula-graph.com.cn/t/topic/6636

## Use MATCH to get STATS

We shoud use STATS JOB instead.

Wrong:

``````MATCH (v:player) return COUNT(v)
``````

We should use:

``````SUBMIT JOB STATS;
# Wait for its being finished

SHOW STATS;
+---------+------------+-------+
| Type    | Name       | Count |
+---------+------------+-------+
| "Tag"   | "player"   | 51    |
| "Tag"   | "team"     | 30    |
| "Edge"  | "follow"   | 81    |
| "Edge"  | "serve"    | 152   |
| "Space" | "vertices" | 81    |
| "Space" | "edges"    | 233   |
+---------+------------+-------+
``````

## Important reference variable and functions you could miss!

How to describe wildcard edge properties?

``````(root@nebula) [basketballplayer]> go from "player100" over * YIELD properties(edge)
+------------------------------------+
| properties(EDGE)                   |
+------------------------------------+
| {end_year: 2016, start_year: 1997} |
+------------------------------------+
| {degree: 95}                       |
+------------------------------------+
| {degree: 95}                       |
+------------------------------------+
Got 3 rows (time spent 1379/50293 us)

Fri, 26 Nov 2021 12:12:44 CST

(root@nebula) [basketballplayer]> go from "player100" over * WHERE properties(edge).degree > 1 YIELD properties(edge)
+------------------+
| properties(EDGE) |
+------------------+
| {degree: 95}     |
+------------------+
| {degree: 95}     |
+------------------+
Got 2 rows (time spent 1518/35277 us)

Fri, 26 Nov 2021 12:13:13 CST
``````

## Escaping key words

i.e. `User` is a keyword, we should quote it with `.

``````(root@nebula) [amundsen]> MATCH (u:User) WHERE id(u) == "" RETURN u
[ERROR (-1009)]: SemanticError: `user': Unknown tag

``````
``````(root@nebula) [amundsen]> MATCH (u:`User`) WHERE id(u) == "" RETURN u
+---+
| u |
+---+
+---+
Empty set (time spent 3313/9641 us)

``````

## RETURN values should be explicitly specified in latest `WITH` Clause

TBD

### wey-gu commented Aug 16, 2022

Get dst/src in bidirect traversal

```-- src/dst function honers the actual src or dst of edge, while \$\$ doesn't

GO FROM "player100" OVER * bidirect YIELD src(edge) as vid | fetch prop on * \$-.vid YIELD properties(vertex);

-- OR do in this way by checking ._type value
GO FROM "player100" OVER * bidirect YIELD edge as e , CASE (follow._type IS NOT NULL AND follow._type > 0) OR (serve._type IS NOT NULL AND serve._type > 0) WHEN true THEN properties(\$^) ELSE properties(\$\$) END AS src```

### wey-gu commented Jan 13, 2023

query all data and render in the studio w/o any indexes

```MATCH (n) WITH n LIMIT 1000
OPTIONAL MATCH p=(n)--()
RETURN p, n```

or

```MATCH (n) WITH n LIMIT 1000
OPTIONAL MATCH (n)-[e]-(m)
RETURN n,e,m```

### wey-gu commented Apr 13, 2023

FIND PATH that with one certain vertex:

```FIND ALL PATH FROM "player100" TO "player101" OVER * BIDIRECT YIELD path as p |
YIELD \$-.p WHERE "player104" IN [n in nodes(\$-.p)| id(n)]```

### wey-gu commented May 26, 2023

Query block subgraphs

```MATCH (startNode)-[e*..2]-(connectedNode)
WHERE id(startNode) IN ["player100", "player101"]
WITH startNode, collect(connectedNode) AS connectedNodes, e AS connectedEdges, rand32() AS block
RETURN block, startNode + connectedNodes AS connectedNodes, connectedEdges```