apache drill query with SQL like column name - apache-drill

I have a problem in querying a CSV (with header) file using Drill.
If I run the following code:
SELECT Bid, Last FROM table(dfs.`/data/bb_20020201.csv` (type => 'text', fieldDelimiter => ',', extractHeader => true));
I got an error code corresponding to the word "Last". If I query another column instead of Last everything works nice.
I think the problem is because Last is also a SQL command.
I would really appreciate any help on this issue.

It seems Last is reserved keyword in Drill. Enclose it in a back-ticks:
SELECT Bid, `Last` FROM ...
https://drill.apache.org/docs/lexical-structure/#identifiers
Or change identifier quotes to double quotes or brackets if you need:
https://drill.apache.org/docs/lexical-structure/#identifier-quotes
If it helps you, you can create a Jira ticket to add Last to reserved keywords table:
https://drill.apache.org/docs/reserved-keywords/

Related

Select statement not working on column name with possible preserved word

So I have a database filled with information on different columns and am trying some stuff out but started with just doing simple select all statements where a condition is true, such as:
'SELECT * FROM files WHERE ID = 2;'
my table name is called files and one of the columns is called ID which works fine. However, when working with my my column name called 'File', it doesn't work properly and I notice that it is blue like the other preserved words 'SELECT, FROM, and WHERE', so doing something like:
SELECT * FROM files WHERE File = 'example'; doesn't work even if example does exist in there, it just returns a blank result.
Is there a way that I can say that 'File' is a column and should be treated like one without having to re-name it to something else?
Any help would be appreciated! :)
MySQL Classes these as Reserved Words you can read more here: https://dev.mysql.com/doc/refman/8.0/en/keywords.html
To escape these words place ' around the word:
SELECT * FROM file WHERE `file` = 'example';

How to assign aliases to an array of column names with Joomla's quoteName()?

I want to use the AS statement for Aliases in a query.
I use this piece of code:
$query->select($db->quoteName(array('NameInQ as nin', 'Name')));
Anyway I get this error:
'Unknown column 'NameInQ as nin' in 'field list'
NameInQ does exist as a column name in the table. nin should be the alias.
What am I doing wrong?
When you tell Joomla:
$query->select($db->quoteName(array('NameInQ as nin', 'Name')));
echo $query->dump(); will tell you:
SELECT `NameInQ as nin`,`Name`
See how it doesn't know how to differentiate an aliased column name from a string with spaces in it?
The Docs: https://api.joomla.org/cms-3/classes/JDatabaseQuery.html#method_quoteName
If you want to assign aliases to column names in Joomla from within the qn() / quoteName() method, you will need to nominate corresponding aliases for all columns.
$query->select($db->quoteName(array('NameInQ', 'Name'), array('nin', 'Name')));
Renders as:
SELECT `NameInQ` AS `nin`,`Name` AS `Name`
// ^-------^----^---^-^----^----^----^-- everything aliased, everything backtick wrapped
Or, of course you could individualize the quoteName() calls, you can avoid aliasing every column.
$query->select(array($db->quoteName('NameInQ', 'nin'), $db->quoteName('Name')));
Renders as:
SELECT `NameInQ` AS `nin`,`Name`
Finally, the truth is: You don't even need to quote any of your sample column names because the query will be stable/secure without the extra method call(s). *I recommend leaving them out to minimize query bloat and developer eye-strain.
$query->select(array('NameInQ AS nin', 'Name'));
or even in raw form:
$query->select('NameInQ AS nin, Name');
For the record, Name (MYSQL is case-insensitive) IS a KEYWORD, but it is not a RESERVED KEYWORD.
See the MySQL Doc: https://dev.mysql.com/doc/refman/5.5/en/keywords.html#keywords-5-5-detailed-N (there is no "(R)" beside Name

How can I order a mySQL column by a number imbedded in a string?

I am parsing genomic positions from a MySQL field. The field is called "change" and the entries are of the form:
g.100214985T>C
g.100249769C>A
g.10185G>T
I am trying to order the field by the numerical portion of the string. I am trying to figure out what mySQL query I can use to accomplish this. I have tried using REGEXPs and SUBSTRING_INDEX but am still running into issues. Any help would be much appreciated!
Assuming you have always 2 characters in front of and 3 at the end you need to have removed:
SELECT CAST(SUBSTR(col from 3) AS UNSIGNED) AS value
FROM `my_table`
ORDER BY value
Watch this sql fiddle also: http://sqlfiddle.com/#!2/7bc0e/67
Thank you #MarcusAdams and #amoudhgz! The following code works:
CAST(SUBSTR(field, 3) AS UNSIGNED).
MySQL already stops the conversion at the first non-numerical character.

Upgrade to Rails 4.2.0: string literals in where conditions wrapped into quotation marks

During an upgrade of rails version in my application from 4.1.8 to 4.2.0, I have encountered the following issue.
String literals in where conditions are now additionally wrapped into quotation marks, which
then become part of a query string, delivering no valid results anymore. This happens only for database fields of a text type (varchar fields are not affected). I am using a MySQL database.
> Table.where(column: 'data')
[08:19:20.822552] Table Load (0.3ms) SELECT `table`.* FROM `table`
WHERE `table`.`column` = '\"data\"'
Now, if you have a row containing data value in a column row, this condition will no longer match (obviously, "data" is not a match anymore).
In Rails 4.1.8 everything worked perfectly fine:
> Table.where(column: 'data')
[08:19:58.303366] Table Load (0.4ms) SELECT `table`.* FROM `table`
WHERE `table`.`column` = 'data'
I don't know if this is a default or a configurable behaviour. I somehow haven't found a corresponding release note on that. I would be very grateful for any suggestions on what has changed and what is the best way to fix it.
Many thanks for help!
Could you try this way:
Table.where("column=?", 'data')
I guess this will work.

mysql: replace \ (backslash) in strings

I am having the following problem:
I have a table T which has a column Name with names. The names have the following structure:
A\\B\C
You can create on yourself like this:
create table T ( Name varchar(10));
insert into T values ('A\\\\B\\C');
select * from T;
Now if I do this:
select Name from T where Name = 'A\\B\C';
That doesn't work, I need to escape the \ (backslash):
select Name from T where Name = 'A\\\\B\\C';
Fine.
But how do I do this automatically to a string Name?
Something like the following won't do it:
select replace('A\\B\C', '\\', '\\\\');
I get: A\\\BC
Any suggestions?
Many thanks in advance.
You have to use "verbatim string".After using that string your Replace function will
look like this
Replace(#"\", #"\\")
I hope it will help for you.
The literal A\\B\C must be coded as A\\\\A\\C, and the parameters of replace() need escaping too:
select 'A\\\\B\\C', replace('A\\\\B\\C', '\\', '\\\\');
output (see this running on SQLFiddle):
A\\B\C A\\\\B\\C
So there is little point in using replace. These two statements are equivalent:
select Name from T where Name = replace('A\\\\B\\C', '\\', '\\\\');
select Name from T where Name = 'A\\\\B\\C';
Usage of regular expression will solve your problem.
This below query will solve the given example.
1) S\\D\B
select * from T where Name REGEXP '[A-Z]\\\\\\\\[A-Z]\\\\[A-Z]$';
if incase the given example might have more then one char
2) D\\B\ACCC
select * from T where Name REGEXP '[A-Z]{1,5}\\\\\\\\[A-Z]{1,5}\\\\[A-Z]{1,5}$';
note: i have used 5 as the max occurrence of char considering the field size is 10 as its mentioned in the create table query.
We can still generalize it.If this still has not met your expectation feel free to ask for my help.
You're confusing what's IN the database with how you represent that data in SQL statements. When a string in the database contains a special character like \, you have to type \\ to represent that character, because \ is a special character in SQL syntax. You have to do this in INSERT statements, but you also have to do it in the parameters to the REPLACE function. There are never actually any double slashes in the data, they're just part of the UI.
Why do you think you need to double the slashes in the SQL expression? If you're typing queries, you should just double the slashes in your command line. If you're generating the query in a programming language, the best solution is to use prepared statements; the API will take care of proper encoding (prepared statements usually use a binary interface, which deals with the raw data). If, for some reason, you need to perform queries by constructing strings, the language should hopefully provide a function to escape the string. For instance, in PHP you would use mysqli_real_escape_string.
But you can't do it by SQL itself -- if you try to feed the non-escaped string to SQL, data is lost and it can't reconstruct it.
You could use LIKE:
SELECT NAME FROM T WHERE NAME LIKE '%\\\\%';
Not exactly sure by what you mean but, this should work.
select replace('A\\B\C', '\', '\\');
It's basically going to replace \ whereever encountered with \\ :)
Is this what you wanted?