|
13 | 13 | // limitations under the License. |
14 | 14 |
|
15 | 15 | import { expect } from 'chai'; |
16 | | -import { tableFromArrays, RecordBatchStreamWriter } from 'apache-arrow'; |
17 | | -import { countRowsInIpc, decodeIpcSchema, patchIpcBytes } from '../../../lib/kernel/KernelArrowIpc'; |
| 16 | +import { tableFromArrays, RecordBatchStreamWriter, Schema, Field, Decimal, Utf8, Int32 } from 'apache-arrow'; |
| 17 | +import { |
| 18 | + arrowSchemaToThriftSchema, |
| 19 | + countRowsInIpc, |
| 20 | + decodeIpcSchema, |
| 21 | + patchIpcBytes, |
| 22 | +} from '../../../lib/kernel/KernelArrowIpc'; |
| 23 | +import { TTypeId } from '../../../thrift/TCLIService_types'; |
18 | 24 | import { rewriteDurationToInt64 } from '../../../lib/kernel/KernelArrowIpcDurationFix'; |
19 | 25 | import HiveDriverError from '../../../lib/errors/HiveDriverError'; |
20 | 26 |
|
@@ -100,3 +106,57 @@ describe('KernelArrowIpc.decodeIpcSchema', () => { |
100 | 106 | expect(() => decodeIpcSchema(Buffer.alloc(0))).to.throw(HiveDriverError); |
101 | 107 | }); |
102 | 108 | }); |
| 109 | + |
| 110 | +describe('KernelArrowIpc.arrowSchemaToThriftSchema decimal qualifiers', () => { |
| 111 | + for (const [precision, scale] of [ |
| 112 | + [10, 2], |
| 113 | + [38, 0], |
| 114 | + [38, 38], |
| 115 | + [10, -2], |
| 116 | + ]) { |
| 117 | + it(`preserves Arrow DECIMAL(${precision},${scale}) precision and scale`, () => { |
| 118 | + const schema = new Schema([new Field('amount', new Decimal(scale, precision, 128), true)]); |
| 119 | + expect(arrowSchemaToThriftSchema(schema).columns[0].typeDesc.types[0].primitiveEntry).to.deep.equal({ |
| 120 | + type: TTypeId.DECIMAL_TYPE, |
| 121 | + typeQualifiers: { qualifiers: { precision: { i32Value: precision }, scale: { i32Value: scale } } }, |
| 122 | + }); |
| 123 | + }); |
| 124 | + } |
| 125 | + |
| 126 | + for (const type of [new Utf8(), new Decimal(2, 10, 128)]) { |
| 127 | + it(`prefers logical decimal qualifiers over the ${type} physical type`, () => { |
| 128 | + const metadata = new Map([ |
| 129 | + ['databricks.type_name', 'DECIMAL'], |
| 130 | + ['databricks.type_precision', '20'], |
| 131 | + ['databricks.type_scale', '0'], |
| 132 | + ]); |
| 133 | + const schema = new Schema([new Field('amount', type, true, metadata)]); |
| 134 | + expect(arrowSchemaToThriftSchema(schema).columns[0].typeDesc.types[0].primitiveEntry).to.deep.equal({ |
| 135 | + type: TTypeId.DECIMAL_TYPE, |
| 136 | + typeQualifiers: { qualifiers: { precision: { i32Value: 20 }, scale: { i32Value: 0 } } }, |
| 137 | + }); |
| 138 | + }); |
| 139 | + } |
| 140 | + |
| 141 | + for (const invalid of ['', 'invalid', '10.5', '2147483648']) { |
| 142 | + it(`falls back to Arrow for invalid decimal qualifiers ${JSON.stringify(invalid)}`, () => { |
| 143 | + const metadata = new Map([ |
| 144 | + ['databricks.type_precision', invalid], |
| 145 | + ['databricks.type_scale', invalid], |
| 146 | + ]); |
| 147 | + const schema = new Schema([new Field('amount', new Decimal(2, 10, 128), true, metadata)]); |
| 148 | + expect( |
| 149 | + arrowSchemaToThriftSchema(schema).columns[0].typeDesc.types[0].primitiveEntry?.typeQualifiers, |
| 150 | + ).to.deep.equal({ qualifiers: { precision: { i32Value: 10 }, scale: { i32Value: 2 } } }); |
| 151 | + }); |
| 152 | + } |
| 153 | + |
| 154 | + it('does not invent missing qualifiers or attach decimal qualifiers to other types', () => { |
| 155 | + const schema = new Schema([ |
| 156 | + new Field('unknown_decimal', new Utf8(), true, new Map([['databricks.type_name', 'DECIMAL']])), |
| 157 | + new Field('integer', new Int32(), true, new Map([['databricks.type_precision', '10']])), |
| 158 | + ]); |
| 159 | + const entries = arrowSchemaToThriftSchema(schema).columns.map((column) => column.typeDesc.types[0].primitiveEntry); |
| 160 | + expect(entries).to.deep.equal([{ type: TTypeId.DECIMAL_TYPE }, { type: TTypeId.INT_TYPE }]); |
| 161 | + }); |
| 162 | +}); |
0 commit comments