use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.
the class CommandFactoriesTest method shouldCreateCommandForCreateTable.
@Test
public void shouldCreateCommandForCreateTable() {
HashMap<String, Expression> tableProperties = new HashMap<>();
tableProperties.putAll(properties);
tableProperties.put(DdlConfig.KEY_NAME_PROPERTY, new StringLiteral("COL1"));
final DdlCommand result = commandFactories.create(sqlExpression, new CreateTable(QualifiedName.of("foo"), Arrays.asList(new TableElement("COL1", "BIGINT"), new TableElement("COL2", "VARCHAR")), true, tableProperties), Collections.emptyMap());
assertThat(result, instanceOf(CreateTableCommand.class));
}
use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.
the class SelectValueMapperTest method createExpressionMetadata.
private List<ExpressionMetadata> createExpressionMetadata(final List<Pair<String, Expression>> expressionPairList, final Schema schema) throws Exception {
final CodeGenRunner codeGenRunner = new CodeGenRunner(schema, new FunctionRegistry());
final List<ExpressionMetadata> expressionEvaluators = new ArrayList<>();
for (Pair<String, Expression> expressionPair : expressionPairList) {
final ExpressionMetadata expressionEvaluator = codeGenRunner.buildCodeGenFromParseTree(expressionPair.getRight());
expressionEvaluators.add(expressionEvaluator);
}
return expressionEvaluators;
}
use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.
the class AvroUtil method checkAndSetAvroSchema.
public Pair<AbstractStreamCreateStatement, String> checkAndSetAvroSchema(final AbstractStreamCreateStatement abstractStreamCreateStatement, final Map<String, Object> streamsProperties, final SchemaRegistryClient schemaRegistryClient) {
Map<String, Expression> ddlProperties = abstractStreamCreateStatement.getProperties();
if (!ddlProperties.containsKey(DdlConfig.VALUE_FORMAT_PROPERTY)) {
throw new KsqlException(String.format("%s should be set in WITH clause of CREATE STREAM/TABLE statement.", DdlConfig.VALUE_FORMAT_PROPERTY));
}
final String serde = StringUtil.cleanQuotes(ddlProperties.get(DdlConfig.VALUE_FORMAT_PROPERTY).toString());
if (!serde.equalsIgnoreCase(DataSource.AVRO_SERDE_NAME)) {
return new Pair<>(abstractStreamCreateStatement, null);
}
String kafkaTopicName = StringUtil.cleanQuotes(ddlProperties.get(DdlConfig.KAFKA_TOPIC_NAME_PROPERTY).toString());
try {
// If the schema is not specified infer it from the Avro schema in Schema Registry.
if (abstractStreamCreateStatement.getElements().isEmpty()) {
SchemaMetadata schemaMetadata = fetchSchemaMetadata(abstractStreamCreateStatement, schemaRegistryClient, kafkaTopicName);
String avroSchemaString = schemaMetadata.getSchema();
streamsProperties.put(DdlConfig.AVRO_SCHEMA, avroSchemaString);
Schema schema = SerDeUtil.getSchemaFromAvro(avroSchemaString);
AbstractStreamCreateStatement abstractStreamCreateStatementCopy = addAvroFields(abstractStreamCreateStatement, schema, schemaMetadata.getId());
return new Pair<>(abstractStreamCreateStatementCopy, SqlFormatter.formatSql(abstractStreamCreateStatementCopy));
} else {
return new Pair<>(abstractStreamCreateStatement, null);
}
} catch (Exception e) {
String errorMessage = String.format(" Could not fetch the AVRO schema from schema registry. %s ", e.getMessage());
throw new KsqlException(errorMessage);
}
}
use of io.confluent.ksql.parser.tree.Expression in project ksql by confluentinc.
the class AvroUtil method addAvroFields.
private AbstractStreamCreateStatement addAvroFields(final AbstractStreamCreateStatement abstractStreamCreateStatement, final Schema schema, int schemaId) {
List<TableElement> elements = new ArrayList<>();
for (Field field : schema.fields()) {
TableElement tableElement = new TableElement(field.name().toUpperCase(), SchemaUtil.getSqlTypeName(field.schema()));
elements.add(tableElement);
}
StringLiteral schemaIdLiteral = new StringLiteral(String.format("%d", schemaId));
Map<String, Expression> properties = new HashMap<>(abstractStreamCreateStatement.getProperties());
if (!abstractStreamCreateStatement.getProperties().containsKey(KsqlConstants.AVRO_SCHEMA_ID)) {
properties.put(KsqlConstants.AVRO_SCHEMA_ID, schemaIdLiteral);
}
return abstractStreamCreateStatement.copyWith(elements, properties);
}
use of io.confluent.ksql.parser.tree.Expression 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));
}
Aggregations