-
Notifications
You must be signed in to change notification settings - Fork 448
Expand file tree
/
Copy pathutils.go
More file actions
198 lines (170 loc) · 7.05 KB
/
Copy pathutils.go
File metadata and controls
198 lines (170 loc) · 7.05 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
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
// Copyright(C) 2026 InfiniFlow, Inc. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package infinity
import (
"fmt"
"regexp"
"strings"
thriftapi "github.com/infiniflow/infinity-go-sdk/internal/thrift"
)
// ValidNamePattern is the pattern for valid names
const ValidNamePattern = `^[a-zA-Z][a-zA-Z0-9_]*$`
var validNameRegex = regexp.MustCompile(ValidNamePattern)
// CheckValidName checks if a name is valid
func CheckValidName(name string, entityType string) error {
if strings.TrimSpace(name) == "" {
return NewInfinityException(
int(ErrorCodeEmptyDBName),
fmt.Sprintf("%s name cannot be empty", entityType),
)
}
if !validNameRegex.MatchString(name) {
return NewInfinityException(
int(ErrorCodeInvalidIdentifierName),
fmt.Sprintf("Invalid %s name: %s", entityType, name),
)
}
return nil
}
// NameValidityCheckDecorator returns a decorator-like function that checks name validity before executing the actual function
func NameValidityCheckDecorator(nameArg string, entityType string, fn func(...interface{}) (interface{}, error)) func(...interface{}) (interface{}, error) {
return func(args ...interface{}) (interface{}, error) {
// Find the name argument and check its validity
// This is a simplified version; in real usage, you'd need to match by parameter name
for _, arg := range args {
if name, ok := arg.(string); ok {
if err := CheckValidName(name, entityType); err != nil {
return nil, err
}
break
}
}
return fn(args...)
}
}
// EscapeString escapes special characters in a string for use in queries
func EscapeString(s string) string {
s = strings.ReplaceAll(s, `\`, `\\`)
s = strings.ReplaceAll(s, `'`, `\'`)
s = strings.ReplaceAll(s, `"`, `\"`)
return s
}
// QuoteIdentifier quotes an identifier (table name, column name, etc.)
func QuoteIdentifier(name string) string {
return fmt.Sprintf(`"%s"`, strings.ReplaceAll(name, `"`, `\"`))
}
// QuoteString quotes a string literal
func QuoteString(s string) string {
return fmt.Sprintf("'%s'", EscapeString(s))
}
// DeprecatedAPI logs a deprecation warning for deprecated APIs
func DeprecatedAPI(oldMethod string, newMethod string) {
fmt.Printf("WARNING: %s is deprecated, please use %s instead\n", oldMethod, newMethod)
}
// BuildResult builds a result from the response
func BuildResult(res interface{}) (map[string][]interface{}, map[string]interface{}, error) {
// This is a placeholder implementation
// In the actual implementation, this would parse the response and build the result
return nil, nil, nil
}
// SelectResToPolars converts a select result to a Polars-like format
func SelectResToPolars(res interface{}) (interface{}, error) {
// This is a placeholder implementation
// In the actual implementation, this would convert the result to a Polars-like format
return res, nil
}
// TraverseConditions traverses SQL conditions and converts them to expressions
func TraverseConditions(cond interface{}) interface{} {
// This is a placeholder implementation
// In the actual implementation, this would traverse SQL conditions
return cond
}
// GetRemoteConstantExprFromValue converts a Go value to a remote constant expression
func GetRemoteConstantExprFromValue(value interface{}) interface{} {
// This is a placeholder implementation
// In the actual implementation, this would convert the value to a remote constant expression
return value
}
// GetRemoteFunctionExprFromFDE converts an FDE to a remote function expression
func GetRemoteFunctionExprFromFDE(fde *FDE) *thriftapi.FunctionExpr {
if fde == nil {
return nil
}
// Flatten 2D tensor data to 1D array
var flatTensorData []float64
for _, row := range fde.TensorData {
flatTensorData = append(flatTensorData, row...)
}
// Create tensor data constant expression
tensorConstExpr := thriftapi.NewConstantExpr()
tensorConstExpr.LiteralType = thriftapi.LiteralType_DoubleArray
tensorConstExpr.F64ArrayValue = flatTensorData
// Create target dimension constant expression
dimConstExpr := thriftapi.NewConstantExpr()
dimConstExpr.LiteralType = thriftapi.LiteralType_Int64
dimValue := int64(fde.TargetDimension)
dimConstExpr.I64Value = &dimValue
// Create parsed expressions for arguments
tensorParsedExpr := thriftapi.NewParsedExpr()
tensorParsedExpr.Type = thriftapi.NewParsedExprType()
tensorParsedExpr.Type.ConstantExpr = tensorConstExpr
dimParsedExpr := thriftapi.NewParsedExpr()
dimParsedExpr.Type = thriftapi.NewParsedExprType()
dimParsedExpr.Type.ConstantExpr = dimConstExpr
// Create FDE function expression
functionExpr := thriftapi.NewFunctionExpr()
functionExpr.FunctionName = "fde"
functionExpr.Arguments = []*thriftapi.ParsedExpr{tensorParsedExpr, dimParsedExpr}
return functionExpr
}
// GetOrdinaryInfo extracts ordinary column information from column info
func GetOrdinaryInfo(columnInfo interface{}, columnDefs []interface{}, columnName string, index int) error {
// This is a placeholder implementation
// In the actual implementation, this would extract column information
return nil
}
// ParsedExpressionToString converts a parsed expression to a string
func ParsedExpressionToString(expr interface{}) string {
// This is a placeholder implementation
// In the actual implementation, this would convert the expression to a string
return fmt.Sprintf("%v", expr)
}
// SearchToString converts a search expression to a string
func SearchToString(search *SearchExpr) string {
// This is a placeholder implementation
// In the actual implementation, this would convert the search to a string
return fmt.Sprintf("%v", search)
}
// QuoteStringLiteral quotes a value as a string literal for a filter
// expression. A backslash is passed through unchanged, which is what a regular
// expression needs, so only the quote itself has to be doubled.
func QuoteStringLiteral(value string) string {
return "'" + strings.ReplaceAll(value, "'", "''") + "'"
}
// RegexFilter builds a `regex(column, pattern)` filter expression for
// Table.Filter.
//
// The server evaluates the pattern with RE2. When the column carries a
// full-text index built with a sparse gram analyzer (`sparsegram-3-12`,
// optionally `-fold`), the literals the pattern proves mandatory narrow the
// scan through that index first and the regular expression then verifies the
// candidates that survive, so the narrowing can only ever remove rows the
// pattern cannot match. A column without such an index keeps a plain scan.
//
// Example:
//
// table.Filter(infinity.RegexFilter("doc", `colou?r of the (sky|sea)`))
func RegexFilter(column, pattern string) string {
return fmt.Sprintf("regex(%s, %s)", column, QuoteStringLiteral(pattern))
}