Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Get ResultSet meta data before execution #1434

Draft
wants to merge 1 commit into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,10 @@
import com.clickhouse.data.format.BinaryStreamUtils;

public interface ClickHousePreparedStatement extends PreparedStatement {
default ResultSetMetaData describeQueryResult() throws SQLException {
return null;
}

@Override
default void setNull(int parameterIndex, int sqlType) throws SQLException {
setNull(parameterIndex, sqlType, null);
Expand Down Expand Up @@ -109,7 +113,12 @@ default void setClob(int parameterIndex, Clob x) throws SQLException {
@Override
default ResultSetMetaData getMetaData() throws SQLException {
ResultSet currentResult = getResultSet();
return currentResult != null ? currentResult.getMetaData() : null;
if (currentResult != null) {
return currentResult.getMetaData();
} else if (getLargeUpdateCount() != -1L) {
return null;
}
return describeQueryResult();
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
import java.sql.Date;
import java.sql.ParameterMetaData;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Time;
import java.sql.Timestamp;
import java.time.LocalDate;
Expand Down Expand Up @@ -35,7 +37,9 @@
import com.clickhouse.data.value.ClickHouseStringValue;
import com.clickhouse.logging.Logger;
import com.clickhouse.logging.LoggerFactory;
import com.clickhouse.jdbc.ClickHouseConnection;
import com.clickhouse.jdbc.ClickHousePreparedStatement;
import com.clickhouse.jdbc.ClickHouseResultSetMetaData;
import com.clickhouse.jdbc.JdbcParameterizedQuery;
import com.clickhouse.jdbc.SqlExceptionUtils;
import com.clickhouse.jdbc.parser.ClickHouseSqlStatement;
Expand Down Expand Up @@ -239,6 +243,39 @@ protected int getMaxParameterIndex() {
return templates.length;
}

@Override
public ResultSetMetaData describeQueryResult() throws SQLException {
if (parsedStmt.isRecognized() && !parsedStmt.isQuery()) {
return null;
}

final String[] vals;
final int len = values.length;
if (batch.isEmpty()) {
vals = new String[len];
System.arraycopy(this.values, 0, vals, 0, len);
} else {
vals = batch.get(0);
}
for (int i = 0; i < len; i++) {
if (vals[i] == null) {
vals[i] = ClickHouseValues.NULL_EXPR;
}
}

final ClickHouseConnection conn = getConnection();
StringBuilder sb = new StringBuilder("desc (");
preparedQuery.apply(sb, vals);
List<ClickHouseColumn> columns = new LinkedList<>();
try (Statement stmt = conn.createStatement(); ResultSet rs = stmt.executeQuery(sb.append(')').toString())) {
while (rs.next()) {
columns.add(ClickHouseColumn.of(rs.getString(1), rs.getString(2)));
}
}
return ClickHouseResultSetMetaData.of(conn.getJdbcConfig(), conn.getCurrentDatabase(), "",
Collections.unmodifiableList(new ArrayList<>(columns)), mapper, conn.getTypeMap());
}

@Override
public ResultSet executeQuery() throws SQLException {
ensureParams();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.math.BigInteger;
import java.net.Inet4Address;
import java.net.Inet6Address;
import java.net.MalformedURLException;
Expand All @@ -16,6 +17,7 @@
import java.sql.ParameterMetaData;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.sql.Statement;
import java.sql.Timestamp;
Expand Down Expand Up @@ -2005,6 +2007,53 @@ public void testInsertWithSettings() throws SQLException {
}
}

@Test(groups = "integration")
public void testGetMetaData() throws SQLException {
try (Connection conn = newConnection(new Properties());
PreparedStatement ps = conn.prepareStatement("select ? a, ? b")) {
ResultSetMetaData md = ps.getMetaData();
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What happens if we pass DDL (e.g. CREATE TABLE) to conn.prepareStatement? It would be nice to add a test in my opinion.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for taking your time to review the PR!

What happens if we pass DDL (e.g. CREATE TABLE) to conn.prepareStatement?

null will be returned at this point. Below is the check in SqlBasedPreparedStatement.java. However, in ClickHouse, create table on cluster will actually return ResultSet, but I guess we don't need that as it's not helping for push-down implementation.

if (parsedStmt.isRecognized() && !parsedStmt.isQuery()) {
    return null;
}

It would be nice to add a test in my opinion.

Yes, the implementation is incomplete and I'll add more tests for sure.

Assert.assertEquals(md.getColumnCount(), 2);
Assert.assertEquals(md.getColumnName(1), "a");
Assert.assertEquals(md.getColumnTypeName(1), "Nullable(Nothing)");
Assert.assertEquals(md.getColumnName(2), "b");
Assert.assertEquals(md.getColumnTypeName(2), "Nullable(Nothing)");

ps.setString(1, "x");
md = ps.getMetaData();
Assert.assertEquals(md.getColumnCount(), 2);
Assert.assertEquals(md.getColumnName(1), "a");
Assert.assertEquals(md.getColumnTypeName(1), "String");
Assert.assertEquals(md.getColumnName(2), "b");
Assert.assertEquals(md.getColumnTypeName(2), "Nullable(Nothing)");

ps.setObject(2, new BigInteger("12345"));
md = ps.getMetaData();
Assert.assertEquals(md.getColumnCount(), 2);
Assert.assertEquals(md.getColumnName(1), "a");
Assert.assertEquals(md.getColumnTypeName(1), "String");
Assert.assertEquals(md.getColumnName(2), "b");
Assert.assertEquals(md.getColumnTypeName(2), "UInt16");

ps.addBatch();
ps.setInt(1, 2);
md = ps.getMetaData();
Assert.assertEquals(md.getColumnCount(), 2);
Assert.assertEquals(md.getColumnName(1), "a");
Assert.assertEquals(md.getColumnTypeName(1), "String");
Assert.assertEquals(md.getColumnName(2), "b");
Assert.assertEquals(md.getColumnTypeName(2), "UInt16");

ps.clearBatch();
ps.clearParameters();
md = ps.getMetaData();
Assert.assertEquals(md.getColumnCount(), 2);
Assert.assertEquals(md.getColumnName(1), "a");
Assert.assertEquals(md.getColumnTypeName(1), "Nullable(Nothing)");
Assert.assertEquals(md.getColumnName(2), "b");
Assert.assertEquals(md.getColumnTypeName(2), "Nullable(Nothing)");
}
}

@Test(groups = "integration")
public void testGetParameterMetaData() throws SQLException {
try (Connection conn = newConnection(new Properties());
Expand Down
Loading