Search in sources :

Example 1 with Analysis

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

the class LogicalPlanBuilder method buildLogicalPlan.

public PlanNode buildLogicalPlan(String queryStr) {
    List<Statement> statements = parser.buildAst(queryStr, metaStore);
    Analysis analysis = new Analysis();
    Analyzer analyzer = new Analyzer(queryStr, analysis, metaStore);
    analyzer.process(statements.get(0), new AnalysisContext(null));
    AggregateAnalysis aggregateAnalysis = new AggregateAnalysis();
    AggregateAnalyzer aggregateAnalyzer = new AggregateAnalyzer(aggregateAnalysis, analysis, functionRegistry);
    AggregateExpressionRewriter aggregateExpressionRewriter = new AggregateExpressionRewriter(functionRegistry);
    for (Expression expression : analysis.getSelectExpressions()) {
        aggregateAnalyzer.process(expression, new AnalysisContext(null));
        if (!aggregateAnalyzer.isHasAggregateFunction()) {
            aggregateAnalysis.addNonAggResultColumns(expression);
        }
        aggregateAnalysis.addFinalSelectExpression(ExpressionTreeRewriter.rewriteWith(aggregateExpressionRewriter, expression));
        aggregateAnalyzer.setHasAggregateFunction(false);
    }
    // Build a logical plan
    return new LogicalPlanner(analysis, aggregateAnalysis, functionRegistry).buildPlan();
}
Also used : LogicalPlanner(io.confluent.ksql.planner.LogicalPlanner) AggregateAnalyzer(io.confluent.ksql.analyzer.AggregateAnalyzer) Expression(io.confluent.ksql.parser.tree.Expression) Statement(io.confluent.ksql.parser.tree.Statement) Analysis(io.confluent.ksql.analyzer.Analysis) AggregateAnalysis(io.confluent.ksql.analyzer.AggregateAnalysis) AggregateAnalysis(io.confluent.ksql.analyzer.AggregateAnalysis) AnalysisContext(io.confluent.ksql.analyzer.AnalysisContext) AggregateExpressionRewriter(io.confluent.ksql.util.AggregateExpressionRewriter) AggregateAnalyzer(io.confluent.ksql.analyzer.AggregateAnalyzer) Analyzer(io.confluent.ksql.analyzer.Analyzer)

Example 2 with Analysis

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

the class CodeGenRunnerTest method testIsNotNull.

@Test
public void testIsNotNull() throws Exception {
    String simpleQuery = "SELECT col0 IS NOT 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));
    Assert.assertThat((Boolean) result0, is(false));
    result0 = expressionEvaluatorMetadata0.getExpressionEvaluator().evaluate(new Object[] { 12345L });
    assertThat(result0, instanceOf(Boolean.class));
    assertThat((Boolean) result0, is(true));
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) Analysis(io.confluent.ksql.analyzer.Analysis) Test(org.junit.Test)

Example 3 with Analysis

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

the class CodeGenRunnerTest method testStringUDFExpr.

@Test
public void testStringUDFExpr() throws Exception {
    GenericRowValueTypeEnforcer genericRowValueTypeEnforcer = new GenericRowValueTypeEnforcer(schema);
    String simpleQuery = "SELECT LCASE(col1), UCASE(col2), TRIM(col1), CONCAT(col1,'_test'), SUBSTRING(col1, 1, 3) FROM codegen_test;";
    Analysis analysis = analyzeQuery(simpleQuery);
    ExpressionMetadata expressionEvaluator0 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(0));
    Object argObj0 = genericRowValueTypeEnforcer.enforceFieldType(2, "Hello");
    Object result0 = expressionEvaluator0.getExpressionEvaluator().evaluate(new Object[] { expressionEvaluator0.getUdfs()[0], argObj0 });
    assertThat(result0, instanceOf(String.class));
    assertThat(result0, equalTo("hello"));
    ExpressionMetadata expressionEvaluator1 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(1));
    Object argObj1 = genericRowValueTypeEnforcer.enforceFieldType(2, "Hello");
    Object result1 = expressionEvaluator1.getExpressionEvaluator().evaluate(new Object[] { expressionEvaluator1.getUdfs()[0], argObj1 });
    assertThat(result1, instanceOf(String.class));
    assertThat(result1, equalTo("HELLO"));
    ExpressionMetadata expressionEvaluator2 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(2));
    Object argObj2 = genericRowValueTypeEnforcer.enforceFieldType(2, " Hello ");
    Object result2 = expressionEvaluator2.getExpressionEvaluator().evaluate(new Object[] { expressionEvaluator2.getUdfs()[0], argObj2 });
    assertThat(result2, instanceOf(String.class));
    assertThat(result2, equalTo("Hello"));
    ExpressionMetadata expressionEvaluator3 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(3));
    Object argObj3 = genericRowValueTypeEnforcer.enforceFieldType(2, "Hello");
    Object result3 = expressionEvaluator3.getExpressionEvaluator().evaluate(new Object[] { expressionEvaluator3.getUdfs()[0], argObj3 });
    assertThat(result3, instanceOf(String.class));
    assertThat(result3, equalTo("Hello_test"));
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) GenericRowValueTypeEnforcer(io.confluent.ksql.util.GenericRowValueTypeEnforcer) Analysis(io.confluent.ksql.analyzer.Analysis) Test(org.junit.Test)

Example 4 with Analysis

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

the class CodeGenRunnerTest method testArithmaticExpr.

@Test
public void testArithmaticExpr() throws Exception {
    String simpleQuery = "SELECT col0+col3, col2, col3+10, col0*25, 12*4+2 FROM codegen_test WHERE col0 > 100;";
    Analysis analysis = analyzeQuery(simpleQuery);
    ExpressionMetadata expressionEvaluatorMetadata0 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(0));
    assertThat(expressionEvaluatorMetadata0.getIndexes().length, equalTo(2));
    assertThat(expressionEvaluatorMetadata0.getIndexes()[0], equalTo(3));
    assertThat(expressionEvaluatorMetadata0.getIndexes()[1], equalTo(0));
    assertThat(expressionEvaluatorMetadata0.getUdfs().length, equalTo(2));
    Object result0 = expressionEvaluatorMetadata0.getExpressionEvaluator().evaluate(new Object[] { 10.0, 5l });
    assertThat(result0, instanceOf(Double.class));
    assertThat(((Double) result0), equalTo(15.0));
    ExpressionMetadata expressionEvaluatorMetadata1 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(3));
    assertThat(expressionEvaluatorMetadata1.getIndexes().length, equalTo(1));
    assertThat(expressionEvaluatorMetadata1.getIndexes()[0], equalTo(0));
    assertThat(expressionEvaluatorMetadata1.getUdfs().length, equalTo(1));
    Object result1 = expressionEvaluatorMetadata1.getExpressionEvaluator().evaluate(new Object[] { 5l });
    assertThat(result1, instanceOf(Long.class));
    assertThat(((Long) result1), equalTo(125l));
    ExpressionMetadata expressionEvaluatorMetadata2 = codeGenRunner.buildCodeGenFromParseTree(analysis.getSelectExpressions().get(4));
    assertThat(expressionEvaluatorMetadata2.getIndexes().length, equalTo(0));
    assertThat(expressionEvaluatorMetadata2.getUdfs().length, equalTo(0));
    Object result2 = expressionEvaluatorMetadata2.getExpressionEvaluator().evaluate(new Object[] {});
    assertThat(result2, instanceOf(Long.class));
    assertThat(((Long) result2), equalTo(50L));
}
Also used : ExpressionMetadata(io.confluent.ksql.util.ExpressionMetadata) Analysis(io.confluent.ksql.analyzer.Analysis) Test(org.junit.Test)

Example 5 with Analysis

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

the class SqlToJavaVisitorTest method shouldCreateCorrectCastJavaExpression.

@Test
public void shouldCreateCorrectCastJavaExpression() throws Exception {
    String simpleQuery = "SELECT cast(col0 AS INTEGER), cast(col3 as BIGINT), cast(col3 as " + "varchar) FROM " + "test1 WHERE " + "col0 > 100;";
    Analysis analysis = analyzeQuery(simpleQuery);
    String javaExpression0 = new SqlToJavaVisitor(schema, functionRegistry).process(analysis.getSelectExpressions().get(0));
    String javaExpression1 = new SqlToJavaVisitor(schema, functionRegistry).process(analysis.getSelectExpressions().get(1));
    String javaExpression2 = new SqlToJavaVisitor(schema, functionRegistry).process(analysis.getSelectExpressions().get(2));
    assertThat(javaExpression0, equalTo("(new Long(TEST1_COL0).intValue())"));
    assertThat(javaExpression1, equalTo("(new Double(TEST1_COL3).longValue())"));
    assertThat(javaExpression2, equalTo("String.valueOf(TEST1_COL3)"));
}
Also used : 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