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

Fixes #3610: Add advanced SQL analytics to streaming cypher rows before returning results to the client #4339

Open
wants to merge 4 commits into
base: dev
Choose a base branch
from

Conversation

vga91
Copy link
Collaborator

@vga91 vga91 commented Jan 24, 2025

Fixes #3610

Overview

Fixes https://github.com/neo4j-contrib/neo4j-apoc-procedures/issues/3610

Created an apoc.jdbc.analytics(<cypherQuery>, <jdbcUrl>, <sqlQueryOverTemporaryTable>, <paramsList>, $config) procedure.
Imho is better than use an aggregation function, because it's more customizable, since I can choose what results to get and how to get them, otherwise to do something like below, with movies_count I should put e.g. a parameter aggKeys and increase the complexity.

            MATCH (p:Person)-[r:ACTED_IN]->(m:Movie)
            RETURN 
                p.name AS actor, 
                m.genre AS genre, 
                r.roles AS roles, 
                COUNT(m) AS movies_count

The procedure executes 3 steps:

  1. creates a temporary table
  2. populates the table starting from the <cypherQuery> param
  3. queries the table with the <sqlQuery> param (which can be an complex analytics query) and returns data

Changes implemented

EDIT: Changes after review

  • added DuckDB, Postgres and MySql mapping to handle different types of numbers, dates and duration
  • improved tests with the missing data types
  • added escape chars to create table, based on Provider conf, to handle reserved keyword like time in MySQL
  • refactoring: moved all jdbc stuff to apoc.load.jdbc package

To evaluate

  • (maybe in a different PR/Issue): Improve handling: some SQL databases like Microsoft SQL Server create temp table in a different way


You can use the `apoc.jdbc.analytics(<cypherQuery>, <jdbcUrl>, <sqlQueryOverTemporaryTable>, <paramsList>, $config)`
to create a temporary table starting from a Cypher query
and delegate complex analytics to the database defined JDBC URL.
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

defined by the JDBC URL

ORDER BY rank, country, name;",
$params,
{ provider: "POSTGRES" })
----
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets add a full example using an in-memory DuckDb to perform analytics

private String mapSqlType(Provider provider, Object value) {
return switch (provider) {
case MYSQL, POSTGRES -> {
if (value instanceof Number) yield "INTEGER";
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what if the Number is a Float?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants