Conversion of bigint unsigned column to bigint signed fails - mariasql

I get a syntax error in a MySql database version 7.0
SELECT
r.id,
r.number,
r.numbertype,
r.forhandler,
LAG(r.number) OVER (PARTITION BY r.numbertype ORDER BY r.number) AS last_row_number,
LEAD(r.number) OVER (PARTITION BY r.numbertype ORDER BY r.number) AS next_row_number,
r.number -(LAG(r.number) OVER (PARTITION BY r.numbertype ORDER BY r.number)) AS gap_last_rk,
CAST (r.number-(LEAD(r.number) OVER (PARTITION BY r.numbertype ORDER BY r.`number`)) AS BIGINT SIGNED) AS gap_next_rk
FROM admin.numberranges r
WHERE r.status=2
ORDER BY r.number;
The syntax error is in my CAST part. My column NUMBER that is a BIG INT UNSIGNED.
I tried convert as well -:(
Error Code: 1064
You have an error in your SQL syntax; check the manual that corresponds to your MariaDB server version for the right syntax to use near 'BIGINT SIGNED) AS neg_number

First, you have a space after CAST, which can result in other parse errors/issues with your question. You have to use CAST(...). Second, the type BIGINT SIGNED is not allowed, check the list for CAST(expr AS type). When you want a signed number you use the type SIGNED or SIGNED INTEGER, as described in the documentation:
The type can be one of the following values:
[...]
SIGNED [INTEGER]
See the following queries on how to use the CAST() function (examples run on MySQL 8.0.23, the result might not be the same for MariaDB but the type restrictions are similar, see the MySQL documentation of CONVERT(expr, type)):
mysql> EXPLAIN Dummy;
+-------+-----------------+------+-----+---------+-------+
| Field | Type | Null | Key | Default | Extra |
+-------+-----------------+------+-----+---------+-------+
| Test | bigint unsigned | YES | | NULL | |
+-------+-----------------+------+-----+---------+-------+
1 row in set (0.01 sec)
mysql> SELECT Test, CAST(Test AS BIGINT SIGNED) FROM Dummy;
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that
corresponds to your MySQL server version for the right syntax
to use near 'BIGINT SIGNED) FROM Dummy' at line 1
mysql> SELECT Test, CAST(Test AS SIGNED) FROM Dummy;
+------+----------------------+
| Test | CAST(Test AS SIGNED) |
+------+----------------------+
| 1234 | 1234 |
+------+----------------------+
1 row in set (0.00 sec)

Related

Azure Kusto - how to parse a string looking for the last node?

I am writing kusto queries to analyze the state of the database when simple queries run for a long time.
For ex: data and type = SQL in dependencies is a sql server query. If its duration at timestamp 2019-06-24T16:41:24.856 is >= 15000 (>= 15 secs)
I would like to query and analyze the dtu_consumption_percent out of AzureMetrics from 2019-06-24T16:40:24.856 to 2019-06-24T16:42:24.856. ( 1 min before and 1 min after the query completion time) to determine the state of the database at that point in time.
Question: I wonder if anyone can give me pointers on getting the database name out of the target column from dependencies?
target looks as below:
tcp:sqlserver-xxx-xxxxxx.database.windows.net | DDDDD
and I am needing to extract DDDDD to join to AzureMetrics column Resource.
Thank you!
As Yoni says you can use parse, or you could use substring:
let T = datatable(Value:string) [
'tcp:sqlserver-xxx-xxxxxx.database.windows.net | DDDDD',
'udp:appserver-yyy-yyyyyy.database.contoso.com | EEEEE'
];
T
// Look for the pipe and take everything after it as the value
| extend ToSubstring = substring(Value, indexof(Value, "|")+1)
https://learn.microsoft.com/en-us/azure/kusto/query/substringfunction
However if you find yourself doing this a lot you may want to take a look at Custom Fields:
https://learn.microsoft.com/en-us/azure/azure-monitor/platform/custom-fields
You could use the parse operator:
https://learn.microsoft.com/en-us/azure/kusto/query/parseoperator
print value = 'tcp:sqlserver-xxx-xxxxxx.database.windows.net | DDDDD'
| parse value with * "| " database
this returns:
| value | database |
|-------------------------------------------------------|----------|
| tcp:sqlserver-xxx-xxxxxx.database.windows.net | DDDDD | DDDDD |

SparkSQL Column Query not showing column contents?

I have created a persistant table via df.saveAsTable
When I run the following query I receive these results
spark.sql("""SELECT * FROM mytable """).show()
I get view of the DataFrame and all of it's columns, and all of the data.
However when I run
spark.sql("""SELECT 'NameDisplay' FROM mytable """).show()
I receive results that look like this
| NameDisplay|
|--|
| NameDisplay |
| NameDisplay |
| NameDisplay |
| NameDisplay |
| NameDisplay |
| NameDisplay |
NameDisplay is definitely one of the columns in the table as it's shown when I run select * - how come this is not shown in the second query?
Issue was using quotes on the column names. Needs to be escaped via backtick ``NameDisplay`
Selecting 'NameDisplay', in SQL, is selecting the literal, text "NameDisplay". In that, the result you got are in fact valid.
To select values of the "NameDisplay" column, then you must issue:
"SELECT NameDisplay FROM mytable "
Or, if you need to quote it (maybe in case the column was created like this or has spaces, or is case-sensitive):
"""SELECT `NameDisplay` FROM mytable"""
This is SQL syntax, nothing specific to Spark.

How to do negation for 'CONTAINS'

I have Cassandra table with one column defined as set.
How can I achieve something like this:
SELECT * FROM <table> WHERE <set_column_name> NOT CONTAINS <value>
Proper secondary index in was already created.
From the documentation:
SELECT select_expression FROM keyspace_name.table_name WHERE
relation AND relation ... ORDER BY ( clustering_column ( ASC | DESC
)...) LIMIT n ALLOW FILTERING
then later:
relation is:
column_name op term
and finally:
op is = | < | > | <= | > | = | CONTAINS | CONTAINS KEY
So there's no native way to perform such query. You have to workaround by designing a new table to specifically satisfy this query.

Cassandra CQL2: Unable to update a column

Hi i have a columnfamily in cassandra db and when i check the contents of table it is shown differently when used as
./cqlsh -2
select * from table1;
KEY,31281881-1bef-447a-88cf-a227dae821d6 | A,0xaa| Cidr,10.10.12.0/24 | B,0xac | C,0x01 | Ip,10.10.12.1 | D,0x00000000 | E,0xace | F,0x00000000 | G,0x7375626e657431 | H,0x666230363 | I,0x00 | J,0x353839
While output is as this for
./cqlsh -3
select * from table1;
key | Cidr | Ip
--------------------------------------+---------------+------------
31281881-1bef-447a-88cf-a227dae821d6 | 10.10.12.0/24 | 10.10.12.1
This value is inserted using java program running.
I want to suppose update value of coulmn "B" which is only seen when using -2 option manually in database, it gives me error that it is hex value.
I am using this command to update but always getting error
cqlsh:sdnctl_db> update table1 SET B='0x7375626e657431' where key='31281881-1bef-447a-88cf-a227dae821d6';
Bad Request: cannot parse '0x7375626e657431' as hex bytes
cqlsh:sdnctl_db> update table1 SET B=0x7375626e657431 where key='31281881-1bef-447a-88cf-a227dae821d6';
Bad Request: line 1:30 no viable alternative at input 'x7375626e657431'
cqlsh:sdnctl_db> update table1 SET B=7375626e657431 where key='31281881-1bef-447a-88cf-a227dae821d6';
Bad Request: line 1:37 mismatched character '6' expecting '-'
I need to insert the hex value only which will be picked by the application but not able to insert.
Kindly help me in correcting the syntax.
It depends on what data type does column B have. Here is the reference of the CQL data types. The documentation says that blob data is represented as a hexadecimal string, so my assumption is that your column B is also a blob. From this other cassandra question (and answer) you see how to insert strings into blobs.
cqlsh:so> CREATE TABLE test (a blob, b int, PRIMARY KEY (b));
cqlsh:so> INSERT INTO test(a,b) VALUES (textAsBlob('a'), 0);
cqlsh:so> SELECT * FROM test;
b | a
---+------
0 | 0x61
cqlsh:so> UPDATE test SET a = textASBlob('b') WHERE b = 0;
cqlsh:so> SELECT * FROM test;
b | a
---+------
0 | 0x62
In your case, you could convert your hex string to a char string in code, and then use the textAsBlob function of cqlsh.
If the data type of column B is int instead, why don't you convert the hex number to int before executing the insert?

Cassandra CQL not equal operator on any column

Hi is there any way I can use != operator using CQL in Cassandra?
I am trying to use a != operator on my columnfamily but when I try using that it says:
cqlsh:EPCContent> select * from "MediaCategoryGroup" where "MCategoryID"!=1;
I get this error:
Invalid syntax at line 1, char 55
select * from "MediaCategoryGroup" where "MCategoryID"!=1;
^
If you look at the Cassandra SELECT syntax, you will see that the WHERE clause must be "composed of relations on the columns that are part of the PRIMARY KEY and/or have a secondary index defined on them." Does your column conform to that specification?
Just for your information this is the full list of relation operators: '=' | '<' | '>' | '<=' | '>=' | '!=' | IN | CONTAINS | CONTAINS KEY.
Go with <> instead !=. It's working for me.

Resources