Skip to content

Commit 2cad597

Browse files
engineer-bot: address @mention on PR #585
Signed-off-by: peco-engineer-bot[bot] <peco-engineer-bot[bot]@users.noreply.github.com>
1 parent c53d06b commit 2cad597

3 files changed

Lines changed: 43 additions & 6 deletions

File tree

‎CHANGELOG.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
- Kernel backend: `getPrimaryKeys()` without a `tableName` now throws a `ParameterError` (`Invalid argument: tableName may not be null`, SQLSTATE `42000`) instead of an opaque native conversion error. (#479)
1111
- Upgrade `thrift` to 0.25, which depends on the CommonJS-compatible `uuid@11` instead of the ESM-only `uuid@13`, so `require('@databricks/sql')` works again in Jest and other environments without `require(esm)`. (#461)
1212
- **Behavior change for direct users of the exported `thrift.TCLIService`** (`DBSQLClient` itself uses the callback form and is unaffected): `TCLIService.Client` calls made without a callback now return native Promises instead of Q promises, and `TCLIService.Processor` runs promise-returning handlers through native Promises. Q-only methods such as `.fail()`, `.fin()`, `.done()` and `.spread()` no longer exist; use `.catch()`, `.finally()` and `.then()`. An unhandled rejection is now a real Node unhandled rejection, which crashes the process under Node's default `--unhandled-rejections=throw`. Q only emitted the `unhandledRejection` event manually, so it never crashed. Handle every promise a callback-less client call returns.
13-
- Fix `DECIMAL` parameters losing their fractional digits on the Thrift backend: `"123.45"` was bound as `decimal(3,0)` and came back as `123`. Both backends now send `DECIMAL(p,s)` derived from the value, accept exponent forms such as `1e-7` (rewritten as a plain numeral), and throw a `ParameterError` for non-numeric values or values that need more than 38 digits. (#474)
13+
- Fix `DECIMAL` parameters losing their fractional digits on the Thrift backend: `"123.45"` was bound as `decimal(3,0)` and came back as `123`. Both backends now send `DECIMAL(p,s)` derived from the value (also for the `DEC` / `NUMERIC` synonyms), stringify `Int64` values beyond 2^53 exactly instead of as `Infinity`, accept exponent forms such as `1e-7` (rewritten as a plain numeral), and throw a `ParameterError` for non-numeric values or values that need more than 38 digits. (#474)
1414

1515
## 3.0.0
1616

‎lib/DBSQLParameter.ts‎

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,11 @@ function inferNumberType(value: number): DBSQLParameterType {
6464
const DECIMAL_MAX_PRECISION = 38;
6565

6666
/**
67-
* Qualify a bare `DECIMAL` with the precision and scale of its value. A bare
68-
* `DECIMAL` makes the Thrift server bind a scale-0 decimal, silently truncating
69-
* `"123.45"` to `123`, and the kernel codec rejects it outright.
67+
* Qualify a bare `DECIMAL` (or its `DEC` / `NUMERIC` synonyms) with the
68+
* precision and scale of its value. Both backends otherwise lose the fraction:
69+
* the Thrift server binds a scale-0 decimal, truncating `"123.45"` to `123`,
70+
* and the kernel's raw-param path forwards the type verbatim, so SEA binds it
71+
* as `DECIMAL(10,0)` and rounds.
7072
*
7173
* Precision is significant integer digits plus scale (Spark's decimal-literal
7274
* rule), so `"99.99"` ⇒ `DECIMAL(4,2)` and `"0.00001"` ⇒ `DECIMAL(5,5)`. Plain
@@ -118,6 +120,18 @@ function qualifyDecimal(stringValue: string): { type: string; value: string } {
118120
return { type, value: sign === '-' ? `-${plain}` : plain };
119121
}
120122

123+
// Unqualified decimal type names; a qualified `DECIMAL(p,s)` is left as given.
124+
const BARE_DECIMAL_TYPE = /^(DECIMAL|DEC|NUMERIC)$/i;
125+
126+
/**
127+
* Exact decimal string for an `Int64`. `Int64#toString` goes through a JS
128+
* number and returns `"Infinity"` for magnitudes ≥ 2^53, so read the
129+
* big-endian two's-complement bytes as a `bigint` instead.
130+
*/
131+
function int64ToString(value: Int64): string {
132+
return value.toBuffer().readBigInt64BE(0).toString();
133+
}
134+
121135
interface DBSQLParameterOptions {
122136
type?: DBSQLParameterType;
123137
value: DBSQLParameterValue;
@@ -141,7 +155,7 @@ export class DBSQLParameter {
141155
const param = this.toUnqualifiedSparkParameter(options);
142156
const stringValue = param.value?.stringValue;
143157
// Only an explicit type can be DECIMAL; inferred types never are.
144-
if (this.type?.toUpperCase() === DBSQLParameterType.DECIMAL && typeof stringValue === 'string') {
158+
if (this.type !== undefined && BARE_DECIMAL_TYPE.test(this.type.trim()) && typeof stringValue === 'string') {
145159
const decimal = qualifyDecimal(stringValue);
146160
param.type = decimal.type;
147161
param.value = new TSparkParameterValue({ stringValue: decimal.value });
@@ -191,7 +205,7 @@ export class DBSQLParameter {
191205
name,
192206
type: wireType ?? DBSQLParameterType.BIGINT,
193207
value: new TSparkParameterValue({
194-
stringValue: this.value.toString(),
208+
stringValue: this.value instanceof Int64 ? int64ToString(this.value) : this.value.toString(),
195209
}),
196210
});
197211
}

‎tests/unit/DBSQLParameter.test.ts‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -200,6 +200,29 @@ describe('DBSQLParameter', () => {
200200
expect(decimal(new Int64(1234))).to.deep.equal(expected('DECIMAL(4,0)', '1234'));
201201
});
202202

203+
it('stringifies Int64 values beyond 2^53 exactly', () => {
204+
// `Int64#toString` returns "Infinity" from 2^53 upwards.
205+
expect(decimal(new Int64(0x200000, 0))).to.deep.equal(expected('DECIMAL(16,0)', '9007199254740992'));
206+
expect(decimal(new Int64(0x200000, 1))).to.deep.equal(expected('DECIMAL(16,0)', '9007199254740993'));
207+
expect(decimal(new Int64(0x7fffffff, 0xffffffff))).to.deep.equal(
208+
expected('DECIMAL(19,0)', '9223372036854775807'),
209+
);
210+
expect(decimal(new Int64(0x80000000, 0))).to.deep.equal(expected('DECIMAL(19,0)', '-9223372036854775808'));
211+
expect(decimal(new Int64(-5))).to.deep.equal(expected('DECIMAL(1,0)', '-5'));
212+
expect(new DBSQLParameter({ value: new Int64(0x200000, 1) }).toSparkParameter()).to.deep.equal(
213+
expected('BIGINT', '9007199254740993'),
214+
);
215+
});
216+
217+
it('qualifies the DEC and NUMERIC synonyms like DECIMAL', () => {
218+
expect(decimal('123.45', 'NUMERIC')).to.deep.equal(expected('DECIMAL(5,2)', '123.45'));
219+
expect(decimal('123.45', 'numeric')).to.deep.equal(expected('DECIMAL(5,2)', '123.45'));
220+
expect(decimal('123.45', 'DEC')).to.deep.equal(expected('DECIMAL(5,2)', '123.45'));
221+
expect(decimal('123.45', ' Decimal ')).to.deep.equal(expected('DECIMAL(5,2)', '123.45'));
222+
expect(decimal('123.45', 'NUMERIC(10,2)')).to.deep.equal(expected('NUMERIC(10,2)', '123.45'));
223+
expect(decimal('123.45', 'DECIMALS')).to.deep.equal(expected('DECIMALS', '123.45'));
224+
});
225+
203226
it('rewrites exponent forms as plain numerals', () => {
204227
// `Number#toString` uses exponent notation below 1e-6 and from 1e21.
205228
expect(decimal(1e-7)).to.deep.equal(expected('DECIMAL(7,7)', '0.0000001'));

0 commit comments

Comments
 (0)