How do we combine multiple row values into single delimited row using Apache Drill - apache-drill

How do we combine multiple row values into single row using Apache Drill?
Example :
Input:
a
b
c
Output:
a,b,c
In oracle or any other database we have listagg function to achieve this. How do we achieve this using Apache Drill?

Drill hasn't listagg function, since it is not a standard SQL ANSI
function. But you can create your own UDF for this purpose [1].
Also you can contribute to Drill to create this function or similar PIVOT
function.
A case statement along with aggregation might do the trick
[2].
Also consider the similar approach like in Hive [3]
[1] https://drill.apache.org/docs/adding-custom-functions-to-drill-introduction/
[2] http://mail-archives.apache.org/mod_mbox/drill-user/201606.mbox/%3CCAAOiHjHyCajBDVerE9fFbgHUyQ+3wYAcX84ZaoUVbAyj0LpjBw#mail.gmail.com%3E
[3] http://hadoopmania.blogspot.com/2015/12/transposepivot-table-in-hive.html

Related

Extract certain members of JSON array in MySQL

I have a table in MySQL where each row contains JSON returned from another system. The JSON will look something like:
[{"userId": "Dave"},{"userId": "Mary", "errorCode" : "DB Fail"}, {"userId": "Lorenza", "errorCode": "Web Error"}]
and I'm only interested in the members of the array containing an error code. In the future, these will be parsed into seperate rows of their own table, but in the meantime does MySql offer a way to extract only these with an errorCode?
I can use JSON_EXTRACT to extract the errorCodes only
JSON_EXTRACT(jsonData, '$[*].errorCode') AS errorCodes
but I really want the rest of the member (userId in the example above)
You could use the JSON_CONTAINS function to find the records with errorCode and then then use JSON_EXTRACT on those records. Put the JSON_CONTAINS in the where clause
I don't think you could do this with a single query without known boundaries of the number of elements, but you could use a stored procedure to run a loop.
e.g. each iteration runs LOCATE to find the position of "errorCode", and uses that location to run SUBSTR and/or SUBSTRING_INDEX to get the userid value and append it to another variable. The looped variable would just be the offset used in the LOCATE query.

CodeIgniter - use MySQL 5.7 JSON_CONTAINS function

So, I have a list of "whitelisted" countries (eg. ['PT', 'US', 'UK', 'ES']) on a column of a table, and I want to check if the user's country (eg. 'US') is whitelisted, in order to show the user the row's content.
I searched and the best and easiest way I found was to compile the whitelisted list into a JSON array, make the column JSON type and use the JSON_CONTAINS function present on MySQL 5.7+
However, I can't figure out how to implement that with CI's database library.
How can I use CI's DB lib to use MySQL's functions? Would there be a better way to achieve this instead of JSON array?
You can use
$this->db->where("(JSON_CONTAINS(field,'[\"US\"]')) > ",0);
The codeigniter where clause required an operator. If we provide an operator codeigniter will ignore IS NULL

Correct xpath when reading JSON out of MYSQL DB using common_schema.extract_json

I am creating a query to re-use periodically to pull data out of a MySQL database (not for use in production code) and want to display values from a JSON object as columns. I installed common_schema and have been using the extract_json function but I can't find the correct xpath to use to get the field I want, I always get null. The query I am using currently is below:
SELECT common_schema.extract_json_value(stores.info,'/Region') as "Sales Region" FROM stores
An example of the JSON object stored in stores.info is below:
{"Town":"HDM","Post Code":"003408","Region":"FGH","OutletCode":"AB43G","CustomerCode":"15134158"}
What xpath do I need to access, for example Region. If the path is correct why is it returning NULL?

mysql calculation field stored in another field

I have fields in mysql that look like this:
constant1 , constant2, variable1, variable2, formula
The formula field stores a formula utilizing constant1, constant2, variable1 and variable2.
For example, the formula field may contain a calculation like this:
constant1 * variable1
When I use a select statement like this:
SELECT constant1, constant2, variable1, variable2, formula
FROM table
How do I retrieve the calculation result of constant1 * variable1 based on formula field?
You need one of two approaches:
Call some sort of eval function on your formula.
Parse the formula into an expression tree, substitute the values and then evaluate the expression.
Usig eval is quick to implement but dangerous because it could do something you didn't expect. Parsing the formula is best done with a dedicated library for building parsers, but if your expression syntax is very simple you could write a parser yourself without using a library. In either case SQL isn't the right language for this.
I'd recommend finding a library that can help you parse the formula. Whichever route you choose though, I'd suggest doing the bulk of the work on the database client, not in SQL. SQL was simply not designed for this sort of task.

Database-Independent to_char Function in SQLAlchemy

I am using SQLAlchemy to make database-independent querys.
I am facing one issue with to_char function.
Consider the simple query like:
select to_char(id,'999') from xyz
It's working on Postgres but MySQL doesn't support it.
How can I make this query database-independent using SQLAlchemy?
use the CAST function for simple type conversions. it is SQL standard and SQLAlchemy supports it directly via the cast() function: http://www.sqlalchemy.org/docs/05/reference/sqlalchemy/expressions.html?highlight=cast#sqlalchemy.sql.expression.cast .
for date values, SQLA has an extract() function that produces the SQL EXTRACT function - a translation layer translates common fieldnames like "month", "day", "minute", etc. into the appropriate keyword for the backend in use.
Use the cast function, for example
cast(numberfield as char)
You're looking for format in MySQL:
select format(id, '999') from xyz
To answer your all-in-one question: There's no cross-database way to do this. Every RDBMS is very different, and there's no real standardization past the fairly simple queries. This is especially true with string manipulations.