protected Node visitWhenClause(final WhenClause node, final Object context) { // use an if/else block here (instead of isPresent.map(...).orElse(...)) so only one object // gets instantiated (issue #1784) if (node.getLocation().isPresent()) { return new WhenClause(node.getLocation().get(), (Expression) process(node.getOperand(), context), (Expression) process(node.getResult(), context)); } else { return new WhenClause((Expression) process(node.getOperand(), context), (Expression) process(node.getResult(), context)); } }
@Override protected Node visitWhenClause(final WhenClause node, final Node parent) { node.setParent(parent); process(node.getOperand(), node); process(node.getResult(), node); return null; }
@Override protected R visitWhenClause(final WhenClause node, final C context) { process(node.getOperand(), context); process(node.getResult(), context); return null; }
@Override public Node visitWhenClause(final SqlBaseParser.WhenClauseContext context) { return new WhenClause( getLocation(context), (Expression) visit(context.condition), (Expression) visit(context.result) ); }
@Override protected String visitWhenClause(final WhenClause node, final Boolean unmangleNames) { return "WHEN " + process(node.getOperand(), unmangleNames) + " THEN " + process( node.getResult(), unmangleNames); }
@Test public void shouldFormatSearchedCaseExpression() { final SearchedCaseExpression expression = new SearchedCaseExpression( Collections.singletonList( new WhenClause(new StringLiteral("foo"), new LongLiteral(1))), Optional.empty()); assertThat(ExpressionFormatter.formatExpression(expression), equalTo("(CASE WHEN 'foo' THEN 1 END)")); }
@Override protected R visitWhenClause(final WhenClause node, final C context) { process(node.getOperand(), context); process(node.getResult(), context); return null; }
@Test public void shouldFormatSearchedCaseExpressionWithDefaultValue() { final SearchedCaseExpression expression = new SearchedCaseExpression( Collections.singletonList( new WhenClause(new StringLiteral("foo"), new LongLiteral(1))), Optional.of(new LongLiteral(2))); assertThat(ExpressionFormatter.formatExpression(expression), equalTo("(CASE WHEN 'foo' THEN 1 ELSE 2 END)")); }
@Test public void shouldBuildSearchedCaseStatement() { // Given: final String statementString = "CREATE STREAM S AS SELECT CASE WHEN orderunits < 10 THEN 'small' WHEN orderunits < 100 THEN 'medium' ELSE 'large' END FROM orders;"; // When: final Statement statement = KSQL_PARSER.buildAst(statementString, metaStore).get(0) .getStatement(); // Then: final SearchedCaseExpression searchedCaseExpression = getSearchedCaseExpressionFromCsas(statement); assertThat(searchedCaseExpression.getWhenClauses().size(), equalTo(2)); assertThat(searchedCaseExpression.getWhenClauses().get(0).getOperand().toString(), equalTo("(ORDERS.ORDERUNITS < 10)")); assertThat(searchedCaseExpression.getWhenClauses().get(0).getResult().toString(), equalTo("'small'")); assertThat(searchedCaseExpression.getWhenClauses().get(1).getOperand().toString(), equalTo("(ORDERS.ORDERUNITS < 100)")); assertThat(searchedCaseExpression.getWhenClauses().get(1).getResult().toString(), equalTo("'medium'")); assertTrue(searchedCaseExpression.getDefaultValue().isPresent()); assertThat(searchedCaseExpression.getDefaultValue().get().toString(), equalTo("'large'")); }
@Test public void shouldFormatSimpleCaseExpression() { final SimpleCaseExpression expression = new SimpleCaseExpression( new StringLiteral("operand"), Collections.singletonList( new WhenClause(new StringLiteral("foo"), new LongLiteral(1))), Optional.empty()); assertThat(ExpressionFormatter.formatExpression(expression), equalTo("(CASE 'operand' WHEN 'foo' THEN 1 END)")); }
@Test public void shouldFormatSimpleCaseExpressionWithDefaultValue() { final SimpleCaseExpression expression = new SimpleCaseExpression( new StringLiteral("operand"), Collections.singletonList( new WhenClause(new StringLiteral("foo"), new LongLiteral(1))), Optional.of(new LongLiteral(2))); assertThat(ExpressionFormatter.formatExpression(expression), equalTo("(CASE 'operand' WHEN 'foo' THEN 1 ELSE 2 END)")); }
@Test public void shouldFormatWhen() { assertThat(ExpressionFormatter.formatExpression(new WhenClause(new LongLiteral(1), new LongLiteral(2))), equalTo("WHEN 1 THEN 2")); }