メインコンテンツまでスキップ
メインコンテンツまでスキップ

URLを操作する関数

注記

このセクションで説明する関数は最大限のパフォーマンスのために最適化されており、ほとんどはRFC-3986標準に従っていません。RFC-3986を実装する関数には、関数名の末尾にRFCが付けられており、一般的に遅くなります。

一般的に、ユーザー文字列や@シンボルを含まない公に登録されたドメインを扱う場合は、非RFC関数バリアントを使用できます。下の表は、URL内のどのシンボルがそれぞれのRFCおよび非RFCバリアントによって解析可能()または不可能()であるかを示しています:

シンボルRFCRFC
' '
\t
<
>
%✔*
{
}
|
\\
^
~✔*
[
]
;✔*
=✔*
&✔*

*でマークされたシンボルはRFC 3986のサブデリミターであり、@シンボルに続くユーザー情報に対して許可されています。

URLのパーツを抽出する関数

関連する部分がURLに存在しない場合は、空の文字列が返されます。

protocol

URLからプロトコルを抽出します。

典型的に返される値の例:http, https, ftp, mailto, tel, magnet。

domain

URLからホスト名を抽出します。

構文

domain(url)

引数

URLはプロトコルありまたはなしで指定できます。例:

svn+ssh://some.svn-hosting.com:80/repo/trunk
some.svn-hosting.com:80/repo/trunk
https://clickhouse.com/time/

これらの例に対して、domain関数は次の結果を返します:

some.svn-hosting.com
some.svn-hosting.com
clickhouse.com

返される値

  • 入力文字列がURLとして解析可能であればホスト名、それ以外の場合は空の文字列。 String

SELECT domain('svn+ssh://some.svn-hosting.com:80/repo/trunk');
┌─domain('svn+ssh://some.svn-hosting.com:80/repo/trunk')─┐
│ some.svn-hosting.com                                   │
└────────────────────────────────────────────────────────┘

domainRFC

URLからホスト名を抽出します。domainに似ていますが、RFC 3986に準拠しています。

構文

domainRFC(url)

引数

返される値

  • 入力文字列がURLとして解析可能であればホスト名、それ以外の場合は空の文字列。 String

SELECT
    domain('http://user:[email protected]:8080/path?query=value#fragment'),
    domainRFC('http://user:[email protected]:8080/path?query=value#fragment');
┌─domain('http://user:[email protected]:8080/path?query=value#fragment')─┬─domainRFC('http://user:[email protected]:8080/path?query=value#fragment')─┐
│                                                                           │ example.com                                                                  │
└───────────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────────────┘

domainWithoutWWW

先頭のwww.が存在する場合、それを取り除いたドメインを返します。

構文

domainWithoutWWW(url)

引数

返される値

  • 入力文字列がURLとして解析可能であれば(先頭のwww.なしの)ドメイン名、それ以外の場合は空の文字列。 String

SELECT domainWithoutWWW('http://[email protected]:80/');
┌─domainWithoutWWW('http://[email protected]:80/')─┐
│ example.com                                         │
└─────────────────────────────────────────────────────┘

domainWithoutWWWRFC

先頭のwww.が存在する場合、それを取り除いたドメインを返します。domainWithoutWWWに似ていますが、RFC 3986に準拠しています。

構文

domainWithoutWWWRFC(url)

引数

返される値

  • 入力文字列がURLとして解析可能であれば(先頭のwww.なしの)ドメイン名、それ以外の場合は空の文字列。 String

クエリ:

SELECT
    domainWithoutWWW('http://user:[email protected]:8080/path?query=value#fragment'),
    domainWithoutWWWRFC('http://user:[email protected]:8080/path?query=value#fragment');

結果:

┌─domainWithoutWWW('http://user:[email protected]:8080/path?query=value#fragment')─┬─domainWithoutWWWRFC('http://user:[email protected]:8080/path?query=value#fragment')─┐
│                                                                                         │ example.com                                                                                │
└─────────────────────────────────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────────────────────────────────────┘

topLevelDomain

URLからトップレベルドメインを抽出します。

topLevelDomain(url)

引数

注記

URLはプロトコルありまたはなしで指定できます。例:

svn+ssh://some.svn-hosting.com:80/repo/trunk
some.svn-hosting.com:80/repo/trunk
https://clickhouse.com/time/

返される値

  • 入力文字列がURLとして解析可能であればドメイン名。それ以外の場合は空の文字列。 String

クエリ:

SELECT topLevelDomain('svn+ssh://www.some.svn-hosting.com:80/repo/trunk');

結果:

┌─topLevelDomain('svn+ssh://www.some.svn-hosting.com:80/repo/trunk')─┐
│ com                                                                │
└────────────────────────────────────────────────────────────────────┘

topLevelDomainRFC

URLからトップレベルドメインを抽出します。 topLevelDomainに似ていますが、RFC 3986に準拠しています。

topLevelDomainRFC(url)

引数

注記

URLはプロトコルありまたはなしで指定できます。例:

svn+ssh://some.svn-hosting.com:80/repo/trunk
some.svn-hosting.com:80/repo/trunk
https://clickhouse.com/time/

返される値

  • 入力文字列がURLとして解析可能であればドメイン名。それ以外の場合は空の文字列。 String

クエリ:

SELECT topLevelDomain('http://foo:foo%[email protected]'), topLevelDomainRFC('http://foo:foo%[email protected]');

結果:

┌─topLevelDomain('http://foo:foo%[email protected]')─┬─topLevelDomainRFC('http://foo:foo%[email protected]')─┐
│                                                │ com                                               │
└────────────────────────────────────────────────┴───────────────────────────────────────────────────┘

firstSignificantSubdomain

「最初の重要なサブドメイン」を返します。 最初の重要なサブドメインは、comnetorg、またはcoのときはセカンドレベルドメインであり、それ以外の場合はサードレベルドメインです。 例えば、firstSignificantSubdomain ('https://news.clickhouse.com/') = 'clickhouse'firstSignificantSubdomain ('https://news.clickhouse.com.tr/') = 'clickhouse'。 「重要でない」セカンドレベルドメインのリストやその他の実装の詳細は将来的に変更される可能性があります。

構文

firstSignificantSubdomain(url)

引数

返される値

  • 最初の重要なサブドメイン。 String

クエリ:

SELECT firstSignificantSubdomain('http://www.example.com/a/b/c?a=b')

結果:

┌─firstSignificantSubdomain('http://www.example.com/a/b/c?a=b')─┐
│ example                                                       │
└───────────────────────────────────────────────────────────────┘

firstSignificantSubdomainRFC

「最初の重要なサブドメイン」を返します。 最初の重要なサブドメインは、comnetorg、またはcoのときはセカンドレベルドメインであり、それ以外の場合はサードレベルドメインです。 例えば、firstSignificantSubdomain ('https://news.clickhouse.com/') = 'clickhouse'firstSignificantSubdomain ('https://news.clickhouse.com.tr/') = 'clickhouse'。 「重要でない」セカンドレベルドメインのリストやその他の実装の詳細は将来的に変更される可能性があります。 firstSignificantsubdomainに似ていますが、RFC 1034に準拠しています。

構文

firstSignificantSubdomainRFC(url)

引数

返される値

  • 最初の重要なサブドメイン。 String

クエリ:

SELECT
    firstSignificantSubdomain('http://user:[email protected]:8080/path?query=value#fragment'),
    firstSignificantSubdomainRFC('http://user:[email protected]:8080/path?query=value#fragment');

結果:

┌─firstSignificantSubdomain('http://user:[email protected]:8080/path?query=value#fragment')─┬─firstSignificantSubdomainRFC('http://user:[email protected]:8080/path?query=value#fragment')─┐
│                                                                                              │ example                                                                                         │
└──────────────────────────────────────────────────────────────────────────────────────────────┴─────────────────────────────────────────────────────────────────────────────────────────────────┘

cutToFirstSignificantSubdomain

「最初の重要なサブドメイン」までのトップレベルサブドメインを含むドメインの部分を返します。

構文

cutToFirstSignificantSubdomain(url)

引数

返される値

  • 可能であれば、最初の重要なサブドメインまでのトップレベルサブドメインを含むドメインの部分。それ以外の場合は空の文字列。 String

クエリ:

SELECT
    cutToFirstSignificantSubdomain('https://news.clickhouse.com.tr/'),
    cutToFirstSignificantSubdomain('www.tr'),
    cutToFirstSignificantSubdomain('tr');

結果:

┌─cutToFirstSignificantSubdomain('https://news.clickhouse.com.tr/')─┬─cutToFirstSignificantSubdomain('www.tr')─┬─cutToFirstSignificantSubdomain('tr')─┐
│ clickhouse.com.tr                                                 │ tr                                       │                                      │
└───────────────────────────────────────────────────────────────────┴──────────────────────────────────────────┴──────────────────────────────────────┘

cutToFirstSignificantSubdomainRFC

「最初の重要なサブドメイン」までのトップレベルサブドメインを含むドメインの部分を返します。 cutToFirstSignificantSubdomainに似ていますが、RFC 3986に準拠しています。

構文

cutToFirstSignificantSubdomainRFC(url)

引数

返される値

  • 可能であれば、最初の重要なサブドメインまでのトップレベルサブドメインを含むドメインの部分。それ以外の場合は空の文字列。 String

クエリ:

SELECT
    cutToFirstSignificantSubdomain('http://user:[email protected]:8080'),
    cutToFirstSignificantSubdomainRFC('http://user:[email protected]:8080');

結果:

┌─cutToFirstSignificantSubdomain('http://user:[email protected]:8080')─┬─cutToFirstSignificantSubdomainRFC('http://user:[email protected]:8080')─┐
│                                                                         │ example.com                                                                │
└─────────────────────────────────────────────────────────────────────────┴────────────────────────────────────────────────────────────────────────────┘

cutToFirstSignificantSubdomainWithWWW

「最初の重要なサブドメイン」までのトップレベルサブドメインを含むドメインの部分を返しますが、wwwは削除しません。

構文

cutToFirstSignificantSubdomainWithWWW(url)

引数

返される値

  • 可能であれば、最初の重要なサブドメインまでのトップレベルサブドメインを含むドメインの部分(wwwあり)。それ以外の場合は空の文字列。 String

クエリ:

SELECT
    cutToFirstSignificantSubdomainWithWWW('https://news.clickhouse.com.tr/'),
    cutToFirstSignificantSubdomainWithWWW('www.tr'),
    cutToFirstSignificantSubdomainWithWWW('tr');

結果:

┌─cutToFirstSignificantSubdomainWithWWW('https://news.clickhouse.com.tr/')─┬─cutToFirstSignificantSubdomainWithWWW('www.tr')─┬─cutToFirstSignificantSubdomainWithWWW('tr')─┐
│ clickhouse.com.tr                                                        │ www.tr                                          │                                             │
└──────────────────────────────────────────────────────────────────────────┴─────────────────────────────────────────────────┴─────────────────────────────────────────────┘

cutToFirstSignificantSubdomainWithWWWRFC

「最初の重要なサブドメイン」までのトップレベルサブドメインを含むドメインの部分を返しますが、wwwは削除しません。 cutToFirstSignificantSubdomainWithWWWに似ていますが、RFC 3986に準拠しています。

構文

cutToFirstSignificantSubdomainWithWWWRFC(url)

引数

返される値

  • 可能であれば、最初の重要なサブドメインまでのトップレベルサブドメインを含むドメインの部分(wwwあり)。それ以外の場合は空の文字列。 String

クエリ:

SELECT
    cutToFirstSignificantSubdomainWithWWW('http:%2F%[email protected]/economicheskiy'),
    cutToFirstSignificantSubdomainWithWWWRFC('http:%2F%[email protected]/economicheskiy');

結果:

┌─cutToFirstSignificantSubdomainWithWWW('http:%2F%[email protected]/economicheskiy')─┬─cutToFirstSignificantSubdomainWithWWWRFC('http:%2F%[email protected]/economicheskiy')─┐
│                                                                                       │ mail.ru                                                                                  │
└───────────────────────────────────────────────────────────────────────────────────────┴──────────────────────────────────────────────────────────────────────────────────────────┘

cutToFirstSignificantSubdomainCustom

最初の重要なサブドメインまでのトップレベルサブドメインを含むドメインの部分を返します。 カスタムTLDリスト名を受け付けます。 この関数は新しいTLDリストが必要な場合やカスタムリストを持っている場合に便利です。

構成例

<!-- <top_level_domains_path>/var/lib/clickhouse/top_level_domains/</top_level_domains_path> -->
<top_level_domains_lists>
    <!-- https://publicsuffix.org/list/public_suffix_list.dat -->
    <public_suffix_list>public_suffix_list.dat</public_suffix_list>
    <!-- NOTE: path is under top_level_domains_path -->
</top_level_domains_lists>

構文

cutToFirstSignificantSubdomain(url, tld)

引数

  • url — URL。 String
  • tld — カスタムTLDリスト名。 String

返される値

  • 最初の重要なサブドメインまでのトップレベルサブドメインを含むドメインの部分。 String

クエリ:

SELECT cutToFirstSignificantSubdomainCustom('bar.foo.there-is-no-such-domain', 'public_suffix_list');

結果:

┌─cutToFirstSignificantSubdomainCustom('bar.foo.there-is-no-such-domain', 'public_suffix_list')─┐
│ foo.there-is-no-such-domain                                                                   │
└───────────────────────────────────────────────────────────────────────────────────────────────┘

参照

cutToFirstSignificantSubdomainCustomRFC

最初の重要なサブドメインまでのトップレベルサブドメインを含むドメインの部分を返します。 カスタムTLDリスト名を受け付けます。 この関数は新しいTLDリストが必要な場合やカスタムリストを持っている場合に便利です。 cutToFirstSignificantSubdomainCustomに似ていますが、RFC 3986に準拠しています。

構文

cutToFirstSignificantSubdomainRFC(url, tld)

引数

  • url — URL。 String
  • tld — カスタムTLDリスト名。 String

返される値

  • 最初の重要なサブドメインまでのトップレベルサブドメインを含むドメインの部分。 String

参照

cutToFirstSignificantSubdomainCustomWithWWW

最初の重要なサブドメインまでのトップレベルサブドメインを含むドメインの部分を返しますが、wwwは削除しません。 カスタムTLDリスト名を受け付けます。 これは新しいTLDリストが必要な場合やカスタムリストを持っている場合に便利です。

構成例

<!-- <top_level_domains_path>/var/lib/clickhouse/top_level_domains/</top_level_domains_path> -->
<top_level_domains_lists>
    <!-- https://publicsuffix.org/list/public_suffix_list.dat -->
    <public_suffix_list>public_suffix_list.dat</public_suffix_list>
    <!-- NOTE: path is under top_level_domains_path -->
</top_level_domains_lists>

構文

cutToFirstSignificantSubdomainCustomWithWWW(url, tld)

引数

  • url — URL。 String
  • tld — カスタムTLDリスト名。 String

返される値

  • 最初の重要なサブドメインまでのトップレベルサブドメインを含むドメインの部分(wwwあり)。それ以外の場合は空の文字列。 String

クエリ:

SELECT cutToFirstSignificantSubdomainCustomWithWWW('www.foo', 'public_suffix_list');

結果:

┌─cutToFirstSignificantSubdomainCustomWithWWW('www.foo', 'public_suffix_list')─┐
│ www.foo                                                                      │
└──────────────────────────────────────────────────────────────────────────────┘

参照

cutToFirstSignificantSubdomainCustomWithWWWRFC

最初の重要なサブドメインまでのトップレベルサブドメインを含むドメインの部分を返しますが、wwwは削除しません。 カスタムTLDリスト名を受け付けます。 新しいTLDリストが必要な場合やカスタムリストを持っている場合に便利です。 cutToFirstSignificantSubdomainCustomWithWWWに似ていますが、RFC 3986に準拠しています。

構文

cutToFirstSignificantSubdomainCustomWithWWWRFC(url, tld)

引数

  • url — URL。 String
  • tld — カスタムTLDリスト名。 String

返される値

  • 最初の重要なサブドメインまでのトップレベルサブドメインを含むドメインの部分(wwwあり)。それ以外の場合は空の文字列。 String

参照

firstSignificantSubdomainCustom

最初の重要なサブドメインを返します。 カスタムTLDリスト名を受け付けます。 新しいTLDリストが必要な場合やカスタムリストを持っている場合に便利です。

構成例:

<!-- <top_level_domains_path>/var/lib/clickhouse/top_level_domains/</top_level_domains_path> -->
<top_level_domains_lists>
    <!-- https://publicsuffix.org/list/public_suffix_list.dat -->
    <public_suffix_list>public_suffix_list.dat</public_suffix_list>
    <!-- NOTE: path is under top_level_domains_path -->
</top_level_domains_lists>

構文

firstSignificantSubdomainCustom(url, tld)

引数

  • url — URL。 String
  • tld — カスタムTLDリスト名。 String

返される値

  • 最初の重要なサブドメイン。 String

クエリ:

SELECT firstSignificantSubdomainCustom('bar.foo.there-is-no-such-domain', 'public_suffix_list');

結果:

┌─firstSignificantSubdomainCustom('bar.foo.there-is-no-such-domain', 'public_suffix_list')─┐
│ foo                                                                                      │
└──────────────────────────────────────────────────────────────────────────────────────────┘

参照

firstSignificantSubdomainCustomRFC

最初の重要なサブドメインを返します。 カスタムTLDリスト名を受け付けます。 新しいTLDリストが必要な場合やカスタムリストを持っている場合に便利です。 firstSignificantSubdomainCustomに似ていますが、RFC 3986に準拠しています。

構文

firstSignificantSubdomainCustomRFC(url, tld)

引数

  • url — URL。 String
  • tld — カスタムTLDリスト名。 String

返される値

  • 最初の重要なサブドメイン。 String

参照

port

ポートを返すか、URLにポートが含まれていない場合や解析できない場合はdefault_portを返します。

構文

port(url [, default_port = 0])

引数

  • url — URL。 String
  • default_port — 返されるデフォルトポート番号。 UInt16

返される値

  • ポートまたは、URLにポートがない場合、または検証エラーの場合はデフォルトポート。 UInt16

クエリ:

SELECT port('http://[email protected]:80/');

結果:

┌─port('http://[email protected]:80/')─┐
│                                      80 │
└─────────────────────────────────────────┘

portRFC

ポートを返すか、URLにポートが含まれていない場合や解析できない場合はdefault_portを返します。 portに似ていますが、RFC 3986に準拠しています。

構文

portRFC(url [, default_port = 0])

引数

  • url — URL。 String
  • default_port — 返されるデフォルトポート番号。 UInt16

返される値

  • ポートまたは、URLにポートがない場合、または検証エラーの場合はデフォルトポート。 UInt16

クエリ:

SELECT
    port('http://user:[email protected]:8080'),
    portRFC('http://user:[email protected]:8080');

結果:

┌─port('http://user:[email protected]:8080')─┬─portRFC('http://user:[email protected]:8080')─┐
│                                             0 │                                             8080 │
└───────────────────────────────────────────────┴──────────────────────────────────────────────────┘

path

クエリ文字列なしでパスを返します。

例:/top/news.html

pathFull

上記と同じですが、クエリ文字列とフラグメントを含みます。

例:/top/news.html?page=2#comments

protocol

URLからプロトコルを抽出します。

構文

protocol(url)

引数

  • url — プロトコルを抽出するURL。 String

返される値

  • プロトコル、または決定できない場合は空の文字列。 String

クエリ:

SELECT protocol('https://clickhouse.com/');

結果:

┌─protocol('https://clickhouse.com/')─┐
│ https                               │
└─────────────────────────────────────┘

queryString

初期の疑問符、#、および#以降のすべてを除いたクエリ文字列を返します。

例:page=1&lr=213

fragment

初期のハッシュシンボルを除いたフラグメント識別子を返します。

queryStringAndFragment

クエリ文字列とフラグメント識別子を返します。

例:page=1#29390

extractURLParameter(url, name)

URLにnameパラメータが存在する場合、その値を返し、存在しない場合は空の文字列を返します。 同じ名前のパラメータが複数ある場合、最初の出現が返されます。 関数は、urlパラメータ内のパラメータがname引数と同じ方法でエンコードされていると仮定します。

extractURLParameters(url)

URLパラメータに対応するname=value文字列の配列を返します。 値はデコードされません。

extractURLParameterNames(url)

URLパラメータの名前に対応する名前文字列の配列を返します。 値はデコードされません。

URLHierarchy(url)

URLを含む配列を返しますが、パスとクエリ文字列によって/?の記号で切り詰められます。 連続するセパレーターは一つとしてカウントされます。 切り詰めは、すべての連続するセパレーターの後の位置で行われます。

URLPathHierarchy(url)

上記と同じですが、結果にプロトコルとホストを含みません。 /要素(ルート)は含まれません。

URLPathHierarchy('https://example.com/browse/CONV-6788') =
[
    '/browse/',
    '/browse/CONV-6788'
]

encodeURLComponent(url)

エンコードされたURLを返します。

例:

SELECT encodeURLComponent('http://127.0.0.1:8123/?query=SELECT 1;') AS EncodedURL;
┌─EncodedURL───────────────────────────────────────────────┐
│ http%3A%2F%2F127.0.0.1%3A8123%2F%3Fquery%3DSELECT%201%3B │
└──────────────────────────────────────────────────────────┘

decodeURLComponent(url)

デコードされたURLを返します。

例:

SELECT decodeURLComponent('http://127.0.0.1:8123/?query=SELECT%201%3B') AS DecodedURL;
┌─DecodedURL─────────────────────────────┐
│ http://127.0.0.1:8123/?query=SELECT 1; │
└────────────────────────────────────────┘

encodeURLFormComponent(url)

エンコードされたURLを返します。RFC-1866に従い、スペース( )はプラス(+)としてエンコードされます。

例:

SELECT encodeURLFormComponent('http://127.0.0.1:8123/?query=SELECT 1 2+3') AS EncodedURL;
┌─EncodedURL────────────────────────────────────────────────┐
│ http%3A%2F%2F127.0.0.1%3A8123%2F%3Fquery%3DSELECT+1+2%2B3 │
└───────────────────────────────────────────────────────────┘

decodeURLFormComponent(url)

デコードされたURLを返します。RFC-1866に従い、通常のプラス(+)はスペース( )としてデコードされます。

例:

SELECT decodeURLFormComponent('http://127.0.0.1:8123/?query=SELECT%201+2%2B3') AS DecodedURL;
┌─DecodedURL────────────────────────────────┐
│ http://127.0.0.1:8123/?query=SELECT 1 2+3 │
└───────────────────────────────────────────┘

netloc

URLからネットワークローカリティ(username:password@host:port)を抽出します。

構文

netloc(url)

引数

返される値

  • username:password@host:portString

クエリ:

SELECT netloc('http://[email protected]:80/');

結果:

┌─netloc('http://[email protected]:80/')─┐
│ [email protected]:80                   │
└───────────────────────────────────────────┘

URLの部分を削除する関数

URLに似たものが存在しない場合、URLは変更されません。

cutWWW

URLのドメインから先頭のwww.を削除します(存在する場合)。

cutQueryString

疑問符を含むクエリ文字列を削除します。

cutFragment

フラグメント識別子を削除します。

cutQueryStringAndFragment

クエリ文字列とフラグメント識別子を、クエスチョンマークや番号記号を含めて削除します。

cutURLParameter(url, name)

URLからnameパラメータを削除します(存在する場合)。 この関数は、パラメータ名の文字をエンコードまたはデコードしません。たとえば、Client IDClient%20IDは異なるパラメータ名として扱われます。

構文

cutURLParameter(url, name)

引数

  • url — URL。 String.
  • name — URLパラメータの名前。 String または Array の Strings。

戻り値

  • name URLパラメータが削除されたURL。 String

クエリ:

SELECT
    cutURLParameter('http://bigmir.net/?a=b&c=d&e=f#g', 'a') as url_without_a,
    cutURLParameter('http://bigmir.net/?a=b&c=d&e=f#g', ['c', 'e']) as url_without_c_and_e;

結果:

┌─url_without_a────────────────┬─url_without_c_and_e──────┐
│ http://bigmir.net/?c=d&e=f#g │ http://bigmir.net/?a=b#g │
└──────────────────────────────┴──────────────────────────┘