use of io.confluent.ksql.parser.tree.Relation 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.Relation 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.Relation in project ksql by confluentinc.
the class AstBuilder method visitJoinRelation.
// *************** from clause *****************
@Override
public Node visitJoinRelation(SqlBaseParser.JoinRelationContext context) {
Relation left = (Relation) visit(context.left);
Relation right;
if (context.CROSS() != null) {
right = (Relation) visit(context.right);
return new Join(getLocation(context), Join.Type.CROSS, left, right, Optional.<JoinCriteria>empty());
}
JoinCriteria criteria;
if (context.NATURAL() != null) {
right = (Relation) visit(context.right);
criteria = new NaturalJoin();
} else {
right = (Relation) visit(context.rightRelation);
if (context.joinCriteria().ON() != null) {
criteria = new JoinOn((Expression) visit(context.joinCriteria().booleanExpression()));
} else if (context.joinCriteria().USING() != null) {
List<String> columns = context.joinCriteria().identifier().stream().map(AstBuilder::getIdentifierText).collect(toList());
criteria = new JoinUsing(columns);
} else {
throw new IllegalArgumentException("Unsupported join criteria");
}
}
Join.Type joinType;
if (context.joinType().LEFT() != null) {
joinType = Join.Type.LEFT;
} else if (context.joinType().RIGHT() != null) {
joinType = Join.Type.RIGHT;
} else if (context.joinType().FULL() != null) {
joinType = Join.Type.FULL;
} else {
joinType = Join.Type.INNER;
}
return new Join(getLocation(context), joinType, left, right, Optional.of(criteria));
}
use of io.confluent.ksql.parser.tree.Relation 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