Search in sources :

Example 11 with Analysis

use of io.confluent.ksql.analyzer.Analysis in project ksql by confluentinc.

the class ExpressionTypeManagerTest method testArithmaticExpr.

@Test
public void testArithmaticExpr() throws Exception {
    String simpleQuery = "SELECT col0+col3, col2, col3+10, col0+10, col0*25 FROM test1 WHERE col0 > 100;";
    Analysis analysis = analyzeQuery(simpleQuery);
    ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
    Schema exprType0 = expressionTypeManager.getExpressionType(analysis.getSelectExpressions().get(0));
    Schema exprType2 = expressionTypeManager.getExpressionType(analysis.getSelectExpressions().get(2));
    Schema exprType3 = expressionTypeManager.getExpressionType(analysis.getSelectExpressions().get(3));
    Schema exprType4 = expressionTypeManager.getExpressionType(analysis.getSelectExpressions().get(4));
    Assert.assertTrue(exprType0.type() == Schema.Type.FLOAT64);
    Assert.assertTrue(exprType2.type() == Schema.Type.FLOAT64);
    Assert.assertTrue(exprType3.type() == Schema.Type.INT64);
    Assert.assertTrue(exprType4.type() == Schema.Type.INT64);
}
Also used : Analysis(io.confluent.ksql.analyzer.Analysis) Schema(org.apache.kafka.connect.data.Schema) Test(org.junit.Test)

Example 12 with Analysis

use of io.confluent.ksql.analyzer.Analysis in project ksql by confluentinc.

the class ExpressionTypeManagerTest method testComparisonExpr.

@Test
public void testComparisonExpr() throws Exception {
    String simpleQuery = "SELECT col0>col3, col0*25<200, col2 = 'test' FROM test1;";
    Analysis analysis = analyzeQuery(simpleQuery);
    ExpressionTypeManager expressionTypeManager = new ExpressionTypeManager(schema, functionRegistry);
    Schema exprType0 = expressionTypeManager.getExpressionType(analysis.getSelectExpressions().get(0));
    Schema exprType1 = expressionTypeManager.getExpressionType(analysis.getSelectExpressions().get(1));
    Schema exprType2 = expressionTypeManager.getExpressionType(analysis.getSelectExpressions().get(2));
    Assert.assertTrue(exprType0.type() == Schema.Type.BOOLEAN);
    Assert.assertTrue(exprType1.type() == Schema.Type.BOOLEAN);
    Assert.assertTrue(exprType2.type() == Schema.Type.BOOLEAN);
}
Also used : Analysis(io.confluent.ksql.analyzer.Analysis) Schema(org.apache.kafka.connect.data.Schema) Test(org.junit.Test)

Example 13 with Analysis

use of io.confluent.ksql.analyzer.Analysis in project ksql by confluentinc.

the class QueryEngine method buildQueryLogicalPlan.

private PlanNode buildQueryLogicalPlan(String sqlExpression, final Query query, final MetaStore tempMetaStore) {
    final QueryAnalyzer queryAnalyzer = new QueryAnalyzer(tempMetaStore, ksqlEngine.getFunctionRegistry());
    final Analysis analysis = queryAnalyzer.analyze(sqlExpression, query);
    final AggregateAnalysis aggAnalysis = queryAnalyzer.analyzeAggregate(query, analysis);
    final PlanNode logicalPlan = new LogicalPlanner(analysis, aggAnalysis, ksqlEngine.getFunctionRegistry()).buildPlan();
    if (logicalPlan instanceof KsqlStructuredDataOutputNode) {
        KsqlStructuredDataOutputNode ksqlStructuredDataOutputNode = (KsqlStructuredDataOutputNode) logicalPlan;
        StructuredDataSource structuredDataSource = new KsqlStream(sqlExpression, ksqlStructuredDataOutputNode.getId().toString(), ksqlStructuredDataOutputNode.getSchema(), ksqlStructuredDataOutputNode.getKeyField(), ksqlStructuredDataOutputNode.getTimestampField() == null ? ksqlStructuredDataOutputNode.getTheSourceNode().getTimestampField() : ksqlStructuredDataOutputNode.getTimestampField(), ksqlStructuredDataOutputNode.getKsqlTopic());
        tempMetaStore.putTopic(ksqlStructuredDataOutputNode.getKsqlTopic());
        tempMetaStore.putSource(structuredDataSource.cloneWithTimeKeyColumns());
    }
    return logicalPlan;
}
Also used : StructuredDataSource(io.confluent.ksql.metastore.StructuredDataSource) KsqlStream(io.confluent.ksql.metastore.KsqlStream) PlanNode(io.confluent.ksql.planner.plan.PlanNode) LogicalPlanner(io.confluent.ksql.planner.LogicalPlanner) Analysis(io.confluent.ksql.analyzer.Analysis) AggregateAnalysis(io.confluent.ksql.analyzer.AggregateAnalysis) QueryAnalyzer(io.confluent.ksql.analyzer.QueryAnalyzer) AggregateAnalysis(io.confluent.ksql.analyzer.AggregateAnalysis) KsqlStructuredDataOutputNode(io.confluent.ksql.planner.plan.KsqlStructuredDataOutputNode)

Example 14 with Analysis

use of io.confluent.ksql.analyzer.Analysis in project ksql by confluentinc.

the class CodeGenRunnerTest method evalBooleanExpr.

private boolean evalBooleanExpr(String queryFormat, int cola, int colb, Object[] values) throws Exception {
    String simpleQuery = String.format(queryFormat, cola, colb);
    Analysis analysis = analyzeQuery(simpleQuery);
    ExpressionMetadata expressionEvaluatorMetadata0 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(0));
    assertThat(expressionEvaluatorMetadata0.getIndexes().length, equalTo(2));
    int idx0 = expressionEvaluatorMetadata0.getIndexes()[0];
    int idx1 = expressionEvaluatorMetadata0.getIndexes()[1];
    assertThat(idx0, anyOf(equalTo(cola), equalTo(colb)));
    assertThat(idx1, anyOf(equalTo(cola), equalTo(colb)));
    assertThat(idx0, not(equalTo(idx1)));
    if (idx0 == colb) {
        Object tmp = values[0];
        values[0] = values[1];
        values[1] = tmp;
    }
    assertThat(expressionEvaluatorMetadata0.getUdfs().length, equalTo(2));
    Object result0 = expressionEvaluatorMetadata0.getExpressionEvaluator().evaluate(values);
    assertThat(result0, instanceOf(Boolean.class));
    return (Boolean) result0;
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) Analysis(io.confluent.ksql.analyzer.Analysis)

Example 15 with Analysis

use of io.confluent.ksql.analyzer.Analysis in project ksql by confluentinc.

the class CodeGenRunnerTest method testIsNull.

@Test
public void testIsNull() throws Exception {
    String simpleQuery = "SELECT col0 IS NULL FROM CODEGEN_TEST;";
    Analysis analysis = analyzeQuery(simpleQuery);
    ExpressionMetadata expressionEvaluatorMetadata0 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(0));
    assertThat(expressionEvaluatorMetadata0.getIndexes().length, equalTo(1));
    int idx0 = expressionEvaluatorMetadata0.getIndexes()[0];
    assertThat(idx0, equalTo(0));
    assertThat(expressionEvaluatorMetadata0.getUdfs().length, equalTo(1));
    Object result0 = expressionEvaluatorMetadata0.getExpressionEvaluator().evaluate(new Object[] { null });
    assertThat(result0, instanceOf(Boolean.class));
    assertThat((Boolean) result0, is(true));
    result0 = expressionEvaluatorMetadata0.getExpressionEvaluator().evaluate(new Object[] { 12345L });
    assertThat(result0, instanceOf(Boolean.class));
    Assert.assertThat((Boolean) result0, is(false));
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) Analysis(io.confluent.ksql.analyzer.Analysis) Test(org.junit.Test)

Aggregations

Analysis (io.confluent.ksql.analyzer.Analysis)19 Test (org.junit.Test)11 AnalysisContext (io.confluent.ksql.analyzer.AnalysisContext)6 Analyzer (io.confluent.ksql.analyzer.Analyzer)6 Statement (io.confluent.ksql.parser.tree.Statement)6 ExpressionMetadata (io.confluent.ksql.util.ExpressionMetadata)6 AggregateAnalysis (io.confluent.ksql.analyzer.AggregateAnalysis)4 Schema (org.apache.kafka.connect.data.Schema)4 AggregateAnalyzer (io.confluent.ksql.analyzer.AggregateAnalyzer)3 Expression (io.confluent.ksql.parser.tree.Expression)3 LogicalPlanner (io.confluent.ksql.planner.LogicalPlanner)3 PlanNode (io.confluent.ksql.planner.plan.PlanNode)3 GenericRowValueTypeEnforcer (io.confluent.ksql.util.GenericRowValueTypeEnforcer)2 QueryAnalyzer (io.confluent.ksql.analyzer.QueryAnalyzer)1 KsqlStream (io.confluent.ksql.metastore.KsqlStream)1 StructuredDataSource (io.confluent.ksql.metastore.StructuredDataSource)1 KsqlStructuredDataOutputNode (io.confluent.ksql.planner.plan.KsqlStructuredDataOutputNode)1 AggregateExpressionRewriter (io.confluent.ksql.util.AggregateExpressionRewriter)1