Skip to content

Commit

Permalink
Remove filtering by DatabaseName when extracting DatabasesV (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
dawidxc authored Jan 10, 2024
1 parent d7613cc commit 12918ad
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 89 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@
package com.google.edwmigration.dumper.application.dumper.connector.teradata;

import static com.google.edwmigration.dumper.application.dumper.connector.teradata.TeradataUtils.optionalIf;
import static com.google.edwmigration.dumper.application.dumper.connector.teradata.query.TeradataSelectBuilder.and;
import static com.google.edwmigration.dumper.application.dumper.connector.teradata.query.TeradataSelectBuilder.eq;
import static com.google.edwmigration.dumper.application.dumper.connector.teradata.query.TeradataSelectBuilder.identifier;
import static com.google.edwmigration.dumper.application.dumper.connector.teradata.query.TeradataSelectBuilder.in;
Expand Down Expand Up @@ -59,13 +58,12 @@ class MetadataQueryGenerator {
.where(eq(identifier("InfoKey"), stringLiteral("VERSION")))
.serialize();

static String createSelectForDatabasesV(
OptionalLong userRows, OptionalLong dbRows, Optional<Expression> condition) {
static String createSelectForDatabasesV(OptionalLong userRows, OptionalLong dbRows) {
if (!userRows.isPresent() && !dbRows.isPresent()) {
return createSimpleSelect("DBC.DatabasesV", condition);
return select("%s").from("DBC.DatabasesV").serialize();
}
SelectExpression usersSelect = createSingleDbKindSelectFromDatabasesV("U", userRows, condition);
SelectExpression dbsSelect = createSingleDbKindSelectFromDatabasesV("D", dbRows, condition);
SelectExpression usersSelect = createSingleDbKindSelectFromDatabasesV("U", userRows);
SelectExpression dbsSelect = createSingleDbKindSelectFromDatabasesV("D", dbRows);
return select("%s")
.from(
subquery(
Expand Down Expand Up @@ -122,19 +120,13 @@ static String createSelectForDiskSpaceV(OptionalLong rowCount, Optional<Expressi
}

private static SelectExpression createSingleDbKindSelectFromDatabasesV(
String dbKind, OptionalLong rowCount, Optional<Expression> condition) {
String dbKind, OptionalLong rowCount) {
Expression dbKindCondition = eq(identifier("DBKind"), stringLiteral(dbKind));
Optional<LimitedSelectParams> params =
optionalIf(
rowCount.isPresent(),
() -> LimitedSelectParams.create(rowCount.getAsLong(), "PermSpace"));
return createLimitedSelect(
params,
"DBC.DatabasesV",
Optional.of(
condition
.<Expression>map(innerCondition -> and(innerCondition, dbKindCondition))
.orElse(dbKindCondition)));
return createLimitedSelect(params, "DBC.DatabasesV", Optional.of(dbKindCondition));
}

private static SelectExpression createLimitedSelect(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,7 +131,7 @@ public void addTasksTo(List<? super Task<?>> out, ConnectorArguments arguments)
// out.add(new JdbcSelectTask(ColumnsFormat.ZIP_ENTRY_NAME, // Was: teradata.columns.csv
// "SELECT \"DatabaseName\", \"TableName\", \"ColumnId\", \"ColumnName\", \"ColumnType\" FROM
// DBC.Columns" + whereDatabaseNameClause + " ;"));
out.add(createTaskForDatabasesV(arguments, databaseNameCondition));
out.add(createTaskForDatabasesV(arguments));
// out.add(new TeradataJdbcSelectTask("td.dbc.Tables.others.csv", "SELECT * FROM DBC.Tables
// WHERE TableKind <> 'F' ORDER BY 1,2,3,4;"));
// out.add(new TeradataJdbcSelectTask("td.dbc.Tables.functions.csv", "SELECT * FROM DBC.Tables
Expand Down Expand Up @@ -228,8 +228,7 @@ private TeradataJdbcSelectTask createTaskForTableTextV(
createSelectForTableTextV(textMaxLength, databaseNameCondition));
}

private TeradataJdbcSelectTask createTaskForDatabasesV(
ConnectorArguments arguments, Optional<Expression> databaseNameCondition)
private TeradataJdbcSelectTask createTaskForDatabasesV(ConnectorArguments arguments)
throws MetadataDumperUsageException {
OptionalLong userRows =
parseMaxRows(arguments, TeradataMetadataConnectorProperties.DATABASES_V_USERS_MAX_ROWS);
Expand All @@ -238,7 +237,7 @@ private TeradataJdbcSelectTask createTaskForDatabasesV(
return new TeradataJdbcSelectTask(
DatabasesVFormat.ZIP_ENTRY_NAME,
TaskCategory.REQUIRED,
MetadataQueryGenerator.createSelectForDatabasesV(userRows, dbRows, databaseNameCondition));
MetadataQueryGenerator.createSelectForDatabasesV(userRows, dbRows));
}

private TeradataJdbcSelectTask createTaskForAllTempTablesVX(ConnectorArguments arguments) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,6 @@
import static com.google.edwmigration.dumper.application.dumper.connector.teradata.MetadataQueryGenerator.createSelectForTableTextV;
import static com.google.edwmigration.dumper.application.dumper.connector.teradata.query.TeradataSelectBuilder.eq;
import static com.google.edwmigration.dumper.application.dumper.connector.teradata.query.TeradataSelectBuilder.identifier;
import static com.google.edwmigration.dumper.application.dumper.connector.teradata.query.TeradataSelectBuilder.in;
import static com.google.edwmigration.dumper.application.dumper.connector.teradata.query.TeradataSelectBuilder.stringLiteral;
import static com.google.edwmigration.dumper.application.dumper.test.DumperTestUtils.assertQueryEquals;

Expand All @@ -41,35 +40,18 @@ public void createSelectForDatabasesV_noLimits() {
// Act
String query =
MetadataQueryGenerator.createSelectForDatabasesV(
/* userRows= */ OptionalLong.empty(),
/* dbRows= */ OptionalLong.empty(),
/* condition= */ Optional.empty());
/* userRows= */ OptionalLong.empty(), /* dbRows= */ OptionalLong.empty());

// Assert
assertQueryEquals("SELECT %s FROM DBC.DatabasesV", query);
}

@Test
public void createSelectForDatabasesV_condition() {
// Act
String query =
MetadataQueryGenerator.createSelectForDatabasesV(
/* userRows= */ OptionalLong.empty(),
/* dbRows= */ OptionalLong.empty(),
/* condition= */ Optional.of(eq(identifier("DatabaseName"), stringLiteral("abc"))));

// Assert
assertQueryEquals("SELECT %s FROM DBC.DatabasesV WHERE DatabaseName = 'abc'", query);
}

@Test
public void createSelectForDatabasesV_usersLimit() {
// Act
String query =
MetadataQueryGenerator.createSelectForDatabasesV(
/* userRows= */ OptionalLong.of(13),
/* dbRows= */ OptionalLong.empty(),
/* condition= */ Optional.empty());
/* userRows= */ OptionalLong.of(13), /* dbRows= */ OptionalLong.empty());

// Assert
assertQueryEquals(
Expand All @@ -82,35 +64,12 @@ public void createSelectForDatabasesV_usersLimit() {
query);
}

@Test
public void createSelectForDatabasesV_usersLimitAndCondition() {
// Act
String query =
MetadataQueryGenerator.createSelectForDatabasesV(
/* userRows= */ OptionalLong.of(13),
/* dbRows= */ OptionalLong.empty(),
/* condition= */ Optional.of(eq(identifier("DatabaseName"), stringLiteral("abc"))));

// Assert
assertQueryEquals(
"SELECT %s FROM ("
+ " SELECT * FROM (SELECT TOP 13 * FROM DBC.DatabasesV"
+ " WHERE (DatabaseName = 'abc' AND DBKind = 'U') ORDER BY PermSpace DESC) AS users"
+ " UNION ALL "
+ " SELECT * FROM (SELECT * FROM DBC.DatabasesV"
+ " WHERE (DatabaseName = 'abc' AND DBKind = 'D')) AS dbs"
+ ") AS t",
query);
}

@Test
public void createSelectForDatabasesV_dbsLimit() {
// Act
String query =
MetadataQueryGenerator.createSelectForDatabasesV(
/* userRows= */ OptionalLong.empty(),
/* dbRows= */ OptionalLong.of(18),
/* condition= */ Optional.empty());
/* userRows= */ OptionalLong.empty(), /* dbRows= */ OptionalLong.of(18));

// Assert
assertQueryEquals(
Expand All @@ -128,9 +87,7 @@ public void createSelectForDatabasesV_usersAndDbsLimit() {
// Act
String query =
MetadataQueryGenerator.createSelectForDatabasesV(
/* userRows= */ OptionalLong.of(15),
/* dbRows= */ OptionalLong.of(18),
/* condition= */ Optional.empty());
/* userRows= */ OptionalLong.of(15), /* dbRows= */ OptionalLong.of(18));

// Assert
assertQueryEquals(
Expand All @@ -144,30 +101,6 @@ public void createSelectForDatabasesV_usersAndDbsLimit() {
query);
}

@Test
public void createSelectForDatabasesV_usersAndDbsLimitAndCondition() {
// Act
String query =
MetadataQueryGenerator.createSelectForDatabasesV(
/* userRows= */ OptionalLong.of(15),
/* dbRows= */ OptionalLong.of(18),
/* condition= */ Optional.of(
in(identifier("DatabaseName"), ImmutableList.of("db1", "db2"))));

// Assert
assertQueryEquals(
"SELECT %s FROM ("
+ " SELECT * FROM (SELECT TOP 15 * FROM DBC.DatabasesV"
+ " WHERE (DatabaseName IN ('db1', 'db2') AND DBKind = 'U')"
+ " ORDER BY PermSpace DESC) AS users"
+ " UNION ALL "
+ " SELECT * FROM (SELECT TOP 18 * FROM DBC.DatabasesV"
+ " WHERE (DatabaseName IN ('db1', 'db2') AND DBKind = 'D')"
+ " ORDER BY PermSpace DESC) AS dbs"
+ ") AS t",
query);
}

@Test
public void dbcInfoQuery() {
assertQueryEquals(
Expand Down

0 comments on commit 12918ad

Please sign in to comment.