Skip to content

Extended support Postgres' Extract( field FROM source) #1591

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jul 19, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Additionally, we have fixed many errors and improved the code quality and the te
* support table option **character set** and **index** options
* support Postgresql optional **TABLE** in **TRUNCATE**
* support for `ANALYZE mytable`
* extended support Postgres' `Extract( field FROM source)` where `field` is a String instead of a Keyword


## Building from the sources
Expand Down
5 changes: 3 additions & 2 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -4410,13 +4410,14 @@ WindowOffset WindowOffset():
ExtractExpression ExtractExpression() :
{
ExtractExpression retval = new ExtractExpression();
String token = null;
String fieldName = null;
Token token = null;
Expression expr = null;
}
{
<K_EXTRACT>
"("
token=RelObjectName() { retval.setName(token); }
( fieldName=RelObjectName() { retval.setName(fieldName); } | token=<S_CHAR_LITERAL> { retval.setName(token.image); } )
<K_FROM>
expr=SimpleExpression() { retval.setExpression(expr); }
")"
Expand Down
35 changes: 35 additions & 0 deletions src/test/java/net/sf/jsqlparser/statement/select/PostgresTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
package net.sf.jsqlparser.statement.select;

import net.sf.jsqlparser.JSQLParserException;
import net.sf.jsqlparser.test.TestUtils;
import org.junit.jupiter.api.Test;

public class PostgresTest {
@Test
public void testExtractFunction() throws JSQLParserException {
String sqlStr = "SELECT EXTRACT(HOUR FROM TIMESTAMP '2001-02-16 20:38:40')";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);

sqlStr = "SELECT EXTRACT('HOUR' FROM TIMESTAMP '2001-02-16 20:38:40')";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);

sqlStr = "SELECT EXTRACT('HOURS' FROM TIMESTAMP '2001-02-16 20:38:40')";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

@Test
public void testExtractFunctionIssue1582() throws JSQLParserException {
String sqlStr = "" +
"select\n" +
" t0.operatienr\n" +
" , case\n" +
" when\n" +
" 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" +
" 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" +
" end = 0 then null\n" +
" else '25. Meer dan 4 uur'\n" +
" end \n" +
" as snijtijd_interval";
TestUtils.assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}
}