66import java .sql .DriverPropertyInfo ;
77import java .sql .SQLException ;
88import java .sql .SQLFeatureNotSupportedException ;
9- import java .util .ArrayList ;
10- import java .util .Collections ;
11- import java .util .HashMap ;
12- import java .util .LinkedHashMap ;
13- import java .util .List ;
14- import java .util .Map ;
15- import java .util .Properties ;
16- import java .util .ServiceLoader ;
9+ import java .util .*;
1710import java .util .Map .Entry ;
1811
1912import com .clickhouse .client .ClickHouseClient ;
@@ -49,6 +42,32 @@ public class ClickHouseDriver implements Driver {
4942
5043 static final java .util .logging .Logger parentLogger = java .util .logging .Logger .getLogger ("com.clickhouse.jdbc" );
5144
45+ static String frameworksDetected = null ;
46+
47+ private static class FrameworksDetection {
48+ private static final List <String > FRAMEWORKS_TO_DETECT = List .of ("apache.spark" );
49+ static volatile String frameworksDetected = null ;
50+
51+ private FrameworksDetection () {
52+ }
53+ public static String getFrameworksDetected () {
54+ if (frameworksDetected == null ) {
55+ Set <String > inferredFrameworks = new LinkedHashSet <>();
56+ for (StackTraceElement ste : Thread .currentThread ().getStackTrace ()) {
57+ for (String framework : FRAMEWORKS_TO_DETECT ) {
58+ if (ste .toString ().contains (framework )) {
59+ inferredFrameworks .add (String .format ("(%s)" , framework ));
60+ }
61+ }
62+ }
63+
64+ frameworksDetected = String .join ("; " , inferredFrameworks );
65+ }
66+ return frameworksDetected ;
67+ }
68+
69+ }
70+
5271 static {
5372 String str = ClickHouseDriver .class .getPackage ().getImplementationVersion ();
5473 if (str != null && !str .isEmpty ()) {
@@ -115,7 +134,8 @@ public static Map<ClickHouseOption, Serializable> toClientOptions(Properties pro
115134 options .put (o , ClickHouseOption .fromString (e .getValue ().toString (), o .getValueType ()));
116135 }
117136 }
118-
137+ if (frameworksDetected != null )
138+ options .put (ClickHouseClientOption .PRODUCT_NAME , frameworksDetected );
119139 return options ;
120140 }
121141
@@ -128,7 +148,7 @@ private DriverPropertyInfo create(ClickHouseOption option, Properties props) {
128148
129149 Class <?> clazz = option .getValueType ();
130150 if (Boolean .class == clazz || boolean .class == clazz ) {
131- propInfo .choices = new String [] { "true" , "false" };
151+ propInfo .choices = new String []{ "true" , "false" };
132152 } else if (clazz .isEnum ()) {
133153 Object [] values = clazz .getEnumConstants ();
134154 String [] names = new String [values .length ];
@@ -152,6 +172,8 @@ public ClickHouseConnection connect(String url, Properties info) throws SQLExcep
152172 if (!acceptsURL (url )) {
153173 return null ;
154174 }
175+ if (!url .toLowerCase ().contains ("disable-frameworks-detection" ))
176+ frameworksDetected = FrameworksDetection .getFrameworksDetected ();
155177
156178 log .debug ("Creating connection" );
157179 return new ClickHouseConnectionImpl (url , info );
0 commit comments