Skip to content

Instantly share code, notes, and snippets.

View onderkalaci's full-sized avatar

Önder Kalacı onderkalaci

View GitHub Profile
CREATE TABLE events (
user_id int,
event_id bigint,
event_type character varying(255),
event_time bigint
);
SELECT master_create_distributed_table('events', 'user_id', 'range');
@onderkalaci
onderkalaci / five_thousands_join.sql
Last active April 13, 2017 07:59
Subquery_join_tests
This file has been truncated, but you can view the full file.
SELECT
e1.user_id,
sum(view_homepage) AS viewed_homepage,
sum(use_demo) AS use_demo,
sum(enter_credit_card) AS entered_credit_card,
sum(submit_card_info) as submit_card_info,
sum(e5.see_bought_screen) as see_bought_screen
FROM (
-- Get the first time each user viewed the homepage.
SELECT
@onderkalaci
onderkalaci / filter_joins.sql
Created April 13, 2017 08:31
Filter and joins
SELECT
e1.user_id,
sum(view_homepage) AS viewed_homepage,
sum(use_demo) AS use_demo,
sum(enter_credit_card) AS entered_credit_card,
sum(submit_card_info) as submit_card_info,
sum(e1.view_homepage) as see_bought_screen
FROM (
-- Get the first time each user viewed the homepage.
SELECT
CREATE TABLE table_1 (
col_1 int,col_2 int,col_3 int,col_4 int,col_5 int,col_6 int,col_7 int,col_8 int,col_9 int,col_10 int,col_11 int,col_12 int,col_13 int,col_14 int,col_15 int,col_16 int,col_17 int,col_18 int,col_19 int,col_20 int,col_21 int,col_22 int,col_23 int,col_24 int,col_25 int,col_26 int,col_27 int,col_28 int,col_29 int,col_30 int,col_31 int,col_32 int,col_33 int,col_34 int,col_35 int,col_36 int,col_37 int,col_38 int,col_39 int,col_40 int,col_41 int,col_42 int,col_43 int,col_44 int,col_45 int,col_46 int,col_47 int,col_48 int,col_49 int,col_50 int,col_51 int,col_52 int,col_53 int,col_54 int,col_55 int,col_56 int,col_57 int,col_58 int,col_59 int,col_60 int,col_61 int,col_62 int,col_63 int,col_64 int,col_65 int,col_66 int,col_67 int,col_68 int,col_69 int,col_70 int,col_71 int,col_72 int,col_73 int,col_74 int,col_75 int,col_76 int,col_77 int,col_78 int,col_79 int,col_80 int,col_81 int,col_82 int,col_83 int,col_84 int,col_85 int,col_86 int,col_87 int,col_88 int,col_89 int,col_90 int,col_91 int,col_92 i
--
-- multi subquery in where queries aims to expand existing subquery pushdown
-- regression tests to cover more cases specifically subqueries in WHERE clause
-- the tables that are used depends to multi_insert_select_behavioral_analytics_create_table.sql
--
-- We don't need shard id sequence here, so commented out to prevent conflicts with concurrent tests
-- ALTER SEQUENCE pg_catalog.pg_dist_shardid_seq RESTART 1430000;
-- ALTER SEQUENCE pg_catalog.pg_dist_jobid_seq RESTART 1430000;
-- subqueries in WHERE with greater operator
--
-- MULTI_VIEW
--
-- This file contains test cases for view support. It verifies various
-- Citus features: simple selects, aggregates, joins, outer joins
-- router queries, single row inserts, multi row inserts via insert
-- into select, multi row insert via copy commands.
SELECT count(*) FROM lineitem_hash_part;
SELECT user_id, counter
FROM (
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (1, 2, 3, 4, 5)
UNION
SELECT user_id, value_2 % 10 AS counter FROM events_table WHERE event_type IN (5, 6, 7, 8, 9, 10)
) user_id
ORDER BY 2 DESC,1
LIMIT 5;
CREATE TABLE users
(
user_id int,
value int
);
CREATE TABLE events
(
user_id int,
value int
@onderkalaci
onderkalaci / steps.sql
Created May 26, 2017 14:16
distribute by hash partition by time
-- create a partitioned local table
CREATE TABLE measurement (
city_id int not null,
logdate date not null,
peaktemp int,
unitsales int
) PARTITION BY RANGE (logdate);
SELECT *, rank() OVER (PARTITION BY solomon_user_id ORDER BY page_views DESC)
FROM (
SELECT solomon_user_id, key, COUNT(*) AS page_views
FROM page_intermediates
GROUP BY solomon_user_id, key
) AS A;