Skip to content

Commit a250886

Browse files
committed
Make the behavior of Postgre dialect independent of ansi mode config
1 parent a9959be commit a250886

File tree

6 files changed

+95
-98
lines changed

6 files changed

+95
-98
lines changed

sql/catalyst/src/main/antlr4/org/apache/spark/sql/catalyst/parser/SqlBase.g4

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -748,7 +748,7 @@ primaryExpression
748748
| qualifiedName '.' ASTERISK #star
749749
| '(' namedExpression (',' namedExpression)+ ')' #rowConstructor
750750
| '(' query ')' #subqueryExpression
751-
| qualifiedName '(' (setQuantifier? argument+=expression (',' argument+=expression)*)? ')'
751+
| functionCallName '(' (setQuantifier? argument+=expression (',' argument+=expression)*)? ')'
752752
(OVER windowSpec)? #functionCall
753753
| identifier '->' expression #lambda
754754
| '(' identifier (',' identifier)+ ')' '->' expression #lambda
@@ -912,6 +912,12 @@ qualifiedNameList
912912
: qualifiedName (',' qualifiedName)*
913913
;
914914

915+
functionCallName
916+
: qualifiedName
917+
| LEFT
918+
| RIGHT
919+
;
920+
915921
qualifiedName
916922
: identifier ('.' identifier)*
917923
;

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/AstBuilder.scala

Lines changed: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1585,7 +1585,7 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
15851585
*/
15861586
override def visitFunctionCall(ctx: FunctionCallContext): Expression = withOrigin(ctx) {
15871587
// Create the function call.
1588-
val name = ctx.qualifiedName.getText
1588+
val name = ctx.functionCallName.getText
15891589
val isDistinct = Option(ctx.setQuantifier()).exists(_.DISTINCT != null)
15901590
val arguments = ctx.argument.asScala.map(expression) match {
15911591
case Seq(UnresolvedStar(None))
@@ -1595,7 +1595,8 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
15951595
case expressions =>
15961596
expressions
15971597
}
1598-
val function = UnresolvedFunction(visitFunctionName(ctx.qualifiedName), arguments, isDistinct)
1598+
val function = UnresolvedFunction(
1599+
visitFunctionName(ctx.functionCallName), arguments, isDistinct)
15991600

16001601
// Check if the function is evaluated in a windowed context.
16011602
ctx.windowSpec match {
@@ -1623,6 +1624,17 @@ class AstBuilder(conf: SQLConf) extends SqlBaseBaseVisitor[AnyRef] with Logging
16231624
visitFunctionName(ctx, ctx.identifier().asScala.map(_.getText))
16241625
}
16251626

1627+
/**
1628+
* Create a function database (optional) and name pair.
1629+
*/
1630+
protected def visitFunctionName(ctx: FunctionCallNameContext): FunctionIdentifier = {
1631+
if (ctx.qualifiedName != null) {
1632+
visitFunctionName(ctx.qualifiedName)
1633+
} else {
1634+
FunctionIdentifier(ctx.getText, None)
1635+
}
1636+
}
1637+
16261638
/**
16271639
* Create a function database (optional) and name pair.
16281640
*/

sql/catalyst/src/main/scala/org/apache/spark/sql/catalyst/parser/ParseDriver.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -101,15 +101,15 @@ abstract class AbstractSqlParser(conf: SQLConf) extends ParserInterface with Log
101101
lexer.removeErrorListeners()
102102
lexer.addErrorListener(ParseErrorListener)
103103
lexer.legacy_setops_precedence_enbled = conf.setOpsPrecedenceEnforced
104-
lexer.ansi = conf.dialectSparkAnsiEnabled
104+
lexer.ansi = conf.ansiEnabled
105105

106106
val tokenStream = new CommonTokenStream(lexer)
107107
val parser = new SqlBaseParser(tokenStream)
108108
parser.addParseListener(PostProcessor)
109109
parser.removeErrorListeners()
110110
parser.addErrorListener(ParseErrorListener)
111111
parser.legacy_setops_precedence_enbled = conf.setOpsPrecedenceEnforced
112-
parser.ansi = conf.dialectSparkAnsiEnabled
112+
parser.ansi = conf.ansiEnabled
113113

114114
try {
115115
try {

sql/core/src/test/resources/sql-tests/inputs/postgreSQL/text.sql

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -44,11 +44,7 @@ select concat_ws(',',10,20,null,30);
4444
select concat_ws('',10,20,null,30);
4545
select concat_ws(NULL,10,20,null,30) is null;
4646
select reverse('abcde');
47-
-- [SPARK-28036] Built-in udf left/right has inconsistent behavior
48-
-- [SPARK-28479][SPARK-28989] Parser error when enabling ANSI mode
49-
set spark.sql.dialect.spark.ansi.enabled=false;
5047
select i, left('ahoj', i), right('ahoj', i) from range(-5, 6) t(i) order by i;
51-
set spark.sql.dialect.spark.ansi.enabled=true;
5248
-- [SPARK-28037] Add built-in String Functions: quote_literal
5349
-- select quote_literal('');
5450
-- select quote_literal('abc''');

sql/core/src/test/resources/sql-tests/results/postgreSQL/text.sql.out

100644100755
Lines changed: 72 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
-- Automatically generated by SQLQueryTestSuite
2-
-- Number of queries: 44
2+
-- Number of queries: 42
33

44

55
-- !query 0
@@ -151,18 +151,10 @@ edcba
151151

152152

153153
-- !query 18
154-
set spark.sql.dialect.spark.ansi.enabled=false
155-
-- !query 18 schema
156-
struct<key:string,value:string>
157-
-- !query 18 output
158-
spark.sql.dialect.spark.ansi.enabled false
159-
160-
161-
-- !query 19
162154
select i, left('ahoj', i), right('ahoj', i) from range(-5, 6) t(i) order by i
163-
-- !query 19 schema
155+
-- !query 18 schema
164156
struct<i:bigint,left('ahoj', t.`i`):string,right('ahoj', t.`i`):string>
165-
-- !query 19 output
157+
-- !query 18 output
166158
-5
167159
-4
168160
-3
@@ -176,200 +168,192 @@ struct<i:bigint,left('ahoj', t.`i`):string,right('ahoj', t.`i`):string>
176168
5 ahoj ahoj
177169

178170

179-
-- !query 20
180-
set spark.sql.dialect.spark.ansi.enabled=true
181-
-- !query 20 schema
182-
struct<key:string,value:string>
183-
-- !query 20 output
184-
spark.sql.dialect.spark.ansi.enabled true
185-
186-
187-
-- !query 21
171+
-- !query 19
188172
/*
189173
* format
190174
*/
191175
select format_string(NULL)
192-
-- !query 21 schema
176+
-- !query 19 schema
193177
struct<format_string(CAST(NULL AS STRING)):string>
194-
-- !query 21 output
178+
-- !query 19 output
195179
NULL
196180

197181

198-
-- !query 22
182+
-- !query 20
199183
select format_string('Hello')
200-
-- !query 22 schema
184+
-- !query 20 schema
201185
struct<format_string(Hello):string>
202-
-- !query 22 output
186+
-- !query 20 output
203187
Hello
204188

205189

206-
-- !query 23
190+
-- !query 21
207191
select format_string('Hello %s', 'World')
208-
-- !query 23 schema
192+
-- !query 21 schema
209193
struct<format_string(Hello %s, World):string>
210-
-- !query 23 output
194+
-- !query 21 output
211195
Hello World
212196

213197

214-
-- !query 24
198+
-- !query 22
215199
select format_string('Hello %%')
216-
-- !query 24 schema
200+
-- !query 22 schema
217201
struct<format_string(Hello %%):string>
218-
-- !query 24 output
202+
-- !query 22 output
219203
Hello %
220204

221205

222-
-- !query 25
206+
-- !query 23
223207
select format_string('Hello %%%%')
224-
-- !query 25 schema
208+
-- !query 23 schema
225209
struct<format_string(Hello %%%%):string>
226-
-- !query 25 output
210+
-- !query 23 output
227211
Hello %%
228212

229213

230-
-- !query 26
214+
-- !query 24
231215
select format_string('Hello %s %s', 'World')
232-
-- !query 26 schema
216+
-- !query 24 schema
233217
struct<>
234-
-- !query 26 output
218+
-- !query 24 output
235219
java.util.MissingFormatArgumentException
236220
Format specifier '%s'
237221

238222

239-
-- !query 27
223+
-- !query 25
240224
select format_string('Hello %s')
241-
-- !query 27 schema
225+
-- !query 25 schema
242226
struct<>
243-
-- !query 27 output
227+
-- !query 25 output
244228
java.util.MissingFormatArgumentException
245229
Format specifier '%s'
246230

247231

248-
-- !query 28
232+
-- !query 26
249233
select format_string('Hello %x', 20)
250-
-- !query 28 schema
234+
-- !query 26 schema
251235
struct<format_string(Hello %x, 20):string>
252-
-- !query 28 output
236+
-- !query 26 output
253237
Hello 14
254238

255239

256-
-- !query 29
240+
-- !query 27
257241
select format_string('%1$s %3$s', 1, 2, 3)
258-
-- !query 29 schema
242+
-- !query 27 schema
259243
struct<format_string(%1$s %3$s, 1, 2, 3):string>
260-
-- !query 29 output
244+
-- !query 27 output
261245
1 3
262246

263247

264-
-- !query 30
248+
-- !query 28
265249
select format_string('%1$s %12$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
266-
-- !query 30 schema
250+
-- !query 28 schema
267251
struct<format_string(%1$s %12$s, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12):string>
268-
-- !query 30 output
252+
-- !query 28 output
269253
1 12
270254

271255

272-
-- !query 31
256+
-- !query 29
273257
select format_string('%1$s %4$s', 1, 2, 3)
274-
-- !query 31 schema
258+
-- !query 29 schema
275259
struct<>
276-
-- !query 31 output
260+
-- !query 29 output
277261
java.util.MissingFormatArgumentException
278262
Format specifier '%4$s'
279263

280264

281-
-- !query 32
265+
-- !query 30
282266
select format_string('%1$s %13$s', 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12)
283-
-- !query 32 schema
267+
-- !query 30 schema
284268
struct<>
285-
-- !query 32 output
269+
-- !query 30 output
286270
java.util.MissingFormatArgumentException
287271
Format specifier '%13$s'
288272

289273

290-
-- !query 33
274+
-- !query 31
291275
select format_string('%0$s', 'Hello')
292-
-- !query 33 schema
276+
-- !query 31 schema
293277
struct<format_string(%0$s, Hello):string>
294-
-- !query 33 output
278+
-- !query 31 output
295279
Hello
296280

297281

298-
-- !query 34
282+
-- !query 32
299283
select format_string('Hello %s %1$s %s', 'World', 'Hello again')
300-
-- !query 34 schema
284+
-- !query 32 schema
301285
struct<format_string(Hello %s %1$s %s, World, Hello again):string>
302-
-- !query 34 output
286+
-- !query 32 output
303287
Hello World World Hello again
304288

305289

306-
-- !query 35
290+
-- !query 33
307291
select format_string('Hello %s %s, %2$s %2$s', 'World', 'Hello again')
308-
-- !query 35 schema
292+
-- !query 33 schema
309293
struct<format_string(Hello %s %s, %2$s %2$s, World, Hello again):string>
310-
-- !query 35 output
294+
-- !query 33 output
311295
Hello World Hello again, Hello again Hello again
312296

313297

314-
-- !query 36
298+
-- !query 34
315299
select format_string('>>%10s<<', 'Hello')
316-
-- !query 36 schema
300+
-- !query 34 schema
317301
struct<format_string(>>%10s<<, Hello):string>
318-
-- !query 36 output
302+
-- !query 34 output
319303
>> Hello<<
320304

321305

322-
-- !query 37
306+
-- !query 35
323307
select format_string('>>%10s<<', NULL)
324-
-- !query 37 schema
308+
-- !query 35 schema
325309
struct<format_string(>>%10s<<, NULL):string>
326-
-- !query 37 output
310+
-- !query 35 output
327311
>> null<<
328312

329313

330-
-- !query 38
314+
-- !query 36
331315
select format_string('>>%10s<<', '')
332-
-- !query 38 schema
316+
-- !query 36 schema
333317
struct<format_string(>>%10s<<, ):string>
334-
-- !query 38 output
318+
-- !query 36 output
335319
>> <<
336320

337321

338-
-- !query 39
322+
-- !query 37
339323
select format_string('>>%-10s<<', '')
340-
-- !query 39 schema
324+
-- !query 37 schema
341325
struct<format_string(>>%-10s<<, ):string>
342-
-- !query 39 output
326+
-- !query 37 output
343327
>> <<
344328

345329

346-
-- !query 40
330+
-- !query 38
347331
select format_string('>>%-10s<<', 'Hello')
348-
-- !query 40 schema
332+
-- !query 38 schema
349333
struct<format_string(>>%-10s<<, Hello):string>
350-
-- !query 40 output
334+
-- !query 38 output
351335
>>Hello <<
352336

353337

354-
-- !query 41
338+
-- !query 39
355339
select format_string('>>%-10s<<', NULL)
356-
-- !query 41 schema
340+
-- !query 39 schema
357341
struct<format_string(>>%-10s<<, NULL):string>
358-
-- !query 41 output
342+
-- !query 39 output
359343
>>null <<
360344

361345

362-
-- !query 42
346+
-- !query 40
363347
select format_string('>>%1$10s<<', 'Hello')
364-
-- !query 42 schema
348+
-- !query 40 schema
365349
struct<format_string(>>%1$10s<<, Hello):string>
366-
-- !query 42 output
350+
-- !query 40 output
367351
>> Hello<<
368352

369353

370-
-- !query 43
354+
-- !query 41
371355
DROP TABLE TEXT_TBL
372-
-- !query 43 schema
356+
-- !query 41 schema
373357
struct<>
374-
-- !query 43 output
358+
-- !query 41 output
375359

sql/core/src/test/scala/org/apache/spark/sql/SQLQueryTestSuite.scala

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,6 @@ class SQLQueryTestSuite extends QueryTest with SharedSparkSession {
343343
localSparkSession.udf.register("boolne", (b1: Boolean, b2: Boolean) => b1 != b2)
344344
// vol used by boolean.sql and case.sql.
345345
localSparkSession.udf.register("vol", (s: String) => s)
346-
localSparkSession.conf.set(SQLConf.DIALECT_SPARK_ANSI_ENABLED.key, true)
347346
localSparkSession.conf.set(SQLConf.DIALECT.key, SQLConf.Dialect.POSTGRESQL.toString)
348347
case _: AnsiTest =>
349348
localSparkSession.conf.set(SQLConf.DIALECT_SPARK_ANSI_ENABLED.key, true)

0 commit comments

Comments
 (0)