Duration column type is returned as custom type

huangapple go评论105阅读模式
英文:

Duration column type is returned as custom type

问题

我在Cassandra中有以下表格:

create table duration_table
(
id int primary key,
duration_col duration
);

[Cassandra数据库版本为4.0.5](https://github.com/apache/cassandra/releases/tag/cassandra-4.0.5),[Cassandra Node.js驱动程序版本为4.6.4](https://www.npmjs.com/package/cassandra-driver?activeTab=readme)。
当我通过以下方式请求表元数据时:

client.metadata.getTable(keyspaceName, "duration_table")

结果是:

...,
{
...,
columns: [
...,
{
"name": "duration_col",
"type": {
"code": 21,
"info": null,
"options": {
"frozen": false
}
},
"isStatic": false
}
]
}

`duration_col`的返回类型代码是`21`,对应于[`cassandra-driver`中的`types.dataTypes.duration`枚举](https://github.com/datastax/nodejs-driver/blob/v4.6.4/lib/types/duration.js)。
然而,当我通过Cassandra驱动程序客户端发送以下请求时:

client.execute("SELECT * FROM duration_table");

结果如下:

{
...,
columns: [
...,
{
"name": "duration_col",
"type": {
"code": 0,
"type": null,
"info": "org.apache.cassandra.db.marshal.DurationType"
}
}
]
}

此处返回的类型是`0`,对应于驱动程序中的`types.dataTypes.custom`枚举。
所以,我的问题是:
 - 为什么相同表格和相同列的类型会有所不同?
 - 是否有保证 `ResultSet` 中的 `info` 字段始终存在,其值为 `org.apache.cassandra.db.marshal.DurationType`?我是说,我能把这个字段视为持续时间列类型的常数吗?
- 是否还有其他在返回时被视为自定义类型但实际上不是自定义类型的Cassandra类型?
英文:

I have the following table in cassandra:

create table duration_table
(
    id           int primary key,
    duration_col duration
);

Cassandra DB version is 4.0.5, cassandra nodejs driver version is 4.6.4.
When I request table metadata via

client.metadata.getTable(keyspaceName, "duration_table")

the result is:

...,
{
  ...,
  columns: [
    ...,
    {
      "name": "duration_col",
      "type": {
        "code": 21,
        "info": null,
        "options": {
          "frozen": false
        }
      },
      "isStatic": false
    }
  ]
}

The returned type code of duration_col is 21 which corresponds to types.dataTypes.duration enum in cassandra-driver.
However, when i send the following request via cassandra driver client:

client.execute("SELECT * FROM duration_table");

the result is the following:

{
  ...,
  columns: [
    ...,
    {
      "name": "duration_col",
      "type": {
        "code": 0,
        "type": null,
        "info": "org.apache.cassandra.db.marshal.DurationType"
      }
    }
  ]
}

The returned type here is 0 which corresponds to types.dataTypes.custom enum in the driver.
So, my questions are:

  • why do the types differ on the same table and the same column?
  • is there a guarantee that the returned info field in ResultSet with value org.apache.cassandra.db.marshal.DurationType will always be present in such scenario? I mean, can I consider this field as a constant for duration column type?
  • are there any other cassandra types that are returned as custom type but actually they are not custom types?

答案1

得分: 2

以下是您要翻译的内容:

感谢您的提问!

您在结果集报告中看到列的元数据将持续时间类型显示为自定义类型,是因为您在连接中使用协议版本4。持续时间类型是在协议版本5(v5)中作为完全支持的类型添加到CQL协议中,但不幸的是,Node.js驱动程序当前仅支持协议版本4(v4)或更低版本。您可以通过在日志顶部附近查找以下内容来查看Node.js驱动程序连接到v4:

info - Connection: 此驱动程序不支持协议版本5,正在降级(未定义)

当您连接到支持持续时间类型的Cassandra服务器时,如果使用小于v5的协议,服务器将自动将持续时间类型转换为自定义类型,以便提供驱动程序可以理解的内容。您可以通过向测试表添加一行数据并打印查询得到的结果集来确认这一点:

ResultSet {
...
rows: [
Row {
id: 1,
duration_col: Duration {
months: 0,
days: 0,
nanoseconds: Long { low: -129542144, high: 13, unsigned: false }
}
}
],
rowLength: 1,
columns: [
{ name: 'id', type: { code: 9, type: null } },
{
name: 'duration_col',
type: {
code: 0,
type: null,
info: 'org.apache.cassandra.db.marshal.DurationType'
}
}
],
...

请注意,在此示例中,持续时间列中的返回值被正确呈现为由某些月份、天数和纳秒组成的值。这与协议v5的规范中定义的持续时间值完全匹配,因此很明显,此列中存储的是持续时间数据。

关于Node.js驱动程序如何在不支持v5的情况下识别持续时间类型的一点快速说明。驱动程序从其他工作中添加了对持续时间类型的支持。虽然支持此类型是实现v5所必需的,但这当然不足以满足v5的全部需求;v5不仅仅是一个新的数据类型。

请注意,当我们为Ruby驱动程序添加了对持续时间类型的支持时,我们正好遇到了这个问题;这正是此注释所指的内容。1

英文:

thanks for the question!

You're seeing the column metadata in your result set report the duration type as a custom type because you're using protocol version 4 with your connection. The duration type was added to the CQL protocol as a fully supported type with protocol version 5 (v5) but unfortunately the nodejs driver currently only supports protocol version 4 (v4) or less. You can see the nodejs driver connecting with v4 by looking for something like the following near the top of your log:

info - Connection:  Protocol version 5 not supported by this driver, downgrading (undefined)

When you connect to a Cassandra server which supports the duration type using something less than v5 the server will automatically convert duration types to custom types in order to give you something your driver can understand. You can confirm this by adding a row of data to your test table and printing the result set you get from querying it:

ResultSet {
...
rows: [
 Row {
   id: 1,
   duration_col: Duration {
     months: 0,
     days: 0,
     nanoseconds: Long { low: -129542144, high: 13, unsigned: false }
   }
 }
],
rowLength: 1,
columns: [
 { name: 'id', type: { code: 9, type: null } },
 {
   name: 'duration_col',
   type: {
     code: 0,
     type: null,
     info: 'org.apache.cassandra.db.marshal.DurationType'
   }
 }
],
...

Note that in this example the returned value in the duration column is correctly rendered as a three-fold value consisting of some number of months, days and nanoseconds. This matches exactly the value of a duration defined in the specification of v5, so clearly what's stored in this column is duration data.

A quick note here on how the nodejs driver is able to identify duration types if it doesn't support v5. The driver added support for the duration type from other work. Support for this type is necessary to implement v5 but it is certainly not sufficient; there's a lot more to v5 than just a new data type.

Note that we hit exactly this problem when we added support for the duration type to the Ruby driver; that's exactly what this comment is referring to.

huangapple
  • 本文由 发表于 2023年5月7日 00:01:43
  • 转载请务必保留本文链接:https://go.coder-hub.com/76189857.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定