use of io.confluent.ksql.parser.tree.Table in project ksql by confluentinc.
the class StatementExecutor method startQuery.
private boolean startQuery(String queryString, Query query, CommandId commandId, Map<QueryId, CommandId> terminatedQueries, Command command, boolean wasDropped) throws Exception {
if (query.getQueryBody() instanceof QuerySpecification) {
QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
Relation into = querySpecification.getInto();
if (into instanceof Table) {
Table table = (Table) into;
if (ksqlEngine.getMetaStore().getSource(table.getName().getSuffix()) != null) {
throw new Exception(String.format("Sink specified in INTO clause already exists: %s", table.getName().getSuffix().toUpperCase()));
}
}
}
QueryMetadata queryMetadata = ksqlEngine.buildMultipleQueries(queryString, command.getKsqlProperties()).get(0);
if (queryMetadata instanceof PersistentQueryMetadata) {
PersistentQueryMetadata persistentQueryMetadata = (PersistentQueryMetadata) queryMetadata;
final QueryId queryId = persistentQueryMetadata.getId();
if (terminatedQueries != null && terminatedQueries.containsKey(queryId)) {
CommandId terminateId = terminatedQueries.get(queryId);
statusStore.put(terminateId, new CommandStatus(CommandStatus.Status.SUCCESS, "Termination request granted"));
statusStore.put(commandId, new CommandStatus(CommandStatus.Status.TERMINATED, "Query terminated"));
ksqlEngine.terminateQuery(queryId, false);
return false;
} else if (wasDropped) {
ksqlEngine.terminateQuery(queryId, false);
return false;
} else {
persistentQueryMetadata.getKafkaStreams().start();
return true;
}
} else {
throw new Exception(String.format("Unexpected query metadata type: %s; was expecting %s", queryMetadata.getClass().getCanonicalName(), PersistentQueryMetadata.class.getCanonicalName()));
}
}
use of io.confluent.ksql.parser.tree.Table in project ksql by confluentinc.
the class AstBuilder method visitAliasedRelation.
@Override
public Node visitAliasedRelation(SqlBaseParser.AliasedRelationContext context) {
Relation child = (Relation) visit(context.relationPrimary());
String alias;
if (context.children.size() == 1) {
Table table = (Table) visit(context.relationPrimary());
alias = table.getName().getSuffix();
} else if (context.children.size() == 2) {
alias = context.children.get(1).getText();
} else {
throw new IllegalArgumentException("AliasedRelationContext must have either 1 or 2 children, but has:" + context.children.size());
}
return new AliasedRelation(getLocation(context), child, alias, getColumnAliases(context.columnAliases()));
}
use of io.confluent.ksql.parser.tree.Table in project ksql by confluentinc.
the class AstBuilder method getSelectStartItems.
private List<SelectItem> getSelectStartItems(final SelectItem selectItem, final Relation from) {
List<SelectItem> selectItems = new ArrayList<>();
AllColumns allColumns = (AllColumns) selectItem;
if (from instanceof Join) {
Join join = (Join) from;
AliasedRelation left = (AliasedRelation) join.getLeft();
StructuredDataSource leftDataSource = dataSourceExtractor.getMetaStore().getSource(left.getRelation().toString());
if (leftDataSource == null) {
throw new InvalidColumnReferenceException(left.getRelation().toString() + " does not exist.");
}
AliasedRelation right = (AliasedRelation) join.getRight();
StructuredDataSource rightDataSource = dataSourceExtractor.getMetaStore().getSource(right.getRelation().toString());
if (rightDataSource == null) {
throw new InvalidColumnReferenceException(right.getRelation().toString() + " does not exist.");
}
for (Field field : leftDataSource.getSchema().fields()) {
QualifiedNameReference qualifiedNameReference = new QualifiedNameReference(allColumns.getLocation().get(), QualifiedName.of(left.getAlias() + "." + field.name()));
SingleColumn newSelectItem = new SingleColumn(qualifiedNameReference, left.getAlias() + "_" + field.name());
selectItems.add(newSelectItem);
}
for (Field field : rightDataSource.getSchema().fields()) {
QualifiedNameReference qualifiedNameReference = new QualifiedNameReference(allColumns.getLocation().get(), QualifiedName.of(right.getAlias() + "." + field.name()));
SingleColumn newSelectItem = new SingleColumn(qualifiedNameReference, right.getAlias() + "_" + field.name());
selectItems.add(newSelectItem);
}
} else {
AliasedRelation fromRel = (AliasedRelation) from;
StructuredDataSource fromDataSource = dataSourceExtractor.getMetaStore().getSource(((Table) fromRel.getRelation()).getName().getSuffix());
if (fromDataSource == null) {
throw new InvalidColumnReferenceException(((Table) fromRel.getRelation()).getName().getSuffix() + " does not exist.");
}
for (Field field : fromDataSource.getSchema().fields()) {
QualifiedNameReference qualifiedNameReference = new QualifiedNameReference(allColumns.getLocation().get(), QualifiedName.of(fromDataSource.getName() + "." + field.name()));
SingleColumn newSelectItem = new SingleColumn(qualifiedNameReference, field.name());
selectItems.add(newSelectItem);
}
}
return selectItems;
}
use of io.confluent.ksql.parser.tree.Table in project ksql by confluentinc.
the class DataSourceExtractor method visitAliasedRelation.
@Override
public Node visitAliasedRelation(final SqlBaseParser.AliasedRelationContext context) {
Table table = (Table) visit(context.relationPrimary());
String alias = null;
if (context.children.size() == 1) {
alias = table.getName().getSuffix().toUpperCase();
} else if (context.children.size() == 2) {
alias = context.children.get(1).getText().toUpperCase();
}
if (!isJoin) {
this.fromAlias = alias;
StructuredDataSource fromDataSource = metaStore.getSource(table.getName().getSuffix());
if (fromDataSource == null) {
throw new KsqlException(table.getName().getSuffix() + " does not exist.");
}
return null;
}
// TODO: Figure out if the call to toUpperCase() here is really necessary
return new AliasedRelation(getLocation(context), table, alias.toUpperCase(), getColumnAliases(context.columnAliases()));
}
use of io.confluent.ksql.parser.tree.Table in project ksql by confluentinc.
the class AstBuilder method visitQuerySpecification.
@Override
public Node visitQuerySpecification(SqlBaseParser.QuerySpecificationContext context) {
Table into;
if (context.into != null) {
into = (Table) visit(context.into);
} else {
// TODO: Generate a unique name
String intoName = "KSQL_Stream_" + System.currentTimeMillis();
into = new Table(QualifiedName.of(intoName), true);
}
Relation from = (Relation) visit(context.from);
Select select = new Select(getLocation(context.SELECT()), false, visit(context.selectItem(), SelectItem.class));
select = new Select(getLocation(context.SELECT()), select.isDistinct(), extractSelectItems(select, from));
getResultDatasource(select, into);
return new QuerySpecification(getLocation(context), select, into, from, visitIfPresent(context.windowExpression(), WindowExpression.class), visitIfPresent(context.where, Expression.class), visitIfPresent(context.groupBy(), GroupBy.class), visitIfPresent(context.having, Expression.class), Optional.<String>empty());
}
Aggregations