@@ -774,4 +774,82 @@ public void testAdjacentStringLiteralsBoolean() throws Exception {
774774 assertEquals ("SELECT 'ab'" , CCJSqlParserUtil .parse ("SELECT 'a'\n 'b'" ,
775775 p -> p .withAdjacentStringLiterals ()).toString ());
776776 }
777+
778+ @ Test
779+ public void testAdjacentStringLiteralsNewline () throws Exception {
780+ // default (off): the MySQL / SQL Server alias reading, unchanged
781+ assertEquals ("SELECT 'a' 'b'" , CCJSqlParserUtil .parse ("SELECT 'a' 'b'" ).toString ());
782+ assertEquals ("SELECT 'a' 'b'" , CCJSqlParserUtil .parse ("SELECT 'a'\n 'b'" ).toString ());
783+ assertThrows (JSQLParserException .class ,
784+ () -> CCJSqlParserUtil .parse ("SELECT * FROM t WHERE x = 'a'\n 'b'" ));
785+ // newline mode: the standard / Postgres reading, "Two string constants
786+ // that are only separated by whitespace with at least one newline are
787+ // concatenated"
788+ PlainSelect select = (PlainSelect ) ((Select ) CCJSqlParserUtil .parse ("SELECT 'a'\n 'b'" ,
789+ p -> p .withAdjacentStringLiterals (
790+ AbstractJSqlParser .AdjacentStringLiterals .NEWLINE )))
791+ .getSelectBody ();
792+ Expression expression = select .getSelectItems ().get (0 ).getExpression ();
793+ assertTrue (expression instanceof StringValue );
794+ assertEquals ("ab" , ((StringValue ) expression ).getValue ());
795+ assertEquals ("SELECT 'ab'" , select .toString ());
796+ // same line keeps the alias reading under newline mode
797+ assertEquals ("SELECT 'a' 'b'" ,
798+ CCJSqlParserUtil .parse ("SELECT 'a' 'b'" ,
799+ p -> p .withAdjacentStringLiterals (
800+ AbstractJSqlParser .AdjacentStringLiterals .NEWLINE ))
801+ .toString ());
802+ // expression positions concatenate too
803+ assertEquals ("SELECT * FROM t WHERE x = 'ab'" ,
804+ CCJSqlParserUtil .parse ("SELECT * FROM t WHERE x = 'a'\n 'b'" ,
805+ p -> p .withAdjacentStringLiterals (
806+ AbstractJSqlParser .AdjacentStringLiterals .NEWLINE ))
807+ .toString ());
808+ // three parts merge into one literal
809+ assertEquals ("SELECT 'abc'" ,
810+ CCJSqlParserUtil .parse ("SELECT 'a'\n 'b'\n 'c'" ,
811+ p -> p .withAdjacentStringLiterals (
812+ AbstractJSqlParser .AdjacentStringLiterals .NEWLINE ))
813+ .toString ());
814+ // the ANSI SQL and Postgres presets carry it, the MySQL preset does not
815+ for (AbstractJSqlParser .Dialect dialect : new AbstractJSqlParser .Dialect [] {
816+ AbstractJSqlParser .Dialect .ANSI_SQL , AbstractJSqlParser .Dialect .POSTGRESQL }) {
817+ assertEquals ("SELECT 'ab'" , CCJSqlParserUtil .parse ("SELECT 'a'\n 'b'" ,
818+ p -> p .withDialect (dialect )).toString ());
819+ }
820+ assertEquals ("SELECT 'a' 'b'" , CCJSqlParserUtil .parse ("SELECT 'a'\n 'b'" ,
821+ p -> p .withDialect (AbstractJSqlParser .Dialect .MYSQL )).toString ());
822+ }
823+
824+ @ Test
825+ public void testAdjacentStringLiteralsWhitespace () throws Exception {
826+ // whitespace mode: the GoogleSQL / Spark reading, same line
827+ // concatenates (their literal chunking)
828+ assertEquals ("SELECT 'ab'" ,
829+ CCJSqlParserUtil .parse ("SELECT 'a' 'b'" ,
830+ p -> p .withAdjacentStringLiterals (
831+ AbstractJSqlParser .AdjacentStringLiterals .WHITESPACE ))
832+ .toString ());
833+ // combined with allowDoubleQuotedStrings: the BigQuery chunking shape
834+ assertEquals ("SELECT \" 12\" " ,
835+ CCJSqlParserUtil .parse ("SELECT \" 1\" \" 2\" " ,
836+ p -> p .withDoubleQuotedStrings (true ).withAdjacentStringLiterals (
837+ AbstractJSqlParser .AdjacentStringLiterals .WHITESPACE ))
838+ .toString ());
839+ }
840+
841+ @ Test
842+ public void testAdjacentStringLiteralsBoolean () throws Exception {
843+ // true: the standard (newline) mode, the same line keeps the alias reading
844+ assertEquals ("SELECT 'ab'" , CCJSqlParserUtil .parse ("SELECT 'a'\n 'b'" ,
845+ p -> p .withAdjacentStringLiterals (true )).toString ());
846+ assertEquals ("SELECT 'a' 'b'" , CCJSqlParserUtil .parse ("SELECT 'a' 'b'" ,
847+ p -> p .withAdjacentStringLiterals (true )).toString ());
848+ // false: off, the alias reading also across newlines
849+ assertEquals ("SELECT 'a' 'b'" , CCJSqlParserUtil .parse ("SELECT 'a'\n 'b'" ,
850+ p -> p .withAdjacentStringLiterals (false )).toString ());
851+ // the no-arg variant enables the standard mode
852+ assertEquals ("SELECT 'ab'" , CCJSqlParserUtil .parse ("SELECT 'a'\n 'b'" ,
853+ p -> p .withAdjacentStringLiterals ()).toString ());
854+ }
777855}
0 commit comments