forked from Jonathan-Rosenberg/delta-go
-
Notifications
You must be signed in to change notification settings - Fork 1
/
partition_utils.go
85 lines (72 loc) · 2.2 KB
/
partition_utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package util
import (
"strings"
"github.com/csimplestring/delta-go/types"
mapset "github.com/deckarep/golang-set/v2"
"github.com/repeale/fp-go"
"github.com/samber/mo"
)
func SplitMetadataAndDataPredicates(condition types.Expression,
partitionColumns []string) (mo.Option[types.Expression], mo.Option[types.Expression]) {
var metadataPredicates []types.Expression
var dataPredicates []types.Expression
for _, p := range SplitConjunctivePredicates(condition) {
if IsPredicateMetadataOnly(p, partitionColumns) {
metadataPredicates = append(metadataPredicates, p)
} else {
dataPredicates = append(dataPredicates, p)
}
}
var metadataConjunction mo.Option[types.Expression]
if len(metadataPredicates) == 0 {
metadataConjunction = mo.None[types.Expression]()
} else {
reducer := func(acc types.Expression, curr types.Expression) types.Expression {
if acc == nil {
// return the first expression
return curr
}
return types.NewAnd(acc, curr)
}
metadataConjunction = mo.Some(fp.Reduce(reducer, nil)(metadataPredicates))
}
var dataConjunction mo.Option[types.Expression]
if len(dataPredicates) == 0 {
dataConjunction = mo.None[types.Expression]()
} else {
reducer := func(acc types.Expression, curr types.Expression) types.Expression {
if acc == nil {
// return the first expression
return curr
}
return types.NewAnd(acc, curr)
}
dataConjunction = mo.Some(fp.Reduce(reducer, nil)(dataPredicates))
}
return metadataConjunction, dataConjunction
}
func SplitConjunctivePredicates(condition types.Expression) []types.Expression {
switch v := condition.(type) {
case *types.And:
return append(SplitConjunctivePredicates(v.Left), SplitConjunctivePredicates(v.Right)...)
default:
return []types.Expression{condition}
}
}
func IsPredicateMetadataOnly(condition types.Expression, partitionColumns []string) bool {
lowercasePartCols := mapset.NewSet(fp.Map(func(v string) string { return strings.ToLower(v) })(partitionColumns)...)
columns := condition.References()
return lowercasePartCols.Contains(columns...)
}
func MaxInt64(x int64, y int64) int64 {
if x > y {
return x
}
return y
}
func MinInt64(x int64, y int64) int64 {
if x < y {
return x
}
return y
}