結構定義規定
Spanner Graph 演算法可在符合下列規定的結構定義圖表上執行:
- 邊緣輸入資料表必須交錯插入來源節點輸入資料表,並具有指向目的地節點輸入資料表的外部鍵或資訊外部鍵。請參閱設計 Spanner Graph 結構定義的最佳做法。
- 圖表演算法的每個輸入資料表都必須有專屬的識別標籤。
以下是不支援的結構定義範例:
-- Unsupported: Both `Person` and `Account` can only be referred to via label `Entity`.
CREATE PROPERTY GRAPH FinGraph
NODE TABLES (
Person KEY (id)
LABEL Entity PROPERTIES (...),
Account KEY (id)
LABEL Entity PROPERTIES (...)
);
以下是支援的結構定義範例:
-- Supported: Even though label `Entity` refers to both `Person` and `Account`,
-- label `Customer` uniquely identifies `Person`, label `Account` uniquely identifies `Account`.
CREATE PROPERTY GRAPH FinGraph
NODE TABLES (
Person KEY (id)
LABEL Customer PROPERTIES (...)
LABEL Entity PROPERTIES (...),
Account KEY (id)
LABEL Account PROPERTIES (...)
LABEL Entity PROPERTIES (...)
);
功能相容性
本節將說明演算法查詢與一般圖表功能的相容性。
支援的 MATCH 子句
在演算法查詢結構中使用 MATCH 子句定義演算法的輸入圖表時,請務必考量下列事項:
- 每個
MATCH只能包含一個元素模式 (節點或邊緣),以及一個具名變數。 - 如果邊緣模式包含端點的明確模式,請勿在節點模式中使用節點標籤或變數。
- 如果元素模式未參照其他位置定義的變數,您可以在元素模式中使用
WHERE子句。 MATCH子句中的標籤運算式必須可識別單一元素資料表。
以下範例說明支援的 MATCH 子句:
-- Supported: Assuming only 1 element table has `Account` label:
MATCH (a:Account)
MATCH (a:Account WHERE a.id > 400)
MATCH (a:Account WHERE a.id IN [101, 102, 105])
MATCH -[e:Transfers WHERE e.amount < 500]->
以下範例顯示不支援的 MATCH 子句:
-- Unsupported: End nodes for edge patterns cannot have labels or named variables.
MATCH (a:Account)-[]->
-- Unsupported: Each MATCH can only name one variable.
MATCH (a:Account)-[e:Transfers]->
-- Unsupported: If there are multiple node element tables in the graph, each MATCH
-- must uniquely identify one element table.
MATCH (a)
-- Unsupported: The `Entity` label maps to both `Account` and `Person` node element tables.
MATCH (a:Account | Entity)
使用 RETURN 子句
演算法查詢結構中的 RETURN 子句會定義演算法查詢傳回的資料。您可以在 RETURN 子句中參照演算法輸出簽章中的資料欄,但須遵守下列限制:
- 如果演算法輸出 Node,您可以傳回節點的任何屬性。你也可以傳回節點的 ELEMENT_DEFINITION_NAME。
- 如果演算法輸出 Path,您可以傳回
PATH_LENGTH(path). - 您可以從演算法傳回任何純量輸出。
- 您可以傳回任何常值常數。
以下範例說明支援的 RETURN 子句:
-- Supported.
CALL PageRank(...) YIELD node, score
RETURN ELEMENT_DEFINITION_NAME(node) AS node_type, node.id, score
-- Supported: You can return constants.
CALL PageRank(...) YIELD node, score
RETURN node.id, score, "pagerank-run1" AS algo_run_id
-- Supported.
CALL ShortestPath(...) YIELD source_node, target_node, path, cost
RETURN source_node.id AS source_id, target_node.id AS target_id,
PATH_LENGTH(path) AS length, cost
以下範例說明不支援的 RETURN 子句:
-- Unsupported: You cannot return the graph element `node` directly.
RETURN node, score
-- Unsupported: You can only return properties of `node`. Applying a function to a node property
-- is not supported.
RETURN node.id + 1, score
-- Unsupported: You can only return the scalar `score` directly from the algorithm output. Applying
-- a function to scalar output is not supported.
RETURN node.id, score + 1
-- Unsupported: General function calls are not allowed.
RETURN JSON_OBJECT(node.id, score) as json_obj
不支援參數
演算法查詢不支援查詢參數。
支援從檢視畫面建立的圖表
在檢視區塊上建立圖表時,只有在定義演算法輸入內容的節點和邊緣 (包括邊緣的結束節點) 不是以檢視區塊為基礎時,系統才會支援演算法查詢。