2

In SQL, I can write a query that already includes the data such as the following:

SELECT 1

Or, with a slightly less trivial example:

WITH users as (
    SELECT 1 id, "tom" name UNION ALL
    SELECT 2 id, "sandra" name
) SELECT * FROM users

Is there a similar way to do this in GQL without having to have already-defined tables? If so, what would be a bare-bones example?

GRAPH (...inline table... ?)
MATCH
  (from_person:Person {name: "Dana"})-[:Owns]->
RETURN
  from_person.name 
7
  • What is the usecase? Commented Oct 15, 2024 at 15:09
  • 1
    @RyoMatsuzaka practicing/mocking up various queries without having to have real-data that is stored. Commented Oct 15, 2024 at 21:06
  • Ok. What kind of GQL, are you using now? Commented Oct 15, 2024 at 21:30
  • 1
    @RyoMatsuzaka currently Cloud Spanner. Commented Oct 15, 2024 at 21:48
  • 1
    @RyoMatsuzaka yes you can: cloud.google.com/spanner/docs/reference/standard-sql/… Commented Oct 16, 2024 at 1:04

2 Answers 2

0

I don't think there are inline tables in GQL. But, you can maybe create temp in-memory data using unwind to mimic inline data to use in the query.

WITH [
{id: 1, name: 'Tom'},
{id: 2, name: 'Sandra'}
] AS users

UNWIND users AS user
MATCH (from_person:Person {name: "Dana"})-[:Owns]->()
RETURN from_person.name, user.id, user.name
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, what dialect is that in (does it work?)
it's cypher
0
+500

Since Cloud Spanner's GQL doesn't support inline data defs within queries, you can create temporary tables using a WITH CTE to define inline data.

-- Define inline data using Common Table Expressions
WITH
  Persons AS (
    SELECT 'tom' AS name UNION ALL
    SELECT 'sandra' AS name UNION ALL
    SELECT 'Dana' AS name
  ),
  Owns AS (
    SELECT 'Dana' AS owner_name, 'Item1' AS item_name
  )

-- Construct a graph using the inline data
GRAPH g AS
(
  -- Create Person nodes
  SELECT name
  FROM Persons
  LABEL Person
),

-- Create Owns relationships
(
  SELECT owner.name AS from_node,
         item.name AS to_node,
         'Owns' AS edge_type
  FROM Persons owner
  JOIN Owns ON owner.name = Owns.owner_name
  JOIN Persons item ON item.name = Owns.item_name
)

-- Query the graph
MATCH
  (from_person:Person {name: 'Dana'})-[:Owns]->(item)
RETURN
  from_person.name;

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.