ramitg254 commented on code in PR #6376:
URL: https://github.com/apache/hive/pull/6376#discussion_r3122141405


##########
ql/src/test/org/apache/hadoop/hive/ql/TestCachedResults.java:
##########
@@ -0,0 +1,269 @@
+/*
+ * 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;
+
+import java.io.File;
+import java.io.IOException;
+import java.util.concurrent.Executors;
+import java.util.concurrent.ScheduledExecutorService;
+import java.util.concurrent.TimeUnit;
+
+import org.apache.hadoop.fs.FileSystem;
+import org.apache.hadoop.fs.Path;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.cache.results.QueryResultsCache;
+import org.apache.hadoop.hive.ql.session.SessionState;
+import org.apache.hive.testutils.HiveTestEnvSetup;
+
+import org.junit.After;
+import org.junit.Assert;
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.ClassRule;
+import org.junit.Rule;
+import org.junit.Test;
+
+import org.junit.rules.TestRule;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+
+public class TestCachedResults {
+
+  private static final Logger LOG = 
LoggerFactory.getLogger(TestCachedResults.class);
+
+  @ClassRule
+  public static HiveTestEnvSetup envSetup = new HiveTestEnvSetup();
+
+  @Rule
+  public TestRule methodRule = envSetup.getMethodRule();
+
+  private static HiveConf conf;
+  private static String cacheDir;
+
+  private ScheduledExecutorService scheduler;
+  private long maxCacheSize = 0;
+  private static long maxAllowedCacheSize = 1000000;
+
+
+  @BeforeClass
+  public static void setUp() throws Exception {
+    conf = envSetup.getTestCtx().hiveConf;
+
+    HiveConf.setBoolVar(conf, 
HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_ENABLED, true);
+    HiveConf.setVar(conf, 
HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_DIRECTORY, "/tmp/hive/cache");
+    HiveConf.setBoolVar(conf, 
HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_NONTRANSACTIONAL_TABLES_ENABLED, 
true);
+    HiveConf.setLongVar(conf, 
HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_MAX_SIZE, maxAllowedCacheSize);
+    LOG.info("max allowed cache size : {}", 
conf.getLongVar(HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_MAX_SIZE));
+
+    cacheDir = 
conf.getVar(HiveConf.ConfVars.HIVE_QUERY_RESULTS_CACHE_DIRECTORY);
+
+    HiveConf.setVar(conf, HiveConf.ConfVars.HIVE_AUTHORIZATION_MANAGER,
+        
"org.apache.hadoop.hive.ql.security.authorization.plugin.sqlstd.SQLStdHiveAuthorizerFactory");
+    HiveConf.setBoolVar(conf, HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY, 
false);
+
+    SessionState.start(conf);
+    createAndPopulateTables();
+  }
+
+  public static void createAndPopulateTables() throws Exception {
+    IDriver driver = DriverFactory.newDriver(conf);
+    runQuery(driver, "DROP TABLE IF EXISTS tab");
+    runQuery(driver, "CREATE TABLE tab (id INT)");
+    runQuery(driver,
+        "INSERT INTO TABLE tab " +
+            "SELECT pos + 1 AS id FROM (" +
+            "  SELECT posexplode(split(space(999), ' ')) AS (pos, val)" +
+            ") t"
+    );
+  }
+
+  @AfterClass
+  public static void afterClass() throws Exception {
+    IDriver driver = DriverFactory.newDriver(conf);
+    driver.run("DROP TABLE IF EXISTS tab");
+  }
+
+  @Before
+  public void beforeEach() {
+    scheduler = Executors.newSingleThreadScheduledExecutor();
+    maxCacheSize = 0;
+  }
+
+  @After
+  public void afterEach() throws InterruptedException {
+    QueryResultsCache.cleanupInstance();
+    Path cacheDirPath = new Path(cacheDir);
+    try {
+      FileSystem fs = cacheDirPath.getFileSystem(conf);
+      fs.delete(cacheDirPath, true);
+    } catch (IOException e) {
+      LOG.warn("Failed to clean up cache directory: {}", cacheDir, e);
+    }
+    scheduler.shutdownNow();
+    scheduler.awaitTermination(1, TimeUnit.SECONDS);
+  }
+
+  private void executeQueries(IDriver driver) throws Exception {
+
+    runQuery(driver,
+        "SELECT t1.id, " +
+            "       SUM(t2.id)   OVER (PARTITION BY t1.id % 10 ORDER BY t1.id) 
AS running_sum, " +
+            "       AVG(t2.id)   OVER (PARTITION BY t1.id % 10)                
AS window_avg, " +
+            "       COUNT(t2.id) OVER (PARTITION BY t1.id % 10 ORDER BY t1.id) 
AS running_cnt " +
+            "FROM tab t1 " +
+            "JOIN tab t2 ON t1.id % 10 = t2.id % 10 " +
+            "WHERE t1.id <= 300"
+    );
+    // running same query to check for cache hit while debugging
+    runQuery(driver,
+        "SELECT t1.id, " +
+            "       SUM(t2.id)   OVER (PARTITION BY t1.id % 10 ORDER BY t1.id) 
AS running_sum, " +
+            "       AVG(t2.id)   OVER (PARTITION BY t1.id % 10)                
AS window_avg, " +
+            "       COUNT(t2.id) OVER (PARTITION BY t1.id % 10 ORDER BY t1.id) 
AS running_cnt " +
+            "FROM tab t1 " +
+            "JOIN tab t2 ON t1.id % 10 = t2.id % 10 " +
+            "WHERE t1.id <= 300"
+    );
+    runQuery(driver,
+        "SELECT base.id, base.bucket, agg.bucket_avg " +
+            "FROM ( " +
+            "    SELECT id, id % 10 AS bucket FROM tab WHERE id <= 500 " +
+            ") base " +
+            "JOIN ( " +
+            "    SELECT id % 10 AS bucket, AVG(id) AS bucket_avg, COUNT(*) AS 
cnt " +
+            "    FROM tab " +
+            "    GROUP BY id % 10 " +
+            ") agg ON base.bucket = agg.bucket " +
+            "ORDER BY base.id"
+    );
+    // running same query to check for cache hit while debugging
+    runQuery(driver,
+        "SELECT base.id, base.bucket, agg.bucket_avg " +
+            "FROM ( " +
+            "    SELECT id, id % 10 AS bucket FROM tab WHERE id <= 500 " +
+            ") base " +
+            "JOIN ( " +
+            "    SELECT id % 10 AS bucket, AVG(id) AS bucket_avg, COUNT(*) AS 
cnt " +
+            "    FROM tab " +
+            "    GROUP BY id % 10 " +
+            ") agg ON base.bucket = agg.bucket " +
+            "ORDER BY base.id"
+    );
+    runQuery(driver,
+        "WITH base AS ( " +
+            "    SELECT id, id % 2 AS is_even, id % 5 AS mod5, id % 10 AS 
mod10 " +
+            "    FROM tab " +
+            "), " +
+            "joined AS ( " +
+            "    SELECT a.id AS a_id, b.id AS b_id, " +
+            "           a.mod5, a.mod10, " +
+            "           (a.id * b.id) AS product " +
+            "    FROM base a " +
+            "    JOIN base b ON a.mod5 = b.mod5 AND a.is_even = b.is_even " +
+            "    WHERE a.id <= 200 AND b.id <= 200 " +
+            ") " +
+            "SELECT mod5, mod10, " +
+            "       COUNT(*)      AS cnt, " +
+            "       SUM(product)  AS total_product, " +
+            "       MAX(product)  AS max_product, " +
+            "       MIN(a_id)     AS min_a " +
+            "FROM joined " +
+            "GROUP BY mod5, mod10 " +
+            "ORDER BY mod5, mod10"
+    );
+    // running same query to check for cache hit while debugging
+    runQuery(driver,
+        "WITH base AS ( " +
+            "    SELECT id, id % 2 AS is_even, id % 5 AS mod5, id % 10 AS 
mod10 " +
+            "    FROM tab " +
+            "), " +
+            "joined AS ( " +
+            "    SELECT a.id AS a_id, b.id AS b_id, " +
+            "           a.mod5, a.mod10, " +
+            "           (a.id * b.id) AS product " +
+            "    FROM base a " +
+            "    JOIN base b ON a.mod5 = b.mod5 AND a.is_even = b.is_even " +
+            "    WHERE a.id <= 200 AND b.id <= 200 " +
+            ") " +
+            "SELECT mod5, mod10, " +
+            "       COUNT(*)      AS cnt, " +
+            "       SUM(product)  AS total_product, " +
+            "       MAX(product)  AS max_product, " +
+            "       MIN(a_id)     AS min_a " +
+            "FROM joined " +
+            "GROUP BY mod5, mod10 " +
+            "ORDER BY mod5, mod10"
+    );
+  }
+
+  @Test
+  public void testSafeCacheWrite() throws Exception {
+    HiveConf.setBoolVar(conf, 
HiveConf.ConfVars.HIVE_QUERY_RESULTS_SAFE_CACHE_WRITE_ENABLED, true);

Review Comment:
   done



-- 
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.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to