8.17.8. Defining New Range Types

8.17.8.定义范围类型

Users can define their own range types. The most common reason to do this is to use ranges over subtypes not provided among the built-in range types. For example, to define a new range type of subtype float8:

用户可自定义范围类型。 例如,要定义子类型float8的新范围类型:

CREATE TYPE floatrange AS RANGE (subtype = float8,subtype_diff = float8mi);SELECT '[1.234, 5.678]'::floatrange;

Because float8 has no meaningful “step”, we do not define a canonicalization function in this example.

因为float8没有有意义的“步长”,所以在此示例中我们没有定义规范化函数。

Defining your own range type also allows you to specify a different subtype B-tree operator class or collation to use, so as to change the sort ordering that determines which values fall into a given range.

定义自己的范围类型还可以使您指定要使用的其他子类型B树运算符类或排序规则,以便更改确定哪些值落入给定范围的排序顺序。

If the subtype is considered to have discrete rather than continuous values, the CREATE TYPE command should specify a canonical function. The canonicalization function takes an input range value,and must return an equivalent range value that may have different bounds and formatting. The canonical output for two ranges that represent the same set of values, for example the integer ranges [1, 7] and [1, 8), must be  identical. It doesn't matter which representation you choose to be the canonical one, so long as two equivalent values with different formattings are always mapped to the same value with the same formatting. In addition to adjusting the inclusive/exclusive bounds format, a canonicalization function might round off boundary values, in case the desired step size is larger than what the subtype is capable of storing. For instance, a range type over timestamp could be defined to have a step size of an hour, in which case the canonicalization function would need to round off bounds that weren't a multiple of an hour, or perhaps throw an error instead.

如果子类型具有离散值而不是连续值,则 CREATE TYPE命令应指定规范函数。规范化函数采用输入范围值,并且必须返回可能具有不同范围和格式的等效范围值。代表同一组值的两个范围的标准输出(例如整数范围[1,7]和[1,8))必须相同。只要将两个格式不同的等效值始终映射为相同格式的相同值,则选择哪种表示形式都无关紧要。除了调整包含/排除边界格式之外,在所需步长大于子类型能够存储的步长的情况下,规范化函数还可以舍入边界值。例如,可以将时间戳上的范围类型定义为一个小时的步长,在这种情况下,规范化功能将需要舍入不是一个小时数倍的边界,或者可能会抛出错误。

In addition, any range type that is meant to be used with GiST or SP-GiST indexes should define a subtype difference, or subtype_diff, function. (The index will still work without subtype_diff,but it is likely to be considerably less efficient than if a difference function is provided.) The subtype difference function takes two input values of the subtype, and returns their difference (i.e., X minus Y) represented as a float8 value. In our example above, the function float8mi that underlies the regular float8 minus operator can be used; but for any other subtype, some type conversion would be necessary. Some creative thought about how to represent differences as numbers might be needed,too. To the greatest extent possible, the subtype_diff function should agree with the sort ordering implied by the selected operator class and collation; that is, its result should be positive whenever its first argument is greater than its second according to the sort ordering.

此外,任何打算与GiST或SP-GiST索引一起使用的范围类型都应定义一个子类型差或subtype_diff函数。 (该索引在没有subtype_diff的情况下仍然可以工作,但是它的效率可能比提供差函数的效率低得多。)子类型差函数接受子类型的两个输入值,并返回它们的差(即X减Y)表示为float8值。在上面的示例中,可以使用常规float8减运算符基础的float8mi函数;但对于任何其他子类型,则需要某种类型转换。也需要一些关于如何将差异表示为数字的创造性思考。subtype_diff函数应尽可能地符合所选操作符类和排序规则所隐含的排序顺序;也就是说,根据排序顺序,只要第一个参数大于第二个参数,其结果就应该为正。

A less-oversimplified example of a subtype_diff function is:

subtype_diff函数的一个不太简化的示例是:

CREATE FUNCTION time_subtype_diff(x time, y time) RETURNS float8 AS'SELECT EXTRACT(EPOCH FROM (x - y))' LANGUAGE sql STRICT IMMUTABLE;CREATE TYPE timerange AS RANGE (subtype = time,subtype_diff = time_subtype_diff);SELECT '[11:10, 23:00]'::timerange;

See CREATE TYPE for more information about creating range types.

有关创建范围类型的更多信息,参见命令 CREATE TYPE。

8.17.8. Defining New Range Types相关推荐

  1. UnicodeDecodeError: 'ascii' codec can't decode byte 0xe3 in position 17: ordinal not in range(128)

    今天在做UI自动化的时候出现这样的提示. 1.原因 因为默认情况下,Python采用的是ASCII 编码方式,而Python在进行编码方式之间的转换时,会将 unicode 作为"中间编码& ...

  2. 8.17.9. Indexing

    8.17.9. Indexing 8.17.9.索引 GiST and SP-GiST indexes can be created for table columns of range types. ...

  3. python基础教程:list转换range()的打印结果

    for循环中常见的一个函数是range(),然而有时候直接打印range的时候,得到的结果却不是我们想要的,这个时候,可以用list列表来转换一下range的结果,让我们更清除的打印出range中的元 ...

  4. python中range函数和xrange函数有什么异同?

    http://ciniao.me/article.php?id=17 简单来说,range生成的是一个列表,而xrange生成的是一个生成器,而生成器在数组很大的时候能够比range更节省空间 转载于 ...

  5. python随机密码生成以整数17为随机数种子_python习题 随机密码生成 + 连续质数计算...

    随机密码生成 描述 补充编程模板中代码,完成如下功能:‪‬‪‬‪‬‪‬‪‬‮‬‫‬‪‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‫‬‪‬‪‬‪‬‪‬‪‬‮‬‪‬‭‬‪‬‪‬‪‬‪‬‪‬‮‬‫‬‮‬‪‬‪‬‪‬‪‬‪ ...

  6. 用Python输出各点两两连接的正17边形

    我可以用Python中的Turtle模块来绘制一个正17边形,代码如下:import turtle turtle.forward(50) turtle.left(180-180/17) for i i ...

  7. C++ Primer - 5th Edition - 书中源代码 - 课后习题答案

    C++ Primer - 5th Edition - 书中源代码 - 课后习题答案 C++ Primer - 5th Edition - 书中源代码 - 课后习题答案 1. C++ Primer, 5 ...

  8. python第一个公开发行版_Python基础篇【第一篇】:python入门

    python 简介 python是一种解释型.面向对象.改动数据类型的高级程序设计语言,由Guido van Rossum于1989年发明,第一个公开发行版发行于1991年. python是纯粹的自由 ...

  9. python核心理念_《三天搞定Python基础概念之第一天》中文版

    前言: 首先,非常感谢Jiang老师将其分享出来!本课件非常经典! 经过笔者亲测,竟然确实只要三天,便可管中窥豹洞见Python及主要库的应用.实属难得诚意之作! 其次,只是鉴于Jiang老师提供的原 ...

最新文章

  1. python比c语言好学吗-学C语言好还是Python好?
  2. python多级字典嵌套_python – 如何拆分字符串并形成多级嵌套字典?
  3. 【spring boot】ajax post提交遇到403
  4. Silverlight3 Tools Download link
  5. 【WS-Federation】到底有多少公司在用WS-Federation
  6. 如何使用Docker、Docker-Compose和Rancher搭建部署Pipeline(二)
  7. Domino下实现仿Google搜索提示效果
  8. 很多中小企业都在用的3款开源软件被曝多个漏洞
  9. 动态新增表字段_国际法规 | 欧盟POPs法规更新新增三氯杀螨醇、修订PFOS的豁免——北测集团...
  10. MySQL数据库设置主从同步
  11. 基于 Linux 和 MiniGUI 的嵌入式系统软件开发指南(八) MiniGUI 和其他嵌入式 Linux 上的图形及图形用户界面系统...
  12. 【教程】合成方法的那些事儿(附加练习源文件)
  13. mysql随机字符串函数
  14. 中文技术文档写作规范(汇总整理版)
  15. python的循环控制语句while和for的使用详解
  16. 基于Java Web的在线考试系统的实现
  17. 真_u3d程序员,基本脚本语法篇
  18. 基于逻辑回归的鸢尾花分类
  19. web player html5源码,GitHub - WEBHH/DanmuPlayer: Html5弹幕视频播放器插件
  20. Nginx介绍和使用

热门文章

  1. 英语基础写作及翻译经典句荟萃
  2. ELF文件解析 ELF File Parsing
  3. [转][荐]小菜编程成长记系列
  4. win7系统下解决VC6.0 与office2007、2010不兼容的问题
  5. mac备忘录html,备忘录 - Mac附带的App - Macbook Pro用户手册
  6. ORI-621龙芯3A处理器CPCI刀片计算机
  7. 小易的考试成绩(0 -1背包问题)
  8. 45.qt quick-qml虚拟软键盘详解(一)
  9. BP算法与梯度下降算法
  10. golang设计模式——装饰器模式