Bigsql like predicate error - bigsql

I am writing sql query for bigsql.
If it looks like this
select t.city from table t where t.city like 'A%'
It works ok, but next one fails:
select t.city from table t where t.city like 'A%' escape '\'
I only add escape expression and it give me following error
Error Code: -5199, SQL State: 57067] DB2 SQL Error: SQLCODE=-5199, SQLSTATE=57067, SQLERRMC=Java DFSIO;1;2, DRIVER=4.15.82
I found this documentation http://www-01.ibm.com/support/knowledgecenter/SSPT3X_2.1.2/com.ibm.swg.im.infosphere.biginsights.bigsql.doc/doc/bsql_like_predicate.html?lang=en
So seems escape should work.
If I escape escape character I get another error
Error Code: -130, SQL State: 22019] DB2 SQL Error: SQLCODE=-130, SQLSTATE=22019, SQLERRMC=null, DRIVER=4.15.82. 2) [Error Code: -727, SQL State: 56098] DB2 SQL Error: SQLCODE=-727, SQLSTATE=56098, SQLERRMC=2;-130;22019;, DRIVER=4.15.82
But if I use not '\' character as escape, but something another, like '/' it works fine.
Any ideas why it may happen?

Try this maybe. You might have to escape the escape character.
select t.city from table t where t.city like 'A%' escape '\\'

Based upon this sample:
\connect bigsql
drop table if exists stack.issue1;
create hadoop table if not exists stack.issue1 (
f1 integer,
f2 integer,
f3 varchar(200),
f4 integer
)
stored as parquetfile;
insert into stack.issue1 (f1,f2,f3,f4) values (0,0,'Detroit',0);
insert into stack.issue1 (f1,f2,f3,f4) values (1,1,'Mt. Pleasant',1);
insert into stack.issue1 (f1,f2,f3,f4) values (2,2,'Marysville',2);
insert into stack.issue1 (f1,f2,f3,f4) values (3,3,'St. Clair',3);
insert into stack.issue1 (f1,f2,f3,f4) values (4,4,'Port Huron',4);
select * from stack.issue1;
select * from stack.issue1 where f3 like 'M%';
\quit
I get the following results:
jsqsh --autoconnect --input-file=./t.sql --output-file=t.out
0 rows affected (total: 0.28s)
0 rows affected (total: 0.22s)
1 row affected (total: 0.37s)
1 row affected (total: 0.35s)
1 row affected (total: 0.38s)
1 row affected (total: 0.35s)
1 row affected (total: 0.35s)
5 rows in results(first row: 0.33s; total: 0.33s)
2 rows in results(first row: 0.26s; total: 0.26s)
cat t.out
+----+----+--------------+----+
| F1 | F2 | F3 | F4 |
+----+----+--------------+----+
| 1 | 1 | Mt. Pleasant | 1 |
| 0 | 0 | Detroit | 0 |
| 4 | 4 | Port Huron | 4 |
| 3 | 3 | St. Clair | 3 |
| 2 | 2 | Marysville | 2 |
+----+----+--------------+----+
+----+----+--------------+----+
| F1 | F2 | F3 | F4 |
+----+----+--------------+----+
| 1 | 1 | Mt. Pleasant | 1 |
| 2 | 2 | Marysville | 2 |
+----+----+--------------+----+
This shows your syntax is correct, however, based upon the -5199 error code, this is an issue with the FMP processes not having enough memory or an issue with the Hadoop I/O component. You can get further information on this error by issuing
db2 ? sql5199n
from the command line.
The SQL error message should have directed you to the node where the error occurred and where the Big SQL log file is and the associated reader log files are located.

SQL5199 error generally means an issue with HDFS ( you can do a db2 \? SQL5199 to get details on the message -- as user bigsql ). Check the bigsql and DFS logs to see if that gives any pointers to the problem.
Hope this helps.

Related

SQL code to call all the columns from a Tb expect one column which can be called using As?

My current code is given below. I wanted to call all the columns from the table using * but the idcastncrew column name should display like castncrewid. In the requirement code, it's not working though, I wish there was a solution for my requirement such as the sample Requirement code.
Current code:-
SELECT idcastncrew AS castncrewid,castncrewname,castncrewtype,castncrewrole,imagelink,vendor,mode FROM subscriber;
Requirement :-
SELECT idcastncrew AS castncrewid, * FROM subscriber;
The closest I think you can get is to have the renamed column twice, once with the new name and once with the old name.
While MySQL does not allow * after an aliased column (causing your second code snippet to give an error), it does allow table.* anywhere...
SELECT idcastncrew AS castncrewid, subscriber.*
FROM subscriber;
To re-iterate; you'll still get a idcastncrew column, but you will ALSO get a castncrewid column.
There is no way to say don't include *this* column when using * in MySQL
https://dbfiddle.uk/?rdbms=mysql_5.7&fiddle=c69c537e46ad29e3c0c8c03d3ebd1bf7
You can alias columns when you alias the table, example as follows
MariaDB [DEV]> create table xxx (id int, str varchar(20));
MariaDB [DEV]> insert into xxx values (1, 'hi');
MariaDB [DEV]> insert into xxx values (2, 'Hello');
MariaDB [DEV]> insert into xxx values (3, 'World');
MariaDB [DEV]> insert into xxx values (4, 'Goodbye');
MariaDB [DEV]> select a.id as id1, a.* from xxx a order by 1;
+------+------+---------+
| id1 | id | str |
+------+------+---------+
| 1 | 1 | hi |
| 2 | 2 | Hello |
| 3 | 3 | World |
| 4 | 4 | Goodbye |
+------+------+---------+

importing CSV file in Google cloud sql only shows 1 row

I am trying to import a csv file that contains 5 columns and 100 rows. When I open the terminal and type SELECT * FROM cmc2 it only shows 1 row.
mysql> SELECT * FROM cmc2;
+--------------------------------+-------+------------+--------------------+--------+
| alt_name | price | market_cap | circulating_supply | volume |
+--------------------------------+-------+------------+--------------------+--------+
| Alt coin name;Price;Market cap | 554 | 714 | 630 | NULL |
+--------------------------------+-------+------------+--------------------+--------+
1 row in set (0.01 sec)
this is my table that i created:
mysql> create table cmc2 (alt_name varchar(30), price int(20), market_cap int(20), circulating_supply int(20), volume int(20));
As per the documentation "CSV files must have one line for each row of data and have comma-separated fields." So you should replace the semicolons in your file with commas.
This should be an easy task if you have a tool like Word, Excel, etc... Where you can do a search (Control + F) and select replace with... then import the CSV file with the required separator

mysql On Duplicate value in field, insert new row with new value

I want to add a new record in a table if duplicate value enters in a unique field. I don't want to update the existing one but want to add a new record by modifying the unique field value.
Is this possible in mysql?
EDIT:
Edited after user comment on this post:
You need write table locking on both of those two processes.
A WRITE lock has the following features:
The only session that holds the lock of a table can read and write data from the table.
Other sessions cannot read data from and write data to the table until the WRITE lock is released.
Also look at SQL UNIQUE Constraint
BEFORE EDIT:
Yes it is possible. And it took me awhile to figure it out. I build this on your input and compering values as test1, test2 etc, where test is always the same and has trailing number. As you specified.
It can be done as MySQL TRANSACTION in 4 steps.
Lets say you have table testT where name is unique to insure we have no doubles.
| id | name |
| --- | ----- |
| 1 | test1 |
| 2 | test3 |
And you want to insert a new item with name test1 we set is as:
SET #newName = 'test1';
Then we need to check if it already exists in table:
SELECT #check:=COUNT(*) FROM testT WHERE name = #newName;
We do a count here to get true or false and save it as #check here so we can compare it later. This will result into 1 row as test1 already exists in table.
Next we do another selection to get the highest number of test* and store it as #number, this next query selects all tests and does a SUBSTRING after 4 latter's giving us all numbers after first 4 latter's. (99999999999) numbers actually just to be sure we don't miss any but in our case result is only "3" because that is last record "test3" in table.
SELECT
#number:= SUBSTRING(name,5,99999999999)
FROM testT;
Now we can do an insert:
INSERT INTO testT(name)
VALUES
(
IF(#check = "", #newName , CONCAT(LEFT(#newName,4),RIGHT(#number,1)+1)
)
);
This tries to insert our #newName into table under IF condition, and that is if our #check is empty then he will insert #newName, if not it will take word test out of string and append a highest #number from earlier and add + 1 too it.
So result for #newName = 'test1' is below. If you change this into #newName = 'test3' result wold be same new insert test4.
**Schema (MySQL v5.7)**
SET #newName = 'test1';
---
**Query #1**
SELECT * FROM testT
ORDER BY id;
| id | name |
| --- | ----- |
| 1 | test1 |
| 2 | test3 |
| 3 | test4 |
---
And if you change it in ANY test* that number does not already exists it will insert it normally. In case below: #newName = 'test6'
SET #newName = 'test6';
**Query #1**
SELECT * FROM testT
ORDER BY id;
| id | name |
| --- | ----- |
| 1 | test1 |
| 2 | test3 |
| 3 | test6 |
This way an insert will always be made.
You can play with this here : View on DB Fiddle just by changing SET #newName = 'test6'
I am no expert and it took me couple of hours to figure this way out, as I wanted to know if this was even possible.
And I would appreciate if any other user can suggestion any other way or improve my method.

Copying all entries from one table to another with a hard coded value

I currently am copying multiple tables from different mysql schemas into 1 table. While trying to copy all of the entries, I am having issues with the "Insert" into temp table.
cua010.doc_table
| ID | _FilePath |
testing.temp_entries
| ID | File | Schema |
Here is my query
INSERT INTO testing.temp_entries (File, Schema )
SELECT _FilePath, 'CU010'
FROM cua010.doc_table
In the end I would like to results to be
| ID | File | Schema |
| 1 | test | cua010 |
| 2 | test2| cua010 |...
This is the error message i get
0 84 14:49:47 INSERT INTO testing.temp_entries (File, Schema )
SELECT _FilePath, 'cua010'
FROM cua010.doc_table Error Code: 1064. 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 'Schema)
SELECT _FilePath, 'cua010'
FROM cua010.doc_table' at line 1 0.031 sec
use "`" around schema (alt +96 in windows)
INSERT INTO testing.temp_entries (File, `Schema` )
SELECT _FilePath, 'cua010'
FROM cua010.doc_table ;
Schema is a reserved word.
INSERT INTO `testing`.`temp_entries` (`File`, `Schema` )
SELECT `_FilePath`, 'CU010'
FROM `cua010`.`doc_table`

Uderstanding DECODE and ENCODE in MYSQL

I needed to save id in such a way that would make it encrypted and so that it would take up exactly 8 characters.
I did that using the following command:
SELECT encode(LPAD(id,4,0),'abc')
This command turns an id = 1
into 0001 and then turns that into a code fe5ab21a
How do I decrypt this code?
Below is an example of a select and the result it generates
SELECT 0001
, DECODE(ENCODE('0001', 'abc'), 'abc')
, UNHEX(DECODE(ENCODE('0001', 'abc'), 'abc'))
, ENCODE('0001','abc')
, DECODE('fe5ab21a', 'abc')
, UNHEX(DECODE('fe5ab21a', 'abc'))
, HEX('0001')
The result:
1
30303031
0001
fe5ab21a
68d357a7005dcbe0
NULL
30303031
As I understand, you want to make like a short unique key-pass that's linked to one of your ID.
Be careful with this kind of encryption, if someone want to break it, it'll take less than a few minutes, it's really weak. But if it's only for some obfuscation without security risk, it's ok.
To improve a bit the security (but decrease the user experience), try to use AES_ENCRYPT()
The wrong thing with your code is that you don't force the type of input/output, here is something with the proper typing.
SQL Fiddle
MySQL 5.6 Schema Setup:
CREATE TABLE t
(`id` int, `name` varchar(7))
;
INSERT INTO t
(`id`, `name`)
VALUES
(1, 'hello'),
(2, 'hola'),
(3, 'bonjour')
;
Query 1:
select *, ENCODE(id,'KEY') as encrypted_id from t
Results:
| id | name | encrypted_id |
|----|---------|--------------|
| 1 | hello | Vw== |
| 2 | hola | yw== |
| 3 | bonjour | iA== |
Query 2:
SELECT * from t where id = CAST(DECODE(FROM_BASE64('yw=='),'KEY') AS CHAR(50))
Results:
| id | name |
|----|------|
| 2 | hola |