[ 
https://issues.apache.org/jira/browse/HIVE-21884?focusedWorklogId=324728&page=com.atlassian.jira.plugin.system.issuetabpanels:worklog-tabpanel#worklog-324728
 ]

ASF GitHub Bot logged work on HIVE-21884:
-----------------------------------------

                Author: ASF GitHub Bot
            Created on: 07/Oct/19 23:46
            Start Date: 07/Oct/19 23:46
    Worklog Time Spent: 10m 
      Work Description: jcamachor commented on pull request #794: HIVE-21884
URL: https://github.com/apache/hive/pull/794#discussion_r332255167
 
 

 ##########
 File path: 
ql/src/java/org/apache/hadoop/hive/ql/parse/ScheduledQueryAnalyzer.java
 ##########
 @@ -0,0 +1,183 @@
+/*
+ * 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 static org.apache.hadoop.hive.ql.parse.HiveParser.TOK_IFEXISTS;
+
+import java.util.ArrayList;
+import java.util.Collections;
+import java.util.HashSet;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Set;
+import java.util.Stack;
+
+import org.antlr.runtime.tree.Tree;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
+import org.apache.hadoop.hive.metastore.Warehouse;
+import org.apache.hadoop.hive.metastore.api.Database;
+import org.apache.hadoop.hive.metastore.api.FieldSchema;
+import org.apache.hadoop.hive.metastore.api.ScheduledQuery;
+import org.apache.hadoop.hive.metastore.api.ScheduledQuery._Fields;
+import org.apache.hadoop.hive.metastore.api.ScheduledQueryKey;
+import 
org.apache.hadoop.hive.metastore.api.ScheduledQueryMaintenanceRequestType;
+import org.apache.hadoop.hive.ql.ErrorMsg;
+import org.apache.hadoop.hive.ql.QueryState;
+import org.apache.hadoop.hive.ql.exec.ColumnInfo;
+import org.apache.hadoop.hive.ql.exec.FunctionRegistry;
+import org.apache.hadoop.hive.ql.exec.FunctionUtils;
+import org.apache.hadoop.hive.ql.exec.TaskFactory;
+import org.apache.hadoop.hive.ql.hooks.WriteEntity;
+import org.apache.hadoop.hive.ql.lib.Dispatcher;
+import org.apache.hadoop.hive.ql.lib.Node;
+import org.apache.hadoop.hive.ql.lib.PreOrderWalker;
+import org.apache.hadoop.hive.ql.plan.ExprNodeDesc;
+import org.apache.hadoop.hive.ql.plan.HiveOperation;
+import org.apache.hadoop.hive.ql.schq.ScheduledQueryMaintWork;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
+import org.apache.hadoop.hive.serde2.typeinfo.TypeInfoUtils;
+import org.apache.thrift.TException;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+public class ScheduledQueryAnalyzer extends BaseSemanticAnalyzer {
+  private static final Logger LOG = 
LoggerFactory.getLogger(ScheduledQueryAnalyzer.class);
+
+  public ScheduledQueryAnalyzer(QueryState queryState) throws 
SemanticException {
+    super(queryState);
+  }
+
+  @Override
+  public void analyzeInternal(ASTNode ast) throws SemanticException {
+    ScheduledQueryMaintWork work;
+    ScheduledQueryMaintenanceRequestType type = 
translateAstType(ast.getToken().getType());
+    ScheduledQuery schq = interpretAstNode(ast);
+    fillScheduledQuery(type, schq);
+    LOG.info("scheduled query operation: " + type + " " + schq);
+    try {
+      schq.validate();
+    } catch (TException e) {
+      throw new SemanticException("ScheduledQuery is invalid", e);
+    }
+    work = new ScheduledQueryMaintWork(type, schq);
+    rootTasks.add(TaskFactory.get(work));
+  }
+
+  private void fillScheduledQuery(ScheduledQueryMaintenanceRequestType type, 
ScheduledQuery schq)
+      throws SemanticException {
+    if (type == ScheduledQueryMaintenanceRequestType.INSERT) {
+      populateUnfilled(schq, buildEmptySchq());
+    } else {
+      try {
+        ScheduledQuery oldSchq = 
db.getMSC().getScheduledQuery(schq.getScheduleKey());
+        populateUnfilled(schq, oldSchq);
+      } catch (TException e) {
+        throw new SemanticException("unable to get Scheduled query" + e);
+      }
+    }
+  }
+
+  private ScheduledQuery buildEmptySchq() {
+    ScheduledQuery ret = new ScheduledQuery();
+    // ret.setScheduleKey() -- not populated
+    // ret.setSchedule(schedule);
+    // ret.setQuery(query);
+    ret.setEnabled(true);
+    ret.setUser(SessionState.get().getUserName());
+    return ret;
+  }
+
+  private void populateUnfilled(ScheduledQuery schq, ScheduledQuery def) {
+    _Fields[] q = ScheduledQuery._Fields.values();
+    for (_Fields field : q) {
+      if (!schq.isSet(field) && def.isSet(field)) {
+        schq.setFieldValue(field, def.getFieldValue(field));
+      }
+    }
+  }
+
+  private ScheduledQueryMaintenanceRequestType translateAstType(int type) 
throws SemanticException {
+    switch (type) {
+    case HiveParser.TOK_CREATE_SCHEDULED_QUERY:
+      return ScheduledQueryMaintenanceRequestType.INSERT;
+    case HiveParser.TOK_ALTER_SCHEDULED_QUERY:
+      return ScheduledQueryMaintenanceRequestType.UPDATE;
+    case HiveParser.TOK_DROP_SCHEDULED_QUERY:
+      return ScheduledQueryMaintenanceRequestType.DELETE;
+    default:
+      throw new SemanticException("Can't handle: " + type);
+    }
+
+  }
+
+  private ScheduledQuery interpretAstNode(ASTNode ast) throws 
SemanticException {
+    // child0 is the schedule name
+    String scheduleName = ast.getChild(0).getText();
+    String clusterNamespace = 
conf.getVar(ConfVars.HIVE_SCHEDULED_QUERIES_NAMESPACE);
+    LOG.info("scheduled query namespace:" + clusterNamespace);
+    ScheduledQueryKey key = new ScheduledQueryKey(scheduleName, 
clusterNamespace);
+    ScheduledQuery ret = new ScheduledQuery(key);
+
+    // child 1..n are arguments/options/etc
+    for (int i = 1; i < ast.getChildCount(); i++) {
+      processScheduledQueryAstNode(ret, (ASTNode) ast.getChild(i));
+    }
+    return ret;
+  }
+
+  private void processScheduledQueryAstNode(ScheduledQuery schq, ASTNode node) 
throws SemanticException {
+    switch (node.getType()) {
+    case HiveParser.TOK_ENABLE:
+      schq.setEnabled(true);
+      return;
+    case HiveParser.TOK_DISABLE:
+      schq.setEnabled(false);
+      return;
+    case HiveParser.TOK_CRON:
+      schq.setSchedule(unescapeSQLString(node.getChild(0).getText()));
+      return;
+    case HiveParser.TOK_EXECUTED_AS:
+      schq.setUser(unescapeSQLString(node.getChild(0).getText()));
+      return;
+    case HiveParser.TOK_QUERY:
+      schq.setQuery(unparseQuery(node.getChild(0)));
+      return;
+    default:
+      throw new SemanticException("Unexpected token: " + node.getType());
+    }
+  }
+
+  private String unparseQuery(Tree child) throws SemanticException {
+    ASTNode input = (ASTNode) child;
+    ((HiveConf) 
(ctx.getConf())).setBoolVar(HiveConf.ConfVars.HIVE_CBO_ENABLED, false);
 
 Review comment:
   Why are we disabling CBO here?
 
----------------------------------------------------------------
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
 
For queries about this service, please contact Infrastructure at:
us...@infra.apache.org


Issue Time Tracking
-------------------

    Worklog Id:     (was: 324728)

> Scheduled query support
> -----------------------
>
>                 Key: HIVE-21884
>                 URL: https://issues.apache.org/jira/browse/HIVE-21884
>             Project: Hive
>          Issue Type: Improvement
>            Reporter: Zoltan Haindrich
>            Assignee: Zoltan Haindrich
>            Priority: Major
>              Labels: pull-request-available
>         Attachments: HIVE-21844.04.patch, HIVE-21844.05.patch, 
> HIVE-21844.06.patch, HIVE-21844.07.patch, HIVE-21844.08.patch, 
> HIVE-21844.09.patch, HIVE-21844.15.patch, HIVE-21844.19.patch, 
> HIVE-21884.01.patch, HIVE-21884.02.patch, HIVE-21884.03.patch, 
> HIVE-21884.09.patch, HIVE-21884.10.patch, HIVE-21884.10.patch, 
> HIVE-21884.11.patch, HIVE-21884.12.patch, HIVE-21884.13.patch, 
> HIVE-21884.14.patch, HIVE-21884.14.patch, HIVE-21884.14.patch, 
> HIVE-21884.16.patch, HIVE-21884.17.patch, HIVE-21884.18.patch, 
> HIVE-21884.20.patch, Scheduled queries2.pdf
>
>          Time Spent: 2h 10m
>  Remaining Estimate: 0h
>
> design document:
> https://docs.google.com/document/d/1mJSFdJi_1cbxJTXC9QvGw2rQ3zzJkNfxOO6b5esmyCE/edit#
> in case the google doc is not reachable:  [^Scheduled queries2.pdf] 



--
This message was sent by Atlassian Jira
(v8.3.4#803005)

Reply via email to