Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In the match statement, the where condition may generate unnecessary storage queries #3257

Closed
cangfengzhs opened this issue Nov 2, 2021 · 4 comments
Assignees
Labels
type/enhancement Type: make the code neat or more efficient
Milestone

Comments

@cangfengzhs
Copy link
Contributor

cangfengzhs commented Nov 2, 2021

(root@nebula) [test]> create tag index t_name_index on t(name(8));
(root@nebula) [test]> insert tag t(name) values 1:("a");
(root@nebula) [test]> match (v:t) where v.name=="a" return v;  // a
+-------------------+
| v                 |
+-------------------+
| (1 :t{name: "a"}) |
+-------------------+
Got 1 rows (time spent 14400/15023 us)

Tue, 02 Nov 2021 18:31:53 CST

(root@nebula) [test]> match (v:t) where v.name== -2233785415175766016 return v; // b
+---+
| v |
+---+
+---+
Empty set (time spent 14978/15568 us)

Tue, 02 Nov 2021 18:32:00 CST

Statement a and b will generate request to storage and get the same response. Because encode of "a" and -2233785415175766016 is same in indexKey and there is not value type verify.
However, according to the code given above, we find that the result is different. According to the profile statement, a filter node is actually added to the execution plan and filtered again according to the condition.

(root@nebula) [test]> profile match (v:t) where v.name == -2233785415175766016 return v;
+---+
| v |
+---+
+---+
Empty set (time spent 14035/14819 us)

Execution Plan (optimize time 4289 us)

-----+-------------+--------------+-------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------
| id | name        | dependencies | profiling data                                                                                        | operator info                                                                     |
-----+-------------+--------------+-------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------
| 10 | Project     | 13           | ver: 0, rows: 0, execTime: 48us, totalTime: 52us                                                      | outputVar: [                                                                      |
|    |             |              |                                                                                                       |   {                                                                               |
|    |             |              |                                                                                                       |     "colNames": [                                                                 |
|    |             |              |                                                                                                       |       "v"                                                                         |
|    |             |              |                                                                                                       |     ],                                                                            |
|    |             |              |                                                                                                       |     "name": "__Project_10",                                                       |
|    |             |              |                                                                                                       |     "type": "DATASET"                                                             |
|    |             |              |                                                                                                       |   }                                                                               |
|    |             |              |                                                                                                       | ]                                                                                 |
|    |             |              |                                                                                                       | inputVar: __Filter_9                                                              |
|    |             |              |                                                                                                       | columns: [                                                                        |
|    |             |              |                                                                                                       |   "$v"                                                                            |
|    |             |              |                                                                                                       | ]                                                                                 |
-----+-------------+--------------+-------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------
| 13 | Filter      | 7            | ver: 0, rows: 0, execTime: 41us, totalTime: 45us                                                      | outputVar: [                                                                      |
|    |             |              |                                                                                                       |   {                                                                               |
|    |             |              |                                                                                                       |     "colNames": [                                                                 |
|    |             |              |                                                                                                       |       "v",                                                                        |
|    |             |              |                                                                                                       |       "__COL_0"                                                                   |
|    |             |              |                                                                                                       |     ],                                                                            |
|    |             |              |                                                                                                       |     "name": "__Filter_9",                                                         |
|    |             |              |                                                                                                       |     "type": "DATASET"                                                             |
|    |             |              |                                                                                                       |   }                                                                               |
|    |             |              |                                                                                                       | ]                                                                                 |
|    |             |              |                                                                                                       | inputVar: __Project_7                                                             |
|    |             |              |                                                                                                       | condition: (($v.name==-2233785415175766016) AND !(hasSameEdgeInPath($-.__COL_0))) |
|    |             |              |                                                                                                       | isStable: false                                                                   |
-----+-------------+--------------+-------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------
|  7 | Project     | 6            | ver: 0, rows: 1, execTime: 137us, totalTime: 142us                                                    | outputVar: [                                                                      |
|    |             |              |                                                                                                       |   {                                                                               |
|    |             |              |                                                                                                       |     "colNames": [                                                                 |
|    |             |              |                                                                                                       |       "v",                                                                        |
|    |             |              |                                                                                                       |       "__COL_0"                                                                   |
|    |             |              |                                                                                                       |     ],                                                                            |
|    |             |              |                                                                                                       |     "name": "__Project_7",                                                        |
|    |             |              |                                                                                                       |     "type": "DATASET"                                                             |
|    |             |              |                                                                                                       |   }                                                                               |
|    |             |              |                                                                                                       | ]                                                                                 |
|    |             |              |                                                                                                       | inputVar: __Project_6                                                             |
|    |             |              |                                                                                                       | columns: [                                                                        |
|    |             |              |                                                                                                       |   "startNode($-._path) AS v",                                                     |
|    |             |              |                                                                                                       |   "reversePath(PathBuild[$-._path]) AS __COL_0"                                   |
|    |             |              |                                                                                                       | ]                                                                                 |
-----+-------------+--------------+-------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------
|  6 | Project     | 5            | ver: 0, rows: 1, execTime: 109us, totalTime: 115us                                                    | outputVar: [                                                                      |
|    |             |              |                                                                                                       |   {                                                                               |
|    |             |              |                                                                                                       |     "colNames": [                                                                 |
|    |             |              |                                                                                                       |       "_path"                                                                     |
|    |             |              |                                                                                                       |     ],                                                                            |
|    |             |              |                                                                                                       |     "name": "__Project_6",                                                        |
|    |             |              |                                                                                                       |     "type": "DATASET"                                                             |
|    |             |              |                                                                                                       |   }                                                                               |
|    |             |              |                                                                                                       | ]                                                                                 |
|    |             |              |                                                                                                       | inputVar: __Filter_5                                                              |
|    |             |              |                                                                                                       | columns: [                                                                        |
|    |             |              |                                                                                                       |   "PathBuild[VERTEX]"                                                             |
|    |             |              |                                                                                                       | ]                                                                                 |
-----+-------------+--------------+-------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------
|  5 | Filter      | 16           | ver: 0, **rows: 1,** execTime: 145us, totalTime: 151us                                                    | outputVar: [                                                                      |
|    |             |              |                                                                                                       |   {                                                                               |
|    |             |              |                                                                                                       |     "colNames": [],                                                               |
|    |             |              |                                                                                                       |     "name": "__Filter_5",                                                         |
|    |             |              |                                                                                                       |     "type": "DATASET"                                                             |
|    |             |              |                                                                                                       |   }                                                                               |
|    |             |              |                                                                                                       | ]                                                                                 |
|    |             |              |                                                                                                       | inputVar: __GetVertices_4                                                         |
|    |             |              |                                                                                                       | condition: ("t" IN tags(VERTEX))                                                  |
|    |             |              |                                                                                                       | isStable: false                                                                   |
-----+-------------+--------------+-------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------
| 16 | GetVertices | 11           | {                                                                                                     | outputVar: [                                                                      |
|    |             |              | ver: 0, **rows: 1**, execTime: 351us, totalTime: 1296us                                                   |   {                                                                               |
|    |             |              | "127.0.0.1":9381 exec/total: 290(us)/867(us)                                                          |     "colNames": [],                                                               |
|    |             |              | total_rpc: 1168(us)                                                                                   |     "name": "__GetVertices_4",                                                    |
|    |             |              | }                                                                                                     |     "type": "DATASET"                                                             |
|    |             |              |                                                                                                       |   }                                                                               |
|    |             |              |                                                                                                       | ]                                                                                 |
|    |             |              |                                                                                                       | inputVar: __IndexScan_1                                                           |
|    |             |              |                                                                                                       | space: 4                                                                          |
|    |             |              |                                                                                                       | dedup: true                                                                       |
|    |             |              |                                                                                                       | limit: 9223372036854775807                                                        |
|    |             |              |                                                                                                       | filter:                                                                           |
|    |             |              |                                                                                                       | orderBy: []                                                                       |
|    |             |              |                                                                                                       | src: $_vid                                                                        |
|    |             |              |                                                                                                       | props: [                                                                          |
|    |             |              |                                                                                                       |   {                                                                               |
|    |             |              |                                                                                                       |     "tagId": 5,                                                                   |
|    |             |              |                                                                                                       |     "props": [                                                                    |
|    |             |              |                                                                                                       |       "name",                                                                     |
|    |             |              |                                                                                                       |       "_tag"                                                                      |
|    |             |              |                                                                                                       |     ]                                                                             |
|    |             |              |                                                                                                       |   }                                                                               |
|    |             |              |                                                                                                       | ]                                                                                 |
|    |             |              |                                                                                                       | exprs:                                                                            |
-----+-------------+--------------+-------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------
| 11 | IndexScan   | 0            | {                                                                                                     | outputVar: [                                                                      |
|    |             |              | ver: 0, **rows: 1,** execTime: 0us, totalTime: 5964us                                                     |   {                                                                               |
|    |             |              | storage_detail: {DedupNode:4026(us),IndexOpuputNode:3906(us),IndexScanNode:3654(us),RelNode:4047(us)} |     "colNames": [                                                                 |
|    |             |              | "127.0.0.1":9381 exec/total: 4611(us)/5573(us)                                                        |       "_vid"                                                                      |
|    |             |              | }                                                                                                     |     ],                                                                            |
|    |             |              |                                                                                                       |     "name": "__IndexScan_1",                                                      |
|    |             |              |                                                                                                       |     "type": "DATASET"                                                             |
|    |             |              |                                                                                                       |   }                                                                               |
|    |             |              |                                                                                                       | ]                                                                                 |
|    |             |              |                                                                                                       | inputVar: __VAR_0                                                                 |
|    |             |              |                                                                                                       | space: 4                                                                          |
|    |             |              |                                                                                                       | dedup: false                                                                      |
|    |             |              |                                                                                                       | limit: 9223372036854775807                                                        |
|    |             |              |                                                                                                       | filter:                                                                           |
|    |             |              |                                                                                                       | orderBy: []                                                                       |
|    |             |              |                                                                                                       | schemaId: 5                                                                       |
|    |             |              |                                                                                                       | isEdge: false                                                                     |
|    |             |              |                                                                                                       | returnCols: [                                                                     |
|    |             |              |                                                                                                       |   "_vid"                                                                          |
|    |             |              |                                                                                                       | ]                                                                                 |
|    |             |              |                                                                                                       | indexCtx: [                                                                       |
|    |             |              |                                                                                                       |   {                                                                               |
|    |             |              |                                                                                                       |     "columnHints": [                                                              |
|    |             |              |                                                                                                       |       {                                                                           |
|    |             |              |                                                                                                       |         "beginValue": "-2233785415175766016",                                     |
|    |             |              |                                                                                                       |         "endValue": "__EMPTY__",                                                  |
|    |             |              |                                                                                                       |         "column": "name",                                                         |
|    |             |              |                                                                                                       |         "scanType": "PREFIX"                                                      |
|    |             |              |                                                                                                       |       }                                                                           |
|    |             |              |                                                                                                       |     ],                                                                            |
|    |             |              |                                                                                                       |     "index_id": 6,                                                                |
|    |             |              |                                                                                                       |     "filter": ""                                                                  |
|    |             |              |                                                                                                       |   }                                                                               |
|    |             |              |                                                                                                       | ]                                                                                 |
-----+-------------+--------------+-------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------
|  0 | Start       |              | ver: 0, rows: 0, execTime: 3us, totalTime: 27us                                                       | outputVar: [                                                                      |
|    |             |              |                                                                                                       |   {                                                                               |
|    |             |              |                                                                                                       |     "colNames": [],                                                               |
|    |             |              |                                                                                                       |     "name": "__Start_0",                                                          |
|    |             |              |                                                                                                       |     "type": "DATASET"                                                             |
|    |             |              |                                                                                                       |   }                                                                               |
|    |             |              |                                                                                                       | ]                                                                                 |
-----+-------------+--------------+-------------------------------------------------------------------------------------------------------+------------------------------------------------------------------------------------

Tue, 02 Nov 2021 18:34:42 CST

So, there are some questions

  1. In the match statement, we can compare values between different types (only ==). But there is no type conversion, so the result of the expression is always false. If we want to support the comparison of weak data types, like sql, "1"==1 is true, then our current logic is not correct. If we need a comparison of strong data types, then "1"==1 will always return false, which is meaningless. The query results can be known during query optimization.The current processing logic will only waste CPU resources
  2. If we want to implement the comparison of weak data types, then it should not be limited to ==, it should also support comparison operations such as <, >, but currently it is not supported.
  3. In this case, the expressions are all used for index optimization, so it is not necessary to perform another filter after selecting the index (if the logic we implemented is correct)
@cangfengzhs cangfengzhs added the type/enhancement Type: make the code neat or more efficient label Nov 2, 2021
@cangfengzhs
Copy link
Contributor Author

This looks like a few errors superimposed together and ultimately ensure the correct query results.

@czpmango
Copy link
Contributor

If we want to support the comparison of weak data types, like sql, "1"==1 is true, then our current logic is not correct.

"1"==1 is true in sql but false in cypher. Our current logic of expression is consistent with cypher and the index logic related should be consistent. IMHO, This is just the behavior of the expression, and the implementation of other modules should be delegated to it. More detail please refer to cypher standand.

@Shylock-Hg
Copy link
Contributor

In my mind, index scan which transformed from filter expression should keep the origin semantic of expression.

@Sophie-Xie Sophie-Xie added this to the v3.1.0 milestone Jan 26, 2022
@Sophie-Xie Sophie-Xie moved this from Backlog to Todo in Nebula Graph v3.1.0 Feb 17, 2022
@Sophie-Xie Sophie-Xie assigned nevermore3 and unassigned jackwener Feb 18, 2022
@Sophie-Xie Sophie-Xie moved this from Todo to Backlog in Nebula Graph v3.1.0 Feb 19, 2022
@CPWstatic
Copy link
Contributor

This would not be fixed at present due to the syntax matter of openCypher.

Repository owner moved this from Backlog to Done in Nebula Graph v3.1.0 Mar 24, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
type/enhancement Type: make the code neat or more efficient
Projects
No open projects
Status: Done
Development

No branches or pull requests

7 participants