百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 编程网 > 正文

Spark 开窗源码讲解(二) rownumber

yuyutoo 2024-10-12 01:20 4 浏览 0 评论

我们平常使用开窗函数会使用row_number这个函数,目的是根据某个字段分区,然后根据某个字段排序,然后底层的逻辑是怎么样的。

还是看WindowExec 作为入口,然后看windowFrameExpressionFactoryPairs 追溯到它的父类WindowExecBase,看到内部的定义,然后用户可以在代码编辑中使用row_number函数,然后打断点。

可以定位到对应的frame


走的是UnboundedPrecedingWindowFunctionFrame,然后关键核心处就是这个排序。

frameType:rowframe
createBoundOrdering(frameType, upper, timeZone)

然后看这个实现:


说明对应的逻辑在 RowBoundOrdering(0)

private[window] final case class RowBoundOrdering(offset: Int) extends BoundOrdering {
  override def compare(
      inputRow: InternalRow,
      inputIndex: Int,
      outputRow: InternalRow,
      outputIndex: Int): Int =
    inputIndex - (outputIndex + offset)
}

然后看UnboundedPrecedingWindowFunctionFrame的write,在write中就是进行产生这个行号


,可以看到这里的compare方法,这里的 index 就是 上游WindowExecBase中的传入值。

var rowIndex = 0

override final def hasNext: Boolean =
  (bufferIterator != null && bufferIterator.hasNext) || nextRowAvailable

val join = new JoinedRow
override final def next(): InternalRow = {
  // Load the next partition if we need to.
  if ((bufferIterator == null || !bufferIterator.hasNext) && nextRowAvailable) {
    fetchNextPartition()
  }

  if (bufferIterator.hasNext) {
    val current = bufferIterator.next()

    // Get the results for the window frames.
    var i = 0
    while (i < numFrames) {
      frames(i).write(rowIndex, current)
      i += 1
    }

    // 'Merge' the input row with the window function result
    join(current, windowFunctionResult)
    rowIndex += 1

    // Return the projection.
    result(join)
  } else {
    throw new NoSuchElementException
  }
}

这里的 index = outputIndex ,看它的定义,就是逐行遍历增加。

然后看 inputIndex,这个值就是 这个分区内这个组中的当前行的序号,不是分区开始的序号
index是 分区内的序号
inputIndex就是分区内某个组(partitionBy 后面字段的内容的值)的序号。

现在模拟下场景。

+---+---+
| id| kk|
+---+---+
|  1|  1|
|  1|  9|
|  1|  3|
|  1|  1|
+---+---+
这是明细,然后根据 ID进行partitionBy,根据kk排序
第一步:重分区,根据hashpartition 分,ID=1的都分入了同一个分区中
第二步:根据 ID + kk 进行排序,这样数据的顺序从
+---+---+
| id| kk|
+---+---+
|  1|  1|
|  1|  9|
|  1|  3|
|  1|  1|
+---+---+
变成了
+---+---+
| id| kk|
+---+---+
|  1|  1|
|  1|  1|
|  1|  3|
|  1|  9|
+---+---+
第三步:
开始遍历这个分区内部的数据
然后调用 frames(i).write(rowIndex, current)
-------------
override def write(index: Int, current: InternalRow): Unit = {
    var bufferUpdated = index == 0

    // Add all rows to the aggregates for which the input row value is equal to or less than
    // the output row upper bound.
    while (nextRow != null && ubound.compare(nextRow, inputIndex, current, index) <= 0) {
      if (processor != null) {
        processor.update(nextRow)
      }
      nextRow = WindowFunctionFrame.getNextOrNull(inputIterator)
      inputIndex += 1
      bufferUpdated = true
    }

    // Only recalculate and update when the buffer changes.
    if (processor != null && bufferUpdated) {
      processor.evaluate(target)
    }
  }
 此时进入的current 就是(1,1)。nextRow=(1,1),inputIndex=0,index=0,
   ubound.compare(nextRow, inputIndex, current, index) 变成了 inputIndex - (outputIndex + offset),其中offset=0,等价于 inputIndex - outputIndex,
那么返回的值就是0,0<=0 返回的就是ture.那么processor.update(nextRow),就产生了一个rownumber=1
此时nextRow 从(1,1) 变成了 (1,1)
然后进入一次循环
此时进入的current就是(1,1),nextRow=(1,1),inputIndex=1,index=1,ubound.compare(nextRow, inputIndex, current, index)返回的依旧是0,
那么processor.update(nextRow),就产生了一个rownumber=2。
此时nextRow 从(1,1) 变成了 (1,3)
然后进入一次循环
此时进入的current就是(1,3),nextRow=(1,3),产生一个rownumber=3
此时nextRow 从(1,3) 变成了 (1,9)
然后进入一次循环
此时进入的current就是(1,9),nextRow=(1,9),产生一个rownumber=4
此时nextRow 从(1,9) 变成了 null

相关推荐

网络规划建设原来也可以这么简单!

废话少说,直接上干货。天气炎热,请各位看官老爷静心阅读。整体思路下图是关于网络建设的所有相关领域,接下来我为大家逐一讲解。网络分层...

网络规划设计师笔记-第 1 章 计算机网络原理

计算机网络原理1.1计算机网络概论(P1-10)...

别输在远见上,网工这样做职业规划,比啥都强

01职业中的规划,人生中的buff“职业规划“这个词,其实对很多年轻人,包括曾经年轻的我来说,都不屑一提。...

网络规划设计师学习中(个人自学笔记分享1),有一起学习的吗?

网络规划设计师,上午考试内容学习:第一章:计算机网络概述(上部分):如果你也在一起学习,那么我们来一起学习吧!坚持1年,争取明年一次性通过!...

在微服务中使用 ASP.NET Core 实现事件溯源和 CQRS

概述:事件溯源和命令查询责任分离(CQRS)已成为解决微服务设计的复杂性的强大架构模式。基本CQRS表示形式在本文中,我们将探讨ASP.NETCore如何使你能够将事件溯源和CQRS...

一个基于ASP.NET Core完全开源的CMS 解决方案

...

用 Nginx 部署 ASP.NET Core 应用程序

用Nginx部署ASP.NETCore应用程序步骤如下:在Linux中安装.NETCore运行时和Nginx:...

Asp.net Core启动流程讲解(一)(asp.net core 入门)

asp.netcore默认项目包括项目根目录级的Startup.cs、Program.cs、appsettings.json(appsettings.Development.json)launch...

十天学会ASP之第五天(十天学会asp教程)

学习目的:学会数据库的基本操作1(写入记录)数据库的基本操作无非是:查询记录,写入记录,删除记录,修改记录。今天我们先学习写入记录。先建立一个表单:<formname="form1"met...

ASP.NET Core 的 WebApplication 类

ASP.NETCore提供了3个主机类(Host)。这些类用于配置应用、管理生命周期和启动Web服务。...

ASP.NET Core中的键控依赖注入(.net依赖注入原理)

大家好,我是深山踏红叶,今天我们来聊一聊ASP.NETCore中的FromKeyedServices,它是在.Net8中引入的。这一特性允许通过键(如字符串或枚举)来注册和检索依赖注入(D...

Asp.net常用方法及request和response-a

asp.net教程asp.net常用方法:1、Request.UrlReferrer请求的来源,可以根据这个判断从百度搜的哪个关键词、防下载盗链、防图片盗链,可以伪造(比如迅雷)。(使用全局一般处理...

ASP.NET Core EFCore 属性配置与DbContext 详解

...

asp.net常考面试题(aspnet题库)

asp.net常考面试题一,列举ASP.Net页面之间传递值的几种方式?1,使用QueryString,如:......?id=1;response.Redirect()......2,使用Sessi...

在Windows系统搭建.NET Core环境并创建运行ASP.NET网站

微软于6月27日在红帽DevNation峰会上正式发布了.NETCore1.0、ASP.NET1.0和EntityFrameworkCore1.0,其将全部支持Windows、OSX和...

取消回复欢迎 发表评论: