Skip to content

Commit

Permalink
Merge #83668 #84626
Browse files Browse the repository at this point in the history
83668: sql: added inet function to support conversion to inet type r=rafiss a=surahman

Release note (sql change):
The inet function has been added to support the conversion of a supplied type to that of the inet
type family. If the conversion fails a SQL error will be output.

Closes #82052.

84626: bench/rttanalysis: update expectations r=ajwerner a=ajwerner

We were off by one. It was okay because we allow the test to be off by one.

Release note: None

Co-authored-by: Saad Ur Rahman <[email protected]>
Co-authored-by: Andrew Werner <[email protected]>
  • Loading branch information
3 people committed Jul 19, 2022
3 parents 5e53f01 + a63ecf8 + 943d832 commit da0a1b8
Show file tree
Hide file tree
Showing 4 changed files with 42 additions and 4 deletions.
2 changes: 2 additions & 0 deletions docs/generated/sql/functions.md
Original file line number Diff line number Diff line change
Expand Up @@ -2708,6 +2708,8 @@ The output can be used to recreate a database.’</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="get_byte"></a><code>get_byte(byte_string: <a href="bytes.html">bytes</a>, index: <a href="int.html">int</a>) &rarr; <a href="int.html">int</a></code></td><td><span class="funcdesc"><p>Extracts a byte at the given index in the byte array.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="inet"></a><code>inet(val: <a href="string.html">string</a>) &rarr; <a href="inet.html">inet</a></code></td><td><span class="funcdesc"><p>If possible, converts input to that of type inet.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="initcap"></a><code>initcap(val: <a href="string.html">string</a>) &rarr; <a href="string.html">string</a></code></td><td><span class="funcdesc"><p>Capitalizes the first letter of <code>val</code>.</p>
</span></td><td>Immutable</td></tr>
<tr><td><a name="left"></a><code>left(input: <a href="bytes.html">bytes</a>, return_set: <a href="int.html">int</a>) &rarr; <a href="bytes.html">bytes</a></code></td><td><span class="funcdesc"><p>Returns the first <code>return_set</code> bytes from <code>input</code>.</p>
Expand Down
8 changes: 4 additions & 4 deletions pkg/bench/rttanalysis/testdata/benchmark_expectations
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ exp,benchmark
15,CreateRole/create_role_with_2_options
16,CreateRole/create_role_with_3_options
14,CreateRole/create_role_with_no_options
16,DropDatabase/drop_database_0_tables
17,DropDatabase/drop_database_1_table
18,DropDatabase/drop_database_2_tables
19,DropDatabase/drop_database_3_tables
15,DropDatabase/drop_database_0_tables
16,DropDatabase/drop_database_1_table
17,DropDatabase/drop_database_2_tables
18,DropDatabase/drop_database_3_tables
20,DropRole/drop_1_role
27,DropRole/drop_2_roles
34,DropRole/drop_3_roles
Expand Down
20 changes: 20 additions & 0 deletions pkg/sql/logictest/testdata/logic_test/inet
Original file line number Diff line number Diff line change
Expand Up @@ -888,3 +888,23 @@ SELECT '127.001.002.003'::INET, '127.001.002.003/016'::INET, '010.001.002.003'::
127.1.2.3 127.1.2.3/16 10.1.2.3

subtest end

# Test inet

query error pq: inet\(\): could not parse "some_invalid_value" as inet. invalid IP
SELECT inet('some_invalid_value')

query T
SELECT inet('::111')
----
::111

query T
SELECT inet('192.168.0.2')
----
192.168.0.2

query T
SELECT inet('::ffff:192.168.0.1/24')
----
::ffff:192.168.0.1/24
16 changes: 16 additions & 0 deletions pkg/sql/sem/builtins/builtins.go
Original file line number Diff line number Diff line change
Expand Up @@ -1041,6 +1041,22 @@ var regularBuiltins = map[string]builtinDefinition{
},
),

"inet": makeBuiltin(defProps(),
tree.Overload{
Types: tree.ArgTypes{{"val", types.String}},
ReturnType: tree.FixedReturnType(types.INet),
Fn: func(ctx *eval.Context, args tree.Datums) (tree.Datum, error) {
inet, err := eval.PerformCast(ctx, args[0], types.INet)
if err != nil {
return nil, pgerror.WithCandidateCode(err, pgcode.InvalidTextRepresentation)
}
return inet, nil
},
Info: "If possible, converts input to that of type inet.",
Volatility: volatility.Immutable,
},
),

"from_ip": makeBuiltin(defProps(),
tree.Overload{
Types: tree.ArgTypes{{"val", types.Bytes}},
Expand Down

0 comments on commit da0a1b8

Please sign in to comment.