github.com/matrixorigin/matrixone@v1.2.0/test/distributed/cases/operator/bit_operator.sql (about)

     1  -- @suit
     2  
     3  -- @case
     4  -- @desc:test bit operators such as &, |, ^, ~, <<, >>
     5  -- @label:bvt
     6  
     7  SELECT 1 ^ 1, 9 &4& 2, 1 ^ 0;
     8  SELECT 29 & 15;
     9  SELECT ~0, 64 << 2, '40' << 2;
    10  SELECT 1 << 2;
    11  SELECT 4 >> 2;
    12  SELECT 100 << ABS(-3);
    13  SELECT BIN(~1);
    14  SELECT 3 & ~8;
    15  
    16  DROP TABLE IF EXISTS t1;
    17  CREATE TABLE t1(
    18      tiny TINYINT NOT NULL,
    19      small SMALLINT NOT NULL,
    20      int_test INT NOT NULL,
    21      big BIGINT NOT NULL
    22  );
    23  INSERT INTO t1(tiny, small, int_test, big) VALUES(0, SIN(1), 1, -0);
    24  INSERT INTO t1() VALUES('2', 32767, '1', 9223372036854775807);
    25  SELECT tiny & small FROM t1;
    26  SELECT tiny << small FROM t1;
    27  SELECT tiny >> big FROM t1;
    28  
    29  DROP TABLE IF EXISTS t1;
    30  CREATE TABLE t1(
    31      tiny TINYINT UNSIGNED NOT NULL,
    32      small SMALLINT UNSIGNED NOT NULL,
    33      int_test INT UNSIGNED NOT NULL,
    34      big BIGINT UNSIGNED NOT NULL
    35  );
    36  INSERT INTO t1(tiny, small, int_test, big) VALUES(0, SIN(1)-COS(0), 1, 9223372036854775807);
    37  INSERT INTO t1() VALUES(1, 927, LENGTH('abcd'), 90);
    38  SELECT big >> COS(0) FROM t1;
    39  SELECT tiny & int_test | small ^ big FROM t1;
    40  SELECT tiny << int_test | small >> big FROM t1;
    41  SELECT tiny << LENGTH('abcdefghijklmnopqrst') FROM t1 ORDER BY tiny;
    42  
    43  DROP TABLE IF EXISTS t1;
    44  CREATE TABLE t1(
    45      int_test INT UNSIGNED NOT NULL,
    46      float_32 FLOAT NOT NULL,
    47      float_64 DOUBLE NOT NULL
    48  );
    49  INSERT INTO t1() VALUES(1, 0.0, 123.146484666486456);
    50  INSERT INTO t1() VALUES('99', 0.000001, 1.0);
    51  SELECT int_test & float_32 FROM t1;
    52  SELECT float_32 | float_64 FROM t1;
    53  SELECT float_32 ^ float_64 + 11 FROM t1;
    54  SELECT float_32 >> int_test FROM t1;
    55  --SELECT float_32 >> int_test / 0 FROM t1;
    56  --SELECT float_32 / 0 >> int_test FROM t1;
    57  --SELECT float_32 / (COS(0) - 1.0) >> int_test FROM t1;
    58  
    59  DROP TABLE IF EXISTS t1;
    60  CREATE TABLE t1(
    61      int_test INT UNSIGNED NOT NULL,
    62      float_32 FLOAT NOT NULL,
    63      d1 DECIMAL NOT NULL
    64  );
    65  INSERT INTO t1() VALUES(1, 0.000001, 1.00000000000000000000000001);
    66  INSERT INTO t1() VALUES(YEAR('2022-02-02'), SIN(1), LENGTH('abcdefghijk') - MONTH('2022-09-09'));
    67  SELECT ~float_32 & float_32 FROM t1;
    68  
    69  DROP TABLE IF EXISTS t1;
    70  CREATE TABLE t1(
    71      str1 CHAR(10),
    72      str2 VARCHAR(10)
    73  );
    74  INSERT INTO t1() VALUES('abc', '123');
    75  INSERT INTO t1() VALUES(NULL, 'dc');
    76  SELECT LENGTH(str1) | LENGTH(str2) FROM t1;
    77  SELECT cast(STARTSWITH(str1, 'a') as int) | LENGTH(str2) FROM t1;
    78  DELETE FROM t1;
    79  INSERT INTO t1() VALUES('123', 23), (LENGTH('abc'), 0), ('0', NULL);
    80  SELECT str1 << '1' FROM t1;
    81  SELECT str1 & '0' FROM t1;
    82  --SELECT str1 & '0.0' FROM t1;
    83  SELECT str1 & '11111' | '000101' & BIN(12) FROM t1;
    84  SELECT str1 & '5555' | SPACE(100)+'1' & BIN(16) FROM t1;
    85  SELECT str1 ^ '000000000000000'+'1'+'000000000000000' & '000000000000000'+'1'+'000000000000000' FROM t1;
    86  
    87  DROP TABLE IF EXISTS t1;
    88  DROP TABLE IF EXISTS t2;
    89  CREATE TABLE t1(
    90      str1 VARCHAR(10),
    91      PRIMARY KEY (str1)
    92  );
    93  CREATE TABLE t2(
    94      n1 INT,
    95      PRIMARY KEY (n1)
    96  );
    97  INSERT INTO t1() VALUES('101'),('-1'),(TRUE),(FALSE);
    98  INSERT INTO t2() VALUES(101),(-1),(FALSE),(TRUE);
    99  SELECT str1 & n1 FROM t1,t2 LIMIT 4;
   100  SELECT str1 & ABS(-SIN(7)) FROM t1;
   101  SELECT n1 & str1 & n1 & '111' & n1 & '1001' FROM t1,t2;
   102  
   103  SELECT 1 << n1 FROM t2;
   104  SELECT n1 << n1 >> n1 FROM t2;
   105  SELECT n1 ^ str1 | n1 & str1 >> n1 << str1 FROM t1,t2;
   106  SELECT n1 ^ 1 | n1 & '111' >> n1 << '1001' FROM t1,t2;
   107  
   108  SELECT '0150' | str1 | n1 | '000111' | n1 | '101010' FROM t1,t2;
   109  
   110  -- multi table insert or update with bit operator
   111  DROP TABLE IF EXISTS t1;
   112  DROP TABLE IF EXISTS t2;
   113  CREATE TABLE t1(
   114      id INT,
   115      str1 VARCHAR(10),
   116      PRIMARY KEY (id)
   117  );
   118  CREATE TABLE t2(
   119      id INT,
   120      n1 INT,
   121      PRIMARY KEY (id)
   122  );
   123  INSERT INTO t1() VALUES(1, '1'), (2, 'red'), (3, 'United'), (4, FALSE);
   124  INSERT INTO t2() VALUES(1, 101), (2, 01010), (4, -1);
   125  SELECT str1 | n1 FROM t1, t2 WHERE t1.id = t2.id AND t1.id = 1;
   126  UPDATE t1 JOIN t2 ON t1.id = t2.id SET str1 = n1 << 2;
   127  UPDATE t1,t2 SET str1 = 2 >> str1, n1 = 3 >> 3 WHERE t1.id = 1;
   128  INSERT INTO t1() VALUES(2 << 4, 'shift'), (3 & 0, 'bit');
   129  INSERT INTO t1 SELECT 2 << 8, 'UK';
   130  SELECT * FROM t1;
   131  
   132  DROP TABLE IF EXISTS t1;
   133  DROP TABLE IF EXISTS t2;
   134  CREATE TABLE t1(
   135      id INT,
   136      class VARCHAR(10),
   137      name VARCHAR(10),
   138      PRIMARY KEY (id)
   139  );
   140  CREATE TABLE t2(
   141      id INT,
   142      grade VARCHAR(10),
   143      score FLOAT,
   144      PRIMARY KEY (id)
   145  );
   146  INSERT INTO t1() VALUES(1,'c1','nion'), (2,'c2','unitd'), (3,'c1','jake'), (4,'c2','hadd'), (5,'c3','laik');
   147  INSERT INTO t2() VALUES(1,'A',70.1), (2,'B',59.3), (3,'C',81.2), (4,'B',48.3), (5,'C',99.4);
   148  SELECT id,MAX(score),grade FROM t2 GROUP BY id,grade HAVING id > (2 << 2);
   149  SELECT t1.id, t1.name, t2.grade FROM t1, t2 WHERE t1.id = t2.id AND t1.id < (2 << 2);
   150  SELECT score FROM t2 WHERE t2.score BETWEEN 2 << 5 AND 2 << 8;
   151  
   152  DROP TABLE IF EXISTS t1;
   153  CREATE TABLE t1(
   154      id INT,
   155      str1 VARCHAR(10),
   156      PRIMARY KEY (id)
   157  );
   158  INSERT INTO t1() VALUES(1,'c1'), (2,'11'), (3,'cd'), (4,'df');
   159  SELECT id | BIN(100), id & HEX(100) FROM t1;
   160  INSERT INTO t1(id) VALUES(0 & BIN(4));
   161  INSERT INTO t1(id) VALUES(-1 & HEX(8));
   162  SELECT id << HEX(88), id >> BIN(88) FROM t1;
   163  SELECT HEX(88) & BIN(88), BIN(id) | BIN(88) FROM t1;
   164  
   165  DROP TABLE IF EXISTS t1;
   166  CREATE TABLE t1(
   167      str1 VARCHAR(10),
   168      PRIMARY KEY (str1)
   169  );
   170  INSERT INTO t1() VALUES ('111'), ('222'), ('0'), ('333');
   171  SELECT HEX(str1) & BIN(88), BIN(str1) | HEX(88) FROM t1;
   172  SELECT HEX(str1) & BIN(88) ^ BIN(str1) | HEX(100) FROM t1;
   173  SELECT HEX(str1) >> BIN(88) << BIN(str1) >> HEX(100) FROM t1;
   174  DROP TABLE t1;
   175  select binary(3) & binary(4);
   176  select binary(3) | binary(4);