Search in sources :

Example 1 with AllColumns

use of io.confluent.ksql.parser.tree.AllColumns 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 2 with AllColumns

use of io.confluent.ksql.parser.tree.AllColumns in project ksql by confluentinc.

the class Analyzer method visitSelect.

@Override
protected Node visitSelect(final Select node, final AnalysisContext context) {
    for (SelectItem selectItem : node.getSelectItems()) {
        if (selectItem instanceof AllColumns) {
            // expand * and T.*
            AllColumns allColumns = (AllColumns) selectItem;
            if ((this.analysis.getFromDataSources() == null) || (this.analysis.getFromDataSources().isEmpty())) {
                throw new KsqlException("FROM clause was not resolved!");
            }
            if (analysis.getJoin() != null) {
                JoinNode joinNode = analysis.getJoin();
                for (Field field : joinNode.getLeft().getSchema().fields()) {
                    QualifiedNameReference qualifiedNameReference = new QualifiedNameReference(allColumns.getLocation().get(), QualifiedName.of(joinNode.getLeftAlias() + "." + field.name()));
                    analysis.addSelectItem(qualifiedNameReference, joinNode.getLeftAlias() + "_" + field.name());
                }
                for (Field field : joinNode.getRight().getSchema().fields()) {
                    QualifiedNameReference qualifiedNameReference = new QualifiedNameReference(allColumns.getLocation().get(), QualifiedName.of(joinNode.getRightAlias() + "." + field.name()));
                    analysis.addSelectItem(qualifiedNameReference, joinNode.getRightAlias() + "_" + field.name());
                }
            } else {
                for (Field field : this.analysis.getFromDataSources().get(0).getLeft().getSchema().fields()) {
                    QualifiedNameReference qualifiedNameReference = new QualifiedNameReference(allColumns.getLocation().get(), QualifiedName.of(this.analysis.getFromDataSources().get(0).getRight() + "." + field.name()));
                    analysis.addSelectItem(qualifiedNameReference, field.name());
                }
            }
        } else if (selectItem instanceof SingleColumn) {
            SingleColumn column = (SingleColumn) selectItem;
            analysis.addSelectItem(column.getExpression(), column.getAlias().get());
        } else {
            throw new IllegalArgumentException("Unsupported SelectItem type: " + selectItem.getClass().getName());
        }
    }
    return null;
}
Also used : Field(org.apache.kafka.connect.data.Field) SelectItem(io.confluent.ksql.parser.tree.SelectItem) JoinNode(io.confluent.ksql.planner.plan.JoinNode) AllColumns(io.confluent.ksql.parser.tree.AllColumns) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) KsqlException(io.confluent.ksql.util.KsqlException) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference)

Aggregations

AllColumns (io.confluent.ksql.parser.tree.AllColumns)2 QualifiedNameReference (io.confluent.ksql.parser.tree.QualifiedNameReference)2 SelectItem (io.confluent.ksql.parser.tree.SelectItem)2 SingleColumn (io.confluent.ksql.parser.tree.SingleColumn)2 Field (org.apache.kafka.connect.data.Field)2 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)1 AliasedRelation (io.confluent.ksql.parser.tree.AliasedRelation)1 CreateTable (io.confluent.ksql.parser.tree.CreateTable)1 DropTable (io.confluent.ksql.parser.tree.DropTable)1 Join (io.confluent.ksql.parser.tree.Join)1 NaturalJoin (io.confluent.ksql.parser.tree.NaturalJoin)1 Table (io.confluent.ksql.parser.tree.Table)1 JoinNode (io.confluent.ksql.planner.plan.JoinNode)1 KsqlException (io.confluent.ksql.util.KsqlException)1 ArrayList (java.util.ArrayList)1