Pig equivalent for SQL's select* from tablename - apache-pig

Pig equivalent for SQL's select* from tablename

Not clear what is meant by "o/p".
But the equivalent for SQL's SELECT * FROM table
in Apache Pig is FOREACH alias GENERATE *;
[ http://hadooptutorial.info/pig-functions-cheat-sheet/ ]

Related

SQL asterisk(*) all possible uses

I have a simple question. I want to list down all the scenarios where we can use the keyword asterisk(or star) *.
I am known of only these scenarios:
Select * from Customers;
Select Count(*) from Customers;
Select Customers.quantity * Customers.price from Customers;
I've searched all over the internet but didn't find any other use case.
Also, the scenarios where we can use * with a column in select query.
Edit: Ok as #Lucas Eder requested to know my use-case, here it is. I've got a program which accepts SQL query and store it in DB. Before storing, it does a validation not allow to create select * and Count(*) queries. Other than that it should allow all other queries. So that's the reason I want to know other scenarios where * is used so that I can whitelist them.
Fun question!
Here's what jOOQ knows about the various SQL dialects (looking at its sources):
Repeating yours (with some comments):
-- Unqualified asterisk
SELECT * FROM t;
-- Unqualified asterisk with useful Google BigQuery extension
SELECT * EXCEPT (a, b) FROM t
-- Asterisk in COUNT
SELECT COUNT(*) FROM t;
-- Multiplication operator for numbers
SELECT a * b FROM t;
-- Multiplication operator for intervals / numbers
SELECT INTERVAL 1 DAY * 3 FROM t;
Other cases jOOQ knows:
-- Qualified asterisk
SELECT t.* FROM t;
-- Multiline comment syntax
SELECT /* important column */ a FROM t;
-- Oracle hint syntax
SELECT /*+FIRST_ROWS*/ a FROM t;
-- Old SQL Server outer join syntax (no longer supported)
SELECT * FROM t, u WHERE t *= u;
-- Oracle precision / scale wildcard
CREATE TABLE t (a NUMBER(*), b NUMBER(*, 2));
-- PostgreSQL syntax for case insensitive pattern matching (ILIKE)
-- (there are many more operators)
SELECT 'abc' ~~* 'A%'
Other cases I know:
-- MATCH_RECOGNIZE pattern matching
SELECT * FROM t MATCH_RECOGNIZE ( ... PATTERN X Y* Z ... )
-- Oracle 21c's projecting everything into JSON
SELECT JSON_OBJECT(*) FROM t
String literal contents, which are specified and parsed as well:
-- Regular expressions (the asterisk is in a string literal, but it's still worth noting)
SELECT regexp_replace(a, 'a*', '') FROM t;
-- Similar predicate (again, it's in a string literal but the format is specified)
SELECT 'abc' SIMILAR TO 'a*'
-- JSON Path contents (there are quite a few possible asterisks here)
SELECT JSON_QUERY(col, '$.*') FROM t;
-- XPath contents
SELECT XMLQUERY('/x/*' PASSING t.xmldoc) FROM t
Esoteric cases:
The ISO/IEC 9075-2:2016(E) SQL standard specifies in 21.6 <embedded SQL Fortran program> [sic!]
<Fortran type specification> ::=
CHARACTER [ <asterisk> <character length> ] [ CHARACTER SET
[ IS ] <character set specification> ]
Yes. You asked for it!
Oracle: * as string literal's delimeter:
SELECT q'*O'Brien*' AS name FROM dual;
Output:
+---------+
| NAME |
+---------+
| O'Brien |
+---------+
db<>fiddle demo
Hive: REGEX Column Specification:
SELECT `commonPrefix*` FROM table_name
* as field terminator for all kind of SQL dialects that support external tables and/or COPY operation.
Azure Synapse: * as placeholder
SELECT p.filepath(1) AS [year],
p.filepath(2) AS [month],
COUNT_BIG(*) AS cnt
FROM OPENROWSET(
BULK 'https://<sth>.windows.net/some_name/pyear=*/pmonth=*/*.parquet'
,FORMAT = 'PARQUET'
) AS p
WHERE p.filepath(1) IN ('2018','2019','2020');
SQL Server compound operator: #var *= x syntax as syntactic sugar for #var = #var * x
DECLARE #var INT = 1;
SELECT #var *= 2;
SELECT #var;
-- 2
db<>fiddle demo
EDIT:
"I've got a program which accepts SQL query and store it in DB. Before storing, it does a validation not allow to create select * and Count(*) queries. Other than that it should allow all other queries. So that's the reason I want to know other scenarios where * is used so that I can whitelist them."
Banning * does not prevent user from grabbing all columns. Example
SELECT * FROM t;
<=>
TABLE t;
-- PostgreSQL, MySQL
db<>fiddle demo

Escape square brackets in Oracle and PostgreSQL

I'm working on a project where the database could be an instance of oracle or Postgres.
I have the need to write a query with a like that work on both dbs.
The query works on a text column containing a JSON string, for example:
{"ruleName":"r2_an","divisionNameList":["div1"],"names":["name1"],"thirdTypeLabels":[],"secondTypeLabels":[],"firstTypeLabels":[]}
I need to select the lines with empty thirdTypeLabels.
select *
from my_table
where JSON like '%thirdTypeLabels%[]%';
On Oracle, for example, does not extract anything, even if in "my_table" there is more than one line matching.
The query is inside a Java software, using JDBC, because we need performace.
Have you any suggestion?
You should use a proper JSON parser otherwise there is no guarantee that %thirdTypeLabels%[]% will restrict the match of the empty array to the thirdTypeLabels key-value pair.
So for Oracle 18c you can use:
SELECT id,
thirdTypeLabelsCount
FROM mytable t
CROSS JOIN
JSON_TABLE(
t.json,
'$'
COLUMNS(
thirdTypeLabelsCount NUMBER PATH '$.thirdTypeLabels.size()'
)
)
WHERE thirdTypeLabelsCount = 0;
or
SELECT *
FROM mytable
WHERE JSON_EXISTS( json, '$ ? (#.thirdTypeLabels.size() == 0) ' )
db<>fiddle
For Postgres you have two choices to make this query work properly:
select *
from the_table
where jsonb_array_length(json::jsonb -> 'thirdTypeLabels') = 0;
Or - starting with Postgres 12 - using a JSON Path expression
select *
from the_table
where jsonb_path_exists(json::jsonb, '$.thirdTypeLabels.size() ? (# == 0)' );
Or use the same JSON path expression as in Oracle:
select *
from the_table
where jsonb_path_exists(json::jsonb, '$' ? (#.thirdTypeLabels.size() == 0)');
In Postgres you should also use a column defined as jsonb rather than text (or varchar)

Adding a SQL View Error

As I add a View to SQL Server I get this error:
Incorrect syntax near the keyword 'DECLARE'
because I have to Declare first a tempTable Currency then insert some values in it from Currecy then retrieved it and Join it to Items table where the Select Statement start
My Question is, does Creating a view disallows the creating of
TempTable or Inserting ?
Thanks a lot.
SQL Server defines CREATE VIEW as following:
CREATE VIEW [ schema_name . ] view_name [ (column [ ,...n ] ) ]
[ WITH <view_attribute> [ ,...n ] ]
AS select_statement
...
That means after AS you have to use a single select statement.
If you like, you can use various techniques to avoid filling your temporary table. CTE is a relatively simple technique that can ACT like a temp table.
So, instead of this:
select *
into #tmp_currency
from Currency
...(alter table #tmp_currency)...
select *
from
othertable ot
join #tmp_currency tc
on tc.your_pk_col= ot.your_fk_col
...you can use this...
;with tmp_currency as (
select
*,
new_col = (whatever value you want to calculate)
from Currency)
select *
from
othertable ot
join tmp_currency tc
on tc.your_pk_col= ot.your_fk_col
View creation is then a trivial task:
create view yourviewname as
with tmp_currency as (
select
*,
new_col = (whatever value you want to calculate)
from Currency)
select *
from
othertable ot
join tmp_currency tc
on tc.your_pk_col= ot.your_fk_col
Views do not allow creation of temporary tables. You need to use a Stored procedure
Yes a view is just a single select statement - it may contain CTEs or derived tables though.
If you do actually need procedural logic that can't be satisfied by those then you can use a multistatement table valued function.

Postgresql 9.2 syntax issue

I am trying to execute this query using Postgresql 9.2
WITH udetail as (select(regexp_split_to_array('user#domain', E'\\#+')))
select * from udetail[1];
Buy it gives me a syntax error near where the start of '['. This same query is working fine under version 9.3. So I'm wonder if there's an alternative way of writing the query to get the same result.
I think you are looking for something like this:
WITH udetail(x) as (
select(regexp_split_to_array('user#domain', E'\\#+')))
select x[1] from udetail;
I don't think you can index a table expression like udetail in your case. It is the column of the table expression that is of array type. Hence, you can use an array subscript number on the column, not on the table itself.
Demo here
You should use an alias either as the query parameter:
with udetail(arr) as (
select regexp_split_to_array('user#domain', E'\\#+')
)
select arr[1] from udetail;
or as column alias:
with udetail as (
select regexp_split_to_array('user#domain', E'\\#+') as arr
)
select arr[1] from udetail;
You can also do it without aliases:
with udetail as (
select regexp_split_to_array('user#domain', E'\\#+')
)
select regexp_split_to_array[1] from udetail;

NOT IN in Hive QL

I'm converting a SQL Server stored procedure to HiveQL.
How can I convert something like:
SELECT * FROM table1 WHERE id NOT IN (7,6,5,4,2,12)
NOT IN is now supported in Hive. See https://cwiki.apache.org/confluence/display/Hive/LanguageManual+UDF.
Try this:
SELECT * FROM table1 WHERE NOT array_contains(array(7,6,5,4,2,12), id)
According to the documentation it says you can use not in:
The negated forms can be written as follows:
from DomesticCat cat where cat.name not between 'A' and 'B'
from DomesticCat cat where cat.name not in ( 'Foo', 'Bar', 'Baz' )
Are you getting an error when you try your query in the question?
Please try based on the references as well.
Reference 1.
NOT IN Clause in HQL.