You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@hive.apache.org by sa...@apache.org on 2024/02/15 11:27:50 UTC

(hive) branch branch-3 updated: HIVE-28068: Backport of HIVE-24808, HIVE-24811, HIVE-22476, HIVE-25299, HIVE-22200, HIVE-21544, HIVE-22841, HIVE-20292, HIVE-17040, HIVE-20718, HIVE-20193 (#5029)

This is an automated email from the ASF dual-hosted git repository.

sankarh pushed a commit to branch branch-3
in repository https://gitbox.apache.org/repos/asf/hive.git


The following commit(s) were added to refs/heads/branch-3 by this push:
     new f82b990a9c3 HIVE-28068: Backport of HIVE-24808, HIVE-24811, HIVE-22476, HIVE-25299, HIVE-22200, HIVE-21544, HIVE-22841, HIVE-20292, HIVE-17040, HIVE-20718, HIVE-20193 (#5029)
f82b990a9c3 is described below

commit f82b990a9c33822cf6a5d3f241234ff6c23a37f0
Author: Aman Raj <10...@users.noreply.github.com>
AuthorDate: Thu Feb 15 16:57:45 2024 +0530

    HIVE-28068: Backport of HIVE-24808, HIVE-24811, HIVE-22476, HIVE-25299, HIVE-22200, HIVE-21544, HIVE-22841, HIVE-20292, HIVE-17040, HIVE-20718, HIVE-20193 (#5029)
    
    * HIVE-24808: Cache Parsed Dates (David Mollitor reviewed by Miklos Gergely)
    * HIVE-24811: Extend Cached Dates to Other Areas (David Mollitor reviewed by Miklos Gergely)
    * HIVE-22476: Hive datediff function provided inconsistent results when hive.fetch.task.conversion is set to none (Slim Bouguerra via Jesus Camacho Rodriguez)
    * HIVE-25299: Casting timestamp to numeric data types is incorrect for non-UTC timezones (Adesh Rao, reviewed by Ashish Kumar, Sankar Hariappan)
    * HIVE-22200: Hash collision may cause column resolution to fail (Jesus Camacho Rodriguez, reviewed by Ashutosh Chauhan)
    * HIVE-21544: Constant propagation corrupts coalesce/case/when expressions during folding (Zoltan Haindrich, reviewed by Jesus Camacho Rodriguez)
    * HIVE-22841: ThriftHttpServlet#getClientNameFromCookie should handle CookieSigner IllegalArgumentException on invalid cookie signature (Kevin Risden via Zoltan Haindrich)
    * HIVE-20292: Bad join ordering in tpcds query93 with primary constraint defined (Vineet Garg, reviewed by Ashutosh Chauhan)
    * HIVE-17040: Join elimination in the presence of FK relationship (Jesus Camacho Rodriguez, reviewed by Vineet Garg)
    * HIVE-20718: Add perf cli driver with constraints (Jesus Camacho Rodriguez, reviewed by Ashutosh Chauhan)
    * HIVE-20193: cboInfo is not present in the explain plan json (Anishek Agarwal, reviewed by Ashutosh Chauhan)
    
    ---------
    Co-authored-by: belugabehr <12...@users.noreply.github.com>
    Co-authored-by: Sruthi Mooriyathvariam <sr...@microsoft.com>
    Co-authored-by: Adesh Rao <ad...@microsoft.com>
    Co-authored-by: Ashish Sharma <as...@microsoft.com>
    Co-authored-by: Jesus Camacho Rodriguez <jc...@apache.org>
    Co-authored-by: Anishek Agarwal <an...@gmail.com>
     ---------
    
    Signed-off-by: Sankar Hariappan <sa...@apache.org>
    Closes (#5029)
---
 .../org/apache/hadoop/hive/common/type/Date.java   |   23 +-
 .../hadoop/hive/common/type/TimestampTZUtil.java   |    6 +-
 .../hadoop/hive/common/type/TimestampUtils.java    |   22 +-
 .../java/org/apache/hadoop/hive/conf/HiveConf.java |    3 +
 .../org/apache/hive/common/util/DateParser.java    |   70 +-
 .../apache/hive/common/util/TestDateParser.java    |   16 +-
 data/conf/perf-reg/spark/hive-site.xml             |    7 +-
 data/conf/perf-reg/tez/hive-site.xml               |   17 +-
 data/scripts/q_perf_test_init.sql                  |   96 +-
 data/scripts/q_perf_test_init_constraints.sql      |  771 +++++++++
 .../auth/TestHttpCookieAuthenticationTest.java     |  185 +++
 .../hadoop/hive/cli/TestTezPerfCliDriver.java      |    2 +-
 ...r.java => TestTezPerfConstraintsCliDriver.java} |    7 +-
 .../test/resources/testconfiguration.properties    |  106 +-
 .../apache/hadoop/hive/cli/control/CliConfigs.java |   17 +-
 .../java/org/apache/hive/jdbc/HiveConnection.java  |    2 +-
 .../hive/jdbc/HttpRequestInterceptorBase.java      |    5 +-
 ql/src/java/org/apache/hadoop/hive/ql/Context.java |    9 +
 ql/src/java/org/apache/hadoop/hive/ql/Driver.java  |    3 +-
 .../java/org/apache/hadoop/hive/ql/QueryPlan.java  |    7 +-
 .../apache/hadoop/hive/ql/exec/ExplainTask.java    |   24 +-
 .../exec/vector/expressions/CastStringToDate.java  |    6 +-
 .../vector/expressions/TruncDateFromString.java    |   69 +
 .../vector/expressions/TruncDateFromTimestamp.java |  180 ++
 .../vector/expressions/VectorUDFDateAddColCol.java |   11 +-
 .../expressions/VectorUDFDateAddColScalar.java     |    7 +-
 .../expressions/VectorUDFDateAddScalarCol.java     |    3 +-
 .../org/apache/hadoop/hive/ql/hooks/ATSHook.java   |    3 +-
 .../hadoop/hive/ql/hooks/HiveProtoLoggingHook.java |    5 +-
 .../ql/optimizer/ConstantPropagateProcFactory.java |    2 +-
 .../hive/ql/optimizer/calcite/HiveRelOptUtil.java  |  302 ++++
 .../calcite/rules/HiveJoinConstraintsRule.java     |  295 ++++
 .../rules/HiveJoinProjectTransposeRule.java        |   15 +
 .../rules/HiveProjectJoinTransposeRule.java        |   37 +-
 .../calcite/rules/HiveProjectMergeRule.java        |   21 +
 .../annotation/OpTraitsRulesProcFactory.java       |   21 +-
 .../hadoop/hive/ql/parse/BaseSemanticAnalyzer.java |    4 +
 .../hadoop/hive/ql/parse/CalcitePlanner.java       |   90 +-
 .../hadoop/hive/ql/parse/ExplainConfiguration.java |   18 +
 .../hive/ql/parse/ExplainSemanticAnalyzer.java     |   12 +-
 .../org/apache/hadoop/hive/ql/parse/HiveLexer.g    |    1 +
 .../org/apache/hadoop/hive/ql/parse/HiveParser.g   |    1 +
 .../hadoop/hive/ql/parse/IdentifiersParser.g       |    2 +-
 .../hadoop/hive/ql/parse/SemanticAnalyzer.java     |   14 +-
 .../apache/hadoop/hive/ql/plan/ExplainWork.java    |   17 +-
 .../hadoop/hive/ql/plan/ExprNodeDescUtils.java     |   39 +-
 .../apache/hadoop/hive/ql/udf/UDFDayOfWeek.java    |   17 +-
 .../apache/hadoop/hive/ql/udf/UDFToBoolean.java    |    4 +-
 .../org/apache/hadoop/hive/ql/udf/UDFToByte.java   |    4 +-
 .../org/apache/hadoop/hive/ql/udf/UDFToDouble.java |    7 +-
 .../org/apache/hadoop/hive/ql/udf/UDFToFloat.java  |    7 +-
 .../apache/hadoop/hive/ql/udf/UDFToInteger.java    |    4 +-
 .../org/apache/hadoop/hive/ql/udf/UDFToLong.java   |    4 +-
 .../org/apache/hadoop/hive/ql/udf/UDFToShort.java  |    4 +-
 .../org/apache/hadoop/hive/ql/udf/UDFUtils.java    |   39 +-
 .../apache/hadoop/hive/ql/udf/UDFWeekOfYear.java   |   19 +-
 .../hadoop/hive/ql/udf/generic/GenericUDF.java     |    7 +-
 .../hadoop/hive/ql/udf/generic/GenericUDFDate.java |    3 +-
 .../hive/ql/udf/generic/GenericUDFDateAdd.java     |    3 +-
 .../hive/ql/udf/generic/GenericUDFDateDiff.java    |   20 +-
 .../hive/ql/udf/generic/GenericUDFTrunc.java       |    6 +-
 .../hadoop/hive/ql/exec/TestExplainTask.java       |    4 +-
 .../expressions/TestVectorCastStatement.java       |    1 +
 .../ql/parse/TestUpdateDeleteSemanticAnalyzer.java |    2 +-
 .../ql/udf/generic/TestGenericUDFDateDiff.java     |   29 +-
 .../hive/ql/udf/generic/TestGenericUDFLastDay.java |    2 +-
 .../hive/ql/udf/generic/TestGenericUDFNextDay.java |    2 +-
 ql/src/test/queries/clientpositive/collision.q     |  337 ++++
 .../queries/clientpositive/constant_prop_when.q    |   20 +
 ql/src/test/queries/clientpositive/date_udf.q      |    6 +
 .../clientpositive/join_constraints_optimization.q |  136 ++
 .../clientpositive/materialized_view_rewrite_1.q   |    6 +-
 .../clientpositive/materialized_view_rewrite_3.q   |    6 +-
 .../clientpositive/materialized_view_rewrite_4.q   |    9 +-
 .../clientpositive/materialized_view_rewrite_5.q   |    6 +-
 .../clientpositive/materialized_view_rewrite_6.q   |    6 +-
 .../clientpositive/materialized_view_rewrite_7.q   |    4 +-
 .../materialized_view_rewrite_no_join_opt.q        |  132 ++
 ...q => materialized_view_rewrite_no_join_opt_2.q} |    9 +-
 .../materialized_view_rewrite_part_1.q             |    6 +-
 .../test/queries/clientpositive/perf/cbo_query1.q  |   27 +
 .../test/queries/clientpositive/perf/cbo_query10.q |   61 +
 .../test/queries/clientpositive/perf/cbo_query11.q |   77 +
 .../test/queries/clientpositive/perf/cbo_query12.q |   35 +
 .../test/queries/clientpositive/perf/cbo_query13.q |   54 +
 .../test/queries/clientpositive/perf/cbo_query14.q |  104 ++
 .../test/queries/clientpositive/perf/cbo_query15.q |   22 +
 .../test/queries/clientpositive/perf/cbo_query16.q |   33 +
 .../test/queries/clientpositive/perf/cbo_query17.q |   47 +
 .../test/queries/clientpositive/perf/cbo_query18.q |   36 +
 .../test/queries/clientpositive/perf/cbo_query19.q |   27 +
 .../test/queries/clientpositive/perf/cbo_query2.q  |   62 +
 .../test/queries/clientpositive/perf/cbo_query20.q |   31 +
 .../test/queries/clientpositive/perf/cbo_query21.q |   32 +
 .../test/queries/clientpositive/perf/cbo_query22.q |   24 +
 .../test/queries/clientpositive/perf/cbo_query23.q |   52 +
 .../test/queries/clientpositive/perf/cbo_query24.q |   51 +
 .../test/queries/clientpositive/perf/cbo_query25.q |   50 +
 .../test/queries/clientpositive/perf/cbo_query26.q |   23 +
 .../test/queries/clientpositive/perf/cbo_query27.q |   25 +
 .../test/queries/clientpositive/perf/cbo_query28.q |   59 +
 .../test/queries/clientpositive/perf/cbo_query29.q |   49 +
 .../test/queries/clientpositive/perf/cbo_query3.q  |   23 +
 .../test/queries/clientpositive/perf/cbo_query30.q |   33 +
 .../test/queries/clientpositive/perf/cbo_query31.q |   54 +
 .../test/queries/clientpositive/perf/cbo_query32.q |   30 +
 .../test/queries/clientpositive/perf/cbo_query33.q |   77 +
 .../test/queries/clientpositive/perf/cbo_query34.q |   33 +
 .../test/queries/clientpositive/perf/cbo_query35.q |   59 +
 .../test/queries/clientpositive/perf/cbo_query36.q |   32 +
 .../test/queries/clientpositive/perf/cbo_query37.q |   19 +
 .../test/queries/clientpositive/perf/cbo_query38.q |   25 +
 .../test/queries/clientpositive/perf/cbo_query39.q |   56 +
 .../test/queries/clientpositive/perf/cbo_query4.q  |  111 ++
 .../test/queries/clientpositive/perf/cbo_query40.q |   30 +
 .../test/queries/clientpositive/perf/cbo_query42.q |   24 +
 .../test/queries/clientpositive/perf/cbo_query43.q |   21 +
 .../test/queries/clientpositive/perf/cbo_query44.q |   37 +
 .../test/queries/clientpositive/perf/cbo_query45.q |   22 +
 .../test/queries/clientpositive/perf/cbo_query46.q |   37 +
 .../test/queries/clientpositive/perf/cbo_query47.q |   53 +
 .../test/queries/clientpositive/perf/cbo_query48.q |   69 +
 .../test/queries/clientpositive/perf/cbo_query49.q |  129 ++
 .../test/queries/clientpositive/perf/cbo_query5.q  |  130 ++
 .../test/queries/clientpositive/perf/cbo_query50.q |   61 +
 .../test/queries/clientpositive/perf/cbo_query51.q |   47 +
 .../test/queries/clientpositive/perf/cbo_query52.q |   24 +
 .../test/queries/clientpositive/perf/cbo_query53.q |   30 +
 .../test/queries/clientpositive/perf/cbo_query54.q |   58 +
 .../test/queries/clientpositive/perf/cbo_query55.q |   16 +
 .../test/queries/clientpositive/perf/cbo_query56.q |   70 +
 .../test/queries/clientpositive/perf/cbo_query57.q |   50 +
 .../test/queries/clientpositive/perf/cbo_query58.q |   67 +
 .../test/queries/clientpositive/perf/cbo_query59.q |   46 +
 .../test/queries/clientpositive/perf/cbo_query6.q  |   30 +
 .../test/queries/clientpositive/perf/cbo_query60.q |   80 +
 .../test/queries/clientpositive/perf/cbo_query61.q |   46 +
 .../test/queries/clientpositive/perf/cbo_query63.q |   31 +
 .../test/queries/clientpositive/perf/cbo_query64.q |  121 ++
 .../test/queries/clientpositive/perf/cbo_query65.q |   31 +
 .../test/queries/clientpositive/perf/cbo_query66.q |  224 +++
 .../test/queries/clientpositive/perf/cbo_query67.q |   46 +
 .../test/queries/clientpositive/perf/cbo_query68.q |   44 +
 .../test/queries/clientpositive/perf/cbo_query69.q |   49 +
 .../test/queries/clientpositive/perf/cbo_query7.q  |   23 +
 .../test/queries/clientpositive/perf/cbo_query70.q |   40 +
 .../test/queries/clientpositive/perf/cbo_query71.q |   42 +
 .../test/queries/clientpositive/perf/cbo_query72.q |   33 +
 .../test/queries/clientpositive/perf/cbo_query73.q |   30 +
 .../test/queries/clientpositive/perf/cbo_query74.q |   63 +
 .../test/queries/clientpositive/perf/cbo_query75.q |   72 +
 .../test/queries/clientpositive/perf/cbo_query76.q |   26 +
 .../test/queries/clientpositive/perf/cbo_query77.q |  109 ++
 .../test/queries/clientpositive/perf/cbo_query78.q |   60 +
 .../test/queries/clientpositive/perf/cbo_query79.q |   25 +
 .../test/queries/clientpositive/perf/cbo_query8.q  |  110 ++
 .../test/queries/clientpositive/perf/cbo_query80.q |   98 ++
 .../test/queries/clientpositive/perf/cbo_query81.q |   33 +
 .../test/queries/clientpositive/perf/cbo_query82.q |   19 +
 .../test/queries/clientpositive/perf/cbo_query83.q |   69 +
 .../test/queries/clientpositive/perf/cbo_query84.q |   23 +
 .../test/queries/clientpositive/perf/cbo_query85.q |   86 +
 .../test/queries/clientpositive/perf/cbo_query86.q |   28 +
 .../test/queries/clientpositive/perf/cbo_query87.q |   25 +
 .../test/queries/clientpositive/perf/cbo_query88.q |   96 ++
 .../test/queries/clientpositive/perf/cbo_query89.q |   30 +
 .../test/queries/clientpositive/perf/cbo_query9.q  |   53 +
 .../test/queries/clientpositive/perf/cbo_query90.q |   24 +
 .../test/queries/clientpositive/perf/cbo_query91.q |   33 +
 .../test/queries/clientpositive/perf/cbo_query92.q |   32 +
 .../test/queries/clientpositive/perf/cbo_query93.q |   20 +
 .../test/queries/clientpositive/perf/cbo_query94.q |   31 +
 .../test/queries/clientpositive/perf/cbo_query95.q |   34 +
 .../test/queries/clientpositive/perf/cbo_query96.q |   18 +
 .../test/queries/clientpositive/perf/cbo_query97.q |   27 +
 .../test/queries/clientpositive/perf/cbo_query98.q |   34 +
 .../test/queries/clientpositive/perf/cbo_query99.q |   37 +
 .../queries/clientpositive/q93_with_constraints.q  |   88 +
 ql/src/test/queries/clientpositive/timestamp_1.q   |   16 +
 ql/src/test/queries/clientpositive/timestamp_2.q   |   15 +
 ql/src/test/queries/clientpositive/timestamp_3.q   |   15 +
 .../test/queries/clientpositive/udf_to_boolean.q   |    9 +-
 .../results/clientpositive/ambiguitycheck.q.out    |    2 +-
 .../beeline/materialized_view_create_rewrite.q.out |    2 +-
 ql/src/test/results/clientpositive/collision.q.out |  819 +++++++++
 .../clientpositive/correlationoptimizer5.q.out     |  499 ++----
 ql/src/test/results/clientpositive/date_udf.q.out  |   45 +
 .../clientpositive/druid/druidmini_mv.q.out        |    6 +-
 ql/src/test/results/clientpositive/input4.q.out    |    2 +-
 ql/src/test/results/clientpositive/join0.q.out     |    2 +-
 ql/src/test/results/clientpositive/join28.q.out    |   90 +-
 .../results/clientpositive/list_bucket_dml_2.q.out |    2 +-
 .../results/clientpositive/list_bucket_dml_4.q.out |    2 +-
 .../results/clientpositive/list_bucket_dml_9.q.out |    2 +-
 .../list_bucket_query_multiskew_1.q.out            |    4 +-
 .../list_bucket_query_multiskew_2.q.out            |    2 +-
 .../list_bucket_query_multiskew_3.q.out            |    4 +-
 .../list_bucket_query_oneskew_1.q.out              |    6 +-
 .../list_bucket_query_oneskew_2.q.out              |    2 +-
 .../clientpositive/llap/acid_bucket_pruning.q.out  |    2 +-
 .../clientpositive/llap/bucketpruning1.q.out       |    4 +-
 .../clientpositive/llap/column_access_stats.q.out  |   49 +-
 .../clientpositive/llap/constant_prop_when.q.out   |   87 +
 .../llap/correlationoptimizer2.q.out               |  144 +-
 .../llap/correlationoptimizer3.q.out               |  137 +-
 .../llap/current_date_timestamp.q.out              |    2 +-
 .../llap/dynamic_semijoin_reduction_sw.q.out       |   18 +-
 .../clientpositive/llap/join32_lessSize.q.out      |  518 +++---
 .../llap/join_constraints_optimization.q.out       |  819 +++++++++
 .../results/clientpositive/llap/llap_smb_ptf.q.out |   36 +-
 .../llap/materialized_view_create_rewrite.q.out    |    2 +-
 .../materialized_view_create_rewrite_dummy.q.out   |    2 +-
 ...materialized_view_create_rewrite_multi_db.q.out |    2 +-
 .../llap/materialized_view_rewrite_1.q.out         |  109 +-
 .../llap/materialized_view_rewrite_2.q.out         |   57 +-
 .../llap/materialized_view_rewrite_3.q.out         |   20 +-
 .../llap/materialized_view_rewrite_4.q.out         |   40 +-
 .../llap/materialized_view_rewrite_5.q.out         |   95 +-
 .../llap/materialized_view_rewrite_6.q.out         |   90 +-
 .../llap/materialized_view_rewrite_7.q.out         |  224 +--
 .../materialized_view_rewrite_no_join_opt.q.out    |  788 +++++++++
 ... materialized_view_rewrite_no_join_opt_2.q.out} |   51 +-
 .../llap/materialized_view_rewrite_part_1.q.out    |  111 +-
 .../llap/materialized_view_rewrite_part_2.q.out    |   66 +-
 .../clientpositive/llap/multiMapJoin1.q.out        |  356 ++--
 .../clientpositive/llap/q93_with_constraints.q.out |  378 +++++
 .../clientpositive/llap/subquery_scalar.q.out      |   34 +-
 .../clientpositive/llap/tez_self_join.q.out        |   56 +-
 .../{spark => llap}/timestamp_1.q.out              |   80 +-
 .../{spark => llap}/timestamp_2.q.out              |   80 +-
 .../{spark => llap}/timestamp_3.q.out              |   74 +-
 .../clientpositive/{ => llap}/udf_to_boolean.q.out |    9 +
 .../clientpositive/llap/vector_case_when_2.q.out   |   36 +-
 .../clientpositive/llap/vectorized_casts.q.out     |   54 +-
 .../llap/vectorized_nested_mapjoin.q.out           |   86 +-
 ql/src/test/results/clientpositive/macro.q.out     |    6 +-
 .../results/clientpositive/mapjoin_subquery.q.out  |  132 +-
 .../results/clientpositive/mapjoin_subquery2.q.out |   56 +-
 .../results/clientpositive/parallel_join0.q.out    |    2 +-
 .../results/clientpositive/perf/spark/query2.q.out |  103 +-
 .../clientpositive/perf/spark/query59.q.out        |   20 +-
 .../clientpositive/perf/spark/query95.q.out        |   12 +-
 .../clientpositive/perf/tez/cbo_query1.q.out       |   91 +
 .../clientpositive/perf/tez/cbo_query10.q.out      |  178 ++
 .../clientpositive/perf/tez/cbo_query11.q.out      |  223 +++
 .../clientpositive/perf/tez/cbo_query12.q.out      |   89 +
 .../clientpositive/perf/tez/cbo_query13.q.out      |  141 ++
 .../clientpositive/perf/tez/cbo_query14.q.out      |  618 +++++++
 .../clientpositive/perf/tez/cbo_query15.q.out      |   69 +
 .../clientpositive/perf/tez/cbo_query16.q.out      |  104 ++
 .../clientpositive/perf/tez/cbo_query17.q.out      |  140 ++
 .../clientpositive/perf/tez/cbo_query18.q.out      |  114 ++
 .../clientpositive/perf/tez/cbo_query19.q.out      |   92 +
 .../clientpositive/perf/tez/cbo_query2.q.out       |  170 ++
 .../clientpositive/perf/tez/cbo_query20.q.out      |   81 +
 .../clientpositive/perf/tez/cbo_query21.q.out      |   90 +
 .../clientpositive/perf/tez/cbo_query22.q.out      |   72 +
 .../clientpositive/perf/tez/cbo_query23.q.out      |  281 ++++
 .../clientpositive/perf/tez/cbo_query24.q.out      |  171 ++
 .../clientpositive/perf/tez/cbo_query25.q.out      |  145 ++
 .../clientpositive/perf/tez/cbo_query26.q.out      |   76 +
 .../clientpositive/perf/tez/cbo_query27.q.out      |   81 +
 .../clientpositive/perf/tez/cbo_query28.q.out      |  146 ++
 .../clientpositive/perf/tez/cbo_query29.q.out      |  143 ++
 .../clientpositive/perf/tez/cbo_query3.q.out       |   64 +
 .../clientpositive/perf/tez/cbo_query30.q.out      |  112 ++
 .../clientpositive/perf/tez/cbo_query31.q.out      |  199 +++
 .../clientpositive/perf/tez/cbo_query32.q.out      |   89 +
 .../clientpositive/perf/tez/cbo_query33.q.out      |  238 +++
 .../clientpositive/perf/tez/cbo_query34.q.out      |   99 ++
 .../clientpositive/perf/tez/cbo_query35.q.out      |  175 ++
 .../clientpositive/perf/tez/cbo_query36.q.out      |   91 +
 .../clientpositive/perf/tez/cbo_query37.q.out      |   63 +
 .../clientpositive/perf/tez/cbo_query38.q.out      |  111 ++
 .../clientpositive/perf/tez/cbo_query39.q.out      |  168 ++
 .../clientpositive/perf/tez/cbo_query4.q.out       |  324 ++++
 .../clientpositive/perf/tez/cbo_query40.q.out      |   91 +
 .../clientpositive/perf/tez/cbo_query42.q.out      |   68 +
 .../clientpositive/perf/tez/cbo_query43.q.out      |   61 +
 .../clientpositive/perf/tez/cbo_query44.q.out      |  115 ++
 .../clientpositive/perf/tez/cbo_query45.q.out      |   89 +
 .../clientpositive/perf/tez/cbo_query46.q.out      |  115 ++
 .../clientpositive/perf/tez/cbo_query47.q.out      |  177 ++
 .../clientpositive/perf/tez/cbo_query48.q.out      |  164 ++
 .../clientpositive/perf/tez/cbo_query49.q.out      |  330 ++++
 .../clientpositive/perf/tez/cbo_query5.q.out       |  342 ++++
 .../clientpositive/perf/tez/cbo_query50.q.out      |  151 ++
 .../clientpositive/perf/tez/cbo_query51.q.out      |  125 ++
 .../clientpositive/perf/tez/cbo_query52.q.out      |   67 +
 .../clientpositive/perf/tez/cbo_query53.q.out      |   87 +
 .../clientpositive/perf/tez/cbo_query54.q.out      |  212 +++
 .../clientpositive/perf/tez/cbo_query55.q.out      |   51 +
 .../clientpositive/perf/tez/cbo_query56.q.out      |  224 +++
 .../clientpositive/perf/tez/cbo_query57.q.out      |  171 ++
 .../clientpositive/perf/tez/cbo_query58.q.out      |  240 +++
 .../clientpositive/perf/tez/cbo_query59.q.out      |  136 ++
 .../clientpositive/perf/tez/cbo_query6.q.out       |  109 ++
 .../clientpositive/perf/tez/cbo_query60.q.out      |  244 +++
 .../clientpositive/perf/tez/cbo_query61.q.out      |  164 ++
 .../clientpositive/perf/tez/cbo_query63.q.out      |   89 +
 .../clientpositive/perf/tez/cbo_query64.q.out      |  438 +++++
 .../clientpositive/perf/tez/cbo_query65.q.out      |   99 ++
 .../clientpositive/perf/tez/cbo_query66.q.out      |  508 ++++++
 .../clientpositive/perf/tez/cbo_query67.q.out      |  120 ++
 .../clientpositive/perf/tez/cbo_query68.q.out      |  129 ++
 .../clientpositive/perf/tez/cbo_query69.q.out      |  156 ++
 .../clientpositive/perf/tez/cbo_query7.q.out       |   76 +
 .../clientpositive/perf/tez/cbo_query70.q.out      |  119 ++
 .../clientpositive/perf/tez/cbo_query71.q.out      |  130 ++
 .../clientpositive/perf/tez/cbo_query72.q.out      |  130 ++
 .../clientpositive/perf/tez/cbo_query73.q.out      |   93 ++
 .../clientpositive/perf/tez/cbo_query74.q.out      |  191 +++
 .../clientpositive/perf/tez/cbo_query75.q.out      |  278 ++++
 .../clientpositive/perf/tez/cbo_query76.q.out      |  101 ++
 .../clientpositive/perf/tez/cbo_query77.q.out      |  316 ++++
 .../clientpositive/perf/tez/cbo_query78.q.out      |  183 ++
 .../clientpositive/perf/tez/cbo_query79.q.out      |   82 +
 .../clientpositive/perf/tez/cbo_query8.q.out       |  266 +++
 .../clientpositive/perf/tez/cbo_query80.q.out      |  301 ++++
 .../clientpositive/perf/tez/cbo_query81.q.out      |  113 ++
 .../clientpositive/perf/tez/cbo_query82.q.out      |   63 +
 .../clientpositive/perf/tez/cbo_query83.q.out      |  222 +++
 .../clientpositive/perf/tez/cbo_query84.q.out      |   84 +
 .../clientpositive/perf/tez/cbo_query85.q.out      |  219 +++
 .../clientpositive/perf/tez/cbo_query86.q.out      |   77 +
 .../clientpositive/perf/tez/cbo_query87.q.out      |  114 ++
 .../clientpositive/perf/tez/cbo_query88.q.out      |  347 ++++
 .../clientpositive/perf/tez/cbo_query89.q.out      |   88 +
 .../clientpositive/perf/tez/cbo_query9.q.out       |  200 +++
 .../clientpositive/perf/tez/cbo_query90.q.out      |   92 +
 .../clientpositive/perf/tez/cbo_query91.q.out      |  110 ++
 .../clientpositive/perf/tez/cbo_query92.q.out      |   94 ++
 .../clientpositive/perf/tez/cbo_query93.q.out      |   59 +
 .../clientpositive/perf/tez/cbo_query94.q.out      |  100 ++
 .../clientpositive/perf/tez/cbo_query95.q.out      |  120 ++
 .../clientpositive/perf/tez/cbo_query96.q.out      |   61 +
 .../clientpositive/perf/tez/cbo_query97.q.out      |   81 +
 .../clientpositive/perf/tez/cbo_query98.q.out      |   87 +
 .../clientpositive/perf/tez/cbo_query99.q.out      |  106 ++
 .../perf/tez/constraints/cbo_query1.q.out          |   90 +
 .../perf/tez/constraints/cbo_query10.q.out         |  177 ++
 .../perf/tez/constraints/cbo_query11.q.out         |  216 +++
 .../perf/tez/constraints/cbo_query12.q.out         |   89 +
 .../perf/tez/constraints/cbo_query13.q.out         |  138 ++
 .../perf/tez/constraints/cbo_query14.q.out         |  612 +++++++
 .../perf/tez/constraints/cbo_query15.q.out         |   68 +
 .../perf/tez/constraints/cbo_query16.q.out         |  103 ++
 .../perf/tez/constraints/cbo_query17.q.out         |  140 ++
 .../perf/tez/constraints/cbo_query18.q.out         |  113 ++
 .../perf/tez/constraints/cbo_query19.q.out         |   91 +
 .../perf/tez/constraints/cbo_query2.q.out          |  170 ++
 .../perf/tez/constraints/cbo_query20.q.out         |   81 +
 .../perf/tez/constraints/cbo_query21.q.out         |   89 +
 .../perf/tez/constraints/cbo_query22.q.out         |   66 +
 .../perf/tez/constraints/cbo_query23.q.out         |  247 +++
 .../perf/tez/constraints/cbo_query24.q.out         |  172 ++
 .../perf/tez/constraints/cbo_query25.q.out         |  145 ++
 .../perf/tez/constraints/cbo_query26.q.out         |   76 +
 .../perf/tez/constraints/cbo_query27.q.out         |   81 +
 .../perf/tez/constraints/cbo_query28.q.out         |  146 ++
 .../perf/tez/constraints/cbo_query29.q.out         |  143 ++
 .../perf/tez/constraints/cbo_query3.q.out          |   64 +
 .../perf/tez/constraints/cbo_query30.q.out         |  112 ++
 .../perf/tez/constraints/cbo_query31.q.out         |  200 +++
 .../perf/tez/constraints/cbo_query32.q.out         |   89 +
 .../perf/tez/constraints/cbo_query33.q.out         |  238 +++
 .../perf/tez/constraints/cbo_query34.q.out         |   99 ++
 .../perf/tez/constraints/cbo_query35.q.out         |  173 ++
 .../perf/tez/constraints/cbo_query36.q.out         |   91 +
 .../perf/tez/constraints/cbo_query37.q.out         |   62 +
 .../perf/tez/constraints/cbo_query38.q.out         |  108 ++
 .../perf/tez/constraints/cbo_query39.q.out         |  156 ++
 .../perf/tez/constraints/cbo_query4.q.out          |  314 ++++
 .../perf/tez/constraints/cbo_query40.q.out         |   90 +
 .../perf/tez/constraints/cbo_query42.q.out         |   68 +
 .../perf/tez/constraints/cbo_query43.q.out         |   61 +
 .../perf/tez/constraints/cbo_query44.q.out         |  113 ++
 .../perf/tez/constraints/cbo_query45.q.out         |   81 +
 .../perf/tez/constraints/cbo_query46.q.out         |  114 ++
 .../perf/tez/constraints/cbo_query47.q.out         |  180 ++
 .../perf/tez/constraints/cbo_query48.q.out         |  161 ++
 .../perf/tez/constraints/cbo_query49.q.out         |  330 ++++
 .../perf/tez/constraints/cbo_query5.q.out          |  339 ++++
 .../perf/tez/constraints/cbo_query50.q.out         |  146 ++
 .../perf/tez/constraints/cbo_query51.q.out         |  125 ++
 .../perf/tez/constraints/cbo_query52.q.out         |   67 +
 .../perf/tez/constraints/cbo_query53.q.out         |   83 +
 .../perf/tez/constraints/cbo_query54.q.out         |  212 +++
 .../perf/tez/constraints/cbo_query55.q.out         |   51 +
 .../perf/tez/constraints/cbo_query56.q.out         |  221 +++
 .../perf/tez/constraints/cbo_query57.q.out         |  174 ++
 .../perf/tez/constraints/cbo_query58.q.out         |  237 +++
 .../perf/tez/constraints/cbo_query59.q.out         |  135 ++
 .../perf/tez/constraints/cbo_query6.q.out          |  108 ++
 .../perf/tez/constraints/cbo_query60.q.out         |  241 +++
 .../perf/tez/constraints/cbo_query61.q.out         |  166 ++
 .../perf/tez/constraints/cbo_query63.q.out         |   85 +
 .../perf/tez/constraints/cbo_query64.q.out         |  402 +++++
 .../perf/tez/constraints/cbo_query65.q.out         |   97 ++
 .../perf/tez/constraints/cbo_query66.q.out         |  506 ++++++
 .../perf/tez/constraints/cbo_query67.q.out         |  118 ++
 .../perf/tez/constraints/cbo_query68.q.out         |  128 ++
 .../perf/tez/constraints/cbo_query69.q.out         |  155 ++
 .../perf/tez/constraints/cbo_query7.q.out          |   76 +
 .../perf/tez/constraints/cbo_query70.q.out         |  119 ++
 .../perf/tez/constraints/cbo_query71.q.out         |  130 ++
 .../perf/tez/constraints/cbo_query72.q.out         |  127 ++
 .../perf/tez/constraints/cbo_query73.q.out         |   93 ++
 .../perf/tez/constraints/cbo_query74.q.out         |  188 +++
 .../perf/tez/constraints/cbo_query75.q.out         |  278 ++++
 .../perf/tez/constraints/cbo_query76.q.out         |   95 ++
 .../perf/tez/constraints/cbo_query77.q.out         |  304 ++++
 .../perf/tez/constraints/cbo_query78.q.out         |  183 ++
 .../perf/tez/constraints/cbo_query79.q.out         |   82 +
 .../perf/tez/constraints/cbo_query8.q.out          |  266 +++
 .../perf/tez/constraints/cbo_query80.q.out         |  298 ++++
 .../perf/tez/constraints/cbo_query81.q.out         |  113 ++
 .../perf/tez/constraints/cbo_query82.q.out         |   62 +
 .../perf/tez/constraints/cbo_query83.q.out         |  219 +++
 .../perf/tez/constraints/cbo_query84.q.out         |   83 +
 .../perf/tez/constraints/cbo_query85.q.out         |  216 +++
 .../perf/tez/constraints/cbo_query86.q.out         |   76 +
 .../perf/tez/constraints/cbo_query87.q.out         |  111 ++
 .../perf/tez/constraints/cbo_query88.q.out         |  355 ++++
 .../perf/tez/constraints/cbo_query89.q.out         |   88 +
 .../perf/tez/constraints/cbo_query9.q.out          |  205 +++
 .../perf/tez/constraints/cbo_query90.q.out         |   94 ++
 .../perf/tez/constraints/cbo_query91.q.out         |  110 ++
 .../perf/tez/constraints/cbo_query92.q.out         |   94 ++
 .../perf/tez/constraints/cbo_query93.q.out         |   58 +
 .../perf/tez/constraints/cbo_query94.q.out         |   99 ++
 .../perf/tez/constraints/cbo_query95.q.out         |  114 ++
 .../perf/tez/constraints/cbo_query96.q.out         |   62 +
 .../perf/tez/constraints/cbo_query97.q.out         |   81 +
 .../perf/tez/constraints/cbo_query98.q.out         |   87 +
 .../perf/tez/constraints/cbo_query99.q.out         |  103 ++
 .../perf/tez/constraints/query1.q.out              |  168 ++
 .../perf/tez/constraints/query10.q.out             |  283 ++++
 .../perf/tez/constraints/query11.q.out             |  354 ++++
 .../perf/tez/{ => constraints}/query12.q.out       |   93 +-
 .../perf/tez/constraints/query13.q.out             |  201 +++
 .../perf/tez/constraints/query14.q.out             | 1024 ++++++++++++
 .../perf/tez/constraints/query15.q.out             |  125 ++
 .../perf/tez/constraints/query16.q.out             |  195 +++
 .../perf/tez/constraints/query17.q.out             |  231 +++
 .../perf/tez/constraints/query18.q.out             |  199 +++
 .../perf/tez/constraints/query19.q.out             |  164 ++
 .../perf/tez/constraints/query2.q.out              |  265 +++
 .../perf/tez/{ => constraints}/query20.q.out       |   93 +-
 .../perf/tez/constraints/query21.q.out             |  136 ++
 .../perf/tez/constraints/query22.q.out             |  109 ++
 .../perf/tez/constraints/query23.q.out             |  365 ++++
 .../perf/tez/constraints/query24.q.out             |  272 +++
 .../perf/tez/constraints/query25.q.out             |  233 +++
 .../perf/tez/constraints/query26.q.out             |  137 ++
 .../perf/tez/constraints/query27.q.out             |  143 ++
 .../perf/tez/constraints/query28.q.out             |  296 ++++
 .../perf/tez/constraints/query29.q.out             |  231 +++
 .../perf/tez/constraints/query3.q.out              |  103 ++
 .../perf/tez/constraints/query30.q.out             |  216 +++
 .../perf/tez/constraints/query31.q.out             |  360 ++++
 .../perf/tez/constraints/query32.q.out             |  143 ++
 .../perf/tez/constraints/query33.q.out             |  351 ++++
 .../perf/tez/constraints/query34.q.out             |  157 ++
 .../perf/tez/constraints/query35.q.out             |  277 ++++
 .../perf/tez/constraints/query36.q.out             |  152 ++
 .../perf/tez/constraints/query37.q.out             |  111 ++
 .../perf/tez/constraints/query38.q.out             |  213 +++
 .../perf/tez/constraints/query39.q.out             |  229 +++
 .../perf/tez/constraints/query4.q.out              |  512 ++++++
 .../perf/tez/constraints/query40.q.out             |  148 ++
 .../perf/tez/constraints/query42.q.out             |  107 ++
 .../perf/tez/constraints/query43.q.out             |  103 ++
 .../perf/tez/constraints/query44.q.out             |  218 +++
 .../perf/tez/constraints/query45.q.out             |  165 ++
 .../perf/tez/constraints/query46.q.out             |  194 +++
 .../perf/tez/constraints/query47.q.out             |  248 +++
 .../perf/tez/constraints/query48.q.out             |  208 +++
 .../perf/tez/{ => constraints}/query49.q.out       |  329 ++--
 .../perf/tez/constraints/query5.q.out              |  513 ++++++
 .../perf/tez/constraints/query50.q.out             |  197 +++
 .../perf/tez/{ => constraints}/query51.q.out       |  120 +-
 .../perf/tez/constraints/query52.q.out             |  107 ++
 .../perf/tez/constraints/query53.q.out             |  129 ++
 .../perf/tez/constraints/query54.q.out             |  385 +++++
 .../perf/tez/constraints/query55.q.out             |   91 +
 .../perf/tez/constraints/query56.q.out             |  335 ++++
 .../perf/tez/constraints/query57.q.out             |  242 +++
 .../perf/tez/constraints/query58.q.out             |  333 ++++
 .../perf/tez/constraints/query59.q.out             |  217 +++
 .../perf/tez/constraints/query6.q.out              |  227 +++
 .../perf/tez/constraints/query60.q.out             |  361 ++++
 .../perf/tez/constraints/query61.q.out             |  282 ++++
 .../perf/tez/constraints/query63.q.out             |  131 ++
 .../perf/tez/constraints/query64.q.out             |  639 +++++++
 .../perf/tez/constraints/query65.q.out             |  151 ++
 .../perf/tez/{ => constraints}/query66.q.out       |  258 ++-
 .../perf/tez/constraints/query67.q.out             |  178 ++
 .../perf/tez/constraints/query68.q.out             |  208 +++
 .../perf/tez/constraints/query69.q.out             |  263 +++
 .../perf/tez/constraints/query7.q.out              |  137 ++
 .../perf/tez/constraints/query70.q.out             |  200 +++
 .../perf/tez/constraints/query71.q.out             |  233 +++
 .../perf/tez/constraints/query72.q.out             |  237 +++
 .../perf/tez/constraints/query73.q.out             |  151 ++
 .../perf/tez/constraints/query74.q.out             |  324 ++++
 .../perf/tez/constraints/query75.q.out             |  474 ++++++
 .../perf/tez/constraints/query76.q.out             |  188 +++
 .../perf/tez/constraints/query77.q.out             |  439 +++++
 .../perf/tez/constraints/query78.q.out             |  294 ++++
 .../perf/tez/constraints/query79.q.out             |  141 ++
 .../perf/tez/{ => constraints}/query8.q.out        |  251 ++-
 .../perf/tez/constraints/query80.q.out             |  456 +++++
 .../perf/tez/constraints/query81.q.out             |  215 +++
 .../perf/tez/constraints/query82.q.out             |  111 ++
 .../perf/tez/constraints/query83.q.out             |  312 ++++
 .../perf/tez/constraints/query84.q.out             |  144 ++
 .../perf/tez/constraints/query85.q.out             |  301 ++++
 .../perf/tez/{ => constraints}/query86.q.out       |   89 +-
 .../perf/tez/{ => constraints}/query87.q.out       |  249 ++-
 .../perf/tez/constraints/query88.q.out             |  634 +++++++
 .../perf/tez/constraints/query89.q.out             |  144 ++
 .../perf/tez/constraints/query9.q.out              |  416 +++++
 .../perf/tez/constraints/query90.q.out             |  179 ++
 .../perf/tez/constraints/query91.q.out             |  189 +++
 .../perf/tez/constraints/query92.q.out             |  154 ++
 .../perf/tez/{ => constraints}/query93.q.out       |   83 +-
 .../perf/tez/constraints/query94.q.out             |  191 +++
 .../perf/tez/constraints/query95.q.out             |  234 +++
 .../perf/tez/constraints/query96.q.out             |  109 ++
 .../perf/tez/constraints/query97.q.out             |  134 ++
 .../perf/tez/{ => constraints}/query98.q.out       |   91 +-
 .../perf/tez/constraints/query99.q.out             |  157 ++
 .../results/clientpositive/perf/tez/query1.q.out   |  189 +--
 .../results/clientpositive/perf/tez/query10.q.out  |  259 ++-
 .../results/clientpositive/perf/tez/query11.q.out  |  331 ++--
 .../results/clientpositive/perf/tez/query12.q.out  |   89 +-
 .../results/clientpositive/perf/tez/query13.q.out  |  147 +-
 .../results/clientpositive/perf/tez/query14.q.out  | 1750 ++++++++++----------
 .../results/clientpositive/perf/tez/query15.q.out  |   93 +-
 .../results/clientpositive/perf/tez/query16.q.out  |  177 +-
 .../results/clientpositive/perf/tez/query17.q.out  |  194 +--
 .../results/clientpositive/perf/tez/query18.q.out  |  133 +-
 .../results/clientpositive/perf/tez/query19.q.out  |  169 +-
 .../results/clientpositive/perf/tez/query2.q.out   |  197 ++-
 .../results/clientpositive/perf/tez/query20.q.out  |   89 +-
 .../results/clientpositive/perf/tez/query21.q.out  |  113 +-
 .../results/clientpositive/perf/tez/query22.q.out  |  109 +-
 .../results/clientpositive/perf/tez/query23.q.out  |  548 +++---
 .../results/clientpositive/perf/tez/query24.q.out  |  252 ++-
 .../results/clientpositive/perf/tez/query25.q.out  |  190 +--
 .../results/clientpositive/perf/tez/query26.q.out  |  128 +-
 .../results/clientpositive/perf/tez/query27.q.out  |  130 +-
 .../results/clientpositive/perf/tez/query28.q.out  |  296 ++--
 .../results/clientpositive/perf/tez/query29.q.out  |  206 ++-
 .../results/clientpositive/perf/tez/query3.q.out   |   83 +-
 .../results/clientpositive/perf/tez/query30.q.out  |  201 ++-
 .../results/clientpositive/perf/tez/query31.q.out  |  420 +++--
 .../results/clientpositive/perf/tez/query32.q.out  |  125 +-
 .../results/clientpositive/perf/tez/query33.q.out  |  328 ++--
 .../results/clientpositive/perf/tez/query34.q.out  |  121 +-
 .../results/clientpositive/perf/tez/query35.q.out  |  259 ++-
 .../results/clientpositive/perf/tez/query36.q.out  |  121 +-
 .../results/clientpositive/perf/tez/query37.q.out  |   94 +-
 .../results/clientpositive/perf/tez/query38.q.out  |  217 ++-
 .../results/clientpositive/perf/tez/query39.q.out  |  205 ++-
 .../results/clientpositive/perf/tez/query4.q.out   |  520 +++---
 .../results/clientpositive/perf/tez/query40.q.out  |  144 +-
 .../results/clientpositive/perf/tez/query42.q.out  |   85 +-
 .../results/clientpositive/perf/tez/query43.q.out  |   87 +-
 .../results/clientpositive/perf/tez/query44.q.out  |  243 +--
 .../results/clientpositive/perf/tez/query45.q.out  |  196 ++-
 .../results/clientpositive/perf/tez/query46.q.out  |  141 +-
 .../results/clientpositive/perf/tez/query47.q.out  |  239 ++-
 .../results/clientpositive/perf/tez/query48.q.out  |  121 +-
 .../results/clientpositive/perf/tez/query49.q.out  |  315 ++--
 .../results/clientpositive/perf/tez/query5.q.out   |  402 ++---
 .../results/clientpositive/perf/tez/query50.q.out  |  136 +-
 .../results/clientpositive/perf/tez/query51.q.out  |  118 +-
 .../results/clientpositive/perf/tez/query52.q.out  |   85 +-
 .../results/clientpositive/perf/tez/query53.q.out  |  115 +-
 .../results/clientpositive/perf/tez/query54.q.out  |  447 +++--
 .../results/clientpositive/perf/tez/query55.q.out  |   85 +-
 .../results/clientpositive/perf/tez/query56.q.out  |  328 ++--
 .../results/clientpositive/perf/tez/query57.q.out  |  239 ++-
 .../results/clientpositive/perf/tez/query58.q.out  |  327 ++--
 .../results/clientpositive/perf/tez/query59.q.out  |  213 ++-
 .../results/clientpositive/perf/tez/query6.q.out   |  259 ++-
 .../results/clientpositive/perf/tez/query60.q.out  |  336 ++--
 .../results/clientpositive/perf/tez/query61.q.out  |  230 ++-
 .../results/clientpositive/perf/tez/query63.q.out  |  115 +-
 .../results/clientpositive/perf/tez/query64.q.out  |  606 ++++---
 .../results/clientpositive/perf/tez/query65.q.out  |  188 +--
 .../results/clientpositive/perf/tez/query66.q.out  |  248 ++-
 .../results/clientpositive/perf/tez/query67.q.out  |  123 +-
 .../results/clientpositive/perf/tez/query68.q.out  |  141 +-
 .../results/clientpositive/perf/tez/query69.q.out  |  265 ++-
 .../results/clientpositive/perf/tez/query7.q.out   |  128 +-
 .../results/clientpositive/perf/tez/query70.q.out  |  171 +-
 .../results/clientpositive/perf/tez/query71.q.out  |  245 +--
 .../results/clientpositive/perf/tez/query72.q.out  |  286 ++--
 .../results/clientpositive/perf/tez/query73.q.out  |  121 +-
 .../results/clientpositive/perf/tez/query74.q.out  |  319 ++--
 .../results/clientpositive/perf/tez/query75.q.out  |  557 +++----
 .../results/clientpositive/perf/tez/query76.q.out  |  221 ++-
 .../results/clientpositive/perf/tez/query77.q.out  |  406 ++---
 .../results/clientpositive/perf/tez/query78.q.out  |  282 ++--
 .../results/clientpositive/perf/tez/query79.q.out  |  123 +-
 .../results/clientpositive/perf/tez/query8.q.out   |  243 ++-
 .../results/clientpositive/perf/tez/query80.q.out  |  438 +++--
 .../results/clientpositive/perf/tez/query81.q.out  |  223 ++-
 .../results/clientpositive/perf/tez/query82.q.out  |   94 +-
 .../results/clientpositive/perf/tez/query83.q.out  |  282 ++--
 .../results/clientpositive/perf/tez/query84.q.out  |  140 +-
 .../results/clientpositive/perf/tez/query85.q.out  |  193 +--
 .../results/clientpositive/perf/tez/query86.q.out  |   93 +-
 .../results/clientpositive/perf/tez/query87.q.out  |  249 ++-
 .../results/clientpositive/perf/tez/query88.q.out  |  775 +++++----
 .../results/clientpositive/perf/tez/query89.q.out  |  121 +-
 .../results/clientpositive/perf/tez/query9.q.out   |  596 +++----
 .../results/clientpositive/perf/tez/query90.q.out  |  179 +-
 .../results/clientpositive/perf/tez/query91.q.out  |  191 +--
 .../results/clientpositive/perf/tez/query92.q.out  |  135 +-
 .../results/clientpositive/perf/tez/query93.q.out  |   81 +-
 .../results/clientpositive/perf/tez/query94.q.out  |  177 +-
 .../results/clientpositive/perf/tez/query95.q.out  |  261 ++-
 .../results/clientpositive/perf/tez/query96.q.out  |  107 +-
 .../results/clientpositive/perf/tez/query97.q.out  |  108 +-
 .../results/clientpositive/perf/tez/query98.q.out  |   87 +-
 .../results/clientpositive/perf/tez/query99.q.out  |  137 +-
 ql/src/test/results/clientpositive/plan_json.q.out |    2 +-
 .../clientpositive/ppd_repeated_alias.q.out        |   42 +-
 .../runtime_skewjoin_mapjoin_spark.q.out           |  724 ++++----
 .../results/clientpositive/smb_mapjoin_25.q.out    |  337 ++--
 .../spark/bucket_map_join_tez1.q.out               |  182 +-
 .../clientpositive/spark/column_access_stats.q.out |   48 +-
 .../results/clientpositive/spark/date_udf.q.out    |   45 +
 .../test/results/clientpositive/spark/join28.q.out |   75 +-
 .../clientpositive/spark/join32_lessSize.q.out     |  458 +++--
 .../clientpositive/spark/list_bucket_dml_2.q.out   |    2 +-
 .../clientpositive/spark/mapjoin_subquery.q.out    |  146 +-
 .../clientpositive/spark/mapjoin_subquery2.q.out   |   41 +-
 .../spark/runtime_skewjoin_mapjoin_spark.q.out     |  233 ++-
 .../clientpositive/spark/smb_mapjoin_25.q.out      |  100 +-
 .../clientpositive/spark/spark_use_op_stats.q.out  |  116 +-
 .../clientpositive/spark/subquery_scalar.q.out     |   34 +-
 .../results/clientpositive/spark/timestamp_1.q.out |   80 +-
 .../results/clientpositive/spark/timestamp_2.q.out |   80 +-
 .../results/clientpositive/spark/timestamp_3.q.out |   74 +-
 .../spark/vectorized_nested_mapjoin.q.out          |   68 +-
 .../test/results/clientpositive/timestamp_1.q.out  |   80 +-
 .../test/results/clientpositive/timestamp_2.q.out  |   80 +-
 .../test/results/clientpositive/timestamp_3.q.out  |   74 +-
 .../results/clientpositive/udf_to_boolean.q.out    |    9 +
 .../clientpositive/vector_case_when_2.q.out        |   36 +-
 .../clientpositive/vector_outer_join3.q.out        |    6 +-
 .../clientpositive/vector_outer_join4.q.out        |    6 +-
 .../clientpositive/vector_outer_join6.q.out        |    4 +-
 .../results/clientpositive/vectorized_casts.q.out  |   54 +-
 .../apache/hadoop/hive/serde2/RandomTypeUtil.java  |    4 +-
 .../primitive/PrimitiveObjectInspectorUtils.java   |   33 +-
 .../hive/service/cli/thrift/ThriftHttpServlet.java |    9 +-
 .../org/apache/hive/service/TestCookieSigner.java  |   55 +-
 .../cli/thrift/ThriftCliServiceTestWithCookie.java |  231 ---
 664 files changed, 76811 insertions(+), 16784 deletions(-)

diff --git a/common/src/java/org/apache/hadoop/hive/common/type/Date.java b/common/src/java/org/apache/hadoop/hive/common/type/Date.java
index b32dac2c996..272e87f2e7e 100644
--- a/common/src/java/org/apache/hadoop/hive/common/type/Date.java
+++ b/common/src/java/org/apache/hadoop/hive/common/type/Date.java
@@ -27,6 +27,8 @@ import java.time.format.DateTimeFormatter;
 import java.time.format.DateTimeFormatterBuilder;
 import java.time.format.ResolverStyle;
 import java.time.format.SignStyle;
+import java.time.temporal.ChronoField;
+import java.util.Objects;
 
 import static java.time.temporal.ChronoField.DAY_OF_MONTH;
 import static java.time.temporal.ChronoField.MONTH_OF_YEAR;
@@ -157,11 +159,28 @@ public class Date implements Comparable<Date> {
         Instant.ofEpochMilli(epochMilli), ZoneOffset.UTC).toLocalDate();
   }
 
-  public static Date valueOf(String s) {
-    s = s.trim();
+  /**
+   * Obtains an instance of Date from a text string such as 2021-02-22T09:39:27.
+   * Other supported formats are "2021-02-22T09:39:27Z", "2021-02-22 09:39:27",
+   * "2021-02-22T09:39:27+00:00", "2021-02-22". Any time information is simply
+   * dropped.
+   *
+   * @param text the text to parse, not null
+   * @return The {@code Date} objects parsed from the text
+   * @throws IllegalArgumentException if the text cannot be parsed into a
+   *           {@code Date}
+   * @throws NullPointerException if {@code text} is null
+   */
+  public static Date valueOf(final String text) {
+    String s = Objects.requireNonNull(text).trim();
     int idx = s.indexOf(" ");
     if (idx != -1) {
       s = s.substring(0, idx);
+    } else {
+      idx = s.indexOf('T');
+      if (idx != -1) {
+        s = s.substring(0, idx);
+      }
     }
     LocalDate localDate;
     try {
diff --git a/common/src/java/org/apache/hadoop/hive/common/type/TimestampTZUtil.java b/common/src/java/org/apache/hadoop/hive/common/type/TimestampTZUtil.java
index 40a846dbb6d..6ae51ebc4a9 100644
--- a/common/src/java/org/apache/hadoop/hive/common/type/TimestampTZUtil.java
+++ b/common/src/java/org/apache/hadoop/hive/common/type/TimestampTZUtil.java
@@ -38,6 +38,7 @@ import java.util.TimeZone;
 import java.util.regex.Matcher;
 import java.util.regex.Pattern;
 
+import org.apache.hive.common.util.DateUtils;
 import org.slf4j.Logger;
 import org.slf4j.LoggerFactory;
 
@@ -57,7 +58,6 @@ public class TimestampTZUtil {
     builder.optionalStart().appendLiteral(" ").append(DateTimeFormatter.ofPattern("HH:mm:ss")).
         optionalStart().appendFraction(ChronoField.NANO_OF_SECOND, 1, 9, true).
         optionalEnd().optionalEnd();
-
     // Zone part
     builder.optionalStart().appendLiteral(" ").optionalEnd();
     builder.optionalStart().appendZoneText(TextStyle.NARROW).optionalEnd();
@@ -197,4 +197,8 @@ public class TimestampTZUtil {
     return Timestamp.ofEpochSecond(localDateTimeAtToZone.toEpochSecond(ZoneOffset.UTC),
         localDateTimeAtToZone.getNano());
   }
+
+  public static double convertTimestampTZToDouble(TimestampTZ timestampTZ) {
+    return timestampTZ.getEpochSecond() + timestampTZ.getNanos() / DateUtils.NANOS_PER_SEC;
+  }
 }
diff --git a/common/src/java/org/apache/hadoop/hive/common/type/TimestampUtils.java b/common/src/java/org/apache/hadoop/hive/common/type/TimestampUtils.java
index 8df18399651..f346903a766 100644
--- a/common/src/java/org/apache/hadoop/hive/common/type/TimestampUtils.java
+++ b/common/src/java/org/apache/hadoop/hive/common/type/TimestampUtils.java
@@ -19,9 +19,12 @@
 package org.apache.hadoop.hive.common.type;
 
 import org.apache.hadoop.hive.serde2.io.HiveDecimalWritable;
+import org.apache.hive.common.util.DateParser;
 
 import java.math.BigDecimal;
 import java.time.DateTimeException;
+import java.time.format.DateTimeParseException;
+import java.util.Objects;
 
 /**
  * Utilities for Timestamps and the relevant conversions.
@@ -170,12 +173,23 @@ public class TimestampUtils {
 
   private static final int DATE_LENGTH = "YYYY-MM-DD".length();
 
-  public static Timestamp stringToTimestamp(String s) {
-    s = s.trim();
+  /**
+   * Convert (parse) a String into a Timestamp.
+   *
+   * @param text The text to parse
+   * @return The Timestamp parsed from the string
+   * @throws IllegalArgumentException if {@code text} cannot be parsed
+   * @throws NullPointerException if {@code text} is null
+   */
+  public static Timestamp stringToTimestamp(final String text) {
+    final String s = Objects.requireNonNull(text).trim();
     // Handle simpler cases directly avoiding exceptions
     if (s.length() == DATE_LENGTH) {
-      // Its a date!
-      return Timestamp.ofEpochMilli(Date.valueOf(s).toEpochMilli());
+      Date d = DateParser.parseDate(s);
+      if (d == null) {
+        throw new IllegalArgumentException("Cannot parse date: " + text);
+      }
+      return Timestamp.ofEpochMilli(d.toEpochMilli());
     }
     try {
       return Timestamp.valueOf(s);
diff --git a/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java b/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
index 11e30a7b37e..94878551f7e 100644
--- a/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
+++ b/common/src/java/org/apache/hadoop/hive/conf/HiveConf.java
@@ -2261,6 +2261,9 @@ public class HiveConf extends Configuration {
         "When hive.optimize.limittranspose is true, this variable specifies the minimal reduction in the\n" +
         "number of tuples of the outer input of the join or the input of the union that you should get in order to apply the rule."),
 
+    HIVE_OPTIMIZE_CONSTRAINTS_JOIN("hive.optimize.constraints.join", true, "Whether to use referential constraints\n" +
+        "to optimize (remove or transform) join operators"),
+
     HIVE_OPTIMIZE_REDUCE_WITH_STATS("hive.optimize.filter.stats.reduction", false, "Whether to simplify comparison\n" +
         "expressions in filter operators using column stats"),
 
diff --git a/common/src/java/org/apache/hive/common/util/DateParser.java b/common/src/java/org/apache/hive/common/util/DateParser.java
index 5db14f19069..a970d64f25a 100644
--- a/common/src/java/org/apache/hive/common/util/DateParser.java
+++ b/common/src/java/org/apache/hive/common/util/DateParser.java
@@ -17,35 +17,71 @@
  */
 package org.apache.hive.common.util;
 
+import java.util.Objects;
+
 import org.apache.hadoop.hive.common.type.Date;
 
+import com.google.common.cache.CacheBuilder;
+import com.google.common.cache.CacheLoader;
+import com.google.common.cache.LoadingCache;
+
 /**
  * Date parser class for Hive.
  */
-public class DateParser {
+public final class DateParser {
+
+  /**
+   * Cache 10 years worth of dates before evicting. Note, it is possible to cast
+   * timestamps (Date and Time) into Dates, therefore the effectiveness of the
+   * cache may be limited if the Time portion of the timestamp has many distinct
+   * values.
+   */
+  private static final LoadingCache<String, Date> DATE_CACHE =
+      CacheBuilder.newBuilder().maximumSize(10 * 365).build(new CacheLoader<String, Date>() {
+        @Override
+        public Date load(final String text) throws Exception {
+          return Date.valueOf(text);
+        }
+      });
+
+  private DateParser() {
+  }
 
-  public DateParser() {
- }
+  /**
+   * Obtains an instance of Date from a text string such as 2021-02-21.
+   *
+   * @param text the text to parse
+   * @return The Date objects generated from parsing the text or null if the
+   *         text could not be parsed
+   * @throws NullPointerException if {@code text} is null
+   */
+  public static Date parseDate(final String text) {
+    Objects.requireNonNull(text);
 
-  public Date parseDate(String strValue) {
+    // Date is a mutable class; do not return cached value
     Date result = new Date();
-    if (parseDate(strValue, result)) {
-      return result;
-    }
-    return null;
+    return (parseDate(text, result)) ? result : null;
   }
 
-  public boolean parseDate(String strValue, Date result) {
-    Date parsedVal;
+  /**
+   * Obtains an instance of Date from a text string such as 2021-02-21. The
+   * {@code result} is not modified if parsing of the {@code text} fails.
+   *
+   * @param text the text to parse
+   * @param result the {@code Date} to load the results into
+   * @return True if parsing was successful; false otherwise.
+   * @throws NullPointerException if {@code text} or {@code result} is null
+   */
+  public static boolean parseDate(final String text, final Date result) {
+    Objects.requireNonNull(text);
+    Objects.requireNonNull(result);
+
     try {
-      parsedVal = Date.valueOf(strValue);
-    } catch (IllegalArgumentException e) {
-      parsedVal = null;
-    }
-    if (parsedVal == null) {
+      Date date = DATE_CACHE.get(text);
+      result.setTimeInMillis(date.toEpochMilli());
+      return true;
+    } catch (Exception e) {
       return false;
     }
-    result.setTimeInMillis(parsedVal.toEpochMilli());
-    return true;
   }
 }
diff --git a/common/src/test/org/apache/hive/common/util/TestDateParser.java b/common/src/test/org/apache/hive/common/util/TestDateParser.java
index b4c4602f97f..34695a469b3 100644
--- a/common/src/test/org/apache/hive/common/util/TestDateParser.java
+++ b/common/src/test/org/apache/hive/common/util/TestDateParser.java
@@ -17,28 +17,30 @@
  */
 package org.apache.hive.common.util;
 
-import static org.junit.Assert.*;
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNull;
+import static org.junit.Assert.assertTrue;
 
 import org.apache.hadoop.hive.common.type.Date;
 import org.junit.Test;
 
 public class TestDateParser {
-  DateParser parser = new DateParser();
-  Date date = new Date();
+  private Date date = new Date();
 
   void checkValidCase(String strValue, Date expected) {
-    Date dateValue = parser.parseDate(strValue);
+    Date dateValue = DateParser.parseDate(strValue);
     assertEquals(expected, dateValue);
 
-    assertTrue(parser.parseDate(strValue, date));
+    assertTrue(DateParser.parseDate(strValue, date));
     assertEquals(expected, date);
   }
 
   void checkInvalidCase(String strValue) {
-    Date dateValue = parser.parseDate(strValue);
+    Date dateValue = DateParser.parseDate(strValue);
     assertNull(dateValue);
 
-    assertFalse(parser.parseDate(strValue, date));
+    assertFalse(DateParser.parseDate(strValue, date));
   }
 
   @Test
diff --git a/data/conf/perf-reg/spark/hive-site.xml b/data/conf/perf-reg/spark/hive-site.xml
index e52a2f9fa77..9a17197a77c 100644
--- a/data/conf/perf-reg/spark/hive-site.xml
+++ b/data/conf/perf-reg/spark/hive-site.xml
@@ -144,10 +144,15 @@
 
 <property>
   <name>hive.support.concurrency</name>
-  <value>false</value>
+  <value>true</value>
   <description>Whether hive supports concurrency or not. A zookeeper instance must be up and running for the default hive lock manager to support read-write locks.</description>
 </property>
 
+<property>
+  <name>hive.txn.manager</name>
+  <value>org.apache.hadoop.hive.ql.lockmgr.DbTxnManager</value>
+</property>
+
 <property>
   <name>fs.pfile.impl</name>
   <value>org.apache.hadoop.fs.ProxyLocalFileSystem</value>
diff --git a/data/conf/perf-reg/tez/hive-site.xml b/data/conf/perf-reg/tez/hive-site.xml
index 78a5481e033..7d38be04cab 100644
--- a/data/conf/perf-reg/tez/hive-site.xml
+++ b/data/conf/perf-reg/tez/hive-site.xml
@@ -168,10 +168,15 @@
 
 <property>
   <name>hive.support.concurrency</name>
-  <value>false</value>
+  <value>true</value>
   <description>Whether hive supports concurrency or not. A zookeeper instance must be up and running for the default hive lock manager to support read-write locks.</description>
 </property>
 
+<property>
+  <name>hive.txn.manager</name>
+  <value>org.apache.hadoop.hive.ql.lockmgr.DbTxnManager</value>
+</property>
+
 <property>
   <name>fs.pfile.impl</name>
   <value>org.apache.hadoop.fs.ProxyLocalFileSystem</value>
@@ -292,4 +297,14 @@
   <value>99</value>
 </property>
 
+<property>
+  <name>hive.merge.nway.joins</name>
+  <value>false</value>
+</property>
+
+<property>
+  <name>hive.stats.fetch.column.stats</name>
+  <value>true</value>
+</property>
+
 </configuration>
diff --git a/data/scripts/q_perf_test_init.sql b/data/scripts/q_perf_test_init.sql
index d27215b4cb5..020e2d0c49e 100644
--- a/data/scripts/q_perf_test_init.sql
+++ b/data/scripts/q_perf_test_init.sql
@@ -1,5 +1,5 @@
 drop table if exists call_center;
-create external table call_center
+create table call_center
 (
     cc_call_center_sk        int,
     cc_call_center_id        string,
@@ -34,10 +34,10 @@ create external table call_center
     cc_tax_percentage        decimal(5,2)
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 drop table if exists catalog_page;
-create external table catalog_page
+create table catalog_page
 (
     cp_catalog_page_sk       int,
     cp_catalog_page_id       string,
@@ -50,11 +50,11 @@ create external table catalog_page
     cp_type                  string
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists catalog_returns;
-create external table catalog_returns
+create table catalog_returns
 (
     cr_returned_date_sk        int,
     cr_returned_time_sk        int,
@@ -85,11 +85,11 @@ create external table catalog_returns
     cr_net_loss                decimal(7,2)
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists catalog_sales;
-create external table catalog_sales
+create table catalog_sales
 (
     cs_sold_date_sk            int,
     cs_sold_time_sk            int,
@@ -127,11 +127,11 @@ create external table catalog_sales
     cs_net_profit              decimal(7,2)
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists customer;
-create external table customer
+create table customer
 (
     c_customer_sk             int,
     c_customer_id             string,
@@ -153,11 +153,11 @@ create external table customer
     c_last_review_date        string
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists customer_address;
-create external table customer_address
+create table customer_address
 (
     ca_address_sk             int,
     ca_address_id             string,
@@ -174,11 +174,11 @@ create external table customer_address
     ca_location_type          string
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists customer_demographics;
-create external table customer_demographics
+create table customer_demographics
 (
     cd_demo_sk                int,
     cd_gender                 string,
@@ -191,11 +191,11 @@ create external table customer_demographics
     cd_dep_college_count      int 
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists date_dim;
-create external table date_dim
+create table date_dim
 (
     d_date_sk                 int,
     d_date_id                 string,
@@ -227,11 +227,11 @@ create external table date_dim
     d_current_year            string 
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists household_demographics;
-create external table household_demographics
+create table household_demographics
 (
     hd_demo_sk                int,
     hd_income_band_sk         int,
@@ -240,22 +240,22 @@ create external table household_demographics
     hd_vehicle_count          int
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists income_band;
-create external table income_band
+create table income_band
 (
     ib_income_band_sk        int,
     ib_lower_bound           int,
     ib_upper_bound           int
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists inventory;
-create external table inventory
+create table inventory
 (
     inv_date_sk                int,
     inv_item_sk                int,
@@ -263,11 +263,11 @@ create external table inventory
     inv_quantity_on_hand       int
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists item;
-create external table item
+create table item
 (
     i_item_sk                 int,
     i_item_id                 string,
@@ -293,11 +293,11 @@ create external table item
     i_product_name            string
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists promotion;
-create external table promotion
+create table promotion
 (
     p_promo_sk                int,
     p_promo_id                string,
@@ -320,22 +320,22 @@ create external table promotion
     p_discount_active         string 
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists reason;
-create external table reason
+create table reason
 (
     r_reason_sk         int,
     r_reason_id         string,
     r_reason_desc       string
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists ship_mode;
-create external table ship_mode
+create table ship_mode
 (
     sm_ship_mode_sk           int,
     sm_ship_mode_id           string,
@@ -345,11 +345,11 @@ create external table ship_mode
     sm_contract               string
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists store;
-create external table store
+create table store
 (
     s_store_sk                int,
     s_store_id                string,
@@ -382,11 +382,11 @@ create external table store
     s_tax_precentage          decimal(5,2)
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists store_returns;
-create external table store_returns
+create table store_returns
 (
     sr_returned_date_sk      int,
     sr_return_time_sk        int,
@@ -410,11 +410,11 @@ create external table store_returns
     sr_net_loss              decimal(7,2)
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists store_sales;
-create external table store_sales
+create table store_sales
 (
     ss_sold_date_sk           int,
     ss_sold_time_sk           int,
@@ -441,11 +441,11 @@ create external table store_sales
     ss_net_profit             decimal(7,2)
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists time_dim;
-create external table time_dim
+create table time_dim
 (
     t_time_sk                 int,
     t_time_id                 string,
@@ -459,11 +459,11 @@ create external table time_dim
     t_meal_time               string
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists warehouse;
-create external table warehouse
+create table warehouse
 (
     w_warehouse_sk            int,
     w_warehouse_id            string,
@@ -481,11 +481,11 @@ create external table warehouse
     w_gmt_offset              decimal(5,2)
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists web_page;
-create external table web_page
+create table web_page
 (
     wp_web_page_sk           int,
     wp_web_page_id           string,
@@ -503,11 +503,11 @@ create external table web_page
     wp_max_ad_count          int
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists web_returns;
-create external table web_returns
+create table web_returns
 (
     wr_returned_date_sk        int,
     wr_returned_time_sk        int,
@@ -535,11 +535,11 @@ create external table web_returns
     wr_net_loss                decimal(7,2)
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists web_sales;
-create external table web_sales
+create table web_sales
 (
     ws_sold_date_sk           int,
     ws_sold_time_sk           int,
@@ -577,11 +577,11 @@ create external table web_sales
     ws_net_profit             decimal(7,2)
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
 drop table if exists web_site;
-create external table web_site
+create table web_site
 (
     web_site_sk             int,
     web_site_id             string,
@@ -611,6 +611,6 @@ create external table web_site
     web_tax_percentage      decimal(5,2)
 )
 row format delimited fields terminated by '\t'
-STORED AS ORC tblproperties ("orc.compress"="ZLIB");
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
 
 
diff --git a/data/scripts/q_perf_test_init_constraints.sql b/data/scripts/q_perf_test_init_constraints.sql
new file mode 100644
index 00000000000..3b3f503ee43
--- /dev/null
+++ b/data/scripts/q_perf_test_init_constraints.sql
@@ -0,0 +1,771 @@
+drop table if exists call_center;
+create table call_center
+(
+    cc_call_center_sk        int,
+    cc_call_center_id        string,
+    cc_rec_start_date        string,
+    cc_rec_end_date          string,
+    cc_closed_date_sk        int,
+    cc_open_date_sk          int,
+    cc_name                  string,
+    cc_class                 string,
+    cc_employees             int,
+    cc_sq_ft                 int,
+    cc_hours                 string,
+    cc_manager               string,
+    cc_mkt_id                int,
+    cc_mkt_class             string,
+    cc_mkt_desc              string,
+    cc_market_manager        string,
+    cc_division              int,
+    cc_division_name         string,
+    cc_company               int,
+    cc_company_name          string,
+    cc_street_number         string,
+    cc_street_name           string,
+    cc_street_type           string,
+    cc_suite_number          string,
+    cc_city                  string,
+    cc_county                string,
+    cc_state                 string,
+    cc_zip                   string,
+    cc_country               string,
+    cc_gmt_offset            decimal(5,2),
+    cc_tax_percentage        decimal(5,2)
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+drop table if exists catalog_page;
+create table catalog_page
+(
+    cp_catalog_page_sk       int,
+    cp_catalog_page_id       string,
+    cp_start_date_sk         int,
+    cp_end_date_sk           int,
+    cp_department            string,
+    cp_catalog_number        int,
+    cp_catalog_page_number   int,
+    cp_description           string,
+    cp_type                  string
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists catalog_returns;
+create table catalog_returns
+(
+    cr_returned_date_sk        int,
+    cr_returned_time_sk        int,
+    cr_item_sk                 int,
+    cr_refunded_customer_sk    int,
+    cr_refunded_cdemo_sk       int,
+    cr_refunded_hdemo_sk       int,
+    cr_refunded_addr_sk        int,
+    cr_returning_customer_sk   int,
+    cr_returning_cdemo_sk      int,
+    cr_returning_hdemo_sk      int,
+    cr_returning_addr_sk       int,
+    cr_call_center_sk          int,
+    cr_catalog_page_sk         int,
+    cr_ship_mode_sk            int,
+    cr_warehouse_sk            int,
+    cr_reason_sk               int,
+    cr_order_number            int,
+    cr_return_quantity         int,
+    cr_return_amount           decimal(7,2),
+    cr_return_tax              decimal(7,2),
+    cr_return_amt_inc_tax      decimal(7,2),
+    cr_fee                     decimal(7,2),
+    cr_return_ship_cost        decimal(7,2),
+    cr_refunded_cash           decimal(7,2),
+    cr_reversed_charge         decimal(7,2),
+    cr_store_credit            decimal(7,2),
+    cr_net_loss                decimal(7,2)
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists catalog_sales;
+create table catalog_sales
+(
+    cs_sold_date_sk            int,
+    cs_sold_time_sk            int,
+    cs_ship_date_sk            int,
+    cs_bill_customer_sk        int,
+    cs_bill_cdemo_sk           int,
+    cs_bill_hdemo_sk           int,
+    cs_bill_addr_sk            int,
+    cs_ship_customer_sk        int,
+    cs_ship_cdemo_sk           int,
+    cs_ship_hdemo_sk           int,
+    cs_ship_addr_sk            int,
+    cs_call_center_sk          int,
+    cs_catalog_page_sk         int,
+    cs_ship_mode_sk            int,
+    cs_warehouse_sk            int,
+    cs_item_sk                 int,
+    cs_promo_sk                int,
+    cs_order_number            int,
+    cs_quantity                int,
+    cs_wholesale_cost          decimal(7,2),
+    cs_list_price              decimal(7,2),
+    cs_sales_price             decimal(7,2),
+    cs_ext_discount_amt        decimal(7,2),
+    cs_ext_sales_price         decimal(7,2),
+    cs_ext_wholesale_cost      decimal(7,2),
+    cs_ext_list_price          decimal(7,2),
+    cs_ext_tax                 decimal(7,2),
+    cs_coupon_amt              decimal(7,2),
+    cs_ext_ship_cost           decimal(7,2),
+    cs_net_paid                decimal(7,2),
+    cs_net_paid_inc_tax        decimal(7,2),
+    cs_net_paid_inc_ship       decimal(7,2),
+    cs_net_paid_inc_ship_tax   decimal(7,2),
+    cs_net_profit              decimal(7,2)
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists customer;
+create table customer
+(
+    c_customer_sk             int,
+    c_customer_id             string,
+    c_current_cdemo_sk        int,
+    c_current_hdemo_sk        int,
+    c_current_addr_sk         int,
+    c_first_shipto_date_sk    int,
+    c_first_sales_date_sk     int,
+    c_salutation              string,
+    c_first_name              string,
+    c_last_name               string,
+    c_preferred_cust_flag     string,
+    c_birth_day               int,
+    c_birth_month             int,
+    c_birth_year              int,
+    c_birth_country           string,
+    c_login                   string,
+    c_email_address           string,
+    c_last_review_date        string
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists customer_address;
+create table customer_address
+(
+    ca_address_sk             int,
+    ca_address_id             string,
+    ca_street_number          string,
+    ca_street_name            string,
+    ca_street_type            string,
+    ca_suite_number           string,
+    ca_city                   string,
+    ca_county                 string,
+    ca_state                  string,
+    ca_zip                    string,
+    ca_country                string,
+    ca_gmt_offset             decimal(5,2),
+    ca_location_type          string
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists customer_demographics;
+create table customer_demographics
+(
+    cd_demo_sk                int,
+    cd_gender                 string,
+    cd_marital_status         string,
+    cd_education_status       string,
+    cd_purchase_estimate      int,
+    cd_credit_rating          string,
+    cd_dep_count              int,
+    cd_dep_employed_count     int,
+    cd_dep_college_count      int 
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists date_dim;
+create table date_dim
+(
+    d_date_sk                 int,
+    d_date_id                 string,
+    d_date                    string,
+    d_month_seq               int,
+    d_week_seq                int,
+    d_quarter_seq             int,
+    d_year                    int,
+    d_dow                     int,
+    d_moy                     int,
+    d_dom                     int,
+    d_qoy                     int,
+    d_fy_year                 int,
+    d_fy_quarter_seq          int,
+    d_fy_week_seq             int,
+    d_day_name                string,
+    d_quarter_name            string,
+    d_holiday                 string,
+    d_weekend                 string,
+    d_following_holiday       string,
+    d_first_dom               int,
+    d_last_dom                int,
+    d_same_day_ly             int,
+    d_same_day_lq             int,
+    d_current_day             string,
+    d_current_week            string,
+    d_current_month           string,
+    d_current_quarter         string,
+    d_current_year            string 
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists household_demographics;
+create table household_demographics
+(
+    hd_demo_sk                int,
+    hd_income_band_sk         int,
+    hd_buy_potential          string,
+    hd_dep_count              int,
+    hd_vehicle_count          int
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists income_band;
+create table income_band
+(
+    ib_income_band_sk        int,
+    ib_lower_bound           int,
+    ib_upper_bound           int
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists inventory;
+create table inventory
+(
+    inv_date_sk                int,
+    inv_item_sk                int,
+    inv_warehouse_sk           int,
+    inv_quantity_on_hand       int
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists item;
+create table item
+(
+    i_item_sk                 int,
+    i_item_id                 string,
+    i_rec_start_date          string,
+    i_rec_end_date            string,
+    i_item_desc               string,
+    i_current_price           decimal(7,2),
+    i_wholesale_cost          decimal(7,2),
+    i_brand_id                int,
+    i_brand                   string,
+    i_class_id                int,
+    i_class                   string,
+    i_category_id             int,
+    i_category                string,
+    i_manufact_id             int,
+    i_manufact                string,
+    i_size                    string,
+    i_formulation             string,
+    i_color                   string,
+    i_units                   string,
+    i_container               string,
+    i_manager_id              int,
+    i_product_name            string
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists promotion;
+create table promotion
+(
+    p_promo_sk                int,
+    p_promo_id                string,
+    p_start_date_sk           int,
+    p_end_date_sk             int,
+    p_item_sk                 int,
+    p_cost                    decimal(15,2),
+    p_response_target         int,
+    p_promo_name              string,
+    p_channel_dmail           string,
+    p_channel_email           string,
+    p_channel_catalog         string,
+    p_channel_tv              string,
+    p_channel_radio           string,
+    p_channel_press           string,
+    p_channel_event           string,
+    p_channel_demo            string,
+    p_channel_details         string,
+    p_purpose                 string,
+    p_discount_active         string 
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists reason;
+create table reason
+(
+    r_reason_sk         int,
+    r_reason_id         string,
+    r_reason_desc       string
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists ship_mode;
+create table ship_mode
+(
+    sm_ship_mode_sk           int,
+    sm_ship_mode_id           string,
+    sm_type                   string,
+    sm_code                   string,
+    sm_carrier                string,
+    sm_contract               string
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists store;
+create table store
+(
+    s_store_sk                int,
+    s_store_id                string,
+    s_rec_start_date          string,
+    s_rec_end_date            string,
+    s_closed_date_sk          int,
+    s_store_name              string,
+    s_number_employees        int,
+    s_floor_space             int,
+    s_hours                   string,
+    s_manager                 string,
+    s_market_id               int,
+    s_geography_class         string,
+    s_market_desc             string,
+    s_market_manager          string,
+    s_division_id             int,
+    s_division_name           string,
+    s_company_id              int,
+    s_company_name            string,
+    s_street_number           string,
+    s_street_name             string,
+    s_street_type             string,
+    s_suite_number            string,
+    s_city                    string,
+    s_county                  string,
+    s_state                   string,
+    s_zip                     string,
+    s_country                 string,
+    s_gmt_offset              decimal(5,2),
+    s_tax_precentage          decimal(5,2)
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists store_returns;
+create table store_returns
+(
+    sr_returned_date_sk      int,
+    sr_return_time_sk        int,
+    sr_item_sk               int,
+    sr_customer_sk           int,
+    sr_cdemo_sk              int,
+    sr_hdemo_sk              int,
+    sr_addr_sk               int,
+    sr_store_sk              int,
+    sr_reason_sk             int,
+    sr_ticket_number         int,
+    sr_return_quantity       int,
+    sr_return_amt            decimal(7,2),
+    sr_return_tax            decimal(7,2),
+    sr_return_amt_inc_tax    decimal(7,2),
+    sr_fee                   decimal(7,2),
+    sr_return_ship_cost      decimal(7,2),
+    sr_refunded_cash         decimal(7,2),
+    sr_reversed_charge       decimal(7,2),
+    sr_store_credit          decimal(7,2),
+    sr_net_loss              decimal(7,2)
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists store_sales;
+create table store_sales
+(
+    ss_sold_date_sk           int,
+    ss_sold_time_sk           int,
+    ss_item_sk                int,
+    ss_customer_sk            int,
+    ss_cdemo_sk               int,
+    ss_hdemo_sk               int,
+    ss_addr_sk                int,
+    ss_store_sk               int,
+    ss_promo_sk               int,
+    ss_ticket_number          int,
+    ss_quantity               int,
+    ss_wholesale_cost         decimal(7,2),
+    ss_list_price             decimal(7,2),
+    ss_sales_price            decimal(7,2),
+    ss_ext_discount_amt       decimal(7,2),
+    ss_ext_sales_price        decimal(7,2),
+    ss_ext_wholesale_cost     decimal(7,2),
+    ss_ext_list_price         decimal(7,2),
+    ss_ext_tax                decimal(7,2),
+    ss_coupon_amt             decimal(7,2),
+    ss_net_paid               decimal(7,2),
+    ss_net_paid_inc_tax       decimal(7,2),
+    ss_net_profit             decimal(7,2)
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists time_dim;
+create table time_dim
+(
+    t_time_sk                 int,
+    t_time_id                 string,
+    t_time                    int,
+    t_hour                    int,
+    t_minute                  int,
+    t_second                  int,
+    t_am_pm                   string,
+    t_shift                   string,
+    t_sub_shift               string,
+    t_meal_time               string
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists warehouse;
+create table warehouse
+(
+    w_warehouse_sk            int,
+    w_warehouse_id            string,
+    w_warehouse_name          string,
+    w_warehouse_sq_ft         int,
+    w_street_number           string,
+    w_street_name             string,
+    w_street_type             string,
+    w_suite_number            string,
+    w_city                    string,
+    w_county                  string,
+    w_state                   string,
+    w_zip                     string,
+    w_country                 string,
+    w_gmt_offset              decimal(5,2)
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists web_page;
+create table web_page
+(
+    wp_web_page_sk           int,
+    wp_web_page_id           string,
+    wp_rec_start_date        string,
+    wp_rec_end_date          string,
+    wp_creation_date_sk      int,
+    wp_access_date_sk        int,
+    wp_autogen_flag          string,
+    wp_customer_sk           int,
+    wp_url                   string,
+    wp_type                  string,
+    wp_char_count            int,
+    wp_link_count            int,
+    wp_image_count           int,
+    wp_max_ad_count          int
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists web_returns;
+create table web_returns
+(
+    wr_returned_date_sk        int,
+    wr_returned_time_sk        int,
+    wr_item_sk                 int,
+    wr_refunded_customer_sk    int,
+    wr_refunded_cdemo_sk       int,
+    wr_refunded_hdemo_sk       int,
+    wr_refunded_addr_sk        int,
+    wr_returning_customer_sk   int,
+    wr_returning_cdemo_sk      int,
+    wr_returning_hdemo_sk      int,
+    wr_returning_addr_sk       int,
+    wr_web_page_sk             int,
+    wr_reason_sk               int,
+    wr_order_number            int,
+    wr_return_quantity         int,
+    wr_return_amt              decimal(7,2),
+    wr_return_tax              decimal(7,2),
+    wr_return_amt_inc_tax      decimal(7,2),
+    wr_fee                     decimal(7,2),
+    wr_return_ship_cost        decimal(7,2),
+    wr_refunded_cash           decimal(7,2),
+    wr_reversed_charge         decimal(7,2),
+    wr_account_credit          decimal(7,2),
+    wr_net_loss                decimal(7,2)
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists web_sales;
+create table web_sales
+(
+    ws_sold_date_sk           int,
+    ws_sold_time_sk           int,
+    ws_ship_date_sk           int,
+    ws_item_sk                int,
+    ws_bill_customer_sk       int,
+    ws_bill_cdemo_sk          int,
+    ws_bill_hdemo_sk          int,
+    ws_bill_addr_sk           int,
+    ws_ship_customer_sk       int,
+    ws_ship_cdemo_sk          int,
+    ws_ship_hdemo_sk          int,
+    ws_ship_addr_sk           int,
+    ws_web_page_sk            int,
+    ws_web_site_sk            int,
+    ws_ship_mode_sk           int,
+    ws_warehouse_sk           int,
+    ws_promo_sk               int,
+    ws_order_number           int,
+    ws_quantity               int,
+    ws_wholesale_cost         decimal(7,2),
+    ws_list_price             decimal(7,2),
+    ws_sales_price            decimal(7,2),
+    ws_ext_discount_amt       decimal(7,2),
+    ws_ext_sales_price        decimal(7,2),
+    ws_ext_wholesale_cost     decimal(7,2),
+    ws_ext_list_price         decimal(7,2),
+    ws_ext_tax                decimal(7,2),
+    ws_coupon_amt             decimal(7,2),
+    ws_ext_ship_cost          decimal(7,2),
+    ws_net_paid               decimal(7,2),
+    ws_net_paid_inc_tax       decimal(7,2),
+    ws_net_paid_inc_ship      decimal(7,2),
+    ws_net_paid_inc_ship_tax  decimal(7,2),
+    ws_net_profit             decimal(7,2)
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+drop table if exists web_site;
+create table web_site
+(
+    web_site_sk             int,
+    web_site_id             string,
+    web_rec_start_date      string,
+    web_rec_end_date        string,
+    web_name                string,
+    web_open_date_sk        int,
+    web_close_date_sk       int,
+    web_class               string,
+    web_manager             string,
+    web_mkt_id              int,
+    web_mkt_class           string,
+    web_mkt_desc            string,
+    web_market_manager      string,
+    web_company_id          int,
+    web_company_name        string,
+    web_street_number       string,
+    web_street_name         string,
+    web_street_type         string,
+    web_suite_number        string,
+    web_city                string,
+    web_county              string,
+    web_state               string,
+    web_zip                 string,
+    web_country             string,
+    web_gmt_offset          decimal(5,2),
+    web_tax_percentage      decimal(5,2)
+)
+row format delimited fields terminated by '\t'
+STORED AS ORC tblproperties ("transactional"="true", "orc.compress"="ZLIB");
+
+
+-- CONSTRAINTS
+alter table customer_address add constraint pk_ca primary key (ca_address_sk) disable novalidate rely;
+alter table customer_demographics add constraint pk_cd primary key (cd_demo_sk) disable novalidate rely;
+alter table date_dim add constraint pk_dd primary key (d_date_sk) disable novalidate rely;
+alter table warehouse add constraint pk_w primary key (w_warehouse_sk) disable novalidate rely;
+alter table ship_mode add constraint pk_sm primary key (sm_ship_mode_sk) disable novalidate rely;
+alter table time_dim add constraint pk_td primary key (t_time_sk) disable novalidate rely;
+alter table reason add constraint pk_r primary key (r_reason_sk) disable novalidate rely;
+alter table income_band add constraint pk_ib primary key (ib_income_band_sk) disable novalidate rely;
+alter table item add constraint pk_i primary key (i_item_sk) disable novalidate rely;
+alter table store add constraint pk_s primary key (s_store_sk) disable novalidate rely;
+alter table call_center add constraint pk_cc primary key (cc_call_center_sk) disable novalidate rely;
+alter table customer add constraint pk_c primary key (c_customer_sk) disable novalidate rely;
+alter table web_site add constraint pk_ws primary key (web_site_sk) disable novalidate rely;
+alter table store_returns add constraint pk_sr primary key (sr_item_sk, sr_ticket_number) disable novalidate rely;
+alter table household_demographics add constraint pk_hd primary key (hd_demo_sk) disable novalidate rely;
+alter table web_page add constraint pk_wp primary key (wp_web_page_sk) disable novalidate rely;
+alter table promotion add constraint pk_p primary key (p_promo_sk) disable novalidate rely;
+alter table catalog_page add constraint pk_cp primary key (cp_catalog_page_sk) disable novalidate rely;
+-- partition_col case
+alter table inventory add constraint pk_in primary key (inv_date_sk, inv_item_sk, inv_warehouse_sk) disable novalidate rely;
+alter table catalog_returns add constraint pk_cr primary key (cr_item_sk, cr_order_number) disable novalidate rely;
+alter table web_returns add constraint pk_wr primary key (wr_item_sk, wr_order_number) disable novalidate rely;
+alter table web_sales add constraint pk_ws2 primary key (ws_item_sk, ws_order_number) disable novalidate rely;
+alter table catalog_sales add constraint pk_cs primary key (cs_item_sk, cs_order_number) disable novalidate rely;
+alter table store_sales add constraint pk_ss primary key (ss_item_sk, ss_ticket_number) disable novalidate rely;
+
+alter table call_center add constraint cc_d1 foreign key  (cc_closed_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table call_center add constraint cc_d2 foreign key  (cc_open_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table catalog_page add constraint cp_d1 foreign key  (cp_end_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table catalog_page add constraint cp_d2 foreign key  (cp_start_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_cc foreign key  (cr_call_center_sk) references call_center (cc_call_center_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_cp foreign key  (cr_catalog_page_sk) references catalog_page (cp_catalog_page_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_cs foreign key  (cr_item_sk, cr_order_number) references catalog_sales (cs_item_sk, cs_order_number) disable novalidate rely;
+alter table catalog_returns add constraint cr_i foreign key  (cr_item_sk) references item (i_item_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_r foreign key  (cr_reason_sk) references reason (r_reason_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_a1 foreign key  (cr_refunded_addr_sk) references customer_address (ca_address_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_cd1 foreign key  (cr_refunded_cdemo_sk) references customer_demographics (cd_demo_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_c1 foreign key  (cr_refunded_customer_sk) references customer (c_customer_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_hd1 foreign key  (cr_refunded_hdemo_sk) references household_demographics (hd_demo_sk) disable novalidate rely;
+-- partition_col case
+alter table catalog_returns add constraint cr_d1 foreign key  (cr_returned_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_t foreign key  (cr_returned_time_sk) references time_dim (t_time_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_a2 foreign key  (cr_returning_addr_sk) references customer_address (ca_address_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_cd2 foreign key  (cr_returning_cdemo_sk) references customer_demographics (cd_demo_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_c2 foreign key  (cr_returning_customer_sk) references customer (c_customer_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_hd2 foreign key  (cr_returning_hdemo_sk) references household_demographics (hd_demo_sk) disable novalidate rely;
+-- alter table catalog_returns add constraint cr_d2 foreign key  (cr_ship_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_sm foreign key  (cr_ship_mode_sk) references ship_mode (sm_ship_mode_sk) disable novalidate rely;
+alter table catalog_returns add constraint cr_w2 foreign key  (cr_warehouse_sk) references warehouse (w_warehouse_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_b_a foreign key  (cs_bill_addr_sk) references customer_address (ca_address_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_b_cd foreign key  (cs_bill_cdemo_sk) references customer_demographics (cd_demo_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_b_c foreign key  (cs_bill_customer_sk) references customer (c_customer_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_b_hd foreign key  (cs_bill_hdemo_sk) references household_demographics (hd_demo_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_cc foreign key  (cs_call_center_sk) references call_center (cc_call_center_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_cp foreign key  (cs_catalog_page_sk) references catalog_page (cp_catalog_page_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_i foreign key  (cs_item_sk) references item (i_item_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_p foreign key  (cs_promo_sk) references promotion (p_promo_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_s_a foreign key  (cs_ship_addr_sk) references customer_address (ca_address_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_s_cd foreign key  (cs_ship_cdemo_sk) references customer_demographics (cd_demo_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_s_c foreign key  (cs_ship_customer_sk) references customer (c_customer_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_d1 foreign key  (cs_ship_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_s_hd foreign key  (cs_ship_hdemo_sk) references household_demographics (hd_demo_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_sm foreign key  (cs_ship_mode_sk) references ship_mode (sm_ship_mode_sk) disable novalidate rely;
+-- partition_col case
+alter table catalog_sales add constraint cs_d2 foreign key  (cs_sold_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_t foreign key  (cs_sold_time_sk) references time_dim (t_time_sk) disable novalidate rely;
+alter table catalog_sales add constraint cs_w foreign key  (cs_warehouse_sk) references warehouse (w_warehouse_sk) disable novalidate rely;
+alter table customer add constraint c_a foreign key  (c_current_addr_sk) references customer_address (ca_address_sk) disable novalidate rely;
+alter table customer add constraint c_cd foreign key  (c_current_cdemo_sk) references customer_demographics (cd_demo_sk) disable novalidate rely;
+alter table customer add constraint c_hd foreign key  (c_current_hdemo_sk) references household_demographics (hd_demo_sk) disable novalidate rely;
+alter table customer add constraint c_fsd foreign key  (c_first_sales_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table customer add constraint c_fsd2 foreign key  (c_first_shipto_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table household_demographics add constraint hd_ib foreign key  (hd_income_band_sk) references income_band (ib_income_band_sk) disable novalidate rely;
+-- partition_col case
+alter table inventory add constraint inv_d foreign key  (inv_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table inventory add constraint inv_i foreign key  (inv_item_sk) references item (i_item_sk) disable novalidate rely;
+alter table inventory add constraint inv_w foreign key  (inv_warehouse_sk) references warehouse (w_warehouse_sk) disable novalidate rely;
+alter table promotion add constraint p_end_date foreign key  (p_end_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table promotion add constraint p_i foreign key  (p_item_sk) references item (i_item_sk) disable novalidate rely;
+alter table promotion add constraint p_start_date foreign key  (p_start_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table store add constraint s_close_date foreign key  (s_closed_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table store_returns add constraint sr_a foreign key  (sr_addr_sk) references customer_address (ca_address_sk) disable novalidate rely;
+alter table store_returns add constraint sr_cd foreign key  (sr_cdemo_sk) references customer_demographics (cd_demo_sk) disable novalidate rely;
+alter table store_returns add constraint sr_c foreign key  (sr_customer_sk) references customer (c_customer_sk) disable novalidate rely;
+alter table store_returns add constraint sr_hd foreign key  (sr_hdemo_sk) references household_demographics (hd_demo_sk) disable novalidate rely;
+alter table store_returns add constraint sr_i foreign key  (sr_item_sk) references item (i_item_sk) disable novalidate rely;
+alter table store_returns add constraint sr_r foreign key  (sr_reason_sk) references reason (r_reason_sk) disable novalidate rely;
+-- partition_col case
+alter table store_returns add constraint sr_ret_d foreign key  (sr_returned_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table store_returns add constraint sr_t foreign key  (sr_return_time_sk) references time_dim (t_time_sk) disable novalidate rely;
+alter table store_returns add constraint sr_s foreign key  (sr_store_sk) references store (s_store_sk) disable novalidate rely;
+alter table store_returns add constraint sr_ss foreign key  (sr_item_sk, sr_ticket_number) references store_sales (ss_item_sk, ss_ticket_number) disable novalidate rely;
+alter table store_sales add constraint ss_a foreign key  (ss_addr_sk) references customer_address (ca_address_sk) disable novalidate rely;
+alter table store_sales add constraint ss_cd foreign key  (ss_cdemo_sk) references customer_demographics (cd_demo_sk) disable novalidate rely;
+alter table store_sales add constraint ss_c foreign key  (ss_customer_sk) references customer (c_customer_sk) disable novalidate rely;
+alter table store_sales add constraint ss_hd foreign key  (ss_hdemo_sk) references household_demographics (hd_demo_sk) disable novalidate rely;
+alter table store_sales add constraint ss_i foreign key  (ss_item_sk) references item (i_item_sk) disable novalidate rely;
+alter table store_sales add constraint ss_p foreign key  (ss_promo_sk) references promotion (p_promo_sk) disable novalidate rely;
+-- partition_col case
+alter table store_sales add constraint ss_d foreign key  (ss_sold_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table store_sales add constraint ss_t foreign key  (ss_sold_time_sk) references time_dim (t_time_sk) disable novalidate rely;
+alter table store_sales add constraint ss_s foreign key  (ss_store_sk) references store (s_store_sk) disable novalidate rely;
+alter table web_page add constraint wp_ad foreign key  (wp_access_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table web_page add constraint wp_cd foreign key  (wp_creation_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table web_returns add constraint wr_i foreign key  (wr_item_sk) references item (i_item_sk) disable novalidate rely;
+alter table web_returns add constraint wr_r foreign key  (wr_reason_sk) references reason (r_reason_sk) disable novalidate rely;
+alter table web_returns add constraint wr_ref_a foreign key  (wr_refunded_addr_sk) references customer_address (ca_address_sk) disable novalidate rely;
+alter table web_returns add constraint wr_ref_cd foreign key  (wr_refunded_cdemo_sk) references customer_demographics (cd_demo_sk) disable novalidate rely;
+alter table web_returns add constraint wr_ref_c foreign key  (wr_refunded_customer_sk) references customer (c_customer_sk) disable novalidate rely;
+alter table web_returns add constraint wr_ref_hd foreign key  (wr_refunded_hdemo_sk) references household_demographics (hd_demo_sk) disable novalidate rely;
+-- partition_col case
+alter table web_returns add constraint wr_ret_d foreign key  (wr_returned_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table web_returns add constraint wr_ret_t foreign key  (wr_returned_time_sk) references time_dim (t_time_sk) disable novalidate rely;
+alter table web_returns add constraint wr_ret_a foreign key  (wr_returning_addr_sk) references customer_address (ca_address_sk) disable novalidate rely;
+alter table web_returns add constraint wr_ret_cd foreign key  (wr_returning_cdemo_sk) references customer_demographics (cd_demo_sk) disable novalidate rely;
+alter table web_returns add constraint wr_ret_c foreign key  (wr_returning_customer_sk) references customer (c_customer_sk) disable novalidate rely;
+alter table web_returns add constraint wr_ret_hd foreign key  (wr_returning_hdemo_sk) references household_demographics (hd_demo_sk) disable novalidate rely;
+alter table web_returns add constraint wr_ws foreign key  (wr_item_sk, wr_order_number) references web_sales (ws_item_sk, ws_order_number) disable novalidate rely;
+alter table web_returns add constraint wr_wp foreign key  (wr_web_page_sk) references web_page (wp_web_page_sk) disable novalidate rely;
+alter table web_sales add constraint ws_b_a foreign key  (ws_bill_addr_sk) references customer_address (ca_address_sk) disable novalidate rely;
+alter table web_sales add constraint ws_b_cd foreign key  (ws_bill_cdemo_sk) references customer_demographics (cd_demo_sk) disable novalidate rely;
+alter table web_sales add constraint ws_b_c foreign key  (ws_bill_customer_sk) references customer (c_customer_sk) disable novalidate rely;
+alter table web_sales add constraint ws_b_hd foreign key  (ws_bill_hdemo_sk) references household_demographics (hd_demo_sk) disable novalidate rely;
+alter table web_sales add constraint ws_i foreign key  (ws_item_sk) references item (i_item_sk) disable novalidate rely;
+alter table web_sales add constraint ws_p foreign key  (ws_promo_sk) references promotion (p_promo_sk) disable novalidate rely;
+alter table web_sales add constraint ws_s_a foreign key  (ws_ship_addr_sk) references customer_address (ca_address_sk) disable novalidate rely;
+alter table web_sales add constraint ws_s_cd foreign key  (ws_ship_cdemo_sk) references customer_demographics (cd_demo_sk) disable novalidate rely;
+alter table web_sales add constraint ws_s_c foreign key  (ws_ship_customer_sk) references customer (c_customer_sk) disable novalidate rely;
+alter table web_sales add constraint ws_s_d foreign key  (ws_ship_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table web_sales add constraint ws_s_hd foreign key  (ws_ship_hdemo_sk) references household_demographics (hd_demo_sk) disable novalidate rely;
+alter table web_sales add constraint ws_sm foreign key  (ws_ship_mode_sk) references ship_mode (sm_ship_mode_sk) disable novalidate rely;
+-- partition_col case
+alter table web_sales add constraint ws_d2 foreign key  (ws_sold_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table web_sales add constraint ws_t foreign key  (ws_sold_time_sk) references time_dim (t_time_sk) disable novalidate rely;
+alter table web_sales add constraint ws_w2 foreign key  (ws_warehouse_sk) references warehouse (w_warehouse_sk) disable novalidate rely;
+alter table web_sales add constraint ws_wp foreign key  (ws_web_page_sk) references web_page (wp_web_page_sk) disable novalidate rely;
+alter table web_sales add constraint ws_ws foreign key  (ws_web_site_sk) references web_site (web_site_sk) disable novalidate rely;
+alter table web_site add constraint web_d1 foreign key  (web_close_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+alter table web_site add constraint web_d2 foreign key (web_open_date_sk) references date_dim (d_date_sk) disable novalidate rely;
+
+alter table store change column s_store_id s_store_id string constraint strid_nn not null disable novalidate rely;
+alter table call_center change column cc_call_center_id cc_call_center_id string constraint ccid_nn not null disable novalidate rely;
+alter table catalog_page change column cp_catalog_page_id cp_catalog_page_id string constraint cpid_nn not null disable novalidate rely;
+alter table web_site change column web_site_id web_site_id string constraint wsid_nn not null disable novalidate rely;
+alter table web_page change column wp_web_page_id wp_web_page_id string constraint wpid_nn not null disable novalidate rely;
+alter table warehouse change column w_warehouse_id w_warehouse_id string constraint wid_nn not null disable novalidate rely;
+alter table customer change column c_customer_id c_customer_id string constraint cid_nn not null disable novalidate rely;
+alter table customer_address change column ca_address_id ca_address_id string constraint caid_nn not null disable novalidate rely;
+alter table date_dim change column d_date_id d_date_id string constraint did_nn not null disable novalidate rely;
+alter table item change column i_item_id i_item_id string constraint itid_nn not null disable novalidate rely;
+alter table promotion change column p_promo_id p_promo_id string constraint pid_nn not null disable novalidate rely;
+alter table reason change column r_reason_id r_reason_id string constraint rid_nn not null disable novalidate rely;
+alter table ship_mode change column sm_ship_mode_id sm_ship_mode_id string constraint smid_nn not null disable novalidate rely;
+alter table time_dim change column t_time_id t_time_id string constraint tid_nn not null disable novalidate rely;
diff --git a/itests/hive-unit/src/test/java/org/apache/hive/service/auth/TestHttpCookieAuthenticationTest.java b/itests/hive-unit/src/test/java/org/apache/hive/service/auth/TestHttpCookieAuthenticationTest.java
new file mode 100644
index 00000000000..827cc689a25
--- /dev/null
+++ b/itests/hive-unit/src/test/java/org/apache/hive/service/auth/TestHttpCookieAuthenticationTest.java
@@ -0,0 +1,185 @@
+/*
+ * 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.hive.service.auth;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotEquals;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import java.lang.reflect.Field;
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.ResultSet;
+import java.sql.Statement;
+import java.util.HashMap;
+import java.util.List;
+import java.util.Map;
+
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hive.jdbc.HiveConnection;
+import org.apache.hive.jdbc.HiveDriver;
+import org.apache.hive.jdbc.miniHS2.MiniHS2;
+import org.apache.http.client.CookieStore;
+import org.apache.http.client.HttpClient;
+import org.apache.http.cookie.Cookie;
+import org.apache.http.impl.cookie.BasicClientCookie;
+import org.apache.thrift.transport.THttpClient;
+import org.apache.thrift.transport.TTransport;
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * TestHttpCookieAuthenticationTest.
+ */
+public class TestHttpCookieAuthenticationTest {
+  private static MiniHS2 miniHS2;
+
+  @BeforeClass
+  public static void startServices() throws Exception {
+    miniHS2 = new MiniHS2.Builder().withHTTPTransport().build();
+
+    Map<String, String> configOverlay = new HashMap<>();
+    configOverlay.put(HiveConf.ConfVars.HIVE_SUPPORT_CONCURRENCY.varname, Boolean.FALSE.toString());
+    configOverlay.put(HiveConf.ConfVars.HIVE_SERVER2_THRIFT_HTTP_COOKIE_AUTH_ENABLED.varname, Boolean.TRUE.toString());
+    miniHS2.start(configOverlay);
+  }
+
+  @AfterClass
+  public static void stopServices() throws Exception {
+    if (miniHS2 != null && miniHS2.isStarted()) {
+      miniHS2.stop();
+      miniHS2.cleanup();
+      miniHS2 = null;
+      MiniHS2.cleanupLocalDir();
+    }
+  }
+
+  @Test
+  public void testHttpJdbcCookies() throws Exception {
+    String sqlQuery = "show tables";
+
+    Class.forName(HiveDriver.class.getCanonicalName());
+
+    String username = System.getProperty("user.name");
+    try(Connection connection = DriverManager.getConnection(miniHS2.getJdbcURL(), username, "bar")) {
+      assertNotNull(connection);
+
+      CookieStore cookieStore = getCookieStoreFromConnection(connection);
+      assertNotNull(cookieStore);
+
+      // Test that basic cookies worked
+      List<Cookie> cookies1 = cookieStore.getCookies();
+      assertEquals(1, cookies1.size());
+
+      try(Statement statement = connection.createStatement()) {
+        assertNotNull(statement);
+        try(ResultSet resultSet = statement.executeQuery(sqlQuery)) {
+          assertNotNull(resultSet);
+        }
+      }
+
+      // Check that cookies worked and still the same after a statement
+      List<Cookie> cookies2 = cookieStore.getCookies();
+      assertEquals(1, cookies2.size());
+      assertEquals(cookies1, cookies2);
+
+      // Empty out cookies to make sure same connection gets new cookies
+      cookieStore.clear();
+      assertTrue(cookieStore.getCookies().isEmpty());
+
+      try(Statement statement = connection.createStatement()) {
+        assertNotNull(statement);
+        try(ResultSet resultSet = statement.executeQuery(sqlQuery)) {
+          assertNotNull(resultSet);
+        }
+      }
+
+      // Check that cookies worked after clearing and got back new cookie
+      List<Cookie> cookies3 = cookieStore.getCookies();
+      assertEquals(1, cookies3.size());
+      assertNotEquals(cookies1, cookies3);
+
+
+      // Get original cookie to copy metadata
+      Cookie originalCookie = cookies3.get(0);
+
+      // Put in a bad client side cookie - ensure HS2 authenticates and overwrites
+      BasicClientCookie badCookie = new BasicClientCookie("hive.server2.auth", "bad");
+      badCookie.setDomain(originalCookie.getDomain());
+      badCookie.setPath(originalCookie.getPath());
+      badCookie.setExpiryDate(originalCookie.getExpiryDate());
+      cookieStore.addCookie(badCookie);
+
+      // Check that putting in the bad cookie overrode the original cookie
+      List<Cookie> cookies4 = cookieStore.getCookies();
+      assertEquals(1, cookies4.size());
+      assertTrue(cookies4.contains(badCookie));
+
+      try(Statement statement = connection.createStatement()) {
+        assertNotNull(statement);
+        try(ResultSet resultSet = statement.executeQuery(sqlQuery)) {
+          assertNotNull(resultSet);
+        }
+      }
+
+      // Check that cookies worked and replaced the bad cookie
+      List<Cookie> cookies5 = cookieStore.getCookies();
+      assertEquals(1, cookies5.size());
+      assertNotEquals(cookies4, cookies5);
+
+      try(Statement statement = connection.createStatement()) {
+        assertNotNull(statement);
+        try(ResultSet resultSet = statement.executeQuery(sqlQuery)) {
+          assertNotNull(resultSet);
+        }
+      }
+
+      // Check that cookies worked and didn't get replaced
+      List<Cookie> cookies6 = cookieStore.getCookies();
+      assertEquals(1, cookies6.size());
+      assertEquals(cookies5, cookies6);
+    }
+  }
+
+  // ((InternalHttpClient) ((THttpClient) ((HiveConnection) connection).transport).client).cookieStore.getCookies()
+  private CookieStore getCookieStoreFromConnection(Connection connection) throws Exception {
+    CookieStore cookieStore = null;
+    if (connection instanceof HiveConnection) {
+      HiveConnection hiveConnection = (HiveConnection) connection;
+
+      Field transportField = hiveConnection.getClass().getDeclaredField("transport");
+      transportField.setAccessible(true);
+      TTransport transport = (TTransport) transportField.get(hiveConnection);
+
+      if(transport instanceof THttpClient) {
+        THttpClient httpTransport = (THttpClient) transport;
+        Field clientField = httpTransport.getClass().getDeclaredField("client");
+        clientField.setAccessible(true);
+        HttpClient httpClient = (HttpClient) clientField.get(httpTransport);
+
+        Field cookieStoreField = httpClient.getClass().getDeclaredField("cookieStore");
+        cookieStoreField.setAccessible(true);
+        cookieStore = (CookieStore) cookieStoreField.get(httpClient);
+      }
+    }
+    return cookieStore;
+  }
+}
diff --git a/itests/qtest/src/test/java/org/apache/hadoop/hive/cli/TestTezPerfCliDriver.java b/itests/qtest/src/test/java/org/apache/hadoop/hive/cli/TestTezPerfCliDriver.java
index 98ceb214047..18a470fd113 100644
--- a/itests/qtest/src/test/java/org/apache/hadoop/hive/cli/TestTezPerfCliDriver.java
+++ b/itests/qtest/src/test/java/org/apache/hadoop/hive/cli/TestTezPerfCliDriver.java
@@ -34,7 +34,7 @@ import org.junit.runners.Parameterized.Parameters;
 @RunWith(Parameterized.class)
 public class TestTezPerfCliDriver {
 
-  static CliAdapter adapter = new CliConfigs.TezPerfCliConfig().getCliAdapter();
+  static CliAdapter adapter = new CliConfigs.TezPerfCliConfig(false).getCliAdapter();
 
   @Parameters(name = "{0}")
   public static List<Object[]> getParameters() throws Exception {
diff --git a/itests/qtest/src/test/java/org/apache/hadoop/hive/cli/TestTezPerfCliDriver.java b/itests/qtest/src/test/java/org/apache/hadoop/hive/cli/TestTezPerfConstraintsCliDriver.java
similarity index 91%
copy from itests/qtest/src/test/java/org/apache/hadoop/hive/cli/TestTezPerfCliDriver.java
copy to itests/qtest/src/test/java/org/apache/hadoop/hive/cli/TestTezPerfConstraintsCliDriver.java
index 98ceb214047..5881353387f 100644
--- a/itests/qtest/src/test/java/org/apache/hadoop/hive/cli/TestTezPerfCliDriver.java
+++ b/itests/qtest/src/test/java/org/apache/hadoop/hive/cli/TestTezPerfConstraintsCliDriver.java
@@ -20,7 +20,6 @@ package org.apache.hadoop.hive.cli;
 import java.io.File;
 import java.util.Comparator;
 import java.util.List;
-
 import org.apache.hadoop.hive.cli.control.CliAdapter;
 import org.apache.hadoop.hive.cli.control.CliConfigs;
 import org.junit.ClassRule;
@@ -32,9 +31,9 @@ import org.junit.runners.Parameterized;
 import org.junit.runners.Parameterized.Parameters;
 
 @RunWith(Parameterized.class)
-public class TestTezPerfCliDriver {
+public class TestTezPerfConstraintsCliDriver {
 
-  static CliAdapter adapter = new CliConfigs.TezPerfCliConfig().getCliAdapter();
+  static CliAdapter adapter = new CliConfigs.TezPerfCliConfig(true).getCliAdapter();
 
   @Parameters(name = "{0}")
   public static List<Object[]> getParameters() throws Exception {
@@ -61,7 +60,7 @@ public class TestTezPerfCliDriver {
   private String name;
   private File qfile;
 
-  public TestTezPerfCliDriver(String name, File qfile) {
+  public TestTezPerfConstraintsCliDriver(String name, File qfile) {
     this.name = name;
     this.qfile = qfile;
   }
diff --git a/itests/src/test/resources/testconfiguration.properties b/itests/src/test/resources/testconfiguration.properties
index 646a1c1e110..c8c5d70829a 100644
--- a/itests/src/test/resources/testconfiguration.properties
+++ b/itests/src/test/resources/testconfiguration.properties
@@ -526,6 +526,7 @@ minillaplocal.query.files=\
   colstats_date_min_max.q,\
   compare_double_bigint_2.q,\
   constprog_dpp.q,\
+  constant_prop_when.q,\
   current_date_timestamp.q,\
   correlationoptimizer1.q,\
   correlationoptimizer2.q,\
@@ -574,6 +575,7 @@ minillaplocal.query.files=\
   jdbc_handler.q,\
   join1.q,\
   join_acid_non_acid.q,\
+  join_constraints_optimization.q,\
   join_filters.q,\
   join_max_hashtable.q,\
   join_nulls.q,\
@@ -627,6 +629,8 @@ minillaplocal.query.files=\
   materialized_view_rewrite_8.q,\
   materialized_view_rewrite_9.q,\
   materialized_view_rewrite_10.q,\
+  materialized_view_rewrite_no_join_opt.q,\
+  materialized_view_rewrite_no_join_opt_2.q,\
   materialized_view_rewrite_part_1.q,\
   materialized_view_rewrite_part_2.q,\
   materialized_view_rewrite_ssb.q,\
@@ -973,7 +977,8 @@ minillaplocal.query.files=\
   dynamic_semijoin_reduction_sw2.q,\
   partialdhj.q,\
   stats_date.q,\
-  dst.q
+  dst.q,\
+  q93_with_constraints.q
 
 encrypted.query.files=encryption_join_unencrypted_tbl.q,\
   encryption_insert_partition_static.q,\
@@ -1759,7 +1764,104 @@ spark.query.negative.files=groupby2_map_skew_multi_distinct.q,\
   spark_stage_max_tasks.q
 
 spark.perf.disabled.query.files=query14.q,\
-  query64.q
+  query64.q,\
+  cbo_query1.q,\
+  cbo_query10.q,\
+  cbo_query11.q,\
+  cbo_query12.q,\
+  cbo_query13.q,\
+  cbo_query14.q,\
+  cbo_query15.q,\
+  cbo_query16.q,\
+  cbo_query17.q,\
+  cbo_query18.q,\
+  cbo_query19.q,\
+  cbo_query2.q,\
+  cbo_query20.q,\
+  cbo_query21.q,\
+  cbo_query22.q,\
+  cbo_query23.q,\
+  cbo_query24.q,\
+  cbo_query25.q,\
+  cbo_query26.q,\
+  cbo_query27.q,\
+  cbo_query28.q,\
+  cbo_query29.q,\
+  cbo_query3.q,\
+  cbo_query30.q,\
+  cbo_query31.q,\
+  cbo_query32.q,\
+  cbo_query33.q,\
+  cbo_query34.q,\
+  cbo_query35.q,\
+  cbo_query36.q,\
+  cbo_query37.q,\
+  cbo_query38.q,\
+  cbo_query39.q,\
+  cbo_query4.q,\
+  cbo_query40.q,\
+  cbo_query42.q,\
+  cbo_query43.q,\
+  cbo_query44.q,\
+  cbo_query45.q,\
+  cbo_query46.q,\
+  cbo_query47.q,\
+  cbo_query48.q,\
+  cbo_query49.q,\
+  cbo_query5.q,\
+  cbo_query50.q,\
+  cbo_query51.q,\
+  cbo_query52.q,\
+  cbo_query53.q,\
+  cbo_query54.q,\
+  cbo_query55.q,\
+  cbo_query56.q,\
+  cbo_query57.q,\
+  cbo_query58.q,\
+  cbo_query59.q,\
+  cbo_query6.q,\
+  cbo_query60.q,\
+  cbo_query61.q,\
+  cbo_query63.q,\
+  cbo_query64.q,\
+  cbo_query65.q,\
+  cbo_query66.q,\
+  cbo_query67.q,\
+  cbo_query68.q,\
+  cbo_query69.q,\
+  cbo_query7.q,\
+  cbo_query70.q,\
+  cbo_query71.q,\
+  cbo_query72.q,\
+  cbo_query73.q,\
+  cbo_query74.q,\
+  cbo_query75.q,\
+  cbo_query76.q,\
+  cbo_query77.q,\
+  cbo_query78.q,\
+  cbo_query79.q,\
+  cbo_query8.q,\
+  cbo_query80.q,\
+  cbo_query81.q,\
+  cbo_query82.q,\
+  cbo_query83.q,\
+  cbo_query84.q,\
+  cbo_query85.q,\
+  cbo_query86.q,\
+  cbo_query87.q,\
+  cbo_query88.q,\
+  cbo_query89.q,\
+  cbo_query9.q,\
+  cbo_query90.q,\
+  cbo_query91.q,\
+  cbo_query92.q,\
+  cbo_query93.q,\
+  cbo_query94.q,\
+  cbo_query95.q,\
+  cbo_query96.q,\
+  cbo_query97.q,\
+  cbo_query98.q,\
+  cbo_query99.q
 
 druid.query.files=druidmini_test1.q,\
   druidmini_test_ts.q,\
diff --git a/itests/util/src/main/java/org/apache/hadoop/hive/cli/control/CliConfigs.java b/itests/util/src/main/java/org/apache/hadoop/hive/cli/control/CliConfigs.java
index 84a9c49d0be..ede512dac21 100644
--- a/itests/util/src/main/java/org/apache/hadoop/hive/cli/control/CliConfigs.java
+++ b/itests/util/src/main/java/org/apache/hadoop/hive/cli/control/CliConfigs.java
@@ -297,7 +297,7 @@ public class CliConfigs {
   }
 
   public static class TezPerfCliConfig extends AbstractCliConfig {
-    public TezPerfCliConfig() {
+    public TezPerfCliConfig(boolean useConstraints) {
       super(CorePerfCliDriver.class);
       try {
         setQueryDir("ql/src/test/queries/clientpositive/perf");
@@ -306,10 +306,21 @@ public class CliConfigs {
         excludesFrom(testConfigProps, "minitez.query.files");
         excludesFrom(testConfigProps, "encrypted.query.files");
 
-        setResultsDir("ql/src/test/results/clientpositive/perf/tez");
+        excludeQuery("cbo_query44.q"); // TODO: Enable when we move to Calcite 1.18
+        excludeQuery("cbo_query45.q"); // TODO: Enable when we move to Calcite 1.18
+        excludeQuery("cbo_query67.q"); // TODO: Enable when we move to Calcite 1.18
+        excludeQuery("cbo_query70.q"); // TODO: Enable when we move to Calcite 1.18
+        excludeQuery("cbo_query86.q"); // TODO: Enable when we move to Calcite 1.18
+
         setLogDir("itests/qtest/target/qfile-results/clientpositive/tez");
 
-        setInitScript("q_perf_test_init.sql");
+        if (useConstraints) {
+          setInitScript("q_perf_test_init_constraints.sql");
+          setResultsDir("ql/src/test/results/clientpositive/perf/tez/constraints");
+        } else {
+          setInitScript("q_perf_test_init.sql");
+          setResultsDir("ql/src/test/results/clientpositive/perf/tez");
+        }
         setCleanupScript("q_perf_test_cleanup.sql");
 
         setHiveConfDir("data/conf/perf-reg/tez");
diff --git a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
index 70696281b6a..ea83ac56f05 100644
--- a/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
+++ b/jdbc/src/java/org/apache/hive/jdbc/HiveConnection.java
@@ -551,7 +551,7 @@ public class HiveConnection implements java.sql.Connection {
     if (isCookieEnabled) {
       // Create a http client with a retry mechanism when the server returns a status code of 401.
       httpClientBuilder =
-          HttpClients.custom().setServiceUnavailableRetryStrategy(
+          HttpClients.custom().setDefaultCookieStore(cookieStore).setServiceUnavailableRetryStrategy(
               new ServiceUnavailableRetryStrategy() {
                 @Override
                 public boolean retryRequest(final HttpResponse response, final int executionCount,
diff --git a/jdbc/src/java/org/apache/hive/jdbc/HttpRequestInterceptorBase.java b/jdbc/src/java/org/apache/hive/jdbc/HttpRequestInterceptorBase.java
index 1ef0ab12c04..bb1abd302d0 100644
--- a/jdbc/src/java/org/apache/hive/jdbc/HttpRequestInterceptorBase.java
+++ b/jdbc/src/java/org/apache/hive/jdbc/HttpRequestInterceptorBase.java
@@ -27,7 +27,6 @@ import org.apache.http.HttpException;
 import org.apache.http.HttpRequest;
 import org.apache.http.HttpRequestInterceptor;
 import org.apache.http.client.CookieStore;
-import org.apache.http.client.protocol.ClientContext;
 import org.apache.http.protocol.HttpContext;
 
 public abstract class HttpRequestInterceptorBase implements HttpRequestInterceptor {
@@ -60,9 +59,7 @@ public abstract class HttpRequestInterceptorBase implements HttpRequestIntercept
       // The necessary condition is either when there are no server side cookies in the
       // cookiestore which can be send back or when the server returns a 401 error code
       // indicating that the previous cookie has expired.
-      if (isCookieEnabled) {
-        httpContext.setAttribute(ClientContext.COOKIE_STORE, cookieStore);
-      }
+
       // Generate the kerberos ticket under the following scenarios:
       // 1. Cookie Authentication is disabled OR
       // 2. The first time when the request is sent OR
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/Context.java b/ql/src/java/org/apache/hadoop/hive/ql/Context.java
index 4a47f4c5f4f..173b9876aa6 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/Context.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/Context.java
@@ -105,6 +105,7 @@ public class Context {
   protected String cboInfo;
   protected boolean cboSucceeded;
   protected String optimizedSql;
+  protected String calcitePlan;
   protected String cmd = "";
   private TokenRewriteStream tokenRewriteStream;
   // Holds the qualified name to tokenRewriteStream for the views
@@ -1032,6 +1033,14 @@ public class Context {
     this.cboSucceeded = cboSucceeded;
   }
 
+  public String getCalcitePlan() {
+    return this.calcitePlan;
+  }
+
+  public void setCalcitePlan(String calcitePlan) {
+    this.calcitePlan = calcitePlan;
+  }
+
   public Table getMaterializedTable(String cteName) {
     return cteTables.get(cteName);
   }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/Driver.java b/ql/src/java/org/apache/hadoop/hive/ql/Driver.java
index ae38aa4ef22..3be55602a03 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/Driver.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/Driver.java
@@ -1032,7 +1032,8 @@ public class Driver implements IDriver {
     PrintStream ps = new PrintStream(baos);
     try {
       List<Task<?>> rootTasks = sem.getAllRootTasks();
-      task.getJSONPlan(ps, rootTasks, sem.getFetchTask(), false, true, true, plan.getOptimizedQueryString());
+      task.getJSONPlan(ps, rootTasks, sem.getFetchTask(), false, true, true, sem.getCboInfo(),
+          plan.getOptimizedQueryString());
       ret = baos.toString();
     } catch (Exception e) {
       LOG.warn("Exception generating explain output: " + e, e);
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/QueryPlan.java b/ql/src/java/org/apache/hadoop/hive/ql/QueryPlan.java
index bbb9e0dda7c..80a7d74af77 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/QueryPlan.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/QueryPlan.java
@@ -74,7 +74,7 @@ import com.google.common.annotations.VisibleForTesting;
 public class QueryPlan implements Serializable {
   private static final long serialVersionUID = 1L;
 
-
+  private String cboInfo;
   private String queryString;
   private String optimizedQueryString;
 
@@ -161,6 +161,7 @@ public class QueryPlan implements Serializable {
     this.acidSinks = sem.getAcidFileSinks();
     this.acidDdlDesc = sem.getAcidDdlDesc();
     this.acidAnalyzeTable = sem.getAcidAnalyzeTable();
+    this.cboInfo = sem.getCboInfo();
   }
 
   /**
@@ -857,4 +858,8 @@ public class QueryPlan implements Serializable {
   public Boolean getAutoCommitValue() {
     return autoCommitValue;
   }
+
+  public String getCboInfo() {
+    return cboInfo;
+  }
 }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/ExplainTask.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/ExplainTask.java
index 5fa5e9e5280..90142c748fc 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/exec/ExplainTask.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/ExplainTask.java
@@ -140,6 +140,16 @@ public class ExplainTask extends Task<ExplainWork> implements Serializable {
     return outJSONObject;
   }
 
+  public String outputCboPlan(String cboPlan, PrintStream out, boolean jsonOutput)
+      throws JSONException {
+    if (out != null) {
+      out.println("CBO PLAN:");
+      out.println(cboPlan);
+    }
+
+    return jsonOutput ? cboPlan : null;
+  }
+
   public JSONObject getJSONLogicalPlan(PrintStream out, ExplainWork work) throws Exception {
     isLogical = true;
 
@@ -221,11 +231,12 @@ public class ExplainTask extends Task<ExplainWork> implements Serializable {
   public JSONObject getJSONPlan(PrintStream out, ExplainWork work)
       throws Exception {
     return getJSONPlan(out, work.getRootTasks(), work.getFetchTask(),
-                       work.isFormatted(), work.getExtended(), work.isAppendTaskType(), work.getOptimizedSQL());
+        work.isFormatted(), work.getExtended(), work.isAppendTaskType(), work.getCboInfo(),
+        work.getOptimizedSQL());
   }
 
   public JSONObject getJSONPlan(PrintStream out, List<Task<?>> tasks, Task<?> fetchTask,
-      boolean jsonOutput, boolean isExtended, boolean appendTaskType, String optimizedSQL) throws Exception {
+      boolean jsonOutput, boolean isExtended, boolean appendTaskType, String cboInfo, String optimizedSQL) throws Exception {
 
     // If the user asked for a formatted output, dump the json output
     // in the output stream
@@ -280,6 +291,9 @@ public class ExplainTask extends Task<ExplainWork> implements Serializable {
       }
 
       if (jsonOutput) {
+        if (cboInfo != null) {
+          outJSONObject.put("cboInfo", cboInfo);
+        }
         outJSONObject.put("STAGE DEPENDENCIES", jsonDependencies);
       }
 
@@ -381,7 +395,11 @@ public class ExplainTask extends Task<ExplainWork> implements Serializable {
       OutputStream outS = resFile.getFileSystem(conf).create(resFile);
       out = new PrintStream(outS);
 
-      if (work.isLogical()) {
+      if (work.isCbo()) {
+        if (work.getCboPlan() != null) {
+          outputCboPlan(work.getCboPlan(), out, work.isFormatted());
+        }
+      } else if (work.isLogical()) {
         JSONObject jsonLogicalPlan = getJSONLogicalPlan(out, work);
         if (work.isFormatted()) {
           out.print(jsonLogicalPlan);
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/CastStringToDate.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/CastStringToDate.java
index a6dff12e1af..706a5dce1b8 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/CastStringToDate.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/CastStringToDate.java
@@ -38,8 +38,6 @@ public class CastStringToDate extends VectorExpression {
 
   private final int inputColumn;
 
-  private transient final DateParser dateParser = new DateParser();
-
   public CastStringToDate() {
     super();
 
@@ -153,8 +151,8 @@ public class CastStringToDate extends VectorExpression {
 
   private void evaluate(LongColumnVector outputColVector, BytesColumnVector inV, int i) {
     String dateString = new String(inV.vector[i], inV.start[i], inV.length[i], StandardCharsets.UTF_8);
-    Date hDate = new Date();
-    if (dateParser.parseDate(dateString, hDate)) {
+    Date hDate = DateParser.parseDate(dateString);
+    if (hDate != null) {
       outputColVector.vector[i] = DateWritableV2.dateToDays(hDate);
       return;
     }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/TruncDateFromString.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/TruncDateFromString.java
new file mode 100644
index 00000000000..861e77111c0
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/TruncDateFromString.java
@@ -0,0 +1,69 @@
+/*
+ * 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.exec.vector.expressions;
+
+import java.nio.charset.StandardCharsets;
+
+import org.apache.hadoop.hive.common.type.Date;
+import org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.ColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.VectorExpressionDescriptor;
+import org.apache.hadoop.hive.ql.exec.vector.VectorExpressionDescriptor.ArgumentType;
+import org.apache.hive.common.util.DateParser;
+
+/**
+ * Vectorized implementation of trunc(date, fmt) function for string input
+ */
+public class TruncDateFromString extends TruncDateFromTimestamp {
+  private static final long serialVersionUID = 1L;
+  private transient Date date = new Date();
+
+  public TruncDateFromString(int colNum, byte[] fmt, int outputColumnNum) {
+    super(colNum, fmt, outputColumnNum);
+  }
+
+  public TruncDateFromString() {
+    super();
+  }
+
+  protected void truncDate(ColumnVector inV, BytesColumnVector outV, int i) {
+    truncDate((BytesColumnVector) inV, outV, i);
+  }
+
+  protected void truncDate(BytesColumnVector inV, BytesColumnVector outV, int i) {
+    if (inV.vector[i] == null) {
+      outV.isNull[i] = true;
+      outV.noNulls = false;
+    }
+
+    String dateString =
+        new String(inV.vector[i], inV.start[i], inV.length[i], StandardCharsets.UTF_8);
+    if (DateParser.parseDate(dateString, date)) {
+      processDate(outV, i, date);
+    } else {
+      outV.isNull[i] = true;
+      outV.noNulls = false;
+    }
+  }
+
+  @Override
+  protected ArgumentType getInputColumnType() {
+    return VectorExpressionDescriptor.ArgumentType.STRING_FAMILY;
+  }
+}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/TruncDateFromTimestamp.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/TruncDateFromTimestamp.java
new file mode 100644
index 00000000000..56bf00586fb
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/TruncDateFromTimestamp.java
@@ -0,0 +1,180 @@
+/*
+ * 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.exec.vector.expressions;
+
+import java.nio.charset.StandardCharsets;
+import java.util.Arrays;
+
+import org.apache.hadoop.hive.common.type.Date;
+import org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.ColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.TimestampColumnVector;
+import org.apache.hadoop.hive.ql.exec.vector.VectorExpressionDescriptor;
+import org.apache.hadoop.hive.ql.exec.vector.VectorExpressionDescriptor.ArgumentType;
+import org.apache.hadoop.hive.ql.exec.vector.VectorExpressionDescriptor.Descriptor;
+import org.apache.hadoop.hive.ql.exec.vector.VectorizedRowBatch;
+import org.apache.hadoop.hive.ql.metadata.HiveException;
+
+/**
+ * Vectorized implementation of trunc(date, fmt) function for timestamp input
+ */
+public class TruncDateFromTimestamp extends VectorExpression {
+  private static final long serialVersionUID = 1L;
+  protected int colNum;
+  protected String fmt;
+
+  public TruncDateFromTimestamp() {
+    super();
+    colNum = -1;
+  }
+
+  public TruncDateFromTimestamp(int colNum, byte[] fmt, int outputColumnNum) {
+    super(outputColumnNum);
+    this.colNum = colNum;
+    this.fmt = new String(fmt, StandardCharsets.UTF_8);
+  }
+
+  @Override
+  public String vectorExpressionParameters() {
+    return "col " + colNum + ", format " + fmt;
+  }
+
+  @Override
+  public void evaluate(VectorizedRowBatch batch) throws HiveException {
+
+    if (childExpressions != null) {
+      this.evaluateChildren(batch);
+    }
+
+    ColumnVector inputColVector = batch.cols[colNum];
+    BytesColumnVector outputColVector = (BytesColumnVector) batch.cols[outputColumnNum];
+
+    int[] sel = batch.selected;
+    boolean[] inputIsNull = inputColVector.isNull;
+    boolean[] outputIsNull = outputColVector.isNull;
+    int n = batch.size;
+
+    // return immediately if batch is empty
+    if (n == 0) {
+      return;
+    }
+
+    // We do not need to do a column reset since we are carefully changing the output.
+    outputColVector.isRepeating = false;
+
+    if (inputColVector.isRepeating) {
+      if (inputColVector.noNulls || !inputIsNull[0]) {
+        outputIsNull[0] = false;
+        truncDate(inputColVector, outputColVector, 0);
+      } else {
+        outputIsNull[0] = true;
+        outputColVector.noNulls = false;
+      }
+      outputColVector.isRepeating = true;
+      return;
+    }
+
+    if (inputColVector.noNulls) {
+      if (batch.selectedInUse) {
+
+        // CONSIDER: For large n, fill n or all of isNull array and use the tighter ELSE loop.
+        if (!outputColVector.noNulls) {
+          for (int j = 0; j != n; j++) {
+            final int i = sel[j];
+            // Set isNull before call in case it changes it mind.
+            outputIsNull[i] = false;
+            truncDate(inputColVector, outputColVector, i);
+          }
+        } else {
+          for (int j = 0; j != n; j++) {
+            final int i = sel[j];
+            truncDate(inputColVector, outputColVector, i);
+          }
+        }
+      } else {
+        if (!outputColVector.noNulls) {
+
+          // Assume it is almost always a performance win to fill all of isNull so we can
+          // safely reset noNulls.
+          Arrays.fill(outputIsNull, false);
+          outputColVector.noNulls = true;
+        }
+        for (int i = 0; i != n; i++) {
+          truncDate(inputColVector, outputColVector, i);
+        }
+      }
+    } else /* there are nulls in the inputColVector */ {
+
+      // Carefully handle NULLs...
+      outputColVector.noNulls = false;
+
+      if (batch.selectedInUse) {
+        for (int j = 0; j != n; j++) {
+          int i = sel[j];
+          outputIsNull[i] = inputIsNull[i];
+          truncDate(inputColVector, outputColVector, i);
+        }
+      } else {
+        System.arraycopy(inputIsNull, 0, outputIsNull, 0, n);
+        for (int i = 0; i != n; i++) {
+          if (!inputColVector.isNull[i]) {
+            truncDate(inputColVector, outputColVector, i);
+          }
+        }
+      }
+    }
+  }
+
+  protected void truncDate(ColumnVector inV, BytesColumnVector outV, int i) {
+    Date date = Date.ofEpochMilli(((TimestampColumnVector) inV).getTime(i));
+    processDate(outV, i, date);
+  }
+
+  protected void processDate(BytesColumnVector outV, int i, Date date) {
+    if ("MONTH".equals(fmt) || "MON".equals(fmt) || "MM".equals(fmt)) {
+      date.setDayOfMonth(1);
+    } else if ("QUARTER".equals(fmt) || "Q".equals(fmt)) {
+      int month = date.getMonth() - 1;
+      int quarter = month / 3;
+      int monthToSet = quarter * 3 + 1;
+      date.setMonth(monthToSet);
+      date.setDayOfMonth(1);
+    } else if ("YEAR".equals(fmt) || "YYYY".equals(fmt) || "YY".equals(fmt)) {
+      date.setMonth(1);
+      date.setDayOfMonth(1);
+    }
+    byte[] bytes = date.toString().getBytes(StandardCharsets.UTF_8);
+    outV.setVal(i, bytes, 0, bytes.length);
+  }
+
+  @Override
+  public Descriptor getDescriptor() {
+    VectorExpressionDescriptor.Builder b = new VectorExpressionDescriptor.Builder();
+    b.setMode(VectorExpressionDescriptor.Mode.PROJECTION).setNumArguments(2)
+        .setArgumentTypes(getInputColumnType(),
+            VectorExpressionDescriptor.ArgumentType.STRING_FAMILY)
+        .setInputExpressionTypes(VectorExpressionDescriptor.InputExpressionType.COLUMN,
+            VectorExpressionDescriptor.InputExpressionType.SCALAR);
+    return b.build();
+  }
+
+  protected ArgumentType getInputColumnType() {
+    return VectorExpressionDescriptor.ArgumentType.TIMESTAMP;
+  }
+}
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateAddColCol.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateAddColCol.java
index 998b6b1f843..5878a764e6b 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateAddColCol.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateAddColCol.java
@@ -41,7 +41,6 @@ public class VectorUDFDateAddColCol extends VectorExpression {
   protected boolean isPositive = true;
 
   private transient final Text text = new Text();
-  private transient final DateParser dateParser = new DateParser();
 
   // Transient members initialized by transientInit method.
   private transient PrimitiveCategory primitiveCategory;
@@ -289,9 +288,8 @@ public class VectorUDFDateAddColCol extends VectorExpression {
       outputVector.isNull[index] = true;
     } else {
       text.set(inputColumnVector1.vector[index], inputColumnVector1.start[index], inputColumnVector1.length[index]);
-      Date hDate = new Date();
-      boolean parsed = dateParser.parseDate(text.toString(), hDate);
-      if (!parsed) {
+      Date hDate = DateParser.parseDate(text.toString());
+      if (hDate == null) {
         outputVector.noNulls = false;
         outputVector.isNull[index] = true;
         return;
@@ -317,9 +315,8 @@ public class VectorUDFDateAddColCol extends VectorExpression {
     }
     text.set(
         inputColumnVector1.vector[0], inputColumnVector1.start[0], inputColumnVector1.length[0]);
-    Date date = new Date();
-    boolean parsed = dateParser.parseDate(text.toString(), date);
-    if (!parsed) {
+    Date date = DateParser.parseDate(text.toString());
+    if (date == null) {
       outputVector.noNulls = false;
       outputVector.isNull[0] = true;
       outputVector.isRepeating = true;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateAddColScalar.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateAddColScalar.java
index 30b20c87e30..b285ef52884 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateAddColScalar.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateAddColScalar.java
@@ -18,6 +18,7 @@
 
 package org.apache.hadoop.hive.ql.exec.vector.expressions;
 
+import org.apache.hadoop.hive.common.type.Date;
 import org.apache.hadoop.hive.ql.exec.vector.BytesColumnVector;
 import org.apache.hadoop.hive.ql.exec.vector.ColumnVector;
 import org.apache.hadoop.hive.ql.exec.vector.LongColumnVector;
@@ -42,7 +43,6 @@ public class VectorUDFDateAddColScalar extends VectorExpression {
   protected boolean isPositive = true;
 
   private transient final Text text = new Text();
-  private transient final DateParser dateParser = new DateParser();
 
   // Transient members initialized by transientInit method.
   private transient PrimitiveCategory primitiveCategory;
@@ -326,9 +326,8 @@ public class VectorUDFDateAddColScalar extends VectorExpression {
   protected void evaluateString(ColumnVector columnVector, LongColumnVector outputVector, int i) {
     BytesColumnVector bcv = (BytesColumnVector) columnVector;
     text.set(bcv.vector[i], bcv.start[i], bcv.length[i]);
-    org.apache.hadoop.hive.common.type.Date hDate = new org.apache.hadoop.hive.common.type.Date();
-    boolean parsed = dateParser.parseDate(text.toString(), hDate);
-    if (!parsed) {
+    Date hDate = DateParser.parseDate(text.toString());
+    if (hDate == null) {
       outputVector.noNulls = false;
       outputVector.isNull[i] = true;
       return;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateAddScalarCol.java b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateAddScalarCol.java
index 669ca5556a3..2ebe8f545ee 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateAddScalarCol.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/exec/vector/expressions/VectorUDFDateAddScalarCol.java
@@ -45,7 +45,6 @@ public class VectorUDFDateAddScalarCol extends VectorExpression {
 
   protected boolean isPositive = true;
 
-  private transient final DateParser dateParser = new DateParser();
   private transient final Date baseDate = new Date();
 
   // Transient members initialized by transientInit method.
@@ -109,7 +108,7 @@ public class VectorUDFDateAddScalarCol extends VectorExpression {
       case STRING:
       case CHAR:
       case VARCHAR:
-        boolean parsed = dateParser.parseDate(new String(stringValue, StandardCharsets.UTF_8), baseDate);
+        boolean parsed = DateParser.parseDate(new String(stringValue, StandardCharsets.UTF_8), baseDate);
         if (!parsed) {
           outputColVector.noNulls = false;
           if (selectedInUse) {
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/hooks/ATSHook.java b/ql/src/java/org/apache/hadoop/hive/ql/hooks/ATSHook.java
index fa69f13965c..a79d4913a48 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/hooks/ATSHook.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/hooks/ATSHook.java
@@ -267,7 +267,8 @@ public class ATSHook implements ExecuteWithHookContext {
                   null,// analyzer
                   config, //explainConfig
                   null, // cboInfo
-                  plan.getOptimizedQueryString() // optimizedSQL
+                  plan.getOptimizedQueryString(), // optimizedSQL
+                  null
               );
                 @SuppressWarnings("unchecked")
                 ExplainTask explain = (ExplainTask) TaskFactory.get(work);
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/hooks/HiveProtoLoggingHook.java b/ql/src/java/org/apache/hadoop/hive/ql/hooks/HiveProtoLoggingHook.java
index aa3a926fb4e..c5ebb2eb343 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/hooks/HiveProtoLoggingHook.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/hooks/HiveProtoLoggingHook.java
@@ -491,8 +491,9 @@ public class HiveProtoLoggingHook implements ExecuteWithHookContext {
           plan.getFetchTask(), // FetchTask
           null, // analyzer
           config, // explainConfig
-          null, // cboInfo,
-          plan.getOptimizedQueryString()
+          plan.getCboInfo(), // cboInfo,
+          plan.getOptimizedQueryString(),
+          null
       );
       ExplainTask explain = (ExplainTask) TaskFactory.get(work, conf);
       explain.initialize(hookContext.getQueryState(), plan, null, null);
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConstantPropagateProcFactory.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConstantPropagateProcFactory.java
index 54d52f8c2a1..c9e97fbb523 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConstantPropagateProcFactory.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/ConstantPropagateProcFactory.java
@@ -589,7 +589,7 @@ public final class ConstantPropagateProcFactory {
        return null;
      }
      GenericUDF childUDF = caseOrWhenexpr.getGenericUDF();
-     List<ExprNodeDesc> children = caseOrWhenexpr.getChildren();
+      List<ExprNodeDesc> children = new ArrayList(caseOrWhenexpr.getChildren());
      int i;
      if (childUDF instanceof GenericUDFWhen) {
        for (i = 1; i < children.size(); i+=2) {
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRelOptUtil.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRelOptUtil.java
index 268284a6da2..3844a44e179 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRelOptUtil.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/HiveRelOptUtil.java
@@ -17,17 +17,34 @@
  */
 package org.apache.hadoop.hive.ql.optimizer.calcite;
 
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Sets;
 import java.util.AbstractList;
 import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedHashSet;
 import java.util.List;
 
 import com.google.common.collect.ImmutableList;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
 import org.apache.calcite.plan.RelOptCluster;
 import org.apache.calcite.plan.RelOptUtil;
 import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelReferentialConstraint;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.Aggregate.Group;
 import org.apache.calcite.rel.core.AggregateCall;
 import org.apache.calcite.rel.core.Filter;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
 import org.apache.calcite.rel.core.RelFactories;
+import org.apache.calcite.rel.core.Sort;
+import org.apache.calcite.rel.core.TableScan;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
 import org.apache.calcite.rel.type.RelDataType;
 import org.apache.calcite.rel.type.RelDataTypeField;
 import org.apache.calcite.rex.RexBuilder;
@@ -35,12 +52,16 @@ import org.apache.calcite.rex.RexCall;
 import org.apache.calcite.rex.RexFieldAccess;
 import org.apache.calcite.rex.RexInputRef;
 import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexOver;
+import org.apache.calcite.rex.RexTableInputRef;
+import org.apache.calcite.rex.RexTableInputRef.RelTableRef;
 import org.apache.calcite.rex.RexUtil;
 import org.apache.calcite.sql.SqlKind;
 import org.apache.calcite.sql.SqlOperator;
 import org.apache.calcite.sql.fun.SqlStdOperatorTable;
 import org.apache.calcite.tools.RelBuilder;
 import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.Pair;
 import org.apache.hadoop.hive.ql.exec.FunctionRegistry;
 import org.apache.hadoop.hive.ql.optimizer.calcite.translator.TypeConverter;
 import org.apache.hadoop.hive.serde2.typeinfo.TypeInfo;
@@ -460,4 +481,285 @@ public class HiveRelOptUtil extends RelOptUtil {
 
     return aggregateFactory.createAggregate(rel, false, ImmutableBitSet.of(), null, aggCalls);
   }
+
+  /**
+   * Given a RelNode, it checks whether there is any filtering condition
+   * below. Basically we check whether the operators
+   * below altered the PK cardinality in any way
+   */
+  public static boolean isRowFilteringPlan(final RelMetadataQuery mq, RelNode operator) {
+    final Multimap<Class<? extends RelNode>, RelNode> nodesBelowNonFkInput =
+        mq.getNodeTypes(operator);
+    for (Entry<Class<? extends RelNode>, Collection<RelNode>> e :
+        nodesBelowNonFkInput.asMap().entrySet()) {
+      if (e.getKey() == TableScan.class) {
+        if (e.getValue().size() > 1) {
+          // Bail out as we may not have more than one TS on non-FK side
+          return true;
+        }
+      } else if (e.getKey() == Project.class) {
+        // We check there is no windowing expression
+        for (RelNode node : e.getValue()) {
+          Project p = (Project) node;
+          for (RexNode expr : p.getChildExps()) {
+            if (expr instanceof RexOver) {
+              // Bail out as it may change cardinality
+              return true;
+            }
+          }
+        }
+      } else if (e.getKey() == Aggregate.class) {
+        // We check there is are not grouping sets
+        for (RelNode node : e.getValue()) {
+          Aggregate a = (Aggregate) node;
+          if (a.getGroupType() != Group.SIMPLE) {
+            // Bail out as it may change cardinality
+            return true;
+          }
+        }
+      } else if (e.getKey() == Sort.class) {
+        // We check whether there is a limit clause
+        for (RelNode node : e.getValue()) {
+          Sort s = (Sort) node;
+          if (s.fetch != null || s.offset != null) {
+            // Bail out as it may change cardinality
+            return true;
+          }
+        }
+      } else {
+        // Bail out, we cannot rewrite the expression if non-fk side cardinality
+        // is being altered
+        return true;
+      }
+    }
+    // It passed all the tests
+    return false;
+  }
+
+  public static Pair<Boolean, List<RexNode>> isRewritablePKFKJoin(RelBuilder builder, Join join,
+        boolean leftInputPotentialFK, RelMetadataQuery mq) {
+    final JoinRelType joinType = join.getJoinType();
+    final RexNode cond = join.getCondition();
+    final RelNode fkInput = leftInputPotentialFK ? join.getLeft() : join.getRight();
+    final RelNode nonFkInput = leftInputPotentialFK ? join.getRight() : join.getLeft();
+    final Pair<Boolean, List<RexNode>> nonRewritable = Pair.of(false, null);
+
+    if (joinType != JoinRelType.INNER) {
+      // If it is not an inner, we transform it as the metadata
+      // providers for expressions do not pull information through
+      // outer join (as it would not be correct)
+      join = (Join) builder
+          .push(join.getLeft()).push(join.getRight())
+          .join(JoinRelType.INNER, cond)
+          .build();
+    }
+
+    // 1) Check whether there is any filtering condition on the
+    // non-FK side. Basically we check whether the operators
+    // below altered the PK cardinality in any way
+    if (HiveRelOptUtil.isRowFilteringPlan(mq, nonFkInput)) {
+      return nonRewritable;
+    }
+
+    // 2) Check whether there is an FK relationship
+    final Map<RexTableInputRef, RexNode> refToRex = new HashMap<>();
+    final EquivalenceClasses ec = new EquivalenceClasses();
+    for (RexNode conj : RelOptUtil.conjunctions(cond)) {
+      if (!conj.isA(SqlKind.EQUALS)) {
+        // Not an equality, we bail out
+        return nonRewritable;
+      }
+      RexCall equiCond = (RexCall) conj;
+      RexNode eqOp1 = equiCond.getOperands().get(0);
+      Set<RexNode> eqOp1ExprsLineage = mq.getExpressionLineage(join, eqOp1);
+      if (eqOp1ExprsLineage == null) {
+        // Cannot be mapped, bail out
+        return nonRewritable;
+      }
+      RexNode eqOp2 = equiCond.getOperands().get(1);
+      Set<RexNode> eqOp2ExprsLineage = mq.getExpressionLineage(join, eqOp2);
+      if (eqOp2ExprsLineage == null) {
+        // Cannot be mapped, bail out
+        return nonRewritable;
+      }
+      List<RexTableInputRef> eqOp2ExprsFiltered = null;
+      for (RexNode eqOpExprLineage1 : eqOp1ExprsLineage) {
+        RexTableInputRef inputRef1 = extractTableInputRef(eqOpExprLineage1);
+        if (inputRef1 == null) {
+          // Bail out as this condition could not be map into an input reference
+          return nonRewritable;
+        }
+        refToRex.put(inputRef1, eqOp1);
+        if (eqOp2ExprsFiltered == null) {
+          // First iteration
+          eqOp2ExprsFiltered = new ArrayList<>();
+          for (RexNode eqOpExprLineage2 : eqOp2ExprsLineage) {
+            RexTableInputRef inputRef2 = extractTableInputRef(eqOpExprLineage2);
+            if (inputRef2 == null) {
+              // Bail out as this condition could not be map into an input reference
+              return nonRewritable;
+            }
+            // Add to list of expressions for follow-up iterations
+            eqOp2ExprsFiltered.add(inputRef2);
+            // Add to equivalence classes and backwards mapping
+            ec.addEquivalenceClass(inputRef1, inputRef2);
+            refToRex.put(inputRef2, eqOp2);
+          }
+        } else {
+          // Rest of iterations, only adding, no checking
+          for (RexTableInputRef inputRef2 : eqOp2ExprsFiltered) {
+            ec.addEquivalenceClass(inputRef1, inputRef2);
+          }
+        }
+      }
+    }
+    if (ec.getEquivalenceClassesMap().isEmpty()) {
+      // This may be a cartesian product, we bail out
+      return nonRewritable;
+    }
+
+    // 3) Gather all tables from the FK side and the table from the
+    // non-FK side
+    final Set<RelTableRef> leftTables = mq.getTableReferences(join.getLeft());
+    final Set<RelTableRef> rightTables =
+        Sets.difference(mq.getTableReferences(join), mq.getTableReferences(join.getLeft()));
+    final Set<RelTableRef> fkTables = join.getLeft() == fkInput ? leftTables : rightTables;
+    final Set<RelTableRef> nonFkTables = join.getLeft() == fkInput ? rightTables : leftTables;
+    assert nonFkTables.size() == 1;
+    final RelTableRef nonFkTable = nonFkTables.iterator().next();
+    final List<String> nonFkTableQName = nonFkTable.getQualifiedName();
+
+    // 4) For each table, check whether there is a matching on the non-FK side.
+    // If there is and it is the only condition, we are ready to transform
+    boolean canBeRewritten = false;
+    List<RexNode> nullableNodes = null;
+    for (RelTableRef tRef : fkTables) {
+      List<RelReferentialConstraint> constraints = tRef.getTable().getReferentialConstraints();
+      for (RelReferentialConstraint constraint : constraints) {
+        if (constraint.getTargetQualifiedName().equals(nonFkTableQName)) {
+          nullableNodes = new ArrayList<>();
+          EquivalenceClasses ecT = EquivalenceClasses.copy(ec);
+          boolean allContained = true;
+          for (int pos = 0; pos < constraint.getNumColumns(); pos++) {
+            int foreignKeyPos = constraint.getColumnPairs().get(pos).source;
+            RelDataType foreignKeyColumnType =
+                tRef.getTable().getRowType().getFieldList().get(foreignKeyPos).getType();
+            RexTableInputRef foreignKeyColumnRef =
+                RexTableInputRef.of(tRef, foreignKeyPos, foreignKeyColumnType);
+            int uniqueKeyPos = constraint.getColumnPairs().get(pos).target;
+            RexTableInputRef uniqueKeyColumnRef = RexTableInputRef.of(nonFkTable, uniqueKeyPos,
+                nonFkTable.getTable().getRowType().getFieldList().get(uniqueKeyPos).getType());
+            if (ecT.getEquivalenceClassesMap().containsKey(uniqueKeyColumnRef) &&
+                ecT.getEquivalenceClassesMap().get(uniqueKeyColumnRef).contains(foreignKeyColumnRef)) {
+              if (foreignKeyColumnType.isNullable()) {
+                if (joinType == JoinRelType.INNER) {
+                  // If it is nullable and it is an INNER, we just need a IS NOT NULL filter
+                  RexNode originalCondOp = refToRex.get(foreignKeyColumnRef);
+                  assert originalCondOp != null;
+                  nullableNodes.add(originalCondOp);
+                } else {
+                  // If it is nullable and this is not an INNER, we cannot execute any transformation
+                  allContained = false;
+                  break;
+                }
+              }
+              // Remove this condition from eq classes as we have checked that it is present
+              // in the join condition
+              ecT.getEquivalenceClassesMap().get(uniqueKeyColumnRef).remove(foreignKeyColumnRef);
+              if (ecT.getEquivalenceClassesMap().get(uniqueKeyColumnRef).size() == 1) { // self
+                ecT.getEquivalenceClassesMap().remove(uniqueKeyColumnRef);
+              }
+              ecT.getEquivalenceClassesMap().get(foreignKeyColumnRef).remove(uniqueKeyColumnRef);
+              if (ecT.getEquivalenceClassesMap().get(foreignKeyColumnRef).size() == 1) { // self
+                ecT.getEquivalenceClassesMap().remove(foreignKeyColumnRef);
+              }
+            } else {
+              // No relationship, we cannot do anything
+              allContained = false;
+              break;
+            }
+          }
+          if (allContained && ecT.getEquivalenceClassesMap().isEmpty()) {
+            // We made it
+            canBeRewritten = true;
+            break;
+          }
+        }
+      }
+    }
+
+    return Pair.of(canBeRewritten, nullableNodes);
+  }
+
+  private static RexTableInputRef extractTableInputRef(RexNode node) {
+    RexTableInputRef ref = null;
+    if (node instanceof RexTableInputRef) {
+      ref = (RexTableInputRef) node;
+    } else if (RexUtil.isLosslessCast(node) &&
+        ((RexCall) node).getOperands().get(0) instanceof RexTableInputRef) {
+      ref = (RexTableInputRef) ((RexCall) node).getOperands().get(0);
+    }
+    return ref;
+  }
+
+  /**
+   * Class representing an equivalence class, i.e., a set of equivalent columns
+   *
+   * TODO: This is a subset of a private class in materialized view rewriting
+   * in Calcite. It should be moved to its own class in Calcite so it can be
+   * accessible here.
+   */
+  private static class EquivalenceClasses {
+
+    private final Map<RexTableInputRef, Set<RexTableInputRef>> nodeToEquivalenceClass;
+
+    protected EquivalenceClasses() {
+      nodeToEquivalenceClass = new HashMap<>();
+    }
+
+    protected static EquivalenceClasses copy(EquivalenceClasses ec) {
+      final EquivalenceClasses newEc = new EquivalenceClasses();
+      for (Entry<RexTableInputRef, Set<RexTableInputRef>> e : ec.nodeToEquivalenceClass.entrySet()) {
+        newEc.nodeToEquivalenceClass.put(e.getKey(), Sets.newLinkedHashSet(e.getValue()));
+      }
+      return newEc;
+    }
+
+    protected void addEquivalenceClass(RexTableInputRef p1, RexTableInputRef p2) {
+      Set<RexTableInputRef> c1 = nodeToEquivalenceClass.get(p1);
+      Set<RexTableInputRef> c2 = nodeToEquivalenceClass.get(p2);
+      if (c1 != null && c2 != null) {
+        // Both present, we need to merge
+        if (c1.size() < c2.size()) {
+          // We swap them to merge
+          Set<RexTableInputRef> c2Temp = c2;
+          c2 = c1;
+          c1 = c2Temp;
+        }
+        for (RexTableInputRef newRef : c2) {
+          c1.add(newRef);
+          nodeToEquivalenceClass.put(newRef, c1);
+        }
+      } else if (c1 != null) {
+        // p1 present, we need to merge into it
+        c1.add(p2);
+        nodeToEquivalenceClass.put(p2, c1);
+      } else if (c2 != null) {
+        // p2 present, we need to merge into it
+        c2.add(p1);
+        nodeToEquivalenceClass.put(p1, c2);
+      } else {
+        // None are present, add to same equivalence class
+        Set<RexTableInputRef> equivalenceClass = new LinkedHashSet<>();
+        equivalenceClass.add(p1);
+        equivalenceClass.add(p2);
+        nodeToEquivalenceClass.put(p1, equivalenceClass);
+        nodeToEquivalenceClass.put(p2, equivalenceClass);
+      }
+    }
+
+    protected Map<RexTableInputRef, Set<RexTableInputRef>> getEquivalenceClassesMap() {
+      return nodeToEquivalenceClass;
+    }
+  }
 }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveJoinConstraintsRule.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveJoinConstraintsRule.java
new file mode 100644
index 00000000000..18be034e30c
--- /dev/null
+++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveJoinConstraintsRule.java
@@ -0,0 +1,295 @@
+/*
+ * 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.optimizer.calcite.rules;
+
+import com.google.common.collect.Multimap;
+import com.google.common.collect.Sets;
+import java.util.ArrayList;
+import java.util.Collection;
+import java.util.HashMap;
+import java.util.LinkedHashSet;
+import java.util.List;
+import java.util.Map;
+import java.util.Map.Entry;
+import java.util.Set;
+import java.util.stream.Collectors;
+import org.apache.calcite.plan.RelOptRule;
+import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelOptUtil;
+import org.apache.calcite.rel.RelNode;
+import org.apache.calcite.rel.RelReferentialConstraint;
+import org.apache.calcite.rel.core.Aggregate;
+import org.apache.calcite.rel.core.Aggregate.Group;
+import org.apache.calcite.rel.core.Join;
+import org.apache.calcite.rel.core.JoinRelType;
+import org.apache.calcite.rel.core.Project;
+import org.apache.calcite.rel.core.TableScan;
+import org.apache.calcite.rel.metadata.RelMetadataQuery;
+import org.apache.calcite.rel.type.RelDataType;
+import org.apache.calcite.rex.RexBuilder;
+import org.apache.calcite.rex.RexCall;
+import org.apache.calcite.rex.RexInputRef;
+import org.apache.calcite.rex.RexNode;
+import org.apache.calcite.rex.RexOver;
+import org.apache.calcite.rex.RexPermuteInputsShuttle;
+import org.apache.calcite.rex.RexTableInputRef;
+import org.apache.calcite.rex.RexTableInputRef.RelTableRef;
+import org.apache.calcite.rex.RexUtil;
+import org.apache.calcite.sql.SqlKind;
+import org.apache.calcite.sql.fun.SqlStdOperatorTable;
+import org.apache.calcite.tools.RelBuilderFactory;
+import org.apache.calcite.util.ImmutableBitSet;
+import org.apache.calcite.util.Pair;
+import org.apache.calcite.util.mapping.Mapping;
+import org.apache.calcite.util.mapping.MappingType;
+import org.apache.calcite.util.mapping.Mappings;
+import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelFactories;
+import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelOptUtil;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+/**
+ * The rule can perform two different optimizations.
+ * 1) Removes a join if is does not alter the cardinality of the one of its inputs.
+ * In particular, this rule is triggered if:
+ * - it is a join on PK-FK/UK-FK,
+ * - project on top only references columns from the FK side, and
+ * - PK/UK side is not filtered
+ * It optionally adds an IS NOT NULL filter if any FK column can be nullable
+ * 2) Transforms a left/right outer join into an inner join if:
+ * - it is a join on PK-FK/UK-FK,
+ * - FK is not nullable
+ * - PK/UK side is not filtered
+ */
+public class HiveJoinConstraintsRule extends RelOptRule {
+
+  public static final HiveJoinConstraintsRule INSTANCE =
+      new HiveJoinConstraintsRule(HiveRelFactories.HIVE_BUILDER);
+  protected static final Logger LOG = LoggerFactory.getLogger(HiveJoinConstraintsRule.class);
+
+
+  protected HiveJoinConstraintsRule(RelBuilderFactory relBuilder) {
+    super(
+        operand(Project.class,
+            some(operand(Join.class, any()))),
+        relBuilder, "HiveJoinConstraintsRule");
+  }
+
+  private static RexTableInputRef extractTableInputRef(RexNode node) {
+    RexTableInputRef ref = null;
+    if (node instanceof RexTableInputRef) {
+      ref = (RexTableInputRef) node;
+    } else if (RexUtil.isLosslessCast(node) &&
+        ((RexCall) node).getOperands().get(0) instanceof RexTableInputRef) {
+      ref = (RexTableInputRef) ((RexCall) node).getOperands().get(0);
+    }
+    return ref;
+  }
+
+  @Override
+  public void onMatch(RelOptRuleCall call) {
+    final Project project = call.rel(0);
+    final RexBuilder rexBuilder = project.getCluster().getRexBuilder();
+    List<RexNode> topProjExprs = project.getChildExps();
+    Join join = call.rel(1);
+    final JoinRelType joinType = join.getJoinType();
+    final RelNode leftInput = join.getLeft();
+    final RelNode rightInput = join.getRight();
+    final RexNode cond = join.getCondition();
+
+    // 1) If it is an inner, check whether project only uses columns from one side.
+    // That side will need to be the FK side.
+    // If it is a left outer, left will be the FK side.
+    // If it is a right outer, right will be the FK side.
+    final RelNode fkInput;
+    final ImmutableBitSet topRefs =
+        RelOptUtil.InputFinder.bits(topProjExprs, null);
+    final ImmutableBitSet leftBits =
+        ImmutableBitSet.range(leftInput.getRowType().getFieldCount());
+    final ImmutableBitSet rightBits =
+        ImmutableBitSet.range(leftInput.getRowType().getFieldCount(),
+            join.getRowType().getFieldCount());
+    // These boolean values represent corresponding left, right input which is potential FK
+    boolean leftInputPotentialFK = topRefs.intersects(leftBits);
+    boolean rightInputPotentialFK = topRefs.intersects(rightBits);
+    if (leftInputPotentialFK && rightInputPotentialFK && joinType == JoinRelType.INNER) {
+      // Both inputs are referenced. Before making a decision, try to swap
+      // references in join condition if it is an inner join, i.e. if a join
+      // condition column is referenced above the join, then we can just
+      // reference the column from the other side.
+      // For example, given two relations R(a1,a2), S(b1) :
+      // SELECT a2, b1 FROM R, S ON R.a1=R.b1 =>
+      // SELECT a2, a1 FROM R, S ON R.a1=R.b1
+      int joinFieldCount = join.getRowType().getFieldCount();
+      Mapping mappingLR = Mappings.create(MappingType.PARTIAL_FUNCTION, joinFieldCount, joinFieldCount);
+      Mapping mappingRL = Mappings.create(MappingType.PARTIAL_FUNCTION, joinFieldCount, joinFieldCount);
+      for (RexNode conj : RelOptUtil.conjunctions(cond)) {
+        if (!conj.isA(SqlKind.EQUALS)) {
+          continue;
+        }
+        RexCall eq = (RexCall) conj;
+        RexNode op1 = eq.getOperands().get(0);
+        RexNode op2 = eq.getOperands().get(1);
+        if (op1 instanceof RexInputRef && op2 instanceof RexInputRef) {
+          // Check references
+          int ref1 = ((RexInputRef) op1).getIndex();
+          int ref2 = ((RexInputRef) op2).getIndex();
+          int leftRef = -1;
+          int rightRef = -1;
+          if (leftBits.get(ref1) && rightBits.get(ref2)) {
+            leftRef = ref1;
+            rightRef = ref2;
+          } else if (rightBits.get(ref1) && leftBits.get(ref2)) {
+            leftRef = ref2;
+            rightRef = ref1;
+          }
+          if (leftRef != -1 && rightRef != -1) {
+            // We do not add more than one mapping per source
+            // as it is useless
+            if (mappingLR.getTargetOpt(leftRef) == -1) {
+              mappingLR.set(leftRef, rightRef);
+            }
+            if (mappingRL.getTargetOpt(rightRef) == -1) {
+              mappingRL.set(rightRef, leftRef);
+            }
+          }
+        }
+      }
+      if (mappingLR.size() != 0) {
+        // First insert missing elements into the mapping as identity mappings
+        for (int i = 0; i < joinFieldCount; i++) {
+          if (mappingLR.getTargetOpt(i) == -1) {
+            mappingLR.set(i, i);
+          }
+          if (mappingRL.getTargetOpt(i) == -1) {
+            mappingRL.set(i, i);
+          }
+        }
+        // Then, we start by trying to reference only left side in top projections
+        List<RexNode> swappedTopProjExprs = topProjExprs.stream()
+            .map(projExpr -> projExpr.accept(new RexPermuteInputsShuttle(mappingRL, call.rel(1))))
+            .collect(Collectors.toList());
+        rightInputPotentialFK = RelOptUtil.InputFinder.bits(swappedTopProjExprs, null).intersects(rightBits);
+        if (!rightInputPotentialFK) {
+          topProjExprs = swappedTopProjExprs;
+        } else {
+          // If it did not work, we try to reference only right side in top projections
+          swappedTopProjExprs = topProjExprs.stream()
+              .map(projExpr -> projExpr.accept(new RexPermuteInputsShuttle(mappingLR, call.rel(1))))
+              .collect(Collectors.toList());
+          leftInputPotentialFK = RelOptUtil.InputFinder.bits(swappedTopProjExprs, null).intersects(leftBits);
+          if (!leftInputPotentialFK) {
+            topProjExprs = swappedTopProjExprs;
+          }
+        }
+      }
+    } else if (!leftInputPotentialFK && !rightInputPotentialFK) {
+      // TODO: There are no references in the project operator above.
+      // In this case, we should probably do two passes, one for
+      // left as FK and one for right as FK, although it may be expensive.
+      // Currently we only assume left as FK
+      leftInputPotentialFK = true;
+    }
+
+    final Mode mode;
+    switch (joinType) {
+    case INNER:
+      if (leftInputPotentialFK && rightInputPotentialFK) {
+        // Bails out as it references columns from both sides (or no columns)
+        // and there is nothing to transform
+        return;
+      }
+      fkInput = leftInputPotentialFK ? leftInput : rightInput;
+      mode = Mode.REMOVE;
+      break;
+    case LEFT:
+      fkInput = leftInput;
+      mode = leftInputPotentialFK && !rightInputPotentialFK ? Mode.REMOVE : Mode.TRANSFORM;
+      break;
+    case RIGHT:
+      fkInput = rightInput;
+      mode = !leftInputPotentialFK && rightInputPotentialFK ? Mode.REMOVE : Mode.TRANSFORM;
+      break;
+    default:
+      // Other type, bail out
+      return;
+    }
+
+    // 2) Check whether this join can be rewritten or removed
+    Pair<Boolean, List<RexNode>> r = HiveRelOptUtil.isRewritablePKFKJoin(call.builder(),
+        join, leftInput == fkInput, call.getMetadataQuery());
+
+    // 3) If it is the only condition, we can trigger the rewriting
+    if (r.left) {
+      List<RexNode> nullableNodes = r.right;
+      // If we reach here, we trigger the transform
+      if (mode == Mode.REMOVE) {
+        if (rightInputPotentialFK) {
+          // First, if FK is the right input, we need to shift
+          nullableNodes = nullableNodes.stream()
+              .map(node -> RexUtil.shift(node, 0, -leftInput.getRowType().getFieldCount()))
+              .collect(Collectors.toList());
+          topProjExprs = topProjExprs.stream()
+              .map(node -> RexUtil.shift(node, 0, -leftInput.getRowType().getFieldCount()))
+              .collect(Collectors.toList());
+        }
+        // Fix nullability in references to the input node
+        topProjExprs = RexUtil.fixUp(rexBuilder, topProjExprs, RelOptUtil.getFieldTypeList(fkInput.getRowType()));
+        // Trigger transformation
+        if (nullableNodes.isEmpty()) {
+          call.transformTo(call.builder()
+              .push(fkInput)
+              .project(topProjExprs)
+              .convert(project.getRowType(), false)
+              .build());
+        } else {
+          RexNode newFilterCond;
+          if (nullableNodes.size() == 1) {
+            newFilterCond = rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, nullableNodes.get(0));
+          } else {
+            List<RexNode> isNotNullConds = new ArrayList<>();
+            for (RexNode nullableNode : nullableNodes) {
+              isNotNullConds.add(rexBuilder.makeCall(SqlStdOperatorTable.IS_NOT_NULL, nullableNode));
+            }
+            newFilterCond = rexBuilder.makeCall(SqlStdOperatorTable.AND, isNotNullConds);
+          }
+          call.transformTo(call.builder()
+              .push(fkInput)
+              .filter(newFilterCond)
+              .project(topProjExprs)
+              .convert(project.getRowType(), false)
+              .build());
+        }
+      } else { // Mode.TRANSFORM
+        // Trigger transformation
+        call.transformTo(call.builder()
+            .push(leftInput).push(rightInput)
+            .join(JoinRelType.INNER, join.getCondition())
+            .convert(call.rel(1).getRowType(), false) // Preserve nullability
+            .project(project.getChildExps())
+            .build());
+      }
+    }
+  }
+
+  private enum Mode {
+    // Removes join operator from the plan
+    REMOVE,
+    // Transforms LEFT/RIGHT outer join into INNER join
+    TRANSFORM
+  }
+}
\ No newline at end of file
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveJoinProjectTransposeRule.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveJoinProjectTransposeRule.java
index 8214cc9a0d0..e6844326068 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveJoinProjectTransposeRule.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveJoinProjectTransposeRule.java
@@ -27,6 +27,21 @@ import org.apache.hadoop.hive.ql.optimizer.calcite.reloperators.HiveProject;
 
 public class HiveJoinProjectTransposeRule extends JoinProjectTransposeRule {
 
+  public static final HiveJoinProjectTransposeRule LEFF_PROJECT_BTW_JOIN =
+      new HiveJoinProjectTransposeRule(
+          operand(HiveJoin.class,
+                  operand(HiveProject.class, operand(HiveJoin.class, any())),
+                  operand(RelNode.class, any())),
+          "JoinProjectTransposeRule(Project-Join-Other)",
+          false, HiveRelFactories.HIVE_BUILDER);
+  public static final HiveJoinProjectTransposeRule RIGHT_PROJECT_BTW_JOIN =
+      new HiveJoinProjectTransposeRule(
+          operand(HiveJoin.class,
+                  operand(RelNode.class, any()),
+                  operand(HiveProject.class, operand(HiveJoin.class, any()))),
+          "JoinProjectTransposeRule(Other-Project-Join)",
+          false, HiveRelFactories.HIVE_BUILDER);
+
   public static final HiveJoinProjectTransposeRule BOTH_PROJECT =
       new HiveJoinProjectTransposeRule(
           operand(HiveJoin.class,
diff --git a/common/src/java/org/apache/hive/common/util/DateParser.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveProjectJoinTransposeRule.java
similarity index 55%
copy from common/src/java/org/apache/hive/common/util/DateParser.java
copy to ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveProjectJoinTransposeRule.java
index 5db14f19069..8dc6d72f765 100644
--- a/common/src/java/org/apache/hive/common/util/DateParser.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveProjectJoinTransposeRule.java
@@ -15,37 +15,20 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.hive.common.util;
+package org.apache.hadoop.hive.ql.optimizer.calcite.rules;
 
-import org.apache.hadoop.hive.common.type.Date;
+import org.apache.calcite.rel.rules.ProjectJoinTransposeRule;
+import org.apache.calcite.rel.rules.PushProjector;
+import org.apache.calcite.tools.RelBuilderFactory;
+import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelFactories;
 
-/**
- * Date parser class for Hive.
- */
-public class DateParser {
+public class HiveProjectJoinTransposeRule extends ProjectJoinTransposeRule {
 
-  public DateParser() {
- }
+  public static final HiveProjectJoinTransposeRule INSTANCE =
+      new HiveProjectJoinTransposeRule(HiveRelFactories.HIVE_BUILDER);
 
-  public Date parseDate(String strValue) {
-    Date result = new Date();
-    if (parseDate(strValue, result)) {
-      return result;
-    }
-    return null;
+  private HiveProjectJoinTransposeRule(RelBuilderFactory relBuilderFactory) {
+    super(PushProjector.ExprCondition.TRUE, relBuilderFactory);
   }
 
-  public boolean parseDate(String strValue, Date result) {
-    Date parsedVal;
-    try {
-      parsedVal = Date.valueOf(strValue);
-    } catch (IllegalArgumentException e) {
-      parsedVal = null;
-    }
-    if (parsedVal == null) {
-      return false;
-    }
-    result.setTimeInMillis(parsedVal.toEpochMilli());
-    return true;
-  }
 }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveProjectMergeRule.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveProjectMergeRule.java
index 07518df9ec1..67ecdec0414 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveProjectMergeRule.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/calcite/rules/HiveProjectMergeRule.java
@@ -20,11 +20,14 @@ package org.apache.hadoop.hive.ql.optimizer.calcite.rules;
 import java.util.Set;
 
 import org.apache.calcite.plan.RelOptRuleCall;
+import org.apache.calcite.plan.RelOptUtil;
 import org.apache.calcite.rel.core.Project;
 import org.apache.calcite.rel.rules.ProjectMergeRule;
 import org.apache.calcite.rex.RexNode;
 import org.apache.calcite.rex.RexOver;
+import org.apache.calcite.tools.RelBuilder;
 import org.apache.calcite.tools.RelBuilderFactory;
+import org.apache.calcite.util.ImmutableBitSet;
 import org.apache.hadoop.hive.ql.optimizer.calcite.HiveCalciteUtil;
 import org.apache.hadoop.hive.ql.optimizer.calcite.HiveRelFactories;
 
@@ -66,4 +69,22 @@ public class HiveProjectMergeRule extends ProjectMergeRule {
     return super.matches(call);
   }
 
+  public void onMatch(RelOptRuleCall call) {
+    final Project topProject = call.rel(0);
+    final Project bottomProject = call.rel(1);
+
+    // If top project does not reference any column at the bottom project,
+    // we can just remove botton project
+    final ImmutableBitSet topRefs =
+        RelOptUtil.InputFinder.bits(topProject.getChildExps(), null);
+    if (topRefs.isEmpty()) {
+      RelBuilder relBuilder = call.builder();
+      relBuilder.push(bottomProject.getInput());
+      relBuilder.project(topProject.getChildExps());
+      call.transformTo(relBuilder.build());
+      return;
+    }
+    super.onMatch(call);
+  }
+
 }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/metainfo/annotation/OpTraitsRulesProcFactory.java b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/metainfo/annotation/OpTraitsRulesProcFactory.java
index 8f751260a00..a9d6a5c9427 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/optimizer/metainfo/annotation/OpTraitsRulesProcFactory.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/optimizer/metainfo/annotation/OpTraitsRulesProcFactory.java
@@ -18,6 +18,8 @@
 
 package org.apache.hadoop.hive.ql.optimizer.metainfo.annotation;
 
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
 import java.util.*;
 import java.util.Map.Entry;
 
@@ -124,18 +126,25 @@ public class OpTraitsRulesProcFactory {
                   continue;
                 }
                 // Fetch the column expression. There should be atleast one.
-                Map<Integer, ExprNodeDesc> colMap = new HashMap<>();
+                Multimap<Integer, ExprNodeColumnDesc> colMap = ArrayListMultimap.create();
                 boolean found = false;
                 ExprNodeDescUtils.getExprNodeColumnDesc(entry.getValue(), colMap);
                 for (Integer hashCode : colMap.keySet()) {
-                  ExprNodeColumnDesc expr = (ExprNodeColumnDesc) colMap.get(hashCode);
-                  if (expr.getColumn().equals(col)) {
-                    bucketCols.add(entry.getKey());
-                    found = true;
+                  Collection<ExprNodeColumnDesc> exprs = colMap.get(hashCode);
+                  for (ExprNodeColumnDesc expr : exprs) {
+                    if (expr.getColumn().equals(col)) {
+                      bucketCols.add(entry.getKey());
+                      found = true;
+                      break;
+                    }
+                  }
+                  if (found) {
                     break;
                   }
                 }
-                if (found) break;
+                if (found) {
+                  break;
+                }
               } // column exprmap.
             } // cols
           }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/BaseSemanticAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/BaseSemanticAnalyzer.java
index 5dcf7d5711d..b270bed7f79 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/BaseSemanticAnalyzer.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/BaseSemanticAnalyzer.java
@@ -183,6 +183,10 @@ public abstract class BaseSemanticAnalyzer {
     return false;
   }
 
+  public String getCboInfo() {
+    return ctx.getCboInfo();
+  }
+
   class RowFormatParams {
     String fieldDelim = null;
     String fieldEscape = null;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java
index b71d78ff5c4..db4635b6177 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/CalcitePlanner.java
@@ -100,7 +100,6 @@ import org.apache.calcite.sql.SqlKind;
 import org.apache.calcite.sql.SqlLiteral;
 import org.apache.calcite.sql.SqlNode;
 import org.apache.calcite.sql.SqlOperator;
-import org.apache.calcite.sql.SqlSampleSpec;
 import org.apache.calcite.sql.SqlWindow;
 import org.apache.calcite.sql.dialect.HiveSqlDialect;
 import org.apache.calcite.sql.parser.SqlParserPos;
@@ -114,8 +113,6 @@ import org.apache.calcite.util.ImmutableIntList;
 import org.apache.calcite.util.Pair;
 import org.apache.hadoop.fs.Path;
 import org.apache.hadoop.hive.common.ObjectPair;
-import org.apache.hadoop.hive.common.ValidTxnList;
-import org.apache.hadoop.hive.common.ValidTxnWriteIdList;
 import org.apache.hadoop.hive.conf.Constants;
 import org.apache.hadoop.hive.conf.HiveConf;
 import org.apache.hadoop.hive.conf.HiveConf.ConfVars;
@@ -192,6 +189,7 @@ import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveIntersectMergeRule;
 import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveIntersectRewriteRule;
 import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveJoinAddNotNullRule;
 import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveJoinCommuteRule;
+import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveJoinConstraintsRule;
 import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveJoinProjectTransposeRule;
 import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveJoinPushTransitivePredicatesRule;
 import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveJoinToMultiJoinRule;
@@ -199,6 +197,7 @@ import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HivePartitionPruneRule;
 import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HivePointLookupOptimizerRule;
 import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HivePreFilteringRule;
 import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveProjectFilterPullUpConstantsRule;
+import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveProjectJoinTransposeRule;
 import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveProjectMergeRule;
 import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveProjectOverIntersectRemoveRule;
 import org.apache.hadoop.hive.ql.optimizer.calcite.rules.HiveProjectSortTransposeRule;
@@ -529,7 +528,15 @@ public class CalcitePlanner extends SemanticAnalyzer {
             this.ctx.setCboSucceeded(true);
             if (this.ctx.isExplainPlan()) {
               ExplainConfiguration explainConfig = this.ctx.getExplainConfig();
-              if (explainConfig.isExtended() || explainConfig.isFormatted()) {
+              if (explainConfig.isCbo()) {
+                if (explainConfig.isCboExtended()) {
+                  // Include join cost
+                  this.ctx.setCalcitePlan(RelOptUtil.toString(newPlan, SqlExplainLevel.ALL_ATTRIBUTES));
+                } else {
+                  // Do not include join cost
+                  this.ctx.setCalcitePlan(RelOptUtil.toString(newPlan));
+                }
+              } else if (explainConfig.isExtended() || explainConfig.isFormatted()) {
                 this.ctx.setOptimizedSql(getOptimizedSql(newPlan));
               }
             }
@@ -1663,7 +1670,8 @@ public class CalcitePlanner extends SemanticAnalyzer {
 
   private enum ExtendedCBOProfile {
     JOIN_REORDERING,
-    WINDOWING_POSTPROCESSING;
+    WINDOWING_POSTPROCESSING,
+    REFERENTIAL_CONSTRAINTS;
   }
 
   /**
@@ -1781,7 +1789,22 @@ public class CalcitePlanner extends SemanticAnalyzer {
             calcitePreCboPlan, mdProvider.getMetadataProvider(), executorProvider);
       }
 
-      // 4. Apply join order optimizations: reordering MST algorithm
+      // Get rid of sq_count_check if group by key is constant
+      if (conf.getBoolVar(ConfVars.HIVE_REMOVE_SQ_COUNT_CHECK)) {
+        perfLogger.PerfLogBegin(this.getClass().getName(), PerfLogger.OPTIMIZER);
+        calcitePreCboPlan =
+            hepPlan(calcitePreCboPlan, false, mdProvider.getMetadataProvider(), null,
+                    HiveRemoveSqCountCheck.INSTANCE);
+        perfLogger.PerfLogEnd(this.getClass().getName(), PerfLogger.OPTIMIZER,
+                              "Calcite: Removing sq_count_check UDF ");
+      }
+      //  4.1 Remove Projects between Joins so that JoinToMultiJoinRule can merge them to MultiJoin.
+      //    Don't run this rule if hive is to remove sq_count_check since that rule expects to have project b/w join.
+      calcitePreCboPlan = hepPlan(calcitePreCboPlan, true, mdProvider.getMetadataProvider(), executorProvider,
+          HepMatchOrder.BOTTOM_UP, HiveJoinProjectTransposeRule.LEFF_PROJECT_BTW_JOIN,
+          HiveJoinProjectTransposeRule.RIGHT_PROJECT_BTW_JOIN);
+
+      // 4.2 Apply join order optimizations: reordering MST algorithm
       //    If join optimizations failed because of missing stats, we continue with
       //    the rest of optimizations
       if (profilesCBO.contains(ExtendedCBOProfile.JOIN_REORDERING)) {
@@ -1870,17 +1893,8 @@ public class CalcitePlanner extends SemanticAnalyzer {
             HiveRemoveGBYSemiJoinRule.INSTANCE);
         perfLogger.PerfLogEnd(this.getClass().getName(), PerfLogger.OPTIMIZER, "Calcite: Removal of gby from semijoin");
 
-      // 9. Get rid of sq_count_check if group by key is constant (HIVE-)
-      if (conf.getBoolVar(ConfVars.HIVE_REMOVE_SQ_COUNT_CHECK)) {
-        perfLogger.PerfLogBegin(this.getClass().getName(), PerfLogger.OPTIMIZER);
-        calciteOptimizedPlan =
-            hepPlan(calciteOptimizedPlan, false, mdProvider.getMetadataProvider(), null,
-                HiveRemoveSqCountCheck.INSTANCE);
-        perfLogger.PerfLogEnd(this.getClass().getName(), PerfLogger.OPTIMIZER,
-            "Calcite: Removing sq_count_check UDF ");
-      }
 
-      // 10. Run rule to fix windowing issue when it is done over
+      // 9. Run rule to fix windowing issue when it is done over
       // aggregation columns (HIVE-10627)
       if (profilesCBO.contains(ExtendedCBOProfile.WINDOWING_POSTPROCESSING)) {
         perfLogger.PerfLogBegin(this.getClass().getName(), PerfLogger.OPTIMIZER);
@@ -1889,7 +1903,7 @@ public class CalcitePlanner extends SemanticAnalyzer {
         perfLogger.PerfLogEnd(this.getClass().getName(), PerfLogger.OPTIMIZER, "Calcite: Window fixing rule");
       }
 
-      // 11. Apply Druid transformation rules
+      // 10. Apply Druid transformation rules
       perfLogger.PerfLogBegin(this.getClass().getName(), PerfLogger.OPTIMIZER);
       calciteOptimizedPlan = hepPlan(calciteOptimizedPlan, false, mdProvider.getMetadataProvider(), null,
           HepMatchOrder.BOTTOM_UP,
@@ -1920,7 +1934,7 @@ public class CalcitePlanner extends SemanticAnalyzer {
               JDBCAggregationPushDownRule.INSTANCE, JDBCSortPushDownRule.INSTANCE
       );
 
-      // 12. Run rules to aid in translation from Calcite tree to Hive tree
+      // 11. Run rules to aid in translation from Calcite tree to Hive tree
       if (HiveConf.getBoolVar(conf, ConfVars.HIVE_CBO_RETPATH_HIVEOP)) {
         perfLogger.PerfLogBegin(this.getClass().getName(), PerfLogger.OPTIMIZER);
         // 12.1. Merge join into multijoin operators (if possible)
@@ -1941,7 +1955,7 @@ public class CalcitePlanner extends SemanticAnalyzer {
                 HiveFilterProjectTSTransposeRule.INSTANCE, HiveFilterProjectTSTransposeRule.INSTANCE_DRUID,
                 HiveProjectFilterPullUpConstantsRule.INSTANCE);
 
-        // 12.2.  Introduce exchange operators below join/multijoin operators
+        // 11.2.  Introduce exchange operators below join/multijoin operators
         calciteOptimizedPlan = hepPlan(calciteOptimizedPlan, false, mdProvider.getMetadataProvider(), null,
                 HepMatchOrder.BOTTOM_UP, HiveInsertExchange4JoinRule.EXCHANGE_BELOW_JOIN,
                 HiveInsertExchange4JoinRule.EXCHANGE_BELOW_MULTIJOIN);
@@ -2053,6 +2067,11 @@ public class CalcitePlanner extends SemanticAnalyzer {
         rules.add(new HivePointLookupOptimizerRule.FilterCondition(minNumORClauses));
         rules.add(new HivePointLookupOptimizerRule.JoinCondition(minNumORClauses));
       }
+      if (conf.getBoolVar(HiveConf.ConfVars.HIVE_OPTIMIZE_CONSTRAINTS_JOIN) &&
+          profilesCBO.contains(ExtendedCBOProfile.REFERENTIAL_CONSTRAINTS)) {
+        rules.add(HiveProjectJoinTransposeRule.INSTANCE);
+        rules.add(HiveJoinConstraintsRule.INSTANCE);
+      }
       rules.add(HiveJoinAddNotNullRule.INSTANCE_JOIN);
       rules.add(HiveJoinAddNotNullRule.INSTANCE_SEMIJOIN);
       rules.add(HiveJoinPushTransitivePredicatesRule.INSTANCE_JOIN);
@@ -2112,21 +2131,16 @@ public class CalcitePlanner extends SemanticAnalyzer {
       perfLogger.PerfLogEnd(this.getClass().getName(), PerfLogger.OPTIMIZER,
         "Calcite: Prejoin ordering transformation, Projection Pruning");
 
-      // 8. Merge, remove and reduce Project if possible
-      perfLogger.PerfLogBegin(this.getClass().getName(), PerfLogger.OPTIMIZER);
-      basePlan = hepPlan(basePlan, false, mdProvider, executorProvider,
-           HiveProjectMergeRule.INSTANCE, ProjectRemoveRule.INSTANCE, HiveSortMergeRule.INSTANCE);
-      perfLogger.PerfLogEnd(this.getClass().getName(), PerfLogger.OPTIMIZER,
-          "Calcite: Prejoin ordering transformation, Merge Project-Project, Merge Sort-Sort");
-
-      // 9. Rerun PPD through Project as column pruning would have introduced
+      // 8. Rerun PPD through Project as column pruning would have introduced
       // DT above scans; By pushing filter just above TS, Hive can push it into
       // storage (incase there are filters on non partition cols). This only
       // matches FIL-PROJ-TS
+      // Also merge, remove and reduce Project if possible
       perfLogger.PerfLogBegin(this.getClass().getName(), PerfLogger.OPTIMIZER);
       basePlan = hepPlan(basePlan, true, mdProvider, executorProvider,
           HiveFilterProjectTSTransposeRule.INSTANCE, HiveFilterProjectTSTransposeRule.INSTANCE_DRUID,
-          HiveProjectFilterPullUpConstantsRule.INSTANCE);
+          HiveProjectFilterPullUpConstantsRule.INSTANCE, HiveProjectMergeRule.INSTANCE,
+          ProjectRemoveRule.INSTANCE, HiveSortMergeRule.INSTANCE);
       perfLogger.PerfLogEnd(this.getClass().getName(), PerfLogger.OPTIMIZER,
         "Calcite: Prejoin ordering transformation, Rerun PPD");
 
@@ -2815,6 +2829,7 @@ public class CalcitePlanner extends SemanticAnalyzer {
         }
 
         // 4. Build operator
+        RelOptHiveTable optTable;
         if (tableType == TableType.DRUID ||
                 (tableType == TableType.JDBC && tabMetaData.getProperty(Constants.JDBC_TABLE) != null)) {
           // Create case sensitive columns list
@@ -2870,7 +2885,7 @@ public class CalcitePlanner extends SemanticAnalyzer {
             DruidTable druidTable = new DruidTable(new DruidSchema(address, address, false),
                 dataSource, RelDataTypeImpl.proto(rowType), metrics, DruidTable.DEFAULT_TIMESTAMP_COLUMN,
                 intervals, null, null);
-            RelOptHiveTable optTable = new RelOptHiveTable(relOptSchema, relOptSchema.getTypeFactory(), fullyQualifiedTabName,
+            optTable = new RelOptHiveTable(relOptSchema, relOptSchema.getTypeFactory(), fullyQualifiedTabName,
                 rowType, tabMetaData, nonPartitionColumns, partitionColumns, virtualCols, conf,
                 partitionCache, colStatsCache, noColsMissingStats);
             final TableScan scan = new HiveTableScan(cluster, cluster.traitSetOf(HiveRelNode.CONVENTION),
@@ -2880,11 +2895,10 @@ public class CalcitePlanner extends SemanticAnalyzer {
                     || qb.getAliasInsideView().contains(tableAlias.toLowerCase()));
             tableRel = DruidQuery.create(cluster, cluster.traitSetOf(BindableConvention.INSTANCE),
                 optTable, druidTable, ImmutableList.of(scan), DruidSqlOperatorConverter.getDefaultMap());
-          } else if (tableType == TableType.JDBC) {
-            RelOptHiveTable optTable = new RelOptHiveTable(relOptSchema, relOptSchema.getTypeFactory(), fullyQualifiedTabName,
+          } else {
+            optTable = new RelOptHiveTable(relOptSchema, relOptSchema.getTypeFactory(), fullyQualifiedTabName,
                   rowType, tabMetaData, nonPartitionColumns, partitionColumns, virtualCols, conf,
                   partitionCache, colStatsCache, noColsMissingStats);
-
             final HiveTableScan hts = new HiveTableScan(cluster,
                   cluster.traitSetOf(HiveRelNode.CONVENTION), optTable,
                   null == tableAlias ? tabMetaData.getTableName() : tableAlias,
@@ -2927,7 +2941,7 @@ public class CalcitePlanner extends SemanticAnalyzer {
             fullyQualifiedTabName.add(tabMetaData.getDbName());
           }
           fullyQualifiedTabName.add(tabMetaData.getTableName());
-          RelOptHiveTable optTable = new RelOptHiveTable(relOptSchema, relOptSchema.getTypeFactory(), fullyQualifiedTabName,
+          optTable = new RelOptHiveTable(relOptSchema, relOptSchema.getTypeFactory(), fullyQualifiedTabName,
               rowType, tabMetaData, nonPartitionColumns, partitionColumns, virtualCols, conf,
               partitionCache, colStatsCache, noColsMissingStats);
           // Build Hive Table Scan Rel
@@ -2938,6 +2952,10 @@ public class CalcitePlanner extends SemanticAnalyzer {
                   || qb.getAliasInsideView().contains(tableAlias.toLowerCase()));
         }
 
+        if (!optTable.getReferentialConstraints().isEmpty()) {
+          profilesCBO.add(ExtendedCBOProfile.REFERENTIAL_CONSTRAINTS);
+        }
+
         // 6. Add Schema(RR) to RelNode-Schema map
         ImmutableMap<String, Integer> hiveToCalciteColMap = buildHiveToCalciteColumnMap(rr,
             tableRel);
@@ -5084,12 +5102,12 @@ public class CalcitePlanner extends SemanticAnalyzer {
         List<ExprNodeDesc> col_list, RowResolver inputRR) {
       // Build a map of Hive column Names (ExprNodeColumnDesc Name)
       // to the positions of those projections in the input
-      Map<Integer, ExprNodeDesc> hashCodeTocolumnDescMap = new HashMap<Integer, ExprNodeDesc>();
+      Multimap<Integer, ExprNodeColumnDesc> hashCodeTocolumnDescMap = ArrayListMultimap.create();
       ExprNodeDescUtils.getExprNodeColumnDesc(col_list, hashCodeTocolumnDescMap);
       ImmutableMap.Builder<String, Integer> hiveColNameToInputPosMapBuilder = new ImmutableMap.Builder<String, Integer>();
       String exprNodecolName;
-      for (ExprNodeDesc exprDesc : hashCodeTocolumnDescMap.values()) {
-        exprNodecolName = ((ExprNodeColumnDesc) exprDesc).getColumn();
+      for (ExprNodeColumnDesc exprDesc : hashCodeTocolumnDescMap.values()) {
+        exprNodecolName = exprDesc.getColumn();
         hiveColNameToInputPosMapBuilder.put(exprNodecolName, inputRR.getPosition(exprNodecolName));
       }
 
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/ExplainConfiguration.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/ExplainConfiguration.java
index 5ca6b59e7ea..1bbbfd9ecd1 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/ExplainConfiguration.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/ExplainConfiguration.java
@@ -41,6 +41,8 @@ public class ExplainConfiguration {
   private boolean extended = false;
   private boolean formatted = false;
   private boolean dependency = false;
+  private boolean cbo = false;
+  private boolean cboExtended = false;
   private boolean logical = false;
   private boolean authorize = false;
   private boolean userLevelExplain = false;
@@ -82,6 +84,22 @@ public class ExplainConfiguration {
     this.dependency = dependency;
   }
 
+  public boolean isCbo() {
+    return cbo;
+  }
+
+  public void setCbo(boolean cbo) {
+    this.cbo = cbo;
+  }
+
+  public boolean isCboExtended() {
+    return cboExtended;
+  }
+
+  public void setCboExtended(boolean cboExtended) {
+    this.cboExtended = cboExtended;
+  }
+
   public boolean isLogical() {
     return logical;
   }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/ExplainSemanticAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/ExplainSemanticAnalyzer.java
index 6ecc9205b41..9ca65d98766 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/ExplainSemanticAnalyzer.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/ExplainSemanticAnalyzer.java
@@ -76,6 +76,14 @@ public class ExplainSemanticAnalyzer extends BaseSemanticAnalyzer {
         config.setExtended(true);
       } else if (explainOptions == HiveParser.KW_DEPENDENCY) {
         config.setDependency(true);
+      } else if (explainOptions == HiveParser.KW_CBO) {
+        config.setCbo(true);
+        if (i + 1 < childCount) {
+          if (ast.getChild(i + 1).getType() == HiveParser.KW_EXTENDED) {
+            config.setCboExtended(true);
+            i++;
+          }
+        }
       } else if (explainOptions == HiveParser.KW_LOGICAL) {
         config.setLogical(true);
       } else if (explainOptions == HiveParser.KW_AUTHORIZATION) {
@@ -188,6 +196,7 @@ public class ExplainSemanticAnalyzer extends BaseSemanticAnalyzer {
     config.setUserLevelExplain(!config.isExtended()
         && !config.isFormatted()
         && !config.isDependency()
+        && !config.isCbo()
         && !config.isLogical()
         && !config.isAuthorize()
         && (
@@ -212,7 +221,8 @@ public class ExplainSemanticAnalyzer extends BaseSemanticAnalyzer {
         sem,
         config,
         ctx.getCboInfo(),
-        ctx.getOptimizedSql());
+        ctx.getOptimizedSql(),
+        ctx.getCalcitePlan());
 
     work.setAppendTaskType(
         HiveConf.getBoolVar(conf, HiveConf.ConfVars.HIVEEXPLAINDEPENDENCYAPPENDTASKTYPES));
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveLexer.g b/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveLexer.g
index c0f7a39201e..9efbaf74038 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveLexer.g
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveLexer.g
@@ -190,6 +190,7 @@ KW_EXTENDED: 'EXTENDED';
 KW_FORMATTED: 'FORMATTED';
 KW_DEPENDENCY: 'DEPENDENCY';
 KW_LOGICAL: 'LOGICAL';
+KW_CBO: 'CBO';
 KW_SERDE: 'SERDE';
 KW_WITH: 'WITH';
 KW_DEFERRED: 'DEFERRED';
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g b/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g
index 9a300d8bd04..6eae2dc004c 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/HiveParser.g
@@ -799,6 +799,7 @@ explainOption
     : KW_EXTENDED
     | KW_FORMATTED
     | KW_DEPENDENCY
+    | KW_CBO KW_EXTENDED?
     | KW_LOGICAL
     | KW_AUTHORIZATION
     | KW_ANALYZE
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g b/ql/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g
index 265a1c7366c..a408d9feb6b 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/IdentifiersParser.g
@@ -791,7 +791,7 @@ principalIdentifier
 nonReserved
     :
     KW_ABORT | KW_ADD | KW_ADMIN | KW_AFTER | KW_ANALYZE | KW_ARCHIVE | KW_ASC | KW_BEFORE | KW_BUCKET | KW_BUCKETS
-    | KW_CASCADE | KW_CHANGE | KW_CHECK | KW_CLUSTER | KW_CLUSTERED | KW_CLUSTERSTATUS | KW_COLLECTION | KW_COLUMNS
+    | KW_CASCADE | KW_CBO | KW_CHANGE | KW_CHECK | KW_CLUSTER | KW_CLUSTERED | KW_CLUSTERSTATUS | KW_COLLECTION | KW_COLUMNS
     | KW_COMMENT | KW_COMPACT | KW_COMPACTIONS | KW_COMPUTE | KW_CONCATENATE | KW_CONTINUE | KW_DATA | KW_DAY
     | KW_DATABASES | KW_DATETIME | KW_DBPROPERTIES | KW_DEFERRED | KW_DEFINED | KW_DELIMITED | KW_DEPENDENCY 
     | KW_DESC | KW_DIRECTORIES | KW_DIRECTORY | KW_DISABLE | KW_DISTRIBUTE | KW_DOW | KW_ELEM_TYPE 
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java
index de098839544..2cf2463b1fa 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/parse/SemanticAnalyzer.java
@@ -25,6 +25,9 @@ import com.google.common.collect.Iterables;
 import com.google.common.collect.Sets;
 import com.google.common.math.IntMath;
 import com.google.common.math.LongMath;
+import com.google.common.collect.ArrayListMultimap;
+import com.google.common.collect.Multimap;
+
 
 import org.antlr.runtime.ClassicToken;
 import org.antlr.runtime.CommonToken;
@@ -3535,7 +3538,7 @@ public class SemanticAnalyzer extends BaseSemanticAnalyzer {
     if (joinKeys == null || joinKeys.length == 0) {
       return input;
     }
-    Map<Integer, ExprNodeDesc> hashes = new HashMap<Integer, ExprNodeDesc>();
+    Multimap<Integer, ExprNodeColumnDesc> hashes = ArrayListMultimap.create();
     if (input instanceof FilterOperator) {
       ExprNodeDescUtils.getExprNodeColumnDesc(Arrays.asList(((FilterDesc)input.getConf()).getPredicate()), hashes);
     }
@@ -3548,7 +3551,14 @@ public class SemanticAnalyzer extends BaseSemanticAnalyzer {
         // virtual column, since those columns can never be null.
         continue;
       }
-      if(null != hashes.get(joinKeys[i].hashCode())) {
+      boolean skip = false;
+      for (ExprNodeColumnDesc node : hashes.get(joinKeys[i].hashCode())) {
+        if (node.isSame(joinKeys[i])) {
+          skip = true;
+          break;
+        }
+      }
+      if (skip) {
         // there is already a predicate on this src.
         continue;
       }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/plan/ExplainWork.java b/ql/src/java/org/apache/hadoop/hive/ql/plan/ExplainWork.java
index a25c435aae6..681dae93686 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/plan/ExplainWork.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/plan/ExplainWork.java
@@ -51,6 +51,7 @@ public class ExplainWork implements Serializable {
   boolean appendTaskType;
 
   String cboInfo;
+  String cboPlan;
 
   private String optimizedSQL;
 
@@ -66,7 +67,8 @@ public class ExplainWork implements Serializable {
       BaseSemanticAnalyzer analyzer,
       ExplainConfiguration config,
       String cboInfo,
-      String optimizedSQL) {
+      String optimizedSQL,
+      String cboPlan) {
     this.resFile = resFile;
     this.rootTasks = new ArrayList<Task<? extends Serializable>>(rootTasks);
     this.fetchTask = fetchTask;
@@ -80,6 +82,7 @@ public class ExplainWork implements Serializable {
     this.pCtx = pCtx;
     this.cboInfo = cboInfo;
     this.optimizedSQL = optimizedSQL;
+    this.cboPlan = cboPlan;
     this.config = config;
   }
 
@@ -155,6 +158,10 @@ public class ExplainWork implements Serializable {
     this.pCtx = pCtx;
   }
 
+  public boolean isCbo() {
+    return config.isCbo();
+  }
+
   public boolean isLogical() {
     return config.isLogical();
   }
@@ -195,6 +202,14 @@ public class ExplainWork implements Serializable {
     this.optimizedSQL = optimizedSQL;
   }
 
+  public String getCboPlan() {
+    return cboPlan;
+  }
+
+  public void setCboPlan(String cboPlan) {
+    this.cboPlan = cboPlan;
+  }
+
   public ExplainConfiguration getConfig() {
     return config;
   }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java b/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java
index ed84ff20641..e15d661ffdb 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/plan/ExprNodeDescUtils.java
@@ -21,7 +21,10 @@ package org.apache.hadoop.hive.ql.plan;
 import java.util.ArrayList;
 import java.util.List;
 import java.util.Map;
-
+import com.google.common.collect.Multimap;
+import java.util.Collection;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
 import org.apache.hadoop.hive.ql.exec.ColumnInfo;
 import org.apache.hadoop.hive.ql.exec.ExprNodeEvaluator;
 import org.apache.hadoop.hive.ql.exec.ExprNodeEvaluatorFactory;
@@ -527,27 +530,37 @@ public class ExprNodeDescUtils {
     } catch (Exception e) {
       return null;
     }
-	}
+  }
 
-	public static void getExprNodeColumnDesc(List<ExprNodeDesc> exprDescList,
-			Map<Integer, ExprNodeDesc> hashCodeTocolumnDescMap) {
-		for (ExprNodeDesc exprNodeDesc : exprDescList) {
-			getExprNodeColumnDesc(exprNodeDesc, hashCodeTocolumnDescMap);
-		}
-	}
+  public static void getExprNodeColumnDesc(List<ExprNodeDesc> exprDescList,
+      Multimap<Integer, ExprNodeColumnDesc> hashCodeTocolumnDescMap) {
+    for (ExprNodeDesc exprNodeDesc : exprDescList) {
+      getExprNodeColumnDesc(exprNodeDesc, hashCodeTocolumnDescMap);
+    }
+  }
 
 	/**
 	 * Get Map of ExprNodeColumnDesc HashCode to ExprNodeColumnDesc.
-	 * 
+	 *
 	 * @param exprDesc
 	 * @param hashCodeToColumnDescMap
 	 *            Assumption: If two ExprNodeColumnDesc have same hash code then
 	 *            they are logically referring to same projection
 	 */
 	public static void getExprNodeColumnDesc(ExprNodeDesc exprDesc,
-			Map<Integer, ExprNodeDesc> hashCodeToColumnDescMap) {
+                                             Multimap<Integer, ExprNodeColumnDesc> hashCodeToColumnDescMap) {
 		if (exprDesc instanceof ExprNodeColumnDesc) {
-			hashCodeToColumnDescMap.put(exprDesc.hashCode(), exprDesc);
+          Collection<ExprNodeColumnDesc> nodes = hashCodeToColumnDescMap.get(exprDesc.hashCode());
+          boolean insert = true;
+          for (ExprNodeColumnDesc node : nodes) {
+            if (node.isSame(exprDesc)) {
+              insert = false;
+              break;
+            }
+          }
+          if (insert) {
+            nodes.add((ExprNodeColumnDesc) exprDesc);
+          }
 		} else if (exprDesc instanceof ExprNodeColumnListDesc) {
 			for (ExprNodeDesc child : exprDesc.getChildren()) {
 				getExprNodeColumnDesc(child, hashCodeToColumnDescMap);
@@ -619,7 +632,7 @@ public class ExprNodeDescUtils {
    * Build ExprNodeColumnDesc for the projections in the input operator from
    * sartpos to endpos(both included). Operator must have an associated
    * colExprMap.
-   * 
+   *
    * @param inputOp
    *          Input Hive Operator
    * @param startPos
@@ -651,7 +664,7 @@ public class ExprNodeDescUtils {
     }
 
     return exprColLst;
-  }  
+  }
 
   public static List<ExprNodeDesc> flattenExprList(List<ExprNodeDesc> sourceList) {
     ArrayList<ExprNodeDesc> result = new ArrayList<ExprNodeDesc>(sourceList.size());
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDayOfWeek.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDayOfWeek.java
index defa9d1aab5..36af8c02f1d 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDayOfWeek.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFDayOfWeek.java
@@ -30,6 +30,7 @@ import org.apache.hadoop.hive.serde2.io.DateWritableV2;
 import org.apache.hadoop.hive.serde2.io.TimestampWritableV2;
 import org.apache.hadoop.io.IntWritable;
 import org.apache.hadoop.io.Text;
+import org.apache.hive.common.util.DateParser;
 
 /**
  * UDFDayOfWeek.
@@ -63,16 +64,14 @@ public class UDFDayOfWeek extends UDF {
    *         string.
    */
   public IntWritable evaluate(Text dateString) {
-    if (dateString == null) {
-      return null;
-    }
-    try {
-      Date date = Date.valueOf(dateString.toString());
-      result.set(date.getDayOfWeek());
-      return result;
-    } catch (IllegalArgumentException e) {
-      return null;
+    if (dateString != null) {
+      Date date = DateParser.parseDate(dateString.toString());
+      if (date != null) {
+        result.set(date.getDayOfWeek());
+        return result;
+      }
     }
+    return null;
   }
 
   public IntWritable evaluate(DateWritableV2 d) {
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToBoolean.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToBoolean.java
index a7f4bf1fcc3..7a94a0ac275 100755
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToBoolean.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToBoolean.java
@@ -20,6 +20,7 @@ package org.apache.hadoop.hive.ql.udf;
 
 
 import org.apache.hadoop.hive.common.type.HiveDecimal;
+import org.apache.hadoop.hive.common.type.TimestampTZ;
 import org.apache.hadoop.hive.ql.exec.UDF;
 import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
 import org.apache.hadoop.hive.ql.exec.vector.expressions.CastDecimalToBoolean;
@@ -188,7 +189,8 @@ public class UDFToBoolean extends UDF {
     if (i == null) {
       return null;
     } else {
-      booleanWritable.set(i.getSeconds() != 0 || i.getNanos() != 0);
+      TimestampTZ timestamp = UDFUtils.getTimestampTZFromTimestamp(i.getTimestamp());
+      booleanWritable.set((timestamp.getEpochSecond() != 0) || (timestamp.getNanos() != 0));
       return booleanWritable;
     }
   }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToByte.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToByte.java
index 8f4ec3b1ef4..a6472f1fdfe 100755
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToByte.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToByte.java
@@ -42,7 +42,7 @@ import org.apache.hadoop.io.Text;
  * UDFToByte.
  *
  */
-@VectorizedExpressions({CastTimestampToLong.class, CastDoubleToLong.class,
+@VectorizedExpressions({CastDoubleToLong.class,
     CastDecimalToLong.class, CastStringToLong.class})
 public class UDFToByte extends UDF {
   private final ByteWritable byteWritable = new ByteWritable();
@@ -187,7 +187,7 @@ public class UDFToByte extends UDF {
     if (i == null) {
       return null;
     } else {
-      final long longValue = i.getSeconds();
+      final long longValue = UDFUtils.getTimestampTZFromTimestamp(i.getTimestamp()).getEpochSecond();
       final byte byteValue = (byte) longValue;
       if (byteValue != longValue) {
         return null;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToDouble.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToDouble.java
index 7a0145243dd..dc383343c2b 100755
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToDouble.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToDouble.java
@@ -18,6 +18,7 @@
 
 package org.apache.hadoop.hive.ql.udf;
 
+import org.apache.hadoop.hive.common.type.TimestampTZUtil;
 import org.apache.hadoop.hive.ql.exec.UDF;
 import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
 import org.apache.hadoop.hive.ql.exec.vector.expressions.CastDecimalToDouble;
@@ -40,7 +41,7 @@ import org.apache.hadoop.io.Text;
  * UDFToDouble.
  *
  */
-@VectorizedExpressions({CastTimestampToDouble.class, CastLongToDouble.class,
+@VectorizedExpressions({CastLongToDouble.class,
     CastDecimalToDouble.class, CastStringToDouble.class})
 public class UDFToDouble extends UDF {
   private final DoubleWritable doubleWritable = new DoubleWritable();
@@ -185,7 +186,9 @@ public class UDFToDouble extends UDF {
       return null;
     } else {
       try {
-        doubleWritable.set(i.getDouble());
+        doubleWritable.set(TimestampTZUtil.convertTimestampTZToDouble(
+          UDFUtils.getTimestampTZFromTimestamp(i.getTimestamp())
+        ));
         return doubleWritable;
       } catch (NumberFormatException e) {
         // MySQL returns 0 if the string is not a well-formed numeric value.
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToFloat.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToFloat.java
index 451b45fbbc4..2f0a020ec6b 100755
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToFloat.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToFloat.java
@@ -18,6 +18,7 @@
 
 package org.apache.hadoop.hive.ql.udf;
 
+import org.apache.hadoop.hive.common.type.TimestampTZUtil;
 import org.apache.hadoop.hive.ql.exec.UDF;
 import org.apache.hadoop.hive.ql.exec.vector.VectorizedExpressions;
 import org.apache.hadoop.hive.ql.exec.vector.expressions.CastDecimalToFloat;
@@ -41,7 +42,7 @@ import org.apache.hadoop.io.Text;
  * UDFToFloat.
  *
  */
-@VectorizedExpressions({CastTimestampToDouble.class, CastLongToFloatViaLongToDouble.class,
+@VectorizedExpressions({CastLongToFloatViaLongToDouble.class,
     CastDecimalToFloat.class, CastStringToFloat.class})
 public class UDFToFloat extends UDF {
   private final FloatWritable floatWritable = new FloatWritable();
@@ -186,7 +187,9 @@ public class UDFToFloat extends UDF {
       return null;
     } else {
       try {
-        floatWritable.set((float) i.getDouble());
+        floatWritable.set((float) TimestampTZUtil.convertTimestampTZToDouble(
+          UDFUtils.getTimestampTZFromTimestamp(i.getTimestamp())
+        ));
         return floatWritable;
       } catch (NumberFormatException e) {
         // MySQL returns 0 if the string is not a well-formed numeric value.
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToInteger.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToInteger.java
index 4fe9c323cc3..1c67d8b2086 100755
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToInteger.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToInteger.java
@@ -44,7 +44,7 @@ import org.apache.hadoop.io.Text;
  * UDFToInteger.
  *
  */
-@VectorizedExpressions({CastTimestampToLong.class, CastDoubleToLong.class,
+@VectorizedExpressions({CastDoubleToLong.class,
     CastDecimalToLong.class, CastStringToLong.class})
 public class UDFToInteger extends UDF {
   private final IntWritable intWritable = new IntWritable();
@@ -197,7 +197,7 @@ public class UDFToInteger extends UDF {
     if (i == null) {
       return null;
     } else {
-      final long longValue = i.getSeconds();
+      final long longValue = UDFUtils.getTimestampTZFromTimestamp(i.getTimestamp()).getEpochSecond();
       final int intValue = (int) longValue;
       if (intValue != longValue) {
         return null;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToLong.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToLong.java
index b31eeb08a0c..01051e4f544 100755
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToLong.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToLong.java
@@ -42,7 +42,7 @@ import org.apache.hadoop.io.Text;
  * UDFToLong.
  *
  */
-@VectorizedExpressions({CastTimestampToLong.class, CastDoubleToLong.class,
+@VectorizedExpressions({CastDoubleToLong.class,
     CastDecimalToLong.class, CastStringToLong.class})
 public class UDFToLong extends UDF {
   private final LongWritable longWritable = new LongWritable();
@@ -199,7 +199,7 @@ public class UDFToLong extends UDF {
     if (i == null) {
       return null;
     } else {
-      longWritable.set(i.getSeconds());
+      longWritable.set(UDFUtils.getTimestampTZFromTimestamp(i.getTimestamp()).getEpochSecond());
       return longWritable;
     }
   }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToShort.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToShort.java
index 315789c1c1f..60f6e8d7de1 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToShort.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFToShort.java
@@ -42,7 +42,7 @@ import org.apache.hadoop.io.Text;
  * UDFToShort.
  *
  */
-@VectorizedExpressions({CastTimestampToLong.class, CastDoubleToLong.class,
+@VectorizedExpressions({CastDoubleToLong.class,
     CastDecimalToLong.class, CastStringToLong.class})
 public class UDFToShort extends UDF {
   ShortWritable shortWritable = new ShortWritable();
@@ -188,7 +188,7 @@ public class UDFToShort extends UDF {
     if (i == null) {
       return null;
     } else {
-      final long longValue = i.getSeconds();
+      final long longValue = UDFUtils.getTimestampTZFromTimestamp(i.getTimestamp()).getEpochSecond();
       final short shortValue = (short) longValue;
       if (shortValue != longValue) {
         return null;
diff --git a/common/src/java/org/apache/hive/common/util/DateParser.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFUtils.java
similarity index 55%
copy from common/src/java/org/apache/hive/common/util/DateParser.java
copy to ql/src/java/org/apache/hadoop/hive/ql/udf/UDFUtils.java
index 5db14f19069..89e571aaacb 100644
--- a/common/src/java/org/apache/hive/common/util/DateParser.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFUtils.java
@@ -15,37 +15,22 @@
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
-package org.apache.hive.common.util;
 
-import org.apache.hadoop.hive.common.type.Date;
+package org.apache.hadoop.hive.ql.udf;
 
-/**
- * Date parser class for Hive.
- */
-public class DateParser {
+import org.apache.hadoop.hive.common.type.Timestamp;
+import org.apache.hadoop.hive.common.type.TimestampTZ;
+import org.apache.hadoop.hive.common.type.TimestampTZUtil;
+import org.apache.hadoop.hive.conf.HiveConf;
+import org.apache.hadoop.hive.ql.session.SessionState;
 
-  public DateParser() {
- }
+import java.time.ZoneId;
 
-  public Date parseDate(String strValue) {
-    Date result = new Date();
-    if (parseDate(strValue, result)) {
-      return result;
-    }
-    return null;
-  }
+public class UDFUtils {
 
-  public boolean parseDate(String strValue, Date result) {
-    Date parsedVal;
-    try {
-      parsedVal = Date.valueOf(strValue);
-    } catch (IllegalArgumentException e) {
-      parsedVal = null;
-    }
-    if (parsedVal == null) {
-      return false;
-    }
-    result.setTimeInMillis(parsedVal.toEpochMilli());
-    return true;
+  public static TimestampTZ getTimestampTZFromTimestamp(Timestamp timestamp) {
+    ZoneId zone = ((SessionState.get() == null) ?
+      new HiveConf().getLocalTimeZone() : SessionState.get().getConf().getLocalTimeZone());
+    return TimestampTZUtil.convert(timestamp, zone);
   }
 }
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFWeekOfYear.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFWeekOfYear.java
index 6d64da115c2..3a138d6222f 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFWeekOfYear.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/UDFWeekOfYear.java
@@ -34,6 +34,7 @@ import org.apache.hadoop.hive.serde2.io.DateWritableV2;
 import org.apache.hadoop.hive.serde2.io.TimestampWritableV2;
 import org.apache.hadoop.io.IntWritable;
 import org.apache.hadoop.io.Text;
+import org.apache.hive.common.util.DateParser;
 
 /**
  * UDFWeekOfYear.
@@ -70,17 +71,15 @@ public class UDFWeekOfYear extends UDF {
    *         string.
    */
   public IntWritable evaluate(Text dateString) {
-    if (dateString == null) {
-      return null;
-    }
-    try {
-      Date date = Date.valueOf(dateString.toString());
-      calendar.setTimeInMillis(date.toEpochMilli());
-      result.set(calendar.get(Calendar.WEEK_OF_YEAR));
-      return result;
-    } catch (IllegalArgumentException e) {
-      return null;
+    if (dateString != null) {
+      Date date = DateParser.parseDate(dateString.toString());
+      if (date != null) {
+        calendar.setTimeInMillis(date.toEpochMilli());
+        result.set(calendar.get(Calendar.WEEK_OF_YEAR));
+        return result;
+      }
     }
+    return null;
   }
 
   public IntWritable evaluate(DateWritableV2 d) {
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDF.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDF.java
index 0d8d659ff6c..9256fc2be02 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDF.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDF.java
@@ -54,6 +54,7 @@ import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectIn
 import org.apache.hadoop.io.BooleanWritable;
 import org.apache.hadoop.io.IntWritable;
 import org.apache.hadoop.io.LongWritable;
+import org.apache.hive.common.util.DateParser;
 
 /**
  * A Generic User-defined function (GenericUDF) for the use with Hive.
@@ -502,11 +503,7 @@ public abstract class GenericUDF implements Closeable {
     case VARCHAR:
     case CHAR:
       String dateStr = converters[i].convert(obj).toString();
-      try {
-        date = Date.valueOf(dateStr);
-      } catch (IllegalArgumentException e) {
-        date = null;
-      }
+      date = DateParser.parseDate(dateStr);
       break;
     case TIMESTAMP:
     case DATE:
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDate.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDate.java
index f5c4eb556df..fe507f60675 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDate.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDate.java
@@ -55,7 +55,6 @@ public class GenericUDFDate extends GenericUDF {
   private transient Converter dateWritableConverter;
   private transient PrimitiveCategory inputType;
   private transient PrimitiveObjectInspector argumentOI;
-  private transient DateParser dateParser = new DateParser();
   private transient final DateWritableV2 output = new DateWritableV2();
   private transient final Date date = new Date();
 
@@ -109,7 +108,7 @@ public class GenericUDFDate extends GenericUDF {
       throw new UDFArgumentException("TO_DATE() received non-null object of VOID type");
     case STRING:
       String dateString = textConverter.convert(arguments[0].get()).toString();
-      if (dateParser.parseDate(dateString, date)) {
+      if (DateParser.parseDate(dateString, date)) {
         output.set(date);
       } else {
         return null;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java
index be7bd1704f8..7b9ef6425c7 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateAdd.java
@@ -63,7 +63,6 @@ import org.apache.hive.common.util.DateParser;
         + "  '2009-07-31'")
 @VectorizedExpressions({VectorUDFDateAddColScalar.class, VectorUDFDateAddScalarCol.class, VectorUDFDateAddColCol.class})
 public class GenericUDFDateAdd extends GenericUDF {
-  private transient final DateParser dateParser = new DateParser();
   private transient final Date dateVal = new Date();
   private transient Converter dateConverter;
   private transient Converter daysConverter;
@@ -166,7 +165,7 @@ public class GenericUDFDateAdd extends GenericUDF {
     switch (inputType1) {
     case STRING:
       String dateString = dateConverter.convert(arguments[0].get()).toString();
-      if (dateParser.parseDate(dateString, dateVal)) {
+      if (DateParser.parseDate(dateString, dateVal)) {
         output.set(dateVal);
       } else {
         return null;
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java
index 00386c8f6c5..e17efe5bb37 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFDateDiff.java
@@ -42,6 +42,7 @@ import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectIn
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorConverter.TimestampConverter;
 import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectInspectorUtils;
 import org.apache.hadoop.io.IntWritable;
+import org.apache.hive.common.util.DateParser;
 
 import javax.annotation.Nullable;
 
@@ -110,17 +111,18 @@ public class GenericUDFDateDiff extends GenericUDF {
     switch (inputType) {
     case STRING:
     case VARCHAR:
-    case CHAR:
+    case CHAR: {
       String dateString = converter.convert(argument.get()).toString();
-      try {
-        return Date.valueOf(dateString);
-      } catch (IllegalArgumentException e) {
-        Timestamp ts = PrimitiveObjectInspectorUtils.getTimestampFromString(dateString);
-        if (ts != null) {
-          return Date.ofEpochMilli(ts.toEpochMilli());
-        }
-        return null;
+      Date date = DateParser.parseDate(dateString);
+      if (date != null) {
+        return date;
+      }
+      Timestamp ts = PrimitiveObjectInspectorUtils.getTimestampFromString(dateString);
+      if (ts != null) {
+        return Date.ofEpochMilli(ts.toEpochMilli());
       }
+      return null;
+    }
     case TIMESTAMP:
       Timestamp ts = ((TimestampWritableV2) converter.convert(argument.get()))
         .getTimestamp();
diff --git a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFTrunc.java b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFTrunc.java
index 7a7d13ef41f..6bf1ed814c4 100644
--- a/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFTrunc.java
+++ b/ql/src/java/org/apache/hadoop/hive/ql/udf/generic/GenericUDFTrunc.java
@@ -50,6 +50,7 @@ import org.apache.hadoop.io.FloatWritable;
 import org.apache.hadoop.io.IntWritable;
 import org.apache.hadoop.io.LongWritable;
 import org.apache.hadoop.io.Text;
+import org.apache.hive.common.util.DateParser;
 
 /**
  * GenericUDFTrunc.
@@ -297,9 +298,8 @@ public class GenericUDFTrunc extends GenericUDF {
     switch (inputType1) {
     case STRING:
       String dateString = textConverter1.convert(arguments[0].get()).toString();
-      try {
-        d = Date.valueOf(dateString.toString());
-      } catch (IllegalArgumentException e) {
+      d = DateParser.parseDate(dateString);
+      if (d == null) {
         return null;
       }
       break;
diff --git a/ql/src/test/org/apache/hadoop/hive/ql/exec/TestExplainTask.java b/ql/src/test/org/apache/hadoop/hive/ql/exec/TestExplainTask.java
index 244bc739438..a0c0413431a 100644
--- a/ql/src/test/org/apache/hadoop/hive/ql/exec/TestExplainTask.java
+++ b/ql/src/test/org/apache/hadoop/hive/ql/exec/TestExplainTask.java
@@ -218,8 +218,8 @@ public class TestExplainTask {
 
 
     JsonNode result = objectMapper.readTree(uut.getJSONPlan(null, tasks, null, true,
-            false, false, null).toString());
-    JsonNode expected = objectMapper.readTree("{\"STAGE DEPENDENCIES\":{\"mockTaskId\":" +
+            false, false, "Plan Optimized by CBO", null).toString());
+    JsonNode expected = objectMapper.readTree("{\"cboInfo\":\"Plan Optimized by CBO\", \"STAGE DEPENDENCIES\":{\"mockTaskId\":" +
             "{\"ROOT STAGE\":\"TRUE\",\"BACKUP STAGE\":\"backup-id-mock\"}},\"STAGE PLANS\":" +
             "{\"mockTaskId\":{}}}");
 
diff --git a/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorCastStatement.java b/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorCastStatement.java
index 9e61fcda853..aa186714715 100644
--- a/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorCastStatement.java
+++ b/ql/src/test/org/apache/hadoop/hive/ql/exec/vector/expressions/TestVectorCastStatement.java
@@ -70,6 +70,7 @@ import junit.framework.Assert;
 import org.junit.Ignore;
 import org.junit.Test;
 
+@org.junit.Ignore("HIVE-25323")
 public class TestVectorCastStatement {
 
   @Test
diff --git a/ql/src/test/org/apache/hadoop/hive/ql/parse/TestUpdateDeleteSemanticAnalyzer.java b/ql/src/test/org/apache/hadoop/hive/ql/parse/TestUpdateDeleteSemanticAnalyzer.java
index ec77b7376c6..fc192c29434 100644
--- a/ql/src/test/org/apache/hadoop/hive/ql/parse/TestUpdateDeleteSemanticAnalyzer.java
+++ b/ql/src/test/org/apache/hadoop/hive/ql/parse/TestUpdateDeleteSemanticAnalyzer.java
@@ -300,7 +300,7 @@ public class TestUpdateDeleteSemanticAnalyzer {
     ExplainConfiguration config = new ExplainConfiguration();
     config.setExtended(true);
     ExplainWork work = new ExplainWork(tmp, sem.getParseContext(), sem.getRootTasks(),
-        sem.getFetchTask(), sem, config, null, plan.getOptimizedQueryString());
+        sem.getFetchTask(), sem, config, null, plan.getOptimizedQueryString(), null);
     ExplainTask task = new ExplainTask();
     task.setWork(work);
     task.initialize(queryState, plan, null, null);
diff --git a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDateDiff.java b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDateDiff.java
index ea183d460b8..1465335b43f 100644
--- a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDateDiff.java
+++ b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFDateDiff.java
@@ -18,6 +18,10 @@
 
 package org.apache.hadoop.hive.ql.udf.generic;
 
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+
+import java.text.ParseException;
 import java.time.LocalDateTime;
 
 import org.apache.hadoop.hive.common.type.Date;
@@ -32,9 +36,29 @@ import org.apache.hadoop.hive.serde2.objectinspector.primitive.PrimitiveObjectIn
 import org.apache.hadoop.io.IntWritable;
 import org.apache.hadoop.io.Text;
 
-import junit.framework.TestCase;
 
-public class TestGenericUDFDateDiff extends TestCase {
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNull;
+import org.junit.Test;
+
+public class TestGenericUDFDateDiff {
+
+  @Test
+  public void testStringToDateISOFormat() throws HiveException, ParseException {
+    GenericUDFDateDiff udf = new GenericUDFDateDiff();
+    ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
+    ObjectInspector valueOI2 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
+    ObjectInspector[] arguments = {valueOI1, valueOI2};
+
+    udf.initialize(arguments);
+    DeferredObject valueObj1 = new DeferredJavaObject(new Text("2019-09-09T10:45:49+02:00"));
+    DeferredObject valueObj2 = new DeferredJavaObject(new Text("2019-11-07 23:20:39.503"));
+    DeferredObject[] args = {valueObj1, valueObj2};
+    IntWritable output = udf.evaluate(args);
+    assertEquals("date_iff() test for STRING failed ", "-59", output.toString());
+  }
+
+  @Test
   public void testStringToDate() throws HiveException {
     GenericUDFDateDiff udf = new GenericUDFDateDiff();
     ObjectInspector valueOI1 = PrimitiveObjectInspectorFactory.javaStringObjectInspector;
@@ -114,3 +138,4 @@ public class TestGenericUDFDateDiff extends TestCase {
   }
 
 }
+
diff --git a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFLastDay.java b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFLastDay.java
index b02ed9ec53f..0dc4e72dad2 100644
--- a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFLastDay.java
+++ b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFLastDay.java
@@ -86,7 +86,7 @@ public class TestGenericUDFLastDay extends TestCase {
     runAndVerify("2016-02-30 10:30:45", null, udf);
     runAndVerify("2014-01-32 10:30:45", null, udf);
     runAndVerify("01/14/2014 10:30:45", null, udf);
-    runAndVerify("2016-02-28T10:30:45", null, udf);
+    runAndVerify("2016-02-28T10:30:45", "2016-02-29", udf);
   }
 
   public void testLastDayTs() throws HiveException {
diff --git a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFNextDay.java b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFNextDay.java
index efbfab2eba0..93454f6a888 100644
--- a/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFNextDay.java
+++ b/ql/src/test/org/apache/hadoop/hive/ql/udf/generic/TestGenericUDFNextDay.java
@@ -85,7 +85,7 @@ public class TestGenericUDFNextDay extends TestCase {
     runAndVerify("2015-02-30 10:30:00", "WE", null, udf);
     runAndVerify("2015-02-32 10:30:00", "WE", null, udf);
     runAndVerify("2015/01/14 14:04:34", "SAT", null, udf);
-    runAndVerify("2015-01-14T14:04:34", "SAT", null, udf);
+    runAndVerify("2015-01-14T14:04:34", "SAT", "2015-01-17", udf);
   }
 
   public void testNextDayErrorArg1() throws HiveException {
diff --git a/ql/src/test/queries/clientpositive/collision.q b/ql/src/test/queries/clientpositive/collision.q
new file mode 100644
index 00000000000..8725fef1a7c
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/collision.q
@@ -0,0 +1,337 @@
+create database cpn;
+create database cpn_view;
+
+drop table if exists cpn.mytable_1000_n;
+CREATE external TABLE IF NOT EXISTS cpn.mytable_1000_n (
+col0 decimal(18,0),
+col1 decimal(18,0),
+col2 char(1),
+col4 decimal(18,0),
+col5 char(1),
+col6 char(1),
+col7 decimal(18,0),
+col8 decimal(18,5),
+col9 decimal(28,0),
+col10 int,
+col13 decimal(18,0),
+col15 string,
+col16 timestamp,
+col17 string,
+col18 string,
+col19 char(1),
+col21 char(10),
+col20 char(1),
+col22 char(3),
+col23 timestamp,
+col11 char(3),
+col24 decimal(18,9),
+col25 decimal(18,5),
+col26 char(1),
+col27 decimal(18,5),
+col28 char(5),
+col75 string,
+col29 char(1),
+col30 decimal(18,5),
+col31 decimal(18,5),
+col72 int,
+col32 decimal(18,0),
+col33 char(1),
+col34 char(5),
+col35 string,
+col36 string,
+col37 decimal(18,0),
+col38 tinyint,
+col73 string,
+col39 char(1),
+parent_col9 decimal(28,0),
+col40 tinyint,
+col41 tinyint,
+col42 decimal(18,0),
+col89 string,
+col90 char(1),
+col91 decimal(18,0),
+col83 string,
+col81 decimal(18,0),
+col92 decimal(18,0),
+col93 decimal(18,0),
+col94 string,
+col80 int,
+col95 decimal(18,5),
+col96 string,
+col97 char(1),
+col78 decimal(18,0),
+col98 tinyint,
+col87 string,
+col99 decimal(18,5),
+col100 string,
+col101 char(1),
+col14 decimal(18,0),
+col102 tinyint,
+col85 string,
+col103 decimal(18,5),
+col104 string,
+col105 char(1),
+col67 decimal(18,0),
+col79 tinyint,
+col106 string,
+col107 timestamp,
+col43 char(10),
+col82 decimal(18,0),
+col84 decimal(18,0),
+col108 decimal(18,0),
+col109 decimal(18,0),
+col110 date,
+col111 date,
+col86 int,
+col88 int,
+col112 tinyint,
+col113 tinyint,
+col44 string,
+col45 string,
+col46 char(1),
+col47 string,
+col48 decimal(18,0),
+col49 decimal(18,0),
+col50 tinyint,
+col51 string,
+col12 char(3),
+col52 decimal(18,9),
+col53 decimal(18,5),
+col54 decimal(18,5),
+col55 char(1),
+col56 tinyint,
+col57 string,
+col58 int,
+col59 char(5),
+col60 decimal(18,0),
+col76 decimal(28,0),
+col61 string,
+col62 string,
+col74 decimal(18,5),
+col65 int,
+col63 smallint,
+col64 char(1),
+col66 string,
+col68 int,
+col69 string,
+col114 date,
+col115 decimal(18,0),
+col70 string,
+col71 string,
+col77 int,
+col116 timestamp,
+col117 timestamp)
+partitioned by (col3 date)
+stored as orc;
+
+EXPLAIN CBO
+CREATE OR REPLACE VIEW cpn_view.myview_1000_n AS
+SELECT
+col0
+,col1
+,col2
+,col3
+,col4
+,col5
+,col6
+,col7
+,COALESCE(col8,0.0) AS col8
+,COALESCE(col9,0) as col9
+,col10
+,CASE WHEN COALESCE(col11,'$')='X' THEN 'Y' ELSE col11 END as Alt_col11
+,CASE WHEN COALESCE(col12,'$')='X' THEN 'Y' ELSE col12 END as Alt_col12
+,col13
+,(Case WHEN trim(col5) IN ('A','B','C') THEN COALESCE(col14,-1) ELSE COALESCE(col14,1) END) AS col14
+,col15
+,col16
+,col17
+,col18
+,col19
+,col20
+,col21
+,col22
+,col23
+,COALESCE(trim(col11),'Z') AS col11
+,col24
+,col25
+,col26
+,col27
+,col28
+,col29
+,col30
+,col31
+,col32
+,col33
+,col34
+,col35
+,col36
+,col37
+,col38
+,COALESCE(trim(col39),'N') AS col39
+,COALESCE(Parent_col9,-999) AS Parent_col9
+,COALESCE(col40,-1) AS col40
+,col41
+,col42
+,CASE WHEN trim(col43)='' THEN 'U' ELSE COALESCE(trim(col43),'U') END AS col43
+,col44
+,col45
+,col46
+,col47
+,col48
+,col49
+,col50
+,col51
+,col12
+,col52
+,col53
+,col54
+,col55
+,col56
+,col57
+,COALESCE(col58,-1) AS col58
+,col59
+,col60
+,col61
+,col62
+,(CASE WHEN trim(cast(col63 as string))='' THEN '-1' ELSE COALESCE(trim(cast(col63 as string)),'-1')END) AS col63
+,col64
+,col65
+,col66
+,COALESCE(col67,-999) as col67
+,col68
+,col69
+,col70
+,col71
+,col72
+,col73
+,col74
+,col75
+,col76
+,col77
+,col116
+,col117
+,COALESCE(col78,-1) AS col78
+,col79 as col79
+,col114
+,col115 
+,COALESCE(col92,'-99') AS col92
+,COALESCE(col80,-1) AS col80
+,col81
+,col93
+,col82 as col82
+,col83
+,col89 AS col118
+,COALESCE(col84,'-99') AS col84
+,col85
+,col86
+,col87
+,COALESCE(col88,'-99') AS col88
+FROM cpn.mytable_1000_n;
+
+CREATE OR REPLACE VIEW cpn_view.myview_1000_n AS
+SELECT
+col0
+,col1
+,col2
+,col3
+,col4
+,col5
+,col6
+,col7
+,COALESCE(col8,0.0) AS col8
+,COALESCE(col9,0) as col9
+,col10
+,CASE WHEN COALESCE(col11,'$')='X' THEN 'Y' ELSE col11 END as Alt_col11
+,CASE WHEN COALESCE(col12,'$')='X' THEN 'Y' ELSE col12 END as Alt_col12
+,col13
+,(Case WHEN trim(col5) IN ('A','B','C') THEN COALESCE(col14,-1) ELSE COALESCE(col14,1) END) AS col14
+,col15
+,col16
+,col17
+,col18
+,col19
+,col20
+,col21
+,col22
+,col23
+,COALESCE(trim(col11),'Z') AS col11
+,col24
+,col25
+,col26
+,col27
+,col28
+,col29
+,col30
+,col31
+,col32
+,col33
+,col34
+,col35
+,col36
+,col37
+,col38
+,COALESCE(trim(col39),'N') AS col39
+,COALESCE(Parent_col9,-999) AS Parent_col9
+,COALESCE(col40,-1) AS col40
+,col41
+,col42
+,CASE WHEN trim(col43)='' THEN 'U' ELSE COALESCE(trim(col43),'U') END AS col43
+,col44
+,col45
+,col46
+,col47
+,col48
+,col49
+,col50
+,col51
+,col12
+,col52
+,col53
+,col54
+,col55
+,col56
+,col57
+,COALESCE(col58,-1) AS col58
+,col59
+,col60
+,col61
+,col62
+,(CASE WHEN trim(cast(col63 as string))='' THEN '-1' ELSE COALESCE(trim(cast(col63 as string)),'-1')END) AS col63
+,col64
+,col65
+,col66
+,COALESCE(col67,-999) as col67
+,col68
+,col69
+,col70
+,col71
+,col72
+,col73
+,col74
+,col75
+,col76
+,col77
+,col116
+,col117
+,COALESCE(col78,-1) AS col78
+,col79 as col79
+,col114
+,col115 
+,COALESCE(col92,'-99') AS col92
+,COALESCE(col80,-1) AS col80
+,col81
+,col93
+,col82 as col82
+,col83
+,col89 AS col118
+,COALESCE(col84,'-99') AS col84
+,col85
+,col86
+,col87
+,COALESCE(col88,'-99') AS col88
+FROM cpn.mytable_1000_n;
+
+select * from cpn_view.myview_1000_n;
+
+drop view cpn_view.myview_1000_n;
+drop table cpn.mytable_1000_n;
diff --git a/ql/src/test/queries/clientpositive/constant_prop_when.q b/ql/src/test/queries/clientpositive/constant_prop_when.q
new file mode 100644
index 00000000000..fc1ab2bf568
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/constant_prop_when.q
@@ -0,0 +1,20 @@
+set hive.fetch.task.conversion=none;
+set hive.optimize.ppd=false;
+
+create table t (s1 string,s2 string);
+
+insert into t values (null,null);
+
+explain
+select
+	coalesce(s1, 'null_value' ), coalesce(s2, 'null_value' ), 
+	coalesce(s1, 'null_value' )=coalesce(s2, 'null_value' ),
+	case when coalesce(s1, 'null_value' )=coalesce(s2, 'null_value' ) then 'eq' else 'noteq' end
+from t;
+
+select
+	coalesce(s1, 'null_value' ), coalesce(s2, 'null_value' ), 
+	coalesce(s1, 'null_value' )=coalesce(s2, 'null_value' ),
+	case when coalesce(s1, 'null_value' )=coalesce(s2, 'null_value' ) then 'eq' else 'noteq' end
+from t;
+
diff --git a/ql/src/test/queries/clientpositive/date_udf.q b/ql/src/test/queries/clientpositive/date_udf.q
index caf120e1afe..4f8c606ffad 100644
--- a/ql/src/test/queries/clientpositive/date_udf.q
+++ b/ql/src/test/queries/clientpositive/date_udf.q
@@ -74,7 +74,13 @@ select
 select min(fl_date) from date_udf_flight;
 select max(fl_date) from date_udf_flight;
 
+create external table testdatediff(datetimecol string) stored as orc;
+insert into testdatediff values ('2019-09-09T10:45:49+02:00'),('2019-07-24');
 
+select datetimecol from testdatediff where datediff(cast(current_timestamp as string), datetimecol)>183;
+select cast(datetimecol as date), datetimecol from testdatediff;
+
+drop table testdatediff;
 drop table date_udf;
 drop table date_udf_string;
 drop table date_udf_flight;
diff --git a/ql/src/test/queries/clientpositive/join_constraints_optimization.q b/ql/src/test/queries/clientpositive/join_constraints_optimization.q
new file mode 100644
index 00000000000..5981b04a883
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/join_constraints_optimization.q
@@ -0,0 +1,136 @@
+set hive.strict.checks.cartesian.product=false;
+
+CREATE TABLE `customer_removal_n0`(
+  `c_custkey` bigint, 
+  `c_name` string, 
+  `c_address` string, 
+  `c_city` string, 
+  `c_nation` string, 
+  `c_region` string, 
+  `c_phone` string, 
+  `c_mktsegment` string,
+  primary key (`c_custkey`) disable rely)
+ROW FORMAT DELIMITED
+FIELDS TERMINATED BY '|'
+STORED AS TEXTFILE;
+
+CREATE TABLE `dates_removal_n0`(
+  `d_datekey` bigint, 
+  `d_date` string, 
+  `d_dayofweek` string, 
+  `d_month` string, 
+  `d_year` int, 
+  `d_yearmonthnum` int, 
+  `d_yearmonth` string, 
+  `d_daynuminweek` int,
+  `d_daynuminmonth` int,
+  `d_daynuminyear` int,
+  `d_monthnuminyear` int,
+  `d_weeknuminyear` int,
+  `d_sellingseason` string,
+  `d_lastdayinweekfl` int,
+  `d_lastdayinmonthfl` int,
+  `d_holidayfl` int ,
+  `d_weekdayfl`int,
+  primary key (`d_datekey`) disable rely)
+ROW FORMAT DELIMITED
+FIELDS TERMINATED BY '|'
+STORED AS TEXTFILE;
+
+CREATE TABLE `lineorder_removal_n0`(
+  `lo_orderkey` bigint, 
+  `lo_linenumber` int, 
+  `lo_custkey` bigint not null disable rely,
+  `lo_partkey` bigint not null disable rely,
+  `lo_suppkey` bigint not null disable rely,
+  `lo_orderdate` bigint,
+  `lo_ordpriority` string, 
+  `lo_shippriority` string, 
+  `lo_quantity` double, 
+  `lo_extendedprice` double, 
+  `lo_ordtotalprice` double, 
+  `lo_discount` double, 
+  `lo_revenue` double, 
+  `lo_supplycost` double, 
+  `lo_tax` double, 
+  `lo_commitdate` bigint, 
+  `lo_shipmode` string,
+  primary key (`lo_orderkey`) disable rely,
+  constraint fk1 foreign key (`lo_custkey`) references `customer_removal_n0`(`c_custkey`) disable rely,
+  constraint fk2 foreign key (`lo_orderdate`) references `dates_removal_n0`(`d_datekey`) disable rely)
+ROW FORMAT DELIMITED
+FIELDS TERMINATED BY '|'
+STORED AS TEXTFILE;
+
+-- CAN BE REMOVED AND DOES NOT NEED FILTER ON JOIN COLUMN
+-- AS COLUMN IS ALREADY NOT NULLABLE
+EXPLAIN
+SELECT `lo_linenumber`
+FROM `lineorder_removal_n0`
+JOIN `customer_removal_n0` ON `lo_custkey` = `c_custkey`;
+
+-- CAN BE REMOVED AND INTRODUCES A FILTER ON JOIN COLUMN
+EXPLAIN
+SELECT `lo_linenumber`
+FROM `lineorder_removal_n0`
+JOIN `dates_removal_n0` ON `lo_orderdate` = `d_datekey`;
+
+-- REMOVES THE JOIN
+EXPLAIN
+SELECT `lo_linenumber`
+FROM `lineorder_removal_n0`
+LEFT OUTER JOIN `customer_removal_n0` ON `lo_custkey` = `c_custkey`;
+
+-- TRANSFORMS THE JOIN
+EXPLAIN
+SELECT `lo_linenumber`, `c_region`
+FROM `lineorder_removal_n0`
+LEFT OUTER JOIN `customer_removal_n0` ON `lo_custkey` = `c_custkey`;
+
+-- NOT TRANSFORMED INTO INNER JOIN SINCE JOIN COLUMN IS NULLABLE
+EXPLAIN
+SELECT `lo_linenumber`
+FROM `lineorder_removal_n0`
+LEFT OUTER JOIN `dates_removal_n0` ON `lo_orderdate` = `d_datekey`;
+
+-- REMOVES BOTH JOINS
+EXPLAIN
+SELECT `lo_linenumber`
+FROM `lineorder_removal_n0`
+JOIN `customer_removal_n0` ON `lo_custkey` = `c_custkey`
+JOIN `dates_removal_n0` ON `lo_orderdate` = `d_datekey`;
+
+-- REMOVES BOTH JOINS
+EXPLAIN
+SELECT `lo_linenumber`
+FROM `lineorder_removal_n0`
+LEFT OUTER JOIN `customer_removal_n0` ON `lo_custkey` = `c_custkey`
+JOIN `dates_removal_n0` ON `lo_orderdate` = `d_datekey`;
+
+-- REMOVE INNER AND NOT TRANFORM OUTER
+EXPLAIN
+SELECT `lo_linenumber` FROM
+(SELECT *
+FROM `lineorder_removal_n0`
+JOIN `customer_removal_n0` ON `lo_custkey` = `c_custkey`) subq
+LEFT OUTER JOIN `dates_removal_n0` ON `lo_orderdate` = `d_datekey`;
+
+-- REMOVE FIRST OUTER AND NOT TRANFORM SECOND OUTER
+EXPLAIN
+SELECT `lo_linenumber`
+FROM `lineorder_removal_n0`
+LEFT OUTER JOIN `customer_removal_n0` ON `lo_custkey` = `c_custkey`
+LEFT OUTER JOIN `dates_removal_n0` ON `lo_orderdate` = `d_datekey`;
+
+-- NOT TRANFORM EITHER
+EXPLAIN
+SELECT `lo_linenumber`
+FROM `lineorder_removal_n0`
+LEFT OUTER JOIN `dates_removal_n0` ON `lo_orderdate` = `d_datekey`
+LEFT OUTER JOIN `customer_removal_n0` ON `lo_custkey` = `c_custkey`;
+
+-- SWAP AND REMOVE
+EXPLAIN
+SELECT `lo_linenumber`, `c_custkey`
+FROM `lineorder_removal_n0`
+JOIN `customer_removal_n0` ON `lo_custkey` = `c_custkey`;
diff --git a/ql/src/test/queries/clientpositive/materialized_view_rewrite_1.q b/ql/src/test/queries/clientpositive/materialized_view_rewrite_1.q
index ee4844277e5..18b9f7d418e 100644
--- a/ql/src/test/queries/clientpositive/materialized_view_rewrite_1.q
+++ b/ql/src/test/queries/clientpositive/materialized_view_rewrite_1.q
@@ -14,7 +14,7 @@ create table emps_n3 (
   commission int)
 stored as orc TBLPROPERTIES ('transactional'='true');
 insert into emps_n3 values (100, 10, 'Bill', 10000, 1000), (200, 20, 'Eric', 8000, 500),
-  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (110, 10, 'Bill', 10000, 250);
+  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (120, 10, 'Bill', 10000, 250);
 analyze table emps_n3 compute statistics for columns;
 
 create table depts_n2 (
@@ -29,14 +29,14 @@ create table dependents_n2 (
   empid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into dependents_n2 values (10, 'Michael'), (10, 'Jane');
+insert into dependents_n2 values (10, 'Michael'), (20, 'Jane');
 analyze table dependents_n2 compute statistics for columns;
 
 create table locations_n2 (
   locationid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into locations_n2 values (10, 'San Francisco'), (10, 'San Diego');
+insert into locations_n2 values (10, 'San Francisco'), (20, 'San Diego');
 analyze table locations_n2 compute statistics for columns;
 
 alter table emps_n3 add constraint pk1 primary key (empid) disable novalidate rely;
diff --git a/ql/src/test/queries/clientpositive/materialized_view_rewrite_3.q b/ql/src/test/queries/clientpositive/materialized_view_rewrite_3.q
index 4aadd5fb0a4..0823f59394d 100644
--- a/ql/src/test/queries/clientpositive/materialized_view_rewrite_3.q
+++ b/ql/src/test/queries/clientpositive/materialized_view_rewrite_3.q
@@ -14,7 +14,7 @@ create table emps_n9 (
   commission int)
 stored as orc TBLPROPERTIES ('transactional'='true');
 insert into emps_n9 values (100, 10, 'Bill', 10000, 1000), (200, 20, 'Eric', 8000, 500),
-  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250);
+  (150, 10, 'Sebastian', 7000, null), (120, 10, 'Theodore', 10000, 250);
 analyze table emps_n9 compute statistics for columns;
 
 create table depts_n7 (
@@ -29,14 +29,14 @@ create table dependents_n5 (
   empid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into dependents_n5 values (10, 'Michael'), (10, 'Jane');
+insert into dependents_n5 values (10, 'Michael'), (20, 'Jane');
 analyze table dependents_n5 compute statistics for columns;
 
 create table locations_n5 (
   locationid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into locations_n5 values (10, 'San Francisco'), (10, 'San Diego');
+insert into locations_n5 values (10, 'San Francisco'), (20, 'San Diego');
 analyze table locations_n5 compute statistics for columns;
 
 alter table emps_n9 add constraint pk1 primary key (empid) disable novalidate rely;
diff --git a/ql/src/test/queries/clientpositive/materialized_view_rewrite_4.q b/ql/src/test/queries/clientpositive/materialized_view_rewrite_4.q
index dc20b68ba9a..6724cec7710 100644
--- a/ql/src/test/queries/clientpositive/materialized_view_rewrite_4.q
+++ b/ql/src/test/queries/clientpositive/materialized_view_rewrite_4.q
@@ -14,7 +14,7 @@ create table emps_n5 (
   commission int)
 stored as orc TBLPROPERTIES ('transactional'='true');
 insert into emps_n5 values (100, 10, 'Bill', 10000, 1000), (200, 20, 'Eric', 8000, 500),
-  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (110, 10, 'Bill', 10000, 250);
+  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (120, 10, 'Bill', 10000, 250);
 analyze table emps_n5 compute statistics for columns;
 
 create table depts_n4 (
@@ -29,14 +29,14 @@ create table dependents_n3 (
   empid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into dependents_n3 values (10, 'Michael'), (10, 'Jane');
+insert into dependents_n3 values (10, 'Michael'), (20, 'Jane');
 analyze table dependents_n3 compute statistics for columns;
 
 create table locations_n3 (
   locationid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into locations_n3 values (10, 'San Francisco'), (10, 'San Diego');
+insert into locations_n3 values (10, 'San Francisco'), (20, 'San Diego');
 analyze table locations_n3 compute statistics for columns;
 
 alter table emps_n5 add constraint pk1 primary key (empid) disable novalidate rely;
@@ -78,7 +78,8 @@ from emps_n5 group by name, salary;
 
 drop materialized view mv1_n3;
 
--- EXAMPLE 25
+-- EXAMPLE 25: REWRITING NOT TRIGGERED WHEN JOIN CONSTRAINTS
+-- OPTIMIZATION IS ENABLED
 create materialized view mv1_n3 as
 select empid, emps_n5.deptno, count(*) as c, sum(empid) as s
 from emps_n5 join depts_n4 using (deptno)
diff --git a/ql/src/test/queries/clientpositive/materialized_view_rewrite_5.q b/ql/src/test/queries/clientpositive/materialized_view_rewrite_5.q
index 0e4fdf49ac0..d87928c0736 100644
--- a/ql/src/test/queries/clientpositive/materialized_view_rewrite_5.q
+++ b/ql/src/test/queries/clientpositive/materialized_view_rewrite_5.q
@@ -14,7 +14,7 @@ create table emps_n2 (
   commission int)
 stored as orc TBLPROPERTIES ('transactional'='true');
 insert into emps_n2 values (100, 10, 'Bill', 10000, 1000), (200, 20, 'Eric', 8000, 500),
-  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (110, 10, 'Bill', 10000, 250);
+  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (120, 10, 'Bill', 10000, 250);
 analyze table emps_n2 compute statistics for columns;
 
 create table depts_n1 (
@@ -29,14 +29,14 @@ create table dependents_n1 (
   empid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into dependents_n1 values (10, 'Michael'), (10, 'Jane');
+insert into dependents_n1 values (10, 'Michael'), (20, 'Jane');
 analyze table dependents_n1 compute statistics for columns;
 
 create table locations_n1 (
   locationid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into locations_n1 values (10, 'San Francisco'), (10, 'San Diego');
+insert into locations_n1 values (10, 'San Francisco'), (20, 'San Diego');
 analyze table locations_n1 compute statistics for columns;
 
 alter table emps_n2 add constraint pk1 primary key (empid) disable novalidate rely;
diff --git a/ql/src/test/queries/clientpositive/materialized_view_rewrite_6.q b/ql/src/test/queries/clientpositive/materialized_view_rewrite_6.q
index 4f05f76330c..23fc3c14ce5 100644
--- a/ql/src/test/queries/clientpositive/materialized_view_rewrite_6.q
+++ b/ql/src/test/queries/clientpositive/materialized_view_rewrite_6.q
@@ -14,7 +14,7 @@ create table emps (
   commission int)
 stored as orc TBLPROPERTIES ('transactional'='true');
 insert into emps values (100, 10, 'Bill', 10000, 1000), (200, 20, 'Eric', 8000, 500),
-  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (110, 10, 'Bill', 10000, 250);
+  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (120, 10, 'Bill', 10000, 250);
 analyze table emps compute statistics for columns;
 
 create table depts (
@@ -29,14 +29,14 @@ create table dependents (
   empid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into dependents values (10, 'Michael'), (10, 'Jane');
+insert into dependents values (10, 'Michael'), (20, 'Jane');
 analyze table dependents compute statistics for columns;
 
 create table locations (
   locationid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into locations values (10, 'San Francisco'), (10, 'San Diego');
+insert into locations values (10, 'San Francisco'), (20, 'San Diego');
 analyze table locations compute statistics for columns;
 
 alter table emps add constraint pk1 primary key (empid) disable novalidate rely;
diff --git a/ql/src/test/queries/clientpositive/materialized_view_rewrite_7.q b/ql/src/test/queries/clientpositive/materialized_view_rewrite_7.q
index 59ed5757569..3d1cedc4f56 100644
--- a/ql/src/test/queries/clientpositive/materialized_view_rewrite_7.q
+++ b/ql/src/test/queries/clientpositive/materialized_view_rewrite_7.q
@@ -29,14 +29,14 @@ create table dependents_n4 (
   empid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into dependents_n4 values (10, 'Michael'), (10, 'Jane');
+insert into dependents_n4 values (10, 'Michael'), (20, 'Jane');
 analyze table dependents_n4 compute statistics for columns;
 
 create table locations_n4 (
   locationid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into locations_n4 values (10, 'San Francisco'), (10, 'San Diego');
+insert into locations_n4 values (10, 'San Francisco'), (20, 'San Diego');
 analyze table locations_n4 compute statistics for columns;
 
 alter table emps_n8 add constraint pk1 primary key (empid) disable novalidate rely;
diff --git a/ql/src/test/queries/clientpositive/materialized_view_rewrite_no_join_opt.q b/ql/src/test/queries/clientpositive/materialized_view_rewrite_no_join_opt.q
new file mode 100644
index 00000000000..8de9c7087a8
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/materialized_view_rewrite_no_join_opt.q
@@ -0,0 +1,132 @@
+-- SORT_QUERY_RESULTS
+
+set hive.support.concurrency=true;
+set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
+set hive.strict.checks.cartesian.product=false;
+set hive.stats.fetch.column.stats=true;
+set hive.optimize.constraints.join=false;
+
+create table emps_n30 (
+  empid int,
+  deptno int,
+  name varchar(256),
+  salary float,
+  commission int)
+stored as orc TBLPROPERTIES ('transactional'='true');
+insert into emps_n30 values (100, 10, 'Bill', 10000, 1000), (200, 20, 'Eric', 8000, 500),
+  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (120, 10, 'Bill', 10000, 250);
+analyze table emps_n30 compute statistics for columns;
+
+create table depts_n20 (
+  deptno int,
+  name varchar(256),
+  locationid int)
+stored as orc TBLPROPERTIES ('transactional'='true');
+insert into depts_n20 values (10, 'Sales', 10), (30, 'Marketing', null), (20, 'HR', 20);
+analyze table depts_n20 compute statistics for columns;
+
+create table dependents_n20 (
+  empid int,
+  name varchar(256))
+stored as orc TBLPROPERTIES ('transactional'='true');
+insert into dependents_n20 values (10, 'Michael'), (20, 'Jane');
+analyze table dependents_n20 compute statistics for columns;
+
+create table locations_n20 (
+  locationid int,
+  name varchar(256))
+stored as orc TBLPROPERTIES ('transactional'='true');
+insert into locations_n20 values (10, 'San Francisco'), (20, 'San Diego');
+analyze table locations_n20 compute statistics for columns;
+
+alter table emps_n30 add constraint pk1 primary key (empid) disable novalidate rely;
+alter table depts_n20 add constraint pk2 primary key (deptno) disable novalidate rely;
+alter table dependents_n20 add constraint pk3 primary key (empid) disable novalidate rely;
+alter table locations_n20 add constraint pk4 primary key (locationid) disable novalidate rely;
+
+alter table emps_n30 add constraint fk1 foreign key (deptno) references depts_n20(deptno) disable novalidate rely;
+alter table depts_n20 add constraint fk2 foreign key (locationid) references locations_n20(locationid) disable novalidate rely;
+
+-- EXAMPLE 1
+create materialized view mv1_n20 as
+select deptno, name, salary, commission
+from emps_n30;
+analyze table mv1_n20 compute statistics for columns;
+
+explain
+select emps_n30.name, emps_n30.salary, emps_n30.commission
+from emps_n30
+join depts_n20 using (deptno);
+
+select emps_n30.name, emps_n30.salary, emps_n30.commission
+from emps_n30
+join depts_n20 using (deptno);
+
+drop materialized view mv1_n20;
+
+-- EXAMPLE 2
+create materialized view mv1_n20 as
+select empid, emps_n30.deptno, count(*) as c, sum(empid) as s
+from emps_n30 join depts_n20 using (deptno)
+group by empid, emps_n30.deptno;
+analyze table mv1_n20 compute statistics for columns;
+
+explain
+select depts_n20.deptno, count(*) as c, sum(empid) as s
+from emps_n30 join depts_n20 using (deptno)
+group by depts_n20.deptno;
+
+select depts_n20.deptno, count(*) as c, sum(empid) as s
+from emps_n30 join depts_n20 using (deptno)
+group by depts_n20.deptno;
+
+drop materialized view mv1_n20;
+
+-- EXAMPLE 3
+create materialized view mv1_n20 as
+select dependents_n20.empid, emps_n30.deptno, sum(salary) as s
+from emps_n30
+join dependents_n20 on (emps_n30.empid = dependents_n20.empid)
+group by dependents_n20.empid, emps_n30.deptno;
+analyze table mv1_n20 compute statistics for columns;
+
+explain
+select dependents_n20.empid, sum(salary) as s
+from emps_n30
+join depts_n20 on (emps_n30.deptno = depts_n20.deptno)
+join dependents_n20 on (emps_n30.empid = dependents_n20.empid)
+group by dependents_n20.empid;
+ 
+select dependents_n20.empid, sum(salary) as s
+from emps_n30
+join depts_n20 on (emps_n30.deptno = depts_n20.deptno)
+join dependents_n20 on (emps_n30.empid = dependents_n20.empid)
+group by dependents_n20.empid;
+ 
+drop materialized view mv1_n20;
+
+-- EXAMPLE 4
+create materialized view mv1_n20 as
+select emps_n30.empid, emps_n30.deptno, emps_n30.name as name1, emps_n30.salary, emps_n30.commission, dependents_n20.name as name2
+from emps_n30 join dependents_n20 on (emps_n30.empid = dependents_n20.empid);
+analyze table mv1_n20 compute statistics for columns;
+
+explain
+select emps_n30.empid, dependents_n20.empid, emps_n30.deptno
+from emps_n30
+join dependents_n20 on (emps_n30.empid = dependents_n20.empid)
+join depts_n20 a on (emps_n30.deptno=a.deptno)
+where emps_n30.name = 'Bill';
+
+select emps_n30.empid, dependents_n20.empid, emps_n30.deptno
+from emps_n30
+join dependents_n20 on (emps_n30.empid = dependents_n20.empid)
+join depts_n20 a on (emps_n30.deptno=a.deptno)
+where emps_n30.name = 'Bill';
+
+drop materialized view mv1_n20;
+
+
+
+
+
diff --git a/ql/src/test/queries/clientpositive/materialized_view_rewrite_part_1.q b/ql/src/test/queries/clientpositive/materialized_view_rewrite_no_join_opt_2.q
similarity index 94%
copy from ql/src/test/queries/clientpositive/materialized_view_rewrite_part_1.q
copy to ql/src/test/queries/clientpositive/materialized_view_rewrite_no_join_opt_2.q
index 5a2e74c8a00..a1372301feb 100644
--- a/ql/src/test/queries/clientpositive/materialized_view_rewrite_part_1.q
+++ b/ql/src/test/queries/clientpositive/materialized_view_rewrite_no_join_opt_2.q
@@ -1,11 +1,10 @@
 -- SORT_QUERY_RESULTS
 
-set hive.vectorized.execution.enabled=false;
 set hive.support.concurrency=true;
 set hive.txn.manager=org.apache.hadoop.hive.ql.lockmgr.DbTxnManager;
 set hive.strict.checks.cartesian.product=false;
 set hive.stats.fetch.column.stats=true;
-set hive.materializedview.rewriting=true;
+set hive.optimize.constraints.join=false;
 
 create table emps_n30 (
   empid int,
@@ -15,7 +14,7 @@ create table emps_n30 (
   commission int)
 stored as orc TBLPROPERTIES ('transactional'='true');
 insert into emps_n30 values (100, 10, 'Bill', 10000, 1000), (200, 20, 'Eric', 8000, 500),
-  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (110, 10, 'Bill', 10000, 250);
+  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (120, 10, 'Bill', 10000, 250);
 analyze table emps_n30 compute statistics for columns;
 
 create table depts_n20 (
@@ -30,14 +29,14 @@ create table dependents_n20 (
   empid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into dependents_n20 values (10, 'Michael'), (10, 'Jane');
+insert into dependents_n20 values (10, 'Michael'), (20, 'Jane');
 analyze table dependents_n20 compute statistics for columns;
 
 create table locations_n20 (
   locationid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into locations_n20 values (10, 'San Francisco'), (10, 'San Diego');
+insert into locations_n20 values (10, 'San Francisco'), (20, 'San Diego');
 analyze table locations_n20 compute statistics for columns;
 
 alter table emps_n30 add constraint pk1 primary key (empid) disable novalidate rely;
diff --git a/ql/src/test/queries/clientpositive/materialized_view_rewrite_part_1.q b/ql/src/test/queries/clientpositive/materialized_view_rewrite_part_1.q
index 5a2e74c8a00..e6980c07f13 100644
--- a/ql/src/test/queries/clientpositive/materialized_view_rewrite_part_1.q
+++ b/ql/src/test/queries/clientpositive/materialized_view_rewrite_part_1.q
@@ -15,7 +15,7 @@ create table emps_n30 (
   commission int)
 stored as orc TBLPROPERTIES ('transactional'='true');
 insert into emps_n30 values (100, 10, 'Bill', 10000, 1000), (200, 20, 'Eric', 8000, 500),
-  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (110, 10, 'Bill', 10000, 250);
+  (150, 10, 'Sebastian', 7000, null), (110, 10, 'Theodore', 10000, 250), (120, 10, 'Bill', 10000, 250);
 analyze table emps_n30 compute statistics for columns;
 
 create table depts_n20 (
@@ -30,14 +30,14 @@ create table dependents_n20 (
   empid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into dependents_n20 values (10, 'Michael'), (10, 'Jane');
+insert into dependents_n20 values (10, 'Michael'), (20, 'Jane');
 analyze table dependents_n20 compute statistics for columns;
 
 create table locations_n20 (
   locationid int,
   name varchar(256))
 stored as orc TBLPROPERTIES ('transactional'='true');
-insert into locations_n20 values (10, 'San Francisco'), (10, 'San Diego');
+insert into locations_n20 values (10, 'San Francisco'), (20, 'San Diego');
 analyze table locations_n20 compute statistics for columns;
 
 alter table emps_n30 add constraint pk1 primary key (empid) disable novalidate rely;
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query1.q b/ql/src/test/queries/clientpositive/perf/cbo_query1.q
new file mode 100644
index 00000000000..7cb0cd20b76
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query1.q
@@ -0,0 +1,27 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query1.tpl and seed 2031708268
+explain cbo
+with customer_total_return as
+(select sr_customer_sk as ctr_customer_sk
+,sr_store_sk as ctr_store_sk
+,sum(SR_FEE) as ctr_total_return
+from store_returns
+,date_dim
+where sr_returned_date_sk = d_date_sk
+and d_year =2000
+group by sr_customer_sk
+,sr_store_sk)
+ select  c_customer_id
+from customer_total_return ctr1
+,store
+,customer
+where ctr1.ctr_total_return > (select avg(ctr_total_return)*1.2
+from customer_total_return ctr2
+where ctr1.ctr_store_sk = ctr2.ctr_store_sk)
+and s_store_sk = ctr1.ctr_store_sk
+and s_state = 'NM'
+and ctr1.ctr_customer_sk = c_customer_sk
+order by c_customer_id
+limit 100;
+
+-- end query 1 in stream 0 using template query1.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query10.q b/ql/src/test/queries/clientpositive/perf/cbo_query10.q
new file mode 100644
index 00000000000..fbdc9db6ddc
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query10.q
@@ -0,0 +1,61 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query10.tpl and seed 797269820
+explain cbo
+select  
+  cd_gender,
+  cd_marital_status,
+  cd_education_status,
+  count(*) cnt1,
+  cd_purchase_estimate,
+  count(*) cnt2,
+  cd_credit_rating,
+  count(*) cnt3,
+  cd_dep_count,
+  count(*) cnt4,
+  cd_dep_employed_count,
+  count(*) cnt5,
+  cd_dep_college_count,
+  count(*) cnt6
+ from
+  customer c,customer_address ca,customer_demographics
+ where
+  c.c_current_addr_sk = ca.ca_address_sk and
+  ca_county in ('Walker County','Richland County','Gaines County','Douglas County','Dona Ana County') and
+  cd_demo_sk = c.c_current_cdemo_sk and 
+  exists (select *
+          from store_sales,date_dim
+          where c.c_customer_sk = ss_customer_sk and
+                ss_sold_date_sk = d_date_sk and
+                d_year = 2002 and
+                d_moy between 4 and 4+3) and
+   (exists (select *
+            from web_sales,date_dim
+            where c.c_customer_sk = ws_bill_customer_sk and
+                  ws_sold_date_sk = d_date_sk and
+                  d_year = 2002 and
+                  d_moy between 4 ANd 4+3) or 
+    exists (select * 
+            from catalog_sales,date_dim
+            where c.c_customer_sk = cs_ship_customer_sk and
+                  cs_sold_date_sk = d_date_sk and
+                  d_year = 2002 and
+                  d_moy between 4 and 4+3))
+ group by cd_gender,
+          cd_marital_status,
+          cd_education_status,
+          cd_purchase_estimate,
+          cd_credit_rating,
+          cd_dep_count,
+          cd_dep_employed_count,
+          cd_dep_college_count
+ order by cd_gender,
+          cd_marital_status,
+          cd_education_status,
+          cd_purchase_estimate,
+          cd_credit_rating,
+          cd_dep_count,
+          cd_dep_employed_count,
+          cd_dep_college_count
+limit 100;
+
+-- end query 1 in stream 0 using template query10.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query11.q b/ql/src/test/queries/clientpositive/perf/cbo_query11.q
new file mode 100644
index 00000000000..09d9529d71b
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query11.q
@@ -0,0 +1,77 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query11.tpl and seed 1819994127
+explain cbo
+with year_total as (
+ select c_customer_id customer_id
+       ,c_first_name customer_first_name
+       ,c_last_name customer_last_name
+       ,c_preferred_cust_flag
+       ,c_birth_country customer_birth_country
+       ,c_login customer_login
+       ,c_email_address customer_email_address
+       ,d_year dyear
+       ,sum(ss_ext_list_price-ss_ext_discount_amt) year_total
+       ,'s' sale_type
+ from customer
+     ,store_sales
+     ,date_dim
+ where c_customer_sk = ss_customer_sk
+   and ss_sold_date_sk = d_date_sk
+ group by c_customer_id
+         ,c_first_name
+         ,c_last_name
+         ,d_year
+         ,c_preferred_cust_flag
+         ,c_birth_country
+         ,c_login
+         ,c_email_address
+         ,d_year 
+ union all
+ select c_customer_id customer_id
+       ,c_first_name customer_first_name
+       ,c_last_name customer_last_name
+       ,c_preferred_cust_flag
+       ,c_birth_country customer_birth_country
+       ,c_login customer_login
+       ,c_email_address customer_email_address
+       ,d_year dyear
+       ,sum(ws_ext_list_price-ws_ext_discount_amt) year_total
+       ,'w' sale_type
+ from customer
+     ,web_sales
+     ,date_dim
+ where c_customer_sk = ws_bill_customer_sk
+   and ws_sold_date_sk = d_date_sk
+ group by c_customer_id
+         ,c_first_name
+         ,c_last_name
+         ,c_preferred_cust_flag
+         ,c_birth_country
+         ,c_login
+         ,c_email_address
+         ,d_year
+         )
+  select  t_s_secyear.c_preferred_cust_flag
+ from year_total t_s_firstyear
+     ,year_total t_s_secyear
+     ,year_total t_w_firstyear
+     ,year_total t_w_secyear
+ where t_s_secyear.customer_id = t_s_firstyear.customer_id
+         and t_s_firstyear.customer_id = t_w_secyear.customer_id
+         and t_s_firstyear.customer_id = t_w_firstyear.customer_id
+         and t_s_firstyear.sale_type = 's'
+         and t_w_firstyear.sale_type = 'w'
+         and t_s_secyear.sale_type = 's'
+         and t_w_secyear.sale_type = 'w'
+         and t_s_firstyear.dyear = 2001
+         and t_s_secyear.dyear = 2001+1
+         and t_w_firstyear.dyear = 2001
+         and t_w_secyear.dyear = 2001+1
+         and t_s_firstyear.year_total > 0
+         and t_w_firstyear.year_total > 0
+         and case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else null end
+             > case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else null end
+ order by t_s_secyear.c_preferred_cust_flag
+limit 100;
+
+-- end query 1 in stream 0 using template query11.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query12.q b/ql/src/test/queries/clientpositive/perf/cbo_query12.q
new file mode 100644
index 00000000000..41029cfa59d
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query12.q
@@ -0,0 +1,35 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query12.tpl and seed 345591136
+explain cbo
+select  i_item_desc 
+      ,i_category 
+      ,i_class 
+      ,i_current_price
+      ,sum(ws_ext_sales_price) as itemrevenue 
+      ,sum(ws_ext_sales_price)*100/sum(sum(ws_ext_sales_price)) over
+          (partition by i_class) as revenueratio
+from	
+	web_sales
+    	,item 
+    	,date_dim
+where 
+	ws_item_sk = i_item_sk 
+  	and i_category in ('Jewelry', 'Sports', 'Books')
+  	and ws_sold_date_sk = d_date_sk
+	and d_date between cast('2001-01-12' as date) 
+				and (cast('2001-01-12' as date) + 30 days)
+group by 
+	i_item_id
+        ,i_item_desc 
+        ,i_category
+        ,i_class
+        ,i_current_price
+order by 
+	i_category
+        ,i_class
+        ,i_item_id
+        ,i_item_desc
+        ,revenueratio
+limit 100;
+
+-- end query 1 in stream 0 using template query12.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query13.q b/ql/src/test/queries/clientpositive/perf/cbo_query13.q
new file mode 100644
index 00000000000..72eb08d499a
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query13.q
@@ -0,0 +1,54 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query13.tpl and seed 622697896
+explain cbo
+select avg(ss_quantity)
+       ,avg(ss_ext_sales_price)
+       ,avg(ss_ext_wholesale_cost)
+       ,sum(ss_ext_wholesale_cost)
+ from store_sales
+     ,store
+     ,customer_demographics
+     ,household_demographics
+     ,customer_address
+     ,date_dim
+ where s_store_sk = ss_store_sk
+ and  ss_sold_date_sk = d_date_sk and d_year = 2001
+ and((ss_hdemo_sk=hd_demo_sk
+  and cd_demo_sk = ss_cdemo_sk
+  and cd_marital_status = 'M'
+  and cd_education_status = '4 yr Degree'
+  and ss_sales_price between 100.00 and 150.00
+  and hd_dep_count = 3   
+     )or
+     (ss_hdemo_sk=hd_demo_sk
+  and cd_demo_sk = ss_cdemo_sk
+  and cd_marital_status = 'D'
+  and cd_education_status = 'Primary'
+  and ss_sales_price between 50.00 and 100.00   
+  and hd_dep_count = 1
+     ) or 
+     (ss_hdemo_sk=hd_demo_sk
+  and cd_demo_sk = ss_cdemo_sk
+  and cd_marital_status = 'U'
+  and cd_education_status = 'Advanced Degree'
+  and ss_sales_price between 150.00 and 200.00 
+  and hd_dep_count = 1  
+     ))
+ and((ss_addr_sk = ca_address_sk
+  and ca_country = 'United States'
+  and ca_state in ('KY', 'GA', 'NM')
+  and ss_net_profit between 100 and 200  
+     ) or
+     (ss_addr_sk = ca_address_sk
+  and ca_country = 'United States'
+  and ca_state in ('MT', 'OR', 'IN')
+  and ss_net_profit between 150 and 300  
+     ) or
+     (ss_addr_sk = ca_address_sk
+  and ca_country = 'United States'
+  and ca_state in ('WI', 'MO', 'WV')
+  and ss_net_profit between 50 and 250  
+     ))
+;
+
+-- end query 1 in stream 0 using template query13.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query14.q b/ql/src/test/queries/clientpositive/perf/cbo_query14.q
new file mode 100644
index 00000000000..eaee914c8df
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query14.q
@@ -0,0 +1,104 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query14.tpl and seed 1819994127
+explain cbo
+with  cross_items as
+ (select i_item_sk ss_item_sk
+ from item,
+ (select iss.i_brand_id brand_id
+     ,iss.i_class_id class_id
+     ,iss.i_category_id category_id
+ from store_sales
+     ,item iss
+     ,date_dim d1
+ where ss_item_sk = iss.i_item_sk
+   and ss_sold_date_sk = d1.d_date_sk
+   and d1.d_year between 1999 AND 1999 + 2
+ intersect 
+ select ics.i_brand_id
+     ,ics.i_class_id
+     ,ics.i_category_id
+ from catalog_sales
+     ,item ics
+     ,date_dim d2
+ where cs_item_sk = ics.i_item_sk
+   and cs_sold_date_sk = d2.d_date_sk
+   and d2.d_year between 1999 AND 1999 + 2
+ intersect
+ select iws.i_brand_id
+     ,iws.i_class_id
+     ,iws.i_category_id
+ from web_sales
+     ,item iws
+     ,date_dim d3
+ where ws_item_sk = iws.i_item_sk
+   and ws_sold_date_sk = d3.d_date_sk
+   and d3.d_year between 1999 AND 1999 + 2) x
+ where i_brand_id = brand_id
+      and i_class_id = class_id
+      and i_category_id = category_id
+),
+ avg_sales as
+ (select avg(quantity*list_price) average_sales
+  from (select ss_quantity quantity
+             ,ss_list_price list_price
+       from store_sales
+           ,date_dim
+       where ss_sold_date_sk = d_date_sk
+         and d_year between 1999 and 2001 
+       union all 
+       select cs_quantity quantity 
+             ,cs_list_price list_price
+       from catalog_sales
+           ,date_dim
+       where cs_sold_date_sk = d_date_sk
+         and d_year between 1998 and 1998 + 2 
+       union all
+       select ws_quantity quantity
+             ,ws_list_price list_price
+       from web_sales
+           ,date_dim
+       where ws_sold_date_sk = d_date_sk
+         and d_year between 1998 and 1998 + 2) x)
+  select  channel, i_brand_id,i_class_id,i_category_id,sum(sales), sum(number_sales)
+ from(
+       select 'store' channel, i_brand_id,i_class_id
+             ,i_category_id,sum(ss_quantity*ss_list_price) sales
+             , count(*) number_sales
+       from store_sales
+           ,item
+           ,date_dim
+       where ss_item_sk in (select ss_item_sk from cross_items)
+         and ss_item_sk = i_item_sk
+         and ss_sold_date_sk = d_date_sk
+         and d_year = 1998+2 
+         and d_moy = 11
+       group by i_brand_id,i_class_id,i_category_id
+       having sum(ss_quantity*ss_list_price) > (select average_sales from avg_sales)
+       union all
+       select 'catalog' channel, i_brand_id,i_class_id,i_category_id, sum(cs_quantity*cs_list_price) sales, count(*) number_sales
+       from catalog_sales
+           ,item
+           ,date_dim
+       where cs_item_sk in (select ss_item_sk from cross_items)
+         and cs_item_sk = i_item_sk
+         and cs_sold_date_sk = d_date_sk
+         and d_year = 1998+2 
+         and d_moy = 11
+       group by i_brand_id,i_class_id,i_category_id
+       having sum(cs_quantity*cs_list_price) > (select average_sales from avg_sales)
+       union all
+       select 'web' channel, i_brand_id,i_class_id,i_category_id, sum(ws_quantity*ws_list_price) sales , count(*) number_sales
+       from web_sales
+           ,item
+           ,date_dim
+       where ws_item_sk in (select ss_item_sk from cross_items)
+         and ws_item_sk = i_item_sk
+         and ws_sold_date_sk = d_date_sk
+         and d_year = 1998+2
+         and d_moy = 11
+       group by i_brand_id,i_class_id,i_category_id
+       having sum(ws_quantity*ws_list_price) > (select average_sales from avg_sales)
+ ) y
+ group by rollup (channel, i_brand_id,i_class_id,i_category_id)
+ order by channel,i_brand_id,i_class_id,i_category_id
+ limit 100;
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query15.q b/ql/src/test/queries/clientpositive/perf/cbo_query15.q
new file mode 100644
index 00000000000..3beea4ea13c
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query15.q
@@ -0,0 +1,22 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query15.tpl and seed 1819994127
+explain cbo
+select  ca_zip
+       ,sum(cs_sales_price)
+ from catalog_sales
+     ,customer
+     ,customer_address
+     ,date_dim
+ where cs_bill_customer_sk = c_customer_sk
+ 	and c_current_addr_sk = ca_address_sk 
+ 	and ( substr(ca_zip,1,5) in ('85669', '86197','88274','83405','86475',
+                                   '85392', '85460', '80348', '81792')
+ 	      or ca_state in ('CA','WA','GA')
+ 	      or cs_sales_price > 500)
+ 	and cs_sold_date_sk = d_date_sk
+ 	and d_qoy = 2 and d_year = 2000
+ group by ca_zip
+ order by ca_zip
+ limit 100;
+
+-- end query 1 in stream 0 using template query15.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query16.q b/ql/src/test/queries/clientpositive/perf/cbo_query16.q
new file mode 100644
index 00000000000..74245aa10b8
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query16.q
@@ -0,0 +1,33 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query16.tpl and seed 171719422
+explain cbo
+select  
+   count(distinct cs_order_number) as `order count`
+  ,sum(cs_ext_ship_cost) as `total shipping cost`
+  ,sum(cs_net_profit) as `total net profit`
+from
+   catalog_sales cs1
+  ,date_dim
+  ,customer_address
+  ,call_center
+where
+    d_date between '2001-4-01' and 
+           (cast('2001-4-01' as date) + 60 days)
+and cs1.cs_ship_date_sk = d_date_sk
+and cs1.cs_ship_addr_sk = ca_address_sk
+and ca_state = 'NY'
+and cs1.cs_call_center_sk = cc_call_center_sk
+and cc_county in ('Ziebach County','Levy County','Huron County','Franklin Parish',
+                  'Daviess County'
+)
+and exists (select *
+            from catalog_sales cs2
+            where cs1.cs_order_number = cs2.cs_order_number
+              and cs1.cs_warehouse_sk <> cs2.cs_warehouse_sk)
+and not exists(select *
+               from catalog_returns cr1
+               where cs1.cs_order_number = cr1.cr_order_number)
+order by count(distinct cs_order_number)
+limit 100;
+
+-- end query 1 in stream 0 using template query16.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query17.q b/ql/src/test/queries/clientpositive/perf/cbo_query17.q
new file mode 100644
index 00000000000..5bf98641ee2
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query17.q
@@ -0,0 +1,47 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query17.tpl and seed 1819994127
+explain cbo
+select  i_item_id
+       ,i_item_desc
+       ,s_state
+       ,count(ss_quantity) as store_sales_quantitycount
+       ,avg(ss_quantity) as store_sales_quantityave
+       ,stddev_samp(ss_quantity) as store_sales_quantitystdev
+       ,stddev_samp(ss_quantity)/avg(ss_quantity) as store_sales_quantitycov
+       ,count(sr_return_quantity) as_store_returns_quantitycount
+       ,avg(sr_return_quantity) as_store_returns_quantityave
+       ,stddev_samp(sr_return_quantity) as_store_returns_quantitystdev
+       ,stddev_samp(sr_return_quantity)/avg(sr_return_quantity) as store_returns_quantitycov
+       ,count(cs_quantity) as catalog_sales_quantitycount ,avg(cs_quantity) as catalog_sales_quantityave
+       ,stddev_samp(cs_quantity)/avg(cs_quantity) as catalog_sales_quantitystdev
+       ,stddev_samp(cs_quantity)/avg(cs_quantity) as catalog_sales_quantitycov
+ from store_sales
+     ,store_returns
+     ,catalog_sales
+     ,date_dim d1
+     ,date_dim d2
+     ,date_dim d3
+     ,store
+     ,item
+ where d1.d_quarter_name = '2000Q1'
+   and d1.d_date_sk = ss_sold_date_sk
+   and i_item_sk = ss_item_sk
+   and s_store_sk = ss_store_sk
+   and ss_customer_sk = sr_customer_sk
+   and ss_item_sk = sr_item_sk
+   and ss_ticket_number = sr_ticket_number
+   and sr_returned_date_sk = d2.d_date_sk
+   and d2.d_quarter_name in ('2000Q1','2000Q2','2000Q3')
+   and sr_customer_sk = cs_bill_customer_sk
+   and sr_item_sk = cs_item_sk
+   and cs_sold_date_sk = d3.d_date_sk
+   and d3.d_quarter_name in ('2000Q1','2000Q2','2000Q3')
+ group by i_item_id
+         ,i_item_desc
+         ,s_state
+ order by i_item_id
+         ,i_item_desc
+         ,s_state
+limit 100;
+
+-- end query 1 in stream 0 using template query17.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query18.q b/ql/src/test/queries/clientpositive/perf/cbo_query18.q
new file mode 100644
index 00000000000..110bee5a3fb
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query18.q
@@ -0,0 +1,36 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query18.tpl and seed 1978355063
+explain cbo
+select  i_item_id,
+        ca_country,
+        ca_state, 
+        ca_county,
+        avg( cast(cs_quantity as numeric(12,2))) agg1,
+        avg( cast(cs_list_price as numeric(12,2))) agg2,
+        avg( cast(cs_coupon_amt as numeric(12,2))) agg3,
+        avg( cast(cs_sales_price as numeric(12,2))) agg4,
+        avg( cast(cs_net_profit as numeric(12,2))) agg5,
+        avg( cast(c_birth_year as numeric(12,2))) agg6,
+        avg( cast(cd1.cd_dep_count as numeric(12,2))) agg7
+ from catalog_sales, customer_demographics cd1, 
+      customer_demographics cd2, customer, customer_address, date_dim, item
+ where cs_sold_date_sk = d_date_sk and
+       cs_item_sk = i_item_sk and
+       cs_bill_cdemo_sk = cd1.cd_demo_sk and
+       cs_bill_customer_sk = c_customer_sk and
+       cd1.cd_gender = 'M' and 
+       cd1.cd_education_status = 'College' and
+       c_current_cdemo_sk = cd2.cd_demo_sk and
+       c_current_addr_sk = ca_address_sk and
+       c_birth_month in (9,5,12,4,1,10) and
+       d_year = 2001 and
+       ca_state in ('ND','WI','AL'
+                   ,'NC','OK','MS','TN')
+ group by rollup (i_item_id, ca_country, ca_state, ca_county)
+ order by ca_country,
+        ca_state, 
+        ca_county,
+	i_item_id
+ limit 100;
+
+-- end query 1 in stream 0 using template query18.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query19.q b/ql/src/test/queries/clientpositive/perf/cbo_query19.q
new file mode 100644
index 00000000000..abcec3601f2
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query19.q
@@ -0,0 +1,27 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query19.tpl and seed 1930872976
+explain cbo
+select  i_brand_id brand_id, i_brand brand, i_manufact_id, i_manufact,
+ 	sum(ss_ext_sales_price) ext_price
+ from date_dim, store_sales, item,customer,customer_address,store
+ where d_date_sk = ss_sold_date_sk
+   and ss_item_sk = i_item_sk
+   and i_manager_id=7
+   and d_moy=11
+   and d_year=1999
+   and ss_customer_sk = c_customer_sk 
+   and c_current_addr_sk = ca_address_sk
+   and substr(ca_zip,1,5) <> substr(s_zip,1,5) 
+   and ss_store_sk = s_store_sk 
+ group by i_brand
+      ,i_brand_id
+      ,i_manufact_id
+      ,i_manufact
+ order by ext_price desc
+         ,i_brand
+         ,i_brand_id
+         ,i_manufact_id
+         ,i_manufact
+limit 100 ;
+
+-- end query 1 in stream 0 using template query19.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query2.q b/ql/src/test/queries/clientpositive/perf/cbo_query2.q
new file mode 100644
index 00000000000..9fcccbf4e4c
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query2.q
@@ -0,0 +1,62 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query2.tpl and seed 1819994127
+explain cbo
+with wscs as
+ (select sold_date_sk
+        ,sales_price
+  from (select ws_sold_date_sk sold_date_sk
+              ,ws_ext_sales_price sales_price
+        from web_sales) x
+        union all
+       (select cs_sold_date_sk sold_date_sk
+              ,cs_ext_sales_price sales_price
+        from catalog_sales)),
+ wswscs as 
+ (select d_week_seq,
+        sum(case when (d_day_name='Sunday') then sales_price else null end) sun_sales,
+        sum(case when (d_day_name='Monday') then sales_price else null end) mon_sales,
+        sum(case when (d_day_name='Tuesday') then sales_price else  null end) tue_sales,
+        sum(case when (d_day_name='Wednesday') then sales_price else null end) wed_sales,
+        sum(case when (d_day_name='Thursday') then sales_price else null end) thu_sales,
+        sum(case when (d_day_name='Friday') then sales_price else null end) fri_sales,
+        sum(case when (d_day_name='Saturday') then sales_price else null end) sat_sales
+ from wscs
+     ,date_dim
+ where d_date_sk = sold_date_sk
+ group by d_week_seq)
+ select d_week_seq1
+       ,round(sun_sales1/sun_sales2,2)
+       ,round(mon_sales1/mon_sales2,2)
+       ,round(tue_sales1/tue_sales2,2)
+       ,round(wed_sales1/wed_sales2,2)
+       ,round(thu_sales1/thu_sales2,2)
+       ,round(fri_sales1/fri_sales2,2)
+       ,round(sat_sales1/sat_sales2,2)
+ from
+ (select wswscs.d_week_seq d_week_seq1
+        ,sun_sales sun_sales1
+        ,mon_sales mon_sales1
+        ,tue_sales tue_sales1
+        ,wed_sales wed_sales1
+        ,thu_sales thu_sales1
+        ,fri_sales fri_sales1
+        ,sat_sales sat_sales1
+  from wswscs,date_dim 
+  where date_dim.d_week_seq = wswscs.d_week_seq and
+        d_year = 2001) y,
+ (select wswscs.d_week_seq d_week_seq2
+        ,sun_sales sun_sales2
+        ,mon_sales mon_sales2
+        ,tue_sales tue_sales2
+        ,wed_sales wed_sales2
+        ,thu_sales thu_sales2
+        ,fri_sales fri_sales2
+        ,sat_sales sat_sales2
+  from wswscs
+      ,date_dim 
+  where date_dim.d_week_seq = wswscs.d_week_seq and
+        d_year = 2001+1) z
+ where d_week_seq1=d_week_seq2-53
+ order by d_week_seq1;
+
+-- end query 1 in stream 0 using template query2.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query20.q b/ql/src/test/queries/clientpositive/perf/cbo_query20.q
new file mode 100644
index 00000000000..1d361b8b362
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query20.q
@@ -0,0 +1,31 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query20.tpl and seed 345591136
+explain cbo
+select  i_item_desc 
+       ,i_category 
+       ,i_class 
+       ,i_current_price
+       ,sum(cs_ext_sales_price) as itemrevenue 
+       ,sum(cs_ext_sales_price)*100/sum(sum(cs_ext_sales_price)) over
+           (partition by i_class) as revenueratio
+ from	catalog_sales
+     ,item 
+     ,date_dim
+ where cs_item_sk = i_item_sk 
+   and i_category in ('Jewelry', 'Sports', 'Books')
+   and cs_sold_date_sk = d_date_sk
+ and d_date between cast('2001-01-12' as date) 
+ 				and (cast('2001-01-12' as date) + 30 days)
+ group by i_item_id
+         ,i_item_desc 
+         ,i_category
+         ,i_class
+         ,i_current_price
+ order by i_category
+         ,i_class
+         ,i_item_id
+         ,i_item_desc
+         ,revenueratio
+limit 100;
+
+-- end query 1 in stream 0 using template query20.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query21.q b/ql/src/test/queries/clientpositive/perf/cbo_query21.q
new file mode 100644
index 00000000000..90daea02c36
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query21.q
@@ -0,0 +1,32 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query21.tpl and seed 1819994127
+explain cbo
+select  *
+ from(select w_warehouse_name
+            ,i_item_id
+            ,sum(case when (cast(d_date as date) < cast ('1998-04-08' as date))
+	                then inv_quantity_on_hand 
+                      else 0 end) as inv_before
+            ,sum(case when (cast(d_date as date) >= cast ('1998-04-08' as date))
+                      then inv_quantity_on_hand 
+                      else 0 end) as inv_after
+   from inventory
+       ,warehouse
+       ,item
+       ,date_dim
+   where i_current_price between 0.99 and 1.49
+     and i_item_sk          = inv_item_sk
+     and inv_warehouse_sk   = w_warehouse_sk
+     and inv_date_sk    = d_date_sk
+     and d_date between (cast ('1998-04-08' as date) - 30 days)
+                    and (cast ('1998-04-08' as date) + 30 days)
+   group by w_warehouse_name, i_item_id) x
+ where (case when inv_before > 0 
+             then inv_after / inv_before 
+             else null
+             end) between 2.0/3.0 and 3.0/2.0
+ order by w_warehouse_name
+         ,i_item_id
+ limit 100;
+
+-- end query 1 in stream 0 using template query21.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query22.q b/ql/src/test/queries/clientpositive/perf/cbo_query22.q
new file mode 100644
index 00000000000..14ceec4174f
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query22.q
@@ -0,0 +1,24 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query22.tpl and seed 1819994127
+explain cbo
+select  i_product_name
+             ,i_brand
+             ,i_class
+             ,i_category
+             ,avg(inv_quantity_on_hand) qoh
+       from inventory
+           ,date_dim
+           ,item
+           ,warehouse
+       where inv_date_sk=d_date_sk
+              and inv_item_sk=i_item_sk
+              and inv_warehouse_sk = w_warehouse_sk
+              and d_month_seq between 1212 and 1212 + 11
+       group by rollup(i_product_name
+                       ,i_brand
+                       ,i_class
+                       ,i_category)
+order by qoh, i_product_name, i_brand, i_class, i_category
+limit 100;
+
+-- end query 1 in stream 0 using template query22.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query23.q b/ql/src/test/queries/clientpositive/perf/cbo_query23.q
new file mode 100644
index 00000000000..a1c661b086f
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query23.q
@@ -0,0 +1,52 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query23.tpl and seed 2031708268
+explain cbo
+with frequent_ss_items as 
+ (select substr(i_item_desc,1,30) itemdesc,i_item_sk item_sk,d_date solddate,count(*) cnt
+  from store_sales
+      ,date_dim 
+      ,item
+  where ss_sold_date_sk = d_date_sk
+    and ss_item_sk = i_item_sk 
+    and d_year in (1999,1999+1,1999+2,1999+3)
+  group by substr(i_item_desc,1,30),i_item_sk,d_date
+  having count(*) >4),
+ max_store_sales as
+ (select max(csales) tpcds_cmax 
+  from (select c_customer_sk,sum(ss_quantity*ss_sales_price) csales
+        from store_sales
+            ,customer
+            ,date_dim 
+        where ss_customer_sk = c_customer_sk
+         and ss_sold_date_sk = d_date_sk
+         and d_year in (1999,1999+1,1999+2,1999+3) 
+        group by c_customer_sk) x),
+ best_ss_customer as
+ (select c_customer_sk,sum(ss_quantity*ss_sales_price) ssales
+  from store_sales
+      ,customer
+  where ss_customer_sk = c_customer_sk
+  group by c_customer_sk
+  having sum(ss_quantity*ss_sales_price) > (95/100.0) * (select
+  *
+from
+ max_store_sales))
+  select  sum(sales)
+ from ((select cs_quantity*cs_list_price sales
+       from catalog_sales
+           ,date_dim 
+       where d_year = 1999 
+         and d_moy = 1 
+         and cs_sold_date_sk = d_date_sk 
+         and cs_item_sk in (select item_sk from frequent_ss_items)
+         and cs_bill_customer_sk in (select c_customer_sk from best_ss_customer))
+      union all
+      (select ws_quantity*ws_list_price sales
+       from web_sales 
+           ,date_dim 
+       where d_year = 1999 
+         and d_moy = 1 
+         and ws_sold_date_sk = d_date_sk 
+         and ws_item_sk in (select item_sk from frequent_ss_items)
+         and ws_bill_customer_sk in (select c_customer_sk from best_ss_customer))) y
+ limit 100;
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query24.q b/ql/src/test/queries/clientpositive/perf/cbo_query24.q
new file mode 100644
index 00000000000..02bcbafb7e3
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query24.q
@@ -0,0 +1,51 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query24.tpl and seed 1220860970
+explain cbo
+with ssales as
+(select c_last_name
+      ,c_first_name
+      ,s_store_name
+      ,ca_state
+      ,s_state
+      ,i_color
+      ,i_current_price
+      ,i_manager_id
+      ,i_units
+      ,i_size
+      ,sum(ss_sales_price) netpaid
+from store_sales
+    ,store_returns
+    ,store
+    ,item
+    ,customer
+    ,customer_address
+where ss_ticket_number = sr_ticket_number
+  and ss_item_sk = sr_item_sk
+  and ss_customer_sk = c_customer_sk
+  and ss_item_sk = i_item_sk
+  and ss_store_sk = s_store_sk
+  and c_birth_country = upper(ca_country)
+  and s_zip = ca_zip
+and s_market_id=7
+group by c_last_name
+        ,c_first_name
+        ,s_store_name
+        ,ca_state
+        ,s_state
+        ,i_color
+        ,i_current_price
+        ,i_manager_id
+        ,i_units
+        ,i_size)
+select c_last_name
+      ,c_first_name
+      ,s_store_name
+      ,sum(netpaid) paid
+from ssales
+where i_color = 'orchid'
+group by c_last_name
+        ,c_first_name
+        ,s_store_name
+having sum(netpaid) > (select 0.05*avg(netpaid)
+                                 from ssales)
+;
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query25.q b/ql/src/test/queries/clientpositive/perf/cbo_query25.q
new file mode 100644
index 00000000000..9611e28f83a
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query25.q
@@ -0,0 +1,50 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query25.tpl and seed 1819994127
+explain cbo
+select  
+ i_item_id
+ ,i_item_desc
+ ,s_store_id
+ ,s_store_name
+ ,sum(ss_net_profit) as store_sales_profit
+ ,sum(sr_net_loss) as store_returns_loss
+ ,sum(cs_net_profit) as catalog_sales_profit
+ from
+ store_sales
+ ,store_returns
+ ,catalog_sales
+ ,date_dim d1
+ ,date_dim d2
+ ,date_dim d3
+ ,store
+ ,item
+ where
+ d1.d_moy = 4
+ and d1.d_year = 2000
+ and d1.d_date_sk = ss_sold_date_sk
+ and i_item_sk = ss_item_sk
+ and s_store_sk = ss_store_sk
+ and ss_customer_sk = sr_customer_sk
+ and ss_item_sk = sr_item_sk
+ and ss_ticket_number = sr_ticket_number
+ and sr_returned_date_sk = d2.d_date_sk
+ and d2.d_moy               between 4 and  10
+ and d2.d_year              = 2000
+ and sr_customer_sk = cs_bill_customer_sk
+ and sr_item_sk = cs_item_sk
+ and cs_sold_date_sk = d3.d_date_sk
+ and d3.d_moy               between 4 and  10 
+ and d3.d_year              = 2000
+ group by
+ i_item_id
+ ,i_item_desc
+ ,s_store_id
+ ,s_store_name
+ order by
+ i_item_id
+ ,i_item_desc
+ ,s_store_id
+ ,s_store_name
+ limit 100;
+
+-- end query 1 in stream 0 using template query25.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query26.q b/ql/src/test/queries/clientpositive/perf/cbo_query26.q
new file mode 100644
index 00000000000..8b874c85bc2
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query26.q
@@ -0,0 +1,23 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query26.tpl and seed 1930872976
+explain cbo
+select  i_item_id, 
+        avg(cs_quantity) agg1,
+        avg(cs_list_price) agg2,
+        avg(cs_coupon_amt) agg3,
+        avg(cs_sales_price) agg4 
+ from catalog_sales, customer_demographics, date_dim, item, promotion
+ where cs_sold_date_sk = d_date_sk and
+       cs_item_sk = i_item_sk and
+       cs_bill_cdemo_sk = cd_demo_sk and
+       cs_promo_sk = p_promo_sk and
+       cd_gender = 'F' and 
+       cd_marital_status = 'W' and
+       cd_education_status = 'Primary' and
+       (p_channel_email = 'N' or p_channel_event = 'N') and
+       d_year = 1998 
+ group by i_item_id
+ order by i_item_id
+ limit 100;
+
+-- end query 1 in stream 0 using template query26.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query27.q b/ql/src/test/queries/clientpositive/perf/cbo_query27.q
new file mode 100644
index 00000000000..48eaad5b542
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query27.q
@@ -0,0 +1,25 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query27.tpl and seed 2017787633
+explain cbo
+select  i_item_id,
+        s_state, grouping(s_state) g_state,
+        avg(ss_quantity) agg1,
+        avg(ss_list_price) agg2,
+        avg(ss_coupon_amt) agg3,
+        avg(ss_sales_price) agg4
+ from store_sales, customer_demographics, date_dim, store, item
+ where ss_sold_date_sk = d_date_sk and
+       ss_item_sk = i_item_sk and
+       ss_store_sk = s_store_sk and
+       ss_cdemo_sk = cd_demo_sk and
+       cd_gender = 'M' and
+       cd_marital_status = 'U' and
+       cd_education_status = '2 yr Degree' and
+       d_year = 2001 and
+       s_state in ('SD','FL', 'MI', 'LA', 'MO', 'SC')
+ group by rollup (i_item_id, s_state)
+ order by i_item_id
+         ,s_state
+ limit 100;
+
+-- end query 1 in stream 0 using template query27.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query28.q b/ql/src/test/queries/clientpositive/perf/cbo_query28.q
new file mode 100644
index 00000000000..ad9dacd65e7
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query28.q
@@ -0,0 +1,59 @@
+set hive.mapred.mode=nonstrict;
+set hive.optimize.metadataonly=true;
+
+-- start query 1 in stream 0 using template query28.tpl and seed 444293455
+explain cbo
+select  *
+from (select avg(ss_list_price) B1_LP
+            ,count(ss_list_price) B1_CNT
+            ,count(distinct ss_list_price) B1_CNTD
+      from store_sales
+      where ss_quantity between 0 and 5
+        and (ss_list_price between 11 and 11+10 
+             or ss_coupon_amt between 460 and 460+1000
+             or ss_wholesale_cost between 14 and 14+20)) B1,
+     (select avg(ss_list_price) B2_LP
+            ,count(ss_list_price) B2_CNT
+            ,count(distinct ss_list_price) B2_CNTD
+      from store_sales
+      where ss_quantity between 6 and 10
+        and (ss_list_price between 91 and 91+10
+          or ss_coupon_amt between 1430 and 1430+1000
+          or ss_wholesale_cost between 32 and 32+20)) B2,
+     (select avg(ss_list_price) B3_LP
+            ,count(ss_list_price) B3_CNT
+            ,count(distinct ss_list_price) B3_CNTD
+      from store_sales
+      where ss_quantity between 11 and 15
+        and (ss_list_price between 66 and 66+10
+          or ss_coupon_amt between 920 and 920+1000
+          or ss_wholesale_cost between 4 and 4+20)) B3,
+     (select avg(ss_list_price) B4_LP
+            ,count(ss_list_price) B4_CNT
+            ,count(distinct ss_list_price) B4_CNTD
+      from store_sales
+      where ss_quantity between 16 and 20
+        and (ss_list_price between 142 and 142+10
+          or ss_coupon_amt between 3054 and 3054+1000
+          or ss_wholesale_cost between 80 and 80+20)) B4,
+     (select avg(ss_list_price) B5_LP
+            ,count(ss_list_price) B5_CNT
+            ,count(distinct ss_list_price) B5_CNTD
+      from store_sales
+      where ss_quantity between 21 and 25
+        and (ss_list_price between 135 and 135+10
+          or ss_coupon_amt between 14180 and 14180+1000
+          or ss_wholesale_cost between 38 and 38+20)) B5,
+     (select avg(ss_list_price) B6_LP
+            ,count(ss_list_price) B6_CNT
+            ,count(distinct ss_list_price) B6_CNTD
+      from store_sales
+      where ss_quantity between 26 and 30
+        and (ss_list_price between 28 and 28+10
+          or ss_coupon_amt between 2513 and 2513+1000
+          or ss_wholesale_cost between 42 and 42+20)) B6
+limit 100;
+
+-- end query 1 in stream 0 using template query28.tpl
+
+set hive.optimize.metadataonly=false;
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query29.q b/ql/src/test/queries/clientpositive/perf/cbo_query29.q
new file mode 100644
index 00000000000..ea9ec12d337
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query29.q
@@ -0,0 +1,49 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query29.tpl and seed 2031708268
+explain cbo
+select   
+     i_item_id
+    ,i_item_desc
+    ,s_store_id
+    ,s_store_name
+    ,sum(ss_quantity)        as store_sales_quantity
+    ,sum(sr_return_quantity) as store_returns_quantity
+    ,sum(cs_quantity)        as catalog_sales_quantity
+ from
+    store_sales
+   ,store_returns
+   ,catalog_sales
+   ,date_dim             d1
+   ,date_dim             d2
+   ,date_dim             d3
+   ,store
+   ,item
+ where
+     d1.d_moy               = 4 
+ and d1.d_year              = 1999
+ and d1.d_date_sk           = ss_sold_date_sk
+ and i_item_sk              = ss_item_sk
+ and s_store_sk             = ss_store_sk
+ and ss_customer_sk         = sr_customer_sk
+ and ss_item_sk             = sr_item_sk
+ and ss_ticket_number       = sr_ticket_number
+ and sr_returned_date_sk    = d2.d_date_sk
+ and d2.d_moy               between 4 and  4 + 3 
+ and d2.d_year              = 1999
+ and sr_customer_sk         = cs_bill_customer_sk
+ and sr_item_sk             = cs_item_sk
+ and cs_sold_date_sk        = d3.d_date_sk     
+ and d3.d_year              in (1999,1999+1,1999+2)
+ group by
+    i_item_id
+   ,i_item_desc
+   ,s_store_id
+   ,s_store_name
+ order by
+    i_item_id 
+   ,i_item_desc
+   ,s_store_id
+   ,s_store_name
+ limit 100;
+
+-- end query 1 in stream 0 using template query29.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query3.q b/ql/src/test/queries/clientpositive/perf/cbo_query3.q
new file mode 100644
index 00000000000..b1bc55bfdfe
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query3.q
@@ -0,0 +1,23 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query3.tpl and seed 2031708268
+explain cbo
+select  dt.d_year 
+       ,item.i_brand_id brand_id 
+       ,item.i_brand brand
+       ,sum(ss_ext_sales_price) sum_agg
+ from  date_dim dt 
+      ,store_sales
+      ,item
+ where dt.d_date_sk = store_sales.ss_sold_date_sk
+   and store_sales.ss_item_sk = item.i_item_sk
+   and item.i_manufact_id = 436
+   and dt.d_moy=12
+ group by dt.d_year
+      ,item.i_brand
+      ,item.i_brand_id
+ order by dt.d_year
+         ,sum_agg desc
+         ,brand_id
+ limit 100;
+
+-- end query 1 in stream 0 using template query3.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query30.q b/ql/src/test/queries/clientpositive/perf/cbo_query30.q
new file mode 100644
index 00000000000..ece63700c0f
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query30.q
@@ -0,0 +1,33 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query30.tpl and seed 1819994127
+explain cbo
+with customer_total_return as
+ (select wr_returning_customer_sk as ctr_customer_sk
+        ,ca_state as ctr_state, 
+ 	sum(wr_return_amt) as ctr_total_return
+ from web_returns
+     ,date_dim
+     ,customer_address
+ where wr_returned_date_sk = d_date_sk 
+   and d_year =2002
+   and wr_returning_addr_sk = ca_address_sk 
+ group by wr_returning_customer_sk
+         ,ca_state)
+  select  c_customer_id,c_salutation,c_first_name,c_last_name,c_preferred_cust_flag
+       ,c_birth_day,c_birth_month,c_birth_year,c_birth_country,c_login,c_email_address
+       ,c_last_review_date,ctr_total_return
+ from customer_total_return ctr1
+     ,customer_address
+     ,customer
+ where ctr1.ctr_total_return > (select avg(ctr_total_return)*1.2
+ 			  from customer_total_return ctr2 
+                  	  where ctr1.ctr_state = ctr2.ctr_state)
+       and ca_address_sk = c_current_addr_sk
+       and ca_state = 'IL'
+       and ctr1.ctr_customer_sk = c_customer_sk
+ order by c_customer_id,c_salutation,c_first_name,c_last_name,c_preferred_cust_flag
+                  ,c_birth_day,c_birth_month,c_birth_year,c_birth_country,c_login,c_email_address
+                  ,c_last_review_date,ctr_total_return
+limit 100;
+
+-- end query 1 in stream 0 using template query30.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query31.q b/ql/src/test/queries/clientpositive/perf/cbo_query31.q
new file mode 100644
index 00000000000..a2babbf23db
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query31.q
@@ -0,0 +1,54 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query31.tpl and seed 1819994127
+explain cbo
+with ss as
+ (select ca_county,d_qoy, d_year,sum(ss_ext_sales_price) as store_sales
+ from store_sales,date_dim,customer_address
+ where ss_sold_date_sk = d_date_sk
+  and ss_addr_sk=ca_address_sk
+ group by ca_county,d_qoy, d_year),
+ ws as
+ (select ca_county,d_qoy, d_year,sum(ws_ext_sales_price) as web_sales
+ from web_sales,date_dim,customer_address
+ where ws_sold_date_sk = d_date_sk
+  and ws_bill_addr_sk=ca_address_sk
+ group by ca_county,d_qoy, d_year)
+ select /* tt */
+        ss1.ca_county
+       ,ss1.d_year
+       ,ws2.web_sales/ws1.web_sales web_q1_q2_increase
+       ,ss2.store_sales/ss1.store_sales store_q1_q2_increase
+       ,ws3.web_sales/ws2.web_sales web_q2_q3_increase
+       ,ss3.store_sales/ss2.store_sales store_q2_q3_increase
+ from
+        ss ss1
+       ,ss ss2
+       ,ss ss3
+       ,ws ws1
+       ,ws ws2
+       ,ws ws3
+ where
+    ss1.d_qoy = 1
+    and ss1.d_year = 2000
+    and ss1.ca_county = ss2.ca_county
+    and ss2.d_qoy = 2
+    and ss2.d_year = 2000
+ and ss2.ca_county = ss3.ca_county
+    and ss3.d_qoy = 3
+    and ss3.d_year = 2000
+    and ss1.ca_county = ws1.ca_county
+    and ws1.d_qoy = 1
+    and ws1.d_year = 2000
+    and ws1.ca_county = ws2.ca_county
+    and ws2.d_qoy = 2
+    and ws2.d_year = 2000
+    and ws1.ca_county = ws3.ca_county
+    and ws3.d_qoy = 3
+    and ws3.d_year =2000
+    and case when ws1.web_sales > 0 then ws2.web_sales/ws1.web_sales else null end 
+       > case when ss1.store_sales > 0 then ss2.store_sales/ss1.store_sales else null end
+    and case when ws2.web_sales > 0 then ws3.web_sales/ws2.web_sales else null end
+       > case when ss2.store_sales > 0 then ss3.store_sales/ss2.store_sales else null end
+ order by ss1.d_year;
+
+-- end query 1 in stream 0 using template query31.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query32.q b/ql/src/test/queries/clientpositive/perf/cbo_query32.q
new file mode 100644
index 00000000000..4f3c7291a8e
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query32.q
@@ -0,0 +1,30 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query32.tpl and seed 2031708268
+explain cbo
+select  sum(cs_ext_discount_amt)  as `excess discount amount` 
+from 
+   catalog_sales 
+   ,item 
+   ,date_dim
+where
+i_manufact_id = 269
+and i_item_sk = cs_item_sk 
+and d_date between '1998-03-18' and 
+        (cast('1998-03-18' as date) + 90 days)
+and d_date_sk = cs_sold_date_sk 
+and cs_ext_discount_amt  
+     > ( 
+         select 
+            1.3 * avg(cs_ext_discount_amt) 
+         from 
+            catalog_sales 
+           ,date_dim
+         where 
+              cs_item_sk = i_item_sk 
+          and d_date between '1998-03-18' and
+                             (cast('1998-03-18' as date) + 90 days)
+          and d_date_sk = cs_sold_date_sk 
+      ) 
+limit 100;
+
+-- end query 1 in stream 0 using template query32.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query33.q b/ql/src/test/queries/clientpositive/perf/cbo_query33.q
new file mode 100644
index 00000000000..fc808aeddb8
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query33.q
@@ -0,0 +1,77 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query33.tpl and seed 1930872976
+explain cbo
+with ss as (
+ select
+          i_manufact_id,sum(ss_ext_sales_price) total_sales
+ from
+ 	store_sales,
+ 	date_dim,
+         customer_address,
+         item
+ where
+         i_manufact_id in (select
+  i_manufact_id
+from
+ item
+where i_category in ('Books'))
+ and     ss_item_sk              = i_item_sk
+ and     ss_sold_date_sk         = d_date_sk
+ and     d_year                  = 1999
+ and     d_moy                   = 3
+ and     ss_addr_sk              = ca_address_sk
+ and     ca_gmt_offset           = -6 
+ group by i_manufact_id),
+ cs as (
+ select
+          i_manufact_id,sum(cs_ext_sales_price) total_sales
+ from
+ 	catalog_sales,
+ 	date_dim,
+         customer_address,
+         item
+ where
+         i_manufact_id               in (select
+  i_manufact_id
+from
+ item
+where i_category in ('Books'))
+ and     cs_item_sk              = i_item_sk
+ and     cs_sold_date_sk         = d_date_sk
+ and     d_year                  = 1999
+ and     d_moy                   = 3
+ and     cs_bill_addr_sk         = ca_address_sk
+ and     ca_gmt_offset           = -6 
+ group by i_manufact_id),
+ ws as (
+ select
+          i_manufact_id,sum(ws_ext_sales_price) total_sales
+ from
+ 	web_sales,
+ 	date_dim,
+         customer_address,
+         item
+ where
+         i_manufact_id               in (select
+  i_manufact_id
+from
+ item
+where i_category in ('Books'))
+ and     ws_item_sk              = i_item_sk
+ and     ws_sold_date_sk         = d_date_sk
+ and     d_year                  = 1999
+ and     d_moy                   = 3
+ and     ws_bill_addr_sk         = ca_address_sk
+ and     ca_gmt_offset           = -6
+ group by i_manufact_id)
+  select  i_manufact_id ,sum(total_sales) total_sales
+ from  (select * from ss 
+        union all
+        select * from cs 
+        union all
+        select * from ws) tmp1
+ group by i_manufact_id
+ order by total_sales
+limit 100;
+
+-- end query 1 in stream 0 using template query33.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query34.q b/ql/src/test/queries/clientpositive/perf/cbo_query34.q
new file mode 100644
index 00000000000..2b93a6144c3
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query34.q
@@ -0,0 +1,33 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query34.tpl and seed 1971067816
+explain cbo
+select c_last_name
+       ,c_first_name
+       ,c_salutation
+       ,c_preferred_cust_flag
+       ,ss_ticket_number
+       ,cnt from
+   (select ss_ticket_number
+          ,ss_customer_sk
+          ,count(*) cnt
+    from store_sales,date_dim,store,household_demographics
+    where store_sales.ss_sold_date_sk = date_dim.d_date_sk
+    and store_sales.ss_store_sk = store.s_store_sk  
+    and store_sales.ss_hdemo_sk = household_demographics.hd_demo_sk
+    and (date_dim.d_dom between 1 and 3 or date_dim.d_dom between 25 and 28)
+    and (household_demographics.hd_buy_potential = '>10000' or
+         household_demographics.hd_buy_potential = 'unknown')
+    and household_demographics.hd_vehicle_count > 0
+    and (case when household_demographics.hd_vehicle_count > 0 
+	then household_demographics.hd_dep_count/ household_demographics.hd_vehicle_count 
+	else null 
+	end)  > 1.2
+    and date_dim.d_year in (2000,2000+1,2000+2)
+    and store.s_county in ('Mobile County','Maverick County','Huron County','Kittitas County',
+                           'Fairfield County','Jackson County','Barrow County','Pennington County')
+    group by ss_ticket_number,ss_customer_sk) dn,customer
+    where ss_customer_sk = c_customer_sk
+      and cnt between 15 and 20
+    order by c_last_name,c_first_name,c_salutation,c_preferred_cust_flag desc;
+
+-- end query 1 in stream 0 using template query34.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query35.q b/ql/src/test/queries/clientpositive/perf/cbo_query35.q
new file mode 100644
index 00000000000..9e877806b22
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query35.q
@@ -0,0 +1,59 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query35.tpl and seed 1930872976
+explain cbo
+select   
+  ca_state,
+  cd_gender,
+  cd_marital_status,
+  count(*) cnt1,
+  avg(cd_dep_count),
+  max(cd_dep_count),
+  sum(cd_dep_count),
+  cd_dep_employed_count,
+  count(*) cnt2,
+  avg(cd_dep_employed_count),
+  max(cd_dep_employed_count),
+  sum(cd_dep_employed_count),
+  cd_dep_college_count,
+  count(*) cnt3,
+  avg(cd_dep_college_count),
+  max(cd_dep_college_count),
+  sum(cd_dep_college_count)
+ from
+  customer c,customer_address ca,customer_demographics
+ where
+  c.c_current_addr_sk = ca.ca_address_sk and
+  cd_demo_sk = c.c_current_cdemo_sk and 
+  exists (select *
+          from store_sales,date_dim
+          where c.c_customer_sk = ss_customer_sk and
+                ss_sold_date_sk = d_date_sk and
+                d_year = 1999 and
+                d_qoy < 4) and
+   (exists (select *
+            from web_sales,date_dim
+            where c.c_customer_sk = ws_bill_customer_sk and
+                  ws_sold_date_sk = d_date_sk and
+                  d_year = 1999 and
+                  d_qoy < 4) or 
+    exists (select * 
+            from catalog_sales,date_dim
+            where c.c_customer_sk = cs_ship_customer_sk and
+                  cs_sold_date_sk = d_date_sk and
+                  d_year = 1999 and
+                  d_qoy < 4))
+ group by ca_state,
+          cd_gender,
+          cd_marital_status,
+          cd_dep_count,
+          cd_dep_employed_count,
+          cd_dep_college_count
+ order by ca_state,
+          cd_gender,
+          cd_marital_status,
+          cd_dep_count,
+          cd_dep_employed_count,
+          cd_dep_college_count
+ limit 100;
+
+-- end query 1 in stream 0 using template query35.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query36.q b/ql/src/test/queries/clientpositive/perf/cbo_query36.q
new file mode 100644
index 00000000000..cab89deeb19
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query36.q
@@ -0,0 +1,32 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query36.tpl and seed 1544728811
+explain cbo
+select  
+    sum(ss_net_profit)/sum(ss_ext_sales_price) as gross_margin
+   ,i_category
+   ,i_class
+   ,grouping(i_category)+grouping(i_class) as lochierarchy
+   ,rank() over (
+ 	partition by grouping(i_category)+grouping(i_class),
+ 	case when grouping(i_class) = 0 then i_category end 
+ 	order by sum(ss_net_profit)/sum(ss_ext_sales_price) asc) as rank_within_parent
+ from
+    store_sales
+   ,date_dim       d1
+   ,item
+   ,store
+ where
+    d1.d_year = 1999 
+ and d1.d_date_sk = ss_sold_date_sk
+ and i_item_sk  = ss_item_sk 
+ and s_store_sk  = ss_store_sk
+ and s_state in ('SD','FL','MI','LA',
+                 'MO','SC','AL','GA')
+ group by rollup(i_category,i_class)
+ order by
+   lochierarchy desc
+  ,case when lochierarchy = 0 then i_category end
+  ,rank_within_parent
+  limit 100;
+
+-- end query 1 in stream 0 using template query36.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query37.q b/ql/src/test/queries/clientpositive/perf/cbo_query37.q
new file mode 100644
index 00000000000..f258dc0aa00
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query37.q
@@ -0,0 +1,19 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query37.tpl and seed 301843662
+explain cbo
+select  i_item_id
+       ,i_item_desc
+       ,i_current_price
+ from item, inventory, date_dim, catalog_sales
+ where i_current_price between 22 and 22 + 30
+ and inv_item_sk = i_item_sk
+ and d_date_sk=inv_date_sk
+ and d_date between cast('2001-06-02' as date) and (cast('2001-06-02' as date) +  60 days)
+ and i_manufact_id in (678,964,918,849)
+ and inv_quantity_on_hand between 100 and 500
+ and cs_item_sk = i_item_sk
+ group by i_item_id,i_item_desc,i_current_price
+ order by i_item_id
+ limit 100;
+
+-- end query 1 in stream 0 using template query37.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query38.q b/ql/src/test/queries/clientpositive/perf/cbo_query38.q
new file mode 100644
index 00000000000..55beed22ce5
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query38.q
@@ -0,0 +1,25 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query38.tpl and seed 1819994127
+explain cbo
+select  count(*) from (
+    select distinct c_last_name, c_first_name, d_date
+    from store_sales, date_dim, customer
+          where store_sales.ss_sold_date_sk = date_dim.d_date_sk
+      and store_sales.ss_customer_sk = customer.c_customer_sk
+      and d_month_seq between 1212 and 1212 + 11
+  intersect
+    select distinct c_last_name, c_first_name, d_date
+    from catalog_sales, date_dim, customer
+          where catalog_sales.cs_sold_date_sk = date_dim.d_date_sk
+      and catalog_sales.cs_bill_customer_sk = customer.c_customer_sk
+      and d_month_seq between 1212 and 1212 + 11
+  intersect
+    select distinct c_last_name, c_first_name, d_date
+    from web_sales, date_dim, customer
+          where web_sales.ws_sold_date_sk = date_dim.d_date_sk
+      and web_sales.ws_bill_customer_sk = customer.c_customer_sk
+      and d_month_seq between 1212 and 1212 + 11
+) hot_cust
+limit 100;
+
+-- end query 1 in stream 0 using template query38.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query39.q b/ql/src/test/queries/clientpositive/perf/cbo_query39.q
new file mode 100644
index 00000000000..1970c05c9b6
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query39.q
@@ -0,0 +1,56 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query39.tpl and seed 1327317894
+explain cbo
+with inv as
+(select w_warehouse_name,w_warehouse_sk,i_item_sk,d_moy
+       ,stdev,mean, case mean when 0 then null else stdev/mean end cov
+ from(select w_warehouse_name,w_warehouse_sk,i_item_sk,d_moy
+            ,stddev_samp(inv_quantity_on_hand) stdev,avg(inv_quantity_on_hand) mean
+      from inventory
+          ,item
+          ,warehouse
+          ,date_dim
+      where inv_item_sk = i_item_sk
+        and inv_warehouse_sk = w_warehouse_sk
+        and inv_date_sk = d_date_sk
+        and d_year =1999
+      group by w_warehouse_name,w_warehouse_sk,i_item_sk,d_moy) foo
+ where case mean when 0 then 0 else stdev/mean end > 1)
+select inv1.w_warehouse_sk,inv1.i_item_sk,inv1.d_moy,inv1.mean, inv1.cov
+        ,inv2.w_warehouse_sk,inv2.i_item_sk,inv2.d_moy,inv2.mean, inv2.cov
+from inv inv1,inv inv2
+where inv1.i_item_sk = inv2.i_item_sk
+  and inv1.w_warehouse_sk =  inv2.w_warehouse_sk
+  and inv1.d_moy=4
+  and inv2.d_moy=4+1
+order by inv1.w_warehouse_sk,inv1.i_item_sk,inv1.d_moy,inv1.mean,inv1.cov
+        ,inv2.d_moy,inv2.mean, inv2.cov
+;
+with inv as
+(select w_warehouse_name,w_warehouse_sk,i_item_sk,d_moy
+       ,stdev,mean, case mean when 0 then null else stdev/mean end cov
+ from(select w_warehouse_name,w_warehouse_sk,i_item_sk,d_moy
+            ,stddev_samp(inv_quantity_on_hand) stdev,avg(inv_quantity_on_hand) mean
+      from inventory
+          ,item
+          ,warehouse
+          ,date_dim
+      where inv_item_sk = i_item_sk
+        and inv_warehouse_sk = w_warehouse_sk
+        and inv_date_sk = d_date_sk
+        and d_year =1999
+      group by w_warehouse_name,w_warehouse_sk,i_item_sk,d_moy) foo
+ where case mean when 0 then 0 else stdev/mean end > 1)
+select inv1.w_warehouse_sk,inv1.i_item_sk,inv1.d_moy,inv1.mean, inv1.cov
+        ,inv2.w_warehouse_sk,inv2.i_item_sk,inv2.d_moy,inv2.mean, inv2.cov
+from inv inv1,inv inv2
+where inv1.i_item_sk = inv2.i_item_sk
+  and inv1.w_warehouse_sk =  inv2.w_warehouse_sk
+  and inv1.d_moy=4
+  and inv2.d_moy=4+1
+  and inv1.cov > 1.5
+order by inv1.w_warehouse_sk,inv1.i_item_sk,inv1.d_moy,inv1.mean,inv1.cov
+        ,inv2.d_moy,inv2.mean, inv2.cov
+;
+
+-- end query 1 in stream 0 using template query39.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query4.q b/ql/src/test/queries/clientpositive/perf/cbo_query4.q
new file mode 100644
index 00000000000..b47d1743311
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query4.q
@@ -0,0 +1,111 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query4.tpl and seed 1819994127
+explain cbo
+with year_total as (
+ select c_customer_id customer_id
+       ,c_first_name customer_first_name
+       ,c_last_name customer_last_name
+       ,c_preferred_cust_flag customer_preferred_cust_flag
+       ,c_birth_country customer_birth_country
+       ,c_login customer_login
+       ,c_email_address customer_email_address
+       ,d_year dyear
+       ,sum(((ss_ext_list_price-ss_ext_wholesale_cost-ss_ext_discount_amt)+ss_ext_sales_price)/2) year_total
+       ,'s' sale_type
+ from customer
+     ,store_sales
+     ,date_dim
+ where c_customer_sk = ss_customer_sk
+   and ss_sold_date_sk = d_date_sk
+ group by c_customer_id
+         ,c_first_name
+         ,c_last_name
+         ,c_preferred_cust_flag
+         ,c_birth_country
+         ,c_login
+         ,c_email_address
+         ,d_year
+ union all
+ select c_customer_id customer_id
+       ,c_first_name customer_first_name
+       ,c_last_name customer_last_name
+       ,c_preferred_cust_flag customer_preferred_cust_flag
+       ,c_birth_country customer_birth_country
+       ,c_login customer_login
+       ,c_email_address customer_email_address
+       ,d_year dyear
+       ,sum((((cs_ext_list_price-cs_ext_wholesale_cost-cs_ext_discount_amt)+cs_ext_sales_price)/2) ) year_total
+       ,'c' sale_type
+ from customer
+     ,catalog_sales
+     ,date_dim
+ where c_customer_sk = cs_bill_customer_sk
+   and cs_sold_date_sk = d_date_sk
+ group by c_customer_id
+         ,c_first_name
+         ,c_last_name
+         ,c_preferred_cust_flag
+         ,c_birth_country
+         ,c_login
+         ,c_email_address
+         ,d_year
+union all
+ select c_customer_id customer_id
+       ,c_first_name customer_first_name
+       ,c_last_name customer_last_name
+       ,c_preferred_cust_flag customer_preferred_cust_flag
+       ,c_birth_country customer_birth_country
+       ,c_login customer_login
+       ,c_email_address customer_email_address
+       ,d_year dyear
+       ,sum((((ws_ext_list_price-ws_ext_wholesale_cost-ws_ext_discount_amt)+ws_ext_sales_price)/2) ) year_total
+       ,'w' sale_type
+ from customer
+     ,web_sales
+     ,date_dim
+ where c_customer_sk = ws_bill_customer_sk
+   and ws_sold_date_sk = d_date_sk
+ group by c_customer_id
+         ,c_first_name
+         ,c_last_name
+         ,c_preferred_cust_flag
+         ,c_birth_country
+         ,c_login
+         ,c_email_address
+         ,d_year
+         )
+  select  t_s_secyear.customer_preferred_cust_flag
+ from year_total t_s_firstyear
+     ,year_total t_s_secyear
+     ,year_total t_c_firstyear
+     ,year_total t_c_secyear
+     ,year_total t_w_firstyear
+     ,year_total t_w_secyear
+ where t_s_secyear.customer_id = t_s_firstyear.customer_id
+   and t_s_firstyear.customer_id = t_c_secyear.customer_id
+   and t_s_firstyear.customer_id = t_c_firstyear.customer_id
+   and t_s_firstyear.customer_id = t_w_firstyear.customer_id
+   and t_s_firstyear.customer_id = t_w_secyear.customer_id
+   and t_s_firstyear.sale_type = 's'
+   and t_c_firstyear.sale_type = 'c'
+   and t_w_firstyear.sale_type = 'w'
+   and t_s_secyear.sale_type = 's'
+   and t_c_secyear.sale_type = 'c'
+   and t_w_secyear.sale_type = 'w'
+   and t_s_firstyear.dyear =  2001
+   and t_s_secyear.dyear = 2001+1
+   and t_c_firstyear.dyear =  2001
+   and t_c_secyear.dyear =  2001+1
+   and t_w_firstyear.dyear = 2001
+   and t_w_secyear.dyear = 2001+1
+   and t_s_firstyear.year_total > 0
+   and t_c_firstyear.year_total > 0
+   and t_w_firstyear.year_total > 0
+   and case when t_c_firstyear.year_total > 0 then t_c_secyear.year_total / t_c_firstyear.year_total else null end
+           > case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else null end
+   and case when t_c_firstyear.year_total > 0 then t_c_secyear.year_total / t_c_firstyear.year_total else null end
+           > case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else null end
+ order by t_s_secyear.customer_preferred_cust_flag
+limit 100;
+
+-- end query 1 in stream 0 using template query4.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query40.q b/ql/src/test/queries/clientpositive/perf/cbo_query40.q
new file mode 100644
index 00000000000..b54ae4b39e9
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query40.q
@@ -0,0 +1,30 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query40.tpl and seed 1819994127
+explain cbo
+select  
+   w_state
+  ,i_item_id
+  ,sum(case when (cast(d_date as date) < cast ('1998-04-08' as date)) 
+ 		then cs_sales_price - coalesce(cr_refunded_cash,0) else 0 end) as sales_before
+  ,sum(case when (cast(d_date as date) >= cast ('1998-04-08' as date)) 
+ 		then cs_sales_price - coalesce(cr_refunded_cash,0) else 0 end) as sales_after
+ from
+   catalog_sales left outer join catalog_returns on
+       (cs_order_number = cr_order_number 
+        and cs_item_sk = cr_item_sk)
+  ,warehouse 
+  ,item
+  ,date_dim
+ where
+     i_current_price between 0.99 and 1.49
+ and i_item_sk          = cs_item_sk
+ and cs_warehouse_sk    = w_warehouse_sk 
+ and cs_sold_date_sk    = d_date_sk
+ and d_date between (cast ('1998-04-08' as date) - 30 days)
+                and (cast ('1998-04-08' as date) + 30 days) 
+ group by
+    w_state,i_item_id
+ order by w_state,i_item_id
+limit 100;
+
+-- end query 1 in stream 0 using template query40.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query42.q b/ql/src/test/queries/clientpositive/perf/cbo_query42.q
new file mode 100644
index 00000000000..43fb4ee6255
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query42.q
@@ -0,0 +1,24 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query42.tpl and seed 1819994127
+explain cbo
+select  dt.d_year
+ 	,item.i_category_id
+ 	,item.i_category
+ 	,sum(ss_ext_sales_price)
+ from 	date_dim dt
+ 	,store_sales
+ 	,item
+ where dt.d_date_sk = store_sales.ss_sold_date_sk
+ 	and store_sales.ss_item_sk = item.i_item_sk
+ 	and item.i_manager_id = 1  	
+ 	and dt.d_moy=12
+ 	and dt.d_year=1998
+ group by 	dt.d_year
+ 		,item.i_category_id
+ 		,item.i_category
+ order by       sum(ss_ext_sales_price) desc,dt.d_year
+ 		,item.i_category_id
+ 		,item.i_category
+limit 100 ;
+
+-- end query 1 in stream 0 using template query42.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query43.q b/ql/src/test/queries/clientpositive/perf/cbo_query43.q
new file mode 100644
index 00000000000..e4aff114bee
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query43.q
@@ -0,0 +1,21 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query43.tpl and seed 1819994127
+explain cbo
+select  s_store_name, s_store_id,
+        sum(case when (d_day_name='Sunday') then ss_sales_price else null end) sun_sales,
+        sum(case when (d_day_name='Monday') then ss_sales_price else null end) mon_sales,
+        sum(case when (d_day_name='Tuesday') then ss_sales_price else  null end) tue_sales,
+        sum(case when (d_day_name='Wednesday') then ss_sales_price else null end) wed_sales,
+        sum(case when (d_day_name='Thursday') then ss_sales_price else null end) thu_sales,
+        sum(case when (d_day_name='Friday') then ss_sales_price else null end) fri_sales,
+        sum(case when (d_day_name='Saturday') then ss_sales_price else null end) sat_sales
+ from date_dim, store_sales, store
+ where d_date_sk = ss_sold_date_sk and
+       s_store_sk = ss_store_sk and
+       s_gmt_offset = -6 and
+       d_year = 1998 
+ group by s_store_name, s_store_id
+ order by s_store_name, s_store_id,sun_sales,mon_sales,tue_sales,wed_sales,thu_sales,fri_sales,sat_sales
+ limit 100;
+
+-- end query 1 in stream 0 using template query43.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query44.q b/ql/src/test/queries/clientpositive/perf/cbo_query44.q
new file mode 100644
index 00000000000..814a7213300
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query44.q
@@ -0,0 +1,37 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query44.tpl and seed 1819994127
+explain cbo
+select  asceding.rnk, i1.i_product_name best_performing, i2.i_product_name worst_performing
+from(select *
+     from (select item_sk,rank() over (order by rank_col asc) rnk
+           from (select ss_item_sk item_sk,avg(ss_net_profit) rank_col 
+                 from store_sales ss1
+                 where ss_store_sk = 410
+                 group by ss_item_sk
+                 having avg(ss_net_profit) > 0.9*(select avg(ss_net_profit) rank_col
+                                                  from store_sales
+                                                  where ss_store_sk = 410
+                                                    and ss_hdemo_sk is null
+                                                  group by ss_store_sk))V1)V11
+     where rnk  < 11) asceding,
+    (select *
+     from (select item_sk,rank() over (order by rank_col desc) rnk
+           from (select ss_item_sk item_sk,avg(ss_net_profit) rank_col
+                 from store_sales ss1
+                 where ss_store_sk = 410
+                 group by ss_item_sk
+                 having avg(ss_net_profit) > 0.9*(select avg(ss_net_profit) rank_col
+                                                  from store_sales
+                                                  where ss_store_sk = 410
+                                                    and ss_hdemo_sk is null
+                                                  group by ss_store_sk))V2)V21
+     where rnk  < 11) descending,
+item i1,
+item i2
+where asceding.rnk = descending.rnk 
+  and i1.i_item_sk=asceding.item_sk
+  and i2.i_item_sk=descending.item_sk
+order by asceding.rnk
+limit 100;
+
+-- end query 1 in stream 0 using template query44.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query45.q b/ql/src/test/queries/clientpositive/perf/cbo_query45.q
new file mode 100644
index 00000000000..e1b4348b7ad
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query45.q
@@ -0,0 +1,22 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query45.tpl and seed 2031708268
+explain cbo
+select  ca_zip, ca_county, sum(ws_sales_price)
+ from web_sales, customer, customer_address, date_dim, item
+ where ws_bill_customer_sk = c_customer_sk
+ 	and c_current_addr_sk = ca_address_sk 
+ 	and ws_item_sk = i_item_sk 
+ 	and ( substr(ca_zip,1,5) in ('85669', '86197','88274','83405','86475', '85392', '85460', '80348', '81792')
+ 	      or 
+ 	      i_item_id in (select i_item_id
+                             from item
+                             where i_item_sk in (2, 3, 5, 7, 11, 13, 17, 19, 23, 29)
+                             )
+ 	    )
+ 	and ws_sold_date_sk = d_date_sk
+ 	and d_qoy = 2 and d_year = 2000
+ group by ca_zip, ca_county
+ order by ca_zip, ca_county
+ limit 100;
+
+-- end query 1 in stream 0 using template query45.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query46.q b/ql/src/test/queries/clientpositive/perf/cbo_query46.q
new file mode 100644
index 00000000000..6a7a94ee6f5
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query46.q
@@ -0,0 +1,37 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query46.tpl and seed 803547492
+explain cbo
+select  c_last_name
+       ,c_first_name
+       ,ca_city
+       ,bought_city
+       ,ss_ticket_number
+       ,amt,profit 
+ from
+   (select ss_ticket_number
+          ,ss_customer_sk
+          ,ca_city bought_city
+          ,sum(ss_coupon_amt) amt
+          ,sum(ss_net_profit) profit
+    from store_sales,date_dim,store,household_demographics,customer_address 
+    where store_sales.ss_sold_date_sk = date_dim.d_date_sk
+    and store_sales.ss_store_sk = store.s_store_sk  
+    and store_sales.ss_hdemo_sk = household_demographics.hd_demo_sk
+    and store_sales.ss_addr_sk = customer_address.ca_address_sk
+    and (household_demographics.hd_dep_count = 2 or
+         household_demographics.hd_vehicle_count= 1)
+    and date_dim.d_dow in (6,0)
+    and date_dim.d_year in (1998,1998+1,1998+2) 
+    and store.s_city in ('Cedar Grove','Wildwood','Union','Salem','Highland Park') 
+    group by ss_ticket_number,ss_customer_sk,ss_addr_sk,ca_city) dn,customer,customer_address current_addr
+    where ss_customer_sk = c_customer_sk
+      and customer.c_current_addr_sk = current_addr.ca_address_sk
+      and current_addr.ca_city <> bought_city
+  order by c_last_name
+          ,c_first_name
+          ,ca_city
+          ,bought_city
+          ,ss_ticket_number
+  limit 100;
+
+-- end query 1 in stream 0 using template query46.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query47.q b/ql/src/test/queries/clientpositive/perf/cbo_query47.q
new file mode 100644
index 00000000000..7348e4b6e74
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query47.q
@@ -0,0 +1,53 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query47.tpl and seed 2031708268
+explain cbo
+with v1 as(
+ select i_category, i_brand,
+        s_store_name, s_company_name,
+        d_year, d_moy,
+        sum(ss_sales_price) sum_sales,
+        avg(sum(ss_sales_price)) over
+          (partition by i_category, i_brand,
+                     s_store_name, s_company_name, d_year)
+          avg_monthly_sales,
+        rank() over
+          (partition by i_category, i_brand,
+                     s_store_name, s_company_name
+           order by d_year, d_moy) rn
+ from item, store_sales, date_dim, store
+ where ss_item_sk = i_item_sk and
+       ss_sold_date_sk = d_date_sk and
+       ss_store_sk = s_store_sk and
+       (
+         d_year = 2000 or
+         ( d_year = 2000-1 and d_moy =12) or
+         ( d_year = 2000+1 and d_moy =1)
+       )
+ group by i_category, i_brand,
+          s_store_name, s_company_name,
+          d_year, d_moy),
+ v2 as(
+ select v1.i_category
+        ,v1.d_year, v1.d_moy
+        ,v1.avg_monthly_sales
+        ,v1.sum_sales, v1_lag.sum_sales psum, v1_lead.sum_sales nsum
+ from v1, v1 v1_lag, v1 v1_lead
+ where v1.i_category = v1_lag.i_category and
+       v1.i_category = v1_lead.i_category and
+       v1.i_brand = v1_lag.i_brand and
+       v1.i_brand = v1_lead.i_brand and
+       v1.s_store_name = v1_lag.s_store_name and
+       v1.s_store_name = v1_lead.s_store_name and
+       v1.s_company_name = v1_lag.s_company_name and
+       v1.s_company_name = v1_lead.s_company_name and
+       v1.rn = v1_lag.rn + 1 and
+       v1.rn = v1_lead.rn - 1)
+  select  *
+ from v2
+ where  d_year = 2000 and    
+        avg_monthly_sales > 0 and
+        case when avg_monthly_sales > 0 then abs(sum_sales - avg_monthly_sales) / avg_monthly_sales else null end > 0.1
+ order by sum_sales - avg_monthly_sales, 3
+ limit 100;
+
+-- end query 1 in stream 0 using template query47.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query48.q b/ql/src/test/queries/clientpositive/perf/cbo_query48.q
new file mode 100644
index 00000000000..21db45fc310
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query48.q
@@ -0,0 +1,69 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query48.tpl and seed 622697896
+explain cbo
+select sum (ss_quantity)
+ from store_sales, store, customer_demographics, customer_address, date_dim
+ where s_store_sk = ss_store_sk
+ and  ss_sold_date_sk = d_date_sk and d_year = 1998
+ and  
+ (
+  (
+   cd_demo_sk = ss_cdemo_sk
+   and 
+   cd_marital_status = 'M'
+   and 
+   cd_education_status = '4 yr Degree'
+   and 
+   ss_sales_price between 100.00 and 150.00  
+   )
+ or
+  (
+  cd_demo_sk = ss_cdemo_sk
+   and 
+   cd_marital_status = 'M'
+   and 
+   cd_education_status = '4 yr Degree'
+   and 
+   ss_sales_price between 50.00 and 100.00   
+  )
+ or 
+ (
+  cd_demo_sk = ss_cdemo_sk
+  and 
+   cd_marital_status = 'M'
+   and 
+   cd_education_status = '4 yr Degree'
+   and 
+   ss_sales_price between 150.00 and 200.00  
+ )
+ )
+ and
+ (
+  (
+  ss_addr_sk = ca_address_sk
+  and
+  ca_country = 'United States'
+  and
+  ca_state in ('KY', 'GA', 'NM')
+  and ss_net_profit between 0 and 2000  
+  )
+ or
+  (ss_addr_sk = ca_address_sk
+  and
+  ca_country = 'United States'
+  and
+  ca_state in ('MT', 'OR', 'IN')
+  and ss_net_profit between 150 and 3000 
+  )
+ or
+  (ss_addr_sk = ca_address_sk
+  and
+  ca_country = 'United States'
+  and
+  ca_state in ('WI', 'MO', 'WV')
+  and ss_net_profit between 50 and 25000 
+  )
+ )
+;
+
+-- end query 1 in stream 0 using template query48.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query49.q b/ql/src/test/queries/clientpositive/perf/cbo_query49.q
new file mode 100644
index 00000000000..fccf25d0009
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query49.q
@@ -0,0 +1,129 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query49.tpl and seed 1819994127
+explain cbo
+select  
+ 'web' as channel
+ ,web.item
+ ,web.return_ratio
+ ,web.return_rank
+ ,web.currency_rank
+ from (
+ 	select 
+ 	 item
+ 	,return_ratio
+ 	,currency_ratio
+ 	,rank() over (order by return_ratio) as return_rank
+ 	,rank() over (order by currency_ratio) as currency_rank
+ 	from
+ 	(	select ws.ws_item_sk as item
+ 		,(cast(sum(coalesce(wr.wr_return_quantity,0)) as dec(15,4))/
+ 		cast(sum(coalesce(ws.ws_quantity,0)) as dec(15,4) )) as return_ratio
+ 		,(cast(sum(coalesce(wr.wr_return_amt,0)) as dec(15,4))/
+ 		cast(sum(coalesce(ws.ws_net_paid,0)) as dec(15,4) )) as currency_ratio
+ 		from 
+ 		 web_sales ws left outer join web_returns wr 
+ 			on (ws.ws_order_number = wr.wr_order_number and 
+ 			ws.ws_item_sk = wr.wr_item_sk)
+                 ,date_dim
+ 		where 
+ 			wr.wr_return_amt > 10000 
+ 			and ws.ws_net_profit > 1
+                         and ws.ws_net_paid > 0
+                         and ws.ws_quantity > 0
+                         and ws_sold_date_sk = d_date_sk
+                         and d_year = 2000
+                         and d_moy = 12
+ 		group by ws.ws_item_sk
+ 	) in_web
+ ) web
+ where 
+ (
+ web.return_rank <= 10
+ or
+ web.currency_rank <= 10
+ )
+ union
+ select 
+ 'catalog' as channel
+ ,catalog.item
+ ,catalog.return_ratio
+ ,catalog.return_rank
+ ,catalog.currency_rank
+ from (
+ 	select 
+ 	 item
+ 	,return_ratio
+ 	,currency_ratio
+ 	,rank() over (order by return_ratio) as return_rank
+ 	,rank() over (order by currency_ratio) as currency_rank
+ 	from
+ 	(	select 
+ 		cs.cs_item_sk as item
+ 		,(cast(sum(coalesce(cr.cr_return_quantity,0)) as dec(15,4))/
+ 		cast(sum(coalesce(cs.cs_quantity,0)) as dec(15,4) )) as return_ratio
+ 		,(cast(sum(coalesce(cr.cr_return_amount,0)) as dec(15,4))/
+ 		cast(sum(coalesce(cs.cs_net_paid,0)) as dec(15,4) )) as currency_ratio
+ 		from 
+ 		catalog_sales cs left outer join catalog_returns cr
+ 			on (cs.cs_order_number = cr.cr_order_number and 
+ 			cs.cs_item_sk = cr.cr_item_sk)
+                ,date_dim
+ 		where 
+ 			cr.cr_return_amount > 10000 
+ 			and cs.cs_net_profit > 1
+                         and cs.cs_net_paid > 0
+                         and cs.cs_quantity > 0
+                         and cs_sold_date_sk = d_date_sk
+                         and d_year = 2000
+                         and d_moy = 12
+                 group by cs.cs_item_sk
+ 	) in_cat
+ ) catalog
+ where 
+ (
+ catalog.return_rank <= 10
+ or
+ catalog.currency_rank <=10
+ )
+ union
+ select 
+ 'store' as channel
+ ,store.item
+ ,store.return_ratio
+ ,store.return_rank
+ ,store.currency_rank
+ from (
+ 	select 
+ 	 item
+ 	,return_ratio
+ 	,currency_ratio
+ 	,rank() over (order by return_ratio) as return_rank
+ 	,rank() over (order by currency_ratio) as currency_rank
+ 	from
+ 	(	select sts.ss_item_sk as item
+ 		,(cast(sum(coalesce(sr.sr_return_quantity,0)) as dec(15,4))/cast(sum(coalesce(sts.ss_quantity,0)) as dec(15,4) )) as return_ratio
+ 		,(cast(sum(coalesce(sr.sr_return_amt,0)) as dec(15,4))/cast(sum(coalesce(sts.ss_net_paid,0)) as dec(15,4) )) as currency_ratio
+ 		from 
+ 		store_sales sts left outer join store_returns sr
+ 			on (sts.ss_ticket_number = sr.sr_ticket_number and sts.ss_item_sk = sr.sr_item_sk)
+                ,date_dim
+ 		where 
+ 			sr.sr_return_amt > 10000 
+ 			and sts.ss_net_profit > 1
+                         and sts.ss_net_paid > 0 
+                         and sts.ss_quantity > 0
+                         and ss_sold_date_sk = d_date_sk
+                         and d_year = 2000
+                         and d_moy = 12
+ 		group by sts.ss_item_sk
+ 	) in_store
+ ) store
+ where  (
+ store.return_rank <= 10
+ or 
+ store.currency_rank <= 10
+ )
+ order by 1,4,5
+ limit 100;
+
+-- end query 1 in stream 0 using template query49.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query5.q b/ql/src/test/queries/clientpositive/perf/cbo_query5.q
new file mode 100644
index 00000000000..405b2da571b
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query5.q
@@ -0,0 +1,130 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query5.tpl and seed 1819994127
+explain cbo
+with ssr as
+ (select s_store_id,
+        sum(sales_price) as sales,
+        sum(profit) as profit,
+        sum(return_amt) as returns,
+        sum(net_loss) as profit_loss
+ from
+  ( select  ss_store_sk as store_sk,
+            ss_sold_date_sk  as date_sk,
+            ss_ext_sales_price as sales_price,
+            ss_net_profit as profit,
+            cast(0 as decimal(7,2)) as return_amt,
+            cast(0 as decimal(7,2)) as net_loss
+    from store_sales
+    union all
+    select sr_store_sk as store_sk,
+           sr_returned_date_sk as date_sk,
+           cast(0 as decimal(7,2)) as sales_price,
+           cast(0 as decimal(7,2)) as profit,
+           sr_return_amt as return_amt,
+           sr_net_loss as net_loss
+    from store_returns
+   ) salesreturns,
+     date_dim,
+     store
+ where date_sk = d_date_sk
+       and d_date between cast('1998-08-04' as date) 
+                  and (cast('1998-08-04' as date) +  14 days)
+       and store_sk = s_store_sk
+ group by s_store_id)
+ ,
+ csr as
+ (select cp_catalog_page_id,
+        sum(sales_price) as sales,
+        sum(profit) as profit,
+        sum(return_amt) as returns,
+        sum(net_loss) as profit_loss
+ from
+  ( select  cs_catalog_page_sk as page_sk,
+            cs_sold_date_sk  as date_sk,
+            cs_ext_sales_price as sales_price,
+            cs_net_profit as profit,
+            cast(0 as decimal(7,2)) as return_amt,
+            cast(0 as decimal(7,2)) as net_loss
+    from catalog_sales
+    union all
+    select cr_catalog_page_sk as page_sk,
+           cr_returned_date_sk as date_sk,
+           cast(0 as decimal(7,2)) as sales_price,
+           cast(0 as decimal(7,2)) as profit,
+           cr_return_amount as return_amt,
+           cr_net_loss as net_loss
+    from catalog_returns
+   ) salesreturns,
+     date_dim,
+     catalog_page
+ where date_sk = d_date_sk
+       and d_date between cast('1998-08-04' as date)
+                  and (cast('1998-08-04' as date) +  14 days)
+       and page_sk = cp_catalog_page_sk
+ group by cp_catalog_page_id)
+ ,
+ wsr as
+ (select web_site_id,
+        sum(sales_price) as sales,
+        sum(profit) as profit,
+        sum(return_amt) as returns,
+        sum(net_loss) as profit_loss
+ from
+  ( select  ws_web_site_sk as wsr_web_site_sk,
+            ws_sold_date_sk  as date_sk,
+            ws_ext_sales_price as sales_price,
+            ws_net_profit as profit,
+            cast(0 as decimal(7,2)) as return_amt,
+            cast(0 as decimal(7,2)) as net_loss
+    from web_sales
+    union all
+    select ws_web_site_sk as wsr_web_site_sk,
+           wr_returned_date_sk as date_sk,
+           cast(0 as decimal(7,2)) as sales_price,
+           cast(0 as decimal(7,2)) as profit,
+           wr_return_amt as return_amt,
+           wr_net_loss as net_loss
+    from web_returns left outer join web_sales on
+         ( wr_item_sk = ws_item_sk
+           and wr_order_number = ws_order_number)
+   ) salesreturns,
+     date_dim,
+     web_site
+ where date_sk = d_date_sk
+       and d_date between cast('1998-08-04' as date)
+                  and (cast('1998-08-04' as date) +  14 days)
+       and wsr_web_site_sk = web_site_sk
+ group by web_site_id)
+  select  channel
+        , id
+        , sum(sales) as sales
+        , sum(returns) as returns
+        , sum(profit) as profit
+ from 
+ (select 'store channel' as channel
+        , 'store' || s_store_id as id
+        , sales
+        , returns
+        , (profit - profit_loss) as profit
+ from   ssr
+ union all
+ select 'catalog channel' as channel
+        , 'catalog_page' || cp_catalog_page_id as id
+        , sales
+        , returns
+        , (profit - profit_loss) as profit
+ from  csr
+ union all
+ select 'web channel' as channel
+        , 'web_site' || web_site_id as id
+        , sales
+        , returns
+        , (profit - profit_loss) as profit
+ from   wsr
+ ) x
+ group by rollup (channel, id)
+ order by channel
+         ,id
+ limit 100;
+
+-- end query 1 in stream 0 using template query5.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query50.q b/ql/src/test/queries/clientpositive/perf/cbo_query50.q
new file mode 100644
index 00000000000..1fb8051080f
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query50.q
@@ -0,0 +1,61 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query50.tpl and seed 1819994127
+explain cbo
+select  
+   s_store_name
+  ,s_company_id
+  ,s_street_number
+  ,s_street_name
+  ,s_street_type
+  ,s_suite_number
+  ,s_city
+  ,s_county
+  ,s_state
+  ,s_zip
+  ,sum(case when (sr_returned_date_sk - ss_sold_date_sk <= 30 ) then 1 else 0 end)  as `30 days` 
+  ,sum(case when (sr_returned_date_sk - ss_sold_date_sk > 30) and 
+                 (sr_returned_date_sk - ss_sold_date_sk <= 60) then 1 else 0 end )  as `31-60 days` 
+  ,sum(case when (sr_returned_date_sk - ss_sold_date_sk > 60) and 
+                 (sr_returned_date_sk - ss_sold_date_sk <= 90) then 1 else 0 end)  as `61-90 days` 
+  ,sum(case when (sr_returned_date_sk - ss_sold_date_sk > 90) and
+                 (sr_returned_date_sk - ss_sold_date_sk <= 120) then 1 else 0 end)  as `91-120 days` 
+  ,sum(case when (sr_returned_date_sk - ss_sold_date_sk  > 120) then 1 else 0 end)  as `>120 days` 
+from
+   store_sales
+  ,store_returns
+  ,store
+  ,date_dim d1
+  ,date_dim d2
+where
+    d2.d_year = 2000
+and d2.d_moy  = 9
+and ss_ticket_number = sr_ticket_number
+and ss_item_sk = sr_item_sk
+and ss_sold_date_sk   = d1.d_date_sk
+and sr_returned_date_sk   = d2.d_date_sk
+and ss_customer_sk = sr_customer_sk
+and ss_store_sk = s_store_sk
+group by
+   s_store_name
+  ,s_company_id
+  ,s_street_number
+  ,s_street_name
+  ,s_street_type
+  ,s_suite_number
+  ,s_city
+  ,s_county
+  ,s_state
+  ,s_zip
+order by s_store_name
+        ,s_company_id
+        ,s_street_number
+        ,s_street_name
+        ,s_street_type
+        ,s_suite_number
+        ,s_city
+        ,s_county
+        ,s_state
+        ,s_zip
+limit 100;
+
+-- end query 1 in stream 0 using template query50.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query51.q b/ql/src/test/queries/clientpositive/perf/cbo_query51.q
new file mode 100644
index 00000000000..16ba023f49f
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query51.q
@@ -0,0 +1,47 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query51.tpl and seed 1819994127
+explain cbo
+WITH web_v1 as (
+select
+  ws_item_sk item_sk, d_date,
+  sum(sum(ws_sales_price))
+      over (partition by ws_item_sk order by d_date rows between unbounded preceding and current row) cume_sales
+from web_sales
+    ,date_dim
+where ws_sold_date_sk=d_date_sk
+  and d_month_seq between 1212 and 1212+11
+  and ws_item_sk is not NULL
+group by ws_item_sk, d_date),
+store_v1 as (
+select
+  ss_item_sk item_sk, d_date,
+  sum(sum(ss_sales_price))
+      over (partition by ss_item_sk order by d_date rows between unbounded preceding and current row) cume_sales
+from store_sales
+    ,date_dim
+where ss_sold_date_sk=d_date_sk
+  and d_month_seq between 1212 and 1212+11
+  and ss_item_sk is not NULL
+group by ss_item_sk, d_date)
+ select  *
+from (select item_sk
+     ,d_date
+     ,web_sales
+     ,store_sales
+     ,max(web_sales)
+         over (partition by item_sk order by d_date rows between unbounded preceding and current row) web_cumulative
+     ,max(store_sales)
+         over (partition by item_sk order by d_date rows between unbounded preceding and current row) store_cumulative
+     from (select case when web.item_sk is not null then web.item_sk else store.item_sk end item_sk
+                 ,case when web.d_date is not null then web.d_date else store.d_date end d_date
+                 ,web.cume_sales web_sales
+                 ,store.cume_sales store_sales
+           from web_v1 web full outer join store_v1 store on (web.item_sk = store.item_sk
+                                                          and web.d_date = store.d_date)
+          )x )y
+where web_cumulative > store_cumulative
+order by item_sk
+        ,d_date
+limit 100;
+
+-- end query 1 in stream 0 using template query51.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query52.q b/ql/src/test/queries/clientpositive/perf/cbo_query52.q
new file mode 100644
index 00000000000..222550240b7
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query52.q
@@ -0,0 +1,24 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query52.tpl and seed 1819994127
+explain cbo
+select  dt.d_year
+ 	,item.i_brand_id brand_id
+ 	,item.i_brand brand
+ 	,sum(ss_ext_sales_price) ext_price
+ from date_dim dt
+     ,store_sales
+     ,item
+ where dt.d_date_sk = store_sales.ss_sold_date_sk
+    and store_sales.ss_item_sk = item.i_item_sk
+    and item.i_manager_id = 1
+    and dt.d_moy=12
+    and dt.d_year=1998
+ group by dt.d_year
+ 	,item.i_brand
+ 	,item.i_brand_id
+ order by dt.d_year
+ 	,ext_price desc
+ 	,brand_id
+limit 100 ;
+
+-- end query 1 in stream 0 using template query52.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query53.q b/ql/src/test/queries/clientpositive/perf/cbo_query53.q
new file mode 100644
index 00000000000..c0edd67c957
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query53.q
@@ -0,0 +1,30 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query53.tpl and seed 1819994127
+explain cbo
+select  * from 
+(select i_manufact_id,
+sum(ss_sales_price) sum_sales,
+avg(sum(ss_sales_price)) over (partition by i_manufact_id) avg_quarterly_sales
+from item, store_sales, date_dim, store
+where ss_item_sk = i_item_sk and
+ss_sold_date_sk = d_date_sk and
+ss_store_sk = s_store_sk and
+d_month_seq in (1212,1212+1,1212+2,1212+3,1212+4,1212+5,1212+6,1212+7,1212+8,1212+9,1212+10,1212+11) and
+((i_category in ('Books','Children','Electronics') and
+i_class in ('personal','portable','reference','self-help') and
+i_brand in ('scholaramalgamalg #14','scholaramalgamalg #7',
+		'exportiunivamalg #9','scholaramalgamalg #9'))
+or(i_category in ('Women','Music','Men') and
+i_class in ('accessories','classical','fragrances','pants') and
+i_brand in ('amalgimporto #1','edu packscholar #1','exportiimporto #1',
+		'importoamalg #1')))
+group by i_manufact_id, d_qoy ) tmp1
+where case when avg_quarterly_sales > 0 
+	then abs (sum_sales - avg_quarterly_sales)/ avg_quarterly_sales 
+	else null end > 0.1
+order by avg_quarterly_sales,
+	 sum_sales,
+	 i_manufact_id
+limit 100;
+
+-- end query 1 in stream 0 using template query53.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query54.q b/ql/src/test/queries/clientpositive/perf/cbo_query54.q
new file mode 100644
index 00000000000..1e626c155d8
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query54.q
@@ -0,0 +1,58 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query54.tpl and seed 1930872976
+explain cbo
+with my_customers as (
+ select distinct c_customer_sk
+        , c_current_addr_sk
+ from   
+        ( select cs_sold_date_sk sold_date_sk,
+                 cs_bill_customer_sk customer_sk,
+                 cs_item_sk item_sk
+          from   catalog_sales
+          union all
+          select ws_sold_date_sk sold_date_sk,
+                 ws_bill_customer_sk customer_sk,
+                 ws_item_sk item_sk
+          from   web_sales
+         ) cs_or_ws_sales,
+         item,
+         date_dim,
+         customer
+ where   sold_date_sk = d_date_sk
+         and item_sk = i_item_sk
+         and i_category = 'Jewelry'
+         and i_class = 'consignment'
+         and c_customer_sk = cs_or_ws_sales.customer_sk
+         and d_moy = 3
+         and d_year = 1999
+ )
+ , my_revenue as (
+ select c_customer_sk,
+        sum(ss_ext_sales_price) as revenue
+ from   my_customers,
+        store_sales,
+        customer_address,
+        store,
+        date_dim
+ where  c_current_addr_sk = ca_address_sk
+        and ca_county = s_county
+        and ca_state = s_state
+        and ss_sold_date_sk = d_date_sk
+        and c_customer_sk = ss_customer_sk
+        and d_month_seq between (select distinct d_month_seq+1
+                                 from   date_dim where d_year = 1999 and d_moy = 3)
+                           and  (select distinct d_month_seq+3
+                                 from   date_dim where d_year = 1999 and d_moy = 3)
+ group by c_customer_sk
+ )
+ , segments as
+ (select cast((revenue/50) as int) as segment
+  from   my_revenue
+ )
+  select  segment, count(*) as num_customers, segment*50 as segment_base
+ from segments
+ group by segment
+ order by segment, num_customers
+ limit 100;
+
+-- end query 1 in stream 0 using template query54.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query55.q b/ql/src/test/queries/clientpositive/perf/cbo_query55.q
new file mode 100644
index 00000000000..55176b2e33d
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query55.q
@@ -0,0 +1,16 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query55.tpl and seed 2031708268
+explain cbo
+select  i_brand_id brand_id, i_brand brand,
+ 	sum(ss_ext_sales_price) ext_price
+ from date_dim, store_sales, item
+ where d_date_sk = ss_sold_date_sk
+ 	and ss_item_sk = i_item_sk
+ 	and i_manager_id=36
+ 	and d_moy=12
+ 	and d_year=2001
+ group by i_brand, i_brand_id
+ order by ext_price desc, i_brand_id
+limit 100 ;
+
+-- end query 1 in stream 0 using template query55.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query56.q b/ql/src/test/queries/clientpositive/perf/cbo_query56.q
new file mode 100644
index 00000000000..15dbc105f5b
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query56.q
@@ -0,0 +1,70 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query56.tpl and seed 1951559352
+explain cbo
+with ss as (
+ select i_item_id,sum(ss_ext_sales_price) total_sales
+ from
+ 	store_sales,
+ 	date_dim,
+         customer_address,
+         item
+ where i_item_id in (select
+     i_item_id
+from item
+where i_color in ('orchid','chiffon','lace'))
+ and     ss_item_sk              = i_item_sk
+ and     ss_sold_date_sk         = d_date_sk
+ and     d_year                  = 2000
+ and     d_moy                   = 1
+ and     ss_addr_sk              = ca_address_sk
+ and     ca_gmt_offset           = -8 
+ group by i_item_id),
+ cs as (
+ select i_item_id,sum(cs_ext_sales_price) total_sales
+ from
+ 	catalog_sales,
+ 	date_dim,
+         customer_address,
+         item
+ where
+         i_item_id               in (select
+  i_item_id
+from item
+where i_color in ('orchid','chiffon','lace'))
+ and     cs_item_sk              = i_item_sk
+ and     cs_sold_date_sk         = d_date_sk
+ and     d_year                  = 2000
+ and     d_moy                   = 1
+ and     cs_bill_addr_sk         = ca_address_sk
+ and     ca_gmt_offset           = -8 
+ group by i_item_id),
+ ws as (
+ select i_item_id,sum(ws_ext_sales_price) total_sales
+ from
+ 	web_sales,
+ 	date_dim,
+         customer_address,
+         item
+ where
+         i_item_id               in (select
+  i_item_id
+from item
+where i_color in ('orchid','chiffon','lace'))
+ and     ws_item_sk              = i_item_sk
+ and     ws_sold_date_sk         = d_date_sk
+ and     d_year                  = 2000
+ and     d_moy                   = 1
+ and     ws_bill_addr_sk         = ca_address_sk
+ and     ca_gmt_offset           = -8
+ group by i_item_id)
+  select  i_item_id ,sum(total_sales) total_sales
+ from  (select * from ss 
+        union all
+        select * from cs 
+        union all
+        select * from ws) tmp1
+ group by i_item_id
+ order by total_sales
+ limit 100;
+
+-- end query 1 in stream 0 using template query56.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query57.q b/ql/src/test/queries/clientpositive/perf/cbo_query57.q
new file mode 100644
index 00000000000..da23fd2a563
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query57.q
@@ -0,0 +1,50 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query57.tpl and seed 2031708268
+explain cbo
+with v1 as(
+ select i_category, i_brand,
+        cc_name,
+        d_year, d_moy,
+        sum(cs_sales_price) sum_sales,
+        avg(sum(cs_sales_price)) over
+          (partition by i_category, i_brand,
+                     cc_name, d_year)
+          avg_monthly_sales,
+        rank() over
+          (partition by i_category, i_brand,
+                     cc_name
+           order by d_year, d_moy) rn
+ from item, catalog_sales, date_dim, call_center
+ where cs_item_sk = i_item_sk and
+       cs_sold_date_sk = d_date_sk and
+       cc_call_center_sk= cs_call_center_sk and
+       (
+         d_year = 2000 or
+         ( d_year = 2000-1 and d_moy =12) or
+         ( d_year = 2000+1 and d_moy =1)
+       )
+ group by i_category, i_brand,
+          cc_name , d_year, d_moy),
+ v2 as(
+ select v1.i_category, v1.i_brand
+        ,v1.d_year, v1.d_moy
+        ,v1.avg_monthly_sales
+        ,v1.sum_sales, v1_lag.sum_sales psum, v1_lead.sum_sales nsum
+ from v1, v1 v1_lag, v1 v1_lead
+ where v1.i_category = v1_lag.i_category and
+       v1.i_category = v1_lead.i_category and
+       v1.i_brand = v1_lag.i_brand and
+       v1.i_brand = v1_lead.i_brand and
+       v1. cc_name = v1_lag. cc_name and
+       v1. cc_name = v1_lead. cc_name and
+       v1.rn = v1_lag.rn + 1 and
+       v1.rn = v1_lead.rn - 1)
+  select  *
+ from v2
+ where  d_year = 2000 and
+        avg_monthly_sales > 0 and
+        case when avg_monthly_sales > 0 then abs(sum_sales - avg_monthly_sales) / avg_monthly_sales else null end > 0.1
+ order by sum_sales - avg_monthly_sales, 3
+ limit 100;
+
+-- end query 1 in stream 0 using template query57.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query58.q b/ql/src/test/queries/clientpositive/perf/cbo_query58.q
new file mode 100644
index 00000000000..bbc1b968823
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query58.q
@@ -0,0 +1,67 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query58.tpl and seed 1819994127
+explain cbo
+with ss_items as
+ (select i_item_id item_id
+        ,sum(ss_ext_sales_price) ss_item_rev 
+ from store_sales
+     ,item
+     ,date_dim
+ where ss_item_sk = i_item_sk
+   and d_date in (select d_date
+                  from date_dim
+                  where d_week_seq = (select d_week_seq 
+                                      from date_dim
+                                      where d_date = '1998-02-19'))
+   and ss_sold_date_sk   = d_date_sk
+ group by i_item_id),
+ cs_items as
+ (select i_item_id item_id
+        ,sum(cs_ext_sales_price) cs_item_rev
+  from catalog_sales
+      ,item
+      ,date_dim
+ where cs_item_sk = i_item_sk
+  and  d_date in (select d_date
+                  from date_dim
+                  where d_week_seq = (select d_week_seq 
+                                      from date_dim
+                                      where d_date = '1998-02-19'))
+  and  cs_sold_date_sk = d_date_sk
+ group by i_item_id),
+ ws_items as
+ (select i_item_id item_id
+        ,sum(ws_ext_sales_price) ws_item_rev
+  from web_sales
+      ,item
+      ,date_dim
+ where ws_item_sk = i_item_sk
+  and  d_date in (select d_date
+                  from date_dim
+                  where d_week_seq =(select d_week_seq 
+                                     from date_dim
+                                     where d_date = '1998-02-19'))
+  and ws_sold_date_sk   = d_date_sk
+ group by i_item_id)
+  select  ss_items.item_id
+       ,ss_item_rev
+       ,ss_item_rev/(ss_item_rev+cs_item_rev+ws_item_rev)/3 * 100 ss_dev
+       ,cs_item_rev
+       ,cs_item_rev/(ss_item_rev+cs_item_rev+ws_item_rev)/3 * 100 cs_dev
+       ,ws_item_rev
+       ,ws_item_rev/(ss_item_rev+cs_item_rev+ws_item_rev)/3 * 100 ws_dev
+       ,(ss_item_rev+cs_item_rev+ws_item_rev)/3 average
+ from ss_items,cs_items,ws_items
+ where ss_items.item_id=cs_items.item_id
+   and ss_items.item_id=ws_items.item_id 
+   and ss_item_rev between 0.9 * cs_item_rev and 1.1 * cs_item_rev
+   and ss_item_rev between 0.9 * ws_item_rev and 1.1 * ws_item_rev
+   and cs_item_rev between 0.9 * ss_item_rev and 1.1 * ss_item_rev
+   and cs_item_rev between 0.9 * ws_item_rev and 1.1 * ws_item_rev
+   and ws_item_rev between 0.9 * ss_item_rev and 1.1 * ss_item_rev
+   and ws_item_rev between 0.9 * cs_item_rev and 1.1 * cs_item_rev
+ order by item_id
+         ,ss_item_rev
+ limit 100;
+
+-- end query 1 in stream 0 using template query58.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query59.q b/ql/src/test/queries/clientpositive/perf/cbo_query59.q
new file mode 100644
index 00000000000..9517f718702
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query59.q
@@ -0,0 +1,46 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query59.tpl and seed 1819994127
+explain cbo
+with wss as 
+ (select d_week_seq,
+        ss_store_sk,
+        sum(case when (d_day_name='Sunday') then ss_sales_price else null end) sun_sales,
+        sum(case when (d_day_name='Monday') then ss_sales_price else null end) mon_sales,
+        sum(case when (d_day_name='Tuesday') then ss_sales_price else  null end) tue_sales,
+        sum(case when (d_day_name='Wednesday') then ss_sales_price else null end) wed_sales,
+        sum(case when (d_day_name='Thursday') then ss_sales_price else null end) thu_sales,
+        sum(case when (d_day_name='Friday') then ss_sales_price else null end) fri_sales,
+        sum(case when (d_day_name='Saturday') then ss_sales_price else null end) sat_sales
+ from store_sales,date_dim
+ where d_date_sk = ss_sold_date_sk
+ group by d_week_seq,ss_store_sk
+ )
+  select  s_store_name1,s_store_id1,d_week_seq1
+       ,sun_sales1/sun_sales2,mon_sales1/mon_sales2
+       ,tue_sales1/tue_sales1,wed_sales1/wed_sales2,thu_sales1/thu_sales2
+       ,fri_sales1/fri_sales2,sat_sales1/sat_sales2
+ from
+ (select s_store_name s_store_name1,wss.d_week_seq d_week_seq1
+        ,s_store_id s_store_id1,sun_sales sun_sales1
+        ,mon_sales mon_sales1,tue_sales tue_sales1
+        ,wed_sales wed_sales1,thu_sales thu_sales1
+        ,fri_sales fri_sales1,sat_sales sat_sales1
+  from wss,store,date_dim d
+  where d.d_week_seq = wss.d_week_seq and
+        ss_store_sk = s_store_sk and 
+        d_month_seq between 1185 and 1185 + 11) y,
+ (select s_store_name s_store_name2,wss.d_week_seq d_week_seq2
+        ,s_store_id s_store_id2,sun_sales sun_sales2
+        ,mon_sales mon_sales2,tue_sales tue_sales2
+        ,wed_sales wed_sales2,thu_sales thu_sales2
+        ,fri_sales fri_sales2,sat_sales sat_sales2
+  from wss,store,date_dim d
+  where d.d_week_seq = wss.d_week_seq and
+        ss_store_sk = s_store_sk and 
+        d_month_seq between 1185+ 12 and 1185 + 23) x
+ where s_store_id1=s_store_id2
+   and d_week_seq1=d_week_seq2-52
+ order by s_store_name1,s_store_id1,d_week_seq1
+limit 100;
+
+-- end query 1 in stream 0 using template query59.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query6.q b/ql/src/test/queries/clientpositive/perf/cbo_query6.q
new file mode 100644
index 00000000000..d9ed084682c
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query6.q
@@ -0,0 +1,30 @@
+set hive.auto.convert.join=true;
+set hive.tez.cartesian-product.enabled=true;
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query6.tpl and seed 1819994127
+explain cbo
+select  a.ca_state state, count(*) cnt
+ from customer_address a
+     ,customer c
+     ,store_sales s
+     ,date_dim d
+     ,item i
+ where       a.ca_address_sk = c.c_current_addr_sk
+ 	and c.c_customer_sk = s.ss_customer_sk
+ 	and s.ss_sold_date_sk = d.d_date_sk
+ 	and s.ss_item_sk = i.i_item_sk
+ 	and d.d_month_seq = 
+ 	     (select distinct (d_month_seq)
+ 	      from date_dim
+               where d_year = 2000
+ 	        and d_moy = 2 )
+ 	and i.i_current_price > 1.2 * 
+             (select avg(j.i_current_price) 
+ 	     from item j 
+ 	     where j.i_category = i.i_category)
+ group by a.ca_state
+ having count(*) >= 10
+ order by cnt 
+ limit 100;
+
+-- end query 1 in stream 0 using template query6.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query60.q b/ql/src/test/queries/clientpositive/perf/cbo_query60.q
new file mode 100644
index 00000000000..71c33499ee7
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query60.q
@@ -0,0 +1,80 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query60.tpl and seed 1930872976
+explain cbo
+with ss as (
+ select
+          i_item_id,sum(ss_ext_sales_price) total_sales
+ from
+ 	store_sales,
+ 	date_dim,
+         customer_address,
+         item
+ where
+         i_item_id in (select
+  i_item_id
+from
+ item
+where i_category in ('Children'))
+ and     ss_item_sk              = i_item_sk
+ and     ss_sold_date_sk         = d_date_sk
+ and     d_year                  = 1999
+ and     d_moy                   = 9
+ and     ss_addr_sk              = ca_address_sk
+ and     ca_gmt_offset           = -6 
+ group by i_item_id),
+ cs as (
+ select
+          i_item_id,sum(cs_ext_sales_price) total_sales
+ from
+ 	catalog_sales,
+ 	date_dim,
+         customer_address,
+         item
+ where
+         i_item_id               in (select
+  i_item_id
+from
+ item
+where i_category in ('Children'))
+ and     cs_item_sk              = i_item_sk
+ and     cs_sold_date_sk         = d_date_sk
+ and     d_year                  = 1999
+ and     d_moy                   = 9
+ and     cs_bill_addr_sk         = ca_address_sk
+ and     ca_gmt_offset           = -6 
+ group by i_item_id),
+ ws as (
+ select
+          i_item_id,sum(ws_ext_sales_price) total_sales
+ from
+ 	web_sales,
+ 	date_dim,
+         customer_address,
+         item
+ where
+         i_item_id               in (select
+  i_item_id
+from
+ item
+where i_category in ('Children'))
+ and     ws_item_sk              = i_item_sk
+ and     ws_sold_date_sk         = d_date_sk
+ and     d_year                  = 1999
+ and     d_moy                   = 9
+ and     ws_bill_addr_sk         = ca_address_sk
+ and     ca_gmt_offset           = -6
+ group by i_item_id)
+  select   
+  i_item_id
+,sum(total_sales) total_sales
+ from  (select * from ss 
+        union all
+        select * from cs 
+        union all
+        select * from ws) tmp1
+ group by i_item_id
+ order by i_item_id
+      ,total_sales
+ limit 100;
+
+-- end query 1 in stream 0 using template query60.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query61.q b/ql/src/test/queries/clientpositive/perf/cbo_query61.q
new file mode 100644
index 00000000000..83c95bffa59
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query61.q
@@ -0,0 +1,46 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query61.tpl and seed 1930872976
+explain cbo
+select  promotions,total,cast(promotions as decimal(15,4))/cast(total as decimal(15,4))*100
+from
+  (select sum(ss_ext_sales_price) promotions
+   from  store_sales
+        ,store
+        ,promotion
+        ,date_dim
+        ,customer
+        ,customer_address 
+        ,item
+   where ss_sold_date_sk = d_date_sk
+   and   ss_store_sk = s_store_sk
+   and   ss_promo_sk = p_promo_sk
+   and   ss_customer_sk= c_customer_sk
+   and   ca_address_sk = c_current_addr_sk
+   and   ss_item_sk = i_item_sk 
+   and   ca_gmt_offset = -7
+   and   i_category = 'Electronics'
+   and   (p_channel_dmail = 'Y' or p_channel_email = 'Y' or p_channel_tv = 'Y')
+   and   s_gmt_offset = -7
+   and   d_year = 1999
+   and   d_moy  = 11) promotional_sales,
+  (select sum(ss_ext_sales_price) total
+   from  store_sales
+        ,store
+        ,date_dim
+        ,customer
+        ,customer_address
+        ,item
+   where ss_sold_date_sk = d_date_sk
+   and   ss_store_sk = s_store_sk
+   and   ss_customer_sk= c_customer_sk
+   and   ca_address_sk = c_current_addr_sk
+   and   ss_item_sk = i_item_sk
+   and   ca_gmt_offset = -7
+   and   i_category = 'Electronics'
+   and   s_gmt_offset = -7
+   and   d_year = 1999
+   and   d_moy  = 11) all_sales
+order by promotions, total
+limit 100;
+
+-- end query 1 in stream 0 using template query61.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query63.q b/ql/src/test/queries/clientpositive/perf/cbo_query63.q
new file mode 100644
index 00000000000..0553f4c9312
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query63.q
@@ -0,0 +1,31 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query63.tpl and seed 1819994127
+explain cbo
+select  * 
+from (select i_manager_id
+             ,sum(ss_sales_price) sum_sales
+             ,avg(sum(ss_sales_price)) over (partition by i_manager_id) avg_monthly_sales
+      from item
+          ,store_sales
+          ,date_dim
+          ,store
+      where ss_item_sk = i_item_sk
+        and ss_sold_date_sk = d_date_sk
+        and ss_store_sk = s_store_sk
+        and d_month_seq in (1212,1212+1,1212+2,1212+3,1212+4,1212+5,1212+6,1212+7,1212+8,1212+9,1212+10,1212+11)
+        and ((    i_category in ('Books','Children','Electronics')
+              and i_class in ('personal','portable','refernece','self-help')
+              and i_brand in ('scholaramalgamalg #14','scholaramalgamalg #7',
+		                  'exportiunivamalg #9','scholaramalgamalg #9'))
+           or(    i_category in ('Women','Music','Men')
+              and i_class in ('accessories','classical','fragrances','pants')
+              and i_brand in ('amalgimporto #1','edu packscholar #1','exportiimporto #1',
+		                 'importoamalg #1')))
+group by i_manager_id, d_moy) tmp1
+where case when avg_monthly_sales > 0 then abs (sum_sales - avg_monthly_sales) / avg_monthly_sales else null end > 0.1
+order by i_manager_id
+        ,avg_monthly_sales
+        ,sum_sales
+limit 100;
+
+-- end query 1 in stream 0 using template query63.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query64.q b/ql/src/test/queries/clientpositive/perf/cbo_query64.q
new file mode 100644
index 00000000000..24f3d856ed6
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query64.q
@@ -0,0 +1,121 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query64.tpl and seed 1220860970
+explain cbo
+with cs_ui as
+ (select cs_item_sk
+        ,sum(cs_ext_list_price) as sale,sum(cr_refunded_cash+cr_reversed_charge+cr_store_credit) as refund
+  from catalog_sales
+      ,catalog_returns
+  where cs_item_sk = cr_item_sk
+    and cs_order_number = cr_order_number
+  group by cs_item_sk
+  having sum(cs_ext_list_price)>2*sum(cr_refunded_cash+cr_reversed_charge+cr_store_credit)),
+cross_sales as
+ (select i_product_name product_name
+     ,i_item_sk item_sk
+     ,s_store_name store_name
+     ,s_zip store_zip
+     ,ad1.ca_street_number b_street_number
+     ,ad1.ca_street_name b_streen_name
+     ,ad1.ca_city b_city
+     ,ad1.ca_zip b_zip
+     ,ad2.ca_street_number c_street_number
+     ,ad2.ca_street_name c_street_name
+     ,ad2.ca_city c_city
+     ,ad2.ca_zip c_zip
+     ,d1.d_year as syear
+     ,d2.d_year as fsyear
+     ,d3.d_year s2year
+     ,count(*) cnt
+     ,sum(ss_wholesale_cost) s1
+     ,sum(ss_list_price) s2
+     ,sum(ss_coupon_amt) s3
+  FROM   store_sales
+        ,store_returns
+        ,cs_ui
+        ,date_dim d1
+        ,date_dim d2
+        ,date_dim d3
+        ,store
+        ,customer
+        ,customer_demographics cd1
+        ,customer_demographics cd2
+        ,promotion
+        ,household_demographics hd1
+        ,household_demographics hd2
+        ,customer_address ad1
+        ,customer_address ad2
+        ,income_band ib1
+        ,income_band ib2
+        ,item
+  WHERE  ss_store_sk = s_store_sk AND
+         ss_sold_date_sk = d1.d_date_sk AND
+         ss_customer_sk = c_customer_sk AND
+         ss_cdemo_sk= cd1.cd_demo_sk AND
+         ss_hdemo_sk = hd1.hd_demo_sk AND
+         ss_addr_sk = ad1.ca_address_sk and
+         ss_item_sk = i_item_sk and
+         ss_item_sk = sr_item_sk and
+         ss_ticket_number = sr_ticket_number and
+         ss_item_sk = cs_ui.cs_item_sk and
+         c_current_cdemo_sk = cd2.cd_demo_sk AND
+         c_current_hdemo_sk = hd2.hd_demo_sk AND
+         c_current_addr_sk = ad2.ca_address_sk and
+         c_first_sales_date_sk = d2.d_date_sk and
+         c_first_shipto_date_sk = d3.d_date_sk and
+         ss_promo_sk = p_promo_sk and
+         hd1.hd_income_band_sk = ib1.ib_income_band_sk and
+         hd2.hd_income_band_sk = ib2.ib_income_band_sk and
+         cd1.cd_marital_status <> cd2.cd_marital_status and
+         i_color in ('maroon','burnished','dim','steel','navajo','chocolate') and
+         i_current_price between 35 and 35 + 10 and
+         i_current_price between 35 + 1 and 35 + 15
+group by i_product_name
+       ,i_item_sk
+       ,s_store_name
+       ,s_zip
+       ,ad1.ca_street_number
+       ,ad1.ca_street_name
+       ,ad1.ca_city
+       ,ad1.ca_zip
+       ,ad2.ca_street_number
+       ,ad2.ca_street_name
+       ,ad2.ca_city
+       ,ad2.ca_zip
+       ,d1.d_year
+       ,d2.d_year
+       ,d3.d_year
+)
+select cs1.product_name
+     ,cs1.store_name
+     ,cs1.store_zip
+     ,cs1.b_street_number
+     ,cs1.b_streen_name
+     ,cs1.b_city
+     ,cs1.b_zip
+     ,cs1.c_street_number
+     ,cs1.c_street_name
+     ,cs1.c_city
+     ,cs1.c_zip
+     ,cs1.syear
+     ,cs1.cnt
+     ,cs1.s1
+     ,cs1.s2
+     ,cs1.s3
+     ,cs2.s1
+     ,cs2.s2
+     ,cs2.s3
+     ,cs2.syear
+     ,cs2.cnt
+from cross_sales cs1,cross_sales cs2
+where cs1.item_sk=cs2.item_sk and
+     cs1.syear = 2000 and
+     cs2.syear = 2000 + 1 and
+     cs2.cnt <= cs1.cnt and
+     cs1.store_name = cs2.store_name and
+     cs1.store_zip = cs2.store_zip
+order by cs1.product_name
+       ,cs1.store_name
+       ,cs2.cnt;
+
+-- end query 1 in stream 0 using template query64.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query65.q b/ql/src/test/queries/clientpositive/perf/cbo_query65.q
new file mode 100644
index 00000000000..e8b752dfc07
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query65.q
@@ -0,0 +1,31 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query65.tpl and seed 1819994127
+explain cbo
+select 
+	s_store_name,
+	i_item_desc,
+	sc.revenue,
+	i_current_price,
+	i_wholesale_cost,
+	i_brand
+ from store, item,
+     (select ss_store_sk, avg(revenue) as ave
+ 	from
+ 	    (select  ss_store_sk, ss_item_sk, 
+ 		     sum(ss_sales_price) as revenue
+ 		from store_sales, date_dim
+ 		where ss_sold_date_sk = d_date_sk and d_month_seq between 1212 and 1212+11
+ 		group by ss_store_sk, ss_item_sk) sa
+ 	group by ss_store_sk) sb,
+     (select  ss_store_sk, ss_item_sk, sum(ss_sales_price) as revenue
+ 	from store_sales, date_dim
+ 	where ss_sold_date_sk = d_date_sk and d_month_seq between 1212 and 1212+11
+ 	group by ss_store_sk, ss_item_sk) sc
+ where sb.ss_store_sk = sc.ss_store_sk and 
+       sc.revenue <= 0.1 * sb.ave and
+       s_store_sk = sc.ss_store_sk and
+       i_item_sk = sc.ss_item_sk
+ order by s_store_name, i_item_desc
+limit 100;
+
+-- end query 1 in stream 0 using template query65.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query66.q b/ql/src/test/queries/clientpositive/perf/cbo_query66.q
new file mode 100644
index 00000000000..518293554d0
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query66.q
@@ -0,0 +1,224 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query66.tpl and seed 2042478054
+explain cbo
+select   
+         w_warehouse_name
+ 	,w_warehouse_sq_ft
+ 	,w_city
+ 	,w_county
+ 	,w_state
+ 	,w_country
+        ,ship_carriers
+        ,year
+ 	,sum(jan_sales) as jan_sales
+ 	,sum(feb_sales) as feb_sales
+ 	,sum(mar_sales) as mar_sales
+ 	,sum(apr_sales) as apr_sales
+ 	,sum(may_sales) as may_sales
+ 	,sum(jun_sales) as jun_sales
+ 	,sum(jul_sales) as jul_sales
+ 	,sum(aug_sales) as aug_sales
+ 	,sum(sep_sales) as sep_sales
+ 	,sum(oct_sales) as oct_sales
+ 	,sum(nov_sales) as nov_sales
+ 	,sum(dec_sales) as dec_sales
+ 	,sum(jan_sales/w_warehouse_sq_ft) as jan_sales_per_sq_foot
+ 	,sum(feb_sales/w_warehouse_sq_ft) as feb_sales_per_sq_foot
+ 	,sum(mar_sales/w_warehouse_sq_ft) as mar_sales_per_sq_foot
+ 	,sum(apr_sales/w_warehouse_sq_ft) as apr_sales_per_sq_foot
+ 	,sum(may_sales/w_warehouse_sq_ft) as may_sales_per_sq_foot
+ 	,sum(jun_sales/w_warehouse_sq_ft) as jun_sales_per_sq_foot
+ 	,sum(jul_sales/w_warehouse_sq_ft) as jul_sales_per_sq_foot
+ 	,sum(aug_sales/w_warehouse_sq_ft) as aug_sales_per_sq_foot
+ 	,sum(sep_sales/w_warehouse_sq_ft) as sep_sales_per_sq_foot
+ 	,sum(oct_sales/w_warehouse_sq_ft) as oct_sales_per_sq_foot
+ 	,sum(nov_sales/w_warehouse_sq_ft) as nov_sales_per_sq_foot
+ 	,sum(dec_sales/w_warehouse_sq_ft) as dec_sales_per_sq_foot
+ 	,sum(jan_net) as jan_net
+ 	,sum(feb_net) as feb_net
+ 	,sum(mar_net) as mar_net
+ 	,sum(apr_net) as apr_net
+ 	,sum(may_net) as may_net
+ 	,sum(jun_net) as jun_net
+ 	,sum(jul_net) as jul_net
+ 	,sum(aug_net) as aug_net
+ 	,sum(sep_net) as sep_net
+ 	,sum(oct_net) as oct_net
+ 	,sum(nov_net) as nov_net
+ 	,sum(dec_net) as dec_net
+ from (
+    (select 
+ 	w_warehouse_name
+ 	,w_warehouse_sq_ft
+ 	,w_city
+ 	,w_county
+ 	,w_state
+ 	,w_country
+ 	,'DIAMOND' || ',' || 'AIRBORNE' as ship_carriers
+       ,d_year as year
+ 	,sum(case when d_moy = 1 
+ 		then ws_sales_price* ws_quantity else 0 end) as jan_sales
+ 	,sum(case when d_moy = 2 
+ 		then ws_sales_price* ws_quantity else 0 end) as feb_sales
+ 	,sum(case when d_moy = 3 
+ 		then ws_sales_price* ws_quantity else 0 end) as mar_sales
+ 	,sum(case when d_moy = 4 
+ 		then ws_sales_price* ws_quantity else 0 end) as apr_sales
+ 	,sum(case when d_moy = 5 
+ 		then ws_sales_price* ws_quantity else 0 end) as may_sales
+ 	,sum(case when d_moy = 6 
+ 		then ws_sales_price* ws_quantity else 0 end) as jun_sales
+ 	,sum(case when d_moy = 7 
+ 		then ws_sales_price* ws_quantity else 0 end) as jul_sales
+ 	,sum(case when d_moy = 8 
+ 		then ws_sales_price* ws_quantity else 0 end) as aug_sales
+ 	,sum(case when d_moy = 9 
+ 		then ws_sales_price* ws_quantity else 0 end) as sep_sales
+ 	,sum(case when d_moy = 10 
+ 		then ws_sales_price* ws_quantity else 0 end) as oct_sales
+ 	,sum(case when d_moy = 11
+ 		then ws_sales_price* ws_quantity else 0 end) as nov_sales
+ 	,sum(case when d_moy = 12
+ 		then ws_sales_price* ws_quantity else 0 end) as dec_sales
+ 	,sum(case when d_moy = 1 
+ 		then ws_net_paid_inc_tax * ws_quantity else 0 end) as jan_net
+ 	,sum(case when d_moy = 2
+ 		then ws_net_paid_inc_tax * ws_quantity else 0 end) as feb_net
+ 	,sum(case when d_moy = 3 
+ 		then ws_net_paid_inc_tax * ws_quantity else 0 end) as mar_net
+ 	,sum(case when d_moy = 4 
+ 		then ws_net_paid_inc_tax * ws_quantity else 0 end) as apr_net
+ 	,sum(case when d_moy = 5 
+ 		then ws_net_paid_inc_tax * ws_quantity else 0 end) as may_net
+ 	,sum(case when d_moy = 6 
+ 		then ws_net_paid_inc_tax * ws_quantity else 0 end) as jun_net
+ 	,sum(case when d_moy = 7 
+ 		then ws_net_paid_inc_tax * ws_quantity else 0 end) as jul_net
+ 	,sum(case when d_moy = 8 
+ 		then ws_net_paid_inc_tax * ws_quantity else 0 end) as aug_net
+ 	,sum(case when d_moy = 9 
+ 		then ws_net_paid_inc_tax * ws_quantity else 0 end) as sep_net
+ 	,sum(case when d_moy = 10 
+ 		then ws_net_paid_inc_tax * ws_quantity else 0 end) as oct_net
+ 	,sum(case when d_moy = 11
+ 		then ws_net_paid_inc_tax * ws_quantity else 0 end) as nov_net
+ 	,sum(case when d_moy = 12
+ 		then ws_net_paid_inc_tax * ws_quantity else 0 end) as dec_net
+     from
+          web_sales
+         ,warehouse
+         ,date_dim
+         ,time_dim
+ 	  ,ship_mode
+     where
+            ws_warehouse_sk =  w_warehouse_sk
+        and ws_sold_date_sk = d_date_sk
+        and ws_sold_time_sk = t_time_sk
+ 	and ws_ship_mode_sk = sm_ship_mode_sk
+        and d_year = 2002
+ 	and t_time between 49530 and 49530+28800 
+ 	and sm_carrier in ('DIAMOND','AIRBORNE')
+     group by 
+        w_warehouse_name
+ 	,w_warehouse_sq_ft
+ 	,w_city
+ 	,w_county
+ 	,w_state
+ 	,w_country
+       ,d_year
+ 	)
+ union all
+    (select 
+ 	w_warehouse_name
+ 	,w_warehouse_sq_ft
+ 	,w_city
+ 	,w_county
+ 	,w_state
+ 	,w_country
+ 	,'DIAMOND' || ',' || 'AIRBORNE' as ship_carriers
+       ,d_year as year
+ 	,sum(case when d_moy = 1 
+ 		then cs_ext_sales_price* cs_quantity else 0 end) as jan_sales
+ 	,sum(case when d_moy = 2 
+ 		then cs_ext_sales_price* cs_quantity else 0 end) as feb_sales
+ 	,sum(case when d_moy = 3 
+ 		then cs_ext_sales_price* cs_quantity else 0 end) as mar_sales
+ 	,sum(case when d_moy = 4 
+ 		then cs_ext_sales_price* cs_quantity else 0 end) as apr_sales
+ 	,sum(case when d_moy = 5 
+ 		then cs_ext_sales_price* cs_quantity else 0 end) as may_sales
+ 	,sum(case when d_moy = 6 
+ 		then cs_ext_sales_price* cs_quantity else 0 end) as jun_sales
+ 	,sum(case when d_moy = 7 
+ 		then cs_ext_sales_price* cs_quantity else 0 end) as jul_sales
+ 	,sum(case when d_moy = 8 
+ 		then cs_ext_sales_price* cs_quantity else 0 end) as aug_sales
+ 	,sum(case when d_moy = 9 
+ 		then cs_ext_sales_price* cs_quantity else 0 end) as sep_sales
+ 	,sum(case when d_moy = 10 
+ 		then cs_ext_sales_price* cs_quantity else 0 end) as oct_sales
+ 	,sum(case when d_moy = 11
+ 		then cs_ext_sales_price* cs_quantity else 0 end) as nov_sales
+ 	,sum(case when d_moy = 12
+ 		then cs_ext_sales_price* cs_quantity else 0 end) as dec_sales
+ 	,sum(case when d_moy = 1 
+ 		then cs_net_paid_inc_ship_tax * cs_quantity else 0 end) as jan_net
+ 	,sum(case when d_moy = 2 
+ 		then cs_net_paid_inc_ship_tax * cs_quantity else 0 end) as feb_net
+ 	,sum(case when d_moy = 3 
+ 		then cs_net_paid_inc_ship_tax * cs_quantity else 0 end) as mar_net
+ 	,sum(case when d_moy = 4 
+ 		then cs_net_paid_inc_ship_tax * cs_quantity else 0 end) as apr_net
+ 	,sum(case when d_moy = 5 
+ 		then cs_net_paid_inc_ship_tax * cs_quantity else 0 end) as may_net
+ 	,sum(case when d_moy = 6 
+ 		then cs_net_paid_inc_ship_tax * cs_quantity else 0 end) as jun_net
+ 	,sum(case when d_moy = 7 
+ 		then cs_net_paid_inc_ship_tax * cs_quantity else 0 end) as jul_net
+ 	,sum(case when d_moy = 8 
+ 		then cs_net_paid_inc_ship_tax * cs_quantity else 0 end) as aug_net
+ 	,sum(case when d_moy = 9 
+ 		then cs_net_paid_inc_ship_tax * cs_quantity else 0 end) as sep_net
+ 	,sum(case when d_moy = 10 
+ 		then cs_net_paid_inc_ship_tax * cs_quantity else 0 end) as oct_net
+ 	,sum(case when d_moy = 11
+ 		then cs_net_paid_inc_ship_tax * cs_quantity else 0 end) as nov_net
+ 	,sum(case when d_moy = 12
+ 		then cs_net_paid_inc_ship_tax * cs_quantity else 0 end) as dec_net
+     from
+          catalog_sales
+         ,warehouse
+         ,date_dim
+         ,time_dim
+ 	 ,ship_mode
+     where
+            cs_warehouse_sk =  w_warehouse_sk
+        and cs_sold_date_sk = d_date_sk
+        and cs_sold_time_sk = t_time_sk
+ 	and cs_ship_mode_sk = sm_ship_mode_sk
+        and d_year = 2002
+ 	and t_time between 49530 AND 49530+28800 
+ 	and sm_carrier in ('DIAMOND','AIRBORNE')
+     group by 
+        w_warehouse_name
+ 	,w_warehouse_sq_ft
+ 	,w_city
+ 	,w_county
+ 	,w_state
+ 	,w_country
+       ,d_year
+     ) 
+ ) x
+ group by 
+        w_warehouse_name
+ 	,w_warehouse_sq_ft
+ 	,w_city
+ 	,w_county
+ 	,w_state
+ 	,w_country
+ 	,ship_carriers
+       ,year
+ order by w_warehouse_name
+ limit 100;
+
+-- end query 1 in stream 0 using template query66.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query67.q b/ql/src/test/queries/clientpositive/perf/cbo_query67.q
new file mode 100644
index 00000000000..5781aacd0a6
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query67.q
@@ -0,0 +1,46 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query67.tpl and seed 1819994127
+explain cbo
+select  *
+from (select i_category
+            ,i_class
+            ,i_brand
+            ,i_product_name
+            ,d_year
+            ,d_qoy
+            ,d_moy
+            ,s_store_id
+            ,sumsales
+            ,rank() over (partition by i_category order by sumsales desc) rk
+      from (select i_category
+                  ,i_class
+                  ,i_brand
+                  ,i_product_name
+                  ,d_year
+                  ,d_qoy
+                  ,d_moy
+                  ,s_store_id
+                  ,sum(coalesce(ss_sales_price*ss_quantity,0)) sumsales
+            from store_sales
+                ,date_dim
+                ,store
+                ,item
+       where  ss_sold_date_sk=d_date_sk
+          and ss_item_sk=i_item_sk
+          and ss_store_sk = s_store_sk
+          and d_month_seq between 1212 and 1212+11
+       group by  rollup(i_category, i_class, i_brand, i_product_name, d_year, d_qoy, d_moy,s_store_id))dw1) dw2
+where rk <= 100
+order by i_category
+        ,i_class
+        ,i_brand
+        ,i_product_name
+        ,d_year
+        ,d_qoy
+        ,d_moy
+        ,s_store_id
+        ,sumsales
+        ,rk
+limit 100;
+
+-- end query 1 in stream 0 using template query67.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query68.q b/ql/src/test/queries/clientpositive/perf/cbo_query68.q
new file mode 100644
index 00000000000..520b9d77fb5
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query68.q
@@ -0,0 +1,44 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query68.tpl and seed 803547492
+explain cbo
+select  c_last_name
+       ,c_first_name
+       ,ca_city
+       ,bought_city
+       ,ss_ticket_number
+       ,extended_price
+       ,extended_tax
+       ,list_price
+ from (select ss_ticket_number
+             ,ss_customer_sk
+             ,ca_city bought_city
+             ,sum(ss_ext_sales_price) extended_price 
+             ,sum(ss_ext_list_price) list_price
+             ,sum(ss_ext_tax) extended_tax 
+       from store_sales
+           ,date_dim
+           ,store
+           ,household_demographics
+           ,customer_address 
+       where store_sales.ss_sold_date_sk = date_dim.d_date_sk
+         and store_sales.ss_store_sk = store.s_store_sk  
+        and store_sales.ss_hdemo_sk = household_demographics.hd_demo_sk
+        and store_sales.ss_addr_sk = customer_address.ca_address_sk
+        and date_dim.d_dom between 1 and 2 
+        and (household_demographics.hd_dep_count = 2 or
+             household_demographics.hd_vehicle_count= 1)
+        and date_dim.d_year in (1998,1998+1,1998+2)
+        and store.s_city in ('Cedar Grove','Wildwood')
+       group by ss_ticket_number
+               ,ss_customer_sk
+               ,ss_addr_sk,ca_city) dn
+      ,customer
+      ,customer_address current_addr
+ where ss_customer_sk = c_customer_sk
+   and customer.c_current_addr_sk = current_addr.ca_address_sk
+   and current_addr.ca_city <> bought_city
+ order by c_last_name
+         ,ss_ticket_number
+ limit 100;
+
+-- end query 1 in stream 0 using template query68.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query69.q b/ql/src/test/queries/clientpositive/perf/cbo_query69.q
new file mode 100644
index 00000000000..01183fba762
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query69.q
@@ -0,0 +1,49 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query69.tpl and seed 797269820
+explain cbo
+select  
+  cd_gender,
+  cd_marital_status,
+  cd_education_status,
+  count(*) cnt1,
+  cd_purchase_estimate,
+  count(*) cnt2,
+  cd_credit_rating,
+  count(*) cnt3
+ from
+  customer c,customer_address ca,customer_demographics
+ where
+  c.c_current_addr_sk = ca.ca_address_sk and
+  ca_state in ('CO','IL','MN') and
+  cd_demo_sk = c.c_current_cdemo_sk and 
+  exists (select *
+          from store_sales,date_dim
+          where c.c_customer_sk = ss_customer_sk and
+                ss_sold_date_sk = d_date_sk and
+                d_year = 1999 and
+                d_moy between 1 and 1+2) and
+   (not exists (select *
+            from web_sales,date_dim
+            where c.c_customer_sk = ws_bill_customer_sk and
+                  ws_sold_date_sk = d_date_sk and
+                  d_year = 1999 and
+                  d_moy between 1 and 1+2) and
+    not exists (select * 
+            from catalog_sales,date_dim
+            where c.c_customer_sk = cs_ship_customer_sk and
+                  cs_sold_date_sk = d_date_sk and
+                  d_year = 1999 and
+                  d_moy between 1 and 1+2))
+ group by cd_gender,
+          cd_marital_status,
+          cd_education_status,
+          cd_purchase_estimate,
+          cd_credit_rating
+ order by cd_gender,
+          cd_marital_status,
+          cd_education_status,
+          cd_purchase_estimate,
+          cd_credit_rating
+ limit 100;
+
+-- end query 1 in stream 0 using template query69.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query7.q b/ql/src/test/queries/clientpositive/perf/cbo_query7.q
new file mode 100644
index 00000000000..3fb7130fff8
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query7.q
@@ -0,0 +1,23 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query7.tpl and seed 1930872976
+explain cbo
+select  i_item_id, 
+        avg(ss_quantity) agg1,
+        avg(ss_list_price) agg2,
+        avg(ss_coupon_amt) agg3,
+        avg(ss_sales_price) agg4 
+ from store_sales, customer_demographics, date_dim, item, promotion
+ where ss_sold_date_sk = d_date_sk and
+       ss_item_sk = i_item_sk and
+       ss_cdemo_sk = cd_demo_sk and
+       ss_promo_sk = p_promo_sk and
+       cd_gender = 'F' and 
+       cd_marital_status = 'W' and
+       cd_education_status = 'Primary' and
+       (p_channel_email = 'N' or p_channel_event = 'N') and
+       d_year = 1998 
+ group by i_item_id
+ order by i_item_id
+ limit 100;
+
+-- end query 1 in stream 0 using template query7.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query70.q b/ql/src/test/queries/clientpositive/perf/cbo_query70.q
new file mode 100644
index 00000000000..5b8b76e3150
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query70.q
@@ -0,0 +1,40 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query70.tpl and seed 1819994127
+explain cbo
+select  
+    sum(ss_net_profit) as total_sum
+   ,s_state
+   ,s_county
+   ,grouping(s_state)+grouping(s_county) as lochierarchy
+   ,rank() over (
+ 	partition by grouping(s_state)+grouping(s_county),
+ 	case when grouping(s_county) = 0 then s_state end 
+ 	order by sum(ss_net_profit) desc) as rank_within_parent
+ from
+    store_sales
+   ,date_dim       d1
+   ,store
+ where
+    d1.d_month_seq between 1212 and 1212+11
+ and d1.d_date_sk = ss_sold_date_sk
+ and s_store_sk  = ss_store_sk
+ and s_state in
+             ( select s_state
+               from  (select s_state as s_state,
+ 			    rank() over ( partition by s_state order by sum(ss_net_profit) desc) as ranking
+                      from   store_sales, store, date_dim
+                      where  d_month_seq between 1212 and 1212+11
+ 			    and d_date_sk = ss_sold_date_sk
+ 			    and s_store_sk  = ss_store_sk
+                      group by s_state
+                     ) tmp1 
+               where ranking <= 5
+             )
+ group by rollup(s_state,s_county)
+ order by
+   lochierarchy desc
+  ,case when lochierarchy = 0 then s_state end
+  ,rank_within_parent
+ limit 100;
+
+-- end query 1 in stream 0 using template query70.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query71.q b/ql/src/test/queries/clientpositive/perf/cbo_query71.q
new file mode 100644
index 00000000000..e4d4df54437
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query71.q
@@ -0,0 +1,42 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query71.tpl and seed 2031708268
+explain cbo
+select i_brand_id brand_id, i_brand brand,t_hour,t_minute,
+ 	sum(ext_price) ext_price
+ from item, (select ws_ext_sales_price as ext_price, 
+                        ws_sold_date_sk as sold_date_sk,
+                        ws_item_sk as sold_item_sk,
+                        ws_sold_time_sk as time_sk  
+                 from web_sales,date_dim
+                 where d_date_sk = ws_sold_date_sk
+                   and d_moy=12
+                   and d_year=2001
+                 union all
+                 select cs_ext_sales_price as ext_price,
+                        cs_sold_date_sk as sold_date_sk,
+                        cs_item_sk as sold_item_sk,
+                        cs_sold_time_sk as time_sk
+                 from catalog_sales,date_dim
+                 where d_date_sk = cs_sold_date_sk
+                   and d_moy=12
+                   and d_year=2001
+                 union all
+                 select ss_ext_sales_price as ext_price,
+                        ss_sold_date_sk as sold_date_sk,
+                        ss_item_sk as sold_item_sk,
+                        ss_sold_time_sk as time_sk
+                 from store_sales,date_dim
+                 where d_date_sk = ss_sold_date_sk
+                   and d_moy=12
+                   and d_year=2001
+                 ) as tmp,time_dim
+ where
+   sold_item_sk = i_item_sk
+   and i_manager_id=1
+   and time_sk = t_time_sk
+   and (t_meal_time = 'breakfast' or t_meal_time = 'dinner')
+ group by i_brand, i_brand_id,t_hour,t_minute
+ order by ext_price desc, i_brand_id
+ ;
+
+-- end query 1 in stream 0 using template query71.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query72.q b/ql/src/test/queries/clientpositive/perf/cbo_query72.q
new file mode 100644
index 00000000000..941a643d7f0
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query72.q
@@ -0,0 +1,33 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query72.tpl and seed 2031708268
+explain cbo
+select  i_item_desc
+      ,w_warehouse_name
+      ,d1.d_week_seq
+      ,count(case when p_promo_sk is null then 1 else 0 end) no_promo
+      ,count(case when p_promo_sk is not null then 1 else 0 end) promo
+      ,count(*) total_cnt
+from catalog_sales
+join inventory on (cs_item_sk = inv_item_sk)
+join warehouse on (w_warehouse_sk=inv_warehouse_sk)
+join item on (i_item_sk = cs_item_sk)
+join customer_demographics on (cs_bill_cdemo_sk = cd_demo_sk)
+join household_demographics on (cs_bill_hdemo_sk = hd_demo_sk)
+join date_dim d1 on (cs_sold_date_sk = d1.d_date_sk)
+join date_dim d2 on (inv_date_sk = d2.d_date_sk)
+join date_dim d3 on (cs_ship_date_sk = d3.d_date_sk)
+left outer join promotion on (cs_promo_sk=p_promo_sk)
+left outer join catalog_returns on (cr_item_sk = cs_item_sk and cr_order_number = cs_order_number)
+where d1.d_week_seq = d2.d_week_seq
+  and inv_quantity_on_hand < cs_quantity 
+  and d3.d_date > d1.d_date + 5
+  and hd_buy_potential = '1001-5000'
+  and d1.d_year = 2001
+  and hd_buy_potential = '1001-5000'
+  and cd_marital_status = 'M'
+  and d1.d_year = 2001
+group by i_item_desc,w_warehouse_name,d1.d_week_seq
+order by total_cnt desc, i_item_desc, w_warehouse_name, d_week_seq
+limit 100;
+
+-- end query 1 in stream 0 using template query72.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query73.q b/ql/src/test/queries/clientpositive/perf/cbo_query73.q
new file mode 100644
index 00000000000..c78225efb28
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query73.q
@@ -0,0 +1,30 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query73.tpl and seed 1971067816
+explain cbo
+select c_last_name
+       ,c_first_name
+       ,c_salutation
+       ,c_preferred_cust_flag 
+       ,ss_ticket_number
+       ,cnt from
+   (select ss_ticket_number
+          ,ss_customer_sk
+          ,count(*) cnt
+    from store_sales,date_dim,store,household_demographics
+    where store_sales.ss_sold_date_sk = date_dim.d_date_sk
+    and store_sales.ss_store_sk = store.s_store_sk  
+    and store_sales.ss_hdemo_sk = household_demographics.hd_demo_sk
+    and date_dim.d_dom between 1 and 2 
+    and (household_demographics.hd_buy_potential = '>10000' or
+         household_demographics.hd_buy_potential = 'unknown')
+    and household_demographics.hd_vehicle_count > 0
+    and case when household_demographics.hd_vehicle_count > 0 then 
+             household_demographics.hd_dep_count/ household_demographics.hd_vehicle_count else null end > 1
+    and date_dim.d_year in (2000,2000+1,2000+2)
+    and store.s_county in ('Mobile County','Maverick County','Huron County','Kittitas County')
+    group by ss_ticket_number,ss_customer_sk) dj,customer
+    where ss_customer_sk = c_customer_sk
+      and cnt between 1 and 5
+    order by cnt desc;
+
+-- end query 1 in stream 0 using template query73.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query74.q b/ql/src/test/queries/clientpositive/perf/cbo_query74.q
new file mode 100644
index 00000000000..71954c8781a
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query74.q
@@ -0,0 +1,63 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query74.tpl and seed 1556717815
+explain cbo
+with year_total as (
+ select c_customer_id customer_id
+       ,c_first_name customer_first_name
+       ,c_last_name customer_last_name
+       ,d_year as year
+       ,max(ss_net_paid) year_total
+       ,'s' sale_type
+ from customer
+     ,store_sales
+     ,date_dim
+ where c_customer_sk = ss_customer_sk
+   and ss_sold_date_sk = d_date_sk
+   and d_year in (2001,2001+1)
+ group by c_customer_id
+         ,c_first_name
+         ,c_last_name
+         ,d_year
+ union all
+ select c_customer_id customer_id
+       ,c_first_name customer_first_name
+       ,c_last_name customer_last_name
+       ,d_year as year
+       ,max(ws_net_paid) year_total
+       ,'w' sale_type
+ from customer
+     ,web_sales
+     ,date_dim
+ where c_customer_sk = ws_bill_customer_sk
+   and ws_sold_date_sk = d_date_sk
+   and d_year in (2001,2001+1)
+ group by c_customer_id
+         ,c_first_name
+         ,c_last_name
+         ,d_year
+         )
+  select 
+        t_s_secyear.customer_id, t_s_secyear.customer_first_name, t_s_secyear.customer_last_name
+ from year_total t_s_firstyear
+     ,year_total t_s_secyear
+     ,year_total t_w_firstyear
+     ,year_total t_w_secyear
+ where t_s_secyear.customer_id = t_s_firstyear.customer_id
+         and t_s_firstyear.customer_id = t_w_secyear.customer_id
+         and t_s_firstyear.customer_id = t_w_firstyear.customer_id
+         and t_s_firstyear.sale_type = 's'
+         and t_w_firstyear.sale_type = 'w'
+         and t_s_secyear.sale_type = 's'
+         and t_w_secyear.sale_type = 'w'
+         and t_s_firstyear.year = 2001
+         and t_s_secyear.year = 2001+1
+         and t_w_firstyear.year = 2001
+         and t_w_secyear.year = 2001+1
+         and t_s_firstyear.year_total > 0
+         and t_w_firstyear.year_total > 0
+         and case when t_w_firstyear.year_total > 0 then t_w_secyear.year_total / t_w_firstyear.year_total else null end
+           > case when t_s_firstyear.year_total > 0 then t_s_secyear.year_total / t_s_firstyear.year_total else null end
+ order by 2,1,3
+limit 100;
+
+-- end query 1 in stream 0 using template query74.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query75.q b/ql/src/test/queries/clientpositive/perf/cbo_query75.q
new file mode 100644
index 00000000000..2d92853febf
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query75.q
@@ -0,0 +1,72 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query75.tpl and seed 1819994127
+explain cbo
+WITH all_sales AS (
+ SELECT d_year
+       ,i_brand_id
+       ,i_class_id
+       ,i_category_id
+       ,i_manufact_id
+       ,SUM(sales_cnt) AS sales_cnt
+       ,SUM(sales_amt) AS sales_amt
+ FROM (SELECT d_year
+             ,i_brand_id
+             ,i_class_id
+             ,i_category_id
+             ,i_manufact_id
+             ,cs_quantity - COALESCE(cr_return_quantity,0) AS sales_cnt
+             ,cs_ext_sales_price - COALESCE(cr_return_amount,0.0) AS sales_amt
+       FROM catalog_sales JOIN item ON i_item_sk=cs_item_sk
+                          JOIN date_dim ON d_date_sk=cs_sold_date_sk
+                          LEFT JOIN catalog_returns ON (cs_order_number=cr_order_number 
+                                                    AND cs_item_sk=cr_item_sk)
+       WHERE i_category='Sports'
+       UNION
+       SELECT d_year
+             ,i_brand_id
+             ,i_class_id
+             ,i_category_id
+             ,i_manufact_id
+             ,ss_quantity - COALESCE(sr_return_quantity,0) AS sales_cnt
+             ,ss_ext_sales_price - COALESCE(sr_return_amt,0.0) AS sales_amt
+       FROM store_sales JOIN item ON i_item_sk=ss_item_sk
+                        JOIN date_dim ON d_date_sk=ss_sold_date_sk
+                        LEFT JOIN store_returns ON (ss_ticket_number=sr_ticket_number 
+                                                AND ss_item_sk=sr_item_sk)
+       WHERE i_category='Sports'
+       UNION
+       SELECT d_year
+             ,i_brand_id
+             ,i_class_id
+             ,i_category_id
+             ,i_manufact_id
+             ,ws_quantity - COALESCE(wr_return_quantity,0) AS sales_cnt
+             ,ws_ext_sales_price - COALESCE(wr_return_amt,0.0) AS sales_amt
+       FROM web_sales JOIN item ON i_item_sk=ws_item_sk
+                      JOIN date_dim ON d_date_sk=ws_sold_date_sk
+                      LEFT JOIN web_returns ON (ws_order_number=wr_order_number 
+                                            AND ws_item_sk=wr_item_sk)
+       WHERE i_category='Sports') sales_detail
+ GROUP BY d_year, i_brand_id, i_class_id, i_category_id, i_manufact_id)
+ SELECT  prev_yr.d_year AS prev_year
+                          ,curr_yr.d_year AS year
+                          ,curr_yr.i_brand_id
+                          ,curr_yr.i_class_id
+                          ,curr_yr.i_category_id
+                          ,curr_yr.i_manufact_id
+                          ,prev_yr.sales_cnt AS prev_yr_cnt
+                          ,curr_yr.sales_cnt AS curr_yr_cnt
+                          ,curr_yr.sales_cnt-prev_yr.sales_cnt AS sales_cnt_diff
+                          ,curr_yr.sales_amt-prev_yr.sales_amt AS sales_amt_diff
+ FROM all_sales curr_yr, all_sales prev_yr
+ WHERE curr_yr.i_brand_id=prev_yr.i_brand_id
+   AND curr_yr.i_class_id=prev_yr.i_class_id
+   AND curr_yr.i_category_id=prev_yr.i_category_id
+   AND curr_yr.i_manufact_id=prev_yr.i_manufact_id
+   AND curr_yr.d_year=2002
+   AND prev_yr.d_year=2002-1
+   AND CAST(curr_yr.sales_cnt AS DECIMAL(17,2))/CAST(prev_yr.sales_cnt AS DECIMAL(17,2))<0.9
+ ORDER BY sales_cnt_diff
+ limit 100;
+
+-- end query 1 in stream 0 using template query75.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query76.q b/ql/src/test/queries/clientpositive/perf/cbo_query76.q
new file mode 100644
index 00000000000..9d2d89efab6
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query76.q
@@ -0,0 +1,26 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query76.tpl and seed 2031708268
+explain cbo
+select  channel, col_name, d_year, d_qoy, i_category, COUNT(*) sales_cnt, SUM(ext_sales_price) sales_amt FROM (
+        SELECT 'store' as channel, 'ss_addr_sk' col_name, d_year, d_qoy, i_category, ss_ext_sales_price ext_sales_price
+         FROM store_sales, item, date_dim
+         WHERE ss_addr_sk IS NULL
+           AND ss_sold_date_sk=d_date_sk
+           AND ss_item_sk=i_item_sk
+        UNION ALL
+        SELECT 'web' as channel, 'ws_web_page_sk' col_name, d_year, d_qoy, i_category, ws_ext_sales_price ext_sales_price
+         FROM web_sales, item, date_dim
+         WHERE ws_web_page_sk IS NULL
+           AND ws_sold_date_sk=d_date_sk
+           AND ws_item_sk=i_item_sk
+        UNION ALL
+        SELECT 'catalog' as channel, 'cs_warehouse_sk' col_name, d_year, d_qoy, i_category, cs_ext_sales_price ext_sales_price
+         FROM catalog_sales, item, date_dim
+         WHERE cs_warehouse_sk IS NULL
+           AND cs_sold_date_sk=d_date_sk
+           AND cs_item_sk=i_item_sk) foo
+GROUP BY channel, col_name, d_year, d_qoy, i_category
+ORDER BY channel, col_name, d_year, d_qoy, i_category
+limit 100;
+
+-- end query 1 in stream 0 using template query76.tpl
diff --git a/ql/src/test/queries/clientpositive/perf/cbo_query77.q b/ql/src/test/queries/clientpositive/perf/cbo_query77.q
new file mode 100644
index 00000000000..3d0f4842267
--- /dev/null
+++ b/ql/src/test/queries/clientpositive/perf/cbo_query77.q
@@ -0,0 +1,109 @@
+set hive.mapred.mode=nonstrict;
+-- start query 1 in stream 0 using template query77.tpl and seed 1819994127
... 97303 lines suppressed ...