Skip to content

Instantly share code, notes, and snippets.

@GavinRay97
Last active August 22, 2024 01:23
Show Gist options
  • Save GavinRay97/44fcd0f17e5c62ed6e7880fed0b0042e to your computer and use it in GitHub Desktop.
Save GavinRay97/44fcd0f17e5c62ed6e7880fed0b0042e to your computer and use it in GitHub Desktop.
Making Apache Calcite work in SQuirreL SQL Client

Making Apache Calcite work in SQuirreL SQL Client

Recently I wanted to test some Calcite functionality, and thought it might be nice to use a GUI instead of sqlline. It turns out, it is:

image

I figured you could just use something like SquirreL, which lets you import arbitrary JDBC drivers. This was less straightforward than I thought.

The hangup was the dependencies and their recursive transitive dependencies. At first, I was manually downloading the proper versions from the Maven portal, but there turned out to be a much easier way.

1. Download all transitive dependencies using hacky mvn:dependency script

All credit to this brilliant fellow for the idea: https://stackoverflow.com/a/68918147/13485494

We make a script which creates a dummy pom.xml just to download the closure of the dependency set:

Assuming we named this get-maven-deps.sh, we invoke it as:

  • ./get-maven-deps.sh org.apache.calcite calcite-core 1.30.0

If successful, you should see:

user@MSI:~/tmp/maven-dep-downloader$ ./get-maven-deps.sh org.apache.calcite calcite-core 1.30.0
[INFO] Scanning for projects...
[INFO]
[INFO] -------------------------< com.temp.temp:temp >-------------------------
[INFO] Building temp 0.0.0
[INFO] --------------------------------[ jar ]---------------------------------
Downloading from central: https://repo.maven.apache.org/maven2/org/apache/calcite/calcite-core/1.30.0/calcite-core-1.30.0.pom
<... SNIPPED ...>
[INFO] --- maven-dependency-plugin:2.8:copy-dependencies (default-cli) @ temp ---
[INFO] Copying jsr305-3.0.2.jar to /home/user/tmp/maven-dep-downloader/libs/1.30.0/jsr305-3.0.2.jar
<... SNIPPED ...>
[INFO] Copying memory-0.9.0.jar to /home/user/tmp/maven-dep-downloader/libs/1.30.0/memory-0.9.0.jar
[INFO] ------------------------------------------------------------------------
[INFO] BUILD SUCCESS
[INFO] ------------------------------------------------------------------------
[INFO] Total time:  7.988 s
[INFO] Finished at: 2022-07-18T16:38:22-04:00
[INFO] ------------------------------------------------------------------------

image

2. Configure Calcite driver in SQuirreL

Now we need to copy the libraries into our SQuirreL installation folder somewhere.

I use a directory called custom-drivers:

  • C:\squirrel-sql\custom-drivers\calcite\1.30.0

After copying the folder, start SQuirreL, and navigate to Drivers -> Create New from the left-hand side:

image

Fill out the information with:

  • Name: Calcite 1.30 (or corresponding version)
  • Example URL: jdbc:calcite:model=<pathToModelFile>
  • Class name: org.apache.calcite.jdbc.Driver

And then from the Extra Class Path tab, press Add:

image

Navigate to the custom-drivers/calcite/1.30.0 directory, press Ctrl+A/Cmd+A to select all, and then add:

image

image

At the bottom of the screen, you should see Driver class org.apache.calcite.jdbc.Driver successfully registered for driver definition: Calcite 1.30.0:

image

Now navigate to Aliases on the left-hand sidebar, and create a new one:

image

You'll need a model for a Calcite schema, if you have a JDBC database you can use that. Set the URL to jdbc:calcite:model=/path/to/this/file.json where the file is a Calcite model, like the below:

{
    "version": "1.0",
    "defaultSchema": "test",
    "schemas": [
        {
            "name": "test",
            "type": "jdbc",
            "jdbcUrl": "jdbc:postgresql://localhost:5432/postgres",
            "jdbcSchema": "public",
            "jdbcUser": "postgres",
            "jdbcPassword": "Password123#",
            "jdbcDriver": "org.postgresql.Driver"
        }
    ]
}

Trying to add this specific model will fail though, as we've not added the Postgres JDBC driver to our classpath:

image

This is easily solved by modifying the Calcite Driver we just added and including whatever DB drivers we need in the classpath. When first installing SQuirreL, you can choose to have it download many common DB JDBC drivers.

In case you've chosen Postgres, the driver will be at:

  • "squirrel-sql\plugins\postgres\lib\postgresql-42.2.9.jar"

We can add it by right-clicking on the Calcite 1.30.0 definition under Drivers and clicking Modify:

image

Now if we try to add the alias again, and test the connection it should be successful:

image

We can finally connect:

image

image

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