use of io.confluent.ksql.name.SourceName in project ksql by confluentinc.
the class QueryRegistryImpl method unregisterQuery.
private void unregisterQuery(final QueryMetadata query) {
if (query instanceof PersistentQueryMetadata) {
final PersistentQueryMetadata persistentQuery = (PersistentQueryMetadata) query;
final QueryId queryId = persistentQuery.getQueryId();
persistentQueries.remove(queryId);
final Set<SharedKafkaStreamsRuntime> toClose = streams.stream().filter(s -> s.getCollocatedQueries().isEmpty()).collect(Collectors.toSet());
streams.removeAll(toClose);
toClose.forEach(SharedKafkaStreamsRuntime::close);
switch(persistentQuery.getPersistentQueryType()) {
case CREATE_SOURCE:
createAsQueries.remove(Iterables.getOnlyElement(persistentQuery.getSourceNames()));
break;
case CREATE_AS:
createAsQueries.remove(persistentQuery.getSinkName().get());
break;
case INSERT:
sinkAndSources(persistentQuery).forEach(sourceName -> insertQueries.computeIfPresent(sourceName, (s, queries) -> {
queries.remove(queryId);
return (queries.isEmpty()) ? null : queries;
}));
break;
default:
}
}
allLiveQueries.remove(query.getQueryId());
notifyDeregister(query);
}
use of io.confluent.ksql.name.SourceName in project ksql by confluentinc.
the class TopicDeleteInjectorTest method shouldThrowIfTopicDoesNotExist.
@Test
public void shouldThrowIfTopicDoesNotExist() {
// Given:
final SourceName STREAM_1 = SourceName.of("stream1");
final DataSource other1 = givenSource(STREAM_1, "topicName");
when(metaStore.getSource(STREAM_1)).thenAnswer(inv -> other1);
when(other1.getKafkaTopicName()).thenReturn("topicName");
final ConfiguredStatement<DropStream> dropStatement = givenStatement("DROP stream1 DELETE TOPIC;", new DropStream(SourceName.of("stream1"), true, true));
doThrow(RuntimeException.class).when(topicClient).deleteTopics(ImmutableList.of("topicName"));
// When:
final Exception e = assertThrows(RuntimeException.class, () -> deleteInjector.inject(dropStatement));
// Then:
assertThat(e.getMessage(), containsString("" + "Could not delete the corresponding kafka topic: topicName"));
}
use of io.confluent.ksql.name.SourceName in project ksql by confluentinc.
the class MetaStoreImpl method putSource.
@Override
public void putSource(final DataSource dataSource, final boolean allowReplace) {
final SourceInfo existing = dataSources.get(dataSource.getName());
if (existing != null && !allowReplace) {
final SourceName name = dataSource.getName();
final String newType = dataSource.getDataSourceType().getKsqlType().toLowerCase();
final String existingType = existing.source.getDataSourceType().getKsqlType().toLowerCase();
throw new KsqlException(String.format("Cannot add %s '%s': A %s with the same name already exists", newType, name.text(), existingType));
} else if (existing != null) {
existing.source.canUpgradeTo(dataSource).ifPresent(msg -> {
throw new KsqlException("Cannot upgrade data source: " + msg);
});
}
// Replace the dataSource if one exists, which may contain changes in the Schema, with
// a copy of the previous source info
dataSources.put(dataSource.getName(), (existing != null) ? existing.copyWith(dataSource) : new SourceInfo(dataSource));
LOG.info("Source {} created on the metastore", dataSource.getName().text());
// Re-build the DROP constraints if existing sources have references to this new source.
// This logic makes sure that drop constraints are set back if sources were deleted during
// the metastore restoration (See deleteSource()).
dataSources.forEach((name, info) -> {
info.references.forEach(ref -> {
if (ref.equals(dataSource.getName())) {
LOG.debug("Add a drop constraint reference back to source '{}' from source '{}'", dataSource.getName().text(), name.text());
addConstraint(dataSource.getName(), name);
}
});
});
}
use of io.confluent.ksql.name.SourceName in project ksql by confluentinc.
the class LogicalPlanner method joinOnNonKeyAttribute.
private static boolean joinOnNonKeyAttribute(final Expression joinExpression, final PlanNode node, final AliasedDataSource aliasedDataSource) {
if (!(joinExpression instanceof ColumnReferenceExp)) {
return true;
}
final ColumnReferenceExp simpleJoinExpression = (ColumnReferenceExp) joinExpression;
final ColumnName joinAttributeName = simpleJoinExpression.getColumnName();
final List<DataSourceNode> dataSourceNodes = node.getSourceNodes().collect(Collectors.toList());
final List<Column> keyColumns;
// n-way join sub-tree (ie, not a leaf)
if (isInnerNode(node)) {
final DataSourceNode qualifiedNode;
if (simpleJoinExpression.maybeQualifier().isPresent()) {
final SourceName qualifierOrAlias = simpleJoinExpression.maybeQualifier().get();
final SourceName qualifier;
if (aliasedDataSource.getAlias().equals(qualifierOrAlias)) {
qualifier = aliasedDataSource.getDataSource().getName();
} else {
qualifier = qualifierOrAlias;
}
final List<DataSourceNode> allNodes = dataSourceNodes.stream().filter(n -> n.getDataSource().getName().equals(qualifier)).collect(Collectors.toList());
if (allNodes.size() != 1) {
throw new KsqlException(String.format("Join qualifier '%s' could not be resolved (either not found or not unique).", qualifier));
}
qualifiedNode = Iterables.getOnlyElement(allNodes);
} else {
final List<DataSourceNode> allNodes = dataSourceNodes.stream().filter(n -> n.getSchema().findColumn(simpleJoinExpression.getColumnName()).isPresent()).collect(Collectors.toList());
if (allNodes.size() != 1) {
throw new KsqlException(String.format("Join identifier '%s' could not be resolved (either not found or not unique).", joinAttributeName));
}
qualifiedNode = Iterables.getOnlyElement(allNodes);
}
keyColumns = qualifiedNode.getSchema().key();
} else {
// leaf node: we know we have single data source
keyColumns = Iterables.getOnlyElement(dataSourceNodes).getSchema().key();
}
// - thus, if the key has more than one column, the join is not on the key
if (keyColumns.size() > 1) {
return true;
}
return !joinAttributeName.equals(Iterables.getOnlyElement(keyColumns).name());
}
use of io.confluent.ksql.name.SourceName in project ksql by confluentinc.
the class JoinNode method resolveSelectStar.
@SuppressWarnings("UnstableApiUsage")
@Override
public Stream<ColumnName> resolveSelectStar(final Optional<SourceName> sourceName) {
final Stream<ColumnName> names = Stream.of(left, right).flatMap(JoinNode::getPreJoinProjectDataSources).filter(s -> !sourceName.isPresent() || sourceName.equals(s.getSourceName())).flatMap(s -> s.resolveSelectStar(sourceName));
if (sourceName.isPresent() || !joinKey.isSynthetic() || !finalJoin) {
return names;
}
// if we use a synthetic key, we know there's only a single key element
final Column syntheticKey = getOnlyElement(getSchema().key());
return Streams.concat(Stream.of(syntheticKey.name()), names);
}
Aggregations