我试图在列上使用rowsBetween(-1,0),但似乎不起作用。但是当我使用滞后时,它工作正常。
我的问题是VersionNumber
和sealed trait NaturalNumber
class _3 extends NaturalNumber
class _2 extends _3
class _1 extends _2
class _0 extends _1
abstract class VersionNumber {
type Nat <: NaturalNumber
def toInt: Int
}
val __0 = new VersionNumber { type Nat = _0 ; def toInt = 0 }
val __1 = new VersionNumber { type Nat = _1 ; def toInt = 1 }
val __2 = new VersionNumber { type Nat = _2 ; def toInt = 2 }
val __3 = new VersionNumber { type Nat = _3 ; def toInt = 3 }
object VersionNumber {
type Aux[N <: NaturalNumber] = VersionNumber {
type Nat = N
}
}
type IsEqual[A, B] = A =:= B
type IsLessOrEqual[A, B] = A <:< B
abstract class VersionInfo {
type Major <: NaturalNumber
type Minor <: NaturalNumber
val major: VersionNumber.Aux[Major]
val minor: VersionNumber.Aux[Minor]
}
object VersionInfo {
type Aux[A <: NaturalNumber, B <: NaturalNumber] = VersionInfo {
type Major = A
type Minor = B
}
def apply[A <: NaturalNumber, B <: NaturalNumber](
a: VersionNumber { type Nat = A },
b: VersionNumber { type Nat = B }
): VersionInfo.Aux[A, B] = new VersionInfo {
type Major = A
type Minor = B
val major = a
val minor = b
}
}
abstract class ResourceManifest {
type Major <: NaturalNumber
type Minor <: NaturalNumber
val major: VersionNumber.Aux[Major]
val minor: VersionNumber.Aux[Minor]
def getResource: Int = major.toInt * 10 + minor.toInt
}
object ResourceManifest {
type Aux[A <: NaturalNumber, B <: NaturalNumber] = ResourceManifest {
type Major = A
type Minor = B
}
def apply[A <: NaturalNumber, B <: NaturalNumber](
a: VersionNumber { type Nat = A },
b: VersionNumber { type Nat = B }
): ResourceManifest.Aux[A, B] = new ResourceManifest {
type Major = A
type Minor = B
val major = a
val minor = b
}
}
def getResource[
MnfMaj <: NaturalNumber,
MnfMin <: NaturalNumber,
VrsMaj <: NaturalNumber,
VrsMin <: NaturalNumber
]
(manifest: ResourceManifest.Aux[MnfMaj, MnfMin])
(versionInfo: VersionInfo.Aux[VrsMaj, VrsMin])
(implicit
maj_check: MnfMaj IsEqual VrsMaj,
min_check: MnfMin IsLessOrEqual VrsMin
)
: Unit = println("it compiles, ship it")
val manifest21 = ResourceManifest(__2, __1)
val manifest22 = ResourceManifest(__2, __2)
getResource(manifest21)(VersionInfo(__2, __1))
getResource(manifest21)(VersionInfo(__2, __2))
// getResource(manifest22)(VersionInfo(__2, __1)) // still won't compile, good
getResource(manifest22)(VersionInfo(__2, __2))
是否相同?
更新:
我使用rowsBetween的原因是我正在尝试转换lag
rowsBetween(-1,0)
等于Teradata [ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING]
吗?
示例代码:
lag
输出:
ROWS BETWEEN 1 PRECEDING AND 1 PRECEDING
行之间有行
df.show()
w = Window.partitionBy(df['ResId']).orderBy(df['vrsn_strt_dts']).rowsBetween(-1, 0)
lagWin = Window.partitionBy(df['ResId']).orderBy(df['vrsn_strt_dts'])
df.withColumn("vrsn_end_dts", max(df['vrsn_strt_dts']).over(w)).show()
df.withColumn("vrsn_end_dts", lag(df['vrsn_strt_dts']).over(lagWin)).show()
具有滞后输出
+--------+-------------------+---------+------------+-------+----------+
| ResId| vrsn_strt_dts|instnc_nm|ship_strm_nb|vyge_id|onhand_cnt|
+--------+-------------------+---------+------------+-------+----------+
|27608171|2018-07-17 04:00:00| Standard| 11B| 1000| 10|
|27608174|2018-08-17 04:00:00| Standard| 11C| 2000| 20|
|27608173|2018-09-17 04:00:00| Standard| 11D| 3000| 30|
|27608171|2018-09-17 04:00:00| Standard| 11B| 1000| 40|
|27608174|2018-09-17 04:00:00| Standard| 11D| 5000| 50|
|27608171|2018-09-18 04:00:00| Standard| 11B| 1000| 10|
|27608171|2018-09-19 04:00:00| Standard| 11B| 1000| 10|
+--------+-------------------+---------+------------+-------+----------+