|
| 1 | +// Copyright (c) 2026 Databricks, Inc. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +import { DBSQLParameter, DBSQLParameterValue } from '../DBSQLParameter'; |
| 16 | +import ParameterError from '../errors/ParameterError'; |
| 17 | +import { SeaNativeTypedValueInput } from './SeaNativeLoader'; |
| 18 | + |
| 19 | +/** |
| 20 | + * Derive `(precision,scale)` from a decimal value string for the SEA |
| 21 | + * `DECIMAL(p,s)` type name — the kernel param codec requires the |
| 22 | + * parenthesised form (plain `"DECIMAL"` is rejected) so it can preserve |
| 23 | + * the caller's fractional digits. `"99.99"` ⇒ `"4,2"`; `"-123"` ⇒ `"3,0"`. |
| 24 | + * Clamped to the Databricks max precision of 38. |
| 25 | + */ |
| 26 | +function decimalPrecisionScale(v: string): string { |
| 27 | + const digits = (v.match(/\d/g) ?? []).length; |
| 28 | + const dot = v.indexOf('.'); |
| 29 | + const scale = dot < 0 ? 0 : (v.slice(dot + 1).match(/\d/g) ?? []).length; |
| 30 | + const precision = Math.min(Math.max(digits, 1), 38); |
| 31 | + return `${precision},${Math.min(scale, precision)}`; |
| 32 | +} |
| 33 | + |
| 34 | +/** |
| 35 | + * Reduce a `DBSQLParameter | DBSQLParameterValue` to the napi |
| 36 | + * `TypedValueInput` (`{ sqlType, value? }`) the kernel's positional-param |
| 37 | + * codec (`parse_typed_value`) accepts. Reuses `DBSQLParameter.toSparkParameter` |
| 38 | + * — the same type-inference + value-stringification the Thrift backend uses — |
| 39 | + * then adapts the type name to the codec's expectations: |
| 40 | + * - DECIMAL → `DECIMAL(p,s)` (parenthesised form required) |
| 41 | + * - INTERVAL * → `INTERVAL` (the codec's single interval type name) |
| 42 | + * - a missing value ⇒ SQL NULL (`parse_typed_value` maps `value: None` to NULL). |
| 43 | + */ |
| 44 | +function toTypedValueInput(value: DBSQLParameter | DBSQLParameterValue): SeaNativeTypedValueInput { |
| 45 | + const param = value instanceof DBSQLParameter ? value : new DBSQLParameter({ value }); |
| 46 | + const spark = param.toSparkParameter(); |
| 47 | + const stringValue = spark.value?.stringValue ?? undefined; |
| 48 | + |
| 49 | + // NULL: no value (and `VOID` ignores any type), matching toSparkParameter's |
| 50 | + // type/value-less shape for null/undefined. |
| 51 | + if (stringValue === undefined || stringValue === null) { |
| 52 | + return { sqlType: 'VOID' }; |
| 53 | + } |
| 54 | + |
| 55 | + let sqlType = spark.type ?? 'STRING'; |
| 56 | + const upper = sqlType.toUpperCase(); |
| 57 | + if (upper === 'DECIMAL') { |
| 58 | + sqlType = `DECIMAL(${decimalPrecisionScale(stringValue)})`; |
| 59 | + } else if (upper.startsWith('INTERVAL')) { |
| 60 | + sqlType = 'INTERVAL'; |
| 61 | + } |
| 62 | + return { sqlType, value: stringValue }; |
| 63 | +} |
| 64 | + |
| 65 | +/** |
| 66 | + * Convert the public `ordinalParameters` option into the napi |
| 67 | + * `positionalParams` array. Returns `undefined` when no positional params |
| 68 | + * were supplied (so the caller can keep the minimal no-options call shape). |
| 69 | + * |
| 70 | + * Named parameters are not yet bindable on the SEA path — the kernel napi |
| 71 | + * surface (`ExecuteOptions.positionalParams`) exposes positional only — so a |
| 72 | + * caller passing `namedParameters` is rejected with a clear `ParameterError` |
| 73 | + * rather than silently ignored. |
| 74 | + */ |
| 75 | +export function buildSeaPositionalParams( |
| 76 | + ordinalParameters?: Array<DBSQLParameter | DBSQLParameterValue>, |
| 77 | +): Array<SeaNativeTypedValueInput> | undefined { |
| 78 | + if (ordinalParameters === undefined || ordinalParameters.length === 0) { |
| 79 | + return undefined; |
| 80 | + } |
| 81 | + return ordinalParameters.map(toTypedValueInput); |
| 82 | +} |
0 commit comments