private FunctionCall getNewFunctionCall( final DereferenceExpression dereferenceExpression, final Object context ) { final Expression createFunctionResult = (Expression) process(dereferenceExpression.getBase(), context); final String fieldName = dereferenceExpression.getFieldName(); return new FunctionCall( QualifiedName.of("FETCH_FIELD_FROM_STRUCT"), ImmutableList.of(createFunctionResult, new StringLiteral(fieldName))); }
protected Node visitStringLiteral(final StringLiteral 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 StringLiteral(node.getLocation().get(), node.getValue()); } else { return new StringLiteral(node.getValue()); } }
@Override protected String visitStringLiteral(final StringLiteral node, final Boolean unmangleNames) { return formatStringLiteral(node.getValue()); }
@Test public void shouldFormatLikePredicate() { final LikePredicate predicate = new LikePredicate(new StringLiteral("string"), new StringLiteral("*"), new StringLiteral("\\")); assertThat(ExpressionFormatter.formatExpression(predicate), equalTo("('string' LIKE '*' ESCAPE '\\')")); }
@Test public void shouldFormatLogicalBinaryExpression() { final LogicalBinaryExpression expression = new LogicalBinaryExpression(LogicalBinaryExpression.Type.AND, new StringLiteral("a"), new StringLiteral("b")); assertThat(ExpressionFormatter.formatExpression(expression), equalTo("('a' AND 'b')")); }
@Test public void shouldFormatNullIfExpression() { assertThat(ExpressionFormatter.formatExpression(new NullIfExpression( new StringLiteral("first"), new StringLiteral("second"))), equalTo("NULLIF('first', 'second')")); }
@Test public void shouldFormatInPredicate() { assertThat(ExpressionFormatter.formatExpression(new InPredicate(new StringLiteral("foo"), new StringLiteral("a"))), equalTo("('foo' IN 'a')")); }
@Override public Node visitStringLiteral(final SqlBaseParser.StringLiteralContext context) { return new StringLiteral(getLocation(context), unquote(context.STRING().getText(), "'")); }
@Override public Node visitTimeZoneString(final SqlBaseParser.TimeZoneStringContext context) { return new StringLiteral(getLocation(context), unquote(context.STRING().getText(), "'")); }
@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 shouldFormatCreateWithEmptySchema() { final CreateStream createStream = new CreateStream( QualifiedName.of("TEST"), Collections.emptyList(), false, Collections.singletonMap( DdlConfig.KAFKA_TOPIC_NAME_PROPERTY, new StringLiteral("topic_test") )); final String sql = SqlFormatter.formatSql(createStream); final String expectedSql = "CREATE STREAM TEST \n WITH (KAFKA_TOPIC='topic_test');"; assertThat(sql, equalTo(expectedSql)); }
@Test public void shouldFormatIsNullPredicate() { assertThat(ExpressionFormatter.formatExpression(new IsNullPredicate(new StringLiteral("name"))), equalTo("('name' IS NULL)")); }
@Test public void shouldFormatInListExpression() { assertThat(ExpressionFormatter.formatExpression(new InListExpression(Collections.singletonList(new StringLiteral("a")))), equalTo("('a')")); }
@Test public void shouldFormatExtract() { final String result = ExpressionFormatter.formatExpression(new Extract(new StringLiteral("17/12/2017"), Extract.Field.DAY)); assertThat(result, equalTo("EXTRACT(DAY FROM '17/12/2017')")); }
@Test public void shouldFormatIsNotNullPredicate() { assertThat(ExpressionFormatter.formatExpression(new IsNotNullPredicate(new StringLiteral("name"))), equalTo("('name' IS NOT NULL)")); }
@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 shouldFormatDereferenceExpression() { assertThat(ExpressionFormatter.formatExpression(new DereferenceExpression(new StringLiteral("foo"), "name")), equalTo("'foo'->name")); }
@Test public void shouldFormatFunctionCallWithCount() { final FunctionCall functionCall = new FunctionCall(QualifiedName.of("function", "COUNT"), Collections.singletonList(new StringLiteral("name"))); assertThat(ExpressionFormatter.formatExpression(functionCall), equalTo("function.COUNT('name')")); }
@Test public void shouldFormatSubscriptExpression() { assertThat(ExpressionFormatter.formatExpression(new SubscriptExpression( new StringLiteral("abc"), new DoubleLiteral("3.0"))), equalTo("'abc'[3.0]")); }
@Test public void shouldFormatStringLiteral() { assertThat(ExpressionFormatter.formatExpression(new StringLiteral("string")), equalTo("'string'")); }