Bitwise & operator in Snowflake? - snowflake-cloud-data-platform

I'm stuck with Snowflake about bitwise operators. In MySql it's allowed to use this syntax:
Flag & 1024 > 0
I'd like to make the same filtering in the where clause with Snowflake but I've found only these function on the web:
https://docs.snowflake.com/en/sql-reference/expressions-byte-bit.html
Do you have any ideal how to do the same thing?

The Flag & 1024 > 0 is equivalent of BITWISE AND:
WHERE BITAND(Flag, 1024) > 0;
db<> fiddle demo MySQL
Snowflake:
CREATE TABLE tab(flag INT)
AS
SELECT 1024 AS flag
UNION SELECT 2048
UNION SELECT 1025;
SELECT *
FROM tab
WHERE BITAND(Flag, 1024) > 0;
-- 1024
-- 1025

Related

Store Operators like >= OR <= in Microsoft Access

I have certain values that are needed for validation in Forms, either they look like Value X >= 0 but it could also be X <= 0, it depends on what operator should be used. How can I store such a value?
(I use MS SQL Server + Access as Frontend)
I basicly wanna store the Value and if it needs to be bigger than or smaller than.
Store the value, as usual, in a field, and the operator in another field as Short Text.
The you can use Eval:
Result = Eval("" & [ValueField] & [OperatorField] & "0")
You can store your value as it is, and to check if this value is positive or not you have two ways
First one
Create a computed column to check the Value column as
CREATE TABLE YourTable(
YourValue INT,
IsPositive AS CASE WHEN YourValue < 0 THEN 0 ELSE 1 END
);
INSERT INTO YourTable (YourValue) VALUES
(1), (-1);
SELECT *
FROM YourTable;
Second one
Use a CASE expression (or even you can create a view) as
SELECT CASE WHEN YourValue < 0 THEN 0 ELSE 1 END IsPositive,
--...
FROM YourTable;

Is there anything possible idea of 'cyclic arithmetic operation' for some specific data type?

In integer-based data type like tinybit, smallbit, bigint, int, uint...
sometimes arithmetic overflow could be found when result is out of range of that data type.
The expression I used, 'cyclic arithmetic operation', that means,
(-1) 2 -> 1 -> 0 -> 255 -> 254 ... (tinybit)
or
(+2) 65533 -> 65535 -> 1 -> 3 ... (smallbit)
etc..
In other words, I want 0 - 1 = 255 in tinybit columns.
I know it's little awkward and everyone have avoided this kind of situation but is there anyone who have solution for this? The reason I ask is not only to avoid arithmetic overflow but also practical need. So "consider extending or changing your column data type!" will not be suitable in my case.
Applying a bit mask seems to be the most straightforward way to do this:
SELECT -10 & 255; -- Results in 246
SELECT 513 & 255; -- Results in 1
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/bitwise-operators-transact-sql
SQL Server does have a modulus operator, %, which might help here. Using it, you can do something like this:
DECLARE #inputValue INT = -1;
DECLARE #outputValue TINYINT;
DECLARE #minValue INT = 0;
DECLARE #maxValue INT = 255;
DECLARE #maxValues INT = 256;
SELECT #outputValue =
CASE
WHEN #inputValue < #minValue THEN #inputValue % #maxValues + #maxValues
ELSE #inputValue % #maxValues
END;
SELECT #outputValue; -- Results in 255
https://learn.microsoft.com/en-us/sql/t-sql/language-elements/modulo-transact-sql

Cassandra bitwise operations and operators (&, or, not)

My use case is the following:
I have a table that has a bigint clustering column X. I also have a value Y that is a bitmask in this use case. I want to do the following query
select * from table where key1 = something1 AND key2 = something2 AND (X & Y) = 1
where the & is the bitwise and operation between X and Y. Is this possible in cassandra?
Also does cassandra have and, or, xor and not operators?
No, Cassandra "operators" are limited to >, >=, <= and >
They are used in range queries on the sorted clustering column after having selected a primary key.
What you want would be a sort of filtering, not a range query.
You can read the post here to know more about the WHERE clause: http://www.datastax.com/dev/blog/a-deep-look-to-the-cql-where-clause

PostgreSQL "&" symbol and number after column name

I have psql version 9.2.4.
I am vivewing log file of our database and i found something weird for me.
There are WHERE statements where:
te_flag&1024 = 0
te_flag&5120 <> 1024
I don't know what part "&1024" after column name does.
Can someone explain it?
Thanks!
A single & is the bitwise and operator. E.g.:
db=> SELECT 4 & 2 AS bitwise_4_and_2;
bitwise_4_and_2
-----------------
0
(1 row)

Find Size of a Database in Oracle

I have a database named "My_Enterprise_Data". I need to find the size that it occupies on the disk.
How do I find it out?
Is the query, SELECT sum(bytes)/1024/1024 AS "Size in MB" FROM user_segments run against the My_Enterprise_Data correct?
The following will show you the data files used by oracle:
select TABLESPACE_NAME "Tablspace",
FILE_NAME "Filename",
BYTES/1024/1024 "Size MB",
MAXBYTES/1024/1024 "Maximum Size MB",
AUTOEXTENSIBLE "Autoextensible"
from SYS.DBA_DATA_FILES
You can then look for the tablespace used by the My_Enterprise_Data schema
An oracle database consists of data files, redo log files, control files, temporary files.
The size of the database actually means the total size of all these files.
select
( select sum(bytes)/1024/1024/1024 data_size from dba_data_files ) +
( select nvl(sum(bytes),0)/1024/1024/1024 temp_size from dba_temp_files ) +
( select sum(bytes)/1024/1024/1024 redo_size from sys.v_$log ) +
( select sum(BLOCK_SIZE*FILE_SIZE_BLKS)/1024/1024/1024 controlfile_size from v$controlfile) "Size in GB"
from
dual
SELECT a.data_size + b.temp_size + c.redo_size + d.controlfile_size
"total_size in GB"
FROM (SELECT SUM (bytes) / 1024 / 1024/1024 data_size FROM dba_data_files) a,
(SELECT NVL (SUM (bytes), 0) / 1024 / 1024/1024 temp_size
FROM dba_temp_files) b,
(SELECT SUM (bytes) / 1024 / 1024/1024 redo_size FROM sys.v_$log) c,
(SELECT SUM (BLOCK_SIZE * FILE_SIZE_BLKS) / 1024 / 1024/1024
controlfile_size
FROM v$controlfile) d;
Great... dba_segments gives the Oracle database size
To find the actual space occupied by the database.
Select sum(bytes)/1024/1024/1024 from dba_segments;

Resources