diff --git ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g index bf77d1c..26e90b3 100644 --- ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g +++ ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g @@ -330,6 +330,9 @@ TOK_RESOURCE_LIST; TOK_COMPACT; TOK_SHOW_COMPACTIONS; TOK_SHOW_TRANSACTIONS; +TOK_DELETE_FROM; +TOK_UPDATE_TABLE; +TOK_SET_COLUMNS_CLAUSE; } @@ -468,6 +471,7 @@ import java.util.HashMap; xlateMap.put("KW_DEFINED", "DEFINED"); xlateMap.put("KW_SUBQUERY", "SUBQUERY"); xlateMap.put("KW_REWRITE", "REWRITE"); + xlateMap.put("KW_UPDATE", "UPDATE"); // Operators xlateMap.put("DOT", "."); @@ -637,6 +641,8 @@ execStatement | exportStatement | importStatement | ddlStatement + | deleteStatement + | updateStatement ; loadStatement @@ -2190,3 +2196,34 @@ limitClause : KW_LIMIT num=Number -> ^(TOK_LIMIT $num) ; + +//DELETE FROM WHERE ...; +deleteStatement +@init { pushMsg("delete statement", state); } +@after { popMsg(state); } + : + KW_DELETE KW_FROM tableName (whereClause)? -> ^(TOK_DELETE_FROM tableName whereClause?) + ; + +/*SET = (3 + col2)*/ +columnAssignmentClause + : + tableOrColumn EQUAL^ atomExpression + ; + +/*SET col1 = 5, col2 = (4 + col4), ...*/ +setColumnsClause + : + KW_SET columnAssignmentClause (COMMA columnAssignmentClause)* -> ^(TOK_SET_COLUMNS_CLAUSE columnAssignmentClause* ) + ; + +/* + UPDATE + SET col1 = val1, col2 = val2... WHERE ... +*/ +updateStatement +@init { pushMsg("update statement", state); } +@after { popMsg(state); } + : + KW_UPDATE tableName setColumnsClause whereClause? -> ^(TOK_UPDATE_TABLE tableName setColumnsClause whereClause?) + ; diff --git ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java index 3fcd54b..746ca1d 100644 --- ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java +++ ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java @@ -1164,6 +1164,10 @@ public boolean doPhase1(ASTNode ast, QB qb, Phase1Ctx ctx_1) case HiveParser.TOK_CTE: processCTE(qb, ast); break; + case HiveParser.TOK_DELETE_FROM: + throw new RuntimeException("DELETE is not (yet) implemented..."); + case HiveParser.TOK_UPDATE_TABLE: + throw new RuntimeException("UPDATE is not (yet) implemented..."); default: skipRecursion = false; break; diff --git ql/src/java/org/apache/hadoop/hive/ql/processors/HiveCommand.java ql/src/java/org/apache/hadoop/hive/ql/processors/HiveCommand.java index 4a6dc61..f5bc427 100644 --- ql/src/java/org/apache/hadoop/hive/ql/processors/HiveCommand.java +++ ql/src/java/org/apache/hadoop/hive/ql/processors/HiveCommand.java @@ -49,6 +49,9 @@ public static HiveCommand find(String[] command) { if (command.length > 1 && "role".equalsIgnoreCase(command[1])) { // special handling for set role r1 statement return null; + } else if(command.length > 1 && "from".equalsIgnoreCase(command[1])) { + //special handling for SQL "delete from
where..." + return null; } else if (COMMANDS.contains(cmd)) { return HiveCommand.valueOf(cmd); } diff --git ql/src/test/org/apache/hadoop/hive/ql/parse/TestIUD.java ql/src/test/org/apache/hadoop/hive/ql/parse/TestIUD.java new file mode 100644 index 0000000..d1e536d --- /dev/null +++ ql/src/test/org/apache/hadoop/hive/ql/parse/TestIUD.java @@ -0,0 +1,118 @@ +/** + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you 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 org.apache.hadoop.hive.ql.parse; + +import org.apache.hadoop.hive.conf.HiveConf; +import org.apache.hadoop.hive.ql.session.SessionState; +import org.junit.Assert; +import org.junit.Before; +import org.junit.BeforeClass; +import org.junit.Test; + +public class TestIUD { + private static HiveConf conf; + + private ParseDriver pd; + private SemanticAnalyzer sA; + + @BeforeClass + public static void initialize() { + conf = new HiveConf(SemanticAnalyzer.class); + SessionState.start(conf); + } + + @Before + public void setup() throws SemanticException { + pd = new ParseDriver(); + sA = new SemanticAnalyzer(conf); + } + + ASTNode parse(String query) throws ParseException { + ASTNode nd = pd.parse(query); + return (ASTNode) nd.getChild(0); + } + @Test + public void testDeleteNoWhere() throws ParseException { + ASTNode ast = parse("DELETE FROM src"); + Assert.assertEquals("AST doesn't match", + "(TOK_DELETE_FROM " + + "(TOK_TABNAME src))", ast.toStringTree()); + } + @Test + public void testDeleteWithWhere() throws ParseException { + ASTNode ast = parse("DELETE FROM src WHERE key IS NOT NULL AND src.value < 0"); + Assert.assertEquals("AST doesn't match", + "(TOK_DELETE_FROM " + + "(TOK_TABNAME src) " + + "(TOK_WHERE " + + "(AND " + + "(TOK_FUNCTION TOK_ISNOTNULL (TOK_TABLE_OR_COL key)) " + + "(< (. (TOK_TABLE_OR_COL src) value) 0))))", + ast.toStringTree()); + } + @Test + public void testUpdateNoWhereSingleSet() throws ParseException { + ASTNode ast = parse("UPDATE src set key = 3"); + Assert.assertEquals("AST doesn't match", + "(TOK_UPDATE_TABLE " + + "(TOK_TABNAME src) " + + "(TOK_SET_COLUMNS_CLAUSE " + + "(= " + + "(TOK_TABLE_OR_COL key) 3)))", + ast.toStringTree()); + } + @Test + public void testUpdateNoWhereMultiSet() throws ParseException { + ASTNode ast = parse("UPDATE src set key = 3, value = 8"); + Assert.assertEquals("AST doesn't match", + "(TOK_UPDATE_TABLE " + + "(TOK_TABNAME src) " + + "(TOK_SET_COLUMNS_CLAUSE " + + "(= " + + "(TOK_TABLE_OR_COL key) 3) " + + "(= " + + "(TOK_TABLE_OR_COL value) 8)))", + ast.toStringTree()); + } + @Test + public void testUpdateWithWhereSingleSet() throws ParseException { + ASTNode ast = parse("UPDATE src SET key = 3 WHERE value IS NULL"); + Assert.assertEquals("AST doesn't match", + "(TOK_UPDATE_TABLE " + + "(TOK_TABNAME src) " + + "(TOK_SET_COLUMNS_CLAUSE " + + "(= " + + "(TOK_TABLE_OR_COL key) 3)) " + + "(TOK_WHERE (TOK_FUNCTION TOK_ISNULL (TOK_TABLE_OR_COL value))))", + ast.toStringTree()); + } + @Test + public void testUpdateWithWhereMultiSet() throws ParseException { + ASTNode ast = parse("UPDATE src SET key = 3, value = 8 WHERE VALUE = 1230997"); + Assert.assertEquals("AST doesn't match", + "(TOK_UPDATE_TABLE " + + "(TOK_TABNAME src) " + + "(TOK_SET_COLUMNS_CLAUSE " + + "(= " + + "(TOK_TABLE_OR_COL key) 3) " + + "(= " + + "(TOK_TABLE_OR_COL value) 8)) " + + "(TOK_WHERE (= (TOK_TABLE_OR_COL VALUE) 1230997)))", + ast.toStringTree()); + } +}