diff options
author | vvvv <vvvv@ydb.tech> | 2023-09-14 02:52:30 +0300 |
---|---|---|
committer | vvvv <vvvv@ydb.tech> | 2023-09-14 03:10:27 +0300 |
commit | 1ca9dcb19e7a0dc0b160e69229e763f45197fd03 (patch) | |
tree | f01ce21482df900fbeb9ff98367c2ea2a44dc505 | |
parent | 3c19fe463b70a5dcefb770a8786c90b298966c4c (diff) | |
download | ydb-1ca9dcb19e7a0dc0b160e69229e763f45197fd03.tar.gz |
bitstring consts, doc for bitstring functions, fixes
select pg_typeof(B'101')
выдает bit
-rw-r--r-- | ydb/library/yql/parser/pg_wrapper/comp_factory.cpp | 8 | ||||
-rw-r--r-- | ydb/library/yql/parser/pg_wrapper/functions.md | 109 | ||||
-rw-r--r-- | ydb/library/yql/providers/common/mkql/yql_provider_mkql.cpp | 10 | ||||
-rw-r--r-- | ydb/library/yql/sql/pg/pg_sql.cpp | 22 | ||||
-rw-r--r-- | ydb/library/yql/sql/pg/pg_sql_autoparam_ut.cpp | 15 |
5 files changed, 149 insertions, 15 deletions
diff --git a/ydb/library/yql/parser/pg_wrapper/comp_factory.cpp b/ydb/library/yql/parser/pg_wrapper/comp_factory.cpp index 254a7a1e35..0e1da3cf8d 100644 --- a/ydb/library/yql/parser/pg_wrapper/comp_factory.cpp +++ b/ydb/library/yql/parser/pg_wrapper/comp_factory.cpp @@ -808,8 +808,12 @@ private: callInfo1.args[1] = { Int32GetDatum(typeMod), false }; callInfo1.args[2] = { BoolGetDatum(true), false }; } else { - callInfo1.args[1] = { ObjectIdGetDatum(TypeIOParam), false }; - callInfo1.args[2] = { Int32GetDatum(typeMod), false }; + if (FInfo1.fn_nargs == 2) { + callInfo1.args[1] = { Int32GetDatum(typeMod), false }; + } else { + callInfo1.args[1] = { ObjectIdGetDatum(TypeIOParam), false }; + callInfo1.args[2] = { Int32GetDatum(typeMod), false }; + } } void* freeMem = nullptr; diff --git a/ydb/library/yql/parser/pg_wrapper/functions.md b/ydb/library/yql/parser/pg_wrapper/functions.md index 6676298289..dd7effdd58 100644 --- a/ydb/library/yql/parser/pg_wrapper/functions.md +++ b/ydb/library/yql/parser/pg_wrapper/functions.md @@ -754,7 +754,7 @@ unistr('d\u0061t\U00000061') → data ```||
|#
-9.5. Binary String Functions and Operators
+# 9.5. Binary String Functions and Operators
This section describes functions and operators for examining and manipulating binary strings, that is values of type bytea. Many of these are equivalent, in purpose and syntax, to the text-string functions described in the previous section.
SQL defines some string functions that use key words, rather than commas, to separate arguments. Details are in Table 9.11. PostgreSQL also provides versions of these functions that use the regular function invocation syntax (see Table 9.12).
@@ -929,3 +929,110 @@ Decodes binary data from a textual representation; supported format values are t decode('MTIzAAE=', 'base64') → \x3132330001
```||
|#
+
+# 9.6. Bit String Functions and Operators
+This section describes functions and operators for examining and manipulating bit strings, that is values of the types bit and bit varying. (While only type bit is mentioned in these tables, values of type bit varying can be used interchangeably.) Bit strings support the usual comparison operators shown in Table 9.1, as well as the operators shown in Table 9.14.
+
+Table 9.14. Bit String Operators
+
+#|
+||Operator|Description|Example(s)||
+bit \|\| bit → bit |
+Concatenation |
+```sql
+B'10001' || B'011' → 10001011
+```||
+||bit & bit → bit|
+Bitwise AND (inputs must be of equal length)|
+```sql
+B'10001' & B'01101' → 00001
+```||
+||bit \| bit → bit|
+Bitwise OR (inputs must be of equal length)|
+```sql
+B'10001' | B'01101' → 11101
+```||
+||bit # bit → bit|
+Bitwise exclusive OR (inputs must be of equal length)|
+```sql
+B'10001' # B'01101' → 11100
+```||
+||~ bit → bit|
+Bitwise NOT|
+```sql
+~ B'10001' → 01110
+```||
+||bit << integer → bit|
+Bitwise shift left (string length is preserved)|
+```sql
+B'10001' << 3 → 01000
+```||
+||bit >> integer → bit|
+Bitwise shift right (string length is preserved)|
+```sql
+B'10001' >> 2 → 00100
+```||
+|#
+
+Some of the functions available for binary strings are also available for bit strings, as shown in Table 9.15.
+
+Table 9.15. Bit String Functions
+
+#|
+||Function|Description|Example(s)||
+||bit_count ( bit ) → bigint|
+Returns the number of bits set in the bit string (also known as “popcount”).|
+```sql
+bit_count(B'10111') → 4
+```||
+||bit_length ( bit ) → integer|
+Returns number of bits in the bit string. (NOT SUPPORTED)|
+```sql
+#bit_length(B'10111') → 5
+```||
+||length ( bit ) → integer|
+Returns number of bits in the bit string.|
+```sql
+length(B'10111') → 5
+```||
+||octet_length ( bit ) → integer|
+Returns number of bytes in the bit string.|
+```sql
+octet_length(B'1011111011') → 2
+```||
+||overlay ( bits bit PLACING newsubstring bit FROM start integer [ FOR count integer ] ) → bit|
+Replaces the substring of bits that starts at the start'th bit and extends for count bits with newsubstring. If count is omitted, it defaults to the length of newsubstring.|
+```sql
+overlay(B'01010101010101010' placing B'11111' from 2 for 3) → 0111110101010101010
+```||
+||position ( substring bit IN bits bit ) → integer|
+Returns first starting index of the specified substring within bits, or zero if it's not present.|
+```sql
+position(B'010' in B'000001101011') → 8
+```||
+||substring ( bits bit [ FROM start integer ] [ FOR count integer ] ) → bit|
+Extracts the substring of bits starting at the start'th bit if that is specified, and stopping after count bits if that is specified. Provide at least one of start and count.|
+```sql
+substring(B'110010111111' from 3 for 2) → 00
+```||
+||get_bit ( bits bit, n integer ) → integer|
+Extracts n'th bit from bit string; the first (leftmost) bit is bit 0.|
+```sql
+get_bit(B'101010101010101010', 6) → 1
+```||
+||set_bit ( bits bit, n integer, newvalue integer ) → bit|
+Sets n'th bit in bit string to newvalue; the first (leftmost) bit is bit 0.|
+```sql
+set_bit(B'101010101010101010', 6, 0) → 101010001010101010
+```||
+|#
+
+In addition, it is possible to cast integral values to and from type bit. Casting an integer to bit(n) copies the rightmost n bits. Casting an integer to a bit string width wider than the integer itself will sign-extend on the left. Some examples:
+
+```sql
+44::bit(10) → 0000101100
+44::bit(3) → 100
+cast(-44 as bit(12)) → 111111010100
+'1110'::bit(4)::integer → 14
+```
+Note that casting to just “bit” means casting to bit(1), and so will deliver only the least significant bit of the integer.
diff --git a/ydb/library/yql/providers/common/mkql/yql_provider_mkql.cpp b/ydb/library/yql/providers/common/mkql/yql_provider_mkql.cpp index 5be252e01c..7ba9958887 100644 --- a/ydb/library/yql/providers/common/mkql/yql_provider_mkql.cpp +++ b/ydb/library/yql/providers/common/mkql/yql_provider_mkql.cpp @@ -2484,7 +2484,10 @@ TMkqlCommonCallableCompiler::TShared::TShared() { } auto typeMod1 = typeMod; - if (node.GetTypeAnn()->Cast<TPgExprType>()->GetName() != "interval" && node.GetTypeAnn()->Cast<TPgExprType>()->GetName() != "_interval") { + if (node.GetTypeAnn()->Cast<TPgExprType>()->GetName() != "interval" && + node.GetTypeAnn()->Cast<TPgExprType>()->GetName() != "_interval" && + node.GetTypeAnn()->Cast<TPgExprType>()->GetName() != "bit" && + node.GetTypeAnn()->Cast<TPgExprType>()->GetName() != "_bit" ) { typeMod1 = TRuntimeNode(); } @@ -2565,7 +2568,10 @@ TMkqlCommonCallableCompiler::TShared::TShared() { } auto typeMod1 = typeMod; - if (node.GetTypeAnn()->Cast<TPgExprType>()->GetName() != "interval" && node.GetTypeAnn()->Cast<TPgExprType>()->GetName() != "_interval") { + if (node.GetTypeAnn()->Cast<TPgExprType>()->GetName() != "interval" && + node.GetTypeAnn()->Cast<TPgExprType>()->GetName() != "_interval" && + node.GetTypeAnn()->Cast<TPgExprType>()->GetName() != "bit" && + node.GetTypeAnn()->Cast<TPgExprType>()->GetName() != "_bit") { typeMod1 = TRuntimeNode(); } diff --git a/ydb/library/yql/sql/pg/pg_sql.cpp b/ydb/library/yql/sql/pg/pg_sql.cpp index 1b45508b0b..fa478e3343 100644 --- a/ydb/library/yql/sql/pg/pg_sql.cpp +++ b/ydb/library/yql/sql/pg/pg_sql.cpp @@ -82,7 +82,7 @@ const char* StrFloatVal(const Value& node) { } const char* StrVal(const Value& node) { - Y_ENSURE(node.type == T_String); + Y_ENSURE(node.type == T_String || node.type == T_BitString); return strVal(&node); } @@ -102,7 +102,7 @@ const char* StrFloatVal(const Node* node) { } const char* StrVal(const Node* node) { - Y_ENSURE(node->type == T_String); + Y_ENSURE(node->type == T_String || node->type == T_BitString); return strVal((const Value*)node); } @@ -116,7 +116,8 @@ bool ValueAsString(const Value& val, TString& ret) { ret = StrFloatVal(val); return true; } - case T_String: { + case T_String: + case T_BitString: { ret = StrVal(val); return true; } @@ -217,6 +218,7 @@ public: numeric, text, unknown, + bit, nil, }; @@ -232,6 +234,8 @@ public: return "text"; case TPgConst::Type::unknown: return "unknown"; + case TPgConst::Type::bit: + return "bit"; case TPgConst::Type::nil: return "unknown"; } @@ -2665,6 +2669,11 @@ public: pgConst.type = TPgConst::Type::unknown; // to support implicit casts return pgConst; } + case T_BitString: { + pgConst.value = ToString(StrVal(val)); + pgConst.type = TPgConst::Type::bit; + return pgConst; + } case T_Null: { pgConst.type = TPgConst::Type::nil; return pgConst; @@ -2716,13 +2725,12 @@ public: } switch (NodeTag(val)) { - case T_Integer: { - return L(A("PgConst"), QA(valueNType->value.GetRef()), pgTypeNode); - } + case T_Integer: case T_Float: { return L(A("PgConst"), QA(valueNType->value.GetRef()), pgTypeNode); } - case T_String: { + case T_String: + case T_BitString: { return L(A("PgConst"), QAX(valueNType->value.GetRef()), pgTypeNode); } case T_Null: { diff --git a/ydb/library/yql/sql/pg/pg_sql_autoparam_ut.cpp b/ydb/library/yql/sql/pg/pg_sql_autoparam_ut.cpp index 53cc47b564..9cd4b4fb3d 100644 --- a/ydb/library/yql/sql/pg/pg_sql_autoparam_ut.cpp +++ b/ydb/library/yql/sql/pg/pg_sql_autoparam_ut.cpp @@ -225,7 +225,7 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) { Y_UNIT_TEST(AutoParamConsts_Select) { TString query = R"( - select 1, 'test' + select 1, 'test', B'10001' )"; TString expectedParamJsonInt4 = R"( {"type":{"pg_type": {"oid": 23}}, @@ -235,15 +235,24 @@ Y_UNIT_TEST_SUITE(PgSqlParsingAutoparam) { {"type":{"pg_type": {"oid": 705}}, "value":{"text_value": "test"}} )"; + TString expectedParamJsonBit = R"( + {"type":{"pg_type": {"oid": 1560}}, + "value":{"text_value": "b10001"}} + )"; THashSet<TString> enabledScopes {"SELECT"}; const TUsedParamsGetter dummyGetter = [] (TSet<TString>& usedParams, const NYql::TAstNode&) { - usedParams = {"a0", "a1"}; + usedParams = {"a0", "a1", "a2"}; }; TMap<TString, TString> expectedParamTypes { {"a0", "(PgType 'int4)"}, {"a1", "(PgType 'unknown)"}, + {"a2", "(PgType 'bit)"}, }; - TestAutoParam(query, {{"a0", expectedParamJsonInt4}, {"a1", expectedParamJsonText}}, expectedParamTypes, dummyGetter, enabledScopes); + TestAutoParam(query, { + {"a0", expectedParamJsonInt4}, + {"a1", expectedParamJsonText}, + {"a2", expectedParamJsonBit}, + }, expectedParamTypes, dummyGetter, enabledScopes); } } |