Skip to content

Commit 08f34e3

Browse files
authored
Merge pull request #329 from wayne-pq/feat/optimized-code-wayne
mark deprecated method and fix unreachable exception
2 parents 8281ea3 + 6a127d5 commit 08f34e3

File tree

4 files changed

+108
-19
lines changed

4 files changed

+108
-19
lines changed

databend-jdbc/pom.xml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,10 @@
3636
<dependency>
3737
<groupId>com.fasterxml.jackson.core</groupId>
3838
<artifactId>jackson-core</artifactId>
39-
<version>2.13.1</version>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.fasterxml.jackson.core</groupId>
42+
<artifactId>jackson-databind</artifactId>
4043
</dependency>
4144

4245
<!-- https://mvnrepository.com/artifact/org.projectlombok/lombok -->

databend-jdbc/src/main/java/com/databend/jdbc/AbstractDatabendResultSet.java

Lines changed: 53 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@
2121
import java.io.InputStream;
2222
import java.io.Reader;
2323
import java.math.BigDecimal;
24+
import java.math.RoundingMode;
2425
import java.net.URL;
2526
import java.nio.charset.StandardCharsets;
2627
import java.sql.Array;
@@ -43,13 +44,7 @@
4344
import java.time.LocalDateTime;
4445
import java.time.ZoneId;
4546
import java.time.ZonedDateTime;
46-
import java.util.Calendar;
47-
import java.util.GregorianCalendar;
48-
import java.util.Iterator;
49-
import java.util.List;
50-
import java.util.Map;
51-
import java.util.Optional;
52-
import java.util.TimeZone;
47+
import java.util.*;
5348
import java.util.concurrent.atomic.AtomicBoolean;
5449
import java.util.concurrent.atomic.AtomicLong;
5550
import java.util.concurrent.atomic.AtomicReference;
@@ -490,21 +485,43 @@ public double getDouble(int columnIndex)
490485
}
491486
}
492487

493-
@Override
488+
/**
489+
* Retrieves a BigDecimal value from the specified column with scaling (Deprecated method)
490+
*
491+
* <p><b>Note: This method is deprecated and will be removed in a future release.
492+
* It is recommended to use the scale-free {@link #getBigDecimal(int)} method
493+
* to get the raw value and control scaling in the business layer.</b></p>
494+
*
495+
* <p><b>Migration Example:</b><br>
496+
* // Data access layer gets raw value<br>
497+
* BigDecimal rawValue = resultSet.getBigDecimal("price");<br>
498+
* // Business layer controls scaling<br>
499+
* BigDecimal scaledValue = rawValue.setScale(2, RoundingMode.HALF_UP);</p>
500+
*
501+
* @param columnIndex 1-based column index
502+
* @param scale Number of decimal places (must be ≥ 0)
503+
* @return BigDecimal value with specified scale
504+
* @throws SQLException If column value is not a valid number format or scale is invalid
505+
*
506+
* @deprecated Since JDK 1.2, scheduled for removal. Use {@link #getBigDecimal(int)}
507+
* with business layer scaling instead.
508+
*/
509+
@Override
510+
@Deprecated
494511
public BigDecimal getBigDecimal(int columnIndex, int scale)
495512
throws SQLException {
496513
Object value = column(columnIndex);
497-
if (value == null) {
514+
if (Objects.isNull(value)) {
498515
return null;
499516
}
500517
try {
501518
BigDecimal bigDecimal = (BigDecimal) value;
502-
return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
519+
return bigDecimal.setScale(scale, RoundingMode.HALF_UP);
503520
} catch (ClassCastException e) {
504521
// try to parse bigDecimal
505522
try {
506523
BigDecimal bigDecimal = new BigDecimal(value.toString());
507-
return bigDecimal.setScale(scale, BigDecimal.ROUND_HALF_UP);
524+
return bigDecimal.setScale(scale, RoundingMode.HALF_UP);
508525
} catch (NumberFormatException ex) {
509526
throw new SQLException("Value at columnIndex " + columnIndex + " is not a valid BigDecimal.");
510527
}
@@ -601,6 +618,7 @@ public InputStream getAsciiStream(int columnIndex)
601618
}
602619

603620
@Override
621+
@Deprecated
604622
public InputStream getUnicodeStream(int columnIndex)
605623
throws SQLException {
606624
throw new SQLFeatureNotSupportedException("getUnicodeStream");
@@ -678,7 +696,29 @@ public double getDouble(String columnLabel)
678696
return getDouble(columnIndex(columnLabel));
679697
}
680698

681-
@Override
699+
/**
700+
* Retrieves a BigDecimal value from the specified column by name with scaling (Deprecated method)
701+
*
702+
* <p><strong>Deprecation Notice:</strong><br>
703+
* This method is scheduled for removal in a future release. The scaling operation should be
704+
* performed in the business layer rather than during data retrieval.</p>
705+
*
706+
* <p><b>Migration Example:</b><br>
707+
* // Data access layer gets raw value<br>
708+
* BigDecimal rawValue = getBigDecimal(columnLabel);<br>
709+
* // Business layer controls scaling<br>
710+
* BigDecimal scaledValue = rawValue.setScale(desiredScale, RoundingMode.HALF_UP);</p>
711+
*
712+
* @param columnLabel the column name label (case sensitivity depends on database)
713+
* @param scale the number of digits after the decimal point (must be ≥ 0)
714+
* @return the column value as BigDecimal with specified scale, or null if the value is SQL NULL
715+
* @throws SQLException if columnLabel is invalid or scale is invalid
716+
*
717+
* @deprecated As of JDK 1.2, replaced by {@link #getBigDecimal(String)} combined with
718+
* explicit scaling in business logic. Scheduled for removal in a future release.
719+
*/
720+
@Override
721+
@Deprecated
682722
public BigDecimal getBigDecimal(String columnLabel, int scale)
683723
throws SQLException {
684724
return getBigDecimal(columnIndex(columnLabel), scale);
@@ -715,6 +755,7 @@ public InputStream getAsciiStream(String columnLabel)
715755
}
716756

717757
@Override
758+
@Deprecated
718759
public InputStream getUnicodeStream(String columnLabel)
719760
throws SQLException {
720761
throw new SQLFeatureNotSupportedException("getUnicodeStream");
@@ -789,7 +830,6 @@ public BigDecimal getBigDecimal(int columnIndex)
789830
if (value == null) {
790831
return null;
791832
}
792-
793833
return parseBigDecimal(String.valueOf(value));
794834
}
795835

databend-jdbc/src/main/java/com/databend/jdbc/DatabendConnection.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,11 @@ private void initializeFileHandler() {
9898
return;
9999
}
100100
try {
101-
System.setProperty("java.util.logging.FileHandler.limit", "2147483647"); // 2GB,Integer.MAX_VALUE
101+
// 2GB,Integer.MAX_VALUE
102+
System.setProperty("java.util.logging.FileHandler.limit", "2147483647");
102103
System.setProperty("java.util.logging.FileHandler.count", "200");
103-
System.setProperty("java.util.logging.FileHandler.append", "true"); // Enable log file reuse
104+
// Enable log file reuse
105+
System.setProperty("java.util.logging.FileHandler.append", "true");
104106
FILE_HANDLER = new FileHandler(file.getAbsolutePath(), Integer.parseInt(System.getProperty("java.util.logging.FileHandler.limit")),
105107
Integer.parseInt(System.getProperty("java.util.logging.FileHandler.count")), true);
106108
FILE_HANDLER.setLevel(Level.ALL);
@@ -730,7 +732,8 @@ DatabendClient startQueryWithFailover(String sql, StageAttachment attach) throws
730732
if (shouldRetryException(e) && attempt < maxRetries) {
731733
lastException = wrapException("query failed", sql, e);
732734
try {
733-
Thread.sleep(Math.min(100 * (1 << attempt), 5000)); // back off retry
735+
// back off retry
736+
Thread.sleep(Math.min(100 * (1 << attempt), 5000));
734737
} catch (InterruptedException ie) {
735738
Thread.currentThread().interrupt();
736739
throw wrapException("query interrupt", sql, ie);
@@ -924,7 +927,8 @@ public void uploadStream(String stageName, String destPrefix, InputStream inputS
924927
}
925928
}
926929
dataStream = new ByteArrayInputStream(byteArrayOutputStream.toByteArray());
927-
fileSize = byteArrayOutputStream.size(); // Update the file size to the compressed size
930+
// Update the file size to the compressed size
931+
fileSize = byteArrayOutputStream.size();
928932
}
929933
if (this.driverUri.presignedUrlDisabled()) {
930934
DatabendPresignClient cli = new DatabendPresignClientV1(httpClient, this.httpUri.toString());
@@ -1128,7 +1132,7 @@ private void doHeartbeat(ArrayList<QueryLiveness> queryLivenesses ) {
11281132
logger.warning("fail to encode heartbeat body: " + e);
11291133
} catch (SQLException e) {
11301134
logger.warning("fail to send heartbeat: " + e);
1131-
} catch (IOException e) {
1135+
} catch (Exception e) {
11321136
logger.warning("fail to send heartbeat: " + e);
11331137
throw new RuntimeException(e);
11341138
}

databend-jdbc/src/test/java/com/databend/jdbc/TestDatabendDatabaseMetaData.java

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import org.testng.annotations.Test;
66

77
import java.math.BigDecimal;
8+
import java.math.RoundingMode;
89
import java.sql.*;
910
import java.util.ArrayList;
1011
import java.util.List;
@@ -59,10 +60,12 @@ public void setUp()
5960
Connection c = Utils.createConnection();
6061
c.createStatement().execute("drop table if exists test_column_meta");
6162
c.createStatement().execute("drop table if exists decimal_test");
63+
c.createStatement().execute("drop table if exists decimal_test_1");
6264
c.createStatement().execute("drop table if exists test_comment");
6365
c.createStatement().execute("drop view if exists v_test_comment");
6466
c.createStatement().execute("create table test_column_meta (nu1 uint8 null, u1 uint8, u2 uint16, u3 uint32, u4 uint64, i1 int8, i2 int16, i3 int32, i4 int64, f1 float32, f2 float64, s1 string,d1 date, d2 datetime, v1 variant, a1 array(int64), t1 Tuple(x Int64, y Int64 NULL)) engine = fuse");
6567
c.createStatement().execute("create table decimal_test (a decimal(4,2))");
68+
c.createStatement().execute("create table decimal_test_1 (a decimal(15,12))");
6669
c.createStatement().execute("create table test_comment (a int comment 'test comment')");
6770
c.createStatement().execute("create view v_test_comment as select * from test_comment");
6871
// json data
@@ -236,6 +239,45 @@ public void testGetObjectWithDecimal() throws Exception {
236239
}
237240
}
238241

242+
@Test(groups = {"IT"})
243+
public void testGetBigDecimal() throws Exception {
244+
String bigDecimalStr = "123.456789012345";
245+
String scaleBigDecimalStr = "123.46";
246+
String columnLabel = "a";
247+
try (Connection connection = Utils.createConnection()) {
248+
connection.createStatement().execute(String.format("insert into decimal_test_1 values(%s)", bigDecimalStr));
249+
ResultSet rs = connection.createStatement().executeQuery("select * from decimal_test_1");
250+
while (rs.next()) {
251+
// get BigDecimal using columnIndex
252+
BigDecimal value = rs.getBigDecimal(1);
253+
Assert.assertEquals(value.stripTrailingZeros().toPlainString(), bigDecimalStr);
254+
255+
// get BigDecimal using columnLabel
256+
value = rs.getBigDecimal(columnLabel);
257+
Assert.assertEquals(value.stripTrailingZeros().toPlainString(), bigDecimalStr);
258+
259+
// get BigDecimal using column index + scale (No longer recommended)
260+
value = rs.getBigDecimal(1, 2);
261+
Assert.assertEquals(value.stripTrailingZeros().toPlainString(), scaleBigDecimalStr);
262+
263+
// get BigDecimal using columnLabel + scale (No longer recommended)
264+
value = rs.getBigDecimal(columnLabel, 2);
265+
Assert.assertEquals(value.stripTrailingZeros().toPlainString(), scaleBigDecimalStr);
266+
267+
// get BigDecimal with scale using index (recommended)
268+
value = rs.getBigDecimal(1);
269+
value = value.setScale(2, RoundingMode.HALF_UP);
270+
Assert.assertEquals(value.stripTrailingZeros().toPlainString(), scaleBigDecimalStr);
271+
272+
// get BigDecimal with scale using columnLabel + scale (recommended)
273+
value = rs.getBigDecimal(columnLabel);
274+
value = value.setScale(2, RoundingMode.HALF_UP);
275+
Assert.assertEquals(value.stripTrailingZeros().toPlainString(), scaleBigDecimalStr);
276+
277+
}
278+
}
279+
}
280+
239281
@Test(groups = {"IT"})
240282
public void testGetPrimaryKeys() throws Exception {
241283
try (Connection connection = Utils.createConnection()) {

0 commit comments

Comments
 (0)