Skip to content

Commit 7f4bf8c

Browse files
Gladys-111wenshao
authored andcommitted
Fix incorrect identifier source location in ODPS parser.
1 parent 43b68cd commit 7f4bf8c

File tree

3 files changed

+55
-1
lines changed

3 files changed

+55
-1
lines changed

core/src/main/java/com/alibaba/druid/sql/dialect/odps/parser/OdpsExprParser.java

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -818,4 +818,21 @@ protected SQLExpr methodRest(SQLExpr expr, boolean acceptLPAREN) {
818818
}
819819
return super.methodRest(expr, acceptLPAREN);
820820
}
821+
822+
@Override
823+
public SQLName name() {
824+
SQLObject locationHolder = null;
825+
if (lexer.isKeepSourceLocation()) {
826+
locationHolder = new SQLIdentifierExpr("temp");
827+
lexer.computeRowAndColumn(locationHolder);
828+
}
829+
830+
SQLName name = super.name();
831+
832+
if (locationHolder != null) {
833+
name.setSource(locationHolder.getSourceLine(), locationHolder.getSourceColumn());
834+
}
835+
836+
return name;
837+
}
821838
}
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package com.alibaba.druid.bvt.sql.odps;
2+
3+
import com.alibaba.druid.DbType;
4+
import com.alibaba.druid.sql.dialect.odps.ast.OdpsCreateTableStatement;
5+
import com.alibaba.druid.sql.parser.SQLParserFeature;
6+
import com.alibaba.druid.sql.parser.SQLParserUtils;
7+
import com.alibaba.druid.sql.parser.SQLStatementParser;
8+
import junit.framework.TestCase;
9+
10+
public class OdpsIdentifierLocationTest extends TestCase {
11+
public void test_0() throws Exception {
12+
String sql = "--odps sql\n" +
13+
"--********************************************************************--\n" +
14+
"--author:dw_on_emr_qa3_testcloud_com\n" +
15+
"--create time:2025-08-11 17:08:41\n" +
16+
"--********************************************************************--\n" +
17+
"\n" +
18+
"CREATE TABLE IF NOT EXISTS partition_table1\n" +
19+
"(\n" +
20+
" a STRING COMMENT 'FIELD'\n" +
21+
" ,b STRING COMMENT 'FIELD'\n" +
22+
")\n" +
23+
"COMMENT 'TABLE COMMENT'\n" +
24+
"PARTITIONED BY (ds STRING COMMENT '分区')\n" +
25+
"LIFECYCLE 70;";
26+
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(
27+
sql,
28+
DbType.odps,
29+
SQLParserFeature.KeepSourceLocation,
30+
SQLParserFeature.KeepComments);
31+
OdpsCreateTableStatement sqlCreateTableStatement = (OdpsCreateTableStatement) parser.parseStatement();
32+
int column = sqlCreateTableStatement.getTableSource().getExpr().getSourceColumn();
33+
int line = sqlCreateTableStatement.getTableSource().getExpr().getSourceLine();
34+
assertEquals(column, 28);
35+
assertEquals(line, 7);
36+
}
37+
}

core/src/test/resources/bvt/parser/odps/0.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,4 @@ WITH t AS (
7676
FROM a
7777
)
7878
SELECT abc
79-
FROM t
79+
FROM t

0 commit comments

Comments
 (0)