Skip to content

Commit

Permalink
fix(bindings/nodejs): map BINARY type to Buffer (#396)
Browse files Browse the repository at this point in the history
  • Loading branch information
everpcpc authored Apr 17, 2024
1 parent 6efd798 commit 7326b3f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 33 deletions.
28 changes: 14 additions & 14 deletions bindings/nodejs/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,20 +42,20 @@ while (row) {

### General Data Types

| Databend | Node.js |
| ----------- | --------------- |
| `BOOLEAN` | `Boolean` |
| `TINYINT` | `Number` |
| `SMALLINT` | `Number` |
| `INT` | `Number` |
| `BIGINT` | `Number` |
| `FLOAT` | `Number` |
| `DOUBLE` | `Number` |
| `DECIMAL` | `String` |
| `DATE` | `Date` |
| `TIMESTAMP` | `Date` |
| `VARCHAR` | `String` |
| `BINARY` | `Array(Number)` |
| Databend | Node.js |
| ----------- | --------- |
| `BOOLEAN` | `Boolean` |
| `TINYINT` | `Number` |
| `SMALLINT` | `Number` |
| `INT` | `Number` |
| `BIGINT` | `Number` |
| `FLOAT` | `Number` |
| `DOUBLE` | `Number` |
| `DECIMAL` | `String` |
| `DATE` | `Date` |
| `TIMESTAMP` | `Date` |
| `VARCHAR` | `String` |
| `BINARY` | `Buffer` |

### Semi-Structured Data Types

Expand Down
32 changes: 23 additions & 9 deletions bindings/nodejs/tests/binding.js
Original file line number Diff line number Diff line change
Expand Up @@ -34,21 +34,35 @@ Then("Select string {string} should be equal to {string}", async function (input
});

Then("Select types should be expected native types", async function () {
// NumberValue::Decimal
const row1 = await this.conn.queryRow(`SELECT 15.7563::Decimal(8,4), 2.0+3.0`);
assert.deepEqual(row1.values(), ["15.7563", "5.0"]);
// Binary
{
const row = await this.conn.queryRow("select to_binary('xyz')");
assert.deepEqual(row.values(), [Buffer.from("xyz")]);
}

// Decimal
{
const row = await this.conn.queryRow(`SELECT 15.7563::Decimal(8,4), 2.0+3.0`);
assert.deepEqual(row.values(), ["15.7563", "5.0"]);
}

// Array
const row2 = await this.conn.queryRow(`SELECT [10::Decimal(15,2), 1.1+2.3]`);
assert.deepEqual(row2.values(), [["10.00", "3.40"]]);
{
const row = await this.conn.queryRow(`SELECT [10::Decimal(15,2), 1.1+2.3]`);
assert.deepEqual(row.values(), [["10.00", "3.40"]]);
}

// Map
const row3 = await this.conn.queryRow(`SELECT {'xx':to_date('2020-01-01')}`);
assert.deepEqual(row3.values(), [{ xx: new Date("2020-01-01") }]);
{
const row = await this.conn.queryRow(`SELECT {'xx':to_date('2020-01-01')}`);
assert.deepEqual(row.values(), [{ xx: new Date("2020-01-01") }]);
}

// Tuple
const row4 = await this.conn.queryRow(`SELECT (10, '20', to_datetime('2024-04-16 12:34:56.789'))`);
assert.deepEqual(row4.values(), [[10, "20", new Date("2024-04-16T12:34:56.789Z")]]);
{
const row = await this.conn.queryRow(`SELECT (10, '20', to_datetime('2024-04-16 12:34:56.789'))`);
assert.deepEqual(row.values(), [[10, "20", new Date("2024-04-16T12:34:56.789Z")]]);
}
});

Then("Select numbers should iterate all rows", async function () {
Expand Down
10 changes: 5 additions & 5 deletions bindings/python/tests/asyncio/steps/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,17 @@ async def _(context, input, output):
@then("Select types should be expected native types")
@async_run_until_complete
async def _(context):
# NumberValue::Decimal
# Binary
row = await context.conn.query_row("select to_binary('xyz')")
assert row.values() == (b"xyz",), f"Binary: {row.values()}"

# Decimal
row = await context.conn.query_row("SELECT 15.7563::Decimal(8,4), 2.0+3.0")
assert row.values() == (
Decimal("15.7563"),
Decimal("5.0"),
), f"Decimal: {row.values()}"

# Binary
row = await context.conn.query_row("select to_binary('xyz')")
assert row.values() == (b"xyz",), f"Binary: {row.values()}"

# Array
row = await context.conn.query_row("select [10::Decimal(15,2), 1.1+2.3]")
assert row.values() == (
Expand Down
10 changes: 5 additions & 5 deletions bindings/python/tests/blocking/steps/binding.py
Original file line number Diff line number Diff line change
Expand Up @@ -57,17 +57,17 @@ def _(context, input, output):

@then("Select types should be expected native types")
async def _(context):
# NumberValue::Decimal
# Binary
row = context.conn.query_row("select to_binary('xyz')")
assert row.values() == (b"xyz",), f"Binary: {row.values()}"

# Decimal
row = context.conn.query_row("SELECT 15.7563::Decimal(8,4), 2.0+3.0")
assert row.values() == (
Decimal("15.7563"),
Decimal("5.0"),
), f"Decimal: {row.values()}"

# Binary
row = context.conn.query_row("select to_binary('xyz')")
assert row.values() == (b"xyz",), f"Binary: {row.values()}"

# Array
row = context.conn.query_row("select [10::Decimal(15,2), 1.1+2.3]")
assert row.values() == (
Expand Down

0 comments on commit 7326b3f

Please sign in to comment.