mirror of
https://github.com/alibaba/druid.git
synced 2023-03-04 19:17:16 +00:00
improved hive sql parser. for issue #2180
This commit is contained in:
@@ -0,0 +1,41 @@
|
||||
package com.alibaba.druid.bvt.sql.hive;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class HiveInsert_0 extends TestCase {
|
||||
public void test_select() throws Exception {
|
||||
String sql = "INSERT INTO TABLE students\n" +
|
||||
" VALUES ('fred flintstone', 35, 1.28), ('barney rubble', 32, 2.32);";//
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.HIVE);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.HIVE);
|
||||
stmt.accept(visitor);
|
||||
|
||||
assertEquals("INSERT INTO TABLE students\n" +
|
||||
"VALUES ('fred flintstone', 35, 1.28), ('barney rubble', 32, 2.32);", SQLUtils.formatHive(sql));
|
||||
|
||||
assertEquals("insert into table students\n" +
|
||||
"values ('fred flintstone', 35, 1.28), ('barney rubble', 32, 2.32);", SQLUtils.formatHive(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
// System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
// System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
assertEquals(1, visitor.getTables().size());
|
||||
assertEquals(0, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,46 @@
|
||||
package com.alibaba.druid.bvt.sql.hive;
|
||||
|
||||
import com.alibaba.druid.sql.SQLUtils;
|
||||
import com.alibaba.druid.sql.ast.SQLStatement;
|
||||
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
|
||||
import com.alibaba.druid.util.JdbcConstants;
|
||||
import junit.framework.TestCase;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
public class HiveInsert_1 extends TestCase {
|
||||
public void test_select() throws Exception {
|
||||
String sql = "FROM page_view_stg pvs\n" +
|
||||
"INSERT OVERWRITE TABLE page_view PARTITION(dt='2008-06-08', country)\n" +
|
||||
" SELECT pvs.viewTime, pvs.userid, pvs.page_url, pvs.referrer_url, null, null, pvs.ip, pvs.cnt";//
|
||||
|
||||
List<SQLStatement> statementList = SQLUtils.parseStatements(sql, JdbcConstants.HIVE);
|
||||
SQLStatement stmt = statementList.get(0);
|
||||
|
||||
assertEquals(1, statementList.size());
|
||||
|
||||
SchemaStatVisitor visitor = SQLUtils.createSchemaStatVisitor(JdbcConstants.HIVE);
|
||||
stmt.accept(visitor);
|
||||
|
||||
assertEquals("FROM page_view_stg pvs\n" +
|
||||
"INSERT OVERWRITE TABLE page_view PARTITION (dt='2008-06-08', country)\n" +
|
||||
"SELECT pvs.viewTime, pvs.userid, pvs.page_url, pvs.referrer_url, NULL\n" +
|
||||
"\t, NULL, pvs.ip, pvs.cnt", SQLUtils.formatHive(sql));
|
||||
|
||||
assertEquals("from page_view_stg pvs\n" +
|
||||
"insert overwrite table page_view partition (dt='2008-06-08', country)\n" +
|
||||
"select pvs.viewTime, pvs.userid, pvs.page_url, pvs.referrer_url, null\n" +
|
||||
"\t, null, pvs.ip, pvs.cnt", SQLUtils.formatHive(sql, SQLUtils.DEFAULT_LCASE_FORMAT_OPTION));
|
||||
|
||||
|
||||
// System.out.println("Tables : " + visitor.getTables());
|
||||
// System.out.println("fields : " + visitor.getColumns());
|
||||
// System.out.println("coditions : " + visitor.getConditions());
|
||||
// System.out.println("orderBy : " + visitor.getOrderByColumns());
|
||||
|
||||
assertEquals(2, visitor.getTables().size());
|
||||
assertEquals(8, visitor.getColumns().size());
|
||||
assertEquals(0, visitor.getConditions().size());
|
||||
|
||||
}
|
||||
}
|
||||
@@ -25,6 +25,7 @@ public class AntlrMySqlTest extends TestCase {
|
||||
for (File file : dir.listFiles()) {
|
||||
System.out.println(file);
|
||||
String sql = FileUtils.readFileToString(file);
|
||||
|
||||
List<SQLStatement> stmtList = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL);
|
||||
for (SQLStatement stmt : stmtList) {
|
||||
stmt.toString();
|
||||
|
||||
+1
-1
@@ -23,7 +23,7 @@ public class OracleJoinResolveTest_2_join extends TestCase {
|
||||
public void test_for_issue() throws Exception {
|
||||
|
||||
String sql = "select a.* from t_user a inner join t_group b where a.uid = id";
|
||||
assertEquals("SELECT a.uid, a.gid, a.name, b.id, b.name\n" +
|
||||
assertEquals("SELECT a.uid, a.gid, a.name, b.id, a.name\n" +
|
||||
"FROM t_user a\n" +
|
||||
"\tINNER JOIN t_group b\n" +
|
||||
"WHERE a.uid = b.id"
|
||||
|
||||
Reference in New Issue
Block a user