.. _ltree:

============
ltree
============


插件ltree简介:
-----------------------------

插件ltree是包含ltree和lquery类型的实现，为树状结构组织的数据提供索引和查询。

- 插件名为 ltree

- 插件版本 v1.1


插件ltree加载方式
-----------------------------

使用时需要在命令行执行create extension ltree;

示例：

.. code::

	create extension ltree;


插件ltree的参数配置
-----------------------------

无需配置任何参数。


插件ltree的使用方法
-----------------------------

加载插件ltree后，按照如下方式使用相应功能


ltree类型
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

支持ltree类型


示例：

.. code::

	select ''::ltree;
	ltree
	-------

	(1 row)

	select '1'::ltree;
	ltree
	-------
	1
	(1 row)

	select '1.2'::ltree;
	ltree
	-------
	1.2
	(1 row)

	select '1.2._3'::ltree;
	ltree
	--------
	1.2._3
	(1 row)

lquery类型
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

支持lquery类型，用于匹配ltree值的类似正则表达式的模式。


示例：

.. code::

	select '1'::lquery;
	lquery
	--------
	1
	(1 row)

	select '4|3|2'::lquery;
	lquery
	--------
	4|3|2
	(1 row)

	select '1.2'::lquery;
	lquery
	--------
	1.2
	(1 row)

	select '1.4|3|2'::lquery;
	lquery
	---------
	1.4|3|2
	(1 row)

	select '1.0'::lquery;
	lquery
	--------
	1.0
	(1 row)

	select '4|3|2.0'::lquery;
	lquery
	---------
	4|3|2.0
	(1 row)

	select '1.2.0'::lquery;
	lquery
	--------
	1.2.0
	(1 row)

	select '1.4|3|2.0'::lquery;
	lquery
	-----------
	1.4|3|2.0
	(1 row)

	select '1.*'::lquery;
	lquery
	--------
	1.*
	(1 row)

	select '4|3|2.*'::lquery;
	lquery
	---------
	4|3|2.*
	(1 row)

	select '1.2.*'::lquery;
	lquery
	--------
	1.2.*
	(1 row)

	select '1.4|3|2.*'::lquery;
	lquery
	-----------
	1.4|3|2.*
	(1 row)

	select '*.1.*'::lquery;
	lquery
	--------
	*.1.*
	(1 row)

	select '*.4|3|2.*'::lquery;
	lquery
	-----------
	*.4|3|2.*
	(1 row)

	select '*.1.2.*'::lquery;
	lquery
	---------
	*.1.2.*
	(1 row)

	select '*.1.4|3|2.*'::lquery;
	lquery
	-------------
	*.1.4|3|2.*
	(1 row)

	select '1.*.4|3|2'::lquery;
	lquery
	-----------
	1.*.4|3|2
	(1 row)

	select '1.*.4|3|2.0'::lquery;
	lquery
	-------------
	1.*.4|3|2.0
	(1 row)

	select '1.*.4|3|2.*{1,4}'::lquery;
	lquery
	------------------
	1.*.4|3|2.*{1,4}
	(1 row)

	select '1.*.4|3|2.*{,4}'::lquery;
	lquery
	-----------------
	1.*.4|3|2.*{,4}
	(1 row)

	select '1.*.4|3|2.*{1,}'::lquery;
	lquery
	-----------------
	1.*.4|3|2.*{1,}
	(1 row)

	select '1.*.4|3|2.*{1}'::lquery;
	lquery
	----------------
	1.*.4|3|2.*{1}
	(1 row)

	select 'qwerty%@*.tu'::lquery;
	lquery
	--------------
	qwerty%@*.tu
	(1 row)


ltree2text(ltree)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ltree类型转换为text类型。


示例：

.. code::

	select ltree2text('1.2.3.34.sdf');
	ltree2text
	--------------
	1.2.3.34.sdf
	(1 row)



text2ltree(text)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

text类型转换为ltree类型。


示例：

.. code::

	select text2ltree('1.2.3.34.sdf');
	text2ltree
	--------------
	1.2.3.34.sdf
	(1 row)


subltree(ltree,int start,int end)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ltree寻找子路径，下标从start到end（从0开始计数），下标越界会出错。


示例：

.. code::

	select subltree('top.child1.child2',1,2);
	subltree
	----------
	child1
	(1 row)


subpath(ltree,int offset,int len)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ltree根据偏移量offset和len寻找子路径，如果offset为负则从路径末端开始。如果len为负数则将许多标签留在路径的末尾。


示例：

.. code::

	select subpath('top.child1.child2',1,2);
	subpath
	---------------
	child1.child2
	(1 row)

	select subpath('top.child1.child2',-1,1);
	subpath
	---------
	child2
	(1 row)

	select subpath('top.child1.child2',0,-2);
	subpath
	---------
	top
	(1 row)

	select subpath('top.child1.child2',0,-1);
	subpath
	------------
	top.child1
	(1 row)

	select subpath('top.child1.child2',0,0);
	subpath
	---------
	(1 row)

	select subpath('top.child1.child2',1,0);
	subpath
	---------
	(1 row)


subpath(ltree,int offset)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

ltree根据偏移量offset寻找子路径，延伸到路径末尾。如果offset为负数则子路径从路径末端开始。


示例：

.. code::

	select subpath('top.child1.child2',0);
	subpath
	-------------------
	top.child1.child2
	(1 row)

	select subpath('top.child1.child2',1);
	subpath
	---------------
	child1.child2
	(1 row)

	select subpath('top.child1.child2',-2);
	    subpath
	---------------
	child1.child2
	(1 行记录)


index(ltree a,ltree b)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

计算第一次在a中出现b的位置，没找到返回-1。


示例：

.. code::

	select index('1.2.3.4.5.6','1.2');
	index
	-------
	0
	(1 row)

	select index('a.1.2.3.4.5.6','1.2');
	index
	-------
	1
	(1 row)

	select index('a.1.2.3.4.5.6','1.2.3');
	index
	-------
	1
	(1 row)

	select index('a.1.2.3.4.5.6','1.2.3.j');
	index
	-------
	-1
	(1 row)

	select index('a.1.2.3.4.5.6','1.2.3.j.4.5.5.5.5.5.5');
	index
	-------
	-1
	(1 row)

	select index('a.1.2.3.4.5.6','1.2.3');
	index
	-------
	1
	(1 row)

	select index('a.1.2.3.4.5.6','6');
	index
	-------
	6
	(1 row)

	select index('a.1.2.3.4.5.6','6.1');
	index
	-------
	-1
	(1 row)

	select index('a.1.2.3.4.5.6','5.6');
	index
	-------
	5
	(1 row)

	select index('0.1.2.3.5.4.5.6','5.6');
	index
	-------
	6
	(1 row)

index(ltree a,ltree b,int offset)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

计算第一次在a中出现b的位置,从偏移处开始搜索，负偏移量从路径末尾开始搜索，没找到返回-1。


示例：

.. code::

	select index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',6);
	index
	-------
	6
	(1 row)

	select index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',7);
	index
	-------
	9
	(1 row)

	select index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-7);
	index
	-------
	6
	(1 row)

	select index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-4);
	index
	-------
	9
	(1 row)

	select index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-3);
	index
	-------
	9
	(1 row)

	select index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-2);
	index
	-------
	-1
	(1 row)

	select index('0.1.2.3.5.4.5.6.8.5.6.8','5.6',-20000);
	index
	-------
	6
	(1 row)


lca(ltree,ltree,...)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

路径最长的公共前缀部分（最多支持8个ltree参数）。


示例：

.. code::

	select lca('{la.2.3,1.2.3.4.5.6,""}') is null;
	?column?
	----------
	t
	(1 row)

	select lca('{la.2.3,1.2.3.4.5.6}') is null;
	?column?
	----------
	f
	(1 row)

	select lca('{1.la.2.3,1.2.3.4.5.6}');
	lca
	-----
	1
	(1 row)

	select lca('{1.2.3,1.2.3.4.5.6}');
	lca
	-----
	1.2
	(1 row)

	select lca('{1.2.3}');
	lca
	-----
	1.2
	(1 row)

	select lca('{1}'), lca('{1}') is null;
	     lca | ?column?
	    -----+----------
	         | f
	(1 row)

	select lca('{}') is null;
	?column?
	----------
	t
	(1 row)

	select lca('1.la.2.3','1.2.3.4.5.6');
	lca
	-----
	1
	(1 row)

	select lca('1.2.3','1.2.3.4.5.6');
	lca
	-----
	1.2
	(1 row)

	select lca('1.2.2.3','1.2.3.4.5.6');
	lca
	-----
	1.2
	(1 row)

	select lca('1.2.2.3','1.2.3.4.5.6','');
	lca
	-----
	(1 row)

	select lca('1.2.2.3','1.2.3.4.5.6','2');
	lca
	-----
	(1 row)

	select lca('1.2.2.3','1.2.3.4.5.6','1');
	lca
	-----
	(1 row)


nlevel(ltree)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

计算路径中标签数量。


示例：

.. code::

	select nlevel('1.2.3.4');
	nlevel
	--------
	4
	(1 row)


插件ltree卸载方法
-----------------------

卸载时drop extension ltree即可。

示例：

.. code::

	drop extension ltree;


插件ltree升级方法
-----------------------

ltree扩展插件通常随着KingbaseES安装包一并升级。通常情况下用户无须单独升级些插件。
