Pular para o conteúdo principal

GSQL x Query using Context Meta-information (Edges Properties)

Testes no ambiente de Cloud do TigerGraph -> https://tgcloud.io/app/solutions

Usando o python notebook no Google Collab -> https://colab.research.google.com/drive/1vCfaTCO5lCxyMswqC8sukty0_OwrBqjP?usp=sharing

Passos

Define and Publish the Schema
Contexto temporal definido pelo atributo date_of_start

# DEFINE / CREATE ALL EDGES AND VERTICES

  USE GLOBAL
  DROP ALL
  CREATE VERTEX Person (PRIMARY_ID id STRING, name STRING, email STRING, username STRING, created_at DATETIME) WITH primary_id_as_attribute="true"
  CREATE VERTEX Country (PRIMARY_ID id STRING, name STRING, language STRING, continent STRING, population INT ) WITH primary_id_as_attribute="true"
  CREATE DIRECTED EDGE WORKING_IN (From Person, To Country, date_of_start DATETIME) WITH REVERSE_EDGE="reverse_working"
  CREATE DIRECTED EDGE LIVING_IN (From Person, To Country, date_of_start DATETIME) WITH REVERSE_EDGE="reverse_living"
  CREATE DIRECTED EDGE FRIENDS_WITH (From Person, To Person, date_of_start DATETIME) WITH REVERSE_EDGE="reverse_friends"

Create the Graph

DROP GRAPH MyGraph
CREATE GRAPH MyGraph(Person, Country, WORKING_IN, LIVING_IN, FRIENDS_WITH, reverse_working, reverse_living, reverse_friends )

Loading Data
Countries

  USE GRAPH MyGraph
  DROP QUERY addCountry
  CREATE QUERY addCountry() FOR GRAPH MyGraph {
    INSERT INTO Country VALUES ("DEU", "Germany", "German", "Europe", 83000000 );
    INSERT INTO Country VALUES ("FRA", "France", "French", "Europe", 67000000 );
    INSERT INTO Country VALUES ("GBR", "United Kingdom", "English", "Europe", 66000000 );
  }
  INTERPRET QUERY addCountry()

Person

  USE GRAPH MyGraph
  DROP QUERY addPerson
  CREATE QUERY addPerson() FOR GRAPH MyGraph {
    INSERT INTO Person VALUES ("John", "John",_,_,_ );
    INSERT INTO Person VALUES ("Harry", "Harry",_,_,_ );
    INSERT INTO Person VALUES ("Anna", "Anna", _,_,_ );
  }
  INTERPRET QUERY addPerson()

Relationships WORKING_IN, LIVING_IN, FRIENDS_WITH

  USE GRAPH MyGraph
  DROP QUERY addRelationships
  CREATE QUERY addRelationships() FOR GRAPH MyGraph {  

    INSERT INTO LIVING_IN VALUES("John", "DEU", to_datetime("2014-01-01"));
    INSERT INTO LIVING_IN VALUES("Harry", "GBR", to_datetime("2013-01-01"));
    INSERT INTO LIVING_IN VALUES("Anna", "DEU", to_datetime("2014-01-01"));
    INSERT INTO LIVING_IN VALUES("Anna", "GBR", to_datetime("2016-01-01"));
    INSERT INTO WORKING_IN VALUES("John", "FRA", to_datetime("2014-01-01"));
    INSERT INTO WORKING_IN VALUES("Harry", "GBR", to_datetime("2014-01-01"));

    INSERT INTO FRIENDS_WITH VALUES("John", "Harry", to_datetime("2011-01-01"));
    INSERT INTO FRIENDS_WITH VALUES("Anna", "John", to_datetime("2012-01-01"));
    INSERT INTO FRIENDS_WITH VALUES("Anna", "Harry", to_datetime("2014-01-01"));

  }
  INTERPRET QUERY addRelationships()


Queries
Queries using start_date

Using SYNTAX V2 for multi-hop
BUT
(SEM-5200): line 46, col 4 Disconnected patterns not yet supported: alias(es) f1 disconnected from rest of pattern

https://dev.tigergraph.com/forum/t/create-query-returns-null/1833

  USE GRAPH MyGraph
  CREATE QUERY start_date_qualifier() FOR GRAPH MyGraph  SYNTAX v2 {
   
    SetAccum<EDGE> @@edges;
    SetAccum<VERTEX> @@source;
    SetAccum<VERTEX> @@target;
 
    PRINT "first query!";
 
    R1 = SELECT p   
    FROM Person:p -((LIVING_IN> | WORKING_IN>):r) -Country:c
    WHERE c.name == "United Kingdom" AND r.date_of_start == to_datetime("2014-01-01")
    ACCUM @@edges += r;
 
    PRINT @@edges;
    reset_collection_accum(@@edges);

    PRINT "second query!";
 
    R2 = SELECT p
    FROM Person:p -(FRIENDS_WITH>:r) -Person:f
    WHERE r.date_of_start > to_datetime("2010-01-01")
    ACCUM @@edges += r, @@source += p, @@target += f;
 
    PRINT @@edges, @@source, @@target;
    reset_collection_accum(@@edges);
    reset_collection_accum(@@source);
    reset_collection_accum(@@target);

    PRINT "third query!";
 
    R3 = SELECT p
    FROM Person:p -(FRIENDS_WITH>:r1) -Person:f1- (FRIENDS_WITH>:r2) -Person:f2
    WHERE r2.date_of_start > r1.date_of_start
    ACCUM @@edges += r2, @@source += p, @@target += f2;
    
    PRINT @@edges, @@source, @@target;
    reset_collection_accum(@@edges);
    reset_collection_accum(@@source);
    reset_collection_accum(@@target);
 
    PRINT "fourth query Failed!";
 
#    R4 = SELECT f1
#    FROM Person:f1- (FRIENDS_WITH>:r1) -Person:f3 , Person:f2- (FRIENDS_WITH>:r2) -Person:f4
#    WHERE r2.date_of_start == r1.date_of_start
#    ACCUM @@edges += r2, @@edges += r1, @@source += f1, @@source += f2, @@target += f3, @@target += f4;
    
    PRINT @@edges, @@source, @@target;
    reset_collection_accum(@@edges);
    reset_collection_accum(@@source);
    reset_collection_accum(@@target);
 
    PRINT "fifth query Failed!";
 
#    R5 = SELECT f1
#    FROM Person:f1- (LIVING_IN>:r1) -Country:c1 , Person:f2- (LIVING_IN>:r2) -Country:c2
#    WHERE r2.date_of_start == r1.date_of_start
#    ACCUM @@edges += r2, @@edges += r1, @@source += f1, @@source += f2, @@target += c1, @@target += c2;
    
    PRINT @@edges, @@source, @@target;
    reset_collection_accum(@@edges);
    reset_collection_accum(@@source);
    reset_collection_accum(@@target);
 
  PRINT "start_date_qual works!";
  }

  INSTALL QUERY start_date_qualifier

conn.runInstalledQuery("start_date_qualifier")

  CREATE QUERY start_date_qualifier_tab() FOR GRAPH MyGraph  SYNTAX v2 {
   
    PRINT "first query!";
 
    SELECT p, r, c into R1   
    FROM Person:p -((LIVING_IN> | WORKING_IN>):r) -Country:c
    WHERE c.name == "United Kingdom" AND r.date_of_start == to_datetime("2014-01-01");
 
    PRINT R1;

    PRINT "second query!";
 
    SELECT p, r, f into R2
    FROM Person:p -(FRIENDS_WITH>:r) -Person:f
    WHERE r.date_of_start > to_datetime("2010-01-01");
 
    PRINT R2;

    PRINT "third query!";
 
    SELECT p, r1, f1, r2, f2 into R3
    FROM Person:p -(FRIENDS_WITH>:r1) -Person:f1- (FRIENDS_WITH>:r2) -Person:f2
    WHERE r2.date_of_start > r1.date_of_start;
    
    PRINT R3;

    PRINT "fourth query Failed!";
 
#    SELECT f1, r1, f3, f2, r2, f4 into R4
#    FROM Person:f1- (FRIENDS_WITH>:r1) -Person:f3 , Person:f2- (FRIENDS_WITH>:r2) -Person:f4
#    WHERE r2.date_of_start == r1.date_of_start;

#   PRINT R4;
 
    PRINT "fifth query Failed!";
 
#    SELECT f1, r1, c1,f2,r2, c2 into R5
#    FROM Person:f1- (LIVING_IN>:r1) -Country:c1 , Person:f2- (LIVING_IN>:r2) -Country:c2
#    WHERE r2.date_of_start == r1.date_of_start;

#    PRINT R5;
 
  PRINT "start_date_qual_tab works!";
  }

  INSTALL QUERY start_date_qualifier_tab

conn.runInstalledQuery("start_date_qualifier_tab")

GSQL Path (10, 37) Error: Kleene star pattern can't have an alias.

  USE GRAPH MyGraph
  CREATE QUERY start_date_qualifier_path(/* Parameters here */) FOR GRAPH MyGraph  SYNTAX v2 {
   
    SetAccum<EDGE> @@edges;
    SetAccum<VERTEX> @@source;
    SetAccum<VERTEX> @@target;
 
    PRINT "path query!";
 
    R2 = SELECT p
    FROM Person:p -(FRIENDS_WITH*1..2:r) -Person:f
    WHERE r.date_of_start > to_datetime("2010-01-01")
    ACCUM @@edges += r, @@source += p, @@target += f;
 
    PRINT @@edges, @@source, @@target;
    reset_collection_accum(@@edges);
    reset_collection_accum(@@source);
    reset_collection_accum(@@target);

  PRINT "start_date_qualifier_path works!";
}

(SEM-2001): line 12, col 37
Kleene star pattern can't have an alias.

Failed to create queries: [start_date_qualifier_path].

Documentação

A query can be run in one of three ways:

    Define and run an unnamed query immediately:

        INTERPRET QUERY: execute the query’s statements

        Alternately, there is also a built-in REST++ endpoint to interpret a query string:
        POST /gsqlserver/interpreted_query
        See the RESTPP API User Guide for details.

    Define a named query and then run it.

        CREATE QUERY: define the functionality of the query

        INTERPRET QUERY: execute the query with input values

    Define a named query, compile it to optimize performance, and then run it.

        CREATE QUERY: define the functionality of the query

        INSTALL QUERY: compile the query

        RUN QUERY: execute the query with input values

Starting with TigerGraph 2.6, there are now two versions of the GSQL Query Language. Both are available in the product. The original version is called GSQL Syntax V1. GSQL Syntax V2 was introduced to provide more flexible and powerful Pattern matching. A query indicates which grammar to use in the SYNTAX clause of the CREATE QUERY header. V1 is the default.

As new features are being added to GSQL, some are available only in V2. In the future, we plan to transition so that Syntax V2 will be the default.

The key differences between the two grammars:

Multi-hop Pattern Matching is only in Syntax V2 (since TigerGraph 2.6)

  • In V2, each SELECT statement can traverse a multi-hop path.

    • The traversal direction is under the control of the query writer, with arrowheads on each edge set to show the direction.

    • There is no arrowhead outside the parentheses:

      Start:s -( (ForwardEdge> | <BackwardEdge):e )- Target:t

    • Users can write paths that explicitly show multiple hops, and they can use a Kleene star (*) to indicate repetition. Start:s -(Edge1>:e1)- Middle:m -(<Edge2:e2)- Target:t or Start:s -(Edge*1..3)- Target:t

      SQL-like Tabular SELECT is only in Syntax V2 (since TigerGraph 3.1)

  • V1 is default when ommited

     

Comentários

Postagens mais visitadas deste blog

Connected Papers: Uma abordagem alternativa para revisão da literatura

Durante um projeto de pesquisa podemos encontrar um artigo que nos identificamos em termos de problema de pesquisa e também de solução. Então surge a vontade de saber como essa área de pesquisa se desenvolveu até chegar a esse ponto ou quais desdobramentos ocorreram a partir dessa solução proposta para identificar o estado da arte nesse tema. Podemos seguir duas abordagens:  realizar uma revisão sistemática usando palavras chaves que melhor caracterizam o tema em bibliotecas digitais de referência para encontrar artigos relacionados ou realizar snowballing ancorado nesse artigo que identificamos previamente, explorando os artigos citados (backward) ou os artigos que o citam (forward)  Mas a ferramenta Connected Papers propõe uma abordagem alternativa para essa busca. O problema inicial é dado um artigo de interesse, precisamos encontrar outros artigos relacionados de "certa forma". Find different methods and approaches to the same subject Track down the state of the art rese...

Knowledge Graph Embedding with Triple Context - Leitura de Abstract

  Jun Shi, Huan Gao, Guilin Qi, and Zhangquan Zhou. 2017. Knowledge Graph Embedding with Triple Context. In Proceedings of the 2017 ACM on Conference on Information and Knowledge Management (CIKM '17). Association for Computing Machinery, New York, NY, USA, 2299–2302. https://doi.org/10.1145/3132847.3133119 ABSTRACT Knowledge graph embedding, which aims to represent entities and relations in vector spaces, has shown outstanding performance on a few knowledge graph completion tasks. Most existing methods are based on the assumption that a knowledge graph is a set of separate triples, ignoring rich graph features, i.e., structural information in the graph. In this paper, we take advantages of structures in knowledge graphs, especially local structures around a triple, which we refer to as triple context. We then propose a Triple-Context-based knowledge Embedding model (TCE). For each triple, two kinds of structure information are considered as its context in the graph; one is the out...

KnOD 2021

Beyond Facts: Online Discourse and Knowledge Graphs A preface to the proceedings of the 1st International Workshop on Knowledge Graphs for Online Discourse Analysis (KnOD 2021, co-located with TheWebConf’21) https://ceur-ws.org/Vol-2877/preface.pdf https://knod2021.wordpress.com/   ABSTRACT Expressing opinions and interacting with others on the Web has led to the production of an abundance of online discourse data, such as claims and viewpoints on controversial topics, their sources and contexts . This data constitutes a valuable source of insights for studies into misinformation spread, bias reinforcement, echo chambers or political agenda setting. While knowledge graphs promise to provide the key to a Web of structured information, they are mainly focused on facts without keeping track of the diversity, connection or temporal evolution of online discourse data. As opposed to facts, claims are inherently more complex. Their interpretation strongly depends on the context and a vari...