Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

Status

...

Page properties


Discussion thread

...

...

...

...

Jira
serverASF JIRA
serverId5aa69414-a9e9-3523-82ec-879b028fb15b
keyFLINK-23687

...

Release


Motivation

Look up join is commonly used feature in Flink SQL. We have received many optimization requirements on look up join. For example:
1. Enforces Suggests left side of lookup join do a hash partitioner to raise cache hint ratio

...

To enable hash lookup join, user only needs specify a new hint (useSHUFFLE_hashHASH) in select clause in query, just like use_hash hint in Oraclewhich is similar with spark[2] sql.

Code Block
languagesql
SELECT /*+ USESHUFFLE_HASH('Orders', 'Customers') */ o.order_id, o.total, c.country, c.zip
FROM Orders AS o
JOIN Customers FOR SYSTEM_TIME AS OF o.proc_time AS c
ON o.customer_id = c.id;


Note:

  1. Table

...

  1. name in

...

  1. SHUFFLE_

...

  1. HASH hint is

...

  1. build table name. Lookup join only supports dimension table to be build table, does not support left side to be build table.
  2. The hint only provides a suggestion to the optimization, it is not an enforcer.

Proposed Changes

Define Hint Strategy

"USESHUFFLE_HASH", this hint can only be applied to CORRELATE relations which satisfy the following conditions:

  1. the correlate is a look up join, other correlate would ignore the hint
  2. the correlate has "Orders" and has  "Customers" as the input table namesdimension table name. 

The code below shows how we define hint strategy for hash lookupJoin.

Code Block
languagejava
titleUSESHUFFLE_HASH hint strategy
      builder
        .hintStrategy("USESHUFFLE_HASH",
            HintStrategy.builder(
                HintPredicates.and(HintPredicates.CORRELATE, isLookupJoin(), joinWithFixedTableNamewithBuildTableName())))
        .build();

Note,

it has a blocker on Calcite version upgrade.

Calcite would translate above sql into Correlate  instead of Join . Correlate  is not a kind of RelNode  that can attach RelHint s currently. The hint could not be propagated to the Correlate nodes, eitheruntil 1.30.0 version.

I've report a

Jira
serverASF JIRA
serverId5aa69414-a9e9-3523-82ec-879b028fb15b
keyCALCITE-4967
in Calcite, which has been merged and would be published in CALCITE 1.30.0 version.

Hint Propagation in Optimizer

We need to ensure the hint would not missed before it is finally used to enforce require the distribution on inputs of LookupJoin. 

...

LookupJoinRules would check whether FlinkLogicalJoin contain USESHUFFLE_HASH Hint. If yes, and the rules require the input must have hash distribution on join keys when converting FlinkLogicalJoin to LookupJoin.

...

So hash lookup Join requires that the input stream should be insert_only stream or it's its upsert keys contains hash keys.

...

Besides, we would add integration tests for connectors to verify it can cooperate with existing source/sink implementations.

...

Rejected Alternatives


Hint Syntax


We we could use name 'SHUFFLEuse_HASHhash' just like spark[2] sqlhint in Oracle.

Code Block
languagesql
SELECT /*+ SHUFFLEUSE_HASH('Orders', 'Customers') */ o.order_id, o.total, c.country, c.zip
FROM Orders AS o
JOIN Customers FOR SYSTEM_TIME AS OF o.proc_time AS c
ON o.customer_id = c.id;


SQL Server[34] uses keyword 'hash' instead of query hint, it's not a good choise for use, so we ignore this.

...

There is a simpler but a little hack hacky implementation, this is also what we apply in the internal version.

That is, propagating 'useSHUFFLE_hashHASH' hint to TableScan  with matched table names. In this way, the hint would not be missed by Flink optimizer until it needs the hint in  LookupJoinRules.

...

Compared with the previous solution, this solution has two advantages:

  1. It does not need Calcite version upgrade, see
    Jira
    serverASF JIRA
    serverId5aa69414-a9e9-3523-82ec-879b028fb15b
    keyCALCITE-4967
  2. It does not need code refactor to ensure the hint would not be missed by Flink optimizer.

...

Anyway, the difference between the two solution is only about the internal implementation and has no impact on the user.

Reference

[1] Oracle Hash hint syntaxUSE_Hash hint

Code Block
languagesql
SELECT /*+ USE_HASH(l h) */ *
  FROM orders h, order_items l
  WHERE l.order_id = h.order_id
    AND l.order_id > 3500;


[2] Spark Hash hint syntax[3] SQL Server Hash Keyword syntaxSHUFFLE_HASH hint

Code Block
languagesql
SELECT /*+ SHUFFLE_HASH(t1) */ * FROM t1 INNER JOIN t2 ON t1.key = t2.key;

[3] IMPALA SHUFFLE hint

Code Block
languagesql
SELECT straight_join weather.wind_velocity, geospatial.altitude
  FROM weather JOIN /* +SHUFFLE */ geospatial
  ON weather.lat = geospatial.lat AND weather.long = geospatial.long;


[4] SQL Server Hash Keyword

Code Block
languagesql
SELECT p.Name, pr.ProductReviewID FROM Production.Product AS p LEFT OUTER HASH JOIN Production.ProductReview AS pr ON p.ProductID = pr.ProductID ORDER BY ProductReviewID DESC;