Search in sources :

Example 1 with Table

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()));
    }
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Relation(io.confluent.ksql.parser.tree.Relation) Table(io.confluent.ksql.parser.tree.Table) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata) QueryMetadata(io.confluent.ksql.util.QueryMetadata) QueryId(io.confluent.ksql.query.QueryId) CommandStatus(io.confluent.ksql.rest.entity.CommandStatus) WakeupException(org.apache.kafka.common.errors.WakeupException) KsqlException(io.confluent.ksql.util.KsqlException) PersistentQueryMetadata(io.confluent.ksql.util.PersistentQueryMetadata)

Example 2 with Table

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()));
}
Also used : Relation(io.confluent.ksql.parser.tree.Relation) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation) DropTable(io.confluent.ksql.parser.tree.DropTable) Table(io.confluent.ksql.parser.tree.Table) CreateTable(io.confluent.ksql.parser.tree.CreateTable) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation)

Example 3 with Table

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;
}
Also used : StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) Field(org.apache.kafka.connect.data.Field) DropTable(io.confluent.ksql.parser.tree.DropTable) Table(io.confluent.ksql.parser.tree.Table) CreateTable(io.confluent.ksql.parser.tree.CreateTable) SelectItem(io.confluent.ksql.parser.tree.SelectItem) ArrayList(java.util.ArrayList) Join(io.confluent.ksql.parser.tree.Join) NaturalJoin(io.confluent.ksql.parser.tree.NaturalJoin) AllColumns(io.confluent.ksql.parser.tree.AllColumns) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation)

Example 4 with Table

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()));
}
Also used : StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) Table(io.confluent.ksql.parser.tree.Table) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation)

Example 5 with Table

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());
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Relation(io.confluent.ksql.parser.tree.Relation) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation) DropTable(io.confluent.ksql.parser.tree.DropTable) Table(io.confluent.ksql.parser.tree.Table) CreateTable(io.confluent.ksql.parser.tree.CreateTable) SimpleGroupBy(io.confluent.ksql.parser.tree.SimpleGroupBy) GroupBy(io.confluent.ksql.parser.tree.GroupBy) InListExpression(io.confluent.ksql.parser.tree.InListExpression) NullIfExpression(io.confluent.ksql.parser.tree.NullIfExpression) SimpleCaseExpression(io.confluent.ksql.parser.tree.SimpleCaseExpression) ComparisonExpression(io.confluent.ksql.parser.tree.ComparisonExpression) DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) Expression(io.confluent.ksql.parser.tree.Expression) LogicalBinaryExpression(io.confluent.ksql.parser.tree.LogicalBinaryExpression) TumblingWindowExpression(io.confluent.ksql.parser.tree.TumblingWindowExpression) ArithmeticBinaryExpression(io.confluent.ksql.parser.tree.ArithmeticBinaryExpression) NotExpression(io.confluent.ksql.parser.tree.NotExpression) HoppingWindowExpression(io.confluent.ksql.parser.tree.HoppingWindowExpression) SubscriptExpression(io.confluent.ksql.parser.tree.SubscriptExpression) SessionWindowExpression(io.confluent.ksql.parser.tree.SessionWindowExpression) SearchedCaseExpression(io.confluent.ksql.parser.tree.SearchedCaseExpression) LambdaExpression(io.confluent.ksql.parser.tree.LambdaExpression) SubqueryExpression(io.confluent.ksql.parser.tree.SubqueryExpression) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) ArithmeticUnaryExpression(io.confluent.ksql.parser.tree.ArithmeticUnaryExpression) SelectItem(io.confluent.ksql.parser.tree.SelectItem) TumblingWindowExpression(io.confluent.ksql.parser.tree.TumblingWindowExpression) HoppingWindowExpression(io.confluent.ksql.parser.tree.HoppingWindowExpression) SessionWindowExpression(io.confluent.ksql.parser.tree.SessionWindowExpression) WindowExpression(io.confluent.ksql.parser.tree.WindowExpression) CreateTableAsSelect(io.confluent.ksql.parser.tree.CreateTableAsSelect) CreateStreamAsSelect(io.confluent.ksql.parser.tree.CreateStreamAsSelect) Select(io.confluent.ksql.parser.tree.Select)

Aggregations

Table (io.confluent.ksql.parser.tree.Table)8 AliasedRelation (io.confluent.ksql.parser.tree.AliasedRelation)5 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)4 CreateTable (io.confluent.ksql.parser.tree.CreateTable)4 DropTable (io.confluent.ksql.parser.tree.DropTable)4 QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)3 Relation (io.confluent.ksql.parser.tree.Relation)3 Expression (io.confluent.ksql.parser.tree.Expression)2 SelectItem (io.confluent.ksql.parser.tree.SelectItem)2 KsqlException (io.confluent.ksql.util.KsqlException)2 AllColumns (io.confluent.ksql.parser.tree.AllColumns)1 ArithmeticBinaryExpression (io.confluent.ksql.parser.tree.ArithmeticBinaryExpression)1 ArithmeticUnaryExpression (io.confluent.ksql.parser.tree.ArithmeticUnaryExpression)1 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)1 CreateStreamAsSelect (io.confluent.ksql.parser.tree.CreateStreamAsSelect)1 CreateTableAsSelect (io.confluent.ksql.parser.tree.CreateTableAsSelect)1 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)1 GroupBy (io.confluent.ksql.parser.tree.GroupBy)1 HoppingWindowExpression (io.confluent.ksql.parser.tree.HoppingWindowExpression)1 InListExpression (io.confluent.ksql.parser.tree.InListExpression)1