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()); } }
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))); }
@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(), "'")); }
@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 shouldFormatIsNullPredicate() { assertThat(ExpressionFormatter.formatExpression(new IsNullPredicate(new StringLiteral("name"))), equalTo("('name' IS 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 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 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 shouldFormatStringLiteral() { assertThat(ExpressionFormatter.formatExpression(new StringLiteral("string")), equalTo("'string'")); }
@Test public void shouldFormatSubscriptExpression() { assertThat(ExpressionFormatter.formatExpression(new SubscriptExpression( new StringLiteral("abc"), new DoubleLiteral("3.0"))), equalTo("'abc'[3.0]")); }
@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)")); }
@Test public void shouldFormatFunctionWithDistinct() { final FunctionCall functionCall = new FunctionCall(new NodeLocation(1, 1), QualifiedName.of("function", "COUNT"), true, Collections.singletonList(new StringLiteral("name"))); assertThat(ExpressionFormatter.formatExpression(functionCall), equalTo("function.COUNT(DISTINCT 'name')")); }
@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 shouldFormatBetweenPredicate() { final BetweenPredicate predicate = new BetweenPredicate(new StringLiteral("blah"), new LongLiteral(5), new LongLiteral(10)); assertThat(ExpressionFormatter.formatExpression(predicate), equalTo("('blah' BETWEEN 5 AND 10)")); }
@Test public void shouldFormatFunctionCallWithWindow() { final FunctionCall functionCall = new FunctionCall(new NodeLocation(1, 1), QualifiedName.of("function"), Optional.of(new Window("window", new WindowExpression("blah", new TumblingWindowExpression(1L, TimeUnit.SECONDS)))), false, Collections.singletonList(new StringLiteral("name"))); assertThat(ExpressionFormatter.formatExpression(functionCall), equalTo("function('name') OVER WINDOW WINDOW blah TUMBLING ( SIZE 1 SECONDS ) ")); }