presto | official home of the Presto distributed SQL query engine
kandi X-RAY | presto Summary
Support
Quality
Security
License
Reuse
- Builds the sql functions .
- Create a new task executor
- Coerces the given type to the given result type .
- Pre - build aggregation .
- Processes a join .
- Create a batch ORC page source .
- Create an orc file source .
- Print summary information about the query .
- Casts a primitive type to a primitive block .
- Creates a Hive page source .
presto Key Features
presto Examples and Code Snippets
Trending Discussions on presto
Trending Discussions on presto
QUESTION
I am trying to find a way to print the result taking into account if exists records in a table or not. If yes I should print all the records in a table, otherwise I should print only one record.
Example:
I have the Table Example:
ColA Colb ColCIf select count(*) from Example > 0 THEN Return
ColA Colb ColC 1 VA1 NULL 2 VB1 NULL 3 NULL VA2If select count(*) from Example <= 0 THEN Return
ColA Colb ColC Result NA NAIs it possible to do something like that? I am doing the development using PRESTO.
Thanks you in advance
ANSWER
Answered 2022-Apr-04 at 08:49We could introduce a dummy/default row via a union, and then retain it only in the event of the Example
table being empty:
WITH cte AS (
SELECT ColA, ColB, ColC, 0 AS pos FROM Example
UNION ALL
SELECT 'Result', 'NA, 'NA', 1
)
SELECT ColA, ColB, ColC
FROM cte
WHERE pos = 0 OR NOT EXISTS (SELECT 1 FROM cte WHERE pos = 0);
QUESTION
I have a table that I'm first trying to group based on unique column values (using dense_rank
) and then further group those items into batches of 5. Below is my table:
And below is what I'm trying to get:
video_id frame_id verb batch_of_five video_a frame_1 walk 1 video_a frame_2 run 1 video_a frame_3 sit 1 video_a frame_4 walk 1 video_a frame_5 walk 1 video_a frame_6 walk 2 video_b frame_7 stand 3 video_b frame_8 stand 3 video_b frame_9 run 3 video_b frame_10 run 3 video_b frame_11 sit 3 video_b frame_12 run 4 video_b frame_13 run 4Where each video_id
has a unique rank and each batch of 10 within each ranked video_id
has its own unique rank (and each batch of 10 overall has a unique id regardless of whether they belong to the same video_id
or not).
I'm able to group based on the video_id
column but am having trouble grouping those items further so that they are both in batches of 10 and unique across all video_ids
. I thought about using a group by clause but I'm trying to keep the other columns intact as well (verb
column).
Here is my presto query so far:
SELECT
*
FROM (
SELECT
*,
-- Give each unique video_id a unique rank
DENSE_RANK() OVER (ORDER BY video_id) AS video_batch
FROM videos
)
ANSWER
Answered 2022-Feb-08 at 16:08Calculate frame rank (partition by video_id), divide by 6 (integer division) to get batch number in video_id partition. Then rank again to get absolute batch number:
with sample_data as(
select 'video_a' as video_id, 'frame_1' as frame_id , 'walk' as verb union all
select 'video_a', 'frame_2' , 'run' union all
select 'video_a', 'frame_3' , 'sit' union all
select 'video_a', 'frame_4' , 'walk' union all
select 'video_a', 'frame_5' , 'walk' union all
select 'video_a', 'frame_6' , 'walk' union all
select 'video_b', 'frame_7' , 'stand' union all
select 'video_b', 'frame_8' , 'stand' union all
select 'video_b', 'frame_9' , 'run' union all
select 'video_b', 'frame_10', 'run' union all
select 'video_b', 'frame_11', 'sit' union all
select 'video_b', 'frame_12', 'run' union all
select 'video_b', 'frame_13', 'run'
)
select s.*,
dense_rank() over(order by video_id, rnk_frame / 6) batch_of_five
from
(
select video_id, frame_id, verb,
CAST(regexp_extract(frame_id,'_(\d*)$',1) AS INT) frame_number,
dense_rank() over(partition by video_id order by CAST(regexp_extract(frame_id,'_(\d*)$',1) AS INT)) rnk_frame
from sample_data
)s
order by video_id, frame_number;
Result:
video_id frame_id verb frame_number rnk_frame batch_of_five
video_a frame_1 walk 1 1 1
video_a frame_2 run 2 2 1
video_a frame_3 sit 3 3 1
video_a frame_4 walk 4 4 1
video_a frame_5 walk 5 5 1
video_a frame_6 walk 6 6 2
video_b frame_7 stand 7 1 3
video_b frame_8 stand 8 2 3
video_b frame_9 run 9 3 3
video_b frame_10 run 10 4 3
video_b frame_11 sit 11 5 3
video_b frame_12 run 12 6 4
video_b frame_13 run 13 7 4
I extracted frame_number to sort as integer, not as string, to get the same sort order as in your question (some sorting column is absolutely necessary), if you already have rank as you mentioned in question, you can use it instead.
QUESTION
I am migrating a query from Vertica to Presto(0.246). The query contains a Vertica function StringTokenizerDelim, which basically explodes and array (market column) into rows.
This is the mockup of the table:
+----+---------+-------------+
| id | product | market |
+----+---------+-------------+
| 1 | Cabinet | AU,GB,FR |
| 2 | Chair | US,GB |
| 3 | Desk | BE,GB,FR,US |
+----+---------+-------------+
The output should be:
+----+---------+--------+
| id | product | market |
+----+---------+--------+
| 1 | Cabinet | AU |
| 1 | Cabinet | GB |
| 1 | Cabinet | FR |
| 2 | Chair | US |
| 2 | Chair | GB |
| 3 | Desk | BE |
| 3 | Desk | GB |
| 3 | Desk | FR |
| 3 | Desk | US |
+----+---------+--------+
What is the equivalent to that function in Presto (if there is any? couldn't find it on the documentation)?
Cheers,
ANSWER
Answered 2022-Feb-09 at 10:49-- sample data
WITH dataset (id, product, market ) AS (
values (1, 'Cabinet', 'AU,GB,FR'),
(2, 'Chair', 'US,GB'),
(3, 'Desk', 'BE,GB,FR,US')
)
--query
SELECT id, product, m as market
FROM dataset
CROSS JOIN UNNEST (split(market, ',')) as t(m)
Output:
id product market 1 Cabinet AU 1 Cabinet GB 1 Cabinet FR 2 Chair US 2 Chair GB 3 Desk BE 3 Desk GB 3 Desk FR 3 Desk USQUESTION
I have a table in presto with 2 columns: date
and value
.
I want to calculate the average of 2nd Quarter's values so the expected result should be: 15.
How can I do this in presto?
date value
2021-01-01 10
2021-01-30 20
2021-02-10 10
2021-04-01 20
2021-04-02 10
2021-07-10 20
ANSWER
Answered 2022-Jan-19 at 11:09You can divide month by 3 and group by the result:
-- sample data
WITH dataset (date, value) AS (
VALUES (date '2021-01-01' , 10),
(date '2021-01-30' , 20),
(date '2021-02-10' , 10),
(date '2021-04-01' , 20),
(date '2021-04-02' , 10),
(date '2021-07-10', 20)
)
--query
SELECT avg(value)
FROM dataset
WHERE month(date) / 3 = 1
GROUP BY month(date) / 3
Output:
_col0 15.0QUESTION
Is there a way to use multiple WITH statements in Athena/Presto?
WITH "revenue" AS (
SELECT
"cik",
"accession",
"year",
"quarter",
"form type" as "form_type",
CAST("value" AS bigint) as "revenue",
CAST("value" - lag("value") over (partition by "cik") AS bigint) as "increment",
ROUND("value" / lag("value") over (partition by "cik"),2) as "ratio"
FROM "gaap"
WHERE
"form type" IN ('10-K')
AND "rep" = 'revenue'
ORDER BY
"cik", "year", "quarter", "fs" desc
)
WITH "cik_with_continuous_growth" AS (
SELECT "cik"
FROM "revenue"
WHERE
"ratio" >= 1.5
AND "year" >= 2016
GROUP BY "cik"
HAVING COUNT("ratio") >= 3
ORDER BY "cik"
)
SELECT * FROM "cik_with_continuous_growth";
Only one sql statement is allowed. Got: WITH "revenue" AS ( SELECT "cik", "accession", "year", "quarter", "form type" as "form_type", CAST("value" AS bigint) as "revenue", CAST("value" - lag("value") over (partition by "cik") AS bigint) as "increment", ROUND("value" / lag("value") over (partition by "cik"),2) as "ratio" FROM "gaap" WHERE "form type" IN ('10-K') AND "rep" = 'revenue' ORDER BY "cik", "year", "quarter", "fs" desc ) WITH "cik_with_continuous_growth" AS ( SELECT "cik" FROM "revenue" WHERE "ratio" >= 1.5 AND "year" >= 2016 GROUP BY "cik" HAVING COUNT("ratio") >= 3 ORDER BY "cik" ) SELECT * FROM "cik_with_continuous_growth"; #WHERE "revenue"."cik" = "cik_with_continuous_growth"."cik";
ANSWER
Answered 2022-Jan-15 at 23:30have you tried with a as ( ) , b as () select * from a,b
?
QUESTION
I'm trying to calculate the City area size by using Geospatial Functions like the bing_tiles_around(), geometry_union(), and st_area() with the below sample data.
City ID | Store ID | latitude | longitude | radius (km)
---------------------------------------------------------
12345 | store_01 | 36.1234 | 31.1234 | 3.11
12345 | store_02 | 36.5678 | 31.5678 | 2.52
I believe I should use the above functions to calculate the area size. As long as I understand based on this reference, the bing_tiles_around() returns an array(BingTile) and I need to transform it into geometry types to use both the geometry_union() and st_area() but I'm not sure how to transform 'array(BingTile)' into 'geometry' type.
with store_info as (
select city_id, store_id, latitude, longitude, radius,
bing_tiles_around(latitude, longitude, 10, radius) as bingTiles_around
from store_detail
)
select city_id, store_id, geometry_union(bingTiles_around)
from store_info
The above query is the current one that I'm working on and it showed me the below error when I executed it. (I know it's not correct usage but just want to share the below error message to resolve this problem)
QueryValidation: user_error: Attempt 1: presto: query failed (200 OK):
"USER_ERROR: com.facebook.presto.sql.analyzer.SemanticException: line 17:70:
Unexpected parameters (array(BingTile)) for function geometry_union.
Expected: geometry_union(array(Geometry)) "
The ultimate goal is to create a sample result dataset as below.
City ID | Area_size
--------------------
12345 | 40.12345
12346 | 50.56789
Can anyone please help me to create this table using the above information? Any help would be appreciated.
Thanks!
ANSWER
Answered 2022-Jan-12 at 10:50bing_tiles_around
returns array of BingTile
while geometry_union
expects array of Geometry
so you need to transform one to another:
-- sample data
WITH dataset (City_ID , Store_ID , latitude , longitude , radius ) AS (
VALUES (12345 , 'store_01' , 36.1234 , 31.1234 , 3.11),
(12345 , 'store_02' , 36.5678 , 31.5678 , 2.52)
)
--query
select city_id,
store_id,
geometry_union(
transform(bingTiles_around, t->bing_tile_polygon(t))
)
from(
select city_id,
store_id,
latitude,
longitude,
radius,
bing_tiles_around(latitude, longitude, 10, radius) as bingTiles_around
from dataset
)
QUESTION
I tried a lot of code to remove .php
from url
for example - ht.abuena.net/presto.php
-> ht.abuena.net/presto
and vice versa - internally
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
nothing works - the url stays unchanged
page is reloading by right clicking on the button Reload
and choosing - Empty Cache and Hard Reload
- so I hope the cache is cleared
live example here - here
ANSWER
Answered 2021-Dec-19 at 19:34With your shown samples, please try following htaccess rules file.
Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} ^$
##using THE_REQUEST variable for condition check.
RewriteCond %{THE_REQUEST} \s/([^.]*)\.php/?\s [NC]
##Performing external redirect here.
RewriteRule ^ %1? [R=301,L]
##Performing rewrite for non-existing pages.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.*)/?$ /$1.php [QSA,L]
QUESTION
could you guys please help me creating a next and previous buttons ? I've been struggling because of my bad javascript . I saw some people use Jquery and almost all Javascript. I'm practicing Javascript so there are a lot of things I don't know. Thank you very much.
Wants: Next / Previous button to go to next page and go back page if users want to read again that page.
Link of my demo: https://jsfiddle. net/hioihia123/zgjswtay/3/
body {
font-size: 18px;
background: url("https://www.toptal.com/designers/subtlepatterns/patterns/groovepaper.png");
font-family: 'Gelasio', serif;
}
main {
color: black;
font-size: 1.1rem;
display: flex;
flex-direction: column;
width:100%;
margin-left : auto;
margin-right: auto;
}
main div {
width: 100%;
padding: 6rem 5rem;
}
h2 {
text-align: center;
font-size: 1.2rem;
font-weight: normal;
margin-bottom: 6rem;
}
h1 {
font-family: 'Ibarra Real Nova', serif;
text-align: center;
font-weight: 400;
font-size: 3rem;
text-transform: uppercase;
margin-bottom: 6rem;
letter-spacing: .1rem;
}
.right-page {
margin-top: 0;
padding-top: 0;
}
.right-page p {
line-height: 1.4;
text-align: justify;
text-justify: inter-word;
}
.right-page p:first-letter {
font-family: 'Ibarra Real Nova', serif;
font-size: 4.5rem;
float: left;
margin-top: .5rem;
margin-right: 1rem;
line-height: .7;
}
.left-page {
text-align: center;
padding-top: 4rem;
}
.left-page small {
font-style: italic;
}
.left-page img {
max-width: 90%;
display: block;
margin-left: auto;
margin-right: auto;
}
@media screen and (min-width: 900px) {
main {
flex-direction: row;
with: 100%;
max-width: 1800px;
}
main div {
width: 50%;
}
.left-page {
padding-top: 14rem;
}
.right-page {
padding-top: 6rem;
max-height: 100vh;
height: 100vh;
}
}
Old book Stories
But go there, lonely,
At eventide,
And hearken, hearken
To the lisping tide.
[ 1 ]
Depender
En cuanto a todas las cosas que existen en el mundo, unas dependen de nosotros,
otras no dependen de nosotros. De nosotros dependen; nuestras opiniones, nuestros
movimientos, nuestros deseos, nuestras inclinaciones, nuestras aversiones; en una
palabra, todas nuestras acciones.
Así, ante toda fantasía perturbadora, está presto a decir: “Tu no eres sino una
imaginación, y en absoluto eres lo que parece”, enseguida examínala con atención y
ponla a prueba, para ello sírvete de las reglas que tienes, principalmente con esta
primera que es, a saber : de si la cosa que te hace penar es del número de aquellas
que dependen de nosotros o de aquellas que no están en nuestro poder. Di sin
titubear: “Esa en nada me atañe”.
ANSWER
Answered 2021-Dec-19 at 04:40Can you simply add the Previous
and Next
buttons at the footer or somewhere you'd prefer, and link to appropriate pages? Won't that be simple enough in your case?
Previous
Next
Since you have all pages in a div
inside main
, its pretty easy to traverse through the pages, get the div that is visible, and hide it, and show the next or previous page depending upon the button clicked :)
Here's a sample:
function changePage(prevOrNext) {
var pages = document.getElementsByTagName('main')[0].children;
for (let pageIndex = 0; pageIndex < pages.length; pageIndex++) {
if (pages[pageIndex].style.display == "block") {
if (prevOrNext == 1 && pageIndex < pages.length - 1) {
pages[pageIndex].style.display = "none"
pages[pageIndex + 1].style.display = "block"
document.body.scrollTop = document.documentElement.scrollTop = 0;
} else if (prevOrNext == 0 && pageIndex > 0) {
pages[pageIndex].style.display = "none"
pages[pageIndex - 1].style.display = "block"
document.body.scrollTop = document.documentElement.scrollTop = 0;
}
}
}
}
body {
font-size: 18px;
background: url("https://www.toptal.com/designers/subtlepatterns/patterns/groovepaper.png");
font-family: 'Gelasio', serif;
}
main {
color: black;
font-size: 1.1rem;
display: flex;
flex-direction: column;
width: 100%;
margin-left: auto;
margin-right: auto;
}
main div {
width: 100%;
padding: 6rem 5rem;
display: none
}
h2 {
text-align: center;
font-size: 1.2rem;
font-weight: normal;
margin-bottom: 6rem;
}
h1 {
font-family: 'Ibarra Real Nova', serif;
text-align: center;
font-weight: 400;
font-size: 3rem;
text-transform: uppercase;
margin-bottom: 6rem;
letter-spacing: .1rem;
}
.right-page {
margin-top: 0;
padding-top: 0;
}
.right-page p {
line-height: 1.4;
text-align: justify;
text-justify: inter-word;
}
.right-page p:first-letter {
font-family: 'Ibarra Real Nova', serif;
font-size: 4.5rem;
float: left;
margin-top: .5rem;
margin-right: 1rem;
line-height: .7;
}
.left-page {
text-align: center;
padding-top: 4rem;
}
.left-page small {
font-style: italic;
}
.left-page img {
max-width: 90%;
display: block;
margin-left: auto;
margin-right: auto;
}
@media screen and (min-width: 900px) {
main {
flex-direction: row;
with: 100%;
max-width: 1800px;
}
main div {
width: 50%;
}
.left-page {
padding-top: 14rem;
}
.right-page {
padding-top: 6rem;
max-height: 100vh;
height: 100vh;
}
}
Old book Stories
But go there, lonely,
At eventide,
And hearken, hearken
To the lisping tide.
[ 1 ]
Depender
En cuanto a todas las cosas que existen en el mundo, unas dependen de nosotros, otras no dependen de nosotros. De nosotros dependen; nuestras opiniones, nuestros movimientos, nuestros deseos, nuestras inclinaciones, nuestras aversiones; en una palabra,
todas nuestras acciones.
Así, ante toda fantasía perturbadora, está presto a decir: “Tu no eres sino una
imaginación, y en absoluto eres lo que parece”, enseguida examínala con atención y ponla a prueba, para ello sírvete de las reglas que tienes, principalmente con esta primera que es, a saber : de si la cosa que te hace penar es del número de
aquellas que dependen de nosotros o de aquellas que no están en nuestro poder. Di sin titubear: “Esa en nada me atañe”.
Previous
Next
QUESTION
we have a Hive Internal partitioned table which is existing from long time. recently we discovered that we need to change a column's name and datatype. also, we need to remove 3 duplicate records from the table. so we performed this step.
alter table tablename CHANGE line_nbr line_id INT; (it was string before)
INSERT OVERWRITE TABLE table1 PARTITION(cmny_cd,order_dt)
select
column1,
columnx
from table1
where id NOT IN ('1','2','3')
and order_dt in (CAST('2020-02-18' AS DATE),CAST('2020-02-19' AS DATE),CAST('2020-10-30' AS DATE));"
Tried to run the above steps to change the column name and type and also remove dupes. during this, we encountered an error saying there are few bad partitions in the table. but when checked, the dupes were not removed, but the column name and type were changed.
Now, we removed the bad partitions from the table, and then tried to run only the insert overwrite step to remove the dupes. It was successful, and removed the dupes. But if i try to query the table now, for some of the dates, it gives me an error
-- presto error: Error opening Hive split gs:xxxx/cmny_cd=xxx/order_dt=2021-09-20/000000_0 (offset=0, length=24370774): Malformed ORC file. Cannot read SQL type 'integer' from ORC stream '._col6' of type STRING with attributes {} [gs:xxxx/cmny_cd=xxx/order_dt=2021-09-20/000000_0]
when i check the show create table, the name and dataype are changed, but when i try the below
describe formatted tablename partition (cmny_cd='xxx',order_dt='2021-09-20');
few partitions, it shows changed name and type, but few, it shows the old name and string datatype. Im guessing this could have happened, because of the failure when we first attempted to change the dataype and remove dupes.
Im confused on what should i do in order to repair the table and query the table without any issues. Can someone please help on this? It would be greatly appreciated.
we ran msck repair table tablename already on the table, but it did not solve the issue
ANSWER
Answered 2021-Dec-15 at 08:53Overwrite all partitions using Hive, like you did when removed duplicates. Some partitions remain not overwritten and causing error in Presto because the type inside ORC file and in Hive metadata is different. So, just try to overwrite partitions which you did not previously overwritten or overwrite all of them:
INSERT OVERWRITE TABLE table1 PARTITION(cmny_cd,order_dt)
select
column1,
columnx
...
from table1
---you can limit partitions in the WHERE
QUESTION
Here I've an array ["chair","desk","charger"], I'd like to check if a different array contains any the elements in the first array.
Example:
["chair","desk"] //returns true;
["screen","charger","computer"] //returns true;
["calendar", "screen"] //returns false;
How could this be done in Presto SQL?
ANSWER
Answered 2021-Dec-03 at 13:48Check cardinality of array returned by array_intersect(x, y) function.
Demo:
with mydata as (
select 1 id, array['chair','desk'] as myarray union all
select 2 id, array['screen','charger','computer'] union all
select 3 id, array['calendar', 'screen']
)
select id,
cardinality(array_intersect(myarray, array['chair','desk','charger']))>0 as contains_flag
from mydata
order by id
Result:
id contains_flag
1 TRUE
2 TRUE
3 FALSE
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
Install presto
You can use presto like any standard Java library. Please include the the jar files in your classpath. You can also use any IDE and you can run and debug the presto component as you would do with any other Java program. Best practice is to use a build tool that supports dependency management such as Maven or Gradle. For Maven installation, please refer maven.apache.org. For Gradle installation, please refer gradle.org .
Support
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesExplore Kits - Develop, implement, customize Projects, Custom Functions and Applications with kandi kits
Save this library and start creating your kit
Share this Page