Uploaded image for project: 'Calcite'
  1. Calcite
  2. CALCITE-1757

Convert sub-query to windowed aggregate function ("WinMagic")

    XMLWordPrintableJSON

Details

    • Bug
    • Status: Open
    • Major
    • Resolution: Unresolved
    • None
    • None
    • None
    • None

    Description

      The WinMagic paper describes how to convert sub-queries into windowed aggregate functions.

      For example, TPC-H query

      SELECT SUM(l_extendedprice) / 7.0 AS avg_yearly
      FROM   tpcd.lineitem, tpcd.part
      WHERE p_partkey = l_partkey
      AND p_brand = 'Brand#23'
      AND p_container = 'MED BOX' 
      AND l_quantity < (
          SELECT 0.2 * avg(l_quantity)
          FROM tpcd.lineitem
          WHERE l_partkey = p_partkey)
      

      becomes

      WITH WinMagic AS (
          SELECT l_extendedprice, l_quantity,
              avg(l_quantity) OVER (PARTITION BY p_partkey) AS avg_l_quantity
          FROM  tpcd.lineitem, tpcd.part
          WHERE p_partkey = l_partkey
          AND p_brand = 'Brand#23'
          AND p_container =  'MED BOX')
       SELECT SUM(l_extendedprice) / 7.0 as avg_yearly
       FROM WinMagic
       WHERE l_quantity < 0.2 * avg_l_quantity;
      

      It is applicable to several TPC-H and TPC-DS queries, and also helps with temporal database queries (e.g. effective dates).

      Attachments

        Issue Links

          Activity

            People

              Unassigned Unassigned
              julianhyde Julian Hyde
              Votes:
              0 Vote for this issue
              Watchers:
              7 Start watching this issue

              Dates

                Created:
                Updated: