Search in sources :

Example 6 with SingleColumn

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

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

the class AstBuilder method visitSelectSingle.

@Override
public Node visitSelectSingle(SqlBaseParser.SelectSingleContext context) {
    Expression selectItemExpression = (Expression) visit(context.expression());
    Optional<String> alias = Optional.ofNullable(context.identifier()).map(AstBuilder::getIdentifierText);
    if (!alias.isPresent()) {
        if (selectItemExpression instanceof QualifiedNameReference) {
            QualifiedNameReference qualifiedNameReference = (QualifiedNameReference) selectItemExpression;
            alias = Optional.of(qualifiedNameReference.getName().getSuffix());
        } else if (selectItemExpression instanceof DereferenceExpression) {
            DereferenceExpression dereferenceExpression = (DereferenceExpression) selectItemExpression;
            if ((dataSourceExtractor.getJoinLeftSchema() != null) && (dataSourceExtractor.getCommonFieldNames().contains(dereferenceExpression.getFieldName()))) {
                alias = Optional.of(dereferenceExpression.getBase().toString() + "_" + dereferenceExpression.getFieldName());
            } else {
                alias = Optional.of(dereferenceExpression.getFieldName());
            }
        } else {
            alias = Optional.of("KSQL_COL_" + selectItemIndex);
        }
    } else {
        alias = Optional.of(alias.get());
    }
    selectItemIndex++;
    return new SingleColumn(getLocation(context), selectItemExpression, alias);
}
Also used : DereferenceExpression(io.confluent.ksql.parser.tree.DereferenceExpression) 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) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) QualifiedNameReference(io.confluent.ksql.parser.tree.QualifiedNameReference)

Example 8 with SingleColumn

use of io.confluent.ksql.parser.tree.SingleColumn 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)

Example 9 with SingleColumn

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

the class KsqlParserTest method testProjection.

@Test
public void testProjection() throws Exception {
    String queryStr = "SELECT col0, col2, col3 FROM test1;";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testProjection fails", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testProjection fails", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    Assert.assertTrue("testProjection fails", querySpecification.getSelect().getSelectItems().size() == 3);
    Assert.assertTrue("testProjection fails", querySpecification.getSelect().getSelectItems().get(0) instanceof SingleColumn);
    SingleColumn column0 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(0);
    Assert.assertTrue("testProjection fails", column0.getAlias().get().equalsIgnoreCase("COL0"));
    Assert.assertTrue("testProjection fails", column0.getExpression().toString().equalsIgnoreCase("TEST1.COL0"));
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) Test(org.junit.Test)

Example 10 with SingleColumn

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

the class KsqlParserTest method testProjectionWithArrayMap.

@Test
public void testProjectionWithArrayMap() throws Exception {
    String queryStr = "SELECT col0, col2, col3, col4[0], col5['key1'] FROM test1;";
    Statement statement = KSQL_PARSER.buildAst(queryStr, metaStore).get(0);
    Assert.assertTrue("testProjectionWithArrayMap fails", statement instanceof Query);
    Query query = (Query) statement;
    Assert.assertTrue("testProjectionWithArrayMap fails", query.getQueryBody() instanceof QuerySpecification);
    QuerySpecification querySpecification = (QuerySpecification) query.getQueryBody();
    Assert.assertTrue("testProjectionWithArrayMap fails", querySpecification.getSelect().getSelectItems().size() == 5);
    Assert.assertTrue("testProjectionWithArrayMap fails", querySpecification.getSelect().getSelectItems().get(0) instanceof SingleColumn);
    SingleColumn column0 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(0);
    Assert.assertTrue("testProjectionWithArrayMap fails", column0.getAlias().get().equalsIgnoreCase("COL0"));
    Assert.assertTrue("testProjectionWithArrayMap fails", column0.getExpression().toString().equalsIgnoreCase("TEST1.COL0"));
    SingleColumn column3 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(3);
    SingleColumn column4 = (SingleColumn) querySpecification.getSelect().getSelectItems().get(4);
    Assert.assertTrue("testProjectionWithArrayMap fails", column3.getExpression().toString().equalsIgnoreCase("TEST1.COL4[0]"));
    Assert.assertTrue("testProjectionWithArrayMap fails", column4.getExpression().toString().equalsIgnoreCase("TEST1.COL5['key1']"));
}
Also used : QuerySpecification(io.confluent.ksql.parser.tree.QuerySpecification) Query(io.confluent.ksql.parser.tree.Query) Statement(io.confluent.ksql.parser.tree.Statement) SingleColumn(io.confluent.ksql.parser.tree.SingleColumn) Test(org.junit.Test)

Aggregations

SingleColumn (io.confluent.ksql.parser.tree.SingleColumn)12 Query (io.confluent.ksql.parser.tree.Query)7 QuerySpecification (io.confluent.ksql.parser.tree.QuerySpecification)7 Statement (io.confluent.ksql.parser.tree.Statement)7 Test (org.junit.Test)7 SelectItem (io.confluent.ksql.parser.tree.SelectItem)4 QualifiedNameReference (io.confluent.ksql.parser.tree.QualifiedNameReference)3 KsqlStream (io.confluent.ksql.metastore.KsqlStream)2 KsqlTopic (io.confluent.ksql.metastore.KsqlTopic)2 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)2 AllColumns (io.confluent.ksql.parser.tree.AllColumns)2 Field (org.apache.kafka.connect.data.Field)2 SchemaBuilder (org.apache.kafka.connect.data.SchemaBuilder)2 AliasedRelation (io.confluent.ksql.parser.tree.AliasedRelation)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 CreateTable (io.confluent.ksql.parser.tree.CreateTable)1 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)1 DropTable (io.confluent.ksql.parser.tree.DropTable)1