import file=tpch_schema
----

import file=tpch_stats
----

# --------------------------------------------------
# Q6
# Forecasting Revenue Change
# Quantifies the amount of revenue increase that would have resulted from
# eliminating certain companywide discounts in a given percentage range in a
# given year. Asking this type of "what if" query can be used to look for ways
# to increase revenues.
#
# Considers all the lineitems shipped in a given year with discounts between
# DISCOUNT-0.01 and DISCOUNT+0.01. The query lists the amount by which the total
# revenue would have increased if these discounts had been eliminated for
# lineitems with l_quantity less than quantity. Note that the potential revenue
# increase is equal to the sum of [l_extendedprice * l_discount] for all
# lineitems with discounts and quantities in the qualifying range.
# --------------------------------------------------
stats-quality database=tpch set=save_tables_prefix=q6
SELECT
    sum(l_extendedprice * l_discount) AS revenue
FROM
    lineitem
WHERE
    l_shipdate >= DATE '1994-01-01'
    AND l_shipdate < DATE '1994-01-01' + INTERVAL '1' YEAR
    AND l_discount BETWEEN 0.06 - 0.01 AND 0.06 + 0.01
    AND l_quantity < 24;
----
----
scalar-group-by
 ├── save-table-name: q6_scalar_group_by_1
 ├── columns: revenue:20(float)
 ├── cardinality: [1 - 1]
 ├── immutable
 ├── stats: [rows=1, distinct(20)=1, null(20)=0]
 ├── key: ()
 ├── fd: ()-->(20)
 ├── project
 │    ├── save-table-name: q6_project_2
 │    ├── columns: column19:19(float!null)
 │    ├── immutable
 │    ├── stats: [rows=71117.89, distinct(19)=71117.9, null(19)=0]
 │    ├── select
 │    │    ├── save-table-name: q6_select_3
 │    │    ├── columns: l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_shipdate:11(date!null)
 │    │    ├── stats: [rows=71117.89, distinct(5)=23.5306, null(5)=0, distinct(6)=68848.6, null(6)=0, distinct(7)=1.8, null(7)=0, distinct(11)=365, null(11)=0, distinct(6,7)=71117.9, null(6,7)=0, distinct(5,7,11)=15459.6, null(5,7,11)=0]
 │    │    │   histogram(5)=  0 2860.5 68257          0
 │    │    │                <--- 1.0 -------- 23.999999999999996
 │    │    │   histogram(7)=  0           0            71118   0
 │    │    │                <--- 0.049999999999999996 ------- 0.07
 │    │    │   histogram(11)=  0       0        923.01     138.42     2123     323.06     2076.7     415.35     2215.2     230.78     2123     184.56     1846     415.35     1892.2     415.35     2169.1     276.92     2030.6     230.78     2076.7     184.56     2215.2     276.92     2169.1     138.42     2215.2     184.56     2215.2     138.42     2169.1     138.42     2123     276.92     2169.1     230.78     2030.6     230.78     2169.1     92.282     2169.1     184.56     2215.2     92.282     2123     230.78     1892.2     415.35     2215.2     138.42     1984.5     276.92     2076.7     184.56     2215.2     138.42     2215.2     46.141     2215.2     46.141     2123     276.92     2024.5     144.61
 │    │    │                 <--- '1993-12-31' -------- '1994-01-06' ------ '1994-01-20' -------- '1994-02-01' -------- '1994-02-14' ------ '1994-02-25' ------ '1994-03-11' -------- '1994-03-24' -------- '1994-04-07' -------- '1994-04-19' -------- '1994-05-03' -------- '1994-05-14' -------- '1994-05-24' -------- '1994-06-04' -------- '1994-06-14' -------- '1994-06-26' ------ '1994-07-06' -------- '1994-07-17' -------- '1994-08-01' -------- '1994-08-11' -------- '1994-08-25' -------- '1994-09-05' ------ '1994-09-16' -------- '1994-09-26' -------- '1994-10-07' -------- '1994-10-17' -------- '1994-10-27' -------- '1994-11-09' -------- '1994-11-22' -------- '1994-12-04' ------ '1994-12-16' -------- '1994-12-31'
 │    │    ├── index-join lineitem
 │    │    │    ├── save-table-name: q6_index_join_4
 │    │    │    ├── columns: l_quantity:5(float!null) l_extendedprice:6(float!null) l_discount:7(float!null) l_shipdate:11(date!null)
 │    │    │    ├── stats: [rows=924787.9, distinct(5)=50, null(5)=0, distinct(6)=612961, null(6)=0, distinct(7)=11, null(7)=0, distinct(11)=365, null(11)=0]
 │    │    │    └── scan lineitem@l_sd
 │    │    │         ├── save-table-name: q6_scan_5
 │    │    │         ├── columns: l_orderkey:1(int!null) l_linenumber:4(int!null) l_shipdate:11(date!null)
 │    │    │         ├── constraint: /11/1/4: [/'1994-01-01' - /'1994-12-31']
 │    │    │         ├── stats: [rows=924787.9, distinct(1)=736000, null(1)=0, distinct(4)=7, null(4)=0, distinct(11)=365, null(11)=0]
 │    │    │         │   histogram(1)=  0 92.46 4531.5  92.46  4531.5  92.46  4531.5  92.46  4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4439  184.92  4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5  92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4439  184.92   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4439  184.92   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4531.5   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46   4623.9   92.46
 │    │    │         │                <--- 197 -------- 23686 -------- 53253 -------- 90435 -------- 121730 -------- 153280 -------- 175456 -------- 208548 -------- 242209 -------- 273057 ------ 296640 -------- 330307 -------- 360999 -------- 386307 -------- 420225 -------- 450050 -------- 477795 -------- 504711 -------- 533153 -------- 556672 -------- 582243 -------- 613729 -------- 646117 -------- 675840 -------- 706048 -------- 733063 -------- 769282 -------- 793922 -------- 820357 -------- 849536 -------- 875719 -------- 905028 -------- 940643 -------- 968355 -------- 998721 -------- 1023621 -------- 1059424 -------- 1084932 -------- 1115553 -------- 1139363 -------- 1167361 -------- 1194400 -------- 1225984 -------- 1253861 -------- 1281633 -------- 1304999 -------- 1336355 -------- 1370759 -------- 1400832 -------- 1434085 -------- 1458852 -------- 1491427 -------- 1525120 -------- 1555205 -------- 1591300 -------- 1619426 -------- 1651458 -------- 1682950 -------- 1711399 -------- 1747591 -------- 1787205 ------ 1822240 -------- 1856163 -------- 1886915 -------- 1910949 -------- 1947202 ------ 1974311 -------- 2009286 -------- 2044034 -------- 2079104 -------- 2103488 -------- 2134657 -------- 2164293 -------- 2204514 -------- 2230823 -------- 2265253 -------- 2289826 -------- 2329539 -------- 2364455 -------- 2393507 -------- 2414628 -------- 2440228 -------- 2465255 -------- 2489568 -------- 2520900 -------- 2554919 -------- 2583333 -------- 2612966 -------- 2644833 -------- 2667362 -------- 2702784 -------- 2727394 -------- 2759748 -------- 2794531 -------- 2822214 -------- 2846624 -------- 2883748 -------- 2919586 -------- 2951908 -------- 2980068 -------- 3014726 -------- 3050725 -------- 3081028 -------- 3113351 -------- 3150243 -------- 3185669 -------- 3214311 -------- 3241281 -------- 3275748 -------- 3303232 -------- 3339559 -------- 3370627 -------- 3393664 -------- 3435265 -------- 3464581 -------- 3489026 -------- 3516096 -------- 3548480 -------- 3587015 -------- 3611239 -------- 3638724 -------- 3668641 -------- 3695751 -------- 3729636 -------- 3751523 -------- 3784608 -------- 3815715 -------- 3848608 -------- 3881184 -------- 3908738 -------- 3940002 -------- 3966176 -------- 4001984 -------- 4035687 -------- 4065283 -------- 4092834 -------- 4133062 -------- 4160613 -------- 4196421 -------- 4223713 -------- 4254788 -------- 4291040 -------- 4313664 -------- 4342823 -------- 4369952 -------- 4391684 -------- 4419040 -------- 4449921 -------- 4471781 -------- 4506210 -------- 4538176 -------- 4571297 -------- 4601121 -------- 4630887 -------- 4657476 -------- 4684803 -------- 4714566 -------- 4744070 -------- 4776385 -------- 4807777 -------- 4839491 -------- 4873953 -------- 4902245 -------- 4936263 -------- 4970721 -------- 5003140 -------- 5029729 -------- 5059010 -------- 5087521 -------- 5121093 -------- 5150405 -------- 5178375 -------- 5203683 -------- 5234531 -------- 5268195 -------- 5300004 -------- 5331558 -------- 5362178 -------- 5385762 -------- 5418498 -------- 5445762 -------- 5483109 -------- 5514561 -------- 5542052 -------- 5569572 -------- 5596102 -------- 5622401 -------- 5652194 -------- 5671362 -------- 5699591 -------- 5727136 -------- 5753284 -------- 5780742 -------- 5809189 -------- 5836545 -------- 5864454 -------- 5894917 -------- 5933825 -------- 5968933 -------- 5999590
 │    │    │         │   histogram(4)=  0 2.2925e+05 0 2.0632e+05 0 1.6221e+05 0 1.328e+05 0 98120 0 63995 0 32090
 │    │    │         │                <------ 1 ---------- 2 ---------- 3 ---------- 4 ------- 5 ----- 6 ----- 7 -
 │    │    │         │   histogram(11)=  0       0        12002      1800      27606      4201      27005      5401      28806      3001      27606      2400      24005      5401      24605      5401      28206      3601      26405      3001      27005      2400      28806      3601      28206      1800      28806      2400      28806      1800      28206      1800      27606      3601      28206      3001      26405      3001      28206      1200      28206      2400      28806      1200      27606      3001      24605      5401      28806      1800      25805      3601      27005      2400      28806      1800      28806      600       28806      600       27606      3601      26326     1880.4
 │    │    │         │                 <--- '1993-12-31' ------- '1994-01-06' ------- '1994-01-20' ------- '1994-02-01' ------- '1994-02-14' ------- '1994-02-25' ------- '1994-03-11' ------- '1994-03-24' ------- '1994-04-07' ------- '1994-04-19' ------- '1994-05-03' ------- '1994-05-14' ------- '1994-05-24' ------- '1994-06-04' ------- '1994-06-14' ------- '1994-06-26' ------- '1994-07-06' ------- '1994-07-17' ------- '1994-08-01' ------- '1994-08-11' ------- '1994-08-25' ------- '1994-09-05' ------- '1994-09-16' ------- '1994-09-26' ------- '1994-10-07' ------- '1994-10-17' ------- '1994-10-27' ------- '1994-11-09' ------- '1994-11-22' ------- '1994-12-04' ------- '1994-12-16' ------- '1994-12-31'
 │    │    │         ├── key: (1,4)
 │    │    │         └── fd: (1,4)-->(11)
 │    │    └── filters
 │    │         ├── (l_discount:7 >= 0.05) AND (l_discount:7 <= 0.07) [type=bool, outer=(7), constraints=(/7: [/0.05 - /0.07]; tight)]
 │    │         └── l_quantity:5 < 24.0 [type=bool, outer=(5), constraints=(/5: (/NULL - /23.999999999999996]; tight)]
 │    └── projections
 │         └── l_extendedprice:6 * l_discount:7 [as=column19:19, type=float, outer=(6,7), immutable]
 └── aggregations
      └── sum [as=sum:20, type=float, outer=(19)]
           └── column19:19 [type=float]

----Stats for q6_scalar_group_by_1----
column_names  row_count  distinct_count  null_count
{revenue}     1          1               0
~~~~
column_names  row_count_est  row_count_err  distinct_count_est  distinct_count_err  null_count_est  null_count_err
{revenue}     1.00           1.00           1.00                1.00                0.00            1.00

----Stats for q6_project_2----
column_names  row_count  distinct_count  null_count
{column19}    114160     108866          0
~~~~
column_names  row_count_est  row_count_err  distinct_count_est  distinct_count_err  null_count_est  null_count_err
{column19}    71118.00       1.61           71118.00            1.53                0.00            1.00

----Stats for q6_select_3----
column_names       row_count  distinct_count  null_count
{l_discount}       114160     3               0
{l_extendedprice}  114160     98751           0
{l_quantity}       114160     23              0
{l_shipdate}       114160     365             0
~~~~
column_names       row_count_est  row_count_err  distinct_count_est  distinct_count_err  null_count_est  null_count_err
{l_discount}       71118.00       1.61           2.00                1.50                0.00            1.00
{l_extendedprice}  71118.00       1.61           68849.00            1.43                0.00            1.00
{l_quantity}       71118.00       1.61           24.00               1.04                0.00            1.00
{l_shipdate}       71118.00       1.61           365.00              1.00                0.00            1.00

----Stats for q6_index_join_4----
column_names       row_count  distinct_count  null_count
{l_discount}       909455     11              0
{l_extendedprice}  909455     565291          0
{l_quantity}       909455     50              0
{l_shipdate}       909455     365             0
~~~~
column_names       row_count_est  row_count_err  distinct_count_est  distinct_count_err  null_count_est  null_count_err
{l_discount}       924788.00      1.02           11.00               1.00                0.00            1.00
{l_extendedprice}  924788.00      1.02           612961.00           1.08                0.00            1.00
{l_quantity}       924788.00      1.02           50.00               1.00                0.00            1.00
{l_shipdate}       924788.00      1.02           365.00              1.00                0.00            1.00

----Stats for q6_scan_5----
column_names    row_count  distinct_count  null_count
{l_linenumber}  909455     7               0
{l_orderkey}    909455     266035          0
{l_shipdate}    909455     365             0
~~~~
column_names    row_count_est  row_count_err  distinct_count_est  distinct_count_err  null_count_est  null_count_err
{l_linenumber}  924788.00      1.02           7.00                1.00                0.00            1.00
{l_orderkey}    924788.00      1.02           736000.00           2.77 <==            0.00            1.00
{l_shipdate}    924788.00      1.02           365.00              1.00                0.00            1.00
----
----
