Skip to content

Commit fa36876

Browse files
committed
fix(bqjdbc): Pass connection ID to DataType class loggers
1 parent 875ff6a commit fa36876

14 files changed

Lines changed: 182 additions & 141 deletions

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryArrowArray.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,14 +28,17 @@
2828
* An implementation of {@link BigQueryBaseArray} used to represent Array values from Arrow data.
2929
*/
3030
class BigQueryArrowArray extends BigQueryBaseArray {
31-
private static final BigQueryJdbcResultSetLogger LOG =
32-
BigQueryJdbcResultSetLogger.getLogger(BigQueryArrowArray.class);
3331
private static final BigQueryTypeCoercer BIGQUERY_TYPE_COERCER =
3432
BigQueryTypeCoercionUtility.INSTANCE;
3533
private JsonStringArrayList<?> values;
3634

3735
public BigQueryArrowArray(Field schema, JsonStringArrayList<?> values) {
38-
super(schema);
36+
this(schema, values, BigQueryJdbcResultSetLogger.getLogger(BigQueryArrowArray.class));
37+
}
38+
39+
public BigQueryArrowArray(
40+
Field schema, JsonStringArrayList<?> values, BigQueryJdbcResultSetLogger log) {
41+
super(schema, log);
3942
this.values = values;
4043
}
4144

@@ -99,7 +102,8 @@ Object getCoercedValue(int index) {
99102
LOG.finestTrace("getCoercedValue", "++enter++");
100103
Object value = this.values.get(index);
101104
return this.arrayOfStruct
102-
? new BigQueryArrowStruct(schema.getSubFields(), (JsonStringHashMap<?, ?>) value)
103-
: BIGQUERY_TYPE_COERCER.coerceTo(getTargetClass(), value);
105+
? new BigQueryArrowStruct(
106+
schema.getSubFields(), (JsonStringHashMap<?, ?>) value, this.LOG.getArrowStructLogger())
107+
: BIGQUERY_TYPE_COERCER.coerceTo(getTargetClass(), value, this.LOG);
104108
}
105109
}

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryArrowResultSet.java

Lines changed: 19 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -326,18 +326,21 @@ public Object getObject(int columnIndex) throws SQLException {
326326
}
327327

328328
if (this.isNested && columnIndex == 1) {
329-
return this.bigQueryTypeCoercer.coerceTo(Integer.class, value);
329+
return this.bigQueryTypeCoercer.coerceTo(Integer.class, value, this.LOG);
330330
}
331331

332332
if (this.isNested && columnIndex == 2) {
333333
Field arrayField = this.schema.getFields().get(0);
334334
if (isStruct(arrayField)) {
335-
return new BigQueryArrowStruct(arrayField.getSubFields(), (JsonStringHashMap<?, ?>) value);
335+
return new BigQueryArrowStruct(
336+
arrayField.getSubFields(),
337+
(JsonStringHashMap<?, ?>) value,
338+
this.LOG.getArrowStructLogger());
336339
}
337340
Class<?> targetClass =
338341
BigQueryJdbcTypeMappings.standardSQLToJavaTypeMapping.get(
339342
arrayField.getType().getStandardType());
340-
return this.bigQueryTypeCoercer.coerceTo(targetClass, value);
343+
return this.bigQueryTypeCoercer.coerceTo(targetClass, value, this.LOG);
341344
}
342345

343346
int fieldIndex = this.isNested ? 0 : columnIndex - 1;
@@ -355,7 +358,7 @@ public Object getObject(int columnIndex) throws SQLException {
355358
newList.add(null);
356359
}
357360
}
358-
return new BigQueryArrowArray(fieldSchema, newList);
361+
return new BigQueryArrowArray(fieldSchema, newList, this.LOG.getArrowArrayLogger());
359362
} else if (elementTypeName == StandardSQLTypeName.RANGE) {
360363
JsonStringArrayList<String> newList = new JsonStringArrayList<>();
361364
for (Object item : originalList) {
@@ -375,11 +378,14 @@ public Object getObject(int columnIndex) throws SQLException {
375378
newList.add(null);
376379
}
377380
}
378-
return new BigQueryArrowArray(fieldSchema, newList);
381+
return new BigQueryArrowArray(fieldSchema, newList, this.LOG.getArrowArrayLogger());
379382
}
380-
return new BigQueryArrowArray(fieldSchema, originalList);
383+
return new BigQueryArrowArray(fieldSchema, originalList, this.LOG.getArrowArrayLogger());
381384
} else if (isStruct(fieldSchema)) {
382-
return new BigQueryArrowStruct(fieldSchema.getSubFields(), (JsonStringHashMap<?, ?>) value);
385+
return new BigQueryArrowStruct(
386+
fieldSchema.getSubFields(),
387+
(JsonStringHashMap<?, ?>) value,
388+
this.LOG.getArrowStructLogger());
383389
} else if (fieldSchema.getType().getStandardType() == StandardSQLTypeName.RANGE) {
384390
JsonStringHashMap<?, ?> rangeMap = (JsonStringHashMap<?, ?>) value;
385391
Object start = rangeMap.get("start");
@@ -403,7 +409,7 @@ public Object getObject(int columnIndex) throws SQLException {
403409
Class<?> targetClass =
404410
BigQueryJdbcTypeMappings.standardSQLToJavaTypeMapping.get(
405411
fieldSchema.getType().getStandardType());
406-
return this.bigQueryTypeCoercer.coerceTo(targetClass, value);
412+
return this.bigQueryTypeCoercer.coerceTo(targetClass, value, this.LOG);
407413
}
408414
}
409415

@@ -430,17 +436,17 @@ private String formatRangeElement(Object element, StandardSQLTypeName elementTyp
430436
switch (elementType) {
431437
case DATE:
432438
// Arrow gives DATE as an Integer (days since epoch)
433-
Date date = this.bigQueryTypeCoercer.coerceTo(Date.class, (Integer) element);
439+
Date date = this.bigQueryTypeCoercer.coerceTo(Date.class, (Integer) element, this.LOG);
434440
return date.toString();
435441
case DATETIME:
436442
// Arrow gives DATETIME as a LocalDateTime
437443
Timestamp dtTs =
438-
this.bigQueryTypeCoercer.coerceTo(Timestamp.class, (LocalDateTime) element);
439-
return this.bigQueryTypeCoercer.coerceTo(String.class, dtTs);
444+
this.bigQueryTypeCoercer.coerceTo(Timestamp.class, (LocalDateTime) element, this.LOG);
445+
return this.bigQueryTypeCoercer.coerceTo(String.class, dtTs, this.LOG);
440446
case TIMESTAMP:
441447
// Arrow gives TIMESTAMP as a Long (microseconds since epoch)
442-
Timestamp ts = this.bigQueryTypeCoercer.coerceTo(Timestamp.class, (Long) element);
443-
return this.bigQueryTypeCoercer.coerceTo(String.class, ts);
448+
Timestamp ts = this.bigQueryTypeCoercer.coerceTo(Timestamp.class, (Long) element, this.LOG);
449+
return this.bigQueryTypeCoercer.coerceTo(String.class, ts, this.LOG);
444450
default:
445451
// Fallback for any other unexpected type
446452
return element.toString();

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryArrowStruct.java

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -30,9 +30,6 @@
3030
* An implementation of {@link BigQueryBaseStruct} used to represent Struct values from Arrow data.
3131
*/
3232
class BigQueryArrowStruct extends BigQueryBaseStruct {
33-
private static final BigQueryJdbcResultSetLogger LOG =
34-
BigQueryJdbcResultSetLogger.getLogger(BigQueryArrowStruct.class);
35-
3633
private static final BigQueryTypeCoercer BIGQUERY_TYPE_COERCER =
3734
BigQueryTypeCoercionUtility.INSTANCE;
3835

@@ -41,6 +38,12 @@ class BigQueryArrowStruct extends BigQueryBaseStruct {
4138
private final JsonStringHashMap<?, ?> values;
4239

4340
BigQueryArrowStruct(FieldList schema, JsonStringHashMap<?, ?> values) {
41+
this(schema, values, BigQueryJdbcResultSetLogger.getLogger(BigQueryArrowStruct.class));
42+
}
43+
44+
BigQueryArrowStruct(
45+
FieldList schema, JsonStringHashMap<?, ?> values, BigQueryJdbcResultSetLogger log) {
46+
super(log);
4447
this.schema = schema;
4548
this.values = values;
4649
}
@@ -73,15 +76,18 @@ public Object[] getAttributes() {
7376
private Object getValue(Field currentSchema, Object currentValue) {
7477
LOG.finestTrace("getValue", "++enter++");
7578
if (isArray(currentSchema)) {
76-
return new BigQueryArrowArray(currentSchema, (JsonStringArrayList<?>) currentValue);
79+
return new BigQueryArrowArray(
80+
currentSchema, (JsonStringArrayList<?>) currentValue, this.LOG.getArrowArrayLogger());
7781
} else if (isStruct(currentSchema)) {
7882
return new BigQueryArrowStruct(
79-
currentSchema.getSubFields(), (JsonStringHashMap<?, ?>) currentValue);
83+
currentSchema.getSubFields(),
84+
(JsonStringHashMap<?, ?>) currentValue,
85+
this.LOG.getArrowStructLogger());
8086
} else {
8187
Class<?> targetClass =
8288
BigQueryJdbcTypeMappings.standardSQLToJavaTypeMapping.get(
8389
currentSchema.getType().getStandardType());
84-
return BIGQUERY_TYPE_COERCER.coerceTo(targetClass, currentValue);
90+
return BIGQUERY_TYPE_COERCER.coerceTo(targetClass, currentValue, this.LOG);
8591
}
8692
}
8793
}

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseArray.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -41,14 +41,14 @@
4141
* reference to an SQL ARRAY value.
4242
*/
4343
abstract class BigQueryBaseArray implements java.sql.Array {
44-
private static final BigQueryJdbcResultSetLogger LOG =
45-
BigQueryJdbcResultSetLogger.getLogger(BigQueryBaseArray.class);
44+
protected final BigQueryJdbcResultSetLogger LOG;
4645

4746
protected final boolean arrayOfStruct;
4847
private boolean valid;
4948
protected Field schema;
5049

51-
BigQueryBaseArray(Field schema) {
50+
BigQueryBaseArray(Field schema, BigQueryJdbcResultSetLogger log) {
51+
this.LOG = log;
5252
this.schema = schema;
5353
this.arrayOfStruct = isStruct(schema);
5454
this.valid = true;
@@ -152,7 +152,6 @@ protected Class<?> getTargetClass() {
152152
abstract Object getCoercedValue(int index);
153153

154154
static boolean isArray(Field currentSchema) {
155-
LOG.finestTrace("isArray", "++enter++");
156155
return currentSchema.getMode() == REPEATED;
157156
}
158157

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseResultSet.java

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -261,7 +261,7 @@ public String getString(int columnIndex) throws SQLException {
261261
LOG.finestTrace("getString", "++enter++");
262262
try {
263263
Object value = getObject(columnIndex);
264-
return this.bigQueryTypeCoercer.coerceTo(String.class, value);
264+
return this.bigQueryTypeCoercer.coerceTo(String.class, value, this.LOG);
265265
} catch (BigQueryJdbcCoercionNotFoundException e) {
266266
throw createCoercionException(columnIndex, String.class, e);
267267
}
@@ -280,7 +280,7 @@ public boolean getBoolean(int columnIndex) throws SQLException {
280280

281281
try {
282282
Object value = getObject(columnIndex);
283-
return this.bigQueryTypeCoercer.coerceTo(Boolean.class, value);
283+
return this.bigQueryTypeCoercer.coerceTo(Boolean.class, value, this.LOG);
284284
} catch (BigQueryJdbcCoercionNotFoundException e) {
285285
throw createCoercionException(columnIndex, Boolean.class, e);
286286
}
@@ -291,7 +291,7 @@ public byte getByte(int columnIndex) throws SQLException {
291291
LOG.finestTrace("getByte", "++enter++");
292292
try {
293293
Object value = getObject(columnIndex);
294-
return this.bigQueryTypeCoercer.coerceTo(Byte.class, value);
294+
return this.bigQueryTypeCoercer.coerceTo(Byte.class, value, this.LOG);
295295
} catch (BigQueryJdbcCoercionNotFoundException | BigQueryJdbcCoercionException e) {
296296
throw createCoercionException(columnIndex, Byte.class, e);
297297
}
@@ -302,7 +302,7 @@ public short getShort(int columnIndex) throws SQLException {
302302
LOG.finestTrace("getShort", "++enter++");
303303
try {
304304
Object value = getObject(columnIndex);
305-
return this.bigQueryTypeCoercer.coerceTo(Short.class, value);
305+
return this.bigQueryTypeCoercer.coerceTo(Short.class, value, this.LOG);
306306
} catch (BigQueryJdbcCoercionNotFoundException | BigQueryJdbcCoercionException e) {
307307
throw createCoercionException(columnIndex, Short.class, e);
308308
}
@@ -313,7 +313,7 @@ public int getInt(int columnIndex) throws SQLException {
313313
LOG.finestTrace("getInt", "++enter++");
314314
try {
315315
Object value = getObject(columnIndex);
316-
return this.bigQueryTypeCoercer.coerceTo(Integer.class, value);
316+
return this.bigQueryTypeCoercer.coerceTo(Integer.class, value, this.LOG);
317317
} catch (BigQueryJdbcCoercionNotFoundException | BigQueryJdbcCoercionException e) {
318318
throw createCoercionException(columnIndex, Integer.class, e);
319319
}
@@ -324,7 +324,7 @@ public long getLong(int columnIndex) throws SQLException {
324324
LOG.finestTrace("getLong", "++enter++");
325325
try {
326326
Object value = getObject(columnIndex);
327-
return this.bigQueryTypeCoercer.coerceTo(Long.class, value);
327+
return this.bigQueryTypeCoercer.coerceTo(Long.class, value, this.LOG);
328328
} catch (BigQueryJdbcCoercionNotFoundException | BigQueryJdbcCoercionException e) {
329329
throw createCoercionException(columnIndex, Long.class, e);
330330
}
@@ -335,7 +335,7 @@ public float getFloat(int columnIndex) throws SQLException {
335335
LOG.finestTrace("getFloat", "++enter++");
336336
try {
337337
Object value = getObject(columnIndex);
338-
return this.bigQueryTypeCoercer.coerceTo(Float.class, value);
338+
return this.bigQueryTypeCoercer.coerceTo(Float.class, value, this.LOG);
339339
} catch (BigQueryJdbcCoercionNotFoundException | BigQueryJdbcCoercionException e) {
340340
throw createCoercionException(columnIndex, Float.class, e);
341341
}
@@ -346,7 +346,7 @@ public double getDouble(int columnIndex) throws SQLException {
346346
LOG.finestTrace("getDouble", "++enter++");
347347
try {
348348
Object value = getObject(columnIndex);
349-
return this.bigQueryTypeCoercer.coerceTo(Double.class, value);
349+
return this.bigQueryTypeCoercer.coerceTo(Double.class, value, this.LOG);
350350
} catch (BigQueryJdbcCoercionNotFoundException | BigQueryJdbcCoercionException e) {
351351
throw createCoercionException(columnIndex, Double.class, e);
352352
}
@@ -359,7 +359,7 @@ public BigDecimal getBigDecimal(int columnIndex, int scale) throws SQLException
359359
LOG.finestTrace("getBigDecimal", "++enter++");
360360
try {
361361
Object value = getObject(columnIndex);
362-
return this.bigQueryTypeCoercer.coerceTo(BigDecimal.class, value);
362+
return this.bigQueryTypeCoercer.coerceTo(BigDecimal.class, value, this.LOG);
363363
} catch (BigQueryJdbcCoercionNotFoundException | BigQueryJdbcCoercionException e) {
364364
throw createCoercionException(columnIndex, BigDecimal.class, e);
365365
}
@@ -370,7 +370,7 @@ public byte[] getBytes(int columnIndex) throws SQLException {
370370
LOG.finestTrace("getBytes", "++enter++");
371371
try {
372372
Object value = getObject(columnIndex);
373-
return this.bigQueryTypeCoercer.coerceTo(byte[].class, value);
373+
return this.bigQueryTypeCoercer.coerceTo(byte[].class, value, this.LOG);
374374
} catch (BigQueryJdbcCoercionNotFoundException e) {
375375
throw createCoercionException(columnIndex, byte[].class, e);
376376
}
@@ -381,7 +381,7 @@ public Date getDate(int columnIndex) throws SQLException {
381381
LOG.finestTrace("getDate", "++enter++");
382382
try {
383383
Object value = getObject(columnIndex);
384-
return this.bigQueryTypeCoercer.coerceTo(java.sql.Date.class, value);
384+
return this.bigQueryTypeCoercer.coerceTo(java.sql.Date.class, value, this.LOG);
385385
} catch (BigQueryJdbcCoercionNotFoundException e) {
386386
throw createCoercionException(columnIndex, java.sql.Date.class, e);
387387
}
@@ -396,7 +396,7 @@ public Time getTime(int columnIndex) throws SQLException {
396396
}
397397
try {
398398
Object value = getObject(columnIndex);
399-
return this.bigQueryTypeCoercer.coerceTo(java.sql.Time.class, value);
399+
return this.bigQueryTypeCoercer.coerceTo(java.sql.Time.class, value, this.LOG);
400400
} catch (BigQueryJdbcCoercionNotFoundException e) {
401401
throw createCoercionException(columnIndex, java.sql.Time.class, e);
402402
}
@@ -411,7 +411,7 @@ public Timestamp getTimestamp(int columnIndex) throws SQLException {
411411
}
412412
try {
413413
Object value = getObject(columnIndex);
414-
return this.bigQueryTypeCoercer.coerceTo(java.sql.Timestamp.class, value);
414+
return this.bigQueryTypeCoercer.coerceTo(java.sql.Timestamp.class, value, this.LOG);
415415
} catch (BigQueryJdbcCoercionNotFoundException e) {
416416
throw createCoercionException(columnIndex, java.sql.Timestamp.class, e);
417417
}
@@ -422,7 +422,7 @@ public BigDecimal getBigDecimal(int columnIndex) throws SQLException {
422422
LOG.finestTrace("getBigDecimal", "++enter++");
423423
try {
424424
Object value = getObject(columnIndex);
425-
return this.bigQueryTypeCoercer.coerceTo(BigDecimal.class, value);
425+
return this.bigQueryTypeCoercer.coerceTo(BigDecimal.class, value, this.LOG);
426426
} catch (BigQueryJdbcCoercionNotFoundException | BigQueryJdbcCoercionException e) {
427427
throw createCoercionException(columnIndex, BigDecimal.class, e);
428428
}

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryBaseStruct.java

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,8 +36,11 @@
3636
* attribute of the SQL structured type that it represents.
3737
*/
3838
abstract class BigQueryBaseStruct implements java.sql.Struct {
39-
private static final BigQueryJdbcResultSetLogger LOG =
40-
BigQueryJdbcResultSetLogger.getLogger(BigQueryBaseStruct.class);
39+
protected final BigQueryJdbcResultSetLogger LOG;
40+
41+
BigQueryBaseStruct(BigQueryJdbcResultSetLogger log) {
42+
this.LOG = log;
43+
}
4144

4245
abstract FieldList getSchema();
4346

@@ -52,7 +55,6 @@ public final Object[] getAttributes(Map<String, Class<?>> map) throws SQLExcepti
5255
}
5356

5457
static boolean isStruct(Field currentSchema) {
55-
LOG.finestTrace("isStruct", "++enter++");
5658
return currentSchema.getType().getStandardType() == STRUCT;
5759
}
5860

java-bigquery/google-cloud-bigquery-jdbc/src/main/java/com/google/cloud/bigquery/jdbc/BigQueryJdbcContextProxy.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020
import java.lang.reflect.InvocationTargetException;
2121
import java.lang.reflect.Method;
2222
import java.lang.reflect.Proxy;
23+
import java.util.logging.Level;
2324

2425
/**
2526
* Dynamic InvocationHandler that transparently wraps JDBC operations. Sets the connection context
@@ -130,8 +131,20 @@ public Object invoke(Object proxy, Method method, Object[] args) throws Throwabl
130131

131132
// Wrap execution in the context of the active connection for all non-bypassed methods
132133
try (BigQueryJdbcMdc.MdcCloseable mdc = BigQueryJdbcMdc.registerInstance(connectionId)) {
134+
if (LOG.isLoggable(Level.FINER)) {
135+
LOG.logp(Level.FINER, target.getClass().getName(), method.getName(), "++entry++");
136+
}
137+
133138
Object result = method.invoke(target, args);
134139

140+
// Suppress exit logging for Connection.close() since its file handler is unmounted during
141+
// execution
142+
if (LOG.isLoggable(Level.FINER)
143+
&& !(java.sql.Connection.class.isAssignableFrom(interfaceType)
144+
&& "close".equals(method.getName()))) {
145+
LOG.logp(Level.FINER, target.getClass().getName(), method.getName(), "++exit++");
146+
}
147+
135148
// Symmetrical Cascade: Dynamic ResultSet concrete classes are deliberately unproxied here.
136149
// Bypassing proxies on high-frequency ResultSet iterations avoids dynamic invocation
137150
// and argument array allocations, allowing the JIT compiler to natively inline getters.

0 commit comments

Comments
 (0)