Skip to content
Open
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
5 changes: 5 additions & 0 deletions src/main/jjtree/net/sf/jsqlparser/parser/JSqlParserCC.jjt
Original file line number Diff line number Diff line change
Expand Up @@ -10705,6 +10705,8 @@ ColDataType ColDataType():
Token prefix = null;
Token tk = null;
Token tk2 = null;
Token intervalUnit = null;
Token intervalTo = null;
String schema;
String type="";
List<String> argumentsStringList = new ArrayList<String>();
Expand Down Expand Up @@ -10753,6 +10755,9 @@ ColDataType ColDataType():
| tk=<K_NAME>
) { schema = tk.image; }

// INTERVAL qualifier such as `interval hour to minute` or `interval year` (PG/Oracle column type)
[ LOOKAHEAD({ tk.kind == K_INTERVAL && getToken(1).kind == K_DATE_LITERAL }) intervalUnit=<K_DATE_LITERAL> { schema += " " + intervalUnit.image; }
[ intervalTo=<K_TO> intervalUnit=<K_DATE_LITERAL> { schema += " " + intervalTo.image + " " + intervalUnit.image; } ] ]
[ LOOKAHEAD(2) "." arrayType = ColDataType() { schema += "." + arrayType.toString(); } ]
{ colDataType.setDataType(schema); }
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import org.junit.jupiter.api.Test;

import static net.sf.jsqlparser.test.TestUtils.assertSqlCanBeParsedAndDeparsed;
import static org.junit.jupiter.api.Assertions.assertEquals;

class ColDataTypeTest {
@Test
Expand Down Expand Up @@ -57,4 +58,26 @@ void testStruct() throws JSQLParserException {
" );\n";
assertSqlCanBeParsedAndDeparsed(sqlStr, true);
}

// PG/Oracle allow an INTERVAL column type carrying a time-unit qualifier,
// e.g. `interval hour to minute`. Previously the qualifier was not consumed
// as part of the data type, so parsing failed with "Encountered ... <K_DATE_LITERAL>".
@Test
void testIntervalQualifierIssue1728() throws JSQLParserException {
String sqlStr = "CREATE TABLE films (code char(5), len interval hour to minute)";
CreateTable create = (CreateTable) assertSqlCanBeParsedAndDeparsed(sqlStr, true);

// The qualifier must be attached to the column's data type, not parsed as an
// alias or left unconsumed.
ColumnDefinition len = create.getColumnDefinitions().stream()
.filter(c -> c.getColumnName().equalsIgnoreCase("len"))
.findFirst()
.orElseThrow();
assertEquals("interval hour to minute", len.getColDataType().getDataType());
}

@Test
void testIntervalQualifierYearToMonthIssue1728() throws JSQLParserException {
assertSqlCanBeParsedAndDeparsed("CREATE TABLE t (a interval year to month)", true);
}
}
Loading