Skip to content

Commit

Permalink
Fix parse interval issue, cannot parse 'cast(xxx as xx) year' correct…
Browse files Browse the repository at this point in the history
…ly. (#6261)

* Fix parse interval issue, cannot parse 'cast(xxx as xx) year' correctly.

* lint.

* Improve.
  • Loading branch information
lingo-xp authored Dec 2, 2024
1 parent 1a7180c commit 1ae0420
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -231,11 +231,6 @@ public SQLExpr primaryRest(SQLExpr expr) {
lexer.nextToken();
Number num = ((SQLNumericLiteralExpr) expr).getNumber();
expr = new SQLDecimalExpr(num.toString());
} else if (lexer.token() == Token.IDENTIFIER) { // hortonworks
SQLIntervalUnit unit = parseIntervalUnit();
if (unit != null) {
expr = new SQLIntervalExpr(expr, unit);
}
}
break;
default:
Expand Down Expand Up @@ -376,13 +371,23 @@ protected SQLExpr parseInterval() {

SQLIntervalExpr intervalExpr = new SQLIntervalExpr();
intervalExpr.setValue(value);
SQLIntervalUnit intervalUnit = SQLIntervalUnit.valueOf(unit.toUpperCase());
SQLIntervalUnit intervalUnit = SQLIntervalUnit.of(unit.toUpperCase());
if (intervalUnit == SQLIntervalUnit.YEAR
&& lexer.token() == Token.TO) {
lexer.nextToken();
acceptIdentifier("MONTH");
intervalUnit = SQLIntervalUnit.YEAR_TO_MONTH;
}
if (intervalUnit == SQLIntervalUnit.YEAR && lexer.nextIf(Token.TO)) {
acceptIdentifier(FnvHash.Constants.MONTH);
intervalUnit = SQLIntervalUnit.YEAR_TO_MONTH;
} else if (intervalUnit == SQLIntervalUnit.DAY && lexer.nextIf(Token.TO)) {
acceptIdentifier(FnvHash.Constants.SECOND);
intervalUnit = SQLIntervalUnit.DAY_HOUR;
} else if (intervalUnit == SQLIntervalUnit.HOUR && lexer.nextIf(Token.TO)) {
acceptIdentifier(FnvHash.Constants.SECOND);
intervalUnit = SQLIntervalUnit.HOUR_SECOND;
}
intervalExpr.setUnit(intervalUnit);

return intervalExpr;
Expand Down
32 changes: 0 additions & 32 deletions core/src/main/java/com/alibaba/druid/sql/parser/SQLExprParser.java
Original file line number Diff line number Diff line change
Expand Up @@ -376,38 +376,6 @@ public int parseIntValue() {
}
}

public SQLIntervalUnit parseIntervalUnit() {
if (lexer.token() == Token.IDENTIFIER) {
SQLIntervalUnit unit = SQLIntervalUnit.of(lexer.stringVal());
if (unit != null) {
lexer.nextToken();
}

if (unit == SQLIntervalUnit.YEAR && lexer.nextIf(Token.TO)) {
if (lexer.nextIfIdentifier(FnvHash.Constants.MONTH)) {
unit = SQLIntervalUnit.YEAR_TO_MONTH;
} else {
throw new ParserException("parse interval unit error, " + lexer.info());
}
} else if (unit == SQLIntervalUnit.DAY && lexer.nextIf(Token.TO)) {
if (lexer.nextIfIdentifier(FnvHash.Constants.SECOND)) {
unit = SQLIntervalUnit.DAY_HOUR;
} else {
throw new ParserException("parse interval unit error, " + lexer.info());
}
} else if (unit == SQLIntervalUnit.HOUR && lexer.nextIf(Token.TO)) {
if (lexer.nextIfIdentifier(FnvHash.Constants.SECOND)) {
unit = SQLIntervalUnit.HOUR_SECOND;
} else {
throw new ParserException("parse interval unit error, " + lexer.info());
}
}

return unit;
}
return null;
}

public void parseAssignItems(List<? super SQLAssignItem> items, SQLObject parent, boolean variant) {
for (; ; ) {
SQLAssignItem item = parseAssignItem(variant, parent);
Expand Down
16 changes: 16 additions & 0 deletions core/src/test/resources/bvt/parser/impala/0.txt
Original file line number Diff line number Diff line change
@@ -1,3 +1,19 @@
select cast(concat('20', strleft(m_brd_season_name, 2)) as int) year
, strright(m_brd_season_name, 2) qtr
, m_brd_season_name
, product_no
, managing_provice_no
from dwd_kudu.t15_imp_key_pro
WHERE
m_brd_season_name > '23'
group by m_brd_season_name, product_no, managing_provice_no
--------------------
SELECT CAST(concat('20', strleft(m_brd_season_name, 2)) AS int) AS year, strright(m_brd_season_name, 2) AS qtr, m_brd_season_name
, product_no, managing_provice_no
FROM dwd_kudu.t15_imp_key_pro
WHERE m_brd_season_name > '23'
GROUP BY m_brd_season_name, product_no, managing_provice_no
------------------------------------------------------------------------------------------------------------------------
SELECT a AS A FROM B AS b LEFT JOIN /* +shuffle */ ( select c from D d join E e on d.id=e.id where e.id = 'test' group by 1,2 ) f on b.id=f.id WHERE 1=1 GROUP BY a HAVING 1=1 limit 2000000
--------------------
SELECT a AS A
Expand Down

0 comments on commit 1ae0420

Please sign in to comment.