Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
KushnirykOleh committed Oct 8, 2024
1 parent c94736d commit 59a17e2
Showing 1 changed file with 43 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
*/
public class TableSnapshotGeneratorDatabricks extends TableSnapshotGenerator {

private final static String LOCATION = "Location";
private final static String TABLE_PROPERTIES = "Table Properties";
private final static String DETAILED_TABLE_INFORMATION_NODE = "# Detailed Table Information";

@Override
public int getPriority(Class<? extends DatabaseObject> objectType, Database database) {
if (database instanceof DatabricksDatabase)
Expand All @@ -35,16 +39,53 @@ protected DatabaseObject snapshotObject(DatabaseObject example, DatabaseSnapshot
String query = String.format("DESCRIBE TABLE EXTENDED %s.%s.%s;", database.getDefaultCatalogName(), database.getDefaultSchemaName(), example.getName());
List<Map<String, ?>> tablePropertiesResponse = Scope.getCurrentScope().getSingleton(ExecutorService.class)
.getExecutor("jdbc", database).queryForList(new RawParameterizedSqlStatement(query));
// DESCRIBE TABLE EXTENDED returns both columns and additional information.
// We need to make sure "Location" is not column in the table, but table location in s3
boolean detailedInformationNode = false;
for (Map<String, ?> tableProperty : tablePropertiesResponse) {
table.setAttribute((String) tableProperty.get("KEY"), tableProperty.get("VALUE"));
if (tableProperty.get("COL_NAME").equals(DETAILED_TABLE_INFORMATION_NODE)) {
detailedInformationNode = true;
continue;
}
if (detailedInformationNode && tableProperty.get("COL_NAME").equals(LOCATION)) {
table.setAttribute(LOCATION, tableProperty.get("DATA_TYPE"));
}
if (detailedInformationNode && tableProperty.get("COL_NAME").equals(TABLE_PROPERTIES)) {
//TODO should i parse and split TableProperties or keep them all together?
table.setAttribute(TABLE_PROPERTIES, tableProperty.get("DATA_TYPE"));
}
//TODO i can get default value for column here `# Column Default Values` ->"COL_NAME" -> "title", "DATA_TYPE" -> "string", "COMMENT" ->
// "'title_test'" --actual default value for the column is in comment column of this resultSet
}
return table;


// String query = String.format("SHOW TBLPROPERTIES %s.%s.%s;", database.getDefaultCatalogName(), database.getDefaultSchemaName(), example.getName());

// String query = String.format("SHOW TABLE EXTENDED IN %s.%s LIKE '%s';", database.getDefaultCatalogName(), database.getDefaultSchemaName(),
// example.getName());
// List<Map<String, ?>> tablePropertiesResponse = Scope.getCurrentScope().getSingleton(ExecutorService.class)
// .getExecutor("jdbc", database).queryForList(new RawParameterizedSqlStatement(query));
// for (Map<String, ?> tableProperty : tablePropertiesResponse) {
// String[] tableParts = ((String) tableProperty.get("INFORMATION")).split("\\r?\\n");
// for (String tablePart : tableParts) {
// if (tablePart.startsWith("Location:")) {
// table.setAttribute("Location", tablePart.replace("Location: ", ""));
// }
// if (tablePart.startsWith("Table Properties:")) {
// table.setAttribute("Table Properties", tablePart.replace("Table Properties: [", "").replace("]", ""));
// }
// }
// }

// String query = String.format("SHOW TBLPROPERTIES %s.%s.%s;", database.getDefaultCatalogName(), database.getDefaultSchemaName(), example.getName());
// List<Map<String, ?>> tablePropertiesResponse = Scope.getCurrentScope().getSingleton(ExecutorService.class)
// .getExecutor("jdbc", database).queryForList(new RawParameterizedSqlStatement(query));
// for (Map<String, ?> tableProperty : tablePropertiesResponse) {
// table.setAttribute((String) tableProperty.get("KEY"), tableProperty.get("VALUE"));
// //TODO combine into
// // "'key0'='value0', 'key1'='value1', .... 'keyN'='valueN'"
// // csv string
//
// }
// return table;
}
Expand Down

0 comments on commit 59a17e2

Please sign in to comment.