Search in sources :

Example 21 with Literal

use of io.confluent.ksql.execution.expression.tree.Literal in project ksql by confluentinc.

the class SqlFormatterTest method shouldFormatCreateTableStatementWithExplicitTimestamp.

@Test
public void shouldFormatCreateTableStatementWithExplicitTimestamp() {
    // Given:
    final CreateSourceProperties props = CreateSourceProperties.from(new ImmutableMap.Builder<String, Literal>().putAll(SOME_WITH_PROPS.copyOfOriginalLiterals()).put(CommonCreateConfigs.TIMESTAMP_NAME_PROPERTY, new StringLiteral("Foo")).put(CommonCreateConfigs.TIMESTAMP_FORMAT_PROPERTY, new StringLiteral("%s")).build());
    final CreateTable createTable = new CreateTable(TEST, ELEMENTS_WITH_PRIMARY_KEY, false, false, props, false);
    // When:
    final String sql = SqlFormatter.formatSql(createTable);
    // Then:
    assertThat(sql, is("CREATE TABLE TEST (`k3` STRING PRIMARY KEY, `Foo` STRING) " + "WITH (KAFKA_TOPIC='topic_test', " + "TIMESTAMP='Foo', TIMESTAMP_FORMAT='%s', VALUE_FORMAT='JSON');"));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) Literal(io.confluent.ksql.execution.expression.tree.Literal) CreateTable(io.confluent.ksql.parser.tree.CreateTable) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ImmutableMap(com.google.common.collect.ImmutableMap) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) Test(org.junit.Test)

Example 22 with Literal

use of io.confluent.ksql.execution.expression.tree.Literal in project ksql by confluentinc.

the class SqlFormatterTest method shouldFormatCreateSourceTableStatement.

@Test
public void shouldFormatCreateSourceTableStatement() {
    // Given:
    final CreateSourceProperties props = CreateSourceProperties.from(new ImmutableMap.Builder<String, Literal>().putAll(SOME_WITH_PROPS.copyOfOriginalLiterals()).build());
    final CreateTable createTable = new CreateTable(TEST, ELEMENTS_WITH_PRIMARY_KEY, false, false, props, true);
    // When:
    final String sql = SqlFormatter.formatSql(createTable);
    // Then:
    assertThat(sql, is("CREATE SOURCE TABLE TEST (`k3` STRING PRIMARY KEY, `Foo` STRING) " + "WITH (KAFKA_TOPIC='topic_test', VALUE_FORMAT='JSON');"));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) Literal(io.confluent.ksql.execution.expression.tree.Literal) CreateTable(io.confluent.ksql.parser.tree.CreateTable) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ImmutableMap(com.google.common.collect.ImmutableMap) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) Test(org.junit.Test)

Example 23 with Literal

use of io.confluent.ksql.execution.expression.tree.Literal in project ksql by confluentinc.

the class SqlFormatterTest method shouldFormatCreateOrReplaceStreamStatement.

@Test
public void shouldFormatCreateOrReplaceStreamStatement() {
    // Given:
    final CreateSourceProperties props = CreateSourceProperties.from(new ImmutableMap.Builder<String, Literal>().putAll(SOME_WITH_PROPS.copyOfOriginalLiterals()).build());
    final CreateStream createTable = new CreateStream(TEST, ELEMENTS_WITHOUT_KEY, true, false, props, false);
    // When:
    final String sql = SqlFormatter.formatSql(createTable);
    // Then:
    assertThat(sql, is("CREATE OR REPLACE STREAM TEST (`Foo` STRING, `Bar` STRING) " + "WITH (KAFKA_TOPIC='topic_test', VALUE_FORMAT='JSON');"));
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) Literal(io.confluent.ksql.execution.expression.tree.Literal) CreateStream(io.confluent.ksql.parser.tree.CreateStream) StringContains.containsString(org.hamcrest.core.StringContains.containsString) ImmutableMap(com.google.common.collect.ImmutableMap) CreateSourceProperties(io.confluent.ksql.parser.properties.with.CreateSourceProperties) Test(org.junit.Test)

Example 24 with Literal

use of io.confluent.ksql.execution.expression.tree.Literal in project ksql by confluentinc.

the class CreateSourceAsProperties method withTopic.

public CreateSourceAsProperties withTopic(final String name, final int partitions, final short replicas) {
    final Map<String, Literal> originals = props.copyOfOriginalLiterals();
    originals.put(CommonCreateConfigs.KAFKA_TOPIC_NAME_PROPERTY, new StringLiteral(name));
    originals.put(CommonCreateConfigs.SOURCE_NUMBER_OF_PARTITIONS, new IntegerLiteral(partitions));
    originals.put(CommonCreateConfigs.SOURCE_NUMBER_OF_REPLICAS, new IntegerLiteral(replicas));
    return new CreateSourceAsProperties(originals);
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) Literal(io.confluent.ksql.execution.expression.tree.Literal) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral)

Example 25 with Literal

use of io.confluent.ksql.execution.expression.tree.Literal in project ksql by confluentinc.

the class UdafUtil method createAggregateFunctionInitArgs.

public static AggregateFunctionInitArguments createAggregateFunctionInitArgs(final int udafIndex, final FunctionCall functionCall, final KsqlConfig config) {
    final List<Expression> args = functionCall.getArguments();
    final List<Object> initArgs = new ArrayList<>(Math.max(0, args.size() - 1));
    // args for index > 0 must be literals:
    for (int idx = 1; idx < args.size(); idx++) {
        final Expression param = args.get(idx);
        if (!(param instanceof Literal)) {
            throw new KsqlException("Parameter " + (idx + 1) + " passed to function " + functionCall.getName().text() + " must be a literal constant, but was expression: '" + param + "'");
        }
        initArgs.add(((Literal) param).getValue());
    }
    final Map<String, Object> functionConfig = config.getKsqlFunctionsConfigProps(functionCall.getName().text());
    return new AggregateFunctionInitArguments(udafIndex, functionConfig, initArgs);
}
Also used : AggregateFunctionInitArguments(io.confluent.ksql.function.AggregateFunctionInitArguments) Expression(io.confluent.ksql.execution.expression.tree.Expression) Literal(io.confluent.ksql.execution.expression.tree.Literal) ArrayList(java.util.ArrayList) KsqlException(io.confluent.ksql.util.KsqlException)

Aggregations

Literal (io.confluent.ksql.execution.expression.tree.Literal)39 StringLiteral (io.confluent.ksql.execution.expression.tree.StringLiteral)38 IntegerLiteral (io.confluent.ksql.execution.expression.tree.IntegerLiteral)32 Test (org.junit.Test)32 BooleanLiteral (io.confluent.ksql.execution.expression.tree.BooleanLiteral)28 Matchers.containsString (org.hamcrest.Matchers.containsString)27 KsqlException (io.confluent.ksql.util.KsqlException)10 ImmutableMap (com.google.common.collect.ImmutableMap)8 CreateSourceProperties (io.confluent.ksql.parser.properties.with.CreateSourceProperties)6 StringContains.containsString (org.hamcrest.core.StringContains.containsString)5 HashMap (java.util.HashMap)4 EqualsTester (com.google.common.testing.EqualsTester)3 DecimalLiteral (io.confluent.ksql.execution.expression.tree.DecimalLiteral)3 DoubleLiteral (io.confluent.ksql.execution.expression.tree.DoubleLiteral)3 Expression (io.confluent.ksql.execution.expression.tree.Expression)3 LongLiteral (io.confluent.ksql.execution.expression.tree.LongLiteral)3 CreateTable (io.confluent.ksql.parser.tree.CreateTable)3 ImmutableMap.of (com.google.common.collect.ImmutableMap.of)2 WindowType (io.confluent.ksql.model.WindowType)2 ColumnName (io.confluent.ksql.name.ColumnName)2