在PostgreSQL中使用plpython3u创建函数时访问边属性的问题



我为以下查询实现python函数,我得到正确的答案,但是当我尝试通过使用plpython3u扩展在postgreSQL中创建函数时,它没有任何错误,但仍然不将结果返回到表。

另外,我应该提到,当我想返回所有路径时,下面的函数可以工作,我的意思是不考虑以下部分:

for i in range(len(flight) - 1):
arrival_time = datetime.strptime(flight[i]["arrival_time"], '%Y-%m-%d %H:%M')
next_departure_time=datetime.strptime(flight[i+1]"departure_time"], '%Y-%m-%d %H:%M')
if arrival_time < next_departure_time:
is_continuous = False
break

这就是为什么我认为问题是我想如何访问边缘属性。有人知道我怎么修吗?

CREATE OR REPLACE FUNCTION graph.paircpath(
origin text,
edge text,
destination text)
RETURNS TABLE(city1 ag_catalog.agtype, airport1 ag_catalog.agtype, flight ag_catalog.agtype, airport2 ag_catalog.agtype, city2 ag_catalog.agtype) 
LANGUAGE 'plpython3u'
COST 100
VOLATILE PARALLEL UNSAFE
ROWS 1000
AS $BODY$
from datetime import datetime
import age
import psycopg2
def paircpath(origin, edge, destination):
conn = psycopg2.connect(host="localhost", port="5432", dbname="postgres", user="postgres", password="13711992")
with conn.cursor() as cursor:
try:
cursor.execute("SET search_path = ag_catalog, public, graph;")
cursor.execute("LOAD 'age';")
cursor.execute("GRANT USAGE ON SCHEMA ag_catalog TO postgres;")
query = f"""SELECT * FROM cypher('graph', $$ 
MATCH (a)-[:LocatedAt*]->(c:City {{name: '{origin}'}})
MATCH (a:Airport)-[e:{edge}]->(b:Airport) 
MATCH (b)-[:LocatedAt*]->(c1:City {{name: '{destination}'}}) 
RETURN c, a, e, b, c1 
$$) AS (city1 agtype, airport1 agtype, flight agtype, airport2 agtype, city2 agtype); """
cursor.execute(query)
paths = cursor.fetchall()
for row in paths:
city1 = row[0]
airport1 = row[1]
flight = row[2]
airport2 = row[3]
city2 = row[4]
is_continuous = True
for i in range(len(flight) - 1):
arrival_time = datetime.strptime(flight[i]["arrival_time"], '%Y-%m-%d %H:%M')
next_departure_time = datetime.strptime(flight[i+1]["departure_time"], '%Y-%m-%d %H:%M')
if arrival_time < next_departure_time:
is_continuous = False
break
if is_continuous:
yield (city1, airport1, flight, airport2, city2)
except Exception as ex:
print(type(ex), ex)
for result in paircpath(origin, edge, destination):
yield result
$BODY$;
ALTER FUNCTION graph.paircpath(text, text, text)
OWNER TO postgres;

函数不返回,只是像下面这样运行:结果

而是通过去掉条件:

for i in range(len(flight) - 1):
arrival_time = datetime.strptime(flight[i]["arrival_time"], '%Y-%m-%d %H:%M')
next_departure_time=datetime.strptime(flight[i+1]"departure_time"], '%Y-%m-%d %H:%M')
if arrival_time < next_departure_time:
is_continuous = False
break

函数显示所有路径,如下所示:无条件结果

似乎你们检查航班是否连续的方式有问题。修改以下代码:

if arrival_time < next_departure_time:
is_continuous = False
break

到代码:

if arrival_time > next_departure_time:
is_continuous = False
break

最新更新