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;
}
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);
}
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;
}
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"));
}
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']"));
}
Aggregations