== Abstract Syntax Tree ==
LogicalSink(table=[default_catalog.default_database.sink], fields=[category, EXPR$1])
+- LogicalAggregate(group=[{0}], EXPR$1=[AVG($1)])
   +- LogicalProject(category=[$1], final=[$0])
      +- LogicalProject(final=[$2], category=[$1])
         +- LogicalAggregate(group=[{0, 1}], final=[MAX($2)])
            +- LogicalProject(id=[$0], category=[$8], price=[$12])
               +- LogicalFilter(condition=[AND(=($0, $10), >=($15, $5), <=($15, $6))])
                  +- LogicalJoin(condition=[true], joinType=[inner])
                     :- LogicalProject(id=[$2.id], itemName=[$2.itemName], description=[$2.description], initialBid=[$2.initialBid], reserve=[$2.reserve], dateTime=[$5], expires=[TO_TIMESTAMP_LTZ($2.expires, 3)], seller=[$2.seller], category=[$2.category], extra=[$2.extra])
                     :  +- LogicalFilter(condition=[=($0, 1)])
                     :     +- LogicalWatermarkAssigner(rowtime=[rowtime], watermark=[-($5, 4000:INTERVAL SECOND)])
                     :        +- LogicalProject(event_type=[$0], person=[$1], auction=[$2], bid=[$3], dateTime=[$4], rowtime=[TO_TIMESTAMP_LTZ($4, 3)])
                     :           +- LogicalTableScan(table=[[default_catalog, default_database, src]])
                     +- LogicalProject(auction=[$3.auction], bidder=[$3.bidder], price=[$3.price], channel=[$3.channel], url=[$3.url], dateTime=[$5], extra=[$3.extra])
                        +- LogicalFilter(condition=[=($0, 2)])
                           +- LogicalWatermarkAssigner(rowtime=[rowtime], watermark=[-($5, 4000:INTERVAL SECOND)])
                              +- LogicalProject(event_type=[$0], person=[$1], auction=[$2], bid=[$3], dateTime=[$4], rowtime=[TO_TIMESTAMP_LTZ($4, 3)])
                                 +- LogicalTableScan(table=[[default_catalog, default_database, src]])

== Optimized Physical Plan ==
Sink(table=[default_catalog.default_database.sink], fields=[category, EXPR$1])
+- GroupAggregate(groupBy=[category], select=[category, AVG_RETRACT(final) AS EXPR$1])
   +- Exchange(distribution=[hash[category]])
      +- Calc(select=[category, final])
         +- GroupAggregate(groupBy=[id, category], select=[id, category, MAX(price) AS final])
            +- Exchange(distribution=[hash[id, category]])
               +- Calc(select=[id, category, price])
                  +- Join(joinType=[InnerJoin], where=[AND(=(id, auction), >=(dateTime0, dateTime), <=(dateTime0, expires))], select=[id, dateTime, expires, category, auction, price, dateTime0], leftInputSpec=[NoUniqueKey], rightInputSpec=[NoUniqueKey])
                     :- Exchange(distribution=[hash[id]])
                     :  +- Calc(select=[auction.id AS id, CAST(Reinterpret(TO_TIMESTAMP_LTZ(dateTime, 3)) AS TIMESTAMP_WITH_LOCAL_TIME_ZONE(3)) AS dateTime, TO_TIMESTAMP_LTZ(auction.expires, 3) AS expires, auction.category AS category], where=[=(event_type, 1)])
                     :     +- TableSourceScan(table=[[default_catalog, default_database, src, watermark=[-(TO_TIMESTAMP_LTZ(dateTime, 3), 4000:INTERVAL SECOND)], watermarkEmitStrategy=[on-periodic]]], fields=[event_type, person, auction, bid, dateTime])
                     +- Exchange(distribution=[hash[auction]])
                        +- Calc(select=[bid.auction AS auction, bid.price AS price, CAST(Reinterpret(TO_TIMESTAMP_LTZ(dateTime, 3)) AS TIMESTAMP_WITH_LOCAL_TIME_ZONE(3)) AS dateTime], where=[=(event_type, 2)])
                           +- TableSourceScan(table=[[default_catalog, default_database, src, watermark=[-(TO_TIMESTAMP_LTZ(dateTime, 3), 4000:INTERVAL SECOND)], watermarkEmitStrategy=[on-periodic]]], fields=[event_type, person, auction, bid, dateTime])

== Optimized Execution Plan ==
Sink(table=[default_catalog.default_database.sink], fields=[category, EXPR$1])
+- GroupAggregate(groupBy=[category], select=[category, AVG_RETRACT(final) AS EXPR$1])
   +- Exchange(distribution=[hash[category]])
      +- Calc(select=[category, final])
         +- GroupAggregate(groupBy=[id, category], select=[id, category, MAX(price) AS final])
            +- Exchange(distribution=[hash[id, category]])
               +- Calc(select=[id, category, price])
                  +- Join(joinType=[InnerJoin], where=[((id = auction) AND (dateTime0 >= dateTime) AND (dateTime0 <= expires))], select=[id, dateTime, expires, category, auction, price, dateTime0], leftInputSpec=[NoUniqueKey], rightInputSpec=[NoUniqueKey])
                     :- Exchange(distribution=[hash[id]])
                     :  +- Calc(select=[auction.id AS id, CAST(Reinterpret(TO_TIMESTAMP_LTZ(dateTime, 3)) AS TIMESTAMP_WITH_LOCAL_TIME_ZONE(3)) AS dateTime, TO_TIMESTAMP_LTZ(auction.expires, 3) AS expires, auction.category AS category], where=[(event_type = 1)])
                     :     +- TableSourceScan(table=[[default_catalog, default_database, src, watermark=[-(TO_TIMESTAMP_LTZ(dateTime, 3), 4000:INTERVAL SECOND)], watermarkEmitStrategy=[on-periodic]]], fields=[event_type, person, auction, bid, dateTime])(reuse_id=[1])
                     +- Exchange(distribution=[hash[auction]])
                        +- Calc(select=[bid.auction AS auction, bid.price AS price, CAST(Reinterpret(TO_TIMESTAMP_LTZ(dateTime, 3)) AS TIMESTAMP_WITH_LOCAL_TIME_ZONE(3)) AS dateTime], where=[(event_type = 2)])
                           +- Reused(reference_id=[1])

== Physical Execution Plan ==
{
  "nodes" : [ {
    "id" : 1281,
    "type" : "Source: src[583]",
    "pact" : "Data Source",
    "contents" : "[583]:TableSourceScan(table=[[default_catalog, default_database, src, watermark=[-(TO_TIMESTAMP_LTZ(dateTime, 3), 4000:INTERVAL SECOND)], watermarkEmitStrategy=[on-periodic]]], fields=[event_type, person, auction, bid, dateTime])",
    "parallelism" : 4
  }, {
    "id" : 1282,
    "type" : "Calc[584]",
    "pact" : "Operator",
    "contents" : "[584]:Calc(select=[auction.id AS id, CAST(Reinterpret(TO_TIMESTAMP_LTZ(dateTime, 3)) AS TIMESTAMP_WITH_LOCAL_TIME_ZONE(3)) AS dateTime, TO_TIMESTAMP_LTZ(auction.expires, 3) AS expires, auction.category AS category], where=[(event_type = 1)])",
    "parallelism" : 4,
    "predecessors" : [ {
      "id" : 1281,
      "ship_strategy" : "FORWARD",
      "side" : "second"
    } ]
  }, {
    "id" : 1284,
    "type" : "Calc[586]",
    "pact" : "Operator",
    "contents" : "[586]:Calc(select=[bid.auction AS auction, bid.price AS price, CAST(Reinterpret(TO_TIMESTAMP_LTZ(dateTime, 3)) AS TIMESTAMP_WITH_LOCAL_TIME_ZONE(3)) AS dateTime], where=[(event_type = 2)])",
    "parallelism" : 4,
    "predecessors" : [ {
      "id" : 1281,
      "ship_strategy" : "FORWARD",
      "side" : "second"
    } ]
  }, {
    "id" : 1286,
    "type" : "Join[588]",
    "pact" : "Operator",
    "contents" : "[588]:Join(joinType=[InnerJoin], where=[((id = auction) AND (dateTime0 >= dateTime) AND (dateTime0 <= expires))], select=[id, dateTime, expires, category, auction, price, dateTime0], leftInputSpec=[NoUniqueKey], rightInputSpec=[NoUniqueKey])",
    "parallelism" : 4,
    "predecessors" : [ {
      "id" : 1282,
      "ship_strategy" : "HASH",
      "side" : "second"
    }, {
      "id" : 1284,
      "ship_strategy" : "HASH",
      "side" : "second"
    } ]
  }, {
    "id" : 1287,
    "type" : "Calc[589]",
    "pact" : "Operator",
    "contents" : "[589]:Calc(select=[id, category, price])",
    "parallelism" : 4,
    "predecessors" : [ {
      "id" : 1286,
      "ship_strategy" : "FORWARD",
      "side" : "second"
    } ]
  }, {
    "id" : 1289,
    "type" : "GroupAggregate[591]",
    "pact" : "Operator",
    "contents" : "[591]:GroupAggregate(groupBy=[id, category], select=[id, category, MAX(price) AS final])",
    "parallelism" : 4,
    "predecessors" : [ {
      "id" : 1287,
      "ship_strategy" : "HASH",
      "side" : "second"
    } ]
  }, {
    "id" : 1290,
    "type" : "Calc[592]",
    "pact" : "Operator",
    "contents" : "[592]:Calc(select=[category, final])",
    "parallelism" : 4,
    "predecessors" : [ {
      "id" : 1289,
      "ship_strategy" : "FORWARD",
      "side" : "second"
    } ]
  }, {
    "id" : 1292,
    "type" : "GroupAggregate[594]",
    "pact" : "Operator",
    "contents" : "[594]:GroupAggregate(groupBy=[category], select=[category, AVG_RETRACT(final) AS EXPR$1])",
    "parallelism" : 4,
    "predecessors" : [ {
      "id" : 1290,
      "ship_strategy" : "HASH",
      "side" : "second"
    } ]
  }, {
    "id" : 1293,
    "type" : "ConstraintEnforcer[595]",
    "pact" : "Operator",
    "contents" : "[595]:ConstraintEnforcer[NotNullEnforcer(fields=[id])]",
    "parallelism" : 4,
    "predecessors" : [ {
      "id" : 1292,
      "ship_strategy" : "FORWARD",
      "side" : "second"
    } ]
  }, {
    "id" : 1294,
    "type" : "Map",
    "pact" : "Operator",
    "contents" : "Map",
    "parallelism" : 4,
    "predecessors" : [ {
      "id" : 1293,
      "ship_strategy" : "FORWARD",
      "side" : "second"
    } ]
  }, {
    "id" : 1296,
    "type" : "Writer : table",
    "pact" : "Operator",
    "contents" : "Writer : table",
    "parallelism" : 4,
    "predecessors" : [ {
      "id" : 1294,
      "ship_strategy" : "shuffle by bucket",
      "side" : "second"
    } ]
  }, {
    "id" : 1297,
    "type" : "Global Committer : table",
    "pact" : "Operator",
    "contents" : "Global Committer : table",
    "parallelism" : 1,
    "predecessors" : [ {
      "id" : 1296,
      "ship_strategy" : "REBALANCE",
      "side" : "second"
    } ]
  }, {
    "id" : 1304,
    "type" : "end: Writer",
    "pact" : "Operator",
    "contents" : "end: Writer",
    "parallelism" : 1,
    "predecessors" : [ {
      "id" : 1297,
      "ship_strategy" : "FORWARD",
      "side" : "second"
    } ]
  } ]
}