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

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に準拠しています。

構文

cutToFirstSignificantSubdomainWithWWW(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を含む配列を返し、パスとクエリ文字列の終わりを/?で切り捨てます。 連続する区切り文字は1つとしてカウントされます。 切り捨ては、すべての連続する区切り文字の後の位置で行われます。

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 │
└──────────────────────────────┴──────────────────────────┘