Skip to content

Commit 2b3ce25

Browse files
extended support Postgres' Extract( field FROM source) where field is a String instead of a Keyword (#1591)
Fixes #1582 Amend the ExtractExpression Add Test case for issue #1582 Amend the README
1 parent 87a37d7 commit 2b3ce25

File tree

3 files changed

+39
-2
lines changed

3 files changed

+39
-2
lines changed

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -73,6 +73,7 @@ Additionally, we have fixed many errors and improved the code quality and the te
7373
* support table option **character set** and **index** options
7474
* support Postgresql optional **TABLE** in **TRUNCATE**
7575
* support for `ANALYZE mytable`
76+
* extended support Postgres' `Extract( field FROM source)` where `field` is a String instead of a Keyword
7677

7778

7879
## Building from the sources

src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4410,13 +4410,14 @@ WindowOffset WindowOffset():
44104410
ExtractExpression ExtractExpression() :
44114411
{
44124412
ExtractExpression retval = new ExtractExpression();
4413-
String token = null;
4413+
String fieldName = null;
4414+
Token token = null;
44144415
Expression expr = null;
44154416
}
44164417
{
44174418
<K_EXTRACT>
44184419
"("
4419-
token=RelObjectName() { retval.setName(token); }
4420+
( fieldName=RelObjectName() { retval.setName(fieldName); } | token=<S_CHAR_LITERAL> { retval.setName(token.image); } )
44204421
<K_FROM>
44214422
expr=SimpleExpression() { retval.setExpression(expr); }
44224423
")"
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package net.sf.jsqlparser.statement.select;
2+
3+
import net.sf.jsqlparser.JSQLParserException;
4+
import net.sf.jsqlparser.test.TestUtils;
5+
import org.junit.jupiter.api.Test;
6+
7+
public class PostgresTest {
8+
@Test
9+
public void testExtractFunction() throws JSQLParserException {
10+
String sqlStr = "SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40')";
11+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
12+
13+
sqlStr = "SELECT EXTRACT('HOUR' FROM TIMESTAMP '2001-02-16 20:38:40')";
14+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
15+
16+
sqlStr = "SELECT EXTRACT('HOURS' FROM TIMESTAMP '2001-02-16 20:38:40')";
17+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
18+
}
19+
20+
@Test
21+
public void testExtractFunctionIssue1582() throws JSQLParserException {
22+
String sqlStr = "" +
23+
"select\n" +
24+
" t0.operatienr\n" +
25+
" , case\n" +
26+
" when\n" +
27+
" case when (t0.vc_begintijd_operatie is null or lpad((extract('hours' from t0.vc_begintijd_operatie::timestamp))::text,2,'0') ||':'|| lpad(extract('minutes' from t0.vc_begintijd_operatie::timestamp)::text,2,'0') = '00:00') then null\n" +
28+
" else (greatest(((extract('hours' from (t0.vc_eindtijd_operatie::timestamp-t0.vc_begintijd_operatie::timestamp))*60 + extract('minutes' from (t0.vc_eindtijd_operatie::timestamp-t0.vc_begintijd_operatie::timestamp)))/60)::numeric(12,2),0))*60\n" +
29+
" end = 0 then null\n" +
30+
" else '25. Meer dan 4 uur'\n" +
31+
" end \n" +
32+
" as snijtijd_interval";
33+
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
34+
}
35+
}

0 commit comments

Comments
 (0)