【提问】YIELD 独立语句 和 GROUP 组合 YIELD 子句的区别

如题, 给出例子如下:

FETCH PROP ON person "..." YIELD properties(person).name as name, properties(vertex).age as age
| GROUP BY $-.name YIELD $-.name, max($-.age) as max_age

FETCH PROP ON person "..." YIELD properties(person).name as name, properties(vertex).age as age
| YIELD $-.name, max($-.age) as max_age

同样都起到分组计算的效果, 有什么不同之处? 望解答多谢.

有个猜测
FETCH PROP ON person “…” YIELD properties(person).name as name, properties(vertex).age as age
| YIELD $-.name, max($-.age) as max_age

是不是在Graph服务Planner被优化为 GROUP BY $-.name YIELD …

是的,只是等价语法。原生 ngql 的 yield 子句支持隐式和显式 groupby,其中隐式 groupby 行为与 cypher 标准保持一致,可以参考 cypher-implicit-groupby

2 个赞

希望文档能补充这一点.

YIELD在作为独立语句使用的板块.

1 个赞

哈哈,好问题,你可以 explain 验证你的猜想呢,两者的计划是一样的。

(root@nebula) [basketballplayer]> explain LOOKUP ON player WHERE player.age > 22 YIELD properties(vertex).name AS name, properties(vertex).age AS age| YIELD $-.name, max($-.age) as max_age
Execution succeeded (time spent 1162/6728 us)

Execution Plan (optimize time 514 us)

-----+------------------+--------------+----------------+---------------------------------------
| id | name             | dependencies | profiling data | operator info                        |
-----+------------------+--------------+----------------+---------------------------------------
|  4 | Aggregate        | 3            |                | outputVar: {                         |
|    |                  |              |                |   "colNames": [                      |
|    |                  |              |                |     "$-.name",                       |
|    |                  |              |                |     "max_age"                        |
|    |                  |              |                |   ],                                 |
|    |                  |              |                |   "type": "DATASET",                 |
|    |                  |              |                |   "name": "__Aggregate_4"            |
|    |                  |              |                | }                                    |
|    |                  |              |                | inputVar: __Project_3                |
|    |                  |              |                | groupKeys: [                         |
|    |                  |              |                |   "$-.name"                          |
|    |                  |              |                | ]                                    |
|    |                  |              |                | groupItems: [                        |
|    |                  |              |                |   {                                  |
|    |                  |              |                |     "expr": "$-.name"                |
|    |                  |              |                |   },                                 |
|    |                  |              |                |   {                                  |
|    |                  |              |                |     "expr": "max($-.age)"            |
|    |                  |              |                |   }                                  |
|    |                  |              |                | ]                                    |
-----+------------------+--------------+----------------+---------------------------------------
|  3 | Project          | 2            |                | outputVar: {                         |
|    |                  |              |                |   "colNames": [                      |
|    |                  |              |                |     "name",                          |
|    |                  |              |                |     "age"                            |
|    |                  |              |                |   ],                                 |
|    |                  |              |                |   "type": "DATASET",                 |
|    |                  |              |                |   "name": "__Project_3"              |
|    |                  |              |                | }                                    |
|    |                  |              |                | inputVar: __Filter_2                 |
|    |                  |              |                | columns: [                           |
|    |                  |              |                |   "properties(VERTEX).name AS name", |
|    |                  |              |                |   "properties(VERTEX).age AS age"    |
|    |                  |              |                | ]                                    |
-----+------------------+--------------+----------------+---------------------------------------
|  2 | Filter           | 5            |                | outputVar: {                         |
|    |                  |              |                |   "colNames": [                      |
|    |                  |              |                |     "_vid",                          |
|    |                  |              |                |     "player._tag",                   |
|    |                  |              |                |     "player.age",                    |
|    |                  |              |                |     "player.name"                    |
|    |                  |              |                |   ],                                 |
|    |                  |              |                |   "type": "DATASET",                 |
|    |                  |              |                |   "name": "__Filter_2"               |
|    |                  |              |                | }                                    |
|    |                  |              |                | inputVar: __TagIndexFullScan_1       |
|    |                  |              |                | condition: (player.age>22)           |
|    |                  |              |                | isStable: false                      |
-----+------------------+--------------+----------------+---------------------------------------
|  5 | TagIndexFullScan | 0            |                | outputVar: {                         |
|    |                  |              |                |   "colNames": [                      |
|    |                  |              |                |     "_vid",                          |
|    |                  |              |                |     "player._tag",                   |
|    |                  |              |                |     "player.age",                    |
|    |                  |              |                |     "player.name"                    |
|    |                  |              |                |   ],                                 |
|    |                  |              |                |   "type": "DATASET",                 |
|    |                  |              |                |   "name": "__TagIndexFullScan_1"     |
|    |                  |              |                | }                                    |
|    |                  |              |                | inputVar:                            |
|    |                  |              |                | space: 49                            |
|    |                  |              |                | dedup: false                         |
|    |                  |              |                | limit: 9223372036854775807           |
|    |                  |              |                | filter:                              |
|    |                  |              |                | orderBy: []                          |
|    |                  |              |                | schemaId: 50                         |
|    |                  |              |                | isEdge: false                        |
|    |                  |              |                | returnCols: [                        |
|    |                  |              |                |   "_vid",                            |
|    |                  |              |                |   "_tag",                            |
|    |                  |              |                |   "age",                             |
|    |                  |              |                |   "name"                             |
|    |                  |              |                | ]                                    |
|    |                  |              |                | indexCtx: [                          |
|    |                  |              |                |   {                                  |
|    |                  |              |                |     "columnHints": [],               |
|    |                  |              |                |     "filter": "",                    |
|    |                  |              |                |     "index_id": 54                   |
|    |                  |              |                |   }                                  |
|    |                  |              |                | ]                                    |
-----+------------------+--------------+----------------+---------------------------------------
|  0 | Start            |              |                | outputVar: {                         |
|    |                  |              |                |   "colNames": [],                    |
|    |                  |              |                |   "type": "DATASET",                 |
|    |                  |              |                |   "name": "__Start_0"                |
|    |                  |              |                | }                                    |
-----+------------------+--------------+----------------+---------------------------------------

Tue, 11 Oct 2022 11:41:43 CST

(root@nebula) [basketballplayer]> explain LOOKUP ON player WHERE player.age > 22 YIELD properties(vertex).name AS name, properties(vertex).age AS age| GROUP BY $-.name YIELD $-.name, max($-.age) as max_age
Execution succeeded (time spent 1158/14940 us)

Execution Plan (optimize time 510 us)

-----+------------------+--------------+----------------+---------------------------------------
| id | name             | dependencies | profiling data | operator info                        |
-----+------------------+--------------+----------------+---------------------------------------
|  4 | Aggregate        | 3            |                | outputVar: {                         |
|    |                  |              |                |   "colNames": [                      |
|    |                  |              |                |     "$-.name",                       |
|    |                  |              |                |     "max_age"                        |
|    |                  |              |                |   ],                                 |
|    |                  |              |                |   "type": "DATASET",                 |
|    |                  |              |                |   "name": "__Aggregate_4"            |
|    |                  |              |                | }                                    |
|    |                  |              |                | inputVar: __Project_3                |
|    |                  |              |                | groupKeys: [                         |
|    |                  |              |                |   "$-.name"                          |
|    |                  |              |                | ]                                    |
|    |                  |              |                | groupItems: [                        |
|    |                  |              |                |   {                                  |
|    |                  |              |                |     "expr": "$-.name"                |
|    |                  |              |                |   },                                 |
|    |                  |              |                |   {                                  |
|    |                  |              |                |     "expr": "max($-.age)"            |
|    |                  |              |                |   }                                  |
|    |                  |              |                | ]                                    |
-----+------------------+--------------+----------------+---------------------------------------
|  3 | Project          | 2            |                | outputVar: {                         |
|    |                  |              |                |   "colNames": [                      |
|    |                  |              |                |     "name",                          |
|    |                  |              |                |     "age"                            |
|    |                  |              |                |   ],                                 |
|    |                  |              |                |   "type": "DATASET",                 |
|    |                  |              |                |   "name": "__Project_3"              |
|    |                  |              |                | }                                    |
|    |                  |              |                | inputVar: __Filter_2                 |
|    |                  |              |                | columns: [                           |
|    |                  |              |                |   "properties(VERTEX).name AS name", |
|    |                  |              |                |   "properties(VERTEX).age AS age"    |
|    |                  |              |                | ]                                    |
-----+------------------+--------------+----------------+---------------------------------------
|  2 | Filter           | 5            |                | outputVar: {                         |
|    |                  |              |                |   "colNames": [                      |
|    |                  |              |                |     "_vid",                          |
|    |                  |              |                |     "player._tag",                   |
|    |                  |              |                |     "player.age",                    |
|    |                  |              |                |     "player.name"                    |
|    |                  |              |                |   ],                                 |
|    |                  |              |                |   "type": "DATASET",                 |
|    |                  |              |                |   "name": "__Filter_2"               |
|    |                  |              |                | }                                    |
|    |                  |              |                | inputVar: __TagIndexFullScan_1       |
|    |                  |              |                | condition: (player.age>22)           |
|    |                  |              |                | isStable: false                      |
-----+------------------+--------------+----------------+---------------------------------------
|  5 | TagIndexFullScan | 0            |                | outputVar: {                         |
|    |                  |              |                |   "colNames": [                      |
|    |                  |              |                |     "_vid",                          |
|    |                  |              |                |     "player._tag",                   |
|    |                  |              |                |     "player.age",                    |
|    |                  |              |                |     "player.name"                    |
|    |                  |              |                |   ],                                 |
|    |                  |              |                |   "type": "DATASET",                 |
|    |                  |              |                |   "name": "__TagIndexFullScan_1"     |
|    |                  |              |                | }                                    |
|    |                  |              |                | inputVar:                            |
|    |                  |              |                | space: 49                            |
|    |                  |              |                | dedup: false                         |
|    |                  |              |                | limit: 9223372036854775807           |
|    |                  |              |                | filter:                              |
|    |                  |              |                | orderBy: []                          |
|    |                  |              |                | schemaId: 50                         |
|    |                  |              |                | isEdge: false                        |
|    |                  |              |                | returnCols: [                        |
|    |                  |              |                |   "_vid",                            |
|    |                  |              |                |   "_tag",                            |
|    |                  |              |                |   "age",                             |
|    |                  |              |                |   "name"                             |
|    |                  |              |                | ]                                    |
|    |                  |              |                | indexCtx: [                          |
|    |                  |              |                |   {                                  |
|    |                  |              |                |     "columnHints": [],               |
|    |                  |              |                |     "filter": "",                    |
|    |                  |              |                |     "index_id": 54                   |
|    |                  |              |                |   }                                  |
|    |                  |              |                | ]                                    |
-----+------------------+--------------+----------------+---------------------------------------
|  0 | Start            |              |                | outputVar: {                         |
|    |                  |              |                |   "colNames": [],                    |
|    |                  |              |                |   "type": "DATASET",                 |
|    |                  |              |                |   "name": "__Start_0"                |
|    |                  |              |                | }                                    |
-----+------------------+--------------+----------------+---------------------------------------

Tue, 11 Oct 2022 11:42:11 CST

1 个赞

感谢提供验证思路.

很好的建议,我提了 issue highlight implicit aggregate in docs · Issue #2195 · vesoft-inc/nebula-docs-cn · GitHub

此话题已在最后回复的 7 天后被自动关闭。不再允许新回复。