Search in sources :

Example 1 with Literal

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

the class ImplicitlyCastResolverTest method shouldCastToDecimal.

@Test
public void shouldCastToDecimal() {
    // Given
    final Map<Literal, BigDecimal> fromLiterals = ImmutableMap.of(new IntegerLiteral(5), new BigDecimal("5.00"), new LongLiteral(5), new BigDecimal("5.00"), new DoubleLiteral(5), new BigDecimal("5.00"), new DecimalLiteral(BigDecimal.TEN), new BigDecimal("10.00"), new DecimalLiteral(new BigDecimal("10.1")), new BigDecimal("10.10"));
    for (final Map.Entry<Literal, BigDecimal> entry : fromLiterals.entrySet()) {
        final Literal literal = entry.getKey();
        final BigDecimal expected = entry.getValue();
        // When
        final Expression expression = ImplicitlyCastResolver.resolve(literal, DECIMAL_5_2);
        // Then
        assertThat("Should cast " + literal.getClass().getSimpleName() + " to " + DECIMAL_5_2, expression, instanceOf(DecimalLiteral.class));
        assertThat("Should cast " + literal.getClass().getSimpleName() + " to " + DECIMAL_5_2, ((DecimalLiteral) expression).getValue(), is(expected));
    }
}
Also used : LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) Expression(io.confluent.ksql.execution.expression.tree.Expression) Literal(io.confluent.ksql.execution.expression.tree.Literal) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) BooleanLiteral(io.confluent.ksql.execution.expression.tree.BooleanLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) ImmutableMap(com.google.common.collect.ImmutableMap) Map(java.util.Map) BigDecimal(java.math.BigDecimal) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Test(org.junit.Test)

Example 2 with Literal

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

the class KsqlParserTest method parseDouble.

private Literal parseDouble(final String literalText) {
    final PreparedStatement<Query> query = KsqlParserTestUtil.buildSingleAst("SELECT * FROM TEST1 WHERE COL3 > " + literalText + ";", metaStore);
    final ComparisonExpression where = (ComparisonExpression) query.getStatement().getWhere().get();
    return (Literal) where.getRight();
}
Also used : ComparisonExpression(io.confluent.ksql.execution.expression.tree.ComparisonExpression) Query(io.confluent.ksql.parser.tree.Query) TerminateQuery(io.confluent.ksql.parser.tree.TerminateQuery) StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) DoubleLiteral(io.confluent.ksql.execution.expression.tree.DoubleLiteral) DecimalLiteral(io.confluent.ksql.execution.expression.tree.DecimalLiteral) Literal(io.confluent.ksql.execution.expression.tree.Literal) LongLiteral(io.confluent.ksql.execution.expression.tree.LongLiteral)

Example 3 with Literal

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

the class SqlFormatterTest method shouldFormatCreateSourceStreamStatement.

@Test
public void shouldFormatCreateSourceStreamStatement() {
    // Given:
    final CreateSourceProperties props = CreateSourceProperties.from(new ImmutableMap.Builder<String, Literal>().putAll(SOME_WITH_PROPS.copyOfOriginalLiterals()).build());
    final CreateStream createStream = new CreateStream(TEST, ELEMENTS_WITH_KEY, false, false, props, true);
    // When:
    final String sql = SqlFormatter.formatSql(createStream);
    // Then:
    assertThat(sql, is("CREATE SOURCE STREAM TEST (`k3` STRING 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) 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 4 with Literal

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

the class SqlFormatterTest method shouldFormatCreateOrReplaceTableStatement.

@Test
public void shouldFormatCreateOrReplaceTableStatement() {
    // 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, true, false, props, false);
    // When:
    final String sql = SqlFormatter.formatSql(createTable);
    // Then:
    assertThat(sql, is("CREATE OR REPLACE 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 5 with Literal

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

the class CreateSourceProperties method withPartitions.

public CreateSourceProperties withPartitions(final int partitions) {
    final Map<String, Literal> originals = props.copyOfOriginalLiterals();
    originals.put(CommonCreateConfigs.SOURCE_NUMBER_OF_PARTITIONS, new IntegerLiteral(partitions));
    return new CreateSourceProperties(originals, durationParser);
}
Also used : StringLiteral(io.confluent.ksql.execution.expression.tree.StringLiteral) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral) Literal(io.confluent.ksql.execution.expression.tree.Literal) IntegerLiteral(io.confluent.ksql.execution.expression.tree.IntegerLiteral)

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