jayzhan211 commented on code in PR #23831:
URL: https://github.com/apache/datafusion/pull/23831#discussion_r3979495871
##########
datafusion/core/src/dataframe/mod.rs:
##########
@@ -1380,6 +1380,46 @@ impl DataFrame {
})
}
+ /// Join this `DataFrame` to the closest eligible row in `right`.
+ ///
+ /// Every left row is emitted exactly once. When present, `on` must contain
+ /// equality comparisons combined with `AND`. `match_condition` must be a
+ /// single `<`, `<=`, `>`, or `>=` comparison whose left and right operands
+ /// reference this `DataFrame` and `right`, respectively.
Review Comment:
Doc example might be helpful, both for `join_asof` and `join_asof_using`. It
would be nice if we can know the difference easily through the example
```
/// Join this `DataFrame` to the closest eligible row in `right`.
///
/// Every left row is emitted exactly once; when no right row matches,
the
/// right columns are `NULL`. A `NULL` in the ordered expression or in
any
/// equality key never matches.
///
/// When present, `on` must contain equality comparisons combined with
/// `AND`. `match_condition` must be a single `<`, `<=`, `>`, or `>=`
/// comparison whose left and right operands reference this `DataFrame`
and
/// `right`, respectively.
///
/// # Example
/// ```
/// # use datafusion::prelude::*;
/// # use datafusion::error::Result;
/// # use datafusion_common::assert_batches_sorted_eq;
/// # #[tokio::main]
/// # async fn main() -> Result<()> {
/// let ctx = SessionContext::new();
/// let trades = ctx.read_batch(record_batch!(
/// ("symbol", Utf8, ["A"]),
/// ("ts", Int64, [4])
/// )?)?.alias("trades")?;
/// let prices = ctx.read_batch(record_batch!(
/// ("symbol", Utf8, ["A"]),
/// ("ts", Int64, [2]),
/// ("price", Int32, [20])
/// )?)?.alias("prices")?;
///
/// // For each trade, the most recent price at or before the trade time.
/// let joined = trades.join_asof(
/// prices,
/// Some(col("trades.symbol").eq(col("prices.symbol"))),
/// col("trades.ts").gt_eq(col("prices.ts")),
/// )?;
/// # Ok(())
/// # }
/// ```
```
##########
datafusion/core/tests/dataframe/mod.rs:
##########
@@ -1521,6 +1521,75 @@ async fn join() -> Result<()> {
Ok(())
}
+#[tokio::test]
+async fn join_asof() -> Result<()> {
+ let ctx = SessionContext::new();
+ let left = ctx
+ .read_batch(record_batch!(
+ ("symbol", Utf8, ["A", "A", "B"]),
+ ("ts", Int64, [1, 4, 2]),
+ ("trade_id", Int32, [1, 2, 3])
+ )?)?
+ .alias("trades")?;
+ let right = ctx
+ .read_batch(record_batch!(
+ ("symbol", Utf8, ["A", "A", "B"]),
+ ("ts", Int64, [2, 4, 1]),
+ ("price", Int32, [20, 40, 101])
+ )?)?
+ .alias("prices")?;
+
+ let results = left
+ .clone()
+ .join_asof(
+ right.clone(),
+ Some(col("trades.symbol").eq(col("prices.symbol"))),
+ col("trades.ts").gt_eq(col("prices.ts")),
+ )?
+ .select(vec![col("trade_id"), col("price")])?
+ .sort(vec![col("trade_id").sort(true, true)])?
+ .collect()
+ .await?;
+
+ assert_batches_eq!(
+ [
+ "+----------+-------+",
+ "| trade_id | price |",
+ "+----------+-------+",
+ "| 1 | |",
+ "| 2 | 40 |",
+ "| 3 | 101 |",
+ "+----------+-------+",
+ ],
+ &results
+ );
+
+ let results = left
+ .join_asof_using(
+ right,
+ vec![datafusion_common::Column::from_name("symbol")],
+ col("trades.ts").gt_eq(col("prices.ts")),
+ )?
+ .select(vec![col("trade_id"), col("price")])?
Review Comment:
It would be nice to add "symbol" for USING syntax, so more test coverage
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]