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();
}
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));
}
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"));
}
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));
}
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)"));
}
Aggregations