vertical-blank / sql-formatter

SQL formatter written with only Java Standard Library, without dependencies.
MIT License
226 stars 47 forks source link

Fix spark lambdas #65

Closed NathanEckert closed 1 year ago

NathanEckert commented 1 year ago

This PR solves the following issue:

In Spark SQL, it is possible to use lambda.

However, the formatter is breaking valid SQL statements:

import static org.assertj.core.api.Assertions.assertThat;

import com.github.vertical_blank.sqlformatter.SqlFormatter;
import com.github.vertical_blank.sqlformatter.languages.Dialect;
import org.junit.jupiter.api.Test;

public class TestFormatter {

    String SQL_INPUT = "SELECT aggregate(array(1, 2, 3), 0, (acc, x) -> acc + x, acc -> acc * 10);";

    @Test
    public void testFormatter() {
        assertThat(SqlFormatter.of(Dialect.SparkSql).format(SQL_INPUT)).contains("->");

    }

}

The output is:

java.lang.AssertionError: 
Expecting actual:
  "SELECT
  aggregate(array(1, 2, 3), 0, (acc, x) - > acc + x, acc - > acc * 10);"
to contain:
  "->"

In order to be valid Spark SQL, the symbol -> should be kept as a whole and not split into - >

NathanEckert commented 1 year ago

mvn spotless:apply

Done

vertical-blank commented 1 year ago

LGTM

vertical-blank commented 1 year ago

@NathanEckert I've released 2.0.4 to Maven Central. https://repo1.maven.org/maven2/com/github/vertical-blank/sql-formatter/2.0.4/

NathanEckert commented 1 year ago

@NathanEckert I've released 2.0.4 to Maven Central. https://repo1.maven.org/maven2/com/github/vertical-blank/sql-formatter/2.0.4/

Thanks a lot !