This is an automated email from the ASF dual-hosted git repository. jackie pushed a commit to branch master in repository https://gitbox.apache.org/repos/asf/pinot.git
The following commit(s) were added to refs/heads/master by this push: new 0797e05da6 [bugfix] Fix null value handling in PinotResultSet (#10177) 0797e05da6 is described below commit 0797e05da61c37629da3e99bdeb4d96dc60d7911 Author: ImprovingRichard <61800572+improvingrich...@users.noreply.github.com> AuthorDate: Thu Jan 26 18:46:48 2023 -0600 [bugfix] Fix null value handling in PinotResultSet (#10177) --- .../org/apache/pinot/client/PinotConnection.java | 8 ++++++ .../pinot/client/PinotPreparedStatement.java | 5 ++-- .../org/apache/pinot/client/PinotResultSet.java | 32 +++++++++++----------- .../org/apache/pinot/client/PinotStatement.java | 5 ++-- .../org/apache/pinot/client/utils/DriverUtils.java | 12 ++++++++ 5 files changed, 42 insertions(+), 20 deletions(-) diff --git a/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotConnection.java b/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotConnection.java index 6847122605..b7186a7977 100644 --- a/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotConnection.java +++ b/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotConnection.java @@ -30,6 +30,7 @@ import org.apache.pinot.client.base.AbstractBaseConnection; import org.apache.pinot.client.controller.PinotControllerTransport; import org.apache.pinot.client.controller.PinotControllerTransportFactory; import org.apache.pinot.client.controller.response.ControllerTenantBrokerResponse; +import org.apache.pinot.spi.utils.CommonConstants.Broker.Request.QueryOptionKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -41,6 +42,7 @@ public class PinotConnection extends AbstractBaseConnection { private boolean _closed; private String _controllerURL; private PinotControllerTransport _controllerTransport; + private final boolean _enableNullHandling; public static final String BROKER_LIST = "brokers"; PinotConnection(String controllerURL, PinotClientTransport transport, String tenant, @@ -64,12 +66,18 @@ public class PinotConnection extends AbstractBaseConnection { brokers = getBrokerList(controllerURL, tenant); } _session = new org.apache.pinot.client.Connection(properties, brokers, transport); + + _enableNullHandling = Boolean.parseBoolean(properties.getProperty(QueryOptionKey.ENABLE_NULL_HANDLING)); } public org.apache.pinot.client.Connection getSession() { return _session; } + public boolean isNullHandlingEnabled() { + return _enableNullHandling; + } + private List<String> getBrokerList(String controllerURL, String tenant) { ControllerTenantBrokerResponse controllerTenantBrokerResponse = _controllerTransport.getBrokersFromController(controllerURL, tenant); diff --git a/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotPreparedStatement.java b/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotPreparedStatement.java index e3b601859f..f2b44aeae6 100644 --- a/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotPreparedStatement.java +++ b/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotPreparedStatement.java @@ -34,7 +34,7 @@ import org.apache.pinot.client.utils.DriverUtils; public class PinotPreparedStatement extends AbstractBasePreparedStatement { private static final String LIMIT_STATEMENT = "LIMIT"; - private Connection _connection; + private PinotConnection _connection; private org.apache.pinot.client.Connection _session; private ResultSetGroup _resultSetGroup; private PreparedStatement _preparedStatement; @@ -51,6 +51,7 @@ public class PinotPreparedStatement extends AbstractBasePreparedStatement { if (!DriverUtils.queryContainsLimitStatement(_query)) { _query += " " + LIMIT_STATEMENT + " " + _maxRows; } + _query = DriverUtils.enableNullHandling(_connection, _query); _preparedStatement = new PreparedStatement(_session, _query); } @@ -177,7 +178,7 @@ public class PinotPreparedStatement extends AbstractBasePreparedStatement { throws SQLException { validateState(); try { - _resultSetGroup = _session.execute(sql); + _resultSetGroup = _session.execute(DriverUtils.enableNullHandling(_connection, sql)); if (_resultSetGroup.getResultSetCount() == 0) { _resultSet = PinotResultSet.empty(); return _resultSet; diff --git a/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotResultSet.java b/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotResultSet.java index 3e900c64ea..4ceaa76094 100644 --- a/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotResultSet.java +++ b/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotResultSet.java @@ -194,8 +194,7 @@ public class PinotResultSet extends AbstractBaseResultSet { throws SQLException { try { String value = getString(columnIndex); - BigDecimal bigDecimal = new BigDecimal(value).setScale(scale); - return bigDecimal; + return value == null ? null : new BigDecimal(value).setScale(scale); } catch (Exception e) { throw new SQLException("Unable to fetch BigDecimal value", e); } @@ -205,7 +204,8 @@ public class PinotResultSet extends AbstractBaseResultSet { public boolean getBoolean(int columnIndex) throws SQLException { validateColumn(columnIndex); - return Boolean.parseBoolean(_resultSet.getString(_currentRow, columnIndex - 1)); + String value = getString(columnIndex); + return value == null ? false : Boolean.parseBoolean(value); } @Override @@ -213,7 +213,7 @@ public class PinotResultSet extends AbstractBaseResultSet { throws SQLException { try { String value = getString(columnIndex); - return Hex.decodeHex(value.toCharArray()); + return value == null ? null : Hex.decodeHex(value.toCharArray()); } catch (Exception e) { throw new SQLException(String.format("Unable to fetch value for column %d", columnIndex), e); } @@ -232,7 +232,7 @@ public class PinotResultSet extends AbstractBaseResultSet { throws SQLException { try { String value = getString(columnIndex); - return DateTimeUtils.getDateFromString(value, cal); + return value == null ? null : DateTimeUtils.getDateFromString(value, cal); } catch (Exception e) { throw new SQLException("Unable to fetch date", e); } @@ -242,32 +242,32 @@ public class PinotResultSet extends AbstractBaseResultSet { public double getDouble(int columnIndex) throws SQLException { validateColumn(columnIndex); - - return _resultSet.getDouble(_currentRow, columnIndex - 1); + String value = getString(columnIndex); + return value == null ? 0.0 : Double.parseDouble(value); } @Override public float getFloat(int columnIndex) throws SQLException { validateColumn(columnIndex); - - return _resultSet.getFloat(_currentRow, columnIndex - 1); + String value = getString(columnIndex); + return value == null ? 0.0f : Float.parseFloat(value); } @Override public int getInt(int columnIndex) throws SQLException { validateColumn(columnIndex); - - return _resultSet.getInt(_currentRow, columnIndex - 1); + String value = getString(columnIndex); + return value == null ? 0 : Integer.parseInt(value); } @Override public long getLong(int columnIndex) throws SQLException { validateColumn(columnIndex); - - return _resultSet.getLong(_currentRow, columnIndex - 1); + String value = getString(columnIndex); + return value == null ? 0 : Long.parseLong(value); } @Override @@ -282,7 +282,7 @@ public class PinotResultSet extends AbstractBaseResultSet { public short getShort(int columnIndex) throws SQLException { Integer value = getInt(columnIndex); - return value.shortValue(); + return value == null ? null : value.shortValue(); } @Override @@ -359,7 +359,7 @@ public class PinotResultSet extends AbstractBaseResultSet { throws SQLException { try { String value = getString(columnIndex); - return DateTimeUtils.getTimeFromString(value, cal); + return value == null ? null : DateTimeUtils.getTimeFromString(value, cal); } catch (Exception e) { throw new SQLException("Unable to fetch date", e); } @@ -370,7 +370,7 @@ public class PinotResultSet extends AbstractBaseResultSet { throws SQLException { try { String value = getString(columnIndex); - return DateTimeUtils.getTimestampFromString(value, cal); + return value == null ? null : DateTimeUtils.getTimestampFromString(value, cal); } catch (Exception e) { throw new SQLException("Unable to fetch date", e); } diff --git a/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotStatement.java b/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotStatement.java index dcedad18a9..9b325b2f1f 100644 --- a/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotStatement.java +++ b/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/PinotStatement.java @@ -28,7 +28,7 @@ import org.apache.pinot.client.utils.DriverUtils; public class PinotStatement extends AbstractBaseStatement { private static final String LIMIT_STATEMENT = "LIMIT"; - private final Connection _connection; + private final PinotConnection _connection; private final org.apache.pinot.client.Connection _session; private boolean _closed; private ResultSet _resultSet; @@ -63,7 +63,8 @@ public class PinotStatement extends AbstractBaseStatement { if (!DriverUtils.queryContainsLimitStatement(sql)) { sql += " " + LIMIT_STATEMENT + " " + _maxRows; } - ResultSetGroup resultSetGroup = _session.execute(sql); + String enabledSql = DriverUtils.enableNullHandling(_connection, sql); + ResultSetGroup resultSetGroup = _session.execute(enabledSql); if (resultSetGroup.getResultSetCount() == 0) { _resultSet = PinotResultSet.empty(); return _resultSet; diff --git a/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/utils/DriverUtils.java b/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/utils/DriverUtils.java index d995342be6..73c1a9a72a 100644 --- a/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/utils/DriverUtils.java +++ b/pinot-clients/pinot-jdbc-client/src/main/java/org/apache/pinot/client/utils/DriverUtils.java @@ -36,10 +36,12 @@ import org.apache.commons.configuration.MapConfiguration; import org.apache.commons.lang3.StringUtils; import org.apache.http.NameValuePair; import org.apache.http.client.utils.URLEncodedUtils; +import org.apache.pinot.client.PinotConnection; import org.apache.pinot.common.config.TlsConfig; import org.apache.pinot.common.utils.TlsUtils; import org.apache.pinot.core.auth.BasicAuthUtils; import org.apache.pinot.spi.env.PinotConfiguration; +import org.apache.pinot.spi.utils.CommonConstants.Broker.Request.QueryOptionKey; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -217,4 +219,14 @@ public class DriverUtils { Matcher matcher = pattern.matcher(query); return matcher.find(); } + + public static String enableNullHandling(PinotConnection connection, String query) { + if (query.contains(QueryOptionKey.ENABLE_NULL_HANDLING)) { + return query; + } + + return connection.isNullHandlingEnabled() + ? String.format("SET %s = true; %s", QueryOptionKey.ENABLE_NULL_HANDLING, query) + : query; + } } --------------------------------------------------------------------- To unsubscribe, e-mail: commits-unsubscr...@pinot.apache.org For additional commands, e-mail: commits-h...@pinot.apache.org