remove sql parser hive support

This commit is contained in:
wenshao
2012-11-10 21:13:14 +08:00
parent 799fe95703
commit 9caa2ccb39
23 changed files with 1 additions and 1121 deletions
@@ -1,24 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.ast;
import com.alibaba.druid.sql.ast.SQLObject;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveASTVisitor;
public interface HiveSQLObject extends SQLObject {
void accept0(HiveASTVisitor visitor);
}
@@ -1,36 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.ast;
import com.alibaba.druid.sql.ast.SQLObjectImpl;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public abstract class HiveSQLObjectImpl extends SQLObjectImpl implements HiveSQLObject {
private static final long serialVersionUID = 1L;
@Override
public void accept0(SQLASTVisitor visitor) {
if (visitor instanceof HiveASTVisitor) {
accept0((HiveASTVisitor) visitor);
} else {
throw new IllegalArgumentException("not support visitor type : " + visitor.getClass().getName());
}
}
public abstract void accept0(HiveASTVisitor visitor);
}
@@ -1,22 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.ast;
import com.alibaba.druid.sql.ast.SQLStatement;
public interface HiveStatement extends SQLStatement, HiveSQLObject {
}
@@ -1,22 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.ast;
public abstract class HiveStatementImpl extends HiveSQLObjectImpl implements HiveStatement {
private static final long serialVersionUID = 1L;
}
@@ -1,86 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.ast.stmt;
import com.alibaba.druid.sql.ast.SQLDataType;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.hive.ast.HiveSQLObjectImpl;
import com.alibaba.druid.sql.dialect.hive.ast.HiveStatement;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveASTVisitor;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public class HiveCreateTableStatement extends SQLCreateTableStatement implements HiveStatement {
private static final long serialVersionUID = 1L;
private PartitionedBy partitionedBy;
public PartitionedBy getPartitionedBy() {
return partitionedBy;
}
public void setPartitionedBy(PartitionedBy partitionedBy) {
this.partitionedBy = partitionedBy;
}
@Override
protected void accept0(SQLASTVisitor visitor) {
this.accept0((HiveASTVisitor) visitor);
}
@Override
public void accept0(HiveASTVisitor visitor) {
if (visitor.visit(this)) {
this.acceptChild(visitor, tableSource);
this.acceptChild(visitor, tableElementList);
this.acceptChild(visitor, partitionedBy);
}
visitor.endVisit(this);
}
public static class PartitionedBy extends HiveSQLObjectImpl {
private static final long serialVersionUID = 1L;
private String name;
private SQLDataType type;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public SQLDataType getType() {
return type;
}
public void setType(SQLDataType type) {
this.type = type;
}
@Override
public void accept0(HiveASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, type);
}
visitor.endVisit(this);
}
}
}
@@ -1,44 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.ast.stmt;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.dialect.hive.ast.HiveStatementImpl;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveASTVisitor;
public class HiveShowTablesStatement extends HiveStatementImpl {
private static final long serialVersionUID = 1L;
private SQLCharExpr pattern;
public SQLCharExpr getPattern() {
return pattern;
}
public void setPattern(SQLCharExpr pattern) {
this.pattern = pattern;
}
@Override
public void accept0(HiveASTVisitor visitor) {
if (visitor.visit(this)) {
acceptChild(visitor, pattern);
}
visitor.endVisit(this);
}
}
@@ -1,58 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.parser;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement;
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement.PartitionedBy;
import com.alibaba.druid.sql.parser.SQLCreateTableParser;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.alibaba.druid.sql.parser.Token;
public class HiveCreateTableParser extends SQLCreateTableParser {
public HiveCreateTableParser(String sql){
super(new SQLExprParser(sql));
}
public HiveCreateTableParser(SQLExprParser exprParser){
super(exprParser);
}
protected HiveCreateTableStatement newCreateStatement() {
return new HiveCreateTableStatement();
}
public SQLCreateTableStatement parseCrateTable(boolean acceptCreate) {
HiveCreateTableStatement stmt = (HiveCreateTableStatement) super.parseCrateTable(acceptCreate);
if (identifierEquals("PARTITIONED")) {
lexer.nextToken();
accept(Token.BY);
accept(Token.LPAREN);
PartitionedBy partitionedBy = new PartitionedBy();
partitionedBy.setName(exprParser.name().toString());
partitionedBy.setType(exprParser.parseDataType());
accept(Token.RPAREN);
stmt.setPartitionedBy(partitionedBy);
}
return stmt;
}
}
@@ -1,31 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.parser;
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.SQLExprParser;
public class HiveExprParser extends SQLExprParser {
public HiveExprParser(String sql) {
super(new HiveLexer(sql));
this.lexer.nextToken();
}
public HiveExprParser(Lexer lexer){
super(lexer);
}
}
@@ -1,140 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.parser;
import java.util.HashMap;
import java.util.Map;
import com.alibaba.druid.sql.parser.Keywords;
import com.alibaba.druid.sql.parser.Lexer;
import com.alibaba.druid.sql.parser.NotAllowCommentException;
import com.alibaba.druid.sql.parser.Token;
public class HiveLexer extends Lexer {
public final static Keywords DEFAULT_HIVE_KEYWORDS;
static {
Map<String, Token> map = new HashMap<String, Token>();
map.putAll(Keywords.DEFAULT_KEYWORDS.getKeywords());
map.put("WITH", Token.WITH);
DEFAULT_HIVE_KEYWORDS = new Keywords(map);
}
public HiveLexer(char[] input, int inputLength, boolean skipComment){
super(input, inputLength, skipComment);
super.keywods = DEFAULT_HIVE_KEYWORDS;
}
public HiveLexer(String input){
super(input);
super.keywods = DEFAULT_HIVE_KEYWORDS;
}
public void scanComment() {
if (ch != '/') {
throw new IllegalStateException();
}
mark = pos;
bufPos = 0;
scanChar();
bufPos++;
// /*+ */
if (ch == '*') {
scanChar();
bufPos++;
while (ch == ' ') {
scanChar();
bufPos++;
}
boolean isHint = false;
int startHintSp = bufPos + 1;
if (ch == '+') {
isHint = true;
scanChar();
bufPos++;
}
for (;;) {
if (ch == '*' && charAt(pos + 1) == '/') {
bufPos += 2;
scanChar();
scanChar();
break;
}
scanChar();
bufPos++;
}
if (isHint) {
stringVal = subString(mark + startHintSp, (bufPos - startHintSp) - 2).trim();
token = Token.HINT;
} else {
stringVal = subString(mark, bufPos);
token = Token.MULTI_LINE_COMMENT;
}
if (token != Token.HINT && !isAllowComment()) {
throw new NotAllowCommentException();
}
return;
}
if (!isAllowComment()) {
throw new NotAllowCommentException();
}
if (ch == '/') {
scanChar();
bufPos++;
for (;;) {
if (ch == '\r') {
if (charAt(pos + 1) == '\n') {
bufPos += 2;
scanChar();
break;
}
bufPos++;
break;
}
if (ch == '\r') {
scanChar();
bufPos++;
break;
}
scanChar();
bufPos++;
}
stringVal = subString(mark + 1, bufPos);
token = Token.LINE_COMMENT;
return;
}
}
}
@@ -1,57 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.parser;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.ast.expr.SQLCharExpr;
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveShowTablesStatement;
import com.alibaba.druid.sql.parser.ParserException;
import com.alibaba.druid.sql.parser.SQLExprParser;
import com.alibaba.druid.sql.parser.SQLStatementParser;
import com.alibaba.druid.sql.parser.Token;
public class HiveStatementParser extends SQLStatementParser {
public HiveStatementParser(SQLExprParser exprParser){
super(exprParser);
}
public HiveStatementParser(String sql){
super(new SQLExprParser(sql));
}
public HiveCreateTableParser getSQLCreateTableParser() {
return new HiveCreateTableParser(this.exprParser);
}
public SQLStatement parseShow() {
acceptIdentifier("SHOW");
if (identifierEquals("TABLES")) {
lexer.nextToken();
HiveShowTablesStatement stmt = new HiveShowTablesStatement();
if (lexer.token() == Token.LITERAL_CHARS) {
stmt.setPattern((SQLCharExpr) exprParser.primary());
}
return stmt;
}
throw new ParserException("TODO " + lexer.info());
}
}
@@ -1,35 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.visitor;
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement;
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveShowTablesStatement;
import com.alibaba.druid.sql.visitor.SQLASTVisitor;
public interface HiveASTVisitor extends SQLASTVisitor {
void endVisit(HiveShowTablesStatement x);
boolean visit(HiveShowTablesStatement x);
void endVisit(HiveCreateTableStatement x);
boolean visit(HiveCreateTableStatement x);
void endVisit(HiveCreateTableStatement.PartitionedBy x);
boolean visit(HiveCreateTableStatement.PartitionedBy x);
}
@@ -1,54 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.visitor;
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement;
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveShowTablesStatement;
import com.alibaba.druid.sql.visitor.SQLASTVisitorAdapter;
public class HiveASTVisitorAdapter extends SQLASTVisitorAdapter implements HiveASTVisitor {
@Override
public void endVisit(HiveCreateTableStatement x) {
}
@Override
public boolean visit(HiveCreateTableStatement x) {
return true;
}
@Override
public void endVisit(HiveCreateTableStatement.PartitionedBy x) {
}
@Override
public boolean visit(HiveCreateTableStatement.PartitionedBy x) {
return true;
}
@Override
public void endVisit(HiveShowTablesStatement x) {
}
@Override
public boolean visit(HiveShowTablesStatement x) {
return true;
}
}
@@ -1,77 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.visitor;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement;
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement.PartitionedBy;
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveShowTablesStatement;
import com.alibaba.druid.sql.visitor.SQLASTOutputVisitor;
public class HiveOutputVisitor extends SQLASTOutputVisitor implements HiveASTVisitor {
public HiveOutputVisitor(Appendable appender){
super(appender);
}
@Override
public void endVisit(HiveCreateTableStatement x) {
}
@Override
public boolean visit(HiveCreateTableStatement x) {
visit((SQLCreateTableStatement) x);
if (x.getPartitionedBy() != null) {
println();
x.getPartitionedBy().accept(this);
}
return false;
}
@Override
public void endVisit(PartitionedBy x) {
}
@Override
public boolean visit(PartitionedBy x) {
print("PARTITIONED BY (");
print(x.getName());
print(" ");
x.getType().accept(this);
print(")");
return false;
}
@Override
public void endVisit(HiveShowTablesStatement x) {
}
@Override
public boolean visit(HiveShowTablesStatement x) {
print("SHOW TABLES");
if (x.getPattern() != null) {
print(" ");
x.getPattern().accept(this);
}
return false;
}
}
@@ -1,60 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.dialect.hive.visitor;
import com.alibaba.druid.sql.ast.statement.SQLCreateTableStatement;
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveCreateTableStatement;
import com.alibaba.druid.sql.dialect.hive.ast.stmt.HiveShowTablesStatement;
import com.alibaba.druid.sql.visitor.SchemaStatVisitor;
import com.alibaba.druid.util.JdbcUtils;
public class HiveSchemaStatVisitor extends SchemaStatVisitor implements HiveASTVisitor {
@Override
public String getDbType() {
return JdbcUtils.HIVE;
}
@Override
public void endVisit(HiveCreateTableStatement x) {
}
@Override
public boolean visit(HiveCreateTableStatement x) {
return visit((SQLCreateTableStatement) x);
}
@Override
public void endVisit(HiveCreateTableStatement.PartitionedBy x) {
}
@Override
public boolean visit(HiveCreateTableStatement.PartitionedBy x) {
return false;
}
@Override
public void endVisit(HiveShowTablesStatement x) {
}
@Override
public boolean visit(HiveShowTablesStatement x) {
return false;
}
}
@@ -61,8 +61,6 @@ public class MySqlLexer extends Lexer {
}
public void scanVariable() {
final char first = ch;
if (ch != '@' && ch != ':' && ch != '#' && ch != '$') {
throw new SQLParseException("illegal variable");
}
@@ -822,7 +822,7 @@ public class Lexer {
* translation)
*/
public final int pos() {
return 0;
return pos;
}
/**
@@ -1,55 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.hive;
import java.util.List;
import junit.framework.Assert;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.hive.parser.HiveStatementParser;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveSchemaStatVisitor;
import com.alibaba.druid.sql.hive.HiveTest;
import com.alibaba.druid.stat.TableStat;
public class HiveCreateTableTest extends HiveTest {
public void test_hive() throws Exception {
String sql = "CREATE TABLE pokes (foo INT, bar STRING);";
HiveStatementParser parser = new HiveStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
HiveSchemaStatVisitor visitor = new HiveSchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("pokes")));
Assert.assertEquals(2, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pokes", "foo")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("pokes", "bar")));
}
}
@@ -1,55 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.hive;
import java.util.List;
import junit.framework.Assert;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.hive.parser.HiveStatementParser;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveSchemaStatVisitor;
import com.alibaba.druid.sql.hive.HiveTest;
import com.alibaba.druid.stat.TableStat;
public class HiveCreateTableTest2 extends HiveTest {
public void test_hive() throws Exception {
String sql = "CREATE TABLE invites (foo INT, bar STRING) PARTITIONED BY (ds STRING);";
HiveStatementParser parser = new HiveStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
HiveSchemaStatVisitor visitor = new HiveSchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("invites")));
Assert.assertEquals(2, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("invites", "foo")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("invites", "bar")));
}
}
@@ -1,53 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.hive;
import java.util.List;
import junit.framework.Assert;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.hive.parser.HiveStatementParser;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveSchemaStatVisitor;
import com.alibaba.druid.sql.hive.HiveTest;
import com.alibaba.druid.stat.TableStat;
public class HiveSelectTest extends HiveTest {
public void test_hive() throws Exception {
String sql = "SELECT COUNT(*) FROM u_data;";
HiveStatementParser parser = new HiveStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
HiveSchemaStatVisitor visitor = new HiveSchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("u_data")));
Assert.assertEquals(1, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("u_data", "*")));
}
}
@@ -1,55 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.hive;
import java.util.List;
import junit.framework.Assert;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.hive.parser.HiveStatementParser;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveSchemaStatVisitor;
import com.alibaba.druid.sql.hive.HiveTest;
import com.alibaba.druid.stat.TableStat;
public class HiveSelectTest2 extends HiveTest {
public void test_hive() throws Exception {
String sql = "SELECT a.foo FROM invites a WHERE a.ds='2008-08-15';";
HiveStatementParser parser = new HiveStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
HiveSchemaStatVisitor visitor = new HiveSchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("invites")));
Assert.assertEquals(2, visitor.getColumns().size());
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("invites", "foo")));
Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("invites", "ds")));
}
}
@@ -1,54 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.hive;
import java.util.List;
import junit.framework.Assert;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.hive.parser.HiveStatementParser;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveSchemaStatVisitor;
import com.alibaba.druid.sql.hive.HiveTest;
public class HiveShowTableTest extends HiveTest {
public void test_hive() throws Exception {
String sql = "SHOW TABLES";
HiveStatementParser parser = new HiveStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
HiveSchemaStatVisitor visitor = new HiveSchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("invites")));
Assert.assertEquals(0, visitor.getColumns().size());
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("invites", "foo")));
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("invites", "bar")));
}
}
@@ -1,54 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.bvt.sql.hive;
import java.util.List;
import junit.framework.Assert;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.hive.parser.HiveStatementParser;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveSchemaStatVisitor;
import com.alibaba.druid.sql.hive.HiveTest;
public class HiveShowTableTest2 extends HiveTest {
public void test_hive() throws Exception {
String sql = "SHOW TABLES '.*s';";
HiveStatementParser parser = new HiveStatementParser(sql);
List<SQLStatement> statementList = parser.parseStatementList();
SQLStatement statemen = statementList.get(0);
print(statementList);
Assert.assertEquals(1, statementList.size());
HiveSchemaStatVisitor visitor = new HiveSchemaStatVisitor();
statemen.accept(visitor);
System.out.println("Tables : " + visitor.getTables());
System.out.println("fields : " + visitor.getColumns());
System.out.println("coditions : " + visitor.getConditions());
System.out.println("relationships : " + visitor.getRelationships());
// Assert.assertTrue(visitor.getTables().containsKey(new TableStat.Name("invites")));
Assert.assertEquals(0, visitor.getColumns().size());
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("invites", "foo")));
// Assert.assertTrue(visitor.getColumns().contains(new TableStat.Column("invites", "bar")));
}
}
@@ -1,46 +0,0 @@
/*
* Copyright 1999-2011 Alibaba Group Holding Ltd.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.alibaba.druid.sql.hive;
import java.util.List;
import junit.framework.TestCase;
import com.alibaba.druid.sql.ast.SQLStatement;
import com.alibaba.druid.sql.dialect.hive.visitor.HiveOutputVisitor;
public class HiveTest extends TestCase {
protected String output(List<SQLStatement> stmtList) {
StringBuilder out = new StringBuilder();
HiveOutputVisitor visitor = new HiveOutputVisitor(out);
for (SQLStatement stmt : stmtList) {
stmt.accept(visitor);
}
return out.toString();
}
protected void print(List<SQLStatement> stmtList) {
String text = output(stmtList);
String outputProperty = System.getProperty("druid.output");
if ("false".equals(outputProperty)) {
return;
}
System.out.println(text);
}
}