The document discusses using common table expressions (CTEs) in PostgreSQL to perform recursion to represent hierarchical or tree-structured data. It provides an example of querying an employee table with a recursive CTE to display the organization structure as a tree. The CTE defines an initial condition to start the recursion, a recursive term to iteratively build the tree, and a termination condition to end the recursion.
Updated version of my tutorial on how to give a great tech talk, this time without Ian Dees. New tutorial is longer thanks to longer talk slot. Mostly the extra time will be spent on exercises.
Updated version of my tutorial on how to give a great tech talk, this time without Ian Dees. New tutorial is longer thanks to longer talk slot. Mostly the extra time will be spent on exercises.
The big data platforms of many organisations are underpinned by a technology that is soon to celebrate its 45th birthday: SQL. This industry stalwart is applied in a multitude of critical points in business data flows; the results that these processes generate may significantly influence business and financial decision making. However, the SQL ecosystem has been overlooked and ignored by more recent innovations in the field of software engineering best practices such as fine grained automated testing and code quality metrics. This exposes organisations to poor application maintainability, high bug rates, and ultimately corporate risk.
We present the work we’ve been doing at Hotels.com to address these issues by bringing some advanced software engineering practices and open source tools to the realm of Apache Hive SQL. We first define the relevance of such approaches and demonstrate how automated testing can be applied to Hive SQL using HiveRunner, a JUnit based testing framework. We next consider how best to structure Hive queries to yield meaningful test scenarios that are maintainable and performant. Finally, we demonstrate how test coverage reports can highlight areas of risk in SQL codebases and weaknesses in the testing process. We do this using Mutant Swarm, an open source mutation testing tool for SQL languages developed by Hotels.com that can deliver insights similar to those produced by Java focused tools such as Jacoco and PIT.
Big Science, Big Data: Simon Metson at Eduserv Symposium 2012Eduserv
This talk looks at experience and lessons from LHC computing applicable to the current 'Big Data' trend; how these tools and techniques can be applied to other disciplines and the potential impact on provision of computing at universities.
“PostgreSQL, Python and Squid” (otherwise known as, “using Python in PostgreSQL and PostgreSQL from Python”) presented at PyPgDay 2013 at PyCon 2013-Christophe Pettus
The big data platforms of many organisations are underpinned by a technology that is soon to celebrate its 45th birthday: SQL. This industry stalwart is applied in a multitude of critical points in business data flows; the results that these processes generate may significantly influence business and financial decision making. However, the SQL ecosystem has been overlooked and ignored by more recent innovations in the field of software engineering best practices such as fine grained automated testing and code quality metrics. This exposes organisations to poor application maintainability, high bug rates, and ultimately corporate risk.
We present the work we’ve been doing at Hotels.com to address these issues by bringing some advanced software engineering practices and open source tools to the realm of Apache Hive SQL. We first define the relevance of such approaches and demonstrate how automated testing can be applied to Hive SQL using HiveRunner, a JUnit based testing framework. We next consider how best to structure Hive queries to yield meaningful test scenarios that are maintainable and performant. Finally, we demonstrate how test coverage reports can highlight areas of risk in SQL codebases and weaknesses in the testing process. We do this using Mutant Swarm, an open source mutation testing tool for SQL languages developed by Hotels.com that can deliver insights similar to those produced by Java focused tools such as Jacoco and PIT.
Big Science, Big Data: Simon Metson at Eduserv Symposium 2012Eduserv
This talk looks at experience and lessons from LHC computing applicable to the current 'Big Data' trend; how these tools and techniques can be applied to other disciplines and the potential impact on provision of computing at universities.
“PostgreSQL, Python and Squid” (otherwise known as, “using Python in PostgreSQL and PostgreSQL from Python”) presented at PyPgDay 2013 at PyCon 2013-Christophe Pettus
In the rapidly evolving landscape of technologies, XML continues to play a vital role in structuring, storing, and transporting data across diverse systems. The recent advancements in artificial intelligence (AI) present new methodologies for enhancing XML development workflows, introducing efficiency, automation, and intelligent capabilities. This presentation will outline the scope and perspective of utilizing AI in XML development. The potential benefits and the possible pitfalls will be highlighted, providing a balanced view of the subject.
We will explore the capabilities of AI in understanding XML markup languages and autonomously creating structured XML content. Additionally, we will examine the capacity of AI to enrich plain text with appropriate XML markup. Practical examples and methodological guidelines will be provided to elucidate how AI can be effectively prompted to interpret and generate accurate XML markup.
Further emphasis will be placed on the role of AI in developing XSLT, or schemas such as XSD and Schematron. We will address the techniques and strategies adopted to create prompts for generating code, explaining code, or refactoring the code, and the results achieved.
The discussion will extend to how AI can be used to transform XML content. In particular, the focus will be on the use of AI XPath extension functions in XSLT, Schematron, Schematron Quick Fixes, or for XML content refactoring.
The presentation aims to deliver a comprehensive overview of AI usage in XML development, providing attendees with the necessary knowledge to make informed decisions. Whether you’re at the early stages of adopting AI or considering integrating it in advanced XML development, this presentation will cover all levels of expertise.
By highlighting the potential advantages and challenges of integrating AI with XML development tools and languages, the presentation seeks to inspire thoughtful conversation around the future of XML development. We’ll not only delve into the technical aspects of AI-powered XML development but also discuss practical implications and possible future directions.
UiPath Test Automation using UiPath Test Suite series, part 5DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 5. In this session, we will cover CI/CD with devops.
Topics covered:
CI/CD with in UiPath
End-to-end overview of CI/CD pipeline with Azure devops
Speaker:
Lyndsey Byblow, Test Suite Sales Engineer @ UiPath, Inc.
Dr. Sean Tan, Head of Data Science, Changi Airport Group
Discover how Changi Airport Group (CAG) leverages graph technologies and generative AI to revolutionize their search capabilities. This session delves into the unique search needs of CAG’s diverse passengers and customers, showcasing how graph data structures enhance the accuracy and relevance of AI-generated search results, mitigating the risk of “hallucinations” and improving the overall customer journey.
How to Get CNIC Information System with Paksim Ga.pptxdanishmna97
Pakdata Cf is a groundbreaking system designed to streamline and facilitate access to CNIC information. This innovative platform leverages advanced technology to provide users with efficient and secure access to their CNIC details.
Sudheer Mechineni, Head of Application Frameworks, Standard Chartered Bank
Discover how Standard Chartered Bank harnessed the power of Neo4j to transform complex data access challenges into a dynamic, scalable graph database solution. This keynote will cover their journey from initial adoption to deploying a fully automated, enterprise-grade causal cluster, highlighting key strategies for modelling organisational changes and ensuring robust disaster recovery. Learn how these innovations have not only enhanced Standard Chartered Bank’s data infrastructure but also positioned them as pioneers in the banking sector’s adoption of graph technology.
LF Energy Webinar: Electrical Grid Modelling and Simulation Through PowSyBl -...DanBrown980551
Do you want to learn how to model and simulate an electrical network from scratch in under an hour?
Then welcome to this PowSyBl workshop, hosted by Rte, the French Transmission System Operator (TSO)!
During the webinar, you will discover the PowSyBl ecosystem as well as handle and study an electrical network through an interactive Python notebook.
PowSyBl is an open source project hosted by LF Energy, which offers a comprehensive set of features for electrical grid modelling and simulation. Among other advanced features, PowSyBl provides:
- A fully editable and extendable library for grid component modelling;
- Visualization tools to display your network;
- Grid simulation tools, such as power flows, security analyses (with or without remedial actions) and sensitivity analyses;
The framework is mostly written in Java, with a Python binding so that Python developers can access PowSyBl functionalities as well.
What you will learn during the webinar:
- For beginners: discover PowSyBl's functionalities through a quick general presentation and the notebook, without needing any expert coding skills;
- For advanced developers: master the skills to efficiently apply PowSyBl functionalities to your real-world scenarios.
GDG Cloud Southlake #33: Boule & Rebala: Effective AppSec in SDLC using Deplo...James Anderson
Effective Application Security in Software Delivery lifecycle using Deployment Firewall and DBOM
The modern software delivery process (or the CI/CD process) includes many tools, distributed teams, open-source code, and cloud platforms. Constant focus on speed to release software to market, along with the traditional slow and manual security checks has caused gaps in continuous security as an important piece in the software supply chain. Today organizations feel more susceptible to external and internal cyber threats due to the vast attack surface in their applications supply chain and the lack of end-to-end governance and risk management.
The software team must secure its software delivery process to avoid vulnerability and security breaches. This needs to be achieved with existing tool chains and without extensive rework of the delivery processes. This talk will present strategies and techniques for providing visibility into the true risk of the existing vulnerabilities, preventing the introduction of security issues in the software, resolving vulnerabilities in production environments quickly, and capturing the deployment bill of materials (DBOM).
Speakers:
Bob Boule
Robert Boule is a technology enthusiast with PASSION for technology and making things work along with a knack for helping others understand how things work. He comes with around 20 years of solution engineering experience in application security, software continuous delivery, and SaaS platforms. He is known for his dynamic presentations in CI/CD and application security integrated in software delivery lifecycle.
Gopinath Rebala
Gopinath Rebala is the CTO of OpsMx, where he has overall responsibility for the machine learning and data processing architectures for Secure Software Delivery. Gopi also has a strong connection with our customers, leading design and architecture for strategic implementations. Gopi is a frequent speaker and well-known leader in continuous delivery and integrating security into software delivery.
GraphSummit Singapore | The Art of the Possible with Graph - Q2 2024Neo4j
Neha Bajwa, Vice President of Product Marketing, Neo4j
Join us as we explore breakthrough innovations enabled by interconnected data and AI. Discover firsthand how organizations use relationships in data to uncover contextual insights and solve our most pressing challenges – from optimizing supply chains, detecting fraud, and improving customer experiences to accelerating drug discoveries.
Removing Uninteresting Bytes in Software FuzzingAftab Hussain
Imagine a world where software fuzzing, the process of mutating bytes in test seeds to uncover hidden and erroneous program behaviors, becomes faster and more effective. A lot depends on the initial seeds, which can significantly dictate the trajectory of a fuzzing campaign, particularly in terms of how long it takes to uncover interesting behaviour in your code. We introduce DIAR, a technique designed to speedup fuzzing campaigns by pinpointing and eliminating those uninteresting bytes in the seeds. Picture this: instead of wasting valuable resources on meaningless mutations in large, bloated seeds, DIAR removes the unnecessary bytes, streamlining the entire process.
In this work, we equipped AFL, a popular fuzzer, with DIAR and examined two critical Linux libraries -- Libxml's xmllint, a tool for parsing xml documents, and Binutil's readelf, an essential debugging and security analysis command-line tool used to display detailed information about ELF (Executable and Linkable Format). Our preliminary results show that AFL+DIAR does not only discover new paths more quickly but also achieves higher coverage overall. This work thus showcases how starting with lean and optimized seeds can lead to faster, more comprehensive fuzzing campaigns -- and DIAR helps you find such seeds.
- These are slides of the talk given at IEEE International Conference on Software Testing Verification and Validation Workshop, ICSTW 2022.
Communications Mining Series - Zero to Hero - Session 1DianaGray10
This session provides introduction to UiPath Communication Mining, importance and platform overview. You will acquire a good understand of the phases in Communication Mining as we go over the platform with you. Topics covered:
• Communication Mining Overview
• Why is it important?
• How can it help today’s business and the benefits
• Phases in Communication Mining
• Demo on Platform overview
• Q/A
Observability Concepts EVERY Developer Should Know -- DeveloperWeek Europe.pdfPaige Cruz
Monitoring and observability aren’t traditionally found in software curriculums and many of us cobble this knowledge together from whatever vendor or ecosystem we were first introduced to and whatever is a part of your current company’s observability stack.
While the dev and ops silo continues to crumble….many organizations still relegate monitoring & observability as the purview of ops, infra and SRE teams. This is a mistake - achieving a highly observable system requires collaboration up and down the stack.
I, a former op, would like to extend an invitation to all application developers to join the observability party will share these foundational concepts to build on:
The Art of the Pitch: WordPress Relationships and SalesLaura Byrne
Clients don’t know what they don’t know. What web solutions are right for them? How does WordPress come into the picture? How do you make sure you understand scope and timeline? What do you do if sometime changes?
All these questions and more will be explored as we talk about matching clients’ needs with what your agency offers without pulling teeth or pulling your hair out. Practical tips, and strategies for successful relationship building that leads to closing the deal.
Encryption in Microsoft 365 - ExpertsLive Netherlands 2024Albert Hoitingh
In this session I delve into the encryption technology used in Microsoft 365 and Microsoft Purview. Including the concepts of Customer Key and Double Key Encryption.
A tale of scale & speed: How the US Navy is enabling software delivery from l...sonjaschweigert1
Rapid and secure feature delivery is a goal across every application team and every branch of the DoD. The Navy’s DevSecOps platform, Party Barge, has achieved:
- Reduction in onboarding time from 5 weeks to 1 day
- Improved developer experience and productivity through actionable findings and reduction of false positives
- Maintenance of superior security standards and inherent policy enforcement with Authorization to Operate (ATO)
Development teams can ship efficiently and ensure applications are cyber ready for Navy Authorizing Officials (AOs). In this webinar, Sigma Defense and Anchore will give attendees a look behind the scenes and demo secure pipeline automation and security artifacts that speed up application ATO and time to production.
We will cover:
- How to remove silos in DevSecOps
- How to build efficient development pipeline roles and component templates
- How to deliver security artifacts that matter for ATO’s (SBOMs, vulnerability reports, and policy evidence)
- How to streamline operations with automated policy checks on container images
UiPath Test Automation using UiPath Test Suite series, part 6DianaGray10
Welcome to UiPath Test Automation using UiPath Test Suite series part 6. In this session, we will cover Test Automation with generative AI and Open AI.
UiPath Test Automation with generative AI and Open AI webinar offers an in-depth exploration of leveraging cutting-edge technologies for test automation within the UiPath platform. Attendees will delve into the integration of generative AI, a test automation solution, with Open AI advanced natural language processing capabilities.
Throughout the session, participants will discover how this synergy empowers testers to automate repetitive tasks, enhance testing accuracy, and expedite the software testing life cycle. Topics covered include the seamless integration process, practical use cases, and the benefits of harnessing AI-driven automation for UiPath testing initiatives. By attending this webinar, testers, and automation professionals can gain valuable insights into harnessing the power of AI to optimize their test automation workflows within the UiPath ecosystem, ultimately driving efficiency and quality in software development processes.
What will you get from this session?
1. Insights into integrating generative AI.
2. Understanding how this integration enhances test automation within the UiPath platform
3. Practical demonstrations
4. Exploration of real-world use cases illustrating the benefits of AI-driven test automation for UiPath
Topics covered:
What is generative AI
Test Automation with generative AI and Open AI.
UiPath integration with generative AI
Speaker:
Deepak Rai, Automation Practice Lead, Boundaryless Group and UiPath MVP
5. What are the Windowing Functions?
• Similar to classical aggregates but does more!
• Provides access to set of rows from the current row
• Introduced SQL:2003 and more detail in SQL:2008
– SQL:2008 (http://wiscorp.com/sql200n.zip) 02: SQL/Foundation
• 4.14.9 Window tables
• 4.15.3 Window functions
• 6.10 <window function>
• 7.8 <window clause>
• Supported by Oracle, SQL Server, Sybase and DB2
– No open source RDBMS so far except PostgreSQL (Firebird trying)
• Used in OLAP mainly but also useful in OLTP
– Analysis and reporting by rankings, cumulative aggregates
PGCon2009, 21-22 May 2009 Ottawa
6. How they work and what you get
PGCon2009, 21-22 May 2009 Ottawa
7. How they work and what do you get
• Windowed table
– Operates on a windowed table
– Returns a value for each row
– Returned value is calculated from the rows in the window
PGCon2009, 21-22 May 2009 Ottawa
8. How they work and what do you get
• You can use…
– New window functions
– Existing aggregate functions
– User-defined window functions
– User-defined aggregate functions
PGCon2009, 21-22 May 2009 Ottawa
9. How they work and what do you get
• Completely new concept!
– With Windowing Functions, you can reach outside the current row
PGCon2009, 21-22 May 2009 Ottawa
10. How they work and what you get
[Aggregates] SELECT key, SUM(val) FROM tbl GROUP BY key;
PGCon2009, 21-22 May 2009 Ottawa
11. How they work and what you get
[Windowing Functions] SELECT key, SUM(val) OVER (PARTITION BY key) FROM tbl;
PGCon2009, 21-22 May 2009 Ottawa
12. How they work and what you get
Who is the highest paid relatively compared with the department average?
depname empno salary
develop 10 5200
sales 1 5000
personnel 5 3500
sales 4 4800
sales 6 550
personnel 2 3900
develop 7 4200
develop 9 4500
sales 3 4800
develop 8 6000
develop 11 5200
PGCon2009, 21-22 May 2009 Ottawa
13. How they work and what you get
SELECT depname, empno, salary, avg(salary) OVER (PARTITION BY depname)::int,
salary - avg(salary) OVER (PARTITION BY depname)::int AS diff
FROM empsalary ORDER BY diff DESC
depname empno salary avg diff
develop 8 6000 5020 980
sales 6 5500 5025 475
personnel 2 3900 3700 200
develop 11 5200 5020 180
develop 10 5200 5020 180
sales 1 5000 5025 -25
personnel 5 3500 3700 -200
sales 4 4800 5025 -225
sales 3 4800 5025 -225
develop 9 4500 5020 -520
develop 7 4200 5020 -820
PGCon2009, 21-22 May 2009 Ottawa
14. Anatomy of a Window
• Represents set of rows abstractly as:
• A partition
– Specified by PARTITION BY clause
– Never moves
– Can contain:
• A frame
– Specified by ORDER BY clause and frame clause
– Defined in a partition
– Moves within a partition
– Never goes across two partitions
• Some functions take values from a partition.
Others take them from a frame.
PGCon2009, 21-22 May 2009 Ottawa
15. A partition
• Specified by PARTITION BY clause in OVER()
• Allows to subdivide the table, much like
GROUP BY clause
• Without a PARTITION BY clause, the whole
table is in a single partition
func
(args)
OVER ( )
partition-clause order-
clause
frame-
clause
PARTITION BY expr, …
PGCon2009, 21-22 May 2009 Ottawa
16. A frame
• Specified by ORDER BY clause and frame
clause in OVER()
• Allows to tell how far the set is applied
• Also defines ordering of the set
• Without order and frame clauses, the whole of
partition is a single frame
func
(args)
OVER ( )
partition-clause order-
clause
frame-
clause
ORDER BY expr [ASC|DESC] (ROWS|RANGE) BETWEEN UNBOUNDED
[NULLS FIRST|LAST], … (PRECEDING|FOLLOWING) AND CURRENT
ROW
PGCon2009, 21-22 May 2009 Ottawa
17. The WINDOW clause
• You can specify common window definitions
in one place and name it, for convenience
• You can use the window at the same query
level
SELECT target_list, … wfunc() OVER w
FROM table_list, …
WHERE qual_list, ….
GROUP BY groupkey_list, ….
HAVING groupqual_list, …
WINDOW w A ( )
S
partition-clause order-
clause
frame-
clause
PGCon2009, 21-22 May 2009 Ottawa
20. row_number()
• Returns number of the current row
SELECT val, row_number() OVER (ORDER BY val DESC) FROM tbl;
val row_number()
5 1
5 2
3 3
1 4
Note: row_number() always incremented values independent of frame
PGCon2009, 21-22 May 2009 Ottawa
21. rank()
• Returns rank of the current row with gap
SELECT val, rank() OVER (ORDER BY val DESC) FROM tbl;
val rank()
5 1
gap
5 1
3 3
1 4
Note: rank() OVER(*empty*) returns 1 for all rows, since all rows
are peers to each other
PGCon2009, 21-22 May 2009 Ottawa
22. dense_rank()
• Returns rank of the current row without gap
SELECT val, dense_rank() OVER (ORDER BY val DESC) FROM tbl;
val dense_rank()
5 1
no gap
5 1
3 2
1 3
Note: dense_rank() OVER(*empty*) returns 1 for all rows, since all rows
are peers to each other
PGCon2009, 21-22 May 2009 Ottawa
23. percent_rank()
• Returns relative rank; (rank() – 1) / (total row – 1)
SELECT val, percent_rank() OVER (ORDER BY val DESC) FROM tbl;
val percent_rank()
5 0
5 0
3 0.666666666666667
1 1
values are always between 0 and 1 inclusive.
Note: percent_rank() OVER(*empty*) returns 0 for all rows, since all rows
are peers to each other
PGCon2009, 21-22 May 2009 Ottawa
24. cume_dist()
• Returns relative rank; (# of preced. or peers) / (total row)
SELECT val, cume_dist() OVER (ORDER BY val DESC) FROM tbl;
val cume_dist()
5 0.5 =2/4
5 0.5 =2/4
3 0.75 =3/4
1 1 =4/4
The result can be emulated by
“count(*) OVER (ORDER BY val DESC) / count(*) OVER ()”
Note: cume_dist() OVER(*empty*) returns 1 for all rows, since all rows
are peers to each other
PGCon2009, 21-22 May 2009 Ottawa
25. ntile()
• Returns dividing bucket number
SELECT val, ntile(3) OVER (ORDER BY val DESC) FROM tbl;
val ntile(3)
5 1 4%3=1
5 1
3 2
1 3
The results are the divided positions, but if there’s remainder add
row from the head
Note: ntile() OVER (*empty*) returns same values as above, since
ntile() doesn’t care the frame but works against the partition
PGCon2009, 21-22 May 2009 Ottawa
26. lag()
• Returns value of row above
SELECT val, lag(val) OVER (ORDER BY val DESC) FROM tbl;
val lag(val)
5 NULL
5 5
3 5
1 3
Note: lag() only acts on a partition.
PGCon2009, 21-22 May 2009 Ottawa
27. lead()
• Returns value of the row below
SELECT val, lead(val) OVER (ORDER BY val DESC) FROM tbl;
val lead(val)
5 5
5 3
3 1
1 NULL
Note: lead() acts against a partition.
PGCon2009, 21-22 May 2009 Ottawa
28. first_value()
• Returns the first value of the frame
SELECT val, first_value(val) OVER (ORDER BY val DESC) FROM tbl;
val first_value(val)
5 5
5 5
3 5
1 5
PGCon2009, 21-22 May 2009 Ottawa
29. last_value()
• Returns the last value of the frame
SELECT val, last_value(val) OVER
(ORDER BY val DESC ROWS BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING) FROM tbl;
val last_value(val)
5 1
5 1
3 1
1 1
Note: frame clause is necessary since you have a frame between
the first row and the current row by only the order clause
PGCon2009, 21-22 May 2009 Ottawa
30. nth_value()
• Returns the n-th value of the frame
SELECT val, nth_value(val, val) OVER
(ORDER BY val DESC ROWS BETWEEN UNBOUNDED PRECEDING
AND UNBOUNDED FOLLOWING) FROM tbl;
val nth_value(val, val)
5 NULL
5 NULL
3 3
1 5
Note: frame clause is necessary since you have a frame between
the first row and the current row by only the order clause
PGCon2009, 21-22 May 2009 Ottawa
31. aggregates(all peers)
• Returns the same values along the frame
SELECT val, sum(val) OVER () FROM tbl;
val sum(val)
5 14
5 14
3 14
1 14
Note: all rows are the peers to each other
PGCon2009, 21-22 May 2009 Ottawa
32. cumulative aggregates
• Returns different values along the frame
SELECT val, sum(val) OVER (ORDER BY val DESC) FROM tbl;
val sum(val)
5 10
5 10
3 13
1 14
Note: row#1 and row#2 return the same value since they are the peers.
the result of row#3 is sum(val of row#1…#3)
PGCon2009, 21-22 May 2009 Ottawa
37. E1 List Table
CREATE TABLE employee(
id INTEGER NOT NULL,
boss_id INTEGER,
UNIQUE(id, boss_id)/*, etc., etc. */
);
INSERT INTO employee(id, boss_id)
VALUES(1,NULL), /* El capo di tutti capi */
(2,1),(3,1),(4,1),
(5,2),(6,2),(7,2),(8,3),(9,3),(10,4),
(11,5),(12,5),(13,6),(14,7),(15,8),
(1,9);
38. Tree Query Initiation
WITH RECURSIVE t(node, path) AS (
SELECT id, ARRAY[id] FROM employee WHERE boss_id IS NULL
/* Initiation Step */
UNION ALL
SELECT e1.id, t.path || ARRAY[e1.id]
FROM employee e1 JOIN t ON (e1.boss_id = t.node)
WHERE id NOT IN (t.path)
)
SELECT
CASE WHEN array_upper(path,1)>1 THEN '+-' ELSE '' END ||
REPEAT('--', array_upper(path,1)-2) ||
node AS "Branch"
FROM t
ORDER BY path;
39. Tree Query Recursion
WITH RECURSIVE t(node, path) AS (
SELECT id, ARRAY[id] FROM employee WHERE boss_id IS NULL
UNION ALL
SELECT e1.id, t.path || ARRAY[e1.id] /* Recursion */
FROM employee e1 JOIN t ON (e1.boss_id = t.node)
WHERE id NOT IN (t.path)
)
SELECT
CASE WHEN array_upper(path,1)>1 THEN '+-' ELSE '' END ||
REPEAT('--', array_upper(path,1)-2) ||
node AS "Branch"
FROM t
ORDER BY path;
40. Tree Query
Termination
WITH RECURSIVE t(node, path) AS (
SELECT id, ARRAY[id] FROM employee WHERE boss_id IS NULL
UNION ALL
SELECT e1.id, t.path || ARRAY[e1.id]
FROM employee e1 JOIN t ON (e1.boss_id = t.node)
WHERE id NOT IN (t.path) /* Termination Condition */
)
SELECT
CASE WHEN array_upper(path,1)>1 THEN '+-' ELSE '' END ||
REPEAT('--', array_upper(path,1)-2) ||
node AS "Branch"
FROM t
ORDER BY path;
41. Tree Query Display
WITH RECURSIVE t(node, path) AS (
SELECT id, ARRAY[id] FROM employee WHERE boss_id IS NULL
UNION ALL
SELECT e1.id, t.path || ARRAY[e1.id]
FROM employee e1 JOIN t ON (e1.boss_id = t.node)
WHERE id NOT IN (t.path)
)
SELECT
CASE WHEN array_upper(path,1)>1 THEN '+-' ELSE '' END ||
REPEAT('--', array_upper(path,1)-2) ||
node AS "Branch" /* Display */
FROM t
ORDER BY path;
43. Travelling Salesman Problem
Given a number of cities and the costs of travelling
from any city to any other city, what is the least-
cost round-trip route that visits each city exactly
once and then returns to the starting city?
44. TSP Schema
CREATE TABLE pairs (
from_city TEXT NOT NULL,
to_city TEXT NOT NULL,
distance INTEGER NOT NULL,
PRIMARY KEY(from_city, to_city),
CHECK (from_city < to_city)
);
45. TSP Data
INSERT INTO pairs
VALUES
('Bari','Bologna',672),
('Bari','Bolzano',939),
('Bari','Firenze',723),
('Bari','Genova',944),
('Bari','Milan',881),
('Bari','Napoli',257),
('Bari','Palermo',708),
('Bari','Reggio Calabria',464),
....
46. TSP Program:
Symmetric Setup
WITH RECURSIVE both_ways(
from_city,
to_city,
distance
) /* Working Table */
AS (
SELECT
from_city,
to_city,
distance
FROM
pairs
UNION ALL
SELECT
to_city AS "from_city",
from_city AS "to_city",
distance
FROM
pairs
),
47. TSP Program:
Symmetric Setup
WITH RECURSIVE both_ways(
from_city,
to_city,
distance
)
AS (/* Distances One Way */
SELECT
from_city,
to_city,
distance
FROM
pairs
UNION ALL
SELECT
to_city AS "from_city",
from_city AS "to_city",
distance
FROM
pairs
),
48. TSP Program:
Symmetric Setup
WITH RECURSIVE both_ways(
from_city,
to_city,
distance
)
AS (
SELECT
from_city,
to_city,
distance
FROM
pairs
UNION ALL /* Distances Other Way */
SELECT
to_city AS "from_city",
from_city AS "to_city",
distance
FROM
pairs
),
49. TSP Program:
Path Initialization Step
paths (
from_city,
to_city,
distance,
path
)
AS (
SELECT
from_city,
to_city,
distance,
ARRAY[from_city] AS "path"
FROM
both_ways b1
WHERE
b1.from_city = 'Roma'
UNION ALL
50. TSP Program:
Path Recursion Step
SELECT
b2.from_city,
b2.to_city,
p.distance + b2.distance,
p.path || b2.from_city
FROM
both_ways b2
JOIN
paths p
ON (
p.to_city = b2.from_city
AND
b2.from_city <> ALL (p.path[
2:array_upper(p.path,1)
]) /* Prevent re-tracing */
AND
array_upper(p.path,1) < 6
)
)
51. TSP Program:
Timely Termination Step
SELECT
b2.from_city,
b2.to_city,
p.distance + b2.distance,
p.path || b2.from_city
FROM
both_ways b2
JOIN
paths p
ON (
p.to_city = b2.from_city
AND
b2.from_city <> ALL (p.path[
2:array_upper(p.path,1)
]) /* Prevent re-tracing */
AND
array_upper(p.path,1) < 6 /* Timely Termination */
)
)
52. TSP Program:
Filter and Display
SELECT
path || to_city AS "path",
distance
FROM
paths
WHERE
to_city = 'Roma'
AND
ARRAY['Milan','Firenze','Napoli'] <@ path
ORDER BY distance, path
LIMIT 1;
53. TSP Program:
Filter and Display
davidfetter@tsp=# i travelling_salesman.sql
path | distance
----------------------------------+----------
{Roma,Firenze,Milan,Napoli,Roma} | 1553
(1 row)
Time: 11679.503 ms
55. Hausdorf-Besicovich-
WTF Dimension
WITH RECURSIVE
Z(Ix, Iy, Cx, Cy, X, Y, I)
AS (
SELECT Ix, Iy, X::float, Y::float, X::float, Y::float, 0
FROM
(SELECT -2.2 + 0.031 * i, i FROM generate_series(0,101) AS i) AS xgen(x,ix)
CROSS JOIN
(SELECT -1.5 + 0.031 * i, i FROM generate_series(0,101) AS i) AS ygen(y,iy)
UNION ALL
SELECT Ix, Iy, Cx, Cy, X * X - Y * Y + Cx AS X, Y * X * 2 + Cy, I + 1
FROM Z
WHERE X * X + Y * Y < 16::float
AND I < 100
),
56. Filter
Zt (Ix, Iy, I) AS (
SELECT Ix, Iy, MAX(I) AS I
FROM Z
GROUP BY Iy, Ix
ORDER BY Iy, Ix
)
57. Display
SELECT array_to_string(
array_agg(
SUBSTRING(
' .,,,-----++++%%%%@@@@#### ',
GREATEST(I,1)
),''
)
FROM Zt
GROUP BY Iy
ORDER BY Iy;