-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathparquetFilter.js
More file actions
175 lines (161 loc) · 5.37 KB
/
Copy pathparquetFilter.js
File metadata and controls
175 lines (161 loc) · 5.37 KB
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
/**
* Converts a WHERE clause AST to hyparquet filter format.
* Returns undefined if the expression cannot be fully converted.
*
* @param {import('squirreling/src/types.js').ExprNode | undefined} where
* @returns {import('hyparquet').ParquetQueryFilter | undefined}
*/
export function whereToParquetFilter(where) {
if (!where) return undefined
return convertExpr(where, false)
}
/**
* Converts an expression node to filter format
*
* @param {import('squirreling/src/types.js').ExprNode} node
* @param {boolean} negate
* @returns {import('hyparquet').ParquetQueryFilter | undefined}
*/
function convertExpr(node, negate) {
if (node.type === 'unary' && node.op === 'NOT') {
return convertExpr(node.argument, !negate)
}
if (node.type === 'binary') {
return convertBinary(node, negate)
}
if (node.type === 'in valuelist') {
return convertInValues(node, negate)
}
if (node.type === 'cast') {
return convertExpr(node.expr, negate)
}
// Non-convertible types - return undefined to skip optimization
return undefined
}
/**
* Converts a binary expression to filter format
*
* @param {import('squirreling/src/types.js').BinaryNode} node
* @param {boolean} negate
* @returns {import('hyparquet').ParquetQueryFilter | undefined}
*/
function convertBinary({ op, left, right }, negate) {
if (op === 'AND') {
const leftFilter = convertExpr(left, negate)
const rightFilter = convertExpr(right, negate)
if (!leftFilter || !rightFilter) return
return negate
? { $or: [leftFilter, rightFilter] }
: { $and: [leftFilter, rightFilter] }
}
if (op === 'OR') {
const leftFilter = convertExpr(left, false)
const rightFilter = convertExpr(right, false)
if (!leftFilter || !rightFilter) return
return negate
? { $nor: [leftFilter, rightFilter] }
: { $or: [leftFilter, rightFilter] }
}
// LIKE is not supported by hyparquet filters
if (op === 'LIKE') return
// Comparison operators: need identifier on one side and literal on the other
const { column, value, flipped } = extractColumnAndValue(left, right)
if (!column || value === undefined) return
// Map SQL operator to MongoDB operator
const mongoOp = mapOperator(op, flipped, negate)
if (!mongoOp) return
return { [column]: { [mongoOp]: value } }
}
/**
* Extracts column name and literal value from binary operands.
* Handles both "column op value" and "value op column" patterns.
*
* @param {import('squirreling/src/types.js').ExprNode} left
* @param {import('squirreling/src/types.js').ExprNode} right
* @returns {{ column: string | undefined; value: import('squirreling/src/types.js').SqlPrimitive | undefined; flipped: boolean }}
*/
function extractColumnAndValue(left, right) {
// column op value
if (left.type === 'identifier' && right.type === 'literal') {
return { column: left.name, value: right.value, flipped: false }
}
// value op column (flipped)
if (left.type === 'literal' && right.type === 'identifier') {
return { column: right.name, value: left.value, flipped: true }
}
// Neither pattern matches
return { column: undefined, value: undefined, flipped: false }
}
/**
* Maps SQL operator to MongoDB operator, accounting for flipped operands
*
* @param {import('squirreling/src/types.js').BinaryOp} op
* @param {boolean} flipped
* @param {boolean} negate
* @returns {string | undefined}
*/
function mapOperator(op, flipped, negate) {
if (!isBinaryOp(op)) return
let mappedOp = op
if (negate) mappedOp = neg(mappedOp)
if (flipped) mappedOp = flip(mappedOp)
// Symmetric operators (same when flipped)
if (mappedOp === '=') return '$eq'
if (mappedOp === '!=' || mappedOp === '<>') return '$ne'
if (mappedOp === '<') return '$lt'
if (mappedOp === '<=') return '$lte'
if (mappedOp === '>') return '$gt'
if (mappedOp === '>=') return '$gte'
}
/**
* @param {import('squirreling/src/types.js').ComparisonOp} op
* @returns {import('squirreling/src/types.js').ComparisonOp}
*/
function neg(op) {
if (op === '<') return '>='
if (op === '<=') return '>'
if (op === '>') return '<='
if (op === '>=') return '<'
if (op === '=') return '!='
if (op === '!=') return '='
if (op === '<>') return '='
throw new Error(`Unexpected comparison operator: ${op}`)
}
/**
* @param {import('squirreling/src/types.js').ComparisonOp} op
* @returns {import('squirreling/src/types.js').ComparisonOp}
*/
function flip(op) {
if (op === '<') return '>'
if (op === '<=') return '>='
if (op === '>') return '<'
if (op === '>=') return '<='
if (op === '=') return '='
if (op === '!=') return '!='
if (op === '<>') return '='
throw new Error(`Unexpected comparison operator: ${op}`)
}
/**
* @param {string} op
* @returns {op is import('squirreling/src/types.js').ComparisonOp}
*/
export function isBinaryOp(op) {
return ['AND', 'OR', 'LIKE', '=', '!=', '<>', '<', '>', '<=', '>='].includes(op)
}
/**
* Converts IN/NOT IN value list expression to filter format
*
* @param {import('squirreling/src/types.js').InValuesNode} node
* @param {boolean} negate
* @returns {import('hyparquet').ParquetQueryFilter | undefined}
*/
function convertInValues(node, negate) {
if (node.expr.type !== 'identifier') return
// All values must be literals
const values = []
for (const val of node.values) {
if (val.type !== 'literal') return
values.push(val.value)
}
return { [node.expr.name]: { [negate ? '$nin' : '$in']: values } }
}