PageRenderTime 56ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/itests/util/src/main/java/org/apache/hadoop/hive/ql/QTestUtil.java

https://github.com/mkgobaco/hive
Java | 1761 lines | 1482 code | 157 blank | 122 comment | 169 complexity | 62a4953bf18ffe4c53bc6def65f1fd4f MD5 | raw file
Possible License(s): Apache-2.0

Large files files are truncated, but you can click here to view the full file

  1. /**
  2. * Licensed to the Apache Software Foundation (ASF) under one
  3. * or more contributor license agreements. See the NOTICE file
  4. * distributed with this work for additional information
  5. * regarding copyright ownership. The ASF licenses this file
  6. * to you under the Apache License, Version 2.0 (the
  7. * "License"); you may not use this file except in compliance
  8. * with the License. You may obtain a copy of the License at
  9. *
  10. * http://www.apache.org/licenses/LICENSE-2.0
  11. *
  12. * Unless required by applicable law or agreed to in writing, software
  13. * distributed under the License is distributed on an "AS IS" BASIS,
  14. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. * See the License for the specific language governing permissions and
  16. * limitations under the License.
  17. */
  18. package org.apache.hadoop.hive.ql;
  19. import static org.apache.hadoop.hive.metastore.MetaStoreUtils.DEFAULT_DATABASE_NAME;
  20. import java.io.BufferedInputStream;
  21. import java.io.BufferedOutputStream;
  22. import java.io.BufferedReader;
  23. import java.io.BufferedWriter;
  24. import java.io.File;
  25. import java.io.FileInputStream;
  26. import java.io.FileNotFoundException;
  27. import java.io.FileOutputStream;
  28. import java.io.FileReader;
  29. import java.io.FileWriter;
  30. import java.io.FilenameFilter;
  31. import java.io.IOException;
  32. import java.io.InputStreamReader;
  33. import java.io.OutputStream;
  34. import java.io.OutputStreamWriter;
  35. import java.io.PrintStream;
  36. import java.io.Serializable;
  37. import java.io.StringWriter;
  38. import java.io.UnsupportedEncodingException;
  39. import java.net.URL;
  40. import java.util.ArrayList;
  41. import java.util.Arrays;
  42. import java.util.Collection;
  43. import java.util.Deque;
  44. import java.util.HashMap;
  45. import java.util.HashSet;
  46. import java.util.LinkedList;
  47. import java.util.List;
  48. import java.util.Set;
  49. import java.util.TreeMap;
  50. import java.util.regex.Matcher;
  51. import java.util.regex.Pattern;
  52. import junit.framework.Assert;
  53. import org.apache.commons.io.FileUtils;
  54. import org.apache.commons.io.IOUtils;
  55. import org.apache.commons.logging.Log;
  56. import org.apache.commons.logging.LogFactory;
  57. import org.apache.hadoop.fs.FileStatus;
  58. import org.apache.hadoop.fs.FileSystem;
  59. import org.apache.hadoop.fs.Path;
  60. import org.apache.hadoop.hbase.zookeeper.MiniZooKeeperCluster;
  61. import org.apache.hadoop.hive.cli.CliDriver;
  62. import org.apache.hadoop.hive.cli.CliSessionState;
  63. import org.apache.hadoop.hive.common.io.DigestPrintStream;
  64. import org.apache.hadoop.hive.common.io.SortAndDigestPrintStream;
  65. import org.apache.hadoop.hive.common.io.SortPrintStream;
  66. import org.apache.hadoop.hive.common.io.CachingPrintStream;
  67. import org.apache.hadoop.hive.conf.HiveConf;
  68. import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
  69. import org.apache.hadoop.hive.metastore.MetaStoreUtils;
  70. import org.apache.hadoop.hive.metastore.api.Index;
  71. import org.apache.hadoop.hive.ql.exec.FunctionRegistry;
  72. import org.apache.hadoop.hive.ql.exec.Task;
  73. import org.apache.hadoop.hive.ql.exec.Utilities;
  74. import org.apache.hadoop.hive.ql.exec.Utilities.StreamPrinter;
  75. import org.apache.hadoop.hive.ql.exec.vector.util.AllVectorTypesRecord;
  76. import org.apache.hadoop.hive.ql.io.IgnoreKeyTextOutputFormat;
  77. import org.apache.hadoop.hive.ql.lockmgr.zookeeper.ZooKeeperHiveLockManager;
  78. import org.apache.hadoop.hive.ql.metadata.Hive;
  79. import org.apache.hadoop.hive.ql.metadata.Table;
  80. import org.apache.hadoop.hive.ql.parse.ASTNode;
  81. import org.apache.hadoop.hive.ql.parse.BaseSemanticAnalyzer;
  82. import org.apache.hadoop.hive.ql.parse.ParseDriver;
  83. import org.apache.hadoop.hive.ql.parse.ParseException;
  84. import org.apache.hadoop.hive.ql.parse.SemanticAnalyzer;
  85. import org.apache.hadoop.hive.ql.parse.SemanticException;
  86. import org.apache.hadoop.hive.ql.session.SessionState;
  87. import org.apache.hadoop.hive.serde.serdeConstants;
  88. import org.apache.hadoop.hive.serde2.thrift.ThriftDeserializer;
  89. import org.apache.hadoop.hive.serde2.thrift.test.Complex;
  90. import org.apache.hadoop.hive.shims.HadoopShims;
  91. import org.apache.hadoop.hive.shims.ShimLoader;
  92. import org.apache.hadoop.mapred.SequenceFileInputFormat;
  93. import org.apache.hadoop.mapred.SequenceFileOutputFormat;
  94. import org.apache.hadoop.mapred.TextInputFormat;
  95. import org.apache.hadoop.util.Shell;
  96. import org.apache.thrift.protocol.TBinaryProtocol;
  97. import org.apache.tools.ant.BuildException;
  98. import org.apache.zookeeper.WatchedEvent;
  99. import org.apache.zookeeper.Watcher;
  100. import org.apache.zookeeper.ZooKeeper;
  101. import org.junit.Assume;
  102. import com.google.common.collect.ImmutableList;
  103. /**
  104. * QTestUtil.
  105. *
  106. */
  107. public class QTestUtil {
  108. public static final String UTF_8 = "UTF-8";
  109. private static final Log LOG = LogFactory.getLog("QTestUtil");
  110. private String testWarehouse;
  111. private final String testFiles;
  112. protected final String outDir;
  113. protected final String logDir;
  114. private final TreeMap<String, String> qMap;
  115. private final Set<String> qSkipSet;
  116. private final Set<String> qSortSet;
  117. private final Set<String> qSortQuerySet;
  118. private final Set<String> qHashQuerySet;
  119. private final Set<String> qSortNHashQuerySet;
  120. private static final String SORT_SUFFIX = ".sorted";
  121. public static final HashSet<String> srcTables = new HashSet<String>();
  122. private static MiniClusterType clusterType = MiniClusterType.none;
  123. private ParseDriver pd;
  124. private Hive db;
  125. protected HiveConf conf;
  126. private Driver drv;
  127. private BaseSemanticAnalyzer sem;
  128. private FileSystem fs;
  129. protected final boolean overWrite;
  130. private CliDriver cliDriver;
  131. private HadoopShims.MiniMrShim mr = null;
  132. private HadoopShims.MiniDFSShim dfs = null;
  133. private boolean miniMr = false;
  134. private String hadoopVer = null;
  135. private QTestSetup setup = null;
  136. static {
  137. for (String srcTable : System.getProperty("test.src.tables", "").trim().split(",")) {
  138. srcTable = srcTable.trim();
  139. if (!srcTable.isEmpty()) {
  140. srcTables.add(srcTable);
  141. }
  142. }
  143. if (srcTables.isEmpty()) {
  144. throw new AssertionError("Source tables cannot be empty");
  145. }
  146. }
  147. public HiveConf getConf() {
  148. return conf;
  149. }
  150. public boolean deleteDirectory(File path) {
  151. if (path.exists()) {
  152. File[] files = path.listFiles();
  153. for (File file : files) {
  154. if (file.isDirectory()) {
  155. deleteDirectory(file);
  156. } else {
  157. file.delete();
  158. }
  159. }
  160. }
  161. return (path.delete());
  162. }
  163. public void copyDirectoryToLocal(Path src, Path dest) throws Exception {
  164. FileSystem srcFs = src.getFileSystem(conf);
  165. FileSystem destFs = dest.getFileSystem(conf);
  166. if (srcFs.exists(src)) {
  167. FileStatus[] files = srcFs.listStatus(src);
  168. for (FileStatus file : files) {
  169. String name = file.getPath().getName();
  170. Path dfs_path = file.getPath();
  171. Path local_path = new Path(dest, name);
  172. // If this is a source table we do not copy it out
  173. if (srcTables.contains(name)) {
  174. continue;
  175. }
  176. if (file.isDir()) {
  177. if (!destFs.exists(local_path)) {
  178. destFs.mkdirs(local_path);
  179. }
  180. copyDirectoryToLocal(dfs_path, local_path);
  181. } else {
  182. srcFs.copyToLocalFile(dfs_path, local_path);
  183. }
  184. }
  185. }
  186. }
  187. static Pattern mapTok = Pattern.compile("(\\.?)(.*)_map_(.*)");
  188. static Pattern reduceTok = Pattern.compile("(.*)(reduce_[^\\.]*)((\\..*)?)");
  189. public void normalizeNames(File path) throws Exception {
  190. if (path.isDirectory()) {
  191. File[] files = path.listFiles();
  192. for (File file : files) {
  193. normalizeNames(file);
  194. }
  195. } else {
  196. Matcher m = reduceTok.matcher(path.getName());
  197. if (m.matches()) {
  198. String name = m.group(1) + "reduce" + m.group(3);
  199. path.renameTo(new File(path.getParent(), name));
  200. } else {
  201. m = mapTok.matcher(path.getName());
  202. if (m.matches()) {
  203. String name = m.group(1) + "map_" + m.group(3);
  204. path.renameTo(new File(path.getParent(), name));
  205. }
  206. }
  207. }
  208. }
  209. public QTestUtil(String outDir, String logDir) throws Exception {
  210. this(outDir, logDir, MiniClusterType.none, null, "0.20");
  211. }
  212. public String getOutputDirectory() {
  213. return outDir;
  214. }
  215. public String getLogDirectory() {
  216. return logDir;
  217. }
  218. private String getHadoopMainVersion(String input) {
  219. if (input == null) {
  220. return null;
  221. }
  222. Pattern p = Pattern.compile("^(\\d+\\.\\d+).*");
  223. Matcher m = p.matcher(input);
  224. if (m.matches()) {
  225. return m.group(1);
  226. }
  227. return null;
  228. }
  229. public void initConf() throws Exception {
  230. String vectorizationEnabled = System.getProperty("test.vectorization.enabled");
  231. if(vectorizationEnabled != null && vectorizationEnabled.equalsIgnoreCase("true")) {
  232. conf.setBoolVar(ConfVars.HIVE_VECTORIZATION_ENABLED, true);
  233. }
  234. // Plug verifying metastore in for testing.
  235. conf.setVar(HiveConf.ConfVars.METASTORE_RAW_STORE_IMPL,
  236. "org.apache.hadoop.hive.metastore.VerifyingObjectStore");
  237. if (mr != null) {
  238. assert dfs != null;
  239. mr.setupConfiguration(conf);
  240. // set fs.default.name to the uri of mini-dfs
  241. String dfsUriString = WindowsPathUtil.getHdfsUriString(dfs.getFileSystem().getUri().toString());
  242. conf.setVar(HiveConf.ConfVars.HADOOPFS, dfsUriString);
  243. // hive.metastore.warehouse.dir needs to be set relative to the mini-dfs
  244. conf.setVar(HiveConf.ConfVars.METASTOREWAREHOUSE,
  245. (new Path(dfsUriString,
  246. "/build/ql/test/data/warehouse/")).toString());
  247. }
  248. // Windows paths should be converted after MiniMrShim.setupConfiguration()
  249. // since setupConfiguration may overwrite configuration values.
  250. if (Shell.WINDOWS) {
  251. WindowsPathUtil.convertPathsFromWindowsToHdfs(conf);
  252. }
  253. }
  254. public enum MiniClusterType {
  255. mr,
  256. tez,
  257. none;
  258. public static MiniClusterType valueForString(String type) {
  259. if (type.equals("miniMR")) {
  260. return mr;
  261. } else if (type.equals("tez")) {
  262. return tez;
  263. } else {
  264. return none;
  265. }
  266. }
  267. }
  268. public QTestUtil(String outDir, String logDir, MiniClusterType clusterType, String hadoopVer)
  269. throws Exception {
  270. this(outDir, logDir, clusterType, null, hadoopVer);
  271. }
  272. public QTestUtil(String outDir, String logDir, MiniClusterType clusterType,
  273. String confDir, String hadoopVer)
  274. throws Exception {
  275. this.outDir = outDir;
  276. this.logDir = logDir;
  277. if (confDir != null && !confDir.isEmpty()) {
  278. HiveConf.setHiveSiteLocation(new URL("file://"+confDir+"/hive-site.xml"));
  279. System.out.println("Setting hive-site: "+HiveConf.getHiveSiteLocation());
  280. }
  281. conf = new HiveConf(Driver.class);
  282. this.miniMr = (clusterType == MiniClusterType.mr);
  283. this.hadoopVer = getHadoopMainVersion(hadoopVer);
  284. qMap = new TreeMap<String, String>();
  285. qSkipSet = new HashSet<String>();
  286. qSortSet = new HashSet<String>();
  287. qSortQuerySet = new HashSet<String>();
  288. qHashQuerySet = new HashSet<String>();
  289. qSortNHashQuerySet = new HashSet<String>();
  290. this.clusterType = clusterType;
  291. HadoopShims shims = ShimLoader.getHadoopShims();
  292. int numberOfDataNodes = 4;
  293. // can run tez tests only on hadoop 2
  294. if (clusterType == MiniClusterType.tez) {
  295. Assume.assumeTrue(ShimLoader.getMajorVersion().equals("0.23"));
  296. // this is necessary temporarily - there's a probem with multi datanodes on MiniTezCluster
  297. // will be fixed in 0.3
  298. numberOfDataNodes = 1;
  299. }
  300. if (clusterType != MiniClusterType.none) {
  301. dfs = shims.getMiniDfs(conf, numberOfDataNodes, true, null);
  302. FileSystem fs = dfs.getFileSystem();
  303. String uriString = WindowsPathUtil.getHdfsUriString(fs.getUri().toString());
  304. if (clusterType == MiniClusterType.tez) {
  305. mr = shims.getMiniTezCluster(conf, 4, uriString, 1);
  306. } else {
  307. mr = shims.getMiniMrCluster(conf, 4, uriString, 1);
  308. }
  309. }
  310. initConf();
  311. // Use the current directory if it is not specified
  312. String dataDir = conf.get("test.data.files");
  313. if (dataDir == null) {
  314. dataDir = new File(".").getAbsolutePath() + "/data/files";
  315. }
  316. testFiles = dataDir;
  317. overWrite = "true".equalsIgnoreCase(System.getProperty("test.output.overwrite"));
  318. setup = new QTestSetup();
  319. setup.preTest(conf);
  320. init();
  321. }
  322. public void shutdown() throws Exception {
  323. cleanUp();
  324. setup.tearDown();
  325. if (mr != null) {
  326. mr.shutdown();
  327. mr = null;
  328. }
  329. FileSystem.closeAll();
  330. if (dfs != null) {
  331. dfs.shutdown();
  332. dfs = null;
  333. }
  334. }
  335. public String readEntireFileIntoString(File queryFile) throws IOException {
  336. InputStreamReader isr = new InputStreamReader(
  337. new BufferedInputStream(new FileInputStream(queryFile)), QTestUtil.UTF_8);
  338. StringWriter sw = new StringWriter();
  339. try {
  340. IOUtils.copy(isr, sw);
  341. } finally {
  342. if (isr != null) {
  343. isr.close();
  344. }
  345. }
  346. return sw.toString();
  347. }
  348. public void addFile(String queryFile) throws IOException {
  349. addFile(queryFile, false);
  350. }
  351. public void addFile(String queryFile, boolean partial) throws IOException {
  352. addFile(new File(queryFile));
  353. }
  354. public void addFile(File qf) throws IOException {
  355. addFile(qf, false);
  356. }
  357. public void addFile(File qf, boolean partial) throws IOException {
  358. String query = readEntireFileIntoString(qf);
  359. qMap.put(qf.getName(), query);
  360. if (partial) return;
  361. if(checkHadoopVersionExclude(qf.getName(), query)
  362. || checkOSExclude(qf.getName(), query)) {
  363. qSkipSet.add(qf.getName());
  364. }
  365. if (matches(SORT_BEFORE_DIFF, query)) {
  366. qSortSet.add(qf.getName());
  367. } else if (matches(SORT_QUERY_RESULTS, query)) {
  368. qSortQuerySet.add(qf.getName());
  369. } else if (matches(HASH_QUERY_RESULTS, query)) {
  370. qHashQuerySet.add(qf.getName());
  371. } else if (matches(SORT_AND_HASH_QUERY_RESULTS, query)) {
  372. qSortNHashQuerySet.add(qf.getName());
  373. }
  374. }
  375. private static final Pattern SORT_BEFORE_DIFF = Pattern.compile("-- SORT_BEFORE_DIFF");
  376. private static final Pattern SORT_QUERY_RESULTS = Pattern.compile("-- SORT_QUERY_RESULTS");
  377. private static final Pattern HASH_QUERY_RESULTS = Pattern.compile("-- HASH_QUERY_RESULTS");
  378. private static final Pattern SORT_AND_HASH_QUERY_RESULTS = Pattern.compile("-- SORT_AND_HASH_QUERY_RESULTS");
  379. private boolean matches(Pattern pattern, String query) {
  380. Matcher matcher = pattern.matcher(query);
  381. if (matcher.find()) {
  382. return true;
  383. }
  384. return false;
  385. }
  386. private boolean checkHadoopVersionExclude(String fileName, String query){
  387. // Look for a hint to not run a test on some Hadoop versions
  388. Pattern pattern = Pattern.compile("-- (EX|IN)CLUDE_HADOOP_MAJOR_VERSIONS\\((.*)\\)");
  389. boolean excludeQuery = false;
  390. boolean includeQuery = false;
  391. Set<String> versionSet = new HashSet<String>();
  392. String hadoopVer = ShimLoader.getMajorVersion();
  393. Matcher matcher = pattern.matcher(query);
  394. // Each qfile may include at most one INCLUDE or EXCLUDE directive.
  395. //
  396. // If a qfile contains an INCLUDE directive, and hadoopVer does
  397. // not appear in the list of versions to include, then the qfile
  398. // is skipped.
  399. //
  400. // If a qfile contains an EXCLUDE directive, and hadoopVer is
  401. // listed in the list of versions to EXCLUDE, then the qfile is
  402. // skipped.
  403. //
  404. // Otherwise, the qfile is included.
  405. if (matcher.find()) {
  406. String prefix = matcher.group(1);
  407. if ("EX".equals(prefix)) {
  408. excludeQuery = true;
  409. } else {
  410. includeQuery = true;
  411. }
  412. String versions = matcher.group(2);
  413. for (String s : versions.split("\\,")) {
  414. s = s.trim();
  415. versionSet.add(s);
  416. }
  417. }
  418. if (matcher.find()) {
  419. //2nd match is not supposed to be there
  420. String message = "QTestUtil: qfile " + fileName
  421. + " contains more than one reference to (EX|IN)CLUDE_HADOOP_MAJOR_VERSIONS";
  422. throw new UnsupportedOperationException(message);
  423. }
  424. if (excludeQuery && versionSet.contains(hadoopVer)) {
  425. System.out.println("QTestUtil: " + fileName
  426. + " EXCLUDE list contains Hadoop Version " + hadoopVer + ". Skipping...");
  427. return true;
  428. } else if (includeQuery && !versionSet.contains(hadoopVer)) {
  429. System.out.println("QTestUtil: " + fileName
  430. + " INCLUDE list does not contain Hadoop Version " + hadoopVer + ". Skipping...");
  431. return true;
  432. }
  433. return false;
  434. }
  435. private boolean checkOSExclude(String fileName, String query){
  436. // Look for a hint to not run a test on some Hadoop versions
  437. Pattern pattern = Pattern.compile("-- (EX|IN)CLUDE_OS_WINDOWS");
  438. // detect whether this query wants to be excluded or included
  439. // on windows
  440. Matcher matcher = pattern.matcher(query);
  441. if (matcher.find()) {
  442. String prefix = matcher.group(1);
  443. if ("EX".equals(prefix)) {
  444. //windows is to be exluded
  445. if(Shell.WINDOWS){
  446. System.out.println("Due to the OS being windows " +
  447. "adding the query " + fileName +
  448. " to the set of tests to skip");
  449. return true;
  450. }
  451. }
  452. else if(!Shell.WINDOWS){
  453. //non windows to be exluded
  454. System.out.println("Due to the OS not being windows " +
  455. "adding the query " + fileName +
  456. " to the set of tests to skip");
  457. return true;
  458. }
  459. }
  460. return false;
  461. }
  462. /**
  463. * Clear out any side effects of running tests
  464. */
  465. public void clearPostTestEffects() throws Exception {
  466. setup.postTest(conf);
  467. }
  468. /**
  469. * Clear out any side effects of running tests
  470. */
  471. public void clearTestSideEffects() throws Exception {
  472. // Delete any tables other than the source tables
  473. // and any databases other than the default database.
  474. for (String dbName : db.getAllDatabases()) {
  475. SessionState.get().setCurrentDatabase(dbName);
  476. for (String tblName : db.getAllTables()) {
  477. if (!DEFAULT_DATABASE_NAME.equals(dbName) || !srcTables.contains(tblName)) {
  478. Table tblObj = db.getTable(tblName);
  479. // dropping index table can not be dropped directly. Dropping the base
  480. // table will automatically drop all its index table
  481. if(tblObj.isIndexTable()) {
  482. continue;
  483. }
  484. db.dropTable(dbName, tblName);
  485. } else {
  486. // this table is defined in srcTables, drop all indexes on it
  487. List<Index> indexes = db.getIndexes(dbName, tblName, (short)-1);
  488. if (indexes != null && indexes.size() > 0) {
  489. for (Index index : indexes) {
  490. db.dropIndex(dbName, tblName, index.getIndexName(), true);
  491. }
  492. }
  493. }
  494. }
  495. if (!DEFAULT_DATABASE_NAME.equals(dbName)) {
  496. // Drop cascade, may need to drop functions
  497. db.dropDatabase(dbName, true, true, true);
  498. }
  499. }
  500. // delete remaining directories for external tables (can affect stats for following tests)
  501. try {
  502. Path p = new Path(testWarehouse);
  503. FileSystem fileSystem = p.getFileSystem(conf);
  504. for (FileStatus status : fileSystem.listStatus(p)) {
  505. if (status.isDir() && !srcTables.contains(status.getPath().getName())) {
  506. fileSystem.delete(status.getPath(), true);
  507. }
  508. }
  509. } catch (IllegalArgumentException e) {
  510. // ignore.. provides invalid url sometimes intentionally
  511. }
  512. SessionState.get().setCurrentDatabase(DEFAULT_DATABASE_NAME);
  513. List<String> roleNames = db.getAllRoleNames();
  514. for (String roleName : roleNames) {
  515. if (!"PUBLIC".equalsIgnoreCase(roleName) && !"ADMIN".equalsIgnoreCase(roleName)) {
  516. db.dropRole(roleName);
  517. }
  518. }
  519. // allocate and initialize a new conf since a test can
  520. // modify conf by using 'set' commands
  521. conf = new HiveConf (Driver.class);
  522. initConf();
  523. db = Hive.get(conf); // propagate new conf to meta store
  524. setup.preTest(conf);
  525. }
  526. public void cleanUp() throws Exception {
  527. // Drop any tables that remain due to unsuccessful runs
  528. for (String s : new String[] {"src", "src1", "src_json", "src_thrift",
  529. "src_sequencefile", "srcpart", "srcbucket", "srcbucket2", "dest1",
  530. "dest2", "dest3", "dest4", "dest4_sequencefile", "dest_j1", "dest_j2",
  531. "dest_g1", "dest_g2", "fetchtask_ioexception",
  532. AllVectorTypesRecord.TABLE_NAME}) {
  533. db.dropTable(MetaStoreUtils.DEFAULT_DATABASE_NAME, s);
  534. }
  535. // delete any contents in the warehouse dir
  536. Path p = new Path(testWarehouse);
  537. FileSystem fs = p.getFileSystem(conf);
  538. try {
  539. FileStatus [] ls = fs.listStatus(p);
  540. for (int i=0; (ls != null) && (i<ls.length); i++) {
  541. fs.delete(ls[i].getPath(), true);
  542. }
  543. } catch (FileNotFoundException e) {
  544. // Best effort
  545. }
  546. FunctionRegistry.unregisterTemporaryUDF("test_udaf");
  547. FunctionRegistry.unregisterTemporaryUDF("test_error");
  548. }
  549. private void runLoadCmd(String loadCmd) throws Exception {
  550. int ecode = 0;
  551. ecode = drv.run(loadCmd).getResponseCode();
  552. drv.close();
  553. if (ecode != 0) {
  554. throw new Exception("load command: " + loadCmd
  555. + " failed with exit code= " + ecode);
  556. }
  557. return;
  558. }
  559. private void runCreateTableCmd(String createTableCmd) throws Exception {
  560. int ecode = 0;
  561. ecode = drv.run(createTableCmd).getResponseCode();
  562. if (ecode != 0) {
  563. throw new Exception("create table command: " + createTableCmd
  564. + " failed with exit code= " + ecode);
  565. }
  566. return;
  567. }
  568. private void runCmd(String cmd) throws Exception {
  569. int ecode = 0;
  570. ecode = drv.run(cmd).getResponseCode();
  571. drv.close();
  572. if (ecode != 0) {
  573. throw new Exception("command: " + cmd
  574. + " failed with exit code= " + ecode);
  575. }
  576. return;
  577. }
  578. public void createSources() throws Exception {
  579. startSessionState();
  580. conf.setBoolean("hive.test.init.phase", true);
  581. // Create a bunch of tables with columns key and value
  582. LinkedList<String> cols = new LinkedList<String>();
  583. cols.add("key");
  584. cols.add("value");
  585. LinkedList<String> part_cols = new LinkedList<String>();
  586. part_cols.add("ds");
  587. part_cols.add("hr");
  588. db.createTable("srcpart", cols, part_cols, TextInputFormat.class,
  589. IgnoreKeyTextOutputFormat.class);
  590. Path fpath;
  591. HashMap<String, String> part_spec = new HashMap<String, String>();
  592. for (String ds : new String[] {"2008-04-08", "2008-04-09"}) {
  593. for (String hr : new String[] {"11", "12"}) {
  594. part_spec.clear();
  595. part_spec.put("ds", ds);
  596. part_spec.put("hr", hr);
  597. // System.out.println("Loading partition with spec: " + part_spec);
  598. // db.createPartition(srcpart, part_spec);
  599. fpath = new Path(testFiles, "kv1.txt");
  600. // db.loadPartition(fpath, srcpart.getName(), part_spec, true);
  601. runLoadCmd("LOAD DATA LOCAL INPATH '" + fpath.toUri().getPath()
  602. + "' OVERWRITE INTO TABLE srcpart PARTITION (ds='" + ds + "',hr='"
  603. + hr + "')");
  604. }
  605. }
  606. ArrayList<String> bucketCols = new ArrayList<String>();
  607. bucketCols.add("key");
  608. runCreateTableCmd("CREATE TABLE srcbucket(key int, value string) CLUSTERED BY (key) INTO 2 BUCKETS STORED AS TEXTFILE");
  609. // db.createTable("srcbucket", cols, null, TextInputFormat.class,
  610. // IgnoreKeyTextOutputFormat.class, 2, bucketCols);
  611. for (String fname : new String[] {"srcbucket0.txt", "srcbucket1.txt"}) {
  612. fpath = new Path(testFiles, fname);
  613. runLoadCmd("LOAD DATA LOCAL INPATH '" + fpath.toUri().getPath()
  614. + "' INTO TABLE srcbucket");
  615. }
  616. runCreateTableCmd("CREATE TABLE srcbucket2(key int, value string) "
  617. + "CLUSTERED BY (key) INTO 4 BUCKETS STORED AS TEXTFILE");
  618. // db.createTable("srcbucket", cols, null, TextInputFormat.class,
  619. // IgnoreKeyTextOutputFormat.class, 2, bucketCols);
  620. for (String fname : new String[] {"srcbucket20.txt", "srcbucket21.txt",
  621. "srcbucket22.txt", "srcbucket23.txt"}) {
  622. fpath = new Path(testFiles, fname);
  623. runLoadCmd("LOAD DATA LOCAL INPATH '" + fpath.toUri().getPath()
  624. + "' INTO TABLE srcbucket2");
  625. }
  626. for (String tname : new String[] {"src", "src1"}) {
  627. db.createTable(tname, cols, null, TextInputFormat.class,
  628. IgnoreKeyTextOutputFormat.class);
  629. }
  630. db.createTable("src_sequencefile", cols, null,
  631. SequenceFileInputFormat.class, SequenceFileOutputFormat.class);
  632. Table srcThrift =
  633. new Table(SessionState.get().getCurrentDatabase(), "src_thrift");
  634. srcThrift.setInputFormatClass(SequenceFileInputFormat.class.getName());
  635. srcThrift.setOutputFormatClass(SequenceFileOutputFormat.class.getName());
  636. srcThrift.setSerializationLib(ThriftDeserializer.class.getName());
  637. srcThrift.setSerdeParam(serdeConstants.SERIALIZATION_CLASS, Complex.class
  638. .getName());
  639. srcThrift.setSerdeParam(serdeConstants.SERIALIZATION_FORMAT,
  640. TBinaryProtocol.class.getName());
  641. db.createTable(srcThrift);
  642. LinkedList<String> json_cols = new LinkedList<String>();
  643. json_cols.add("json");
  644. db.createTable("src_json", json_cols, null, TextInputFormat.class,
  645. IgnoreKeyTextOutputFormat.class);
  646. // load the input data into the src table
  647. fpath = new Path(testFiles, "kv1.txt");
  648. runLoadCmd("LOAD DATA LOCAL INPATH '" + fpath.toUri().getPath() + "' INTO TABLE src");
  649. // load the input data into the src table
  650. fpath = new Path(testFiles, "kv3.txt");
  651. runLoadCmd("LOAD DATA LOCAL INPATH '" + fpath.toUri().getPath() + "' INTO TABLE src1");
  652. // load the input data into the src_sequencefile table
  653. fpath = new Path(testFiles, "kv1.seq");
  654. runLoadCmd("LOAD DATA LOCAL INPATH '" + fpath.toUri().getPath()
  655. + "' INTO TABLE src_sequencefile");
  656. // load the input data into the src_thrift table
  657. fpath = new Path(testFiles, "complex.seq");
  658. runLoadCmd("LOAD DATA LOCAL INPATH '" + fpath.toUri().getPath()
  659. + "' INTO TABLE src_thrift");
  660. // load the json data into the src_json table
  661. fpath = new Path(testFiles, "json.txt");
  662. runLoadCmd("LOAD DATA LOCAL INPATH '" + fpath.toUri().getPath()
  663. + "' INTO TABLE src_json");
  664. FileSystem localFs = FileSystem.getLocal(conf);
  665. // create and load data into orc table
  666. fpath = new Path(testFiles, AllVectorTypesRecord.TABLE_NAME);
  667. runCreateTableCmd(AllVectorTypesRecord.TABLE_CREATE_COMMAND);
  668. runLoadCmd("LOAD DATA LOCAL INPATH '" + fpath.toUri().getPath()
  669. + "' INTO TABLE "+AllVectorTypesRecord.TABLE_NAME);
  670. runCmd("DROP FUNCTION IF EXISTS qtest_get_java_boolean ");
  671. runCmd("CREATE FUNCTION qtest_get_java_boolean "
  672. + " AS 'org.apache.hadoop.hive.ql.udf.generic.GenericUDFTestGetJavaBoolean'");
  673. conf.setBoolean("hive.test.init.phase", false);
  674. }
  675. public void init() throws Exception {
  676. // System.out.println(conf.toString());
  677. testWarehouse = conf.getVar(HiveConf.ConfVars.METASTOREWAREHOUSE);
  678. // conf.logVars(System.out);
  679. // System.out.flush();
  680. SessionState.start(conf);
  681. db = Hive.get(conf);
  682. fs = FileSystem.get(conf);
  683. drv = new Driver(conf);
  684. drv.init();
  685. pd = new ParseDriver();
  686. sem = new SemanticAnalyzer(conf);
  687. }
  688. public void init(String tname) throws Exception {
  689. cleanUp();
  690. createSources();
  691. LinkedList<String> cols = new LinkedList<String>();
  692. cols.add("key");
  693. cols.add("value");
  694. LinkedList<String> part_cols = new LinkedList<String>();
  695. part_cols.add("ds");
  696. part_cols.add("hr");
  697. db.createTable("dest1", cols, null, TextInputFormat.class,
  698. IgnoreKeyTextOutputFormat.class);
  699. db.createTable("dest2", cols, null, TextInputFormat.class,
  700. IgnoreKeyTextOutputFormat.class);
  701. db.createTable("dest3", cols, part_cols, TextInputFormat.class,
  702. IgnoreKeyTextOutputFormat.class);
  703. Table dest3 = db.getTable("dest3");
  704. HashMap<String, String> part_spec = new HashMap<String, String>();
  705. part_spec.put("ds", "2008-04-08");
  706. part_spec.put("hr", "12");
  707. db.createPartition(dest3, part_spec);
  708. db.createTable("dest4", cols, null, TextInputFormat.class,
  709. IgnoreKeyTextOutputFormat.class);
  710. db.createTable("dest4_sequencefile", cols, null,
  711. SequenceFileInputFormat.class, SequenceFileOutputFormat.class);
  712. }
  713. public void cliInit(String tname) throws Exception {
  714. cliInit(tname, true);
  715. }
  716. public String cliInit(String tname, boolean recreate) throws Exception {
  717. if (recreate) {
  718. cleanUp();
  719. createSources();
  720. }
  721. HiveConf.setVar(conf, HiveConf.ConfVars.HIVE_AUTHENTICATOR_MANAGER,
  722. "org.apache.hadoop.hive.ql.security.DummyAuthenticator");
  723. Utilities.clearWorkMap();
  724. CliSessionState ss = new CliSessionState(conf);
  725. assert ss != null;
  726. ss.in = System.in;
  727. String stdoutName = null;
  728. if (outDir != null) {
  729. // TODO: why is this needed?
  730. File qf = new File(outDir, tname);
  731. stdoutName = qf.getName().concat(".out");
  732. } else {
  733. stdoutName = tname + ".out";
  734. }
  735. File outf = new File(logDir, stdoutName);
  736. OutputStream fo = new BufferedOutputStream(new FileOutputStream(outf));
  737. if (qSortQuerySet.contains(tname)) {
  738. ss.out = new SortPrintStream(fo, "UTF-8");
  739. } else if (qHashQuerySet.contains(tname)) {
  740. ss.out = new DigestPrintStream(fo, "UTF-8");
  741. } else if (qSortNHashQuerySet.contains(tname)) {
  742. ss.out = new SortAndDigestPrintStream(fo, "UTF-8");
  743. } else {
  744. ss.out = new PrintStream(fo, true, "UTF-8");
  745. }
  746. ss.err = new CachingPrintStream(fo, true, "UTF-8");
  747. ss.setIsSilent(true);
  748. SessionState oldSs = SessionState.get();
  749. if (oldSs != null && clusterType == MiniClusterType.tez) {
  750. oldSs.close();
  751. }
  752. if (oldSs != null && oldSs.out != null && oldSs.out != System.out) {
  753. oldSs.out.close();
  754. }
  755. SessionState.start(ss);
  756. cliDriver = new CliDriver();
  757. if (tname.equals("init_file.q")) {
  758. ss.initFiles.add("../../data/scripts/test_init_file.sql");
  759. }
  760. cliDriver.processInitFiles(ss);
  761. return outf.getAbsolutePath();
  762. }
  763. private CliSessionState startSessionState()
  764. throws FileNotFoundException, UnsupportedEncodingException {
  765. HiveConf.setVar(conf, HiveConf.ConfVars.HIVE_AUTHENTICATOR_MANAGER,
  766. "org.apache.hadoop.hive.ql.security.DummyAuthenticator");
  767. CliSessionState ss = new CliSessionState(conf);
  768. assert ss != null;
  769. SessionState.start(ss);
  770. return ss;
  771. }
  772. public int executeOne(String tname) {
  773. String q = qMap.get(tname);
  774. if (q.indexOf(";") == -1) {
  775. return -1;
  776. }
  777. String q1 = q.substring(0, q.indexOf(";") + 1);
  778. String qrest = q.substring(q.indexOf(";") + 1);
  779. qMap.put(tname, qrest);
  780. System.out.println("Executing " + q1);
  781. return cliDriver.processLine(q1);
  782. }
  783. public int execute(String tname) {
  784. try {
  785. return drv.run(qMap.get(tname)).getResponseCode();
  786. } catch (CommandNeedRetryException e) {
  787. // TODO Auto-generated catch block
  788. e.printStackTrace();
  789. return -1;
  790. }
  791. }
  792. private static final String CRLF = System.getProperty("line.separator");
  793. public int executeClient(String tname1, String tname2) {
  794. String commands = getCommands(tname1) + CRLF + getCommands(tname2);
  795. return cliDriver.processLine(commands);
  796. }
  797. public int executeClient(String tname) {
  798. return cliDriver.processLine(getCommands(tname));
  799. }
  800. private String getCommands(String tname) {
  801. String commands = qMap.get(tname);
  802. StringBuilder newCommands = new StringBuilder(commands.length());
  803. int lastMatchEnd = 0;
  804. Matcher commentMatcher = Pattern.compile("^--.*$", Pattern.MULTILINE).matcher(commands);
  805. while (commentMatcher.find()) {
  806. newCommands.append(commands.substring(lastMatchEnd, commentMatcher.start()));
  807. newCommands.append(commentMatcher.group().replaceAll("(?<!\\\\);", "\\\\;"));
  808. lastMatchEnd = commentMatcher.end();
  809. }
  810. newCommands.append(commands.substring(lastMatchEnd, commands.length()));
  811. commands = newCommands.toString();
  812. return commands;
  813. }
  814. public boolean shouldBeSkipped(String tname) {
  815. return qSkipSet.contains(tname);
  816. }
  817. public void convertSequenceFileToTextFile() throws Exception {
  818. // Create an instance of hive in order to create the tables
  819. testWarehouse = conf.getVar(HiveConf.ConfVars.METASTOREWAREHOUSE);
  820. db = Hive.get(conf);
  821. // Create dest4 to replace dest4_sequencefile
  822. LinkedList<String> cols = new LinkedList<String>();
  823. cols.add("key");
  824. cols.add("value");
  825. // Move all data from dest4_sequencefile to dest4
  826. drv
  827. .run("FROM dest4_sequencefile INSERT OVERWRITE TABLE dest4 SELECT dest4_sequencefile.*");
  828. // Drop dest4_sequencefile
  829. db.dropTable(MetaStoreUtils.DEFAULT_DATABASE_NAME, "dest4_sequencefile",
  830. true, true);
  831. }
  832. public int checkNegativeResults(String tname, Exception e) throws Exception {
  833. File qf = new File(outDir, tname);
  834. String expf = outPath(outDir.toString(), tname.concat(".out"));
  835. File outf = null;
  836. outf = new File(logDir);
  837. outf = new File(outf, qf.getName().concat(".out"));
  838. FileWriter outfd = new FileWriter(outf);
  839. if (e instanceof ParseException) {
  840. outfd.write("Parse Error: ");
  841. } else if (e instanceof SemanticException) {
  842. outfd.write("Semantic Exception: \n");
  843. } else {
  844. throw e;
  845. }
  846. outfd.write(e.getMessage());
  847. outfd.close();
  848. int exitVal = executeDiffCommand(outf.getPath(), expf, false,
  849. qSortSet.contains(qf.getName()));
  850. if (exitVal != 0 && overWrite) {
  851. exitVal = overwriteResults(outf.getPath(), expf);
  852. }
  853. return exitVal;
  854. }
  855. public int checkParseResults(String tname, ASTNode tree) throws Exception {
  856. if (tree != null) {
  857. File parseDir = new File(outDir, "parse");
  858. String expf = outPath(parseDir.toString(), tname.concat(".out"));
  859. File outf = null;
  860. outf = new File(logDir);
  861. outf = new File(outf, tname.concat(".out"));
  862. FileWriter outfd = new FileWriter(outf);
  863. outfd.write(tree.toStringTree());
  864. outfd.close();
  865. int exitVal = executeDiffCommand(outf.getPath(), expf, false, false);
  866. if (exitVal != 0 && overWrite) {
  867. exitVal = overwriteResults(outf.getPath(), expf);
  868. }
  869. return exitVal;
  870. } else {
  871. throw new Exception("Parse tree is null");
  872. }
  873. }
  874. private final Pattern[] xmlPlanMask = toPattern(new String[] {
  875. "<java version=\".*\" class=\"java.beans.XMLDecoder\">",
  876. "<string>.*/tmp/.*</string>",
  877. "<string>file:.*</string>",
  878. "<string>pfile:.*</string>",
  879. "<string>[0-9]{10}</string>",
  880. "<string>/.*/warehouse/.*</string>"
  881. });
  882. public int checkPlan(String tname, List<Task<? extends Serializable>> tasks) throws Exception {
  883. if (tasks == null) {
  884. throw new Exception("Plan is null");
  885. }
  886. File planDir = new File(outDir, "plan");
  887. String planFile = outPath(planDir.toString(), tname + ".xml");
  888. File outf = null;
  889. outf = new File(logDir);
  890. outf = new File(outf, tname.concat(".xml"));
  891. FileOutputStream ofs = new FileOutputStream(outf);
  892. try {
  893. conf.set(HiveConf.ConfVars.PLAN_SERIALIZATION.varname, "javaXML");
  894. for (Task<? extends Serializable> plan : tasks) {
  895. Utilities.serializePlan(plan, ofs, conf);
  896. }
  897. fixXml4JDK7(outf.getPath());
  898. maskPatterns(xmlPlanMask, outf.getPath());
  899. int exitVal = executeDiffCommand(outf.getPath(), planFile, true, false);
  900. if (exitVal != 0 && overWrite) {
  901. exitVal = overwriteResults(outf.getPath(), planFile);
  902. }
  903. return exitVal;
  904. } finally {
  905. conf.set(HiveConf.ConfVars.PLAN_SERIALIZATION.varname, "kryo");
  906. }
  907. }
  908. /**
  909. * Given the current configurations (e.g., hadoop version and execution mode), return
  910. * the correct file name to compare with the current test run output.
  911. * @param outDir The directory where the reference log files are stored.
  912. * @param testName The test file name (terminated by ".out").
  913. * @return The file name appended with the configuration values if it exists.
  914. */
  915. public String outPath(String outDir, String testName) {
  916. String ret = (new File(outDir, testName)).getPath();
  917. // List of configurations. Currently the list consists of hadoop version and execution mode only
  918. List<String> configs = new ArrayList<String>();
  919. configs.add(this.hadoopVer);
  920. Deque<String> stack = new LinkedList<String>();
  921. StringBuilder sb = new StringBuilder();
  922. sb.append(testName);
  923. stack.push(sb.toString());
  924. // example file names are input1.q.out_0.20.0_minimr or input2.q.out_0.17
  925. for (String s: configs) {
  926. sb.append('_');
  927. sb.append(s);
  928. stack.push(sb.toString());
  929. }
  930. while (stack.size() > 0) {
  931. String fileName = stack.pop();
  932. File f = new File(outDir, fileName);
  933. if (f.exists()) {
  934. ret = f.getPath();
  935. break;
  936. }
  937. }
  938. return ret;
  939. }
  940. /**
  941. * Fix the XML generated by JDK7 which is slightly different from what's generated by JDK6,
  942. * causing 40+ test failures. There are mainly two problems:
  943. *
  944. * 1. object element's properties, id and class, are in reverse order, i.e.
  945. * <object class="org.apache.hadoop.hive.ql.exec.MapRedTask" id="MapRedTask0">
  946. * which needs to be fixed to
  947. * <object id="MapRedTask0" class="org.apache.hadoop.hive.ql.exec.MapRedTask">
  948. * 2. JDK introduces Enum as class, i.e.
  949. * <object id="GenericUDAFEvaluator$Mode0" class="java.lang.Enum">
  950. * <class>org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator$Mode</class>
  951. * which needs to be fixed to
  952. * <object id="GenericUDAFEvaluator$Mode0" class="org.apache.hadoop.hive.ql.udf.generic.GenericUDAFEvaluator$Mode"
  953. * method="valueOf">
  954. *
  955. * Though not elegant, this allows these test cases to pass until we have a better serialization mechanism.
  956. *
  957. * Did I mention this is test code?
  958. *
  959. * @param fname the name of the file to fix
  960. * @throws Exception in case of IO error
  961. */
  962. private static void fixXml4JDK7(String fname) throws Exception {
  963. String version = System.getProperty("java.version");
  964. if (!version.startsWith("1.7")) {
  965. return;
  966. }
  967. BufferedReader in = new BufferedReader(new FileReader(fname));
  968. BufferedWriter out = new BufferedWriter(new FileWriter(fname + ".orig"));
  969. String line = null;
  970. while (null != (line = in.readLine())) {
  971. out.write(line);
  972. out.write('\n');
  973. }
  974. in.close();
  975. out.close();
  976. in = new BufferedReader(new FileReader(fname + ".orig"));
  977. out = new BufferedWriter(new FileWriter(fname));
  978. while (null != (line = in.readLine())) {
  979. if (line.indexOf("<object ") == -1 || line.indexOf("class=") == -1) {
  980. out.write(line);
  981. } else {
  982. StringBuilder sb = new StringBuilder();
  983. String prefix = line.substring(0, line.indexOf("<object") + 7);
  984. sb.append( prefix );
  985. String postfix = line.substring(line.lastIndexOf('"') + 1);
  986. String id = getPropertyValue(line, "id");
  987. if (id != null) {
  988. sb.append(" id=" + id);
  989. }
  990. String cls = getPropertyValue(line, "class");
  991. assert(cls != null);
  992. if (cls.equals("\"java.lang.Enum\"")) {
  993. line = in.readLine();
  994. cls = "\"" + getElementValue(line, "class") + "\"";
  995. sb.append(" class=" + cls + " method=\"valueOf\"" );
  996. } else {
  997. sb.append(" class=" + cls);
  998. }
  999. sb.append(postfix);
  1000. out.write(sb.toString());
  1001. }
  1002. out.write('\n');
  1003. }
  1004. in.close();
  1005. out.close();
  1006. }
  1007. /**
  1008. * Get the value of a property in line. The returned value has original quotes
  1009. */
  1010. private static String getPropertyValue(String line, String name) {
  1011. int start = line.indexOf( name + "=" );
  1012. if (start == -1) {
  1013. return null;
  1014. }
  1015. start += name.length() + 1;
  1016. int end = line.indexOf("\"", start + 1);
  1017. return line.substring( start, end + 1 );
  1018. }
  1019. /**
  1020. * Get the value of the element in input. (Note: the returned value has no quotes.)
  1021. */
  1022. private static String getElementValue(String line, String name) {
  1023. assert(line.contains("<" + name + ">"));
  1024. int start = line.indexOf("<" + name + ">") + name.length() + 2;
  1025. int end = line.indexOf("</" + name + ">");
  1026. return line.substring(start, end);
  1027. }
  1028. private Pattern[] toPattern(String[] patternStrs) {
  1029. Pattern[] patterns = new Pattern[patternStrs.length];
  1030. for (int i = 0; i < patternStrs.length; i++) {
  1031. patterns[i] = Pattern.compile(patternStrs[i]);
  1032. }
  1033. return patterns;
  1034. }
  1035. private void maskPatterns(Pattern[] patterns, String fname) throws Exception {
  1036. String maskPattern = "#### A masked pattern was here ####";
  1037. String line;
  1038. BufferedReader in;
  1039. BufferedWriter out;
  1040. File file = new File(fname);
  1041. File fileOrig = new File(fname + ".orig");
  1042. FileUtils.copyFile(file, fileOrig);
  1043. in = new BufferedReader(new InputStreamReader(new FileInputStream(fileOrig), "UTF-8"));
  1044. out = new BufferedWriter(new OutputStreamWriter(new FileOutputStream(file), "UTF-8"));
  1045. boolean lastWasMasked = false;
  1046. while (null != (line = in.readLine())) {
  1047. for (Pattern pattern : patterns) {
  1048. line = pattern.matcher(line).replaceAll(maskPattern);
  1049. }
  1050. if (line.equals(maskPattern)) {
  1051. // We're folding multiple masked lines into one.
  1052. if (!lastWasMasked) {
  1053. out.write(line);
  1054. out.write("\n");
  1055. lastWasMasked = true;
  1056. }
  1057. } else {
  1058. out.write(line);
  1059. out.write("\n");
  1060. lastWasMasked = false;
  1061. }
  1062. }
  1063. in.close();
  1064. out.close();
  1065. }
  1066. private final Pattern[] planMask = toPattern(new String[] {
  1067. ".*file:.*",
  1068. ".*pfile:.*",
  1069. ".*hdfs:.*",
  1070. ".*/tmp/.*",
  1071. ".*invalidscheme:.*",
  1072. ".*lastUpdateTime.*",
  1073. ".*lastAccessTime.*",
  1074. ".*lastModifiedTime.*",
  1075. ".*[Oo]wner.*",
  1076. ".*CreateTime.*",
  1077. ".*LastAccessTime.*",
  1078. ".*Location.*",
  1079. ".*LOCATION '.*",
  1080. ".*transient_lastDdlTime.*",
  1081. ".*last_modified_.*",
  1082. ".*at org.*",
  1083. ".*at sun.*",
  1084. ".*at java.*",
  1085. ".*at junit.*",
  1086. ".*Caused by:.*",
  1087. ".*LOCK_QUERYID:.*",
  1088. ".*LOCK_TIME:.*",
  1089. ".*grantTime.*",
  1090. ".*[.][.][.] [0-9]* more.*",
  1091. ".*job_[0-9_]*.*",
  1092. ".*job_local[0-9_]*.*",
  1093. ".*USING 'java -cp.*",
  1094. "^Deleted.*",
  1095. ".*DagName:.*",
  1096. ".*Input:.*/data/files/.*",
  1097. ".*Output:.*/data/files/.*",
  1098. ".*total number of created files now is.*"
  1099. });
  1100. public int checkCliDriverResults(String tname) throws Exception {
  1101. assert(qMap.containsKey(tname));
  1102. String outFileName = outPath(outDir, tname + ".out");
  1103. File f = new File(logDir, tname + ".out");
  1104. maskPatterns(planMask, f.getPath());
  1105. int exitVal = executeDiffCommand(f.getPath(),
  1106. outFileName, false,
  1107. qSortSet.contains(tname));
  1108. if (exitVal != 0 && overWrite) {
  1109. exitVal = overwriteResults(f.getPath(), outFileName);
  1110. }
  1111. return exitVal;
  1112. }
  1113. public int checkCompareCliDriverResults(String tname, List<String> outputs) throws Exception {
  1114. assert outputs.size() > 1;
  1115. maskPatterns(planMask, outputs.get(0));
  1116. for (int i = 1; i < outputs.size(); ++i) {
  1117. maskPatterns(planMask, outputs.get(i));
  1118. int ecode = executeDiffCommand(
  1119. outputs.get(i - 1), outputs.get(i), false, qSortSet.contains(tname));
  1120. if (ecode != 0) {
  1121. System.out.println("Files don't match: " + outputs.get(i - 1) + " and " + outputs.get(i));
  1122. return ecode;
  1123. }
  1124. }
  1125. return 0;
  1126. }
  1127. private static int overwriteResults(String inFileName, String outFileName) throws Exception {
  1128. // This method can be replaced with Files.copy(source, target, REPLACE_EXISTING)
  1129. // once Hive uses JAVA 7.
  1130. System.out.println("Overwriting results " + inFileName + " to " + outFileName);
  1131. return executeCmd(new String[] {
  1132. "cp",
  1133. getQuotedString(inFileName),
  1134. getQuotedString(outFileName)
  1135. });
  1136. }
  1137. private static int executeDiffCommand(String inFileName,
  1138. String outFileName,
  1139. boolean ignoreWhiteSpace,
  1140. boolean sortResults
  1141. ) throws Exception {
  1142. int result = 0;
  1143. if (sortResults) {
  1144. // sort will try to open the output file in write mode on windows. We need to
  1145. // close it first.
  1146. SessionState ss = SessionState.get();
  1147. if (ss != null && ss.out != null && ss.out != System.out) {
  1148. ss.out.close();
  1149. }
  1150. String inSorted = inFileName + SORT_SUFFIX;
  1151. String outSorted = outFileName + SORT_SUFFIX;
  1152. result = sortFiles(inFileName, inSorted);
  1153. result |= sortFiles(outFileName, outSorted);
  1154. if (result != 0) {
  1155. System.err.println("ERROR: Could not sort files before comparing");
  1156. return result;
  1157. }
  1158. inFileName = inSorted;
  1159. outFileName = outSorted;
  1160. }
  1161. ArrayList<String> diffCommandArgs = new ArrayList<String>();
  1162. diffCommandArgs.add("diff");
  1163. // Text file comparison
  1164. diffCommandArgs.add("-a");
  1165. // Ignore changes in the amount of white space
  1166. if (ignoreWhiteSpace || Shell.WINDOWS) {
  1167. diffCommandArgs.add("-b");
  1168. }
  1169. // Files created on Windows machines have different line endings
  1170. // than files created on Unix/Linux. Windows uses carriage return and line feed
  1171. // ("\r\n") as a line ending, whereas Unix uses just line feed ("\n").
  1172. // Also StringBuilder.toString(), Stream to String conversions adds extra
  1173. // spaces at the end of the line.
  1174. if (Shell.WINDOWS) {
  1175. diffCommandArgs.add("--strip-trailing-cr"); // Strip trailing carriage return on input
  1176. diffCommandArgs.add("-B"); // Ignore changes whose lines are all blank
  1177. }
  1178. // Add files to compare to the arguments list
  1179. diffCommandArgs.add(getQuotedString(inFileName));
  1180. diffCommandArgs.add(getQuotedString(outFileName));
  1181. result = executeCmd(diffCommandArgs);
  1182. if (sortResults) {
  1183. new File(inFileName).delete();
  1184. new File(outFileName).delete();
  1185. }
  1186. return result;
  1187. }
  1188. private static int sortFiles(String in, String out) throws Exception {
  1189. return executeCmd(new String[] {
  1190. "sort",
  1191. getQuotedString(in),
  1192. }, out, null);
  1193. }
  1194. private static int executeCmd(Collection<String> args) throws Exception {
  1195. return executeCmd(args, null, null);
  1196. }
  1197. private static int executeCmd(String[] args) throws Exception {
  1198. return executeCmd(args, null, null);
  1199. }
  1200. private static int executeCmd(Collection<String> args, String outFile, String errFile) throws Exception {
  1201. String[] cmdArray = args.toArray(new String[args.size()]);
  1202. return executeCmd(cmdArray, outFile, errFile);
  1203. }
  1204. private static int executeCmd(String[] args, String outFile, String errFile) throws Exception {
  1205. System.out.println("Running: " + org.apache.commons.lang.StringUtils.join(args, ' '));
  1206. PrintStream out = outFile == null ?
  1207. SessionState.getConsole().getChildOutStream() :
  1208. new PrintStream(new FileOutputStream(outFile), true);
  1209. PrintStream err = errFile == null ?
  1210. SessionState.getConsole().getChildErrStream() :
  1211. new PrintStream(new FileOutputStream(errFile), true);
  1212. Process executor = Runtime.getRuntime().exec(args);
  1213. StreamPrinter errPrinter = new StreamPrinter(executor.getErrorStream(), null, err);
  1214. StreamPrinter outPrinter = new StreamPrinter(executor.getInputStream(), null, out);
  1215. outPrinter.start();
  1216. errPrinter.start();
  1217. int result = executor.waitFor();
  1218. outPrinter.join();
  1219. errPrinter.join();
  1220. if (outFile != null) {
  1221. out.close();
  1222. }
  1223. if (errFile != null) {
  1224. err.close();
  1225. }
  1226. return result;
  1227. }
  1228. private static String getQuotedString(String str){
  1229. return Shell.WINDOWS ? String.format("\"%s\"", str) : str;
  1230. }
  1231. public ASTNode parseQuery(String tname) throws Exception {
  1232. return pd.parse(qMap.get(tname));
  1233. }
  1234. public void resetParser() throws SemanticException {
  1235. drv.init();
  1236. pd = new ParseDriver();
  1237. sem = new SemanticAnalyzer(conf);
  1238. }
  1239. public List<Task<? extends Serializable>> analyzeAST(ASTNode ast) throws Exception {
  1240. // Do semantic analysis and plan generation
  1241. Context ctx = new Context(conf);
  1242. while ((ast.getToken() == null) && (ast.getChildCount() > 0)) {
  1243. ast = (ASTNode) ast.getChild(0);
  1244. }
  1245. sem.getOutputs().clear();
  1246. sem.getInputs().clear();
  1247. sem.analyze(ast, ctx);
  1248. ctx.clear();
  1249. return sem.getRootTasks();
  1250. }
  1251. public TreeMap<String, String> getQMap() {
  1252. return qMap;
  1253. }
  1254. /**
  1255. * QTestSetup defines test fixtures which are reused across testcases,
  1256. * and are needed before any test can be run
  1257. */
  1258. public static class QTestSetup
  1259. {
  1260. private MiniZooKeeperCluster zooKeeperCluster = null;
  1261. private int zkPort;
  1262. private ZooKeeper zooKeeper;
  1263. public QTestSetup() {
  1264. }
  1265. public void preTest(HiveConf conf) throws Exception {
  1266. if (zooKeeperCluster == null) {
  1267. //create temp dir
  1268. String tmpBaseDir = System.getProperty("test.tmp.dir");
  1269. File tmpDir = Utilities.createTempDir(tmpBaseDir);
  1270. zooKeeperCluster = new MiniZooKeeperCluster();
  1271. zkPort = zooKeeperCluster.startup(tmpDir);
  1272. }
  1273. if (zooKeeper != null) {
  1274. zooKeeper.close();
  1275. }
  1276. int sessionTimeout = conf.getIntVar(HiveConf.ConfVars.HIVE_ZOOKEEPER_SESSION_TIMEOUT);
  1277. zooKeeper = new ZooKeeper("localhost:" + zkPort, sessionTimeout, new Watcher() {
  1278. @Override
  1279. public void process(WatchedEvent arg0) {
  1280. }
  1281. });
  1282. String zkServer = "localhost";
  1283. conf.set("hive.zookeeper.quorum", zkServer);

Large files files are truncated, but you can click here to view the full file