You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@tajo.apache.org by hy...@apache.org on 2014/05/20 20:46:08 UTC

[10/48] git commit: TAJO-758: Supports parameter values in the SQL file. (Hyoungjun Kim via hyunsik)

TAJO-758: Supports parameter values in the SQL file. (Hyoungjun Kim via hyunsik)


Project: http://git-wip-us.apache.org/repos/asf/tajo/repo
Commit: http://git-wip-us.apache.org/repos/asf/tajo/commit/fd896a6c
Tree: http://git-wip-us.apache.org/repos/asf/tajo/tree/fd896a6c
Diff: http://git-wip-us.apache.org/repos/asf/tajo/diff/fd896a6c

Branch: refs/heads/window_function
Commit: fd896a6c8e28b1e2f0458e49edbbec6195b78901
Parents: 5bbb7e8
Author: Hyunsik Choi <hy...@apache.org>
Authored: Sun Apr 27 23:30:27 2014 +0900
Committer: Hyunsik Choi <hy...@apache.org>
Committed: Sun Apr 27 23:30:27 2014 +0900

----------------------------------------------------------------------
 CHANGES                                         |  4 +
 .../main/java/org/apache/tajo/cli/TajoCli.java  | 40 +++++++++-
 .../java/org/apache/tajo/cli/TestTajoCli.java   | 83 ++++++++++++++++++++
 3 files changed, 124 insertions(+), 3 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/tajo/blob/fd896a6c/CHANGES
----------------------------------------------------------------------
diff --git a/CHANGES b/CHANGES
index 00f00ad..8b56c1c 100644
--- a/CHANGES
+++ b/CHANGES
@@ -9,8 +9,12 @@ Release 0.9.0 - unreleased
   IMPROVEMENT
 
     TAJO-769: A minor improvements for HCatalogStore (Fengdong Yu via hyunsik)
+
     TAJO-734: Arrange TajoCli output message. (hyoungjunkim via jihoon)
 
+    TAJO-758: Supports parameter values in the SQL file.
+    (Hyoungjun Kim via hyunsik)
+
   SUB TASKS
 
     TAJO-783: Remove yarn-related code from tajo-core. (hyunsik)

http://git-wip-us.apache.org/repos/asf/tajo/blob/fd896a6c/tajo-client/src/main/java/org/apache/tajo/cli/TajoCli.java
----------------------------------------------------------------------
diff --git a/tajo-client/src/main/java/org/apache/tajo/cli/TajoCli.java b/tajo-client/src/main/java/org/apache/tajo/cli/TajoCli.java
index 606ca88..cad755e 100644
--- a/tajo-client/src/main/java/org/apache/tajo/cli/TajoCli.java
+++ b/tajo-client/src/main/java/org/apache/tajo/cli/TajoCli.java
@@ -81,7 +81,7 @@ public class TajoCli {
   };
   private final Map<String, TajoShellCommand> commands = new TreeMap<String, TajoShellCommand>();
 
-  private static final Options options;
+  protected static final Options options;
   private static final String HOME_DIR = System.getProperty("user.home");
   private static final String HISTORY_FILE = ".tajo_history";
 
@@ -91,6 +91,8 @@ public class TajoCli {
     options.addOption("f", "file", true, "execute commands from file, then exit");
     options.addOption("h", "host", true, "Tajo server host");
     options.addOption("p", "port", true, "Tajo server port");
+    options.addOption("conf", "conf", true, "configuration value");
+    options.addOption("param", "param", true, "parameter value in SQL file");
     options.addOption("help", "help", false, "help");
   }
 
@@ -124,8 +126,8 @@ public class TajoCli {
     this.reader.setExpandEvents(false);
     this.sout = new PrintWriter(reader.getOutput());
     Class formatterClass = conf.getClass(conf.getVar(ConfVars.CLI__OUTPUT_FORMATTER_CLASS),
-            DefaultTajoCliOutputFormatter.class);
-
+        DefaultTajoCliOutputFormatter.class);
+    
     this.outputFormatter = (TajoCliOutputFormatter)formatterClass.newInstance();
     this.outputFormatter.init(conf);
 
@@ -150,6 +152,16 @@ public class TajoCli {
       baseDatabase = (String) cmd.getArgList().get(0);
     }
 
+    if (cmd.getOptionValues("conf") != null) {
+      for (String eachParam: cmd.getOptionValues("conf")) {
+        String[] tokens = eachParam.split("=");
+        if (tokens.length != 2) {
+          continue;
+        }
+        conf.set(tokens[0], tokens[1]);
+      }
+    }
+
     // if there is no "-h" option,
     if(hostName == null) {
       if (conf.getVar(ConfVars.TAJO_MASTER_CLIENT_RPC_ADDRESS) != null) {
@@ -189,9 +201,11 @@ public class TajoCli {
     }
     if (cmd.hasOption("f")) {
       outputFormatter.setScirptMode();
+      cmd.getOptionValues("");
       File sqlFile = new File(cmd.getOptionValue("f"));
       if (sqlFile.exists()) {
         String script = FileUtil.readTextFile(new File(cmd.getOptionValue("f")));
+        script = replaceParam(script, cmd.getOptionValues("param"));
         executeScript(script);
         sout.flush();
         System.exit(0);
@@ -204,6 +218,26 @@ public class TajoCli {
     addShutdownHook();
   }
 
+  public TajoCliContext getContext() {
+    return context;
+  }
+
+  protected static String replaceParam(String script, String[] params) {
+    if (params == null || params.length == 0) {
+      return script;
+    }
+
+    for (String eachParam: params) {
+      String[] tokens = eachParam.split("=");
+      if (tokens.length != 2) {
+        continue;
+      }
+      script = script.replace("${" + tokens[0] + "}", tokens[1]);
+    }
+
+    return script;
+  }
+
   private void initHistory() {
     try {
       String historyPath = HOME_DIR + File.separator + HISTORY_FILE;

http://git-wip-us.apache.org/repos/asf/tajo/blob/fd896a6c/tajo-core/src/test/java/org/apache/tajo/cli/TestTajoCli.java
----------------------------------------------------------------------
diff --git a/tajo-core/src/test/java/org/apache/tajo/cli/TestTajoCli.java b/tajo-core/src/test/java/org/apache/tajo/cli/TestTajoCli.java
new file mode 100644
index 0000000..02ec15d
--- /dev/null
+++ b/tajo-core/src/test/java/org/apache/tajo/cli/TestTajoCli.java
@@ -0,0 +1,83 @@
+/**
+ * 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.tajo.cli;
+
+import org.apache.commons.cli.CommandLine;
+import org.apache.commons.cli.CommandLineParser;
+import org.apache.commons.cli.PosixParser;
+import org.apache.tajo.TpchTestBase;
+import org.apache.tajo.conf.TajoConf;
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
+public class TestTajoCli {
+  @Test
+  public void testParseParam() throws Exception {
+    String[] args = new String[]{"-f", "test.sql", "--param", "test1=10", "--param", "test2=20"};
+
+    CommandLineParser parser = new PosixParser();
+    CommandLine cmd = parser.parse(TajoCli.options, args);
+
+    String fileName = cmd.getOptionValue("f");
+    assertNotNull(fileName);
+    assertEquals(args[1], fileName);
+
+    String[] paramValues = cmd.getOptionValues("param");
+
+    assertNotNull(paramValues);
+    assertEquals(2, paramValues.length);
+
+    assertEquals("test1=10", paramValues[0]);
+    assertEquals("test2=20", paramValues[1]);
+  }
+
+  @Test
+  public void testParseConf() throws Exception {
+    String[] args = new String[]{"--conf", "tajo.cli.print.pause=false",
+        "--conf", "tajo.executor.join.inner.in-memory-table-num=256"};
+
+    CommandLineParser parser = new PosixParser();
+    CommandLine cmd = parser.parse(TajoCli.options, args);
+    String[] confValues = cmd.getOptionValues("conf");
+
+    assertNotNull(confValues);
+    assertEquals(2, confValues.length);
+
+    assertEquals("tajo.cli.print.pause=false", confValues[0]);
+    assertEquals("tajo.executor.join.inner.in-memory-table-num=256", confValues[1]);
+
+    TajoConf tajoConf = TpchTestBase.getInstance().getTestingCluster().getConfiguration();
+
+    TajoCli shell = new TajoCli(tajoConf, args, System.in, System.out);
+    assertEquals("false", shell.getContext().getConf().get("tajo.cli.print.pause"));
+    assertEquals("256", shell.getContext().getConf().get("tajo.executor.join.inner.in-memory-table-num"));
+  }
+
+  @Test
+  public void testReplaceParam() throws Exception {
+    String sql = "select * from lineitem where l_tax > ${tax} and l_returnflag > '${returnflag}'";
+    String[] params = new String[]{"tax=10", "returnflag=A"};
+
+
+    String expected = "select * from lineitem where l_tax > 10 and l_returnflag > 'A'";
+    assertEquals(expected, TajoCli.replaceParam(sql, params));
+  }
+}