### Question:
ShardMetadataRecordCursor implements RecordCursor { @VisibleForTesting static Iterator<Long> getTableIds(IDBI dbi, TupleDomain<Integer> tupleDomain) { Map<Integer, Domain> domains = tupleDomain.getDomains().get(); Domain schemaNameDomain = domains.get(getColumnIndex(SHARD_METADATA, SCHEMA_NAME)); Domain tableNameDomain = domains.get(getColumnIndex(SHARD_METADATA, TABLE_NAME)); List<String> values = new ArrayList<>(); StringBuilder sql = new StringBuilder("SELECT table_id FROM tables "); if (schemaNameDomain != null || tableNameDomain != null) { sql.append("WHERE "); List<String> predicates = new ArrayList<>(); if (tableNameDomain != null && tableNameDomain.isSingleValue()) { predicates.add("table_name = ?"); values.add(getStringValue(tableNameDomain.getSingleValue())); } if (schemaNameDomain != null && schemaNameDomain.isSingleValue()) { predicates.add("schema_name = ?"); values.add(getStringValue(schemaNameDomain.getSingleValue())); } sql.append(Joiner.on(" AND ").join(predicates)); } ImmutableList.Builder<Long> tableIds = ImmutableList.builder(); try (Connection connection = dbi.open().getConnection(); PreparedStatement statement = connection.prepareStatement(sql.toString())) { for (int i = 0; i < values.size(); i++) { statement.setString(i + 1, values.get(i)); } try (ResultSet resultSet = statement.executeQuery()) { while (resultSet.next()) { tableIds.add(resultSet.getLong("table_id")); } } } catch (SQLException | DBIException e) { throw metadataError(e); } return tableIds.build().iterator(); } ShardMetadataRecordCursor(IDBI dbi, TupleDomain<Integer> tupleDomain); @Override long getCompletedBytes(); @Override long getReadTimeNanos(); @Override Type getType(int field); @Override boolean advanceNextPosition(); @Override boolean getBoolean(int field); @Override long getLong(int field); @Override double getDouble(int field); @Override Slice getSlice(int field); @Override Object getObject(int field); @Override boolean isNull(int field); @Override void close(); static final SchemaTableName SHARD_METADATA_TABLE_NAME; static final ConnectorTableMetadata SHARD_METADATA; }### Answer:
@Test public void testNoSchemaFilter() { createTable(tableMetadataBuilder(new SchemaTableName("other", "orders")) .column("orderkey", BIGINT) .build()); createTable(tableMetadataBuilder(new SchemaTableName("schema1", "foo")) .column("orderkey", BIGINT) .build()); TupleDomain<Integer> tupleDomain = TupleDomain.withColumnDomains( ImmutableMap.<Integer, Domain>builder() .put(1, Domain.singleValue(createVarcharType(10), utf8Slice("orders"))) .build()); MetadataDao metadataDao = dummyHandle.attach(MetadataDao.class); Set<Long> actual = ImmutableSet.copyOf(ShardMetadataRecordCursor.getTableIds(dbi, tupleDomain)); Set<Long> expected = ImmutableSet.of( metadataDao.getTableInformation("other", "orders").getTableId(), metadataDao.getTableInformation("test", "orders").getTableId()); assertEquals(actual, expected); }
@Test public void testNoTableFilter() { createTable(tableMetadataBuilder(new SchemaTableName("test", "orders2")) .column("orderkey", BIGINT) .build()); createTable(tableMetadataBuilder(new SchemaTableName("schema1", "foo")) .column("orderkey", BIGINT) .build()); TupleDomain<Integer> tupleDomain = TupleDomain.withColumnDomains( ImmutableMap.<Integer, Domain>builder() .put(0, Domain.singleValue(createVarcharType(10), utf8Slice("test"))) .build()); MetadataDao metadataDao = dummyHandle.attach(MetadataDao.class); Set<Long> actual = ImmutableSet.copyOf(ShardMetadataRecordCursor.getTableIds(dbi, tupleDomain)); Set<Long> expected = ImmutableSet.of( metadataDao.getTableInformation("test", "orders").getTableId(), metadataDao.getTableInformation("test", "orders2").getTableId()); assertEquals(actual, expected); }