aboutsummaryrefslogtreecommitdiff
path: root/exec/java-exec/src/test/java/org/apache/drill/exec/sql/TestAnalyze.java
blob: 04e9b99aa354f92b8ab689020b77e2f726cd61a6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
/*
 * 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.drill.exec.sql;

import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import org.apache.drill.PlanTestBase;
import org.apache.drill.exec.ExecConstants;
import org.apache.drill.exec.proto.UserBitShared.QueryType;
import org.apache.drill.exec.record.RecordBatchLoader;
import org.apache.drill.exec.record.VectorWrapper;
import org.apache.drill.exec.rpc.user.QueryDataBatch;
import org.apache.drill.exec.vector.ValueVector;
import org.apache.drill.test.BaseTestQuery;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import org.junit.BeforeClass;
import org.junit.Ignore;
import org.junit.Test;

public class TestAnalyze extends BaseTestQuery {

  @BeforeClass
  public static void copyData() throws Exception {
    dirTestWatcher.copyResourceToRoot(Paths.get("multilevel", "parquet"));
  }

  // Analyze for all columns
  @Test
  public void basic1() throws Exception {
    try {
      test("ALTER SESSION SET `planner.slice_target` = 1");
      test("ALTER SESSION SET `store.format` = 'parquet'");
      test("CREATE TABLE dfs.tmp.region_basic1 AS SELECT * from cp.`region.json`");
      test("ANALYZE TABLE dfs.tmp.region_basic1 COMPUTE STATISTICS");
      test("SELECT * FROM dfs.tmp.`region_basic1/.stats.drill`");
      test("create table dfs.tmp.flatstats1 as select flatten(`directories`[0].`columns`) as `columns`"
              + " from dfs.tmp.`region_basic1/.stats.drill`");

      testBuilder()
          .sqlQuery("SELECT tbl.`columns`.`column` as `column`, tbl.`columns`.rowcount as rowcount,"
              + " tbl.`columns`.nonnullrowcount as nonnullrowcount, tbl.`columns`.ndv as ndv,"
              + " tbl.`columns`.avgwidth as avgwidth"
              + " FROM dfs.tmp.flatstats1 tbl")
          .unOrdered()
          .baselineColumns("column", "rowcount", "nonnullrowcount", "ndv", "avgwidth")
          .baselineValues("`region_id`", 110.0, 110.0, 110L, 8.0)
          .baselineValues("`sales_city`", 110.0, 110.0, 109L, 8.663636363636364)
          .baselineValues("`sales_state_province`", 110.0, 110.0, 13L, 2.4272727272727272)
          .baselineValues("`sales_district`", 110.0, 110.0, 23L, 9.318181818181818)
          .baselineValues("`sales_region`", 110.0, 110.0, 8L, 10.8)
          .baselineValues("`sales_country`", 110.0, 110.0, 4L, 3.909090909090909)
          .baselineValues("`sales_district_id`", 110.0, 110.0, 23L, 8.0)
          .go();
    } finally {
      test("ALTER SESSION SET `planner.slice_target` = " + ExecConstants.SLICE_TARGET_DEFAULT);
    }
  }

  // Analyze for only a subset of the columns in table
  @Test
  public void basic2() throws Exception {
    try {
      test("ALTER SESSION SET `planner.slice_target` = 1");
      test("ALTER SESSION SET `store.format` = 'parquet'");
      test("CREATE TABLE dfs.tmp.employee_basic2 AS SELECT * from cp.`employee.json`");
      test("ANALYZE TABLE dfs.tmp.employee_basic2 COMPUTE STATISTICS (employee_id, birth_date)");
      test("SELECT * FROM dfs.tmp.`employee_basic2/.stats.drill`");
      test("create table dfs.tmp.flatstats2 as select flatten(`directories`[0].`columns`) as `columns`"
          + " from dfs.tmp.`employee_basic2/.stats.drill`");

      testBuilder()
          .sqlQuery("SELECT tbl.`columns`.`column` as `column`, tbl.`columns`.rowcount as rowcount,"
              + " tbl.`columns`.nonnullrowcount as nonnullrowcount, tbl.`columns`.ndv as ndv,"
              + " tbl.`columns`.avgwidth as avgwidth"
              + " FROM dfs.tmp.flatstats2 tbl")
          .unOrdered()
          .baselineColumns("column", "rowcount", "nonnullrowcount", "ndv", "avgwidth")
          .baselineValues("`employee_id`", 1155.0, 1155.0, 1155L, 8.0)
          .baselineValues("`birth_date`", 1155.0, 1155.0, 52L, 10.0)
          .go();
    } finally {
      test("ALTER SESSION SET `planner.slice_target` = " + ExecConstants.SLICE_TARGET_DEFAULT);
    }
  }

  // Analyze with sampling percentage
  @Test
  public void basic3() throws Exception {
    try {
      test("ALTER SESSION SET `planner.slice_target` = 1");
      test("ALTER SESSION SET `store.format` = 'parquet'");
      test("ALTER SESSION SET `exec.statistics.deterministic_sampling` = true");
      test("CREATE TABLE dfs.tmp.employee_basic3 AS SELECT * from cp.`employee.json`");
      test("ANALYZE TABLE dfs.tmp.employee_basic3 COMPUTE STATISTICS (employee_id, birth_date) SAMPLE 55 PERCENT");
      test("SELECT * FROM dfs.tmp.`employee_basic3/.stats.drill`");
      test("create table dfs.tmp.flatstats3 as select flatten(`directories`[0].`columns`) as `columns`"
              + " from dfs.tmp.`employee_basic3/.stats.drill`");

      testBuilder()
              .sqlQuery("SELECT tbl.`columns`.`column` as `column`, tbl.`columns`.rowcount as rowcount,"
                      + " tbl.`columns`.nonnullrowcount as nonnullrowcount, tbl.`columns`.ndv as ndv,"
                      + " tbl.`columns`.avgwidth as avgwidth"
                      + " FROM dfs.tmp.flatstats3 tbl")
              .unOrdered()
              .baselineColumns("column", "rowcount", "nonnullrowcount", "ndv", "avgwidth")
              .baselineValues("`employee_id`", 1138.0, 1138.0, 1138L, 8.00127815945039)
              .baselineValues("`birth_date`", 1138.0, 1138.0, 38L, 10.001597699312988)
              .go();
    } finally {
      test("ALTER SESSION SET `exec.statistics.deterministic_sampling` = false");
      test("ALTER SESSION SET `planner.slice_target` = " + ExecConstants.SLICE_TARGET_DEFAULT);
    }
  }

  @Test
  public void join() throws Exception {
    try {
      test("ALTER SESSION SET `planner.slice_target` = 1");
      test("ALTER SESSION SET `store.format` = 'parquet'");
      test("CREATE TABLE dfs.tmp.lineitem AS SELECT * FROM cp.`tpch/lineitem.parquet`");
      test("CREATE TABLE dfs.tmp.orders AS select * FROM cp.`tpch/orders.parquet`");
      test("ANALYZE TABLE dfs.tmp.lineitem COMPUTE STATISTICS");
      test("ANALYZE TABLE dfs.tmp.orders COMPUTE STATISTICS");
      test("SELECT * FROM dfs.tmp.`lineitem/.stats.drill`");
      test("SELECT * FROM dfs.tmp.`orders/.stats.drill`");
      test("ALTER SESSION SET `planner.statistics.use` = true");
      test("SELECT * FROM dfs.tmp.`lineitem` l JOIN dfs.tmp.`orders` o ON l.l_orderkey = o.o_orderkey");
    } finally {
      test("ALTER SESSION SET `planner.slice_target` = " + ExecConstants.SLICE_TARGET_DEFAULT);
    }
  }

  @Test
  public void testAnalyzeSupportedFormats() throws Exception {
    //Only allow computing statistics on PARQUET files.
    try {
      test("ALTER SESSION SET `planner.slice_target` = 1");
      test("ALTER SESSION SET `store.format` = 'json'");
      test("CREATE TABLE dfs.tmp.employee_basic4 AS SELECT * from cp.`employee.json`");
      //Should display not supported
      verifyAnalyzeOutput("ANALYZE TABLE dfs.tmp.employee_basic4 COMPUTE STATISTICS",
          "Table employee_basic4 is not supported by ANALYZE. "
          + "Support is currently limited to directory-based Parquet tables.");

      test("DROP TABLE dfs.tmp.employee_basic4");
      test("ALTER SESSION SET `store.format` = 'parquet'");
      test("CREATE TABLE dfs.tmp.employee_basic4 AS SELECT * from cp.`employee.json`");
      //Should complete successfully (16 columns in employee.json)
      verifyAnalyzeOutput("ANALYZE TABLE dfs.tmp.employee_basic4 COMPUTE STATISTICS",
          "16");
    } finally {
      test("ALTER SESSION SET `planner.slice_target` = " + ExecConstants.SLICE_TARGET_DEFAULT);
    }
  }

  @Test
  public void testAnalyzePartitionedTables() throws Exception {
    //Computing statistics on columns, dir0, dir1
    try {
      final String tmpLocation = "/multilevel/parquet";
      test("ALTER SESSION SET `planner.slice_target` = 1");
      test("ALTER SESSION SET `store.format` = 'parquet'");
      test("CREATE TABLE dfs.tmp.parquet1 AS SELECT * from dfs.`%s`", tmpLocation);
      verifyAnalyzeOutput("ANALYZE TABLE dfs.tmp.parquet1 COMPUTE STATISTICS", "11");
      test("SELECT * FROM dfs.tmp.`parquet1/.stats.drill`");
      test("create table dfs.tmp.flatstats4 as select flatten(`directories`[0].`columns`) as `columns` " +
           "from dfs.tmp.`parquet1/.stats.drill`");
      //Verify statistics
      testBuilder()
          .sqlQuery("SELECT tbl.`columns`.`column` as `column`, tbl.`columns`.rowcount as rowcount,"
              + " tbl.`columns`.nonnullrowcount as nonnullrowcount, tbl.`columns`.ndv as ndv,"
              + " tbl.`columns`.avgwidth as avgwidth"
              + " FROM dfs.tmp.flatstats4 tbl")
          .unOrdered()
          .baselineColumns("column", "rowcount", "nonnullrowcount", "ndv", "avgwidth")
          .baselineValues("`o_orderkey`", 120.0, 120.0, 119L, 4.0)
          .baselineValues("`o_custkey`", 120.0, 120.0, 113L, 4.0)
          .baselineValues("`o_orderstatus`", 120.0, 120.0, 3L, 1.0)
          .baselineValues("`o_totalprice`", 120.0, 120.0, 120L, 8.0)
          .baselineValues("`o_orderdate`", 120.0, 120.0, 111L, 4.0)
          .baselineValues("`o_orderpriority`", 120.0, 120.0, 5L, 8.458333333333334)
          .baselineValues("`o_clerk`", 120.0, 120.0, 114L, 15.0)
          .baselineValues("`o_shippriority`", 120.0, 120.0, 1L, 4.0)
          .baselineValues("`o_comment`", 120.0, 120.0, 120L, 46.333333333333336)
          .baselineValues("`dir0`", 120.0, 120.0, 3L, 4.0)
          .baselineValues("`dir1`", 120.0, 120.0, 4L, 2.0)
          .go();
    } finally {
      test("ALTER SESSION SET `planner.slice_target` = " + ExecConstants.SLICE_TARGET_DEFAULT);
    }
  }

  @Test
  public void testStaleness() throws Exception {
    // copy the data into the temporary location
    final String tmpLocation = "/multilevel/parquet";
    test("ALTER SESSION SET `planner.slice_target` = 1");
    test("ALTER SESSION SET `store.format` = 'parquet'");
    test("CREATE TABLE dfs.tmp.parquetStale AS SELECT o_orderkey, o_custkey, o_orderstatus, " +
         "o_totalprice, o_orderdate, o_orderpriority, o_clerk, o_shippriority, o_comment from dfs.`%s`", tmpLocation);
    verifyAnalyzeOutput("ANALYZE TABLE dfs.tmp.parquetStale COMPUTE STATISTICS", "9");
    verifyAnalyzeOutput("ANALYZE TABLE dfs.tmp.parquetStale COMPUTE STATISTICS",
        "Table parquetStale has not changed since last ANALYZE!");
    // Verify we recompute statistics once a new file/directory is added. Update the directory some
    // time after ANALYZE so that the timestamps are different.
    Thread.sleep(1000);
    final String Q4 = "/multilevel/parquet/1996/Q4";
    test("CREATE TABLE dfs.tmp.`parquetStale/1996/Q5` AS SELECT o_orderkey, o_custkey, o_orderstatus, " +
         "o_totalprice, o_orderdate, o_orderpriority, o_clerk, o_shippriority, o_comment from dfs.`%s`", Q4);
    verifyAnalyzeOutput("ANALYZE TABLE dfs.tmp.parquetStale COMPUTE STATISTICS", "9");
    Thread.sleep(1000);
    test("DROP TABLE dfs.tmp.`parquetStale/1996/Q5`");
    verifyAnalyzeOutput("ANALYZE TABLE dfs.tmp.parquetStale COMPUTE STATISTICS", "9");
  }

  @Test
  public void testUseStatistics() throws Exception {
    //Test ndv/rowcount for scan
    test("ALTER SESSION SET `planner.slice_target` = 1");
    test("ALTER SESSION SET `store.format` = 'parquet'");
    test("CREATE TABLE dfs.tmp.employeeUseStat AS SELECT * from cp.`employee.json`");
    test("CREATE TABLE dfs.tmp.departmentUseStat AS SELECT * from cp.`department.json`");
    test("ANALYZE TABLE dfs.tmp.employeeUseStat COMPUTE STATISTICS");
    test("ANALYZE TABLE dfs.tmp.departmentUseStat COMPUTE STATISTICS");
    test("ALTER SESSION SET `planner.statistics.use` = true");
    String query = " select employee_id from dfs.tmp.employeeUseStat where department_id = 2";
    String[] expectedPlan1 = {"Filter\\(condition.*\\).*rowcount = 96.25,.*",
            "Scan.*columns=\\[`department_id`, `employee_id`\\].*rowcount = 1155.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan1, new String[]{});

    query = " select employee_id from dfs.tmp.employeeUseStat where department_id IN (2, 5)";
    String[] expectedPlan2 = {"Filter\\(condition.*\\).*rowcount = 192.5,.*",
            "Scan.*columns=\\[`department_id`, `employee_id`\\].*rowcount = 1155.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan2, new String[]{});

    query = "select employee_id from dfs.tmp.employeeUseStat where department_id IN (2, 5) and employee_id = 5";
    String[] expectedPlan3 = {"Filter\\(condition.*\\).*rowcount = 1.0,.*",
            "Scan.*columns=\\[`department_id`, `employee_id`\\].*rowcount = 1155.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan3, new String[]{});

    query = " select emp.employee_id from dfs.tmp.employeeUseStat emp join dfs.tmp.departmentUseStat dept"
        + " on emp.department_id = dept.department_id";
    String[] expectedPlan4 = {"HashJoin\\(condition.*\\).*rowcount = 1154.9999999999995,.*",
            "Scan.*columns=\\[`department_id`, `employee_id`\\].*rowcount = 1155.0.*",
            "Scan.*columns=\\[`department_id`\\].*rowcount = 12.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan4, new String[]{});

    query = " select emp.employee_id from dfs.tmp.employeeUseStat emp join dfs.tmp.departmentUseStat dept"
            + " on emp.department_id = dept.department_id where dept.department_id = 5";
    String[] expectedPlan5 = {"HashJoin\\(condition.*\\).*rowcount = 96.24999999999997,.*",
            "Scan.*columns=\\[`department_id`, `employee_id`\\].*rowcount = 1155.0.*",
            "Scan.*columns=\\[`department_id`\\].*rowcount = 12.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan5, new String[]{});

    query = " select emp.employee_id from dfs.tmp.employeeUseStat emp join dfs.tmp.departmentUseStat dept"
            + " on emp.department_id = dept.department_id"
            + " where dept.department_id = 5 and emp.employee_id = 10";
    String[] expectedPlan6 = {"MergeJoin\\(condition.*\\).*rowcount = 1.0,.*",
            "Filter\\(condition=\\[AND\\(=\\(\\$1, 10\\), =\\(\\$0, 5\\)\\)\\]\\).*rowcount = 1.0,.*",
            "Scan.*columns=\\[`department_id`, `employee_id`\\].*rowcount = 1155.0.*",
            "Filter\\(condition=\\[=\\(\\$0, 5\\)\\]\\).*rowcount = 1.0,.*",
            "Scan.*columns=\\[`department_id`\\].*rowcount = 12.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan6, new String[]{});

    query = " select emp.employee_id, count(*)"
            + " from dfs.tmp.employeeUseStat emp"
            + " group by emp.employee_id";
    String[] expectedPlan7 = {"HashAgg\\(group=\\[\\{0\\}\\], EXPR\\$1=\\[COUNT\\(\\)\\]\\).*rowcount = 1155.0,.*",
            "Scan.*columns=\\[`employee_id`\\].*rowcount = 1155.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan7, new String[]{});

    query = " select emp.employee_id from dfs.tmp.employeeUseStat emp join dfs.tmp.departmentUseStat dept"
            + " on emp.department_id = dept.department_id "
            + " group by emp.employee_id";
    String[] expectedPlan8 = {"HashAgg\\(group=\\[\\{0\\}\\]\\).*rowcount = 730.0992454469839,.*",
            "HashJoin\\(condition.*\\).*rowcount = 1154.9999999999995,.*",
            "Scan.*columns=\\[`department_id`, `employee_id`\\].*rowcount = 1155.0.*",
            "Scan.*columns=\\[`department_id`\\].*rowcount = 12.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan8, new String[]{});

    query = "select emp.employee_id, dept.department_description"
            + " from dfs.tmp.employeeUseStat emp join dfs.tmp.departmentUseStat dept"
            + " on emp.department_id = dept.department_id "
            + " group by emp.employee_id, emp.store_id, dept.department_description "
            + " having dept.department_description = 'FINANCE'";
    String[] expectedPlan9 = {"HashAgg\\(group=\\[\\{0, 1, 2\\}\\]\\).*rowcount = 92.3487011031316.*",
            "HashJoin\\(condition.*\\).*rowcount = 96.24999999999997,.*",
            "Scan.*columns=\\[`department_id`, `employee_id`, `store_id`\\].*rowcount = 1155.0.*",
            "Filter\\(condition=\\[=\\(\\$1, 'FINANCE'\\)\\]\\).*rowcount = 1.0,.*",
            "Scan.*columns=\\[`department_id`, `department_description`\\].*rowcount = 12.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan9, new String[]{});

    query = " select emp.employee_id from dfs.tmp.employeeUseStat emp join dfs.tmp.departmentUseStat dept\n"
            + " on emp.department_id = dept.department_id "
            + " group by emp.employee_id, emp.store_id "
            + " having emp.store_id = 7";
    String[] expectedPlan10 = {"HashAgg\\(group=\\[\\{0, 1\\}\\]\\).*rowcount = 29.203969817879365.*",
            "HashJoin\\(condition.*\\).*rowcount = 46.2,.*",
            "Filter\\(condition=\\[=\\(\\$2, 7\\)\\]\\).*rowcount = 46.2,.*",
            "Scan.*columns=\\[`department_id`, `employee_id`, `store_id`\\].*rowcount = 1155.0.*",
            "Scan.*columns=\\[`department_id`\\].*rowcount = 12.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan10, new String[]{});

    query = " select emp.employee_id from dfs.tmp.employeeUseStat emp join dfs.tmp.departmentUseStat dept\n"
            + " on emp.department_id = dept.department_id "
            + " group by emp.employee_id "
            + " having emp.employee_id = 7";
    String[] expectedPlan11 = {"StreamAgg\\(group=\\[\\{0\\}\\]\\).*rowcount = 1.0.*",
            "HashJoin\\(condition.*\\).*rowcount = 1.0,.*",
            "Filter\\(condition=\\[=\\(\\$1, 7\\)\\]\\).*rowcount = 1.0.*",
            "Scan.*columns=\\[`department_id`\\].*rowcount = 12.0.*",
            "Scan.*columns=\\[`department_id`, `employee_id`\\].*rowcount = 1155.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan11, new String[]{});
  }

  @Ignore("Fails intermittently. Enable after fixing the issue.")
  @Test
  public void testWithMetadataCaching() throws Exception {
    test("ALTER SESSION SET `planner.slice_target` = 1");
    test("ALTER SESSION SET `store.format` = 'parquet'");
    test("ALTER SESSION SET `planner.statistics.use` = true");
    final String tmpLocation = "/multilevel/parquet";
    // copy the data into the temporary location
    test("DROP TABLE dfs.tmp.parquetStale");
    test("CREATE TABLE dfs.tmp.parquetStale AS SELECT o_orderkey, o_custkey, o_orderstatus, " +
            "o_totalprice, o_orderdate, o_orderpriority, o_clerk, o_shippriority, o_comment from dfs.`%s`", tmpLocation);
    String query = "select count(distinct o_orderkey) from dfs.tmp.parquetStale";
    verifyAnalyzeOutput("ANALYZE TABLE dfs.tmp.parquetStale COMPUTE STATISTICS", "9");
    test("REFRESH TABLE METADATA dfs.tmp.parquetStale");
    // Verify we recompute statistics once a new file/directory is added. Update the directory some
    // time after ANALYZE so that the timestamps are different.
    Thread.sleep(1000);
    final String Q4 = "/multilevel/parquet/1996/Q4";
    test("CREATE TABLE dfs.tmp.`parquetStale/1996/Q5` AS SELECT o_orderkey, o_custkey, o_orderstatus, " +
            "o_totalprice, o_orderdate, o_orderpriority, o_clerk, o_shippriority, o_comment from dfs.`%s`", Q4);
    // query should use STALE statistics
    String[] expectedStalePlan = {"StreamAgg\\(group=\\[\\{0\\}\\]\\).*rowcount = 119.0.*",
        "Scan.*rowcount = 130.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedStalePlan, new String[]{});
    // Query should use Parquet Metadata, since statistics not available. In this case, NDV is computed as
    // 1/10*rowcount (Calcite default). Hence, NDV is 13.0 instead of the correct 119.0
    test("DROP TABLE dfs.tmp.`parquetStale/.stats.drill`");
    String[] expectedPlan1 = {"HashAgg\\(group=\\[\\{0\\}\\]\\).*rowcount = 13.0.*",
        "Scan.*rowcount = 130.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan1, new String[]{});
    // query should use the new statistics. NDV remains unaffected since we copy the Q4 into Q5
    verifyAnalyzeOutput("ANALYZE TABLE dfs.tmp.parquetStale COMPUTE STATISTICS", "9");
    String[] expectedPlan2 = {"StreamAgg\\(group=\\[\\{0\\}\\]\\).*rowcount = 119.0.*",
        "Scan.*rowcount = 130.0.*"};
    PlanTestBase.testPlanWithAttributesMatchingPatterns(query, expectedPlan2, new String[]{});
    test("DROP TABLE dfs.tmp.`parquetStale/1996/Q5`");
  }

  //Helper function to verify output of ANALYZE statement
  private void verifyAnalyzeOutput(String query, String message) throws Exception {
    List<QueryDataBatch>result = testRunAndReturn(QueryType.SQL, query);
    List<List<String>> output = new ArrayList<>();
    assertTrue(result.size() == 1);
    final QueryDataBatch batch = result.get(0);
    final RecordBatchLoader loader = new RecordBatchLoader(getDrillbitContext().getAllocator());
    loader.load(batch.getHeader().getDef(), batch.getData());
    output.add(new ArrayList<String>());
    for (VectorWrapper<?> vw: loader) {
      ValueVector.Accessor accessor = vw.getValueVector().getAccessor();
      Object o = accessor.getObject(0);
      output.get(0).add(o == null ? null: o.toString());
    }
    batch.release();
    loader.clear();
    assertTrue(output.get(0).size() == 2);
    assertEquals(message, output.get(0).get(1));
  }
}