Search in sources :

Example 6 with Expression

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));
}
Also used : StringLiteral(io.confluent.ksql.parser.tree.StringLiteral) Expression(io.confluent.ksql.parser.tree.Expression) HashMap(java.util.HashMap) CreateTable(io.confluent.ksql.parser.tree.CreateTable) EasyMock.anyString(org.easymock.EasyMock.anyString) TableElement(io.confluent.ksql.parser.tree.TableElement) Test(org.junit.Test)

Example 7 with Expression

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;
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) FunctionRegistry(io.confluent.ksql.function.FunctionRegistry) CodeGenRunner(io.confluent.ksql.codegen.CodeGenRunner) Expression(io.confluent.ksql.parser.tree.Expression) ArrayList(java.util.ArrayList)

Example 8 with Expression

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);
    }
}
Also used : SchemaMetadata(io.confluent.kafka.schemaregistry.client.SchemaMetadata) Expression(io.confluent.ksql.parser.tree.Expression) Schema(org.apache.kafka.connect.data.Schema) AbstractStreamCreateStatement(io.confluent.ksql.parser.tree.AbstractStreamCreateStatement) IOException(java.io.IOException) RestClientException(io.confluent.kafka.schemaregistry.client.rest.exceptions.RestClientException)

Example 9 with Expression

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);
}
Also used : Field(org.apache.kafka.connect.data.Field) StringLiteral(io.confluent.ksql.parser.tree.StringLiteral) Expression(io.confluent.ksql.parser.tree.Expression) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) TableElement(io.confluent.ksql.parser.tree.TableElement)

Example 10 with Expression

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));
}
Also used : Relation(io.confluent.ksql.parser.tree.Relation) AliasedRelation(io.confluent.ksql.parser.tree.AliasedRelation) 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) JoinCriteria(io.confluent.ksql.parser.tree.JoinCriteria) Join(io.confluent.ksql.parser.tree.Join) NaturalJoin(io.confluent.ksql.parser.tree.NaturalJoin) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) Collectors.toList(java.util.stream.Collectors.toList) JoinUsing(io.confluent.ksql.parser.tree.JoinUsing) NaturalJoin(io.confluent.ksql.parser.tree.NaturalJoin) JoinOn(io.confluent.ksql.parser.tree.JoinOn)

Aggregations

Expression (io.confluent.ksql.parser.tree.Expression)36 DereferenceExpression (io.confluent.ksql.parser.tree.DereferenceExpression)17 ComparisonExpression (io.confluent.ksql.parser.tree.ComparisonExpression)13 WindowExpression (io.confluent.ksql.parser.tree.WindowExpression)10 HashMap (java.util.HashMap)10 ArithmeticBinaryExpression (io.confluent.ksql.parser.tree.ArithmeticBinaryExpression)8 ArithmeticUnaryExpression (io.confluent.ksql.parser.tree.ArithmeticUnaryExpression)8 InListExpression (io.confluent.ksql.parser.tree.InListExpression)8 LogicalBinaryExpression (io.confluent.ksql.parser.tree.LogicalBinaryExpression)8 NotExpression (io.confluent.ksql.parser.tree.NotExpression)8 NullIfExpression (io.confluent.ksql.parser.tree.NullIfExpression)8 QualifiedNameReference (io.confluent.ksql.parser.tree.QualifiedNameReference)8 SearchedCaseExpression (io.confluent.ksql.parser.tree.SearchedCaseExpression)8 SimpleCaseExpression (io.confluent.ksql.parser.tree.SimpleCaseExpression)8 StringLiteral (io.confluent.ksql.parser.tree.StringLiteral)8 SubqueryExpression (io.confluent.ksql.parser.tree.SubqueryExpression)8 SubscriptExpression (io.confluent.ksql.parser.tree.SubscriptExpression)8 ArrayList (java.util.ArrayList)8 Test (org.junit.Test)8 HoppingWindowExpression (io.confluent.ksql.parser.tree.HoppingWindowExpression)7