(exprs []*Expression, op Operator)
| 203 | } |
| 204 | |
| 205 | func (b *sqlExprBuilder) writeBinaryCondition(exprs []*Expression, op Operator) error { |
| 206 | // Backwards compatibility: For IN and NIN, the right hand side may be a flattened list of values, not a single list. |
| 207 | if op == OperatorIn || op == OperatorNin { |
| 208 | if len(exprs) == 2 { |
| 209 | rhs := exprs[1] |
| 210 | typ := reflect.TypeOf(rhs.Value) |
| 211 | isListVal := typ != nil && typ.Kind() == reflect.Slice |
| 212 | if rhs.Name == "" && !isListVal && rhs.Condition == nil && rhs.Subquery == nil { |
| 213 | // Convert the right hand side to a list |
| 214 | exprs[1] = &Expression{Value: []any{rhs.Value}} |
| 215 | } |
| 216 | } |
| 217 | if len(exprs) > 2 { |
| 218 | vals := make([]any, 0, len(exprs)-1) |
| 219 | for _, e := range exprs[1:] { |
| 220 | vals = append(vals, e.Value) |
| 221 | } |
| 222 | exprs = []*Expression{exprs[0], {Value: vals}} |
| 223 | } |
| 224 | } |
| 225 | |
| 226 | if len(exprs) != 2 { |
| 227 | return fmt.Errorf("binary condition must have exactly 2 expressions") |
| 228 | } |
| 229 | |
| 230 | left := exprs[0] |
| 231 | if left == nil { |
| 232 | return fmt.Errorf("left expression is nil") |
| 233 | } |
| 234 | |
| 235 | right := exprs[1] |
| 236 | if right == nil { |
| 237 | return fmt.Errorf("right expression is nil") |
| 238 | } |
| 239 | |
| 240 | // Check there isn't an unnest on the right side |
| 241 | if right.Name != "" { |
| 242 | _, unnest, _, err := b.sqlForName(right.Name) |
| 243 | if err != nil { |
| 244 | return err |
| 245 | } |
| 246 | if unnest { |
| 247 | return fmt.Errorf("cannot apply expression to dimension %q because it requires unnesting, which is only supported for the left side of an operation", right.Name) |
| 248 | } |
| 249 | } |
| 250 | |
| 251 | // Handle unnest on the left side |
| 252 | if left.Name != "" { |
| 253 | leftExpr, unnest, lookup, err := b.sqlForName(left.Name) |
| 254 | if err != nil { |
| 255 | return err |
| 256 | } |
| 257 | |
| 258 | // If not unnested, write the expression as-is or if its a lookup rewrite as per dialect |
| 259 | if !unnest { |
| 260 | if lookup != nil { |
| 261 | b.writeString(fmt.Sprintf("%s IN ", lookup.keyExpr)) |
| 262 | b.writeByte('(') |
no test coverage detected