提交 f8b75754 编写于 作者: 2 2149 提交者: wangzelin.wzl

add expr: (#198)

上级 4c03c716
......@@ -425,6 +425,13 @@
#define N_DBMS_LOB_CONVERT_CLOB_CHARSET "dbms_lob_convert_clob_charset"
#define N_IP2INT "ip2int"
#define N_INT2IP "int2ip"
#define N_INETATON "inet_aton"
#define N_INET6ATON "inet6_aton"
#define N_INET6NTOA "inet6_ntoa"
#define N_IS_IPV4 "is_ipv4"
#define N_IS_IPV6 "is_ipv6"
#define N_IS_IPV4_MAPPED "is_ipv4_mapped"
#define N_IS_IPV4_COMPAT "is_ipv4_compat"
#define N_UPPER "upper"
#define N_SIGN "sign"
#define N_LOWER "lower"
......
......@@ -278,6 +278,7 @@ ob_set_subtarget(ob_sql engine
engine/expr/ob_expr_int_div.cpp
engine/expr/ob_expr_interval.cpp
engine/expr/ob_expr_ip2int.cpp
engine/expr/ob_expr_inet.cpp
engine/expr/ob_expr_is.cpp
engine/expr/ob_expr_is_serving_tenant.cpp
engine/expr/ob_expr_last_exec_id.cpp
......
......@@ -41,6 +41,7 @@
#include "ob_expr_int2ip.h"
#include "ob_expr_int_div.h"
#include "ob_expr_ip2int.h"
#include "ob_expr_inet.h"
#include "ob_expr_is.h"
#include "ob_expr_last_exec_id.h"
#include "ob_expr_last_trace_id.h"
......@@ -187,73 +188,73 @@ namespace oceanbase {
using namespace common;
namespace sql {
extern int cast_eval_arg(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int anytype_to_varchar_char_explicit(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int anytype_anytype_explicit(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_acos_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_and_exprN(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_asin_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_assign_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_atan2_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_atan_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_between_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_bool_expr_for_integer_type(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_bool_expr_for_float_type(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_bool_expr_for_double_type(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_bool_expr_for_other_type(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_char_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_coalesce_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_cos_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_cosh_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_exp_expr_double(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_exp_expr_number(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_ceil_floor(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_round_expr_datetime1(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_round_expr_datetime2(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_round_expr_numeric2(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_round_expr_numeric1(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_initcap_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_left_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_ln_expr_mysql(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_ln_expr_oracle_double(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_ln_expr_oracle_number(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_log10_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_log2_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_log_expr_double(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_log_expr_number(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_not_between_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_or_exprN(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_power_expr_oracle(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_right_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_sign_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_sin_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_sinh_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_sqrt_expr_mysql(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_sqrt_expr_oracle_double(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_sqrt_expr_oracle_number(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_str_to_date_expr_date(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_str_to_date_expr_time(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_str_to_date_expr_datetime(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_tan_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_tanh_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_timestampadd_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_time_to_usec_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_todays_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_to_temporal_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_translate_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_usec_to_time_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_charset_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_collation_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_coercibility_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_set_collation_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_cmp_meta_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_trunc_expr_datetime(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_trunc_expr_numeric(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_truncate_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int calc_reverse_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum);
extern int calc_instrb_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum);
extern int calc_convert_expr(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& res_datum);
extern int calc_translate_using_expr(const ObExpr&, ObEvalCtx&, ObDatum&);
extern int cast_eval_arg(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int anytype_to_varchar_char_explicit(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int anytype_anytype_explicit(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_acos_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_and_exprN(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_asin_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_assign_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_atan2_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_atan_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_between_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_bool_expr_for_integer_type(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_bool_expr_for_float_type(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_bool_expr_for_double_type(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_bool_expr_for_other_type(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_char_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_coalesce_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_cos_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_cosh_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_exp_expr_double(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_exp_expr_number(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_ceil_floor(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_round_expr_datetime1(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_round_expr_datetime2(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_round_expr_numeric2(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_round_expr_numeric1(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_initcap_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_left_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_ln_expr_mysql(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_ln_expr_oracle_double(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_ln_expr_oracle_number(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_log10_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_log2_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_log_expr_double(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_log_expr_number(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_not_between_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_or_exprN(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_power_expr_oracle(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_right_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_sign_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_sin_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_sinh_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_sqrt_expr_mysql(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_sqrt_expr_oracle_double(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_sqrt_expr_oracle_number(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_str_to_date_expr_date(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_str_to_date_expr_time(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_str_to_date_expr_datetime(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_tan_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_tanh_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_timestampadd_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_time_to_usec_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_todays_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_to_temporal_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_translate_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_usec_to_time_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_charset_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_collation_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_coercibility_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_set_collation_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_cmp_meta_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_trunc_expr_datetime(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_trunc_expr_numeric(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_truncate_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
extern int calc_reverse_expr(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res_datum);
extern int calc_instrb_expr(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res_datum);
extern int calc_convert_expr(const ObExpr &expr, ObEvalCtx &ctx, ObDatum &res_datum);
extern int calc_translate_using_expr(const ObExpr &, ObEvalCtx &, ObDatum &);
// append only, can not delete, set to NULL for mark delete
static ObExpr::EvalFunc g_expr_eval_functions[] = {
......@@ -636,37 +637,44 @@ static ObExpr::EvalFunc g_expr_eval_functions[] = {
ObExprNlsLower::calc_lower, /* 378 */
ObExprNlsUpper::calc_upper, /* 379 */
ObExprToOutfileRow::to_outfile_str, /* 380 */
NULL, // ObExprIs::calc_is_infinite, /* 381 */
NULL, // ObExprIs::calc_is_nan, /* 382 */
NULL, // ObExprIsNot::calc_is_not_infinite, /* 383 */
NULL, // ObExprIsNot::calc_is_not_nan, /* 384 */
ObExprOracleNullif::eval_nullif_not_null, /* 385 */
NULL, // ObExprNaNvl::eval_nanvl, /* 386 */
ObExprFormat::calc_format_expr, /* 387 */
calc_translate_using_expr, /* 388 */
ObExprQuarter::calc_quater, /* 389 */
ObExprBitLength::calc_bit_length, /* 390 */
NULL, // ObExprConvertOracle::calc_convert_oracle_expr, /* 391 */
NULL, // ObExprUnistr::calc_unistr_expr, /* 392 */
NULL, // ObExprAsciistr::calc_asciistr_expr, /* 393 */
NULL, // ObExprAtTimeZone::eval_at_time_zone, /* 394 */
NULL, // ObExprAtLocal::eval_at_local, /* 395 */
ObExprToSingleByte::calc_to_single_byte, /* 396 */
ObExprToMultiByte::calc_to_multi_byte, /* 397 */
NULL, // ObExprDllUdf::eval_dll_udf, /* 398 */
NULL, // ObExprRawtonhex::calc_rawtonhex_expr, /* 399 */
ObExprPi::eval_pi, /* 400 */
ObExprConvertTZ::eval_convert_tz, /* 401 */
ObExprUtcTime::eval_utc_time, /* 402 */
ObExprUtcDate::eval_utc_date, /* 403 */
ObExprGetFormat::calc_get_format, /* 404 */
NULL, //ObExprCollectionConstruct::eval_collection_construct, /* 405 */
NULL, //ObExprObjAccess::eval_obj_access, /* 406 */
ObExprTimeFormat::calc_time_format, /* 407 */
ObExprMakedate::calc_makedate, /* 408 */
ObExprPeriodAdd::calc_periodadd, /* 409 */
ObExprExportSet::eval_export_set, /* 410 */
ObExprConvertTZ::eval_convert_tz /* 411 */
NULL, // ObExprIs::calc_is_infinite, /* 381 */
NULL, // ObExprIs::calc_is_nan, /* 382 */
NULL, // ObExprIsNot::calc_is_not_infinite, /* 383 */
NULL, // ObExprIsNot::calc_is_not_nan, /* 384 */
ObExprOracleNullif::eval_nullif_not_null, /* 385 */
NULL, // ObExprNaNvl::eval_nanvl, /* 386 */
ObExprFormat::calc_format_expr, /* 387 */
calc_translate_using_expr, /* 388 */
ObExprQuarter::calc_quater, /* 389 */
ObExprBitLength::calc_bit_length, /* 390 */
NULL, // ObExprConvertOracle::calc_convert_oracle_expr, /* 391 */
NULL, // ObExprUnistr::calc_unistr_expr, /* 392 */
NULL, // ObExprAsciistr::calc_asciistr_expr, /* 393 */
NULL, // ObExprAtTimeZone::eval_at_time_zone, /* 394 */
NULL, // ObExprAtLocal::eval_at_local, /* 395 */
ObExprToSingleByte::calc_to_single_byte, /* 396 */
ObExprToMultiByte::calc_to_multi_byte, /* 397 */
NULL, // ObExprDllUdf::eval_dll_udf, /* 398 */
NULL, // ObExprRawtonhex::calc_rawtonhex_expr, /* 399 */
ObExprPi::eval_pi, /* 400 */
ObExprConvertTZ::eval_convert_tz, /* 401 */
ObExprUtcTime::eval_utc_time, /* 402 */
ObExprUtcDate::eval_utc_date, /* 403 */
ObExprGetFormat::calc_get_format, /* 404 */
NULL, // ObExprCollectionConstruct::eval_collection_construct, /* 405 */
NULL, // ObExprObjAccess::eval_obj_access, /* 406 */
ObExprTimeFormat::calc_time_format, /* 407 */
ObExprMakedate::calc_makedate, /* 408 */
ObExprPeriodAdd::calc_periodadd, /* 409 */
ObExprExportSet::eval_export_set, /* 410 */
ObExprConvertTZ::eval_convert_tz, /* 411 */
ObExprInet6Aton::calc_inet6_aton, /* 412 */
ObExprIsIpv4::calc_is_ipv4, /* 413 */
ObExprIsIpv6::calc_is_ipv6, /* 414 */
ObExprIsIpv4Mapped::calc_is_ipv4_mapped, /* 415 */
ObExprIsIpv4Compat::calc_is_ipv4_compat, /* 416 */
ObExprInetAton::calc_inet_aton, /* 417 */
ObExprInet6Ntoa::calc_inet6_ntoa, /* 418 */
};
REG_SER_FUNC_ARRAY(OB_SFA_SQL_EXPR_EVAL, g_expr_eval_functions, ARRAYSIZEOF(g_expr_eval_functions));
......
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#define USING_LOG_PREFIX SQL_ENG
#include <string.h>
#include "sql/parser/ob_item_type.h"
#include "sql/engine/expr/ob_expr_inet.h"
#include "sql/engine/ob_exec_context.h"
using namespace oceanbase::common;
namespace oceanbase {
namespace sql {
int ObExprInetCommon::str_to_ipv4(int len, const char *str, bool& is_ip_format_invalid, in_addr* ipv4addr)
{
is_ip_format_invalid = false;
int ret = OB_SUCCESS;
//Shortest IPv4 address:"x.x.x.x",length:7
if (7 > len || INET_ADDRSTRLEN - 1 < len) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, too short or too long", K(len));
} else if (OB_UNLIKELY(OB_UNLIKELY(OB_ISNULL(str) || OB_ISNULL(ipv4addr)))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ip_str or ipv4addr is null", K(ret), K(str), K(ipv4addr));
} else {
unsigned char byte_addr[4];
int dotcnt = 0, numcnt = 0;
int byte = 0;
char c;
for (int i = 0; !is_ip_format_invalid && i < len && *(i + str); ++i) {
c = *(i + str);
if ('.' == c) {
if (255 < byte) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid", K(byte));
} else if (0 == numcnt || 3 < numcnt) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, group num count invalid", K(numcnt), K(dotcnt));
} else if (i == len - 1 || !*(i + str + 1)) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, end with '.'", K(i));
} else {
byte_addr[dotcnt] = (unsigned char) byte;
}
dotcnt ++;
if (3 < dotcnt) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, too much '.'", K(dotcnt), K(i));
} else {
}
byte = 0;
numcnt = 0;
} else if ('9' >= c && '0' <= c) {
byte = byte * 10 + c - '0';
numcnt ++;
} else {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, invalid number", K(c), K(i));
}
}
if (255 < byte) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid", K(byte));
} else if (3 != dotcnt) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, number of '.' is not 3", K(dotcnt));
} else if ('.' == c) { // IP number can't end on '.'
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, end with '.'");
} else {
byte_addr[3] = (unsigned char) byte;
}
MEMCPY((unsigned char *) ipv4addr, byte_addr, sizeof(in_addr));
}
return ret;
}
int ObExprInetCommon::str_to_ipv6(int len, const char *str, bool& is_ip_format_invalid, in6_addr* ipv6addr)
{
int ret = OB_SUCCESS;
is_ip_format_invalid = false;
//Ipv6 length of mysql support: 2~39
//Shortest IPv6 address:"::",length:2
if (2 > len || INET6_ADDRSTRLEN - 1 < len) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, too short or too long", K(len));
} else if (OB_UNLIKELY(OB_ISNULL(str) || OB_ISNULL(ipv6addr))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ip_str or ipv6addr is null", K(ret), K(str), K(ipv6addr));
} else {
int dst_index = 0;
char c;
int group = 0, num = 0;
int numcnt = 0;
int group_start = 0;
int gap_index = -1;
int i = 0;
uint8_t ip_addr[sizeof(in6_addr)];
MEMSET(ip_addr, 0, sizeof(in6_addr));
if (':' == str[0]) {
if (':' == str[1]) {
i = 1;
} else { //cannot start with ":x"
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, start with :x", K(str[1]));
}
} else {
}
for (; !is_ip_format_invalid && i < len && str[i]; ++i) {
c = str[i];
if (':' == c) {
group_start = i + 1;
if (0 == numcnt) {
if (-1 == gap_index) {
gap_index = dst_index;
} else { //number of "::" greater than 1
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, too much '::'", K(gap_index), K(i));
}
} else if (i >= len || !str[i+1]) { // cannot end with "x:"
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, end with x:", K(i));
} else if (dst_index + 2 > sizeof(in6_addr)) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid", K(i), K(dst_index));
} else {
ip_addr[dst_index] = (unsigned char) (group >> 8) & 0xff;
ip_addr[dst_index + 1] = (unsigned char) group & 0xff;
dst_index += 2;
group = 0;
numcnt = 0;
}
} else if ('.' == c) {
if (dst_index + sizeof(in_addr) > sizeof(in6_addr)) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, no room for ipv4", K(dst_index), K(i));
} else if (OB_FAIL(str_to_ipv4(len - group_start, str + group_start, is_ip_format_invalid, (in_addr *)(ip_addr + dst_index)))) {
LOG_WARN("fail to excute str_to_ipv4", K(ret));
} else if (is_ip_format_invalid) {
LOG_WARN("ipv4 format invalid", K(group_start));
} else {
numcnt = 0;
dst_index += 4;
}
break;
} else {
numcnt++;
if (4 < numcnt) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, too many numbers in one group", K(numcnt), K(i));
} else {
if ('0' <= c && '9' >= c) {
num = c - '0';
} else if ('a' <= c && 'f' >= c) {
num = c - 'a' + 10;
} else if ('A' <= c && 'F' >= c) {
num = c - 'A' + 10;
} else {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, invaild number", K(c), K(i));
}
group = (group << 4) | num;
}
}
}
if (!is_ip_format_invalid) {
if (0 != numcnt) { //add last group: ":xxxx"
if (dst_index + 2 > sizeof(in6_addr)) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, too many groups", K(dst_index), K(i));
} else {
ip_addr[dst_index] = (unsigned char) (group >> 8) & 0xff;
ip_addr[dst_index+1] = (unsigned char) group & 0xff;
dst_index += 2;
}
} else {
}
if (-1 != gap_index) {
if (dst_index == sizeof(in6_addr)) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid, no room for ::", K(gap_index));
} else {
int to_move = dst_index - gap_index;
for (int i = 1; i <= to_move; ++i) {
ip_addr[sizeof(in6_addr) - i] = ip_addr[gap_index + to_move - i];
ip_addr[gap_index + to_move - i] = 0;
}
}
} else if (dst_index != sizeof(in6_addr)) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid", K(dst_index),K(sizeof(in6_addr)));
} else {
}
MEMCPY(ipv6addr, ip_addr, sizeof(in6_addr));
}
}
return ret;
}
int ObExprInetCommon::ip_to_str(ObString& ip_binary, bool& is_ip_format_invalid, ObString& ip_str)
{
is_ip_format_invalid = false;
int ret = OB_SUCCESS;
const char *ip = ip_binary.ptr();
char * result_ptr = ip_str.ptr();
char result[common::MAX_IP_ADDR_LENGTH];
if (OB_UNLIKELY(OB_ISNULL(ip) || OB_ISNULL(result_ptr))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ip or result is null", K(ret), K(ip), K(result));
}else if (sizeof(in_addr) == ip_binary.length()) {
const unsigned char *ipv4_groups = (const unsigned char *) ip;
sprintf((char *)result_ptr, "%d.%d.%d.%d", ipv4_groups[0], ipv4_groups[1], ipv4_groups[2], ipv4_groups[3]);
ip_str.assign(result_ptr, static_cast<ObString::obstr_size_t>(strlen(result_ptr)));
} else if (sizeof(in6_addr) == ip_binary.length()) {
int gap = -1, gap_len = -1;
int rg = -1, rg_len = -1;
const uint16_t *ipv6_binary = (const uint16_t *) ip;
uint16_t ipv6_groups[8];
for (int i= 0; i < 8; ++i) {
ipv6_groups[i] = (ipv6_binary[i] << 8) | ((ipv6_binary[i] >> 8) & 0xff);
}
//find longest gap
for (int i = 0; i < 8; ++i) {
if (0 == ipv6_groups[i]) {
if (-1 == rg_len) {
rg_len = 1;
rg = i;
} else {
rg_len++;
}
} else {
if (rg_len > gap_len) {
gap = rg;
gap_len = rg_len;
} else {
}
rg_len = -1;
}
}
if (rg_len > gap_len) {
gap = rg;
gap_len = rg_len;
} else {
}
char *p = &result[0];
for (int i = 0; i < 8; ++i) {
if (gap == i) {
if (0 == i) {
*p = ':';
++p;
} else {
}
*p = ':';
++p;
i += gap_len - 1; //skip gap
} else if (6 == i && 0 == gap &&
(6 == gap_len || // IPv4-compatible
(5 == gap_len && 0xffff == ipv6_groups[5]) // IPv4-mapped
))
{
const unsigned char *ipv4_groups = (const unsigned char *) ip + 12;
p += sprintf(p, "%d.%d.%d.%d", ipv4_groups[0], ipv4_groups[1], ipv4_groups[2], ipv4_groups[3]);
break;
} else {
p += sprintf(p, "%x", ipv6_groups[i]);
if (i != 7) {
*p = ':';
++p;
}
}
}
*p = '\0';
int len = static_cast<ObString::obstr_size_t>(strlen(result));
MEMCPY(result_ptr, result, len);
ip_str.assign(result_ptr, len);
} else {
is_ip_format_invalid = true;
LOG_WARN("invalid ip length", K(ip_binary.length()));
}
return ret;
}
ObExprInetAton::ObExprInetAton(ObIAllocator& alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_INETATON, N_INETATON, 1, NOT_ROW_DIMENSION)
{}
ObExprInetAton::~ObExprInetAton()
{}
int ObExprInetAton::calc(ObObj& result, const ObObj& text, uint64_t cast_mode)
{
int ret = OB_SUCCESS;
if (text.is_null()) {
result.set_null();
} else {
ObString m_text = text.get_string();
bool is_ip_format_invalid = false;
if (OB_FAIL(ob_inet_aton(result, m_text, is_ip_format_invalid))) {
LOG_WARN("fail to excute ob_inet_aton", K(ret));
} else if (is_ip_format_invalid) {
if (CM_IS_WARN_ON_FAIL(cast_mode)) {
result.set_null();
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("ip format invalid", K(ret), K(m_text));
}
}
}
return ret;
}
int ObExprInetAton::calc_result1(ObObj& result, const ObObj& text, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
uint64_t cast_mode = 0;
ObSQLSessionInfo* session = expr_ctx.my_session_;
if (OB_UNLIKELY(OB_ISNULL(session))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else {
ObSQLUtils::get_default_cast_mode(session->get_stmt_type(), session, cast_mode);
if (OB_FAIL(calc(result, text, cast_mode))) {
LOG_WARN("fail to calc", K(ret));
} else if (OB_LIKELY(!result.is_null())) {
result.set_collation_type(result_type_.get_collation_type());
result.set_collation_level(result_type_.get_collation_level());
} else {
}
}
return ret;
}
template <typename T>
int ObExprInetAton::ob_inet_aton(T& result, const ObString& text, bool& is_ip_format_invalid)
{
int ret = OB_SUCCESS;
is_ip_format_invalid = false;
if (OB_UNLIKELY(OB_ISNULL(text.ptr()))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("ip format invalid, text is null", K(ret));
}else if (0 == text.length()) {
is_ip_format_invalid = true;
LOG_WARN("ip length is zero");
} else {
int len = text.length();
char c;
int cnt = 0;
unsigned long value = 0;
int byte = 0;
for (int i = 0; !is_ip_format_invalid && i < len; ++i) {
c = text.ptr()[i];
if ('0' <= c && '9' >= c) {
byte = byte * 10 + c - '0';
if (255 < byte) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid", K(byte), K(text));
} else {
}
} else if ('.' == c) {
cnt++;
value = (value << 8) + byte;
byte = 0;
} else {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid", K(c), K(text));
}
}
if (is_ip_format_invalid) {
} else if (3 < cnt || '.' == c) { // IP number can't end on '.', '.' count <= 3
is_ip_format_invalid = true;
LOG_WARN("ip format invalid", K(text), K(cnt), K(c));
} else {
/*
Attempt to support short forms of IP-addresses.
Examples:
127 -> 0.0.0.127
127.255 -> 127.0.0.255
127.256 -> NULL
127.2.1 -> 127.2.0.1
*/
if (2 == cnt) {
value <<= 8;
} else if (1 == cnt) {
value <<= 16;
} else {
}
value = (value << 8) + byte;
result.set_int(value);
}
}
return ret;
}
int ObExprInetAton::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
int ret = OB_SUCCESS;
if (rt_expr.arg_cnt_ != 1) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("inet_aton expr should have one param", K(ret), K(rt_expr.arg_cnt_));
} else if (OB_UNLIKELY(OB_ISNULL(rt_expr.args_) || OB_ISNULL(rt_expr.args_[0]))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of inet_aton expr is null", K(ret), K(rt_expr.args_));
} else {
rt_expr.eval_func_ = ObExprInetAton::calc_inet_aton;
}
return ret;
}
int ObExprInetAton::calc_inet_aton(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
{
int ret = OB_SUCCESS;
if (OB_FAIL(expr.eval_param_value(ctx))) {
LOG_WARN("inet_aton expr eval param value failed", K(ret));
} else {
ObDatum& text = expr.locate_param_datum(ctx, 0);
if (text.is_null()) {
expr_datum.set_null();
} else {
ObString m_text = text.get_string();
bool is_ip_format_invalid = false;
if (OB_FAIL(ob_inet_aton(expr_datum, m_text, is_ip_format_invalid))) {
LOG_WARN("fail to excute ob_inet_aton", K(ret));
}else if (is_ip_format_invalid) {
uint64_t cast_mode = 0;
ObSQLSessionInfo* session = ctx.exec_ctx_.get_my_session();
if (OB_UNLIKELY(OB_ISNULL(session))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else {
ObSQLUtils::get_default_cast_mode(session->get_stmt_type(), session, cast_mode);
if (CM_IS_WARN_ON_FAIL(cast_mode)) { //support no strict sql_mode
expr_datum.set_null();
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("fail to convert ip to int", K(ret), K(m_text));
}
}
}
}
}
return ret;
}
ObExprInet6Ntoa::ObExprInet6Ntoa(ObIAllocator& alloc) : ObStringExprOperator(alloc, T_FUN_SYS_INET6NTOA, N_INET6NTOA, 1)
{}
ObExprInet6Ntoa::~ObExprInet6Ntoa()
{}
inline int ObExprInet6Ntoa::calc_result_type1(
ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const
{
int ret = OB_SUCCESS;
UNUSED(text);
type.set_varchar();
type.set_length(common::MAX_IP_ADDR_LENGTH);
type.set_collation_level(common::CS_LEVEL_COERCIBLE);
const sql::ObSQLSessionInfo *session = type_ctx.get_session();
if (OB_UNLIKELY(OB_ISNULL(session))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is null",K(ret));
} else {
type.set_collation_type(session->get_nls_collation());
}
return ret;
}
int ObExprInet6Ntoa::calc(ObObj& result, const ObObj& text, common::ObExprStringBuf& string_buf, uint64_t cast_mode)
{
int ret = OB_SUCCESS;
bool is_ip_format_invalid = false;
if (text.is_null()) {
result.set_null();
} else if (!text.is_varbinary()) {
is_ip_format_invalid = true;
LOG_WARN("ip is not binary", K(ret), K(text));
} else {
ObString num_val = text.get_varbinary();
char *buf = static_cast<char*>(string_buf.alloc(MAX_IP_ADDR_LENGTH));
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("alloc memory failed", K(ret), K(text));
} else {
ObString ip_str(MAX_IP_ADDR_LENGTH, 0, buf);
ObExprInetCommon::ip_to_str(num_val, is_ip_format_invalid, ip_str);
result.set_varchar(ip_str);
}
}
if (OB_SUCC(ret) && is_ip_format_invalid) {
if (CM_IS_WARN_ON_FAIL(cast_mode)) {
result.set_null();
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("ip format invalid", K(ret), K(text));
}
}
return ret;
}
int ObExprInet6Ntoa::calc_result1(ObObj& result, const ObObj& text, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
uint64_t cast_mode = 0;
ObSQLSessionInfo* session = expr_ctx.my_session_;
if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_NOT_INIT;
LOG_WARN("varchar buffer not init", K(ret));
} else if (OB_UNLIKELY(OB_ISNULL(session))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else {
ObSQLUtils::get_default_cast_mode(session->get_stmt_type(), session, cast_mode);
if (OB_FAIL(calc(result, text, *expr_ctx.calc_buf_, cast_mode))) {
LOG_WARN("fail to calc", K(ret));
} else if (OB_LIKELY(!result.is_null())) {
result.set_collation_type(result_type_.get_collation_type());
result.set_collation_level(result_type_.get_collation_level());
} else {
}
}
return ret;
}
int ObExprInet6Ntoa::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
int ret = OB_SUCCESS;
if (1 != rt_expr.arg_cnt_) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("inet6_ntoa expr should have one param", K(ret), K(rt_expr.arg_cnt_));
} else if (OB_UNLIKELY(OB_ISNULL(rt_expr.args_) || OB_ISNULL(rt_expr.args_[0]))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of inet6_ntoa expr is null", K(ret), K(rt_expr.args_));
} else {
rt_expr.eval_func_ = ObExprInet6Ntoa::calc_inet6_ntoa;
}
return ret;
}
int ObExprInet6Ntoa::calc_inet6_ntoa(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
{
int ret = OB_SUCCESS;
if (OB_FAIL(expr.eval_param_value(ctx))) {
LOG_WARN("inet6_ntoa expr eval param value failed", K(ret));
} else {
ObDatum& text = expr.locate_param_datum(ctx, 0);
if (text.is_null()) {
expr_datum.set_null();
} else {
char * buf = NULL;
CK(expr.res_buf_len_ >= MAX_IP_ADDR_LENGTH);
if (OB_FAIL(ret)) {
LOG_WARN("result buf size greater than MAX_IP_ADDR_LENGTH", K(ret));
} else if (OB_ISNULL(buf = expr.get_str_res_mem(ctx, MAX_IP_ADDR_LENGTH))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("Failed to allocate memory for lob locator", K(ret), K(MAX_IP_ADDR_LENGTH));
} else {
bool is_ip_format_invalid = false;
ObString num_val = text.get_string();
ObString ip_str(MAX_IP_ADDR_LENGTH, 0, buf);
if (!ob_is_varbinary_type(expr.args_[0]->datum_meta_.type_,expr.args_[0]->datum_meta_.cs_type_)) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid", K(ret), K(text));
} else if (OB_FAIL(ObExprInetCommon::ip_to_str(num_val, is_ip_format_invalid, ip_str))) {
LOG_WARN("fail to excute ip_to_str", K(ret));
} else if (!is_ip_format_invalid) {
expr_datum.set_string(ip_str);
}
if (OB_SUCC(ret) && is_ip_format_invalid) {
uint64_t cast_mode = 0;
ObSQLSessionInfo* session = ctx.exec_ctx_.get_my_session();
if (OB_UNLIKELY(OB_ISNULL(session))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else {
ObSQLUtils::get_default_cast_mode(session->get_stmt_type(), session, cast_mode);
if (CM_IS_WARN_ON_FAIL(cast_mode)) {
expr_datum.set_null(); //support no strict sql_mode
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("ip format invalid", K(ret),K(text));
}
}
}
}
}
}
return ret;
}
ObExprInet6Aton::ObExprInet6Aton(ObIAllocator& alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_INET6ATON, N_INET6ATON, 1, NOT_ROW_DIMENSION)
{}
ObExprInet6Aton::~ObExprInet6Aton()
{}
int ObExprInet6Aton::calc(ObObj& result, const ObObj& text, common::ObExprStringBuf& string_buf, uint64_t cast_mode)
{
int ret = OB_SUCCESS;
if (text.is_null()) {
result.set_null();
} else {
ObString num_val = text.get_varbinary();
char *buf = static_cast<char*>(string_buf.alloc(sizeof(in6_addr)));
if (OB_ISNULL(buf)) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_ERROR("alloc memory failed", K(ret), K(text));
} else {
bool is_ip_format_invalid = false;
ObString m_text = text.get_string();
ObString result_buf(sizeof(in6_addr), 0, buf);
if (OB_FAIL(inet6_aton(m_text, is_ip_format_invalid, result_buf))) {
LOG_WARN("fail to excute inet6_aton");
} else if (is_ip_format_invalid) {
if (CM_IS_WARN_ON_FAIL(cast_mode)) {
result.set_null(); //support no strict mode
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("ip format invalid", K(ret), K(m_text));
}
} else {
result.set_varbinary(result_buf);
}
}
}
return ret;
}
int ObExprInet6Aton::calc_result1(ObObj& result, const ObObj& text, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
uint64_t cast_mode = 0;
ObSQLSessionInfo* session = expr_ctx.my_session_;
if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_NOT_INIT;
LOG_WARN("varchar buffer not init", K(ret));
} else if (OB_UNLIKELY(OB_ISNULL(session))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("session is NULL", K(ret));
} else {
ObSQLUtils::get_default_cast_mode(session->get_stmt_type(), session, cast_mode);
if (OB_FAIL(calc(result, text, *expr_ctx.calc_buf_, cast_mode))) {
LOG_WARN("fail to calc", K(ret));
} else if (OB_LIKELY(!result.is_null())) {
result.set_collation_type(result_type_.get_collation_type());
result.set_collation_level(result_type_.get_collation_level());
} else {
}
}
return ret;
}
int ObExprInet6Aton::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
int ret = OB_SUCCESS;
if (1 != rt_expr.arg_cnt_) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("inet6_aton expr should have one param", K(ret), K(rt_expr.arg_cnt_));
} else if (OB_UNLIKELY(OB_ISNULL(rt_expr.args_) || OB_ISNULL(rt_expr.args_[0]))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of inet6_aton expr is null", K(ret), K(rt_expr.args_));
} else {
rt_expr.eval_func_ = ObExprInet6Aton::calc_inet6_aton;
}
return ret;
}
int ObExprInet6Aton::calc_inet6_aton(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
{
int ret = OB_SUCCESS;
if (OB_FAIL(expr.eval_param_value(ctx))) {
LOG_WARN("inet6_ntoa expr eval param value failed", K(ret));
} else {
char * buf = NULL;
CK(expr.res_buf_len_ >= sizeof(in6_addr));
if (OB_FAIL(ret)) {
LOG_WARN("result buf size greater than sizeof(in6_addr)", K(ret));
} else if (OB_ISNULL(buf = expr.get_str_res_mem(ctx, sizeof(in6_addr)))) {
ret = OB_ALLOCATE_MEMORY_FAILED;
LOG_WARN("Failed to allocate memory for lob locator", K(ret), K(sizeof(in6_addr)));
} else {
ObDatum& text = expr.locate_param_datum(ctx, 0);
ObString m_text = text.get_string();
bool is_ip_format_invalid = false;
if (text.is_null()) {
expr_datum.set_null();
} else {
ObString str_result(sizeof(in6_addr), 0, buf);
if (OB_FAIL(inet6_aton(m_text, is_ip_format_invalid, str_result))) {
LOG_WARN("fail to excute inet6_aton", K(ret));
} else if (is_ip_format_invalid) {
uint64_t cast_mode = 0;
ObSQLSessionInfo* session = ctx.exec_ctx_.get_my_session();
if (OB_ISNULL(session)) {
LOG_WARN("session is NULL", K(ret));
} else {
ObSQLUtils::get_default_cast_mode(session->get_stmt_type(), session, cast_mode);
if (CM_IS_WARN_ON_FAIL(cast_mode)) {
expr_datum.set_null(); //support no strict mode
} else {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("ip format invalid", K(ret));
}
}
} else {
expr_datum.set_string(str_result);
}
}
}
}
return ret;
}
int ObExprInet6Aton::inet6_aton(const ObString& ip, bool& is_ip_format_invalid, ObString& str_result)
{
is_ip_format_invalid = false;
int ret = OB_SUCCESS;
char buf[MAX_IP_ADDR_LENGTH];
if (INET6_ADDRSTRLEN - 1 < ip.length()) {
is_ip_format_invalid = true;
LOG_WARN("ip format invalid", K(ip));
} else {
char *result_buf = str_result.ptr();
if (OB_UNLIKELY(OB_ISNULL(result_buf))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("result_buf is null", K(ret));
} else {
MEMCPY(buf, ip.ptr(), ip.length());
buf[ip.length()] = '\0';
if (OB_FAIL(ObExprInetCommon::str_to_ipv4(ip.length(), buf, is_ip_format_invalid, (in_addr *)result_buf))) {
LOG_WARN("fail to excute str_to_ipv4", K(ret));
}else if (is_ip_format_invalid) {
if (OB_FAIL(ObExprInetCommon::str_to_ipv6(ip.length(), buf, is_ip_format_invalid, (in6_addr *)result_buf))) {
LOG_WARN("fail to excute str_to_ipv6", K(ret));
} else if (is_ip_format_invalid) {
LOG_WARN("ip format invalid", K(ip));
} else {
str_result.assign(result_buf, sizeof(in6_addr));
}
} else {
str_result.assign(result_buf, sizeof(in_addr));
}
}
}
return ret;
}
ObExprIsIpv4::ObExprIsIpv4(ObIAllocator& alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_IS_IPV4, N_IS_IPV4, 1, NOT_ROW_DIMENSION)
{}
ObExprIsIpv4::~ObExprIsIpv4()
{}
int ObExprIsIpv4::calc(ObObj& result, const ObObj& text)
{
int ret = OB_SUCCESS;
if (text.is_null()) {
result.set_int(0);
} else {
ObString m_text = text.get_string();
if (OB_FAIL(is_ipv4(result, m_text))) {
LOG_WARN("fail to excute is_ipv4", K(ret));
}
}
return ret;
}
int ObExprIsIpv4::calc_result1(ObObj& result, const ObObj& text, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_NOT_INIT;
LOG_WARN("varchar buffer not init", K(ret));
} else if (OB_FAIL(calc(result, text))) {
LOG_WARN("fail to excute calc", K(ret));
}
return ret;
}
template <typename T>
int ObExprIsIpv4::is_ipv4(T& result, const ObString& text)
{
char buf[16];
int ipv4_ret = 1;
int ret = OB_SUCCESS;
if (INET_ADDRSTRLEN - 1 < text.length()) {
ipv4_ret = 0;
} else {
MEMCPY(buf, text.ptr(), text.length());
int len = text.length();
buf[len] = '\0';
struct in_addr addr;
bool is_ip_invalid;
if (OB_FAIL(ObExprInetCommon::str_to_ipv4(len, buf, is_ip_invalid, &addr))) {
LOG_WARN("fail to excute str_to_ipv4");
} else {
ipv4_ret = is_ip_invalid ? 0 : 1;
}
}
result.set_int(ipv4_ret);
return ret;
}
int ObExprIsIpv4::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
int ret = OB_SUCCESS;
if (1 != rt_expr.arg_cnt_) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("is_ipv4 expr should have one param", K(ret), K(rt_expr.arg_cnt_));
} else if (OB_UNLIKELY(OB_UNLIKELY(OB_ISNULL(rt_expr.args_) || OB_ISNULL(rt_expr.args_[0])))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of is_ipv4 expr is null", K(ret), K(rt_expr.args_));
} else {
rt_expr.eval_func_ = ObExprIsIpv4::calc_is_ipv4;
}
return ret;
}
int ObExprIsIpv4::calc_is_ipv4(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
{
int ret = OB_SUCCESS;
if (OB_FAIL(expr.eval_param_value(ctx))) {
LOG_WARN("is_ipv4 expr eval param value failed", K(ret));
} else {
ObDatum& text = expr.locate_param_datum(ctx, 0);
ObString m_text = text.get_string();
if (text.is_null()) {
expr_datum.set_int(0);
} else if (OB_FAIL(is_ipv4(expr_datum, m_text))) {
LOG_WARN("fail to excute is_ipv4", K(ret));
} else {
}
}
return ret;
}
ObExprIsIpv4Mapped::ObExprIsIpv4Mapped(ObIAllocator& alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_IS_IPV4_MAPPED, N_IS_IPV4_MAPPED, 1, NOT_ROW_DIMENSION)
{}
ObExprIsIpv4Mapped::~ObExprIsIpv4Mapped()
{}
void ObExprIsIpv4Mapped::calc(ObObj& result, const ObObj& text)
{
if (text.is_null()) {
result.set_int(0);
} else if (!text.is_varbinary()) {
result.set_int(0);
} else {
ObString m_text = text.get_varbinary();
is_ipv4_mapped(result, m_text);
}
}
int ObExprIsIpv4Mapped::calc_result1(ObObj& result, const ObObj& text, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_NOT_INIT;
LOG_WARN("varchar buffer not init", K(ret));
} else {
calc(result, text);
}
return ret;
}
int ObExprIsIpv4Mapped::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
int ret = OB_SUCCESS;
if (1 != rt_expr.arg_cnt_) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("is_ipv4_mapped expr should have one param", K(ret), K(rt_expr.arg_cnt_));
} else if (OB_UNLIKELY(OB_ISNULL(rt_expr.args_) || OB_ISNULL(rt_expr.args_[0]))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of is_ipv4_mapped expr is null", K(ret), K(rt_expr.args_));
} else {
rt_expr.eval_func_ = ObExprIsIpv4Mapped::calc_is_ipv4_mapped;
}
return ret;
}
int ObExprIsIpv4Mapped::calc_is_ipv4_mapped(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
{
int ret = OB_SUCCESS;
if (OB_FAIL(expr.eval_param_value(ctx))) {
LOG_WARN("is_ipv4_mapped expr eval param value failed", K(ret));
} else {
ObDatum& text = expr.locate_param_datum(ctx, 0);
if (text.is_null()) {
expr_datum.set_int(0);
} else if (!ob_is_varbinary_type(expr.args_[0]->datum_meta_.type_,expr.args_[0]->datum_meta_.cs_type_)) {
expr_datum.set_int(0);
} else {
ObString m_text = text.get_string();
is_ipv4_mapped(expr_datum, m_text);
}
}
return ret;
}
template <typename T>
void ObExprIsIpv4Mapped::is_ipv4_mapped(T& result, const ObString& num_val)
{
char buf[16];
int ipv4_ret = 1;
if (sizeof(in6_addr) != num_val.length()) { //length of binary ipv6 addr is 16
ipv4_ret = 0;
} else {
MEMCPY(buf, num_val.ptr(), num_val.length());
ipv4_ret = IN6_IS_ADDR_V4MAPPED((struct in6_addr *) buf);
}
result.set_int(ipv4_ret);
}
ObExprIsIpv4Compat::ObExprIsIpv4Compat(ObIAllocator& alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_IS_IPV4_COMPAT, N_IS_IPV4_COMPAT, 1, NOT_ROW_DIMENSION)
{}
ObExprIsIpv4Compat::~ObExprIsIpv4Compat()
{}
void ObExprIsIpv4Compat::calc(ObObj& result, const ObObj& text)
{
if (text.is_null()) {
result.set_int(0);
} else if (!text.is_varbinary()) {
result.set_int(0);
} else {
ObString m_text = text.get_varbinary();
is_ipv4_compat(result, m_text);
}
}
int ObExprIsIpv4Compat::calc_result1(ObObj& result, const ObObj& text, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_NOT_INIT;
LOG_WARN("varchar buffer not init", K(ret));
} else {
calc(result, text);
}
return ret;
}
int ObExprIsIpv4Compat::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
int ret = OB_SUCCESS;
if (1 != rt_expr.arg_cnt_) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("is_ipv4_compat expr should have one param", K(ret), K(rt_expr.arg_cnt_));
} else if (OB_UNLIKELY(OB_ISNULL(rt_expr.args_) || OB_ISNULL(rt_expr.args_[0]))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of is_ipv4_compat expr is null", K(ret), K(rt_expr.args_));
} else {
rt_expr.eval_func_ = ObExprIsIpv4Compat::calc_is_ipv4_compat;
}
return ret;
}
int ObExprIsIpv4Compat::calc_is_ipv4_compat(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
{
int ret = OB_SUCCESS;
if (OB_FAIL(expr.eval_param_value(ctx))) {
LOG_WARN("is_ipv4_compat expr eval param value failed", K(ret));
} else {
ObDatum& text = expr.locate_param_datum(ctx, 0);
if (text.is_null()) {
expr_datum.set_int(0);
} else if (!ob_is_varbinary_type(expr.args_[0]->datum_meta_.type_, expr.args_[0]->datum_meta_.cs_type_)) {
expr_datum.set_int(0);
} else {
ObString m_text = text.get_string();
is_ipv4_compat(expr_datum, m_text);
}
}
return ret;
}
template <typename T>
void ObExprIsIpv4Compat::is_ipv4_compat(T& result, const ObString& num_val)
{
char buf[16];
int ipv4_ret = 1;
if (sizeof(in6_addr) != num_val.length()) { //length of binary ipv6 addr is 16
ipv4_ret = 0;
} else {
MEMCPY(buf, num_val.ptr(), num_val.length());
ipv4_ret = IN6_IS_ADDR_V4COMPAT((struct in6_addr *) buf);
}
result.set_int(ipv4_ret);
}
ObExprIsIpv6::ObExprIsIpv6(ObIAllocator& alloc)
: ObFuncExprOperator(alloc, T_FUN_SYS_IS_IPV6, N_IS_IPV6, 1, NOT_ROW_DIMENSION)
{}
ObExprIsIpv6::~ObExprIsIpv6()
{}
int ObExprIsIpv6::calc(ObObj& result, const ObObj& text)
{
int ret = OB_SUCCESS;
if (text.is_null()) {
result.set_int(0);
} else {
ObString m_text = text.get_string();
if (OB_FAIL(is_ipv6(result, m_text))) {
LOG_WARN("fail to excute is_ipv6", K(ret));
}
}
return ret;
}
int ObExprIsIpv6::calc_result1(ObObj& result, const ObObj& text, ObExprCtx& expr_ctx) const
{
int ret = OB_SUCCESS;
if (OB_ISNULL(expr_ctx.calc_buf_)) {
ret = OB_NOT_INIT;
LOG_WARN("varchar buffer not init", K(ret));
} else if (OB_FAIL(calc(result, text))) {
LOG_WARN("fail to excute calc", K(ret));
}
return ret;
}
int ObExprIsIpv6::cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const
{
UNUSED(op_cg_ctx);
UNUSED(raw_expr);
int ret = OB_SUCCESS;
if (1 != rt_expr.arg_cnt_) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("is_ipv6 expr should have one param", K(ret), K(rt_expr.arg_cnt_));
} else if (OB_UNLIKELY(OB_ISNULL(rt_expr.args_) || OB_ISNULL(rt_expr.args_[0]))) {
ret = OB_ERR_UNEXPECTED;
LOG_WARN("children of is_ipv6 expr is null", K(ret), K(rt_expr.args_));
} else {
rt_expr.eval_func_ = ObExprIsIpv6::calc_is_ipv6;
}
return ret;
}
int ObExprIsIpv6::calc_is_ipv6(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum)
{
int ret = OB_SUCCESS;
if (OB_FAIL(expr.eval_param_value(ctx))) {
LOG_WARN("is_ipv6 expr eval param value failed", K(ret));
} else {
ObDatum& text = expr.locate_param_datum(ctx, 0);
ObString m_text = text.get_string();
if (text.is_null()) {
expr_datum.set_int(0);
} else if (OB_FAIL(is_ipv6(expr_datum, m_text))) {
LOG_WARN("fail to excute is_ipv6", K(ret));
}
}
return ret;
}
template <typename T>
int ObExprIsIpv6::is_ipv6(T& result, const ObString& text)
{
char buf[MAX_IP_ADDR_LENGTH];
int ipv6_ret = 1;
int ret = OB_SUCCESS;
if (MAX_IP_ADDR_LENGTH - 1 < text.length()) {
ipv6_ret = 0;
} else {
MEMCPY(buf, text.ptr(), text.length());
buf[text.length()] = '\0';
in6_addr addr;
bool is_ip_invaild;
if (OB_FAIL(ObExprInetCommon::str_to_ipv6(text.length(), buf, is_ip_invaild, &addr))) {
LOG_WARN("fail to excute str_to_ipv6", K(ret));
} else {
ipv6_ret = is_ip_invaild ? 0 : 1;
}
}
result.set_int(ipv6_ret);
return ret;
}
} // namespace sql
} // namespace oceanbase
/**
* Copyright (c) 2021 OceanBase
* OceanBase CE is licensed under Mulan PubL v2.
* You can use this software according to the terms and conditions of the Mulan PubL v2.
* You may obtain a copy of Mulan PubL v2 at:
* http://license.coscl.org.cn/MulanPubL-2.0
* THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY KIND,
* EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO NON-INFRINGEMENT,
* MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE.
* See the Mulan PubL v2 for more details.
*/
#ifndef OCEANBASE_SQL_ENGINE_EXPR_OB_EXPR_INET_
#define OCEANBASE_SQL_ENGINE_EXPR_OB_EXPR_INET_
#include "sql/engine/expr/ob_expr_operator.h"
namespace oceanbase {
namespace sql {
class ObExprInetCommon {
public :
static int str_to_ipv4(int len, const char *str, bool& is_ip_format_invalid, in_addr* ipv4addr);
static int str_to_ipv6(int len, const char *str, bool& is_ip_format_invalid, in6_addr* ipv6addr);
static int ip_to_str(ObString& ip_binary, bool& is_ip_format_invalid, ObString& ip_str);
};
class ObExprInetAton : public ObFuncExprOperator {
public:
explicit ObExprInetAton(common::ObIAllocator& alloc);
virtual ~ObExprInetAton();
virtual int calc_result_type1(ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const;
static int calc(common::ObObj& result, const common::ObObj& text, uint64_t cast_mode);
virtual int calc_result1(common::ObObj& result, const common::ObObj& text, common::ObExprCtx& expr_ctx) const;
virtual int cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const override;
static int calc_inet_aton(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum);
private:
// helper func
template <typename T>
static int ob_inet_aton(T& result, const common::ObString& text, bool& is_ip_format_invalid);
private:
DISALLOW_COPY_AND_ASSIGN(ObExprInetAton);
};
inline int ObExprInetAton::calc_result_type1(
ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const
{
UNUSED(type_ctx);
type.set_int();
type.set_precision(common::ObAccuracy::DDL_DEFAULT_ACCURACY[common::ObIntType].precision_);
type.set_scale(common::ObAccuracy::DDL_DEFAULT_ACCURACY[common::ObIntType].scale_);
// set calc type
text.set_calc_type(common::ObVarcharType);
return common::OB_SUCCESS;
}
class ObExprInet6Ntoa : public ObStringExprOperator {
public:
explicit ObExprInet6Ntoa(common::ObIAllocator& alloc);
virtual ~ObExprInet6Ntoa();
virtual int calc_result_type1(ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const;
static int calc(common::ObObj& result, const common::ObObj& text, common::ObExprStringBuf& string_buf, uint64_t cast_mode);
virtual int calc_result1(common::ObObj& result, const common::ObObj& text, common::ObExprCtx& expr_ctx) const;
virtual int cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const override;
static int calc_inet6_ntoa(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum);
private:
DISALLOW_COPY_AND_ASSIGN(ObExprInet6Ntoa);
};
class ObExprInet6Aton : public ObFuncExprOperator {
public:
explicit ObExprInet6Aton(common::ObIAllocator& alloc);
virtual ~ObExprInet6Aton();
virtual int calc_result_type1(ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const;
static int calc(common::ObObj& result, const common::ObObj& text, common::ObExprStringBuf& string_buf, uint64_t cast_mode);
virtual int calc_result1(common::ObObj& result, const common::ObObj& text, common::ObExprCtx& expr_ctx) const;
virtual int cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const override;
static int calc_inet6_aton(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum);
private:
// helper func
static int inet6_aton(const ObString& ip, bool& is_ip_format_invalid, ObString& str_result);
private:
DISALLOW_COPY_AND_ASSIGN(ObExprInet6Aton);
};
inline int ObExprInet6Aton::calc_result_type1(
ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const
{
UNUSED(type_ctx);
type.set_varbinary();
type.set_length(16);
type.set_collation_level(common::CS_LEVEL_COERCIBLE);
text.set_calc_type(common::ObVarcharType);
return common::OB_SUCCESS;
}
class ObExprIsIpv4 : public ObFuncExprOperator {
public:
explicit ObExprIsIpv4(common::ObIAllocator& alloc);
virtual ~ObExprIsIpv4();
virtual int calc_result_type1(ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const;
static int calc(common::ObObj& result, const common::ObObj& text);
virtual int calc_result1(common::ObObj& result, const common::ObObj& text, common::ObExprCtx& expr_ctx) const;
virtual int cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const override;
static int calc_is_ipv4(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum);
private:
// helper func
template <typename T>
static int is_ipv4(T& result, const common::ObString& text);
private:
DISALLOW_COPY_AND_ASSIGN(ObExprIsIpv4);
};
inline int ObExprIsIpv4::calc_result_type1(
ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const
{
UNUSED(type_ctx);
type.set_tinyint();
type.set_precision(common::ObAccuracy::DDL_DEFAULT_ACCURACY[common::ObTinyIntType].precision_);
type.set_scale(common::ObAccuracy::DDL_DEFAULT_ACCURACY[common::ObTinyIntType].scale_);
// set calc type
text.set_calc_type(common::ObVarcharType);
return common::OB_SUCCESS;
}
class ObExprIsIpv4Mapped : public ObFuncExprOperator {
public:
explicit ObExprIsIpv4Mapped(common::ObIAllocator& alloc);
virtual ~ObExprIsIpv4Mapped();
virtual int calc_result_type1(ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const;
static void calc(common::ObObj& result, const common::ObObj& text);
virtual int calc_result1(common::ObObj& result, const common::ObObj& text, common::ObExprCtx& expr_ctx) const;
virtual int cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const override;
static int calc_is_ipv4_mapped(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum);
private:
// helper func
template <typename T>
static void is_ipv4_mapped(T& result, const common::ObString& num_val);
private:
DISALLOW_COPY_AND_ASSIGN(ObExprIsIpv4Mapped);
};
inline int ObExprIsIpv4Mapped::calc_result_type1(
ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const
{
UNUSED(type_ctx);
UNUSED(text);
type.set_tinyint();
type.set_precision(common::ObAccuracy::DDL_DEFAULT_ACCURACY[common::ObTinyIntType].precision_);
type.set_scale(common::ObAccuracy::DDL_DEFAULT_ACCURACY[common::ObTinyIntType].scale_);
return common::OB_SUCCESS;
}
class ObExprIsIpv4Compat : public ObFuncExprOperator {
public:
explicit ObExprIsIpv4Compat(common::ObIAllocator& alloc);
virtual ~ObExprIsIpv4Compat();
virtual int calc_result_type1(ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const;
static void calc(common::ObObj& result, const common::ObObj& text);
virtual int calc_result1(common::ObObj& result, const common::ObObj& text, common::ObExprCtx& expr_ctx) const;
virtual int cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const override;
static int calc_is_ipv4_compat(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum);
private:
// helper func
template <typename T>
static void is_ipv4_compat(T& result, const common::ObString& num_val);
private:
DISALLOW_COPY_AND_ASSIGN(ObExprIsIpv4Compat);
};
inline int ObExprIsIpv4Compat::calc_result_type1(
ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const
{
UNUSED(type_ctx);
UNUSED(text);
type.set_tinyint();
type.set_precision(common::ObAccuracy::DDL_DEFAULT_ACCURACY[common::ObTinyIntType].precision_);
type.set_scale(common::ObAccuracy::DDL_DEFAULT_ACCURACY[common::ObTinyIntType].scale_);
return common::OB_SUCCESS;
}
class ObExprIsIpv6 : public ObFuncExprOperator {
public:
explicit ObExprIsIpv6(common::ObIAllocator& alloc);
virtual ~ObExprIsIpv6();
virtual int calc_result_type1(ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const;
static int calc(common::ObObj& result, const common::ObObj& text);
virtual int calc_result1(common::ObObj& result, const common::ObObj& text, common::ObExprCtx& expr_ctx) const;
virtual int cg_expr(ObExprCGCtx& op_cg_ctx, const ObRawExpr& raw_expr, ObExpr& rt_expr) const override;
static int calc_is_ipv6(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& expr_datum);
private:
// helper func
template <typename T>
static int is_ipv6(T& result, const common::ObString& num_val);
private:
DISALLOW_COPY_AND_ASSIGN(ObExprIsIpv6);
};
inline int ObExprIsIpv6::calc_result_type1(
ObExprResType& type, ObExprResType& text, common::ObExprTypeCtx& type_ctx) const
{
UNUSED(type_ctx);
type.set_tinyint();
type.set_precision(common::ObAccuracy::DDL_DEFAULT_ACCURACY[common::ObTinyIntType].precision_);
type.set_scale(common::ObAccuracy::DDL_DEFAULT_ACCURACY[common::ObTinyIntType].scale_);
// set calc type
text.set_calc_type(common::ObVarcharType);
return common::OB_SUCCESS;
}
} // namespace sql
} // namespace oceanbase
#endif /* OCEANBASE_SQL_ENGINE_EXPR_OB_EXPR_INET6_NTOA_ */
......@@ -73,6 +73,7 @@ int ObExprInt2ip::int2ip(ObObj& result, const int64_t int_val, ObExprStringBuf&
} else {
in_addr addr;
addr.s_addr = htonl(static_cast<uint32_t>(int_val));
// Fix me,The problem with inet_ntop() is that it is available starting from Windows Vista, but the minimum supported version is Windows 2000.
const char* iret = inet_ntop(AF_INET, &addr, buf, 16);
if (OB_ISNULL(iret)) {
ret = OB_ERR_UNEXPECTED;
......
......@@ -377,7 +377,7 @@ int ObExprMod::mod_float(const ObExpr& expr, ObEvalCtx& ctx, ObDatum& datum)
} else {
const float left_f = left->get_float();
const float right_f = right->get_float();
if (fabsf(right_f) == 0.0) {
if (fabsf(right_f) < share::ObUnitConfig::CPU_EPSILON) {
datum.set_float(left_f);
} else {
datum.set_float(fmodf(left_f, right_f));
......
......@@ -1412,7 +1412,7 @@ int ObRelationalExprOperator::compare_nullsafe(int64_t& result, const ObObj& obj
}
bool ObRelationalExprOperator::can_cmp_without_cast(
ObExprResType type1, ObExprResType type2, ObCmpOp cmp_op, const ObSQLSessionInfo& session)
const ObExprResType& type1, const ObExprResType& type2, ObCmpOp cmp_op, const ObSQLSessionInfo& session)
{
bool need_no_cast = false;
if (ob_is_enum_or_set_type(type1.get_type()) && ob_is_enum_or_set_type(type2.get_type())) {
......@@ -2222,10 +2222,7 @@ int ObSubQueryRelationalExpr::calc_resultN(
tmp_row.count_ = param_num - 1;
left_row = &tmp_row;
const ObObj& idx_obj = param_array[param_num - 1];
if (OB_ISNULL(left_row)) {
ret = OB_INVALID_ARGUMENT;
LOG_WARN("left_row is null", K(left_row), K(ret));
} else if (OB_FAIL(idx_obj.get_int(subquery_idx))) {
if (OB_FAIL(idx_obj.get_int(subquery_idx))) {
LOG_WARN("get subquery index failed", K(ret));
} else if (T_WITH_ALL == subquery_key_) {
if (OB_FAIL(calc_result_with_all(result, *left_row, subquery_idx, expr_ctx))) {
......@@ -3427,8 +3424,11 @@ int ObBitwiseExprOperator::calc_(
LOG_WARN("failed to get int64", K(obj2), K(ret));
} else {
ObNumber result;
result.from((int64_v1 & int64_v2), *expr_ctx.calc_buf_);
res.set_number(result);
if (OB_FAIL(result.from((int64_v1 & int64_v2), *expr_ctx.calc_buf_))) {
LOG_WARN("get ObNumber from int64 failed", K(ret), K(int64_v1 & int64_v2));
} else {
res.set_number(result);
}
}
}
}
......
......@@ -1002,7 +1002,7 @@ public:
static int get_equal_meta(common::ObObjMeta& meta, const common::ObObjMeta& meta1, const common::ObObjMeta& meta2);
OB_INLINE static bool can_cmp_without_cast(
ObExprResType type1, ObExprResType type2, common::ObCmpOp cmp_op, common::obj_cmp_func& cmp_func)
const ObExprResType& type1, const ObExprResType& type2, common::ObCmpOp cmp_op, common::obj_cmp_func& cmp_func)
{
bool need_no_cast = false;
// Special processing shows that compare is called (for example: c1> c2),
......@@ -1117,7 +1117,7 @@ protected:
}
static bool can_cmp_without_cast(
ObExprResType type1, ObExprResType type2, common::ObCmpOp cmp_op, const ObSQLSessionInfo& session);
const ObExprResType& type1, const ObExprResType& type2, common::ObCmpOp cmp_op, const ObSQLSessionInfo& session);
protected:
// only use for comparison with 2 operands(calc_result2)
......
......@@ -65,6 +65,7 @@
#include "sql/engine/expr/ob_expr_not_in.h"
#include "sql/engine/expr/ob_expr_int2ip.h"
#include "sql/engine/expr/ob_expr_ip2int.h"
#include "sql/engine/expr/ob_expr_inet.h"
#include "sql/engine/expr/ob_expr_last_exec_id.h"
#include "sql/engine/expr/ob_expr_last_trace_id.h"
#include "sql/engine/expr/ob_expr_is.h"
......@@ -446,6 +447,13 @@ void ObExprOperatorFactory::register_expr_operators()
REG_OP(ObExprNotIn);
REG_OP(ObExprInt2ip);
REG_OP(ObExprIp2int);
REG_OP(ObExprInetAton);
REG_OP(ObExprInet6Ntoa);
REG_OP(ObExprInet6Aton);
REG_OP(ObExprIsIpv4);
REG_OP(ObExprIsIpv6);
REG_OP(ObExprIsIpv4Mapped);
REG_OP(ObExprIsIpv4Compat);
REG_OP(ObExprInsert);
REG_OP(ObExprIs);
REG_OP(ObExprIsNot);
......
......@@ -376,7 +376,7 @@ struct ObExprOperatorFetcher : public ObSqlExpression {
public:
ObExprOperatorFetcher()
// the allocator is never used
: ObSqlExpression(*lib::ObMallocAllocator::get_instance(), 0)
: ObSqlExpression(*lib::ObMallocAllocator::get_instance(), 0), op_(NULL)
{}
virtual int add_expr_item(const ObPostExprItem& item) override
......
......@@ -428,6 +428,13 @@ typedef enum ObItemType {
T_FUN_SYS_PI = 713,
T_FUN_SYS_EXPORT_SET = 714,
T_FUN_SYS_INET6NTOA = 715,
T_FUN_SYS_INET6ATON = 716,
T_FUN_SYS_IS_IPV4 = 717,
T_FUN_SYS_IS_IPV6 = 718,
T_FUN_SYS_IS_IPV4_MAPPED = 719,
T_FUN_SYS_IS_IPV4_COMPAT = 720,
T_FUN_SYS_INETATON = 721,
///< @note add new mysql only function type before this line
T_MYSQL_ONLY_SYS_MAX_OP = 800,
......
......@@ -173,6 +173,13 @@ const char* get_type_name(int type)
case T_FUN_SYS_MD5 : return "T_FUN_SYS_MD5";
case T_FUN_SYS_IP2INT : return "T_FUN_SYS_IP2INT";
case T_FUN_SYS_INT2IP : return "T_FUN_SYS_INT2IP";
case T_FUN_SYS_INET6NTOA : return "T_FUN_SYS_INET6NTOA";
case T_FUN_SYS_INET6ATON : return "T_FUN_SYS_INET6ATON";
case T_FUN_SYS_INETATON : return "T_FUN_SYS_INETATON";
case T_FUN_SYS_IS_IPV4 : return "T_FUN_SYS_IS_IPV4";
case T_FUN_SYS_IS_IPV6 : return "T_FUN_SYS_IS_IPV6";
case T_FUN_SYS_IS_IPV4_MAPPED : return "T_FUN_SYS_IS_IPV4_MAPPED";
case T_FUN_SYS_IS_IPV4_COMPAT : return "T_FUN_SYS_IS_IPV4_COMPAT";
case T_FUN_SYS_INSERT : return "T_FUN_SYS_INSERT";
case T_FUN_SYS_LENGTH : return "T_FUN_SYS_LENGTH";
case T_FUN_SYS_HEX : return "T_FUN_SYS_HEX";
......
......@@ -3547,6 +3547,8 @@ int ObRawExprResolverImpl::process_fun_sys_node(const ParseNode* node, ObRawExpr
type = ObExprOperatorFactory::get_type_by_name("pow");
} else if (0 == func_name.case_compare("ws")) {
type = ObExprOperatorFactory::get_type_by_name("word_segment");
} else if (0 == func_name.case_compare("inet_ntoa")) {
type = ObExprOperatorFactory::get_type_by_name("int2ip");
} else {
type = ObExprOperatorFactory::get_type_by_name(func_name);
}
......
......@@ -667,6 +667,9 @@ int ObRawExprUtils::function_alias(ObRawExprFactory& expr_factory, ObSysFunRawEx
} else if (0 == expr->get_func_name().case_compare("ws")) {
// ws is synonym for word_segment
expr->set_func_name("word_segment");
} else if (0 == expr->get_func_name().case_compare("inet_ntoa")) {
// inet_ntoa is synonym for int2ip
expr->set_func_name("int2ip");
} else {
}
return ret;
......
......@@ -3245,6 +3245,8 @@ int ObResolverUtils::resolve_generated_column_info(
type = ObExprOperatorFactory::get_type_by_name("pow");
} else if (0 == func_name.case_compare("ws")) {
type = ObExprOperatorFactory::get_type_by_name("word_segment");
} else if (0 == func_name.case_compare("inet_ntoa")) {
type = ObExprOperatorFactory::get_type_by_name("int2ip");
} else {
type = ObExprOperatorFactory::get_type_by_name(func_name);
}
......
drop table if exists t1,t2;
select inet_ntoa(null),inet_aton(null);
inet_ntoa(null) inet_aton(null)
NULL NULL
select inet_ntoa(inet_aton("255.255.255.255.255.255.255.255"));
inet_ntoa(inet_aton("255.255.255.255.255.255.255.255"))
NULL
select inet_aton("255.255.255.255.255"),inet_aton("255.255.1.255"),inet_aton("0.1.255");
inet_aton("255.255.255.255.255") inet_aton("255.255.1.255") inet_aton("0.1.255")
NULL 4294902271 65791
select inet_ntoa(1099511627775),inet_ntoa(4294902271),inet_ntoa(511);
inet_ntoa(1099511627775) inet_ntoa(4294902271) inet_ntoa(511)
NULL 255.255.1.255 0.0.1.255
select inet_aton("0.255.255.255.255");
inet_aton("0.255.255.255.255")
NULL
select inet_aton("255.255.255.0000");
inet_aton("255.255.255.0000")
4294967040
select hex(inet_aton('127'));
hex(inet_aton('127'))
7F
select hex(inet_aton('127.1'));
hex(inet_aton('127.1'))
7F000001
select hex(inet_aton('127.1.1'));
hex(inet_aton('127.1.1'))
7F010001
select inet_aton("122.256");
inet_aton("122.256")
NULL
select inet_aton("122.226.");
inet_aton("122.226.")
NULL
select inet_aton("");
inet_aton("")
NULL
# -- INET6_ATON: checking NULL, invalid types, out-of range values...
SELECT INET6_ATON(NULL) IS NULL;
INET6_ATON(NULL) IS NULL
1
SELECT INET6_ATON(123) IS NULL;
INET6_ATON(123) IS NULL
1
SELECT INET6_ATON(123.45) IS NULL;
INET6_ATON(123.45) IS NULL
1
SELECT INET6_ATON(str_to_date('2014,2,28 09', '%Y,%m,%d %h')) IS NULL;
INET6_ATON(str_to_date('2014,2,28 09', '%Y,%m,%d %h')) IS NULL
1
SELECT INET6_ATON('1.2.3') IS NULL;
INET6_ATON('1.2.3') IS NULL
1
SELECT INET6_ATON('1.2.3.') IS NULL;
INET6_ATON('1.2.3.') IS NULL
1
SELECT INET6_ATON('1..3.4') IS NULL;
INET6_ATON('1..3.4') IS NULL
1
SELECT INET6_ATON('-1.2.3.4') IS NULL;
INET6_ATON('-1.2.3.4') IS NULL
1
SELECT INET6_ATON('1.2.3.256') IS NULL;
INET6_ATON('1.2.3.256') IS NULL
1
SELECT INET6_ATON('1.2.3.4.5') IS NULL;
INET6_ATON('1.2.3.4.5') IS NULL
1
SELECT INET6_ATON('0001.2.3.4') IS NULL;
INET6_ATON('0001.2.3.4') IS NULL
1
SELECT INET6_ATON('0x1.2.3.4') IS NULL;
INET6_ATON('0x1.2.3.4') IS NULL
1
SELECT INET6_ATON('a.2.3.4') IS NULL;
INET6_ATON('a.2.3.4') IS NULL
1
SELECT INET6_ATON('1.2.3.4:80') IS NULL;
INET6_ATON('1.2.3.4:80') IS NULL
1
SELECT INET6_ATON('1.2.3.4/32') IS NULL;
INET6_ATON('1.2.3.4/32') IS NULL
1
SELECT INET6_ATON('oceanbase') IS NULL;
INET6_ATON('oceanbase') IS NULL
1
SELECT INET6_ATON(':::') IS NULL;
INET6_ATON(':::') IS NULL
1
SELECT INET6_ATON(':1:2:3') IS NULL;
INET6_ATON(':1:2:3') IS NULL
1
SELECT INET6_ATON('1:2:3:') IS NULL;
INET6_ATON('1:2:3:') IS NULL
1
SELECT INET6_ATON(':1::2:3') IS NULL;
INET6_ATON(':1::2:3') IS NULL
1
SELECT INET6_ATON('1::2:3:') IS NULL;
INET6_ATON('1::2:3:') IS NULL
1
SELECT INET6_ATON('::00001') IS NULL;
INET6_ATON('::00001') IS NULL
1
SELECT INET6_ATON('::00001:2') IS NULL;
INET6_ATON('::00001:2') IS NULL
1
SELECT INET6_ATON('::12345') IS NULL;
INET6_ATON('::12345') IS NULL
1
SELECT INET6_ATON('1020::3040::5060') IS NULL;
INET6_ATON('1020::3040::5060') IS NULL
1
SELECT INET6_ATON('::ABCZ') IS NULL;
INET6_ATON('::ABCZ') IS NULL
1
SELECT INET6_ATON('::0x1.2.3.4') IS NULL;
INET6_ATON('::0x1.2.3.4') IS NULL
1
SELECT INET6_ATON('::1.0x2.3.4') IS NULL;
INET6_ATON('::1.0x2.3.4') IS NULL
1
SELECT INET6_ATON('::a.b.c.d') IS NULL;
INET6_ATON('::a.b.c.d') IS NULL
1
SELECT INET6_ATON('::FFFF:0x1.2.3.4') IS NULL;
INET6_ATON('::FFFF:0x1.2.3.4') IS NULL
1
SELECT INET6_ATON('::FFFF:1.0x2.3.4') IS NULL;
INET6_ATON('::FFFF:1.0x2.3.4') IS NULL
1
SELECT INET6_ATON('::FFFF:a.b.c.d') IS NULL;
INET6_ATON('::FFFF:a.b.c.d') IS NULL
1
SELECT INET6_ATON('::1.2.3.4:ABCD') IS NULL;
INET6_ATON('::1.2.3.4:ABCD') IS NULL
1
# NOTE: such addresses are supported because getaddrinfo() supports them.
# This is just to record the current behaviour.
SELECT HEX(INET6_ATON('::ABCD:1.2.3.4'));
HEX(INET6_ATON('::ABCD:1.2.3.4'))
00000000000000000000ABCD01020304
# -- INET6_ATON: checking binary representation...
SELECT HEX(INET6_ATON('0.0.0.0'));
HEX(INET6_ATON('0.0.0.0'))
00000000
SELECT HEX(INET6_ATON('00.00.00.00'));
HEX(INET6_ATON('00.00.00.00'))
00000000
SELECT HEX(INET6_ATON('000.000.000.000'));
HEX(INET6_ATON('000.000.000.000'))
00000000
SELECT HEX(INET6_ATON('1.2.3.4'));
HEX(INET6_ATON('1.2.3.4'))
01020304
SELECT HEX(INET6_ATON('01.02.03.04'));
HEX(INET6_ATON('01.02.03.04'))
01020304
SELECT HEX(INET6_ATON('001.002.003.004'));
HEX(INET6_ATON('001.002.003.004'))
01020304
SELECT HEX(INET6_ATON('255.255.255.255'));
HEX(INET6_ATON('255.255.255.255'))
FFFFFFFF
SELECT HEX(INET6_ATON('::'));
HEX(INET6_ATON('::'))
00000000000000000000000000000000
SELECT HEX(INET6_ATON('0::0'));
HEX(INET6_ATON('0::0'))
00000000000000000000000000000000
SELECT HEX(INET6_ATON('1::2'));
HEX(INET6_ATON('1::2'))
00010000000000000000000000000002
SELECT HEX(INET6_ATON('0::'));
HEX(INET6_ATON('0::'))
00000000000000000000000000000000
SELECT HEX(INET6_ATON('1::'));
HEX(INET6_ATON('1::'))
00010000000000000000000000000000
SELECT HEX(INET6_ATON('::0'));
HEX(INET6_ATON('::0'))
00000000000000000000000000000000
SELECT HEX(INET6_ATON('::1'));
HEX(INET6_ATON('::1'))
00000000000000000000000000000001
SELECT HEX(INET6_ATON('1:2:3:4:5:6:7:8'));
HEX(INET6_ATON('1:2:3:4:5:6:7:8'))
00010002000300040005000600070008
SELECT HEX(INET6_ATON('::2:3:4:5:6:7:8'));
HEX(INET6_ATON('::2:3:4:5:6:7:8'))
00000002000300040005000600070008
SELECT HEX(INET6_ATON('1::3:4:5:6:7:8'));
HEX(INET6_ATON('1::3:4:5:6:7:8'))
00010000000300040005000600070008
SELECT HEX(INET6_ATON('1:2::4:5:6:7:8'));
HEX(INET6_ATON('1:2::4:5:6:7:8'))
00010002000000040005000600070008
SELECT HEX(INET6_ATON('1:2:3::5:6:7:8'));
HEX(INET6_ATON('1:2:3::5:6:7:8'))
00010002000300000005000600070008
SELECT HEX(INET6_ATON('1:2:3:4::6:7:8'));
HEX(INET6_ATON('1:2:3:4::6:7:8'))
00010002000300040000000600070008
SELECT HEX(INET6_ATON('1:2:3:4:5::7:8'));
HEX(INET6_ATON('1:2:3:4:5::7:8'))
00010002000300040005000000070008
SELECT HEX(INET6_ATON('1:2:3:4:5:6::8'));
HEX(INET6_ATON('1:2:3:4:5:6::8'))
00010002000300040005000600000008
SELECT HEX(INET6_ATON('1:2:3:4:5:6:7::'));
HEX(INET6_ATON('1:2:3:4:5:6:7::'))
00010002000300040005000600070000
SELECT HEX(INET6_ATON('0000:0000::0000:0001'));
HEX(INET6_ATON('0000:0000::0000:0001'))
00000000000000000000000000000001
SELECT HEX(INET6_ATON('1234:5678:9abc:def0:4321:8765:cba9:0fed'));
HEX(INET6_ATON('1234:5678:9abc:def0:4321:8765:cba9:0fed'))
123456789ABCDEF043218765CBA90FED
SELECT HEX(INET6_ATON('0000:0000:0000:0000:0000:0000:0000:0001'));
HEX(INET6_ATON('0000:0000:0000:0000:0000:0000:0000:0001'))
00000000000000000000000000000001
SELECT HEX(INET6_ATON('::C0A8:0102'));
HEX(INET6_ATON('::C0A8:0102'))
000000000000000000000000C0A80102
SELECT HEX(INET6_ATON('::c0a8:0102'));
HEX(INET6_ATON('::c0a8:0102'))
000000000000000000000000C0A80102
SELECT HEX(INET6_ATON('::192.168.1.2'));
HEX(INET6_ATON('::192.168.1.2'))
000000000000000000000000C0A80102
SELECT HEX(INET6_ATON('::FfFf:C0a8:0102'));
HEX(INET6_ATON('::FfFf:C0a8:0102'))
00000000000000000000FFFFC0A80102
SELECT HEX(INET6_ATON('::ffff:c0a8:0102'));
HEX(INET6_ATON('::ffff:c0a8:0102'))
00000000000000000000FFFFC0A80102
SELECT HEX(INET6_ATON('::ffff:192.168.1.2'));
HEX(INET6_ATON('::ffff:192.168.1.2'))
00000000000000000000FFFFC0A80102
SELECT HEX(INET6_ATON('::01.2.3.4'));
HEX(INET6_ATON('::01.2.3.4'))
00000000000000000000000001020304
SELECT HEX(INET6_ATON('::1.02.3.4'));
HEX(INET6_ATON('::1.02.3.4'))
00000000000000000000000001020304
SELECT HEX(INET6_ATON('::1.2.03.4'));
HEX(INET6_ATON('::1.2.03.4'))
00000000000000000000000001020304
SELECT HEX(INET6_ATON('::1.2.3.04'));
HEX(INET6_ATON('::1.2.3.04'))
00000000000000000000000001020304
SELECT HEX(INET6_ATON('::1.2.3.00'));
HEX(INET6_ATON('::1.2.3.00'))
00000000000000000000000001020300
SELECT HEX(INET6_ATON('::FFFF:01.2.3.4'));
HEX(INET6_ATON('::FFFF:01.2.3.4'))
00000000000000000000FFFF01020304
SELECT HEX(INET6_ATON('::FFFF:1.02.3.4'));
HEX(INET6_ATON('::FFFF:1.02.3.4'))
00000000000000000000FFFF01020304
SELECT HEX(INET6_ATON('::FFFF:1.2.03.4'));
HEX(INET6_ATON('::FFFF:1.2.03.4'))
00000000000000000000FFFF01020304
SELECT HEX(INET6_ATON('::FFFF:1.2.3.04'));
HEX(INET6_ATON('::FFFF:1.2.3.04'))
00000000000000000000FFFF01020304
SELECT HEX(INET6_ATON('::FFFF:1.2.3.00'));
HEX(INET6_ATON('::FFFF:1.2.3.00'))
00000000000000000000FFFF01020300
# -- INET6_ATON: checking the length is either 4 or 16...
SELECT LENGTH(INET6_ATON('0.0.0.0'));
LENGTH(INET6_ATON('0.0.0.0'))
4
SELECT LENGTH(INET6_ATON('255.255.255.255'));
LENGTH(INET6_ATON('255.255.255.255'))
4
SELECT LENGTH(INET6_ATON('::'));
LENGTH(INET6_ATON('::'))
16
SELECT LENGTH(INET6_ATON('1020:3040:5060:7080:90A0:B0C0:D0E0:F010'));
LENGTH(INET6_ATON('1020:3040:5060:7080:90A0:B0C0:D0E0:F010'))
16
# -- INET6_NTOA: checking NULL, invalid types, out-of range values...
SELECT INET6_NTOA(NULL);
INET6_NTOA(NULL)
NULL
SELECT INET6_NTOA(123);
INET6_NTOA(123)
NULL
SELECT INET6_NTOA(123.456);
INET6_NTOA(123.456)
NULL
SELECT INET6_NTOA(str_to_date('2014,2,28 09', '%Y,%m,%d %h'));
INET6_NTOA(str_to_date('2014,2,28 09', '%Y,%m,%d %h'))
NULL
SELECT INET6_NTOA(UNHEX('C0A801'));
INET6_NTOA(UNHEX('C0A801'))
NULL
SELECT INET6_NTOA(UNHEX('C0A80102'));
INET6_NTOA(UNHEX('C0A80102'))
192.168.1.2
SELECT INET6_NTOA(UNHEX('C0A8010203'));
INET6_NTOA(UNHEX('C0A8010203'))
NULL
SELECT INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F'));
INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F'))
NULL
SELECT INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F10'));
INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F10'))
102:304:506:708:90a:b0c:d0e:f10
SELECT INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F1011'));
INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F1011'))
NULL
SELECT INET6_NTOA('1234'), INET6_NTOA(BINARY('1234'));
INET6_NTOA('1234') INET6_NTOA(BINARY('1234'))
NULL 49.50.51.52
SELECT INET6_NTOA('0123456789abcdef'), INET6_NTOA(BINARY('0123456789abcdef'));
INET6_NTOA('0123456789abcdef') INET6_NTOA(BINARY('0123456789abcdef'))
NULL 3031:3233:3435:3637:3839:6162:6364:6566
# -- Checking double-conversion...
SELECT INET6_NTOA(INET6_ATON('::'));
INET6_NTOA(INET6_ATON('::'))
::
SELECT INET6_NTOA(INET6_ATON('0::0'));
INET6_NTOA(INET6_ATON('0::0'))
::
SELECT INET6_NTOA(INET6_ATON('1::2'));
INET6_NTOA(INET6_ATON('1::2'))
1::2
SELECT INET6_NTOA(INET6_ATON('0::'));
INET6_NTOA(INET6_ATON('0::'))
::
SELECT INET6_NTOA(INET6_ATON('1::'));
INET6_NTOA(INET6_ATON('1::'))
1::
SELECT INET6_NTOA(INET6_ATON('::0'));
INET6_NTOA(INET6_ATON('::0'))
::
SELECT INET6_NTOA(INET6_ATON('::1'));
INET6_NTOA(INET6_ATON('::1'))
::1
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7:8'));
INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7:8'))
1:2:3:4:5:6:7:8
SELECT INET6_NTOA(INET6_ATON('::2:3:4:5:6:7:8'));
INET6_NTOA(INET6_ATON('::2:3:4:5:6:7:8'))
::2:3:4:5:6:7:8
SELECT INET6_NTOA(INET6_ATON('1::3:4:5:6:7:8'));
INET6_NTOA(INET6_ATON('1::3:4:5:6:7:8'))
1::3:4:5:6:7:8
SELECT INET6_NTOA(INET6_ATON('1:2::4:5:6:7:8'));
INET6_NTOA(INET6_ATON('1:2::4:5:6:7:8'))
1:2::4:5:6:7:8
SELECT INET6_NTOA(INET6_ATON('1:2:3::5:6:7:8'));
INET6_NTOA(INET6_ATON('1:2:3::5:6:7:8'))
1:2:3::5:6:7:8
SELECT INET6_NTOA(INET6_ATON('1:2:3:4::6:7:8'));
INET6_NTOA(INET6_ATON('1:2:3:4::6:7:8'))
1:2:3:4::6:7:8
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5::7:8'));
INET6_NTOA(INET6_ATON('1:2:3:4:5::7:8'))
1:2:3:4:5::7:8
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6::8'));
INET6_NTOA(INET6_ATON('1:2:3:4:5:6::8'))
1:2:3:4:5:6::8
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7::'));
INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7::'))
1:2:3:4:5:6:7::
SELECT INET6_NTOA(INET6_ATON('0000:0000::0000:0001'));
INET6_NTOA(INET6_ATON('0000:0000::0000:0001'))
::1
SELECT INET6_NTOA(INET6_ATON('1234:5678:9abc:def0:4321:8765:cba9:0fed'));
INET6_NTOA(INET6_ATON('1234:5678:9abc:def0:4321:8765:cba9:0fed'))
1234:5678:9abc:def0:4321:8765:cba9:fed
SELECT INET6_NTOA(INET6_ATON('0000:0000:0000:0000:0000:0000:0000:0001'));
INET6_NTOA(INET6_ATON('0000:0000:0000:0000:0000:0000:0000:0001'))
::1
SELECT INET6_NTOA(INET6_ATON('0000:0000:0000:0000:1:1:255.255.255.255'));
INET6_NTOA(INET6_ATON('0000:0000:0000:0000:1:1:255.255.255.255'))
::1:1:ffff:ffff
SELECT INET6_NTOA(INET6_ATON('0000:0000:0000:0000:0001:0001:255.255.255.255'));
INET6_NTOA(INET6_ATON('0000:0000:0000:0000:0001:0001:255.255.255.255'))
::1:1:ffff:ffff
SELECT INET6_NTOA(INET6_ATON('::C0A8:0102'));
INET6_NTOA(INET6_ATON('::C0A8:0102'))
::192.168.1.2
SELECT INET6_NTOA(INET6_ATON('::c0a8:0102'));
INET6_NTOA(INET6_ATON('::c0a8:0102'))
::192.168.1.2
SELECT INET6_NTOA(INET6_ATON('::192.168.1.2'));
INET6_NTOA(INET6_ATON('::192.168.1.2'))
::192.168.1.2
SELECT INET6_NTOA(INET6_ATON('::FfFf:C0a8:0102'));
INET6_NTOA(INET6_ATON('::FfFf:C0a8:0102'))
::ffff:192.168.1.2
SELECT INET6_NTOA(INET6_ATON('::ffff:c0a8:0102'));
INET6_NTOA(INET6_ATON('::ffff:c0a8:0102'))
::ffff:192.168.1.2
SELECT INET6_NTOA(INET6_ATON('::ffff:192.168.1.2'));
INET6_NTOA(INET6_ATON('::ffff:192.168.1.2'))
::ffff:192.168.1.2
SELECT INET6_NTOA(INET6_ATON('::01.2.3.4'));
INET6_NTOA(INET6_ATON('::01.2.3.4'))
::1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::1.02.3.4'));
INET6_NTOA(INET6_ATON('::1.02.3.4'))
::1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::1.2.03.4'));
INET6_NTOA(INET6_ATON('::1.2.03.4'))
::1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::1.2.3.04'));
INET6_NTOA(INET6_ATON('::1.2.3.04'))
::1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::1.2.3.00'));
INET6_NTOA(INET6_ATON('::1.2.3.00'))
::1.2.3.0
SELECT INET6_NTOA(INET6_ATON('::FFFF:01.2.3.4'));
INET6_NTOA(INET6_ATON('::FFFF:01.2.3.4'))
::ffff:1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.02.3.4'));
INET6_NTOA(INET6_ATON('::FFFF:1.02.3.4'))
::ffff:1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.2.03.4'));
INET6_NTOA(INET6_ATON('::FFFF:1.2.03.4'))
::ffff:1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.2.3.04'));
INET6_NTOA(INET6_ATON('::FFFF:1.2.3.04'))
::ffff:1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.2.3.00'));
INET6_NTOA(INET6_ATON('::FFFF:1.2.3.00'))
::ffff:1.2.3.0
# -- Comparing INET_ATON() and INET6_ATON()...
SELECT HEX(INET_ATON('192.168.1.2'));
HEX(INET_ATON('192.168.1.2'))
C0A80102
SELECT HEX(INET6_ATON('192.168.1.2'));
HEX(INET6_ATON('192.168.1.2'))
C0A80102
SELECT HEX(INET_ATON('255.255.255.255'));
HEX(INET_ATON('255.255.255.255'))
FFFFFFFF
SELECT HEX(INET6_ATON('255.255.255.255'));
HEX(INET6_ATON('255.255.255.255'))
FFFFFFFF
SELECT HEX(INET_ATON('192.168.08.2'));
HEX(INET_ATON('192.168.08.2'))
C0A80802
SELECT HEX(INET6_ATON('192.168.08.2'));
HEX(INET6_ATON('192.168.08.2'))
C0A80802
SELECT HEX(INET_ATON('192.168.0x8.2'));
HEX(INET_ATON('192.168.0x8.2'))
NULL
SELECT HEX(INET6_ATON('192.168.0x8.2'));
HEX(INET6_ATON('192.168.0x8.2'))
NULL
SELECT HEX(INET_ATON('1.2.255'));
HEX(INET_ATON('1.2.255'))
10200FF
SELECT HEX(INET6_ATON('1.2.255'));
HEX(INET6_ATON('1.2.255'))
NULL
SELECT HEX(INET_ATON('1.2.256'));
HEX(INET_ATON('1.2.256'))
NULL
SELECT HEX(INET6_ATON('1.2.256'));
HEX(INET6_ATON('1.2.256'))
NULL
SELECT HEX(INET_ATON('1.0002.3.4'));
HEX(INET_ATON('1.0002.3.4'))
1020304
SELECT HEX(INET6_ATON('1.0002.3.4'));
HEX(INET6_ATON('1.0002.3.4'))
NULL
SELECT HEX(INET_ATON('1.2.3.4.5'));
HEX(INET_ATON('1.2.3.4.5'))
NULL
SELECT HEX(INET6_ATON('1.2.3.4.5'));
HEX(INET6_ATON('1.2.3.4.5'))
NULL
# -- Checking mix of INET- and INET6- functions...
SELECT HEX(INET6_ATON(INET_NTOA(INET_ATON('1.2.3.4')))) AS x;
x
01020304
# -- Checking IS_IPV4() / IS_IPV6()...
SELECT IS_IPV4(NULL);
IS_IPV4(NULL)
0
SELECT IS_IPV4(1);
IS_IPV4(1)
0
SELECT IS_IPV4(1.0);
IS_IPV4(1.0)
0
SELECT IS_IPV4('1.2.3.4');
IS_IPV4('1.2.3.4')
1
SELECT IS_IPV4('001.02.000.255');
IS_IPV4('001.02.000.255')
1
SELECT IS_IPV4('::1.2.0.255');
IS_IPV4('::1.2.0.255')
0
SELECT IS_IPV4('::1');
IS_IPV4('::1')
0
SELECT IS_IPV4(BINARY('1.2.3.4'));
IS_IPV4(BINARY('1.2.3.4'))
1
SELECT IS_IPV6(NULL);
IS_IPV6(NULL)
0
SELECT IS_IPV6(1);
IS_IPV6(1)
0
SELECT IS_IPV6(1.0);
IS_IPV6(1.0)
0
SELECT IS_IPV6('1.2.3.4');
IS_IPV6('1.2.3.4')
0
SELECT IS_IPV6('001.02.000.255');
IS_IPV6('001.02.000.255')
0
SELECT IS_IPV6('::001.02.000.255');
IS_IPV6('::001.02.000.255')
1
SELECT IS_IPV6('::1.2.0.255');
IS_IPV6('::1.2.0.255')
1
SELECT IS_IPV6('::1');
IS_IPV6('::1')
1
SELECT IS_IPV6('0000:0000:0000:0000:0000:0000:0000:0001');
IS_IPV6('0000:0000:0000:0000:0000:0000:0000:0001')
1
SELECT IS_IPV6(BINARY('0000:0000:0000:0000:0000:0000:0000:0001'));
IS_IPV6(BINARY('0000:0000:0000:0000:0000:0000:0000:0001'))
1
# -- Checking IS_IPV4_MAPPED() and IS_IPV4_COMPAT()...
SELECT IS_IPV4_MAPPED(NULL), IS_IPV4_COMPAT(NULL);
IS_IPV4_MAPPED(NULL) IS_IPV4_COMPAT(NULL)
0 0
SELECT IS_IPV4_MAPPED(INET6_ATON('1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('1.2.3.4'));
IS_IPV4_MAPPED(INET6_ATON('1.2.3.4')) IS_IPV4_COMPAT(INET6_ATON('1.2.3.4'))
0 0
SELECT IS_IPV4_MAPPED(INET6_ATON('::1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('::1.2.3.4'));
IS_IPV4_MAPPED(INET6_ATON('::1.2.3.4')) IS_IPV4_COMPAT(INET6_ATON('::1.2.3.4'))
0 1
SELECT IS_IPV4_MAPPED(INET6_ATON('::FFFF:1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('::FFFF:1.2.3.4'));
IS_IPV4_MAPPED(INET6_ATON('::FFFF:1.2.3.4')) IS_IPV4_COMPAT(INET6_ATON('::FFFF:1.2.3.4'))
1 0
SELECT IS_IPV4_MAPPED(INET6_ATON('::ABCD:1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('::ABCD:1.2.3.4'));
IS_IPV4_MAPPED(INET6_ATON('::ABCD:1.2.3.4')) IS_IPV4_COMPAT(INET6_ATON('::ABCD:1.2.3.4'))
0 0
SELECT IS_IPV4_MAPPED(INET6_ATON('::1')),
IS_IPV4_COMPAT(INET6_ATON('::1'));
IS_IPV4_MAPPED(INET6_ATON('::1')) IS_IPV4_COMPAT(INET6_ATON('::1'))
0 0
SELECT IS_IPV4_MAPPED(INET6_ATON('::')),
IS_IPV4_COMPAT(INET6_ATON('::'));
IS_IPV4_MAPPED(INET6_ATON('::')) IS_IPV4_COMPAT(INET6_ATON('::'))
0 0
# -- Checking IS_IPV4_COMPAT()...
# -- Working with a table...
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
CREATE TABLE t1(ip INT UNSIGNED);
CREATE TABLE t2(ip VARBINARY(16));
INSERT INTO t1 VALUES
(INET_ATON('1.2.3.4')), (INET_ATON('255.255.255.255'));
SELECT INET_NTOA(ip) FROM t1;
INET_NTOA(ip)
1.2.3.4
255.255.255.255
INSERT INTO t2 VALUES
(INET6_ATON('1.2.3.4')), (INET6_ATON('255.255.255.255')),
(INET6_ATON('::1.2.3.4')), (INET6_ATON('::ffff:255.255.255.255')),
(INET6_ATON('::')), (INET6_ATON('::1')),
(INET6_ATON('1020:3040:5060:7080:90A0:B0C0:D0E0:F010'));
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
INET6_NTOA(ip) HEX(ip) LENGTH(ip)
1.2.3.4 01020304 4
255.255.255.255 FFFFFFFF 4
::1.2.3.4 00000000000000000000000001020304 16
::ffff:255.255.255.255 00000000000000000000FFFFFFFFFFFF 16
:: 00000000000000000000000000000000 16
::1 00000000000000000000000000000001 16
1020:3040:5060:7080:90a0:b0c0:d0e0:f010 102030405060708090A0B0C0D0E0F010 16
# test different sql mode
set sql_mode = STRICT_TRANS_TABLES;
select @@sql_mode;
@@sql_mode
STRICT_TRANS_TABLES
DELETE FROM t2;
INSERT INTO t2 VALUES (INET6_ATON('1.0002.3.4'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (INET6_ATON('1.2.255'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (INET6_ATON('1.2.256'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (INET6_ATON('192.168.0x8.2'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (inet_aton("122.256"));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (inet_aton("122.226."));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (inet_aton(""));
ERROR HY000: Invalid argument
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
INET6_NTOA(ip) HEX(ip) LENGTH(ip)
SELECT INET6_ATON('1.0002.3.4');
INET6_ATON('1.0002.3.4')
NULL
SELECT INET6_ATON("");
INET6_ATON("")
NULL
set sql_mode = STRICT_ALL_TABLES;
select @@sql_mode;
@@sql_mode
STRICT_ALL_TABLES
DELETE FROM t2;
INSERT INTO t2 VALUES (INET6_ATON('1.0002.3.4'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (INET6_ATON('1.2.255'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (INET6_ATON('1.2.256'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (INET6_ATON('192.168.0x8.2'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (inet_aton("122.256"));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (inet_aton("122.226."));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (inet_aton(""));
ERROR HY000: Invalid argument
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
INET6_NTOA(ip) HEX(ip) LENGTH(ip)
SELECT INET6_ATON('1.0002.3.4');
INET6_ATON('1.0002.3.4')
NULL
SELECT INET6_ATON("");
INET6_ATON("")
NULL
set sql_mode = '';
select @@sql_mode;
@@sql_mode
DELETE FROM t2;
INSERT INTO t2 VALUES (INET6_ATON('1.0002.3.4'));
INSERT INTO t2 VALUES (INET6_ATON('1.2.255'));
INSERT INTO t2 VALUES (INET6_ATON('1.2.256'));
INSERT INTO t2 VALUES (INET6_ATON('192.168.0x8.2'));
INSERT INTO t2 VALUES (inet_aton("122.256"));
INSERT INTO t2 VALUES (inet_aton("122.226."));
INSERT INTO t2 VALUES (inet_aton(""));
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
INET6_NTOA(ip) HEX(ip) LENGTH(ip)
NULL NULL NULL
NULL NULL NULL
NULL NULL NULL
NULL NULL NULL
NULL NULL NULL
NULL NULL NULL
NULL NULL NULL
SELECT INET6_ATON('1.0002.3.4');
INET6_ATON('1.0002.3.4')
NULL
SELECT INET6_ATON("");
INET6_ATON("")
NULL
DROP TABLE t1;
DROP TABLE t2;
# -- Done misc test.
#
# End of tests
#
connect sys_jianhua, $OBMYSQL_MS0,root@sys,,oceanbase,$OBMYSQL_PORT;
connection sys_jianhua;
alter system set _enable_static_typing_engine = false;
connect mysql_jianhua, $OBMYSQL_MS0,root@mysql,'',test,$OBMYSQL_PORT;
connection mysql_jianhua;
drop table if exists t1,t2;
select inet_ntoa(null),inet_aton(null);
inet_ntoa(null) inet_aton(null)
NULL NULL
select inet_ntoa(inet_aton("255.255.255.255.255.255.255.255"));
inet_ntoa(inet_aton("255.255.255.255.255.255.255.255"))
NULL
select inet_aton("255.255.255.255.255"),inet_aton("255.255.1.255"),inet_aton("0.1.255");
inet_aton("255.255.255.255.255") inet_aton("255.255.1.255") inet_aton("0.1.255")
NULL 4294902271 65791
select inet_ntoa(1099511627775),inet_ntoa(4294902271),inet_ntoa(511);
inet_ntoa(1099511627775) inet_ntoa(4294902271) inet_ntoa(511)
NULL 255.255.1.255 0.0.1.255
select inet_aton("0.255.255.255.255");
inet_aton("0.255.255.255.255")
NULL
select inet_aton("255.255.255.0000");
inet_aton("255.255.255.0000")
4294967040
select hex(inet_aton('127'));
hex(inet_aton('127'))
7F
select hex(inet_aton('127.1'));
hex(inet_aton('127.1'))
7F000001
select hex(inet_aton('127.1.1'));
hex(inet_aton('127.1.1'))
7F010001
select inet_aton("122.256");
inet_aton("122.256")
NULL
select inet_aton("122.226.");
inet_aton("122.226.")
NULL
select inet_aton("");
inet_aton("")
NULL
# -- INET6_ATON: checking NULL, invalid types, out-of range values...
SELECT INET6_ATON(NULL) IS NULL;
INET6_ATON(NULL) IS NULL
1
SELECT INET6_ATON(123) IS NULL;
INET6_ATON(123) IS NULL
1
SELECT INET6_ATON(123.45) IS NULL;
INET6_ATON(123.45) IS NULL
1
SELECT INET6_ATON(str_to_date('2014,2,28 09', '%Y,%m,%d %h')) IS NULL;
INET6_ATON(str_to_date('2014,2,28 09', '%Y,%m,%d %h')) IS NULL
1
SELECT INET6_ATON('1.2.3') IS NULL;
INET6_ATON('1.2.3') IS NULL
1
SELECT INET6_ATON('1.2.3.') IS NULL;
INET6_ATON('1.2.3.') IS NULL
1
SELECT INET6_ATON('1..3.4') IS NULL;
INET6_ATON('1..3.4') IS NULL
1
SELECT INET6_ATON('-1.2.3.4') IS NULL;
INET6_ATON('-1.2.3.4') IS NULL
1
SELECT INET6_ATON('1.2.3.256') IS NULL;
INET6_ATON('1.2.3.256') IS NULL
1
SELECT INET6_ATON('1.2.3.4.5') IS NULL;
INET6_ATON('1.2.3.4.5') IS NULL
1
SELECT INET6_ATON('0001.2.3.4') IS NULL;
INET6_ATON('0001.2.3.4') IS NULL
1
SELECT INET6_ATON('0x1.2.3.4') IS NULL;
INET6_ATON('0x1.2.3.4') IS NULL
1
SELECT INET6_ATON('a.2.3.4') IS NULL;
INET6_ATON('a.2.3.4') IS NULL
1
SELECT INET6_ATON('1.2.3.4:80') IS NULL;
INET6_ATON('1.2.3.4:80') IS NULL
1
SELECT INET6_ATON('1.2.3.4/32') IS NULL;
INET6_ATON('1.2.3.4/32') IS NULL
1
SELECT INET6_ATON('oceanbase') IS NULL;
INET6_ATON('oceanbase') IS NULL
1
SELECT INET6_ATON(':::') IS NULL;
INET6_ATON(':::') IS NULL
1
SELECT INET6_ATON(':1:2:3') IS NULL;
INET6_ATON(':1:2:3') IS NULL
1
SELECT INET6_ATON('1:2:3:') IS NULL;
INET6_ATON('1:2:3:') IS NULL
1
SELECT INET6_ATON(':1::2:3') IS NULL;
INET6_ATON(':1::2:3') IS NULL
1
SELECT INET6_ATON('1::2:3:') IS NULL;
INET6_ATON('1::2:3:') IS NULL
1
SELECT INET6_ATON('::00001') IS NULL;
INET6_ATON('::00001') IS NULL
1
SELECT INET6_ATON('::00001:2') IS NULL;
INET6_ATON('::00001:2') IS NULL
1
SELECT INET6_ATON('::12345') IS NULL;
INET6_ATON('::12345') IS NULL
1
SELECT INET6_ATON('1020::3040::5060') IS NULL;
INET6_ATON('1020::3040::5060') IS NULL
1
SELECT INET6_ATON('::ABCZ') IS NULL;
INET6_ATON('::ABCZ') IS NULL
1
SELECT INET6_ATON('::0x1.2.3.4') IS NULL;
INET6_ATON('::0x1.2.3.4') IS NULL
1
SELECT INET6_ATON('::1.0x2.3.4') IS NULL;
INET6_ATON('::1.0x2.3.4') IS NULL
1
SELECT INET6_ATON('::a.b.c.d') IS NULL;
INET6_ATON('::a.b.c.d') IS NULL
1
SELECT INET6_ATON('::FFFF:0x1.2.3.4') IS NULL;
INET6_ATON('::FFFF:0x1.2.3.4') IS NULL
1
SELECT INET6_ATON('::FFFF:1.0x2.3.4') IS NULL;
INET6_ATON('::FFFF:1.0x2.3.4') IS NULL
1
SELECT INET6_ATON('::FFFF:a.b.c.d') IS NULL;
INET6_ATON('::FFFF:a.b.c.d') IS NULL
1
SELECT INET6_ATON('::1.2.3.4:ABCD') IS NULL;
INET6_ATON('::1.2.3.4:ABCD') IS NULL
1
# NOTE: such addresses are supported because getaddrinfo() supports them.
# This is just to record the current behaviour.
SELECT HEX(INET6_ATON('::ABCD:1.2.3.4'));
HEX(INET6_ATON('::ABCD:1.2.3.4'))
00000000000000000000ABCD01020304
# -- INET6_ATON: checking binary representation...
SELECT HEX(INET6_ATON('0.0.0.0'));
HEX(INET6_ATON('0.0.0.0'))
00000000
SELECT HEX(INET6_ATON('00.00.00.00'));
HEX(INET6_ATON('00.00.00.00'))
00000000
SELECT HEX(INET6_ATON('000.000.000.000'));
HEX(INET6_ATON('000.000.000.000'))
00000000
SELECT HEX(INET6_ATON('1.2.3.4'));
HEX(INET6_ATON('1.2.3.4'))
01020304
SELECT HEX(INET6_ATON('01.02.03.04'));
HEX(INET6_ATON('01.02.03.04'))
01020304
SELECT HEX(INET6_ATON('001.002.003.004'));
HEX(INET6_ATON('001.002.003.004'))
01020304
SELECT HEX(INET6_ATON('255.255.255.255'));
HEX(INET6_ATON('255.255.255.255'))
FFFFFFFF
SELECT HEX(INET6_ATON('::'));
HEX(INET6_ATON('::'))
00000000000000000000000000000000
SELECT HEX(INET6_ATON('0::0'));
HEX(INET6_ATON('0::0'))
00000000000000000000000000000000
SELECT HEX(INET6_ATON('1::2'));
HEX(INET6_ATON('1::2'))
00010000000000000000000000000002
SELECT HEX(INET6_ATON('0::'));
HEX(INET6_ATON('0::'))
00000000000000000000000000000000
SELECT HEX(INET6_ATON('1::'));
HEX(INET6_ATON('1::'))
00010000000000000000000000000000
SELECT HEX(INET6_ATON('::0'));
HEX(INET6_ATON('::0'))
00000000000000000000000000000000
SELECT HEX(INET6_ATON('::1'));
HEX(INET6_ATON('::1'))
00000000000000000000000000000001
SELECT HEX(INET6_ATON('1:2:3:4:5:6:7:8'));
HEX(INET6_ATON('1:2:3:4:5:6:7:8'))
00010002000300040005000600070008
SELECT HEX(INET6_ATON('::2:3:4:5:6:7:8'));
HEX(INET6_ATON('::2:3:4:5:6:7:8'))
00000002000300040005000600070008
SELECT HEX(INET6_ATON('1::3:4:5:6:7:8'));
HEX(INET6_ATON('1::3:4:5:6:7:8'))
00010000000300040005000600070008
SELECT HEX(INET6_ATON('1:2::4:5:6:7:8'));
HEX(INET6_ATON('1:2::4:5:6:7:8'))
00010002000000040005000600070008
SELECT HEX(INET6_ATON('1:2:3::5:6:7:8'));
HEX(INET6_ATON('1:2:3::5:6:7:8'))
00010002000300000005000600070008
SELECT HEX(INET6_ATON('1:2:3:4::6:7:8'));
HEX(INET6_ATON('1:2:3:4::6:7:8'))
00010002000300040000000600070008
SELECT HEX(INET6_ATON('1:2:3:4:5::7:8'));
HEX(INET6_ATON('1:2:3:4:5::7:8'))
00010002000300040005000000070008
SELECT HEX(INET6_ATON('1:2:3:4:5:6::8'));
HEX(INET6_ATON('1:2:3:4:5:6::8'))
00010002000300040005000600000008
SELECT HEX(INET6_ATON('1:2:3:4:5:6:7::'));
HEX(INET6_ATON('1:2:3:4:5:6:7::'))
00010002000300040005000600070000
SELECT HEX(INET6_ATON('0000:0000::0000:0001'));
HEX(INET6_ATON('0000:0000::0000:0001'))
00000000000000000000000000000001
SELECT HEX(INET6_ATON('1234:5678:9abc:def0:4321:8765:cba9:0fed'));
HEX(INET6_ATON('1234:5678:9abc:def0:4321:8765:cba9:0fed'))
123456789ABCDEF043218765CBA90FED
SELECT HEX(INET6_ATON('0000:0000:0000:0000:0000:0000:0000:0001'));
HEX(INET6_ATON('0000:0000:0000:0000:0000:0000:0000:0001'))
00000000000000000000000000000001
SELECT HEX(INET6_ATON('::C0A8:0102'));
HEX(INET6_ATON('::C0A8:0102'))
000000000000000000000000C0A80102
SELECT HEX(INET6_ATON('::c0a8:0102'));
HEX(INET6_ATON('::c0a8:0102'))
000000000000000000000000C0A80102
SELECT HEX(INET6_ATON('::192.168.1.2'));
HEX(INET6_ATON('::192.168.1.2'))
000000000000000000000000C0A80102
SELECT HEX(INET6_ATON('::FfFf:C0a8:0102'));
HEX(INET6_ATON('::FfFf:C0a8:0102'))
00000000000000000000FFFFC0A80102
SELECT HEX(INET6_ATON('::ffff:c0a8:0102'));
HEX(INET6_ATON('::ffff:c0a8:0102'))
00000000000000000000FFFFC0A80102
SELECT HEX(INET6_ATON('::ffff:192.168.1.2'));
HEX(INET6_ATON('::ffff:192.168.1.2'))
00000000000000000000FFFFC0A80102
SELECT HEX(INET6_ATON('::01.2.3.4'));
HEX(INET6_ATON('::01.2.3.4'))
00000000000000000000000001020304
SELECT HEX(INET6_ATON('::1.02.3.4'));
HEX(INET6_ATON('::1.02.3.4'))
00000000000000000000000001020304
SELECT HEX(INET6_ATON('::1.2.03.4'));
HEX(INET6_ATON('::1.2.03.4'))
00000000000000000000000001020304
SELECT HEX(INET6_ATON('::1.2.3.04'));
HEX(INET6_ATON('::1.2.3.04'))
00000000000000000000000001020304
SELECT HEX(INET6_ATON('::1.2.3.00'));
HEX(INET6_ATON('::1.2.3.00'))
00000000000000000000000001020300
SELECT HEX(INET6_ATON('::FFFF:01.2.3.4'));
HEX(INET6_ATON('::FFFF:01.2.3.4'))
00000000000000000000FFFF01020304
SELECT HEX(INET6_ATON('::FFFF:1.02.3.4'));
HEX(INET6_ATON('::FFFF:1.02.3.4'))
00000000000000000000FFFF01020304
SELECT HEX(INET6_ATON('::FFFF:1.2.03.4'));
HEX(INET6_ATON('::FFFF:1.2.03.4'))
00000000000000000000FFFF01020304
SELECT HEX(INET6_ATON('::FFFF:1.2.3.04'));
HEX(INET6_ATON('::FFFF:1.2.3.04'))
00000000000000000000FFFF01020304
SELECT HEX(INET6_ATON('::FFFF:1.2.3.00'));
HEX(INET6_ATON('::FFFF:1.2.3.00'))
00000000000000000000FFFF01020300
# -- INET6_ATON: checking the length is either 4 or 16...
SELECT LENGTH(INET6_ATON('0.0.0.0'));
LENGTH(INET6_ATON('0.0.0.0'))
4
SELECT LENGTH(INET6_ATON('255.255.255.255'));
LENGTH(INET6_ATON('255.255.255.255'))
4
SELECT LENGTH(INET6_ATON('::'));
LENGTH(INET6_ATON('::'))
16
SELECT LENGTH(INET6_ATON('1020:3040:5060:7080:90A0:B0C0:D0E0:F010'));
LENGTH(INET6_ATON('1020:3040:5060:7080:90A0:B0C0:D0E0:F010'))
16
# -- INET6_NTOA: checking NULL, invalid types, out-of range values...
SELECT INET6_NTOA(NULL);
INET6_NTOA(NULL)
NULL
SELECT INET6_NTOA(123);
INET6_NTOA(123)
NULL
SELECT INET6_NTOA(123.456);
INET6_NTOA(123.456)
NULL
SELECT INET6_NTOA(str_to_date('2014,2,28 09', '%Y,%m,%d %h'));
INET6_NTOA(str_to_date('2014,2,28 09', '%Y,%m,%d %h'))
NULL
SELECT INET6_NTOA(UNHEX('C0A801'));
INET6_NTOA(UNHEX('C0A801'))
NULL
SELECT INET6_NTOA(UNHEX('C0A80102'));
INET6_NTOA(UNHEX('C0A80102'))
192.168.1.2
SELECT INET6_NTOA(UNHEX('C0A8010203'));
INET6_NTOA(UNHEX('C0A8010203'))
NULL
SELECT INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F'));
INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F'))
NULL
SELECT INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F10'));
INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F10'))
102:304:506:708:90a:b0c:d0e:f10
SELECT INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F1011'));
INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F1011'))
NULL
SELECT INET6_NTOA('1234'), INET6_NTOA(BINARY('1234'));
INET6_NTOA('1234') INET6_NTOA(BINARY('1234'))
NULL 49.50.51.52
SELECT INET6_NTOA('0123456789abcdef'), INET6_NTOA(BINARY('0123456789abcdef'));
INET6_NTOA('0123456789abcdef') INET6_NTOA(BINARY('0123456789abcdef'))
NULL 3031:3233:3435:3637:3839:6162:6364:6566
# -- Checking double-conversion...
SELECT INET6_NTOA(INET6_ATON('::'));
INET6_NTOA(INET6_ATON('::'))
::
SELECT INET6_NTOA(INET6_ATON('0::0'));
INET6_NTOA(INET6_ATON('0::0'))
::
SELECT INET6_NTOA(INET6_ATON('1::2'));
INET6_NTOA(INET6_ATON('1::2'))
1::2
SELECT INET6_NTOA(INET6_ATON('0::'));
INET6_NTOA(INET6_ATON('0::'))
::
SELECT INET6_NTOA(INET6_ATON('1::'));
INET6_NTOA(INET6_ATON('1::'))
1::
SELECT INET6_NTOA(INET6_ATON('::0'));
INET6_NTOA(INET6_ATON('::0'))
::
SELECT INET6_NTOA(INET6_ATON('::1'));
INET6_NTOA(INET6_ATON('::1'))
::1
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7:8'));
INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7:8'))
1:2:3:4:5:6:7:8
SELECT INET6_NTOA(INET6_ATON('::2:3:4:5:6:7:8'));
INET6_NTOA(INET6_ATON('::2:3:4:5:6:7:8'))
::2:3:4:5:6:7:8
SELECT INET6_NTOA(INET6_ATON('1::3:4:5:6:7:8'));
INET6_NTOA(INET6_ATON('1::3:4:5:6:7:8'))
1::3:4:5:6:7:8
SELECT INET6_NTOA(INET6_ATON('1:2::4:5:6:7:8'));
INET6_NTOA(INET6_ATON('1:2::4:5:6:7:8'))
1:2::4:5:6:7:8
SELECT INET6_NTOA(INET6_ATON('1:2:3::5:6:7:8'));
INET6_NTOA(INET6_ATON('1:2:3::5:6:7:8'))
1:2:3::5:6:7:8
SELECT INET6_NTOA(INET6_ATON('1:2:3:4::6:7:8'));
INET6_NTOA(INET6_ATON('1:2:3:4::6:7:8'))
1:2:3:4::6:7:8
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5::7:8'));
INET6_NTOA(INET6_ATON('1:2:3:4:5::7:8'))
1:2:3:4:5::7:8
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6::8'));
INET6_NTOA(INET6_ATON('1:2:3:4:5:6::8'))
1:2:3:4:5:6::8
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7::'));
INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7::'))
1:2:3:4:5:6:7::
SELECT INET6_NTOA(INET6_ATON('0000:0000::0000:0001'));
INET6_NTOA(INET6_ATON('0000:0000::0000:0001'))
::1
SELECT INET6_NTOA(INET6_ATON('1234:5678:9abc:def0:4321:8765:cba9:0fed'));
INET6_NTOA(INET6_ATON('1234:5678:9abc:def0:4321:8765:cba9:0fed'))
1234:5678:9abc:def0:4321:8765:cba9:fed
SELECT INET6_NTOA(INET6_ATON('0000:0000:0000:0000:0000:0000:0000:0001'));
INET6_NTOA(INET6_ATON('0000:0000:0000:0000:0000:0000:0000:0001'))
::1
SELECT INET6_NTOA(INET6_ATON('0000:0000:0000:0000:1:1:255.255.255.255'));
INET6_NTOA(INET6_ATON('0000:0000:0000:0000:1:1:255.255.255.255'))
::1:1:ffff:ffff
SELECT INET6_NTOA(INET6_ATON('0000:0000:0000:0000:0001:0001:255.255.255.255'));
INET6_NTOA(INET6_ATON('0000:0000:0000:0000:0001:0001:255.255.255.255'))
::1:1:ffff:ffff
SELECT INET6_NTOA(INET6_ATON('::C0A8:0102'));
INET6_NTOA(INET6_ATON('::C0A8:0102'))
::192.168.1.2
SELECT INET6_NTOA(INET6_ATON('::c0a8:0102'));
INET6_NTOA(INET6_ATON('::c0a8:0102'))
::192.168.1.2
SELECT INET6_NTOA(INET6_ATON('::192.168.1.2'));
INET6_NTOA(INET6_ATON('::192.168.1.2'))
::192.168.1.2
SELECT INET6_NTOA(INET6_ATON('::FfFf:C0a8:0102'));
INET6_NTOA(INET6_ATON('::FfFf:C0a8:0102'))
::ffff:192.168.1.2
SELECT INET6_NTOA(INET6_ATON('::ffff:c0a8:0102'));
INET6_NTOA(INET6_ATON('::ffff:c0a8:0102'))
::ffff:192.168.1.2
SELECT INET6_NTOA(INET6_ATON('::ffff:192.168.1.2'));
INET6_NTOA(INET6_ATON('::ffff:192.168.1.2'))
::ffff:192.168.1.2
SELECT INET6_NTOA(INET6_ATON('::01.2.3.4'));
INET6_NTOA(INET6_ATON('::01.2.3.4'))
::1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::1.02.3.4'));
INET6_NTOA(INET6_ATON('::1.02.3.4'))
::1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::1.2.03.4'));
INET6_NTOA(INET6_ATON('::1.2.03.4'))
::1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::1.2.3.04'));
INET6_NTOA(INET6_ATON('::1.2.3.04'))
::1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::1.2.3.00'));
INET6_NTOA(INET6_ATON('::1.2.3.00'))
::1.2.3.0
SELECT INET6_NTOA(INET6_ATON('::FFFF:01.2.3.4'));
INET6_NTOA(INET6_ATON('::FFFF:01.2.3.4'))
::ffff:1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.02.3.4'));
INET6_NTOA(INET6_ATON('::FFFF:1.02.3.4'))
::ffff:1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.2.03.4'));
INET6_NTOA(INET6_ATON('::FFFF:1.2.03.4'))
::ffff:1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.2.3.04'));
INET6_NTOA(INET6_ATON('::FFFF:1.2.3.04'))
::ffff:1.2.3.4
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.2.3.00'));
INET6_NTOA(INET6_ATON('::FFFF:1.2.3.00'))
::ffff:1.2.3.0
# -- Comparing INET_ATON() and INET6_ATON()...
SELECT HEX(INET_ATON('192.168.1.2'));
HEX(INET_ATON('192.168.1.2'))
C0A80102
SELECT HEX(INET6_ATON('192.168.1.2'));
HEX(INET6_ATON('192.168.1.2'))
C0A80102
SELECT HEX(INET_ATON('255.255.255.255'));
HEX(INET_ATON('255.255.255.255'))
FFFFFFFF
SELECT HEX(INET6_ATON('255.255.255.255'));
HEX(INET6_ATON('255.255.255.255'))
FFFFFFFF
SELECT HEX(INET_ATON('192.168.08.2'));
HEX(INET_ATON('192.168.08.2'))
C0A80802
SELECT HEX(INET6_ATON('192.168.08.2'));
HEX(INET6_ATON('192.168.08.2'))
C0A80802
SELECT HEX(INET_ATON('192.168.0x8.2'));
HEX(INET_ATON('192.168.0x8.2'))
NULL
SELECT HEX(INET6_ATON('192.168.0x8.2'));
HEX(INET6_ATON('192.168.0x8.2'))
NULL
SELECT HEX(INET_ATON('1.2.255'));
HEX(INET_ATON('1.2.255'))
10200FF
SELECT HEX(INET6_ATON('1.2.255'));
HEX(INET6_ATON('1.2.255'))
NULL
SELECT HEX(INET_ATON('1.2.256'));
HEX(INET_ATON('1.2.256'))
NULL
SELECT HEX(INET6_ATON('1.2.256'));
HEX(INET6_ATON('1.2.256'))
NULL
SELECT HEX(INET_ATON('1.0002.3.4'));
HEX(INET_ATON('1.0002.3.4'))
1020304
SELECT HEX(INET6_ATON('1.0002.3.4'));
HEX(INET6_ATON('1.0002.3.4'))
NULL
SELECT HEX(INET_ATON('1.2.3.4.5'));
HEX(INET_ATON('1.2.3.4.5'))
NULL
SELECT HEX(INET6_ATON('1.2.3.4.5'));
HEX(INET6_ATON('1.2.3.4.5'))
NULL
# -- Checking mix of INET- and INET6- functions...
SELECT HEX(INET6_ATON(INET_NTOA(INET_ATON('1.2.3.4')))) AS x;
x
01020304
# -- Checking IS_IPV4() / IS_IPV6()...
SELECT IS_IPV4(NULL);
IS_IPV4(NULL)
0
SELECT IS_IPV4(1);
IS_IPV4(1)
0
SELECT IS_IPV4(1.0);
IS_IPV4(1.0)
0
SELECT IS_IPV4('1.2.3.4');
IS_IPV4('1.2.3.4')
1
SELECT IS_IPV4('001.02.000.255');
IS_IPV4('001.02.000.255')
1
SELECT IS_IPV4('::1.2.0.255');
IS_IPV4('::1.2.0.255')
0
SELECT IS_IPV4('::1');
IS_IPV4('::1')
0
SELECT IS_IPV4(BINARY('1.2.3.4'));
IS_IPV4(BINARY('1.2.3.4'))
1
SELECT IS_IPV6(NULL);
IS_IPV6(NULL)
0
SELECT IS_IPV6(1);
IS_IPV6(1)
0
SELECT IS_IPV6(1.0);
IS_IPV6(1.0)
0
SELECT IS_IPV6('1.2.3.4');
IS_IPV6('1.2.3.4')
0
SELECT IS_IPV6('001.02.000.255');
IS_IPV6('001.02.000.255')
0
SELECT IS_IPV6('::001.02.000.255');
IS_IPV6('::001.02.000.255')
1
SELECT IS_IPV6('::1.2.0.255');
IS_IPV6('::1.2.0.255')
1
SELECT IS_IPV6('::1');
IS_IPV6('::1')
1
SELECT IS_IPV6('0000:0000:0000:0000:0000:0000:0000:0001');
IS_IPV6('0000:0000:0000:0000:0000:0000:0000:0001')
1
SELECT IS_IPV6(BINARY('0000:0000:0000:0000:0000:0000:0000:0001'));
IS_IPV6(BINARY('0000:0000:0000:0000:0000:0000:0000:0001'))
1
# -- Checking IS_IPV4_MAPPED() and IS_IPV4_COMPAT()...
SELECT IS_IPV4_MAPPED(NULL), IS_IPV4_COMPAT(NULL);
IS_IPV4_MAPPED(NULL) IS_IPV4_COMPAT(NULL)
0 0
SELECT IS_IPV4_MAPPED(INET6_ATON('1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('1.2.3.4'));
IS_IPV4_MAPPED(INET6_ATON('1.2.3.4')) IS_IPV4_COMPAT(INET6_ATON('1.2.3.4'))
0 0
SELECT IS_IPV4_MAPPED(INET6_ATON('::1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('::1.2.3.4'));
IS_IPV4_MAPPED(INET6_ATON('::1.2.3.4')) IS_IPV4_COMPAT(INET6_ATON('::1.2.3.4'))
0 1
SELECT IS_IPV4_MAPPED(INET6_ATON('::FFFF:1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('::FFFF:1.2.3.4'));
IS_IPV4_MAPPED(INET6_ATON('::FFFF:1.2.3.4')) IS_IPV4_COMPAT(INET6_ATON('::FFFF:1.2.3.4'))
1 0
SELECT IS_IPV4_MAPPED(INET6_ATON('::ABCD:1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('::ABCD:1.2.3.4'));
IS_IPV4_MAPPED(INET6_ATON('::ABCD:1.2.3.4')) IS_IPV4_COMPAT(INET6_ATON('::ABCD:1.2.3.4'))
0 0
SELECT IS_IPV4_MAPPED(INET6_ATON('::1')),
IS_IPV4_COMPAT(INET6_ATON('::1'));
IS_IPV4_MAPPED(INET6_ATON('::1')) IS_IPV4_COMPAT(INET6_ATON('::1'))
0 0
SELECT IS_IPV4_MAPPED(INET6_ATON('::')),
IS_IPV4_COMPAT(INET6_ATON('::'));
IS_IPV4_MAPPED(INET6_ATON('::')) IS_IPV4_COMPAT(INET6_ATON('::'))
0 0
# -- Checking IS_IPV4_COMPAT()...
# -- Working with a table...
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
CREATE TABLE t1(ip INT UNSIGNED);
CREATE TABLE t2(ip VARBINARY(16));
INSERT INTO t1 VALUES
(INET_ATON('1.2.3.4')), (INET_ATON('255.255.255.255'));
SELECT INET_NTOA(ip) FROM t1;
INET_NTOA(ip)
1.2.3.4
255.255.255.255
INSERT INTO t2 VALUES
(INET6_ATON('1.2.3.4')), (INET6_ATON('255.255.255.255')),
(INET6_ATON('::1.2.3.4')), (INET6_ATON('::ffff:255.255.255.255')),
(INET6_ATON('::')), (INET6_ATON('::1')),
(INET6_ATON('1020:3040:5060:7080:90A0:B0C0:D0E0:F010'));
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
INET6_NTOA(ip) HEX(ip) LENGTH(ip)
1.2.3.4 01020304 4
255.255.255.255 FFFFFFFF 4
::1.2.3.4 00000000000000000000000001020304 16
::ffff:255.255.255.255 00000000000000000000FFFFFFFFFFFF 16
:: 00000000000000000000000000000000 16
::1 00000000000000000000000000000001 16
1020:3040:5060:7080:90a0:b0c0:d0e0:f010 102030405060708090A0B0C0D0E0F010 16
# test different sql mode
set sql_mode = STRICT_TRANS_TABLES;
select @@sql_mode;
@@sql_mode
STRICT_TRANS_TABLES
DELETE FROM t2;
INSERT INTO t2 VALUES (INET6_ATON('1.0002.3.4'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (INET6_ATON('1.2.255'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (INET6_ATON('1.2.256'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (INET6_ATON('192.168.0x8.2'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (inet_aton("122.256"));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (inet_aton("122.226."));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (inet_aton(""));
ERROR HY000: Invalid argument
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
INET6_NTOA(ip) HEX(ip) LENGTH(ip)
SELECT INET6_ATON('1.0002.3.4');
INET6_ATON('1.0002.3.4')
NULL
SELECT INET6_ATON("");
INET6_ATON("")
NULL
set sql_mode = STRICT_ALL_TABLES;
select @@sql_mode;
@@sql_mode
STRICT_ALL_TABLES
DELETE FROM t2;
INSERT INTO t2 VALUES (INET6_ATON('1.0002.3.4'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (INET6_ATON('1.2.255'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (INET6_ATON('1.2.256'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (INET6_ATON('192.168.0x8.2'));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (inet_aton("122.256"));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (inet_aton("122.226."));
ERROR HY000: Invalid argument
INSERT INTO t2 VALUES (inet_aton(""));
ERROR HY000: Invalid argument
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
INET6_NTOA(ip) HEX(ip) LENGTH(ip)
SELECT INET6_ATON('1.0002.3.4');
INET6_ATON('1.0002.3.4')
NULL
SELECT INET6_ATON("");
INET6_ATON("")
NULL
set sql_mode = '';
select @@sql_mode;
@@sql_mode
DELETE FROM t2;
INSERT INTO t2 VALUES (INET6_ATON('1.0002.3.4'));
INSERT INTO t2 VALUES (INET6_ATON('1.2.255'));
INSERT INTO t2 VALUES (INET6_ATON('1.2.256'));
INSERT INTO t2 VALUES (INET6_ATON('192.168.0x8.2'));
INSERT INTO t2 VALUES (inet_aton("122.256"));
INSERT INTO t2 VALUES (inet_aton("122.226."));
INSERT INTO t2 VALUES (inet_aton(""));
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
INET6_NTOA(ip) HEX(ip) LENGTH(ip)
NULL NULL NULL
NULL NULL NULL
NULL NULL NULL
NULL NULL NULL
NULL NULL NULL
NULL NULL NULL
NULL NULL NULL
SELECT INET6_ATON('1.0002.3.4');
INET6_ATON('1.0002.3.4')
NULL
SELECT INET6_ATON("");
INET6_ATON("")
NULL
DROP TABLE t1;
DROP TABLE t2;
# -- Done misc test.
#
# End of tests
#
connection sys_jianhua;
alter system set _enable_static_typing_engine = true;
--disable_query_log
set @@session.explicit_defaults_for_timestamp=off;
--enable_query_log
--disable_warnings
drop table if exists t1,t2;
--enable_warnings
#
# Testing of misc functions
#
select inet_ntoa(null),inet_aton(null);
select inet_ntoa(inet_aton("255.255.255.255.255.255.255.255"));
select inet_aton("255.255.255.255.255"),inet_aton("255.255.1.255"),inet_aton("0.1.255");
select inet_ntoa(1099511627775),inet_ntoa(4294902271),inet_ntoa(511);
select inet_aton("0.255.255.255.255");
select inet_aton("255.255.255.0000");
select hex(inet_aton('127'));
select hex(inet_aton('127.1'));
select hex(inet_aton('127.1.1'));
select inet_aton("122.256");
select inet_aton("122.226.");
select inet_aton("");
--echo
--echo # -- INET6_ATON: checking NULL, invalid types, out-of range values...
--echo
SELECT INET6_ATON(NULL) IS NULL;
SELECT INET6_ATON(123) IS NULL;
SELECT INET6_ATON(123.45) IS NULL;
SELECT INET6_ATON(str_to_date('2014,2,28 09', '%Y,%m,%d %h')) IS NULL;
SELECT INET6_ATON('1.2.3') IS NULL;
SELECT INET6_ATON('1.2.3.') IS NULL;
SELECT INET6_ATON('1..3.4') IS NULL;
SELECT INET6_ATON('-1.2.3.4') IS NULL;
SELECT INET6_ATON('1.2.3.256') IS NULL;
SELECT INET6_ATON('1.2.3.4.5') IS NULL;
SELECT INET6_ATON('0001.2.3.4') IS NULL;
SELECT INET6_ATON('0x1.2.3.4') IS NULL;
SELECT INET6_ATON('a.2.3.4') IS NULL;
SELECT INET6_ATON('1.2.3.4:80') IS NULL;
SELECT INET6_ATON('1.2.3.4/32') IS NULL;
SELECT INET6_ATON('oceanbase') IS NULL;
SELECT INET6_ATON(':::') IS NULL;
SELECT INET6_ATON(':1:2:3') IS NULL;
SELECT INET6_ATON('1:2:3:') IS NULL;
SELECT INET6_ATON(':1::2:3') IS NULL;
SELECT INET6_ATON('1::2:3:') IS NULL;
SELECT INET6_ATON('::00001') IS NULL;
SELECT INET6_ATON('::00001:2') IS NULL;
SELECT INET6_ATON('::12345') IS NULL;
SELECT INET6_ATON('1020::3040::5060') IS NULL;
SELECT INET6_ATON('::ABCZ') IS NULL;
SELECT INET6_ATON('::0x1.2.3.4') IS NULL;
SELECT INET6_ATON('::1.0x2.3.4') IS NULL;
SELECT INET6_ATON('::a.b.c.d') IS NULL;
SELECT INET6_ATON('::FFFF:0x1.2.3.4') IS NULL;
SELECT INET6_ATON('::FFFF:1.0x2.3.4') IS NULL;
SELECT INET6_ATON('::FFFF:a.b.c.d') IS NULL;
SELECT INET6_ATON('::1.2.3.4:ABCD') IS NULL;
--echo # NOTE: such addresses are supported because getaddrinfo() supports them.
--echo # This is just to record the current behaviour.
SELECT HEX(INET6_ATON('::ABCD:1.2.3.4'));
--echo
--echo # -- INET6_ATON: checking binary representation...
--echo
SELECT HEX(INET6_ATON('0.0.0.0'));
SELECT HEX(INET6_ATON('00.00.00.00'));
SELECT HEX(INET6_ATON('000.000.000.000'));
SELECT HEX(INET6_ATON('1.2.3.4'));
SELECT HEX(INET6_ATON('01.02.03.04'));
SELECT HEX(INET6_ATON('001.002.003.004'));
SELECT HEX(INET6_ATON('255.255.255.255'));
SELECT HEX(INET6_ATON('::'));
SELECT HEX(INET6_ATON('0::0'));
SELECT HEX(INET6_ATON('1::2'));
SELECT HEX(INET6_ATON('0::'));
SELECT HEX(INET6_ATON('1::'));
SELECT HEX(INET6_ATON('::0'));
SELECT HEX(INET6_ATON('::1'));
SELECT HEX(INET6_ATON('1:2:3:4:5:6:7:8'));
SELECT HEX(INET6_ATON('::2:3:4:5:6:7:8'));
SELECT HEX(INET6_ATON('1::3:4:5:6:7:8'));
SELECT HEX(INET6_ATON('1:2::4:5:6:7:8'));
SELECT HEX(INET6_ATON('1:2:3::5:6:7:8'));
SELECT HEX(INET6_ATON('1:2:3:4::6:7:8'));
SELECT HEX(INET6_ATON('1:2:3:4:5::7:8'));
SELECT HEX(INET6_ATON('1:2:3:4:5:6::8'));
SELECT HEX(INET6_ATON('1:2:3:4:5:6:7::'));
SELECT HEX(INET6_ATON('0000:0000::0000:0001'));
SELECT HEX(INET6_ATON('1234:5678:9abc:def0:4321:8765:cba9:0fed'));
SELECT HEX(INET6_ATON('0000:0000:0000:0000:0000:0000:0000:0001'));
SELECT HEX(INET6_ATON('::C0A8:0102'));
SELECT HEX(INET6_ATON('::c0a8:0102'));
SELECT HEX(INET6_ATON('::192.168.1.2'));
SELECT HEX(INET6_ATON('::FfFf:C0a8:0102'));
SELECT HEX(INET6_ATON('::ffff:c0a8:0102'));
SELECT HEX(INET6_ATON('::ffff:192.168.1.2'));
SELECT HEX(INET6_ATON('::01.2.3.4'));
SELECT HEX(INET6_ATON('::1.02.3.4'));
SELECT HEX(INET6_ATON('::1.2.03.4'));
SELECT HEX(INET6_ATON('::1.2.3.04'));
SELECT HEX(INET6_ATON('::1.2.3.00'));
SELECT HEX(INET6_ATON('::FFFF:01.2.3.4'));
SELECT HEX(INET6_ATON('::FFFF:1.02.3.4'));
SELECT HEX(INET6_ATON('::FFFF:1.2.03.4'));
SELECT HEX(INET6_ATON('::FFFF:1.2.3.04'));
SELECT HEX(INET6_ATON('::FFFF:1.2.3.00'));
--echo
--echo # -- INET6_ATON: checking the length is either 4 or 16...
--echo
SELECT LENGTH(INET6_ATON('0.0.0.0'));
SELECT LENGTH(INET6_ATON('255.255.255.255'));
SELECT LENGTH(INET6_ATON('::'));
SELECT LENGTH(INET6_ATON('1020:3040:5060:7080:90A0:B0C0:D0E0:F010'));
--echo
--echo # -- INET6_NTOA: checking NULL, invalid types, out-of range values...
--echo
SELECT INET6_NTOA(NULL);
SELECT INET6_NTOA(123);
SELECT INET6_NTOA(123.456);
SELECT INET6_NTOA(str_to_date('2014,2,28 09', '%Y,%m,%d %h'));
SELECT INET6_NTOA(UNHEX('C0A801')); # 3 bytes -> NULL
SELECT INET6_NTOA(UNHEX('C0A80102')); # 4 bytes -> 192.168.1.2
SELECT INET6_NTOA(UNHEX('C0A8010203')); # 5 bytes -> NULL
SELECT INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F')); # 15 bytes -> NULL
SELECT INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F10')); # 16 bytes -> IP
SELECT INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F1011')); # 17 bytes -> NULL
SELECT INET6_NTOA('1234'), INET6_NTOA(BINARY('1234'));
SELECT INET6_NTOA('0123456789abcdef'), INET6_NTOA(BINARY('0123456789abcdef'));
--echo
--echo # -- Checking double-conversion...
--echo
SELECT INET6_NTOA(INET6_ATON('::'));
SELECT INET6_NTOA(INET6_ATON('0::0'));
SELECT INET6_NTOA(INET6_ATON('1::2'));
SELECT INET6_NTOA(INET6_ATON('0::'));
SELECT INET6_NTOA(INET6_ATON('1::'));
SELECT INET6_NTOA(INET6_ATON('::0'));
SELECT INET6_NTOA(INET6_ATON('::1'));
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7:8'));
SELECT INET6_NTOA(INET6_ATON('::2:3:4:5:6:7:8'));
SELECT INET6_NTOA(INET6_ATON('1::3:4:5:6:7:8'));
SELECT INET6_NTOA(INET6_ATON('1:2::4:5:6:7:8'));
SELECT INET6_NTOA(INET6_ATON('1:2:3::5:6:7:8'));
SELECT INET6_NTOA(INET6_ATON('1:2:3:4::6:7:8'));
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5::7:8'));
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6::8'));
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7::'));
SELECT INET6_NTOA(INET6_ATON('0000:0000::0000:0001'));
SELECT INET6_NTOA(INET6_ATON('1234:5678:9abc:def0:4321:8765:cba9:0fed'));
SELECT INET6_NTOA(INET6_ATON('0000:0000:0000:0000:0000:0000:0000:0001'));
SELECT INET6_NTOA(INET6_ATON('0000:0000:0000:0000:1:1:255.255.255.255'));
SELECT INET6_NTOA(INET6_ATON('0000:0000:0000:0000:0001:0001:255.255.255.255'));
SELECT INET6_NTOA(INET6_ATON('::C0A8:0102'));
SELECT INET6_NTOA(INET6_ATON('::c0a8:0102'));
SELECT INET6_NTOA(INET6_ATON('::192.168.1.2'));
SELECT INET6_NTOA(INET6_ATON('::FfFf:C0a8:0102'));
SELECT INET6_NTOA(INET6_ATON('::ffff:c0a8:0102'));
SELECT INET6_NTOA(INET6_ATON('::ffff:192.168.1.2'));
SELECT INET6_NTOA(INET6_ATON('::01.2.3.4'));
SELECT INET6_NTOA(INET6_ATON('::1.02.3.4'));
SELECT INET6_NTOA(INET6_ATON('::1.2.03.4'));
SELECT INET6_NTOA(INET6_ATON('::1.2.3.04'));
SELECT INET6_NTOA(INET6_ATON('::1.2.3.00'));
SELECT INET6_NTOA(INET6_ATON('::FFFF:01.2.3.4'));
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.02.3.4'));
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.2.03.4'));
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.2.3.04'));
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.2.3.00'));
--echo
--echo # -- Comparing INET_ATON() and INET6_ATON()...
--echo
SELECT HEX(INET_ATON('192.168.1.2'));
SELECT HEX(INET6_ATON('192.168.1.2'));
SELECT HEX(INET_ATON('255.255.255.255'));
SELECT HEX(INET6_ATON('255.255.255.255'));
SELECT HEX(INET_ATON('192.168.08.2'));
SELECT HEX(INET6_ATON('192.168.08.2'));
SELECT HEX(INET_ATON('192.168.0x8.2'));
SELECT HEX(INET6_ATON('192.168.0x8.2'));
SELECT HEX(INET_ATON('1.2.255'));
SELECT HEX(INET6_ATON('1.2.255'));
SELECT HEX(INET_ATON('1.2.256'));
SELECT HEX(INET6_ATON('1.2.256'));
SELECT HEX(INET_ATON('1.0002.3.4'));
SELECT HEX(INET6_ATON('1.0002.3.4'));
SELECT HEX(INET_ATON('1.2.3.4.5'));
SELECT HEX(INET6_ATON('1.2.3.4.5'));
--echo
--echo # -- Checking mix of INET- and INET6- functions...
--echo
SELECT HEX(INET6_ATON(INET_NTOA(INET_ATON('1.2.3.4')))) AS x;
--echo
--echo # -- Checking IS_IPV4() / IS_IPV6()...
--echo
SELECT IS_IPV4(NULL);
SELECT IS_IPV4(1);
SELECT IS_IPV4(1.0);
SELECT IS_IPV4('1.2.3.4');
SELECT IS_IPV4('001.02.000.255');
SELECT IS_IPV4('::1.2.0.255');
SELECT IS_IPV4('::1');
SELECT IS_IPV4(BINARY('1.2.3.4'));
SELECT IS_IPV6(NULL);
SELECT IS_IPV6(1);
SELECT IS_IPV6(1.0);
SELECT IS_IPV6('1.2.3.4');
SELECT IS_IPV6('001.02.000.255');
SELECT IS_IPV6('::001.02.000.255');
SELECT IS_IPV6('::1.2.0.255');
SELECT IS_IPV6('::1');
SELECT IS_IPV6('0000:0000:0000:0000:0000:0000:0000:0001');
SELECT IS_IPV6(BINARY('0000:0000:0000:0000:0000:0000:0000:0001'));
--echo
--echo # -- Checking IS_IPV4_MAPPED() and IS_IPV4_COMPAT()...
--echo
SELECT IS_IPV4_MAPPED(NULL), IS_IPV4_COMPAT(NULL);
SELECT IS_IPV4_MAPPED(INET6_ATON('1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('1.2.3.4'));
SELECT IS_IPV4_MAPPED(INET6_ATON('::1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('::1.2.3.4'));
SELECT IS_IPV4_MAPPED(INET6_ATON('::FFFF:1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('::FFFF:1.2.3.4'));
SELECT IS_IPV4_MAPPED(INET6_ATON('::ABCD:1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('::ABCD:1.2.3.4'));
SELECT IS_IPV4_MAPPED(INET6_ATON('::1')),
IS_IPV4_COMPAT(INET6_ATON('::1'));
SELECT IS_IPV4_MAPPED(INET6_ATON('::')),
IS_IPV4_COMPAT(INET6_ATON('::'));
# NOTE: IS_IPV4_COMPAT() / IS_IPV4_MAPPED() could work with "regular strings in
# binary collation" too, but there is no way to create a "regular string"
# starting with \0.
--echo
--echo # -- Checking IS_IPV4_COMPAT()...
--echo
--echo
--echo # -- Working with a table...
--echo
--disable_warnings
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
--enable_warnings
CREATE TABLE t1(ip INT UNSIGNED);
CREATE TABLE t2(ip VARBINARY(16));
--echo
INSERT INTO t1 VALUES
(INET_ATON('1.2.3.4')), (INET_ATON('255.255.255.255'));
SELECT INET_NTOA(ip) FROM t1;
--echo
INSERT INTO t2 VALUES
(INET6_ATON('1.2.3.4')), (INET6_ATON('255.255.255.255')),
(INET6_ATON('::1.2.3.4')), (INET6_ATON('::ffff:255.255.255.255')),
(INET6_ATON('::')), (INET6_ATON('::1')),
(INET6_ATON('1020:3040:5060:7080:90A0:B0C0:D0E0:F010'));
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
--echo
--echo # test different sql mode
--echo
set sql_mode = STRICT_TRANS_TABLES;
select @@sql_mode;
DELETE FROM t2;
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('1.0002.3.4'));
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('1.2.255'));
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('1.2.256'));
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('192.168.0x8.2'));
--error 1210
INSERT INTO t2 VALUES (inet_aton("122.256"));
--error 1210
INSERT INTO t2 VALUES (inet_aton("122.226."));
--error 1210
INSERT INTO t2 VALUES (inet_aton(""));
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
SELECT INET6_ATON('1.0002.3.4');
SELECT INET6_ATON("");
--echo
set sql_mode = STRICT_ALL_TABLES;
select @@sql_mode;
DELETE FROM t2;
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('1.0002.3.4'));
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('1.2.255'));
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('1.2.256'));
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('192.168.0x8.2'));
--error 1210
INSERT INTO t2 VALUES (inet_aton("122.256"));
--error 1210
INSERT INTO t2 VALUES (inet_aton("122.226."));
--error 1210
INSERT INTO t2 VALUES (inet_aton(""));
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
SELECT INET6_ATON('1.0002.3.4');
SELECT INET6_ATON("");
--echo
set sql_mode = '';
select @@sql_mode;
DELETE FROM t2;
INSERT INTO t2 VALUES (INET6_ATON('1.0002.3.4'));
INSERT INTO t2 VALUES (INET6_ATON('1.2.255'));
INSERT INTO t2 VALUES (INET6_ATON('1.2.256'));
INSERT INTO t2 VALUES (INET6_ATON('192.168.0x8.2'));
INSERT INTO t2 VALUES (inet_aton("122.256"));
INSERT INTO t2 VALUES (inet_aton("122.226."));
INSERT INTO t2 VALUES (inet_aton(""));
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
SELECT INET6_ATON('1.0002.3.4');
SELECT INET6_ATON("");
DROP TABLE t1;
DROP TABLE t2;
--echo
--echo # -- Done misc test.
--echo
--echo #
--echo # End of tests
--echo #
connect (sys_jianhua, $OBMYSQL_MS0,root@sys,,oceanbase,$OBMYSQL_PORT);
connection sys_jianhua;
alter system set _enable_static_typing_engine = false;
connect(mysql_jianhua, $OBMYSQL_MS0,root@mysql,'',test,$OBMYSQL_PORT);
connection mysql_jianhua;
--disable_query_log
set @@session.explicit_defaults_for_timestamp=off;
--enable_query_log
--disable_warnings
drop table if exists t1,t2;
--enable_warnings
#
# Testing of misc functions
#
select inet_ntoa(null),inet_aton(null);
select inet_ntoa(inet_aton("255.255.255.255.255.255.255.255"));
select inet_aton("255.255.255.255.255"),inet_aton("255.255.1.255"),inet_aton("0.1.255");
select inet_ntoa(1099511627775),inet_ntoa(4294902271),inet_ntoa(511);
select inet_aton("0.255.255.255.255");
select inet_aton("255.255.255.0000");
select hex(inet_aton('127'));
select hex(inet_aton('127.1'));
select hex(inet_aton('127.1.1'));
select inet_aton("122.256");
select inet_aton("122.226.");
select inet_aton("");
--echo
--echo # -- INET6_ATON: checking NULL, invalid types, out-of range values...
--echo
SELECT INET6_ATON(NULL) IS NULL;
SELECT INET6_ATON(123) IS NULL;
SELECT INET6_ATON(123.45) IS NULL;
SELECT INET6_ATON(str_to_date('2014,2,28 09', '%Y,%m,%d %h')) IS NULL;
SELECT INET6_ATON('1.2.3') IS NULL;
SELECT INET6_ATON('1.2.3.') IS NULL;
SELECT INET6_ATON('1..3.4') IS NULL;
SELECT INET6_ATON('-1.2.3.4') IS NULL;
SELECT INET6_ATON('1.2.3.256') IS NULL;
SELECT INET6_ATON('1.2.3.4.5') IS NULL;
SELECT INET6_ATON('0001.2.3.4') IS NULL;
SELECT INET6_ATON('0x1.2.3.4') IS NULL;
SELECT INET6_ATON('a.2.3.4') IS NULL;
SELECT INET6_ATON('1.2.3.4:80') IS NULL;
SELECT INET6_ATON('1.2.3.4/32') IS NULL;
SELECT INET6_ATON('oceanbase') IS NULL;
SELECT INET6_ATON(':::') IS NULL;
SELECT INET6_ATON(':1:2:3') IS NULL;
SELECT INET6_ATON('1:2:3:') IS NULL;
SELECT INET6_ATON(':1::2:3') IS NULL;
SELECT INET6_ATON('1::2:3:') IS NULL;
SELECT INET6_ATON('::00001') IS NULL;
SELECT INET6_ATON('::00001:2') IS NULL;
SELECT INET6_ATON('::12345') IS NULL;
SELECT INET6_ATON('1020::3040::5060') IS NULL;
SELECT INET6_ATON('::ABCZ') IS NULL;
SELECT INET6_ATON('::0x1.2.3.4') IS NULL;
SELECT INET6_ATON('::1.0x2.3.4') IS NULL;
SELECT INET6_ATON('::a.b.c.d') IS NULL;
SELECT INET6_ATON('::FFFF:0x1.2.3.4') IS NULL;
SELECT INET6_ATON('::FFFF:1.0x2.3.4') IS NULL;
SELECT INET6_ATON('::FFFF:a.b.c.d') IS NULL;
SELECT INET6_ATON('::1.2.3.4:ABCD') IS NULL;
--echo # NOTE: such addresses are supported because getaddrinfo() supports them.
--echo # This is just to record the current behaviour.
SELECT HEX(INET6_ATON('::ABCD:1.2.3.4'));
--echo
--echo # -- INET6_ATON: checking binary representation...
--echo
SELECT HEX(INET6_ATON('0.0.0.0'));
SELECT HEX(INET6_ATON('00.00.00.00'));
SELECT HEX(INET6_ATON('000.000.000.000'));
SELECT HEX(INET6_ATON('1.2.3.4'));
SELECT HEX(INET6_ATON('01.02.03.04'));
SELECT HEX(INET6_ATON('001.002.003.004'));
SELECT HEX(INET6_ATON('255.255.255.255'));
SELECT HEX(INET6_ATON('::'));
SELECT HEX(INET6_ATON('0::0'));
SELECT HEX(INET6_ATON('1::2'));
SELECT HEX(INET6_ATON('0::'));
SELECT HEX(INET6_ATON('1::'));
SELECT HEX(INET6_ATON('::0'));
SELECT HEX(INET6_ATON('::1'));
SELECT HEX(INET6_ATON('1:2:3:4:5:6:7:8'));
SELECT HEX(INET6_ATON('::2:3:4:5:6:7:8'));
SELECT HEX(INET6_ATON('1::3:4:5:6:7:8'));
SELECT HEX(INET6_ATON('1:2::4:5:6:7:8'));
SELECT HEX(INET6_ATON('1:2:3::5:6:7:8'));
SELECT HEX(INET6_ATON('1:2:3:4::6:7:8'));
SELECT HEX(INET6_ATON('1:2:3:4:5::7:8'));
SELECT HEX(INET6_ATON('1:2:3:4:5:6::8'));
SELECT HEX(INET6_ATON('1:2:3:4:5:6:7::'));
SELECT HEX(INET6_ATON('0000:0000::0000:0001'));
SELECT HEX(INET6_ATON('1234:5678:9abc:def0:4321:8765:cba9:0fed'));
SELECT HEX(INET6_ATON('0000:0000:0000:0000:0000:0000:0000:0001'));
SELECT HEX(INET6_ATON('::C0A8:0102'));
SELECT HEX(INET6_ATON('::c0a8:0102'));
SELECT HEX(INET6_ATON('::192.168.1.2'));
SELECT HEX(INET6_ATON('::FfFf:C0a8:0102'));
SELECT HEX(INET6_ATON('::ffff:c0a8:0102'));
SELECT HEX(INET6_ATON('::ffff:192.168.1.2'));
SELECT HEX(INET6_ATON('::01.2.3.4'));
SELECT HEX(INET6_ATON('::1.02.3.4'));
SELECT HEX(INET6_ATON('::1.2.03.4'));
SELECT HEX(INET6_ATON('::1.2.3.04'));
SELECT HEX(INET6_ATON('::1.2.3.00'));
SELECT HEX(INET6_ATON('::FFFF:01.2.3.4'));
SELECT HEX(INET6_ATON('::FFFF:1.02.3.4'));
SELECT HEX(INET6_ATON('::FFFF:1.2.03.4'));
SELECT HEX(INET6_ATON('::FFFF:1.2.3.04'));
SELECT HEX(INET6_ATON('::FFFF:1.2.3.00'));
--echo
--echo # -- INET6_ATON: checking the length is either 4 or 16...
--echo
SELECT LENGTH(INET6_ATON('0.0.0.0'));
SELECT LENGTH(INET6_ATON('255.255.255.255'));
SELECT LENGTH(INET6_ATON('::'));
SELECT LENGTH(INET6_ATON('1020:3040:5060:7080:90A0:B0C0:D0E0:F010'));
--echo
--echo # -- INET6_NTOA: checking NULL, invalid types, out-of range values...
--echo
SELECT INET6_NTOA(NULL);
SELECT INET6_NTOA(123);
SELECT INET6_NTOA(123.456);
SELECT INET6_NTOA(str_to_date('2014,2,28 09', '%Y,%m,%d %h'));
SELECT INET6_NTOA(UNHEX('C0A801')); # 3 bytes -> NULL
SELECT INET6_NTOA(UNHEX('C0A80102')); # 4 bytes -> 192.168.1.2
SELECT INET6_NTOA(UNHEX('C0A8010203')); # 5 bytes -> NULL
SELECT INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F')); # 15 bytes -> NULL
SELECT INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F10')); # 16 bytes -> IP
SELECT INET6_NTOA(UNHEX('0102030405060708090A0B0C0D0E0F1011')); # 17 bytes -> NULL
SELECT INET6_NTOA('1234'), INET6_NTOA(BINARY('1234'));
SELECT INET6_NTOA('0123456789abcdef'), INET6_NTOA(BINARY('0123456789abcdef'));
--echo
--echo # -- Checking double-conversion...
--echo
SELECT INET6_NTOA(INET6_ATON('::'));
SELECT INET6_NTOA(INET6_ATON('0::0'));
SELECT INET6_NTOA(INET6_ATON('1::2'));
SELECT INET6_NTOA(INET6_ATON('0::'));
SELECT INET6_NTOA(INET6_ATON('1::'));
SELECT INET6_NTOA(INET6_ATON('::0'));
SELECT INET6_NTOA(INET6_ATON('::1'));
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7:8'));
SELECT INET6_NTOA(INET6_ATON('::2:3:4:5:6:7:8'));
SELECT INET6_NTOA(INET6_ATON('1::3:4:5:6:7:8'));
SELECT INET6_NTOA(INET6_ATON('1:2::4:5:6:7:8'));
SELECT INET6_NTOA(INET6_ATON('1:2:3::5:6:7:8'));
SELECT INET6_NTOA(INET6_ATON('1:2:3:4::6:7:8'));
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5::7:8'));
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6::8'));
SELECT INET6_NTOA(INET6_ATON('1:2:3:4:5:6:7::'));
SELECT INET6_NTOA(INET6_ATON('0000:0000::0000:0001'));
SELECT INET6_NTOA(INET6_ATON('1234:5678:9abc:def0:4321:8765:cba9:0fed'));
SELECT INET6_NTOA(INET6_ATON('0000:0000:0000:0000:0000:0000:0000:0001'));
SELECT INET6_NTOA(INET6_ATON('0000:0000:0000:0000:1:1:255.255.255.255'));
SELECT INET6_NTOA(INET6_ATON('0000:0000:0000:0000:0001:0001:255.255.255.255'));
SELECT INET6_NTOA(INET6_ATON('::C0A8:0102'));
SELECT INET6_NTOA(INET6_ATON('::c0a8:0102'));
SELECT INET6_NTOA(INET6_ATON('::192.168.1.2'));
SELECT INET6_NTOA(INET6_ATON('::FfFf:C0a8:0102'));
SELECT INET6_NTOA(INET6_ATON('::ffff:c0a8:0102'));
SELECT INET6_NTOA(INET6_ATON('::ffff:192.168.1.2'));
SELECT INET6_NTOA(INET6_ATON('::01.2.3.4'));
SELECT INET6_NTOA(INET6_ATON('::1.02.3.4'));
SELECT INET6_NTOA(INET6_ATON('::1.2.03.4'));
SELECT INET6_NTOA(INET6_ATON('::1.2.3.04'));
SELECT INET6_NTOA(INET6_ATON('::1.2.3.00'));
SELECT INET6_NTOA(INET6_ATON('::FFFF:01.2.3.4'));
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.02.3.4'));
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.2.03.4'));
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.2.3.04'));
SELECT INET6_NTOA(INET6_ATON('::FFFF:1.2.3.00'));
--echo
--echo # -- Comparing INET_ATON() and INET6_ATON()...
--echo
SELECT HEX(INET_ATON('192.168.1.2'));
SELECT HEX(INET6_ATON('192.168.1.2'));
SELECT HEX(INET_ATON('255.255.255.255'));
SELECT HEX(INET6_ATON('255.255.255.255'));
SELECT HEX(INET_ATON('192.168.08.2'));
SELECT HEX(INET6_ATON('192.168.08.2'));
SELECT HEX(INET_ATON('192.168.0x8.2'));
SELECT HEX(INET6_ATON('192.168.0x8.2'));
SELECT HEX(INET_ATON('1.2.255'));
SELECT HEX(INET6_ATON('1.2.255'));
SELECT HEX(INET_ATON('1.2.256'));
SELECT HEX(INET6_ATON('1.2.256'));
SELECT HEX(INET_ATON('1.0002.3.4'));
SELECT HEX(INET6_ATON('1.0002.3.4'));
SELECT HEX(INET_ATON('1.2.3.4.5'));
SELECT HEX(INET6_ATON('1.2.3.4.5'));
--echo
--echo # -- Checking mix of INET- and INET6- functions...
--echo
SELECT HEX(INET6_ATON(INET_NTOA(INET_ATON('1.2.3.4')))) AS x;
--echo
--echo # -- Checking IS_IPV4() / IS_IPV6()...
--echo
SELECT IS_IPV4(NULL);
SELECT IS_IPV4(1);
SELECT IS_IPV4(1.0);
SELECT IS_IPV4('1.2.3.4');
SELECT IS_IPV4('001.02.000.255');
SELECT IS_IPV4('::1.2.0.255');
SELECT IS_IPV4('::1');
SELECT IS_IPV4(BINARY('1.2.3.4'));
SELECT IS_IPV6(NULL);
SELECT IS_IPV6(1);
SELECT IS_IPV6(1.0);
SELECT IS_IPV6('1.2.3.4');
SELECT IS_IPV6('001.02.000.255');
SELECT IS_IPV6('::001.02.000.255');
SELECT IS_IPV6('::1.2.0.255');
SELECT IS_IPV6('::1');
SELECT IS_IPV6('0000:0000:0000:0000:0000:0000:0000:0001');
SELECT IS_IPV6(BINARY('0000:0000:0000:0000:0000:0000:0000:0001'));
--echo
--echo # -- Checking IS_IPV4_MAPPED() and IS_IPV4_COMPAT()...
--echo
SELECT IS_IPV4_MAPPED(NULL), IS_IPV4_COMPAT(NULL);
SELECT IS_IPV4_MAPPED(INET6_ATON('1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('1.2.3.4'));
SELECT IS_IPV4_MAPPED(INET6_ATON('::1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('::1.2.3.4'));
SELECT IS_IPV4_MAPPED(INET6_ATON('::FFFF:1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('::FFFF:1.2.3.4'));
SELECT IS_IPV4_MAPPED(INET6_ATON('::ABCD:1.2.3.4')),
IS_IPV4_COMPAT(INET6_ATON('::ABCD:1.2.3.4'));
SELECT IS_IPV4_MAPPED(INET6_ATON('::1')),
IS_IPV4_COMPAT(INET6_ATON('::1'));
SELECT IS_IPV4_MAPPED(INET6_ATON('::')),
IS_IPV4_COMPAT(INET6_ATON('::'));
# NOTE: IS_IPV4_COMPAT() / IS_IPV4_MAPPED() could work with "regular strings in
# binary collation" too, but there is no way to create a "regular string"
# starting with \0.
--echo
--echo # -- Checking IS_IPV4_COMPAT()...
--echo
--echo
--echo # -- Working with a table...
--echo
--disable_warnings
DROP TABLE IF EXISTS t1;
DROP TABLE IF EXISTS t2;
--enable_warnings
CREATE TABLE t1(ip INT UNSIGNED);
CREATE TABLE t2(ip VARBINARY(16));
--echo
INSERT INTO t1 VALUES
(INET_ATON('1.2.3.4')), (INET_ATON('255.255.255.255'));
SELECT INET_NTOA(ip) FROM t1;
--echo
INSERT INTO t2 VALUES
(INET6_ATON('1.2.3.4')), (INET6_ATON('255.255.255.255')),
(INET6_ATON('::1.2.3.4')), (INET6_ATON('::ffff:255.255.255.255')),
(INET6_ATON('::')), (INET6_ATON('::1')),
(INET6_ATON('1020:3040:5060:7080:90A0:B0C0:D0E0:F010'));
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
--echo
--echo # test different sql mode
--echo
set sql_mode = STRICT_TRANS_TABLES;
select @@sql_mode;
DELETE FROM t2;
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('1.0002.3.4'));
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('1.2.255'));
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('1.2.256'));
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('192.168.0x8.2'));
--error 1210
INSERT INTO t2 VALUES (inet_aton("122.256"));
--error 1210
INSERT INTO t2 VALUES (inet_aton("122.226."));
--error 1210
INSERT INTO t2 VALUES (inet_aton(""));
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
SELECT INET6_ATON('1.0002.3.4');
SELECT INET6_ATON("");
--echo
set sql_mode = STRICT_ALL_TABLES;
select @@sql_mode;
DELETE FROM t2;
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('1.0002.3.4'));
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('1.2.255'));
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('1.2.256'));
--error 1210
INSERT INTO t2 VALUES (INET6_ATON('192.168.0x8.2'));
--error 1210
INSERT INTO t2 VALUES (inet_aton("122.256"));
--error 1210
INSERT INTO t2 VALUES (inet_aton("122.226."));
--error 1210
INSERT INTO t2 VALUES (inet_aton(""));
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
SELECT INET6_ATON('1.0002.3.4');
SELECT INET6_ATON("");
--echo
set sql_mode = '';
select @@sql_mode;
DELETE FROM t2;
INSERT INTO t2 VALUES (INET6_ATON('1.0002.3.4'));
INSERT INTO t2 VALUES (INET6_ATON('1.2.255'));
INSERT INTO t2 VALUES (INET6_ATON('1.2.256'));
INSERT INTO t2 VALUES (INET6_ATON('192.168.0x8.2'));
INSERT INTO t2 VALUES (inet_aton("122.256"));
INSERT INTO t2 VALUES (inet_aton("122.226."));
INSERT INTO t2 VALUES (inet_aton(""));
SELECT INET6_NTOA(ip), HEX(ip), LENGTH(ip) FROM t2;
SELECT INET6_ATON('1.0002.3.4');
SELECT INET6_ATON("");
DROP TABLE t1;
DROP TABLE t2;
--echo
--echo # -- Done misc test.
--echo
--echo #
--echo # End of tests
--echo #
connection sys_jianhua;
alter system set _enable_static_typing_engine = true;
\ No newline at end of file
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册