use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class NameResolveVisitor method isRowNumColumn.
/**
* 是否是 rownum 或者 rownum 别名
*
* @param x x 是否是 rownum 或者 rownum 别名
* @param source 从 source 数据中查找 and 判断
* @return true:是、false:不是
*/
public boolean isRowNumColumn(SQLExpr x, SQLSelectQueryBlock source) {
if (x instanceof SQLIdentifierExpr) {
SQLIdentifierExpr identifierExpr = (SQLIdentifierExpr) x;
long nameHashCode64 = identifierExpr.nameHashCode64();
if (nameHashCode64 == FnvHash.Constants.ROWNUM) {
return true;
}
SQLSelectQueryBlock queryBlock = source;
if (queryBlock.getFrom() instanceof SQLSubqueryTableSource && ((SQLSubqueryTableSource) queryBlock.getFrom()).getSelect().getQuery() instanceof SQLSelectQueryBlock) {
SQLSelectQueryBlock subQueryBlock = ((SQLSubqueryTableSource) queryBlock.getFrom()).getSelect().getQueryBlock();
SQLSelectItem selectItem = subQueryBlock.findSelectItem(nameHashCode64);
if (selectItem != null && isRowNumColumn(selectItem.getExpr(), subQueryBlock)) {
return true;
}
}
}
return false;
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class MySqlSelectTest_203_dla method test_0.
public void test_0() throws Exception {
String sql = "SELECT $1, $2 FROM\n" + "TABLE temp_1\n" + "(\n" + " $1 int,\n" + " $2 string\n" + ")\n" + "TBLPROPERTIES (\n" + " CATALOG='oss',\n" + " LOCATION='oss://oss-cn-hangzhou-for-openanalytics-dailybuild/jinluo/tbl1_part/kv1.txt',\n" + " SCHEMA='jinluo_test0810'\n" + ")\n" + "META LIFECYCLE 1";
// System.out.println(sql);
MySqlStatementParser parser = new MySqlStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLSelectStatement stmt = (SQLSelectStatement) statementList.get(0);
assertEquals(1, statementList.size());
assertEquals("SELECT $1, $2\n" + "FROM TABLE temp_1 (\n" + "\t$1 int,\n" + "\t$2 string\n" + ")\n" + "TBLPROPERTIES (\n" + "\t'CATALOG' = 'oss',\n" + "\t'LOCATION' = 'oss://oss-cn-hangzhou-for-openanalytics-dailybuild/jinluo/tbl1_part/kv1.txt',\n" + "\t'SCHEMA' = 'jinluo_test0810'\n" + ")\n" + "META LIFECYCLE 1", stmt.toString());
assertEquals("select $1, $2\n" + "from table temp_1 (\n" + "\t$1 int,\n" + "\t$2 string\n" + ")\n" + "tblproperties (\n" + "\t'CATALOG' = 'oss',\n" + "\t'LOCATION' = 'oss://oss-cn-hangzhou-for-openanalytics-dailybuild/jinluo/tbl1_part/kv1.txt',\n" + "\t'SCHEMA' = 'jinluo_test0810'\n" + ")\n" + "meta lifecycle 1", stmt.toLowerCaseString());
final TempTableNameGen tempTableNameGen = new TempTableNameGen() {
@Override
public String generateName() {
return "__temp_table_1";
}
};
final List<SQLCreateTableStatement> createTableStatementList = new ArrayList<SQLCreateTableStatement>();
SQLASTVisitorAdapter v = new MySqlASTVisitorAdapter() {
public boolean visit(SQLAdhocTableSource x) {
final String tableName = tempTableNameGen.generateName();
HiveCreateTableStatement createStmt = (HiveCreateTableStatement) x.getDefinition();
createStmt.setParent(null);
createStmt.setTableName(tableName);
createStmt.setExternal(true);
SQLUtils.replaceInParent(x, new SQLExprTableSource(tableName));
createTableStatementList.add(createStmt);
return false;
}
public boolean visit(SQLVariantRefExpr x) {
String name = x.getName();
if (name != null && name.startsWith("$")) {
SQLUtils.replaceInParent(x, new SQLIdentifierExpr(name));
}
return false;
}
};
stmt.accept(v);
for (SQLCreateTableStatement createStmt : createTableStatementList) {
System.out.println(createStmt.toString(VisitorFeature.OutputNameQuote));
}
System.out.println();
System.out.println(stmt.toString(VisitorFeature.OutputNameQuote));
//
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class ClearSchema_0 method test_insert_0.
public void test_insert_0() throws Exception {
String sql = "INSERT INTO testdb.Websites (name, country)\n" + "SELECT app_name, country FROM testdb.apps;";
SQLStatement stmt = SQLUtils.parseStatements(sql, JdbcConstants.MYSQL).get(0);
SQLASTVisitor v = new SQLASTVisitorAdapter() {
@Override
public boolean visit(SQLPropertyExpr x) {
if (SQLUtils.replaceInParent(x, new SQLIdentifierExpr(x.getName()))) {
return false;
}
return super.visit(x);
}
};
stmt.accept(v);
assertEquals("INSERT INTO Websites (name, country)\n" + "SELECT app_name, country\n" + "FROM apps;", stmt.toString());
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class SchemaStatTest21_issue3980 method test_schemaStat.
public void test_schemaStat() throws Exception {
SchemaRepository repository = new SchemaRepository(JdbcConstants.MYSQL);
String sql = "select n.user_name,\n" + "n.user_passwd,\n" + "n.user_project,\n" + "n.start_date,\n" + "n.end_date\n" + "from (\n" + "select t.name as user_name,\n" + "t.passwd as user_passwd,\n" + "cast(from_unixtime(t.from_time, \"yyyyMMdd\") as int) as start_date,\n" + "cast(from_unixtime(t.to_time, \"yyyyMMdd\") as int) as end_date\n" + "from tableA as t\n" + "where t.user_id = 1\n" + "union all\n" + "select p.project as user_project\n" + "from tableB as p\n" + "where p.project_id = 10\n" + ") as n;";
SQLStatementParser parser = SQLParserUtils.createSQLStatementParser(sql, JdbcConstants.MYSQL);
SQLStatement stmt = parser.parseStatementList().get(0);
SchemaStatVisitor statVisitor = SQLUtils.createSchemaStatVisitor(repository);
stmt.accept(statVisitor);
System.out.println("Tables : " + statVisitor.getTables());
System.out.println("columns : " + statVisitor.getColumns());
// group by
System.out.println(statVisitor.getGroupByColumns());
// group by
System.out.println("relationships : " + statVisitor.getRelationships());
System.out.println("conditions : " + statVisitor.getConditions());
System.out.println("functions : " + statVisitor.getFunctions());
assertEquals(7, statVisitor.getColumns().size());
assertEquals(2, statVisitor.getConditions().size());
assertEquals(2, statVisitor.getFunctions().size());
SQLPropertyExpr expr = (SQLPropertyExpr) statVisitor.getFunctions().get(0).getArguments().get(0);
SQLIdentifierExpr tableAlias = (SQLIdentifierExpr) expr.getOwner();
tableAlias.getResolvedTableSource();
}
use of com.alibaba.druid.sql.ast.expr.SQLIdentifierExpr in project druid by alibaba.
the class HiveStatementParser method parseStatementListDialect.
public boolean parseStatementListDialect(List<SQLStatement> statementList) {
if (lexer.token() == Token.HINT) {
List<SQLCommentHint> hints = this.exprParser.parseHints();
boolean tddlHints = false;
boolean accept = false;
boolean acceptHint = false;
switch(lexer.token()) {
case SELECT:
case WITH:
case DELETE:
case UPDATE:
case INSERT:
case SHOW:
case REPLACE:
case TRUNCATE:
case DROP:
case ALTER:
case CREATE:
case CHECK:
case SET:
case DESC:
case OPTIMIZE:
case ANALYZE:
case KILL:
case EXPLAIN:
case LPAREN:
acceptHint = true;
default:
break;
}
if (lexer.identifierEquals("MSCK")) {
acceptHint = true;
}
if (acceptHint) {
SQLStatementImpl stmt = (SQLStatementImpl) this.parseStatement();
stmt.setHeadHints(hints);
statementList.add(stmt);
return true;
}
}
if (lexer.token() == Token.FROM) {
SQLStatement stmt = this.parseInsert();
statementList.add(stmt);
return true;
}
if (lexer.identifierEquals(FnvHash.Constants.LOAD)) {
HiveLoadDataStatement stmt = parseLoad();
statementList.add(stmt);
return true;
}
if (lexer.identifierEquals(FnvHash.Constants.ANALYZE)) {
SQLStatement stmt = parseAnalyze();
statementList.add(stmt);
return true;
}
if (lexer.identifierEquals(FnvHash.Constants.EXPORT)) {
SQLStatement stmt = parseExport();
statementList.add(stmt);
return true;
}
if (lexer.identifierEquals(FnvHash.Constants.IMPORT)) {
SQLStatement stmt = parseImport();
statementList.add(stmt);
return true;
}
if (lexer.identifierEquals("MSCK")) {
SQLStatement stmt = parseMsck();
statementList.add(stmt);
return true;
}
if (lexer.identifierEquals(FnvHash.Constants.SHOW)) {
Lexer.SavePoint savePoint = this.lexer.mark();
lexer.nextToken();
if (lexer.identifierEquals(FnvHash.Constants.VIEWS)) {
lexer.nextToken();
SQLShowViewsStatement stmt = new SQLShowViewsStatement();
if (lexer.token() == Token.IN) {
lexer.nextToken();
SQLName db = this.exprParser.name();
stmt.setDatabase(db);
}
if (lexer.token() == Token.LIKE) {
lexer.nextToken();
SQLExpr pattern = this.exprParser.expr();
stmt.setLike(pattern);
}
statementList.add(stmt);
return true;
}
if (lexer.identifierEquals(FnvHash.Constants.TABLES)) {
lexer.reset(savePoint);
SQLStatement stmt = this.parseShowTables();
statementList.add(stmt);
return true;
}
if (lexer.identifierEquals(FnvHash.Constants.DATABASES)) {
lexer.nextToken();
SQLShowDatabasesStatement stmt = parseShowDatabases(false);
statementList.add(stmt);
return true;
}
if (lexer.token() == Token.INDEX) {
lexer.nextToken();
SQLShowIndexesStatement stmt = new SQLShowIndexesStatement();
stmt.setType("INDEX");
if (lexer.token() == Token.ON) {
lexer.nextToken();
SQLName table = exprParser.name();
stmt.setTable(table);
}
if (lexer.token() == Token.HINT) {
stmt.setHints(this.exprParser.parseHints());
}
statementList.add(stmt);
return true;
}
if (lexer.token() == Token.CREATE) {
SQLShowCreateTableStatement stmt = parseShowCreateTable();
statementList.add(stmt);
return true;
}
if (lexer.identifierEquals(FnvHash.Constants.PARTITIONS)) {
lexer.nextToken();
SQLShowPartitionsStmt stmt = new SQLShowPartitionsStmt();
if (lexer.token() == Token.FROM) {
lexer.nextToken();
}
SQLExpr expr = this.exprParser.expr();
stmt.setTableSource(new SQLExprTableSource(expr));
if (lexer.token() == Token.PARTITION) {
lexer.nextToken();
accept(Token.LPAREN);
parseAssignItems(stmt.getPartition(), stmt, false);
accept(Token.RPAREN);
}
if (lexer.token() == Token.WHERE) {
lexer.nextToken();
stmt.setWhere(this.exprParser.expr());
}
statementList.add(stmt);
return true;
}
if (lexer.identifierEquals(FnvHash.Constants.COLUMNS)) {
lexer.nextToken();
SQLShowColumnsStatement stmt = new SQLShowColumnsStatement();
if (lexer.token() == Token.FROM || lexer.token() == Token.IN) {
lexer.nextToken();
SQLName table = exprParser.name();
if (lexer.token() == Token.SUB && table instanceof SQLIdentifierExpr) {
lexer.mark();
lexer.nextToken();
String strVal = lexer.stringVal();
lexer.nextToken();
if (table instanceof SQLIdentifierExpr) {
SQLIdentifierExpr ident = (SQLIdentifierExpr) table;
table = new SQLIdentifierExpr(ident.getName() + "-" + strVal);
}
}
stmt.setTable(table);
}
if (lexer.token() == Token.LIKE) {
lexer.nextToken();
SQLExpr like = exprParser.expr();
stmt.setLike(like);
}
if (lexer.token() == Token.WHERE) {
lexer.nextToken();
SQLExpr where = exprParser.expr();
stmt.setWhere(where);
}
statementList.add(stmt);
return true;
}
throw new ParserException("TODO " + lexer.info());
}
return false;
}
Aggregations