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

SNOW-805980: Show Pagination POC at JDBC driver level #1502

Closed
wants to merge 2 commits into from
Closed
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 @@ -1498,6 +1498,8 @@ public ResultSet getTables(
}
}

// adding this just for POC purpose.
showTablesCommand += " limit 1";
logger.debug("sql command to get table metadata: {}", showTablesCommand);

resultSet = executeAndReturnEmptyResultIfNotFound(statement, showTablesCommand, GET_TABLES);
Expand All @@ -1510,51 +1512,83 @@ public ResultSet getTables(
Arrays.toString(types));

return new SnowflakeDatabaseMetaDataQueryResultSet(GET_TABLES, resultSet, statement) {

// maintain last table which was returned to use this information for next pagination call
String lastTable = null;

@Override
public boolean next() throws SQLException {
logger.debug("public boolean next()", false);
incrementRow();

// iterate throw the show table result until we find an entry
// that matches the table name
// get the next table
while (showObjectResultSet.next()) {
String tableName = showObjectResultSet.getString(2);

String dbName;
String schemaName;
String kind;
String comment;

if (viewOnly) {
dbName = showObjectResultSet.getString(4);
schemaName = showObjectResultSet.getString(5);
kind = "VIEW";
comment = showObjectResultSet.getString(7);
} else {
dbName = showObjectResultSet.getString(3);
schemaName = showObjectResultSet.getString(4);
kind = showObjectResultSet.getString(5);
comment = showObjectResultSet.getString(6);
}
if (createRow()) return true;
}

if ((compiledTablePattern == null || compiledTablePattern.matcher(tableName).matches())
&& (compiledSchemaPattern == null
|| compiledSchemaPattern.matcher(schemaName).matches())) {
nextRow[0] = dbName;
nextRow[1] = schemaName;
nextRow[2] = tableName;
nextRow[3] = kind;
nextRow[4] = comment;
nextRow[5] = null;
nextRow[6] = null;
nextRow[7] = null;
nextRow[8] = null;
nextRow[9] = null;
return true;
}
// if no more results are found, now trigger pagination call
// Note: In actuality, instead of show tables command, a pagination call with a token will be invoked here.
// For now, a show command is invoked.
ResultSet resultSet1 = executeAndReturnEmptyResultIfNotFound(statement, "show /* JDBC:DatabaseMetaData.getTables() */ tables in schema \"JDBC_DB1\".\"JDBC_SCHEMA11\" limit 1 from '" + lastTable + "'", GET_TABLES);
Copy link
Collaborator

@sfc-gh-igarish sfc-gh-igarish Aug 30, 2023

Choose a reason for hiding this comment

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

This won't work everytime, if table list change after getting first set of values. Do we have any design doc or something like that? Let's review that before you make code changes. If we don't have any doc, please start a doc first.

Copy link
Collaborator

Choose a reason for hiding this comment

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

+1 to what Ilesh said. It would be great to see a one pager that first outlines what is it that we're trying to achieve and how we're thinking of doing it. Looking at code is great but it does not give the big picture.

sendInBandTelemetryMetadataMetrics(
resultSet1,
"getTables",
originalCatalog,
originalSchemaPattern,
tableNamePattern,
Arrays.toString(types));
this.showObjectResultSet = resultSet1;

// get first result from pagination call and return it as a part of next()
boolean foundAnyObject = false;
while (showObjectResultSet.next()) {
foundAnyObject = true;
if (createRow()) return true;
}

// if no results are found out of pagination, that means all objects are found. Close the ResultSet and show is done.
if (!foundAnyObject) {
close();
return false;
}
return true;
}

private boolean createRow() throws SQLException {
String tableName = showObjectResultSet.getString(2);

String dbName;
String schemaName;
String kind;
String comment;

if (viewOnly) {
dbName = showObjectResultSet.getString(4);
schemaName = showObjectResultSet.getString(5);
kind = "VIEW";
comment = showObjectResultSet.getString(7);
} else {
dbName = showObjectResultSet.getString(3);
schemaName = showObjectResultSet.getString(4);
kind = showObjectResultSet.getString(5);
comment = showObjectResultSet.getString(6);
}

close(); // close
if ((compiledTablePattern == null || compiledTablePattern.matcher(tableName).matches())
&& (compiledSchemaPattern == null
|| compiledSchemaPattern.matcher(schemaName).matches())) {
nextRow[0] = dbName;
nextRow[1] = schemaName;
nextRow[2] = lastTable = tableName;
nextRow[3] = kind;
nextRow[4] = comment;
nextRow[5] = null;
nextRow[6] = null;
nextRow[7] = null;
nextRow[8] = null;
nextRow[9] = null;
return true;
}
return false;
}
};
Expand Down
Loading