Skip to content
This repository was archived by the owner on Jun 9, 2025. It is now read-only.

Commit 0f0c6c5

Browse files
authored
Merge pull request #9 from buserbrasil/schumann/fix-connection
fix connection
2 parents 7a1a5cd + 6c3aace commit 0f0c6c5

File tree

6 files changed

+104
-21
lines changed

6 files changed

+104
-21
lines changed

Dockerfile

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,11 @@
1-
FROM clojure:tools-deps-1.11.1.1413-focal
1+
FROM clojure:tools-deps-1.11.1.1435-bookworm
22

33
COPY . /driver
44

5-
ARG METABASE_VERSION="v0.49.3"
5+
RUN apt-get update && apt-get install -y --no-install-recommends curl && \
6+
rm -rf /var/lib/apt/lists/*
7+
8+
ARG METABASE_VERSION="v0.49.15"
69

710
RUN curl -Lo - https://github.com/metabase/metabase/archive/refs/tags/${METABASE_VERSION}.tar.gz | tar -xz && mv metabase-* /metabase
811

Makefile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11

22
ROOT_DIR:=$(shell dirname $(realpath $(firstword $(MAKEFILE_LIST))))
3-
METABASE_VERSION=v0.49.3
3+
METABASE_VERSION=v0.49.15
44

55
build:
66
@echo "build"

deps.edn

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -2,17 +2,13 @@
22
["src" "resources"]
33

44
:deps
5-
{com.databricks/databricks-jdbc {:mvn/version "2.6.34" :exclusion [log4j/log4j]}}
5+
{com.databricks/databricks-jdbc {:mvn/version "2.6.38"}}
6+
67

78
;; the stuff below is only for hacking on the driver locally and is not needed if you follow the instructions in the
89
;; README and create a `:local/root` dep for the driver and launch the REPL from the Metabase project rather than
910
;; from here
10-
11-
;;; needed for Metabase as a local dep for local development
12-
:mvn/repos
13-
{"opensaml" {:url "https://build.shibboleth.net/nexus/content/repositories/releases/"}}
14-
1511
:aliases
1612
{:dev
1713
{:extra-deps
18-
{io.github.metabase/metabase {:git/tag "v0.49.3", :git/sha "4202328"}}}}}
14+
{com.github.metabase/metabase {:git/tag "v0.49.15", :git/sha "77a08d7710d79f82c52c1d1e3cd3d4edc10bd8406d93eec9988d12158334ecc3"}}}}}

src/metabase/driver/databricks_sql.clj

Lines changed: 25 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33
[clojure.string :as str]
44
[medley.core :as m]
55
[metabase.driver :as driver]
6+
[metabase.driver.sql-jdbc
7+
[common :as sql-jdbc.common]]
68
[metabase.driver.sql-jdbc.connection :as sql-jdbc.conn]
79
[metabase.driver.sql-jdbc.execute :as sql-jdbc.execute]
810
[metabase.driver.sql-jdbc.sync :as sql-jdbc.sync]
@@ -11,23 +13,35 @@
1113
[metabase.driver.sql.util.unprepare :as unprepare]
1214
[metabase.mbql.util :as mbql.u]
1315
[metabase.query-processor.util :as qp.util])
14-
(:import [java.sql Connection ResultSet]))
16+
(:import
17+
(java.sql Connection ResultSet)))
1518

1619
(set! *warn-on-reflection* true)
1720

1821
(driver/register! :databricks-sql, :parent :sql-jdbc)
1922

23+
(defn- sparksql-databricks
24+
"Create a database specification for a Spark SQL database."
25+
[{:keys [host db jdbc-flags] :as opts}]
26+
(merge
27+
{:classname "metabase.driver.databricks-sql.FixedDatabricksDriver"
28+
:subprotocol "databricks"
29+
:subname (str "//" host ":443/" db jdbc-flags)}
30+
(dissoc opts :host :db :jdbc-flags)))
31+
2032
(defmethod sql-jdbc.conn/connection-details->spec :databricks-sql
21-
[_ {:keys [host http-path token db]}]
22-
{:classname "com.databricks.client.jdbc.Driver"
23-
:subprotocol "databricks"
24-
:subname (str "//" host ":443/" db)
25-
:transportMode "http"
26-
:ssl 1
27-
:AuthMech 3
28-
:httpPath http-path
29-
:uid "token"
30-
:pwd token})
33+
[_ details]
34+
(-> details
35+
(assoc :jdbc-flags (str ";transportMode=http"
36+
";ssl=1"
37+
";AuthMech=3"
38+
";LogLevel=0"
39+
";UID=token"
40+
";PWD=" (:token details)
41+
";httpPath=" (:http-path details)))
42+
(select-keys [:host :db :jdbc-flags :dbname])
43+
sparksql-databricks
44+
(sql-jdbc.common/handle-additional-options details)))
3145

3246
;; The Hive JDBC driver doesn't support `Connection.isValid()`,
3347
;; so we need to supply a test query for c3p0 to use to validate
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
(ns metabase.driver.databricks-sql.FixedDatabricksDriver
2+
(:gen-class
3+
:extends com.databricks.client.jdbc.Driver
4+
:exposes-methods {connect superConnect}
5+
:init init
6+
:prefix "driver-"
7+
:constructors {[] []})
8+
(:require [metabase.driver.databricks-sql.connection :as connection]))
9+
10+
(defn driver-init
11+
"Initializes the Spark driver"
12+
[]
13+
[[] nil])
14+
15+
(defn driver-connect
16+
"Connects to a Spark database, fixing the connection to with Metabase"
17+
[^com.databricks.client.jdbc.Driver this, ^String url, ^java.util.Properties info]
18+
(connection/decorate-and-fix (.superConnect this url info)))
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
(ns metabase.driver.databricks-sql.connection
2+
(:require [clojure.reflect]
3+
[clojure.set])
4+
(:import [java.sql ResultSet SQLException]))
5+
6+
(defmacro decorator
7+
[clazz proto & fs]
8+
(let [proto-name (gensym "proto")
9+
methods (->> (clojure.reflect/reflect (resolve clazz))
10+
:members
11+
(filter #(instance? clojure.reflect.Method %))
12+
(map (fn [{:keys [name parameter-types]}]
13+
[name (count parameter-types)]))
14+
set)
15+
to-delegate (clojure.set/difference
16+
methods
17+
(->> fs
18+
(map (fn [[name params]]
19+
[name (count params)]))
20+
set))
21+
method-bodies
22+
(concat
23+
fs ;; these are our own definitions
24+
(for [[name n-params] to-delegate]
25+
(let [params (->> (range n-params)
26+
(map #(gensym (str "x" %))))]
27+
`(~name [~@params]
28+
(. ~proto-name (~name ~@params))) ;; this is where we delegate to the prototype
29+
)))]
30+
`(let [~proto-name ~proto]
31+
(proxy
32+
[~clazz] []
33+
~@(->> method-bodies (group-by first) (sort-by first)
34+
(map (fn [[name bodies]]
35+
`(~name ~@(for [[name & rest] bodies]
36+
rest)))))))))
37+
38+
(defn decorate-and-fix
39+
[impl]
40+
(when impl
41+
(decorator
42+
java.sql.Connection
43+
impl
44+
(getHoldability
45+
[]
46+
ResultSet/CLOSE_CURSORS_AT_COMMIT)
47+
(setReadOnly
48+
[read-only?]
49+
(when (.isClosed this)
50+
(throw (SQLException. "Connection is closed")))
51+
(when read-only?
52+
(throw (SQLException. "Enabling read-only mode is not supported")))))))

0 commit comments

Comments
 (0)