横向解决方法:将参数从查询中的其他位置传递给set-returns函数

时间:2011-11-08 16:36:48

标签: sql postgresql join user-defined-functions

在PostgreSQL中,我正在尝试加入一个需要来自查询中其他地方的参数的set-returns函数。我怎样才能重写这个查询,这样就不会产生“对FROM子句条目的无效引用?”理解它,所写的查询需要LATERAL支持,Postgres没有。

drop table if exists questions;
create table questions (
  id int not null primary key,
  user_id int not null
);
insert into questions
  select generate_series(1,1100), (random()*20000)::int;

drop table if exists users;
create table users (
  id int not null primary key
);
insert into users select generate_series(1, 20000);

drop function if exists question_weightings();
create function question_weightings()
returns table (question_id int, weighting int) as $$
  select questions.id, (random()*10)::int
  from questions;
$$ language sql stable;

drop function if exists similarity(int);
create function similarity(weighting int)
returns table (user_id int, score int) as $$
  select users.id,  (random() * $1)::int
  from users;
$$ language sql stable;

select questions.id, qw.weightings
from questions
join question_weightings() as qw
on qw.question_id = questions.id
join similarity(qw.weighting) as sim
on sim.user_id = questions.user_id;

我怀疑答案是在这个帖子中的某个地方: http://archives.postgresql.org/pgsql-general/2011-08/msg00482.php。 但我已经玩过CTE,子查询,OFFSET 0等的各种组合,并且已经空白了;每个组合似乎永远循环调用相似()而不是一次调用它并加入它。

1 个答案:

答案 0 :(得分:2)

您的示例中有几个问题。

  • 您尝试将参数提供给函数,同时加入其结果,这将首先影响函数的输入。这种恶性循环永远不会对校长起作用。

错误信息很清楚:

ERROR:  invalid reference to FROM-clause entry for table "qw"
LINE 5: join similarity(qw.weighting) as sim on sim.user_id = questi...
                        ^
HINT:  There is an entry for table "qw", but it cannot be referenced from this part of the query.

但还有更多:

  • 您无法将整个SET OF值提供给一个值的函数。
  • 您不能使用random()作为STABLE来定义函数。
  • 您的语法不一致。某些表的别名但不适用于其他表。首先要把它拉直。也许你让自己感到困惑。
  • 您混合了标识符weightingweightings。可能是错别字。
  • 如果您打算用 $ n 表示法引用它们,请不要为IN参数命名。这只会产生可能的命名冲突。或者使用不能混淆的名称,例如使用前缀将它们区分开来。

我将你的演示变成了可行的东西:

-- DROP SCHMEMA x CASCADE;
CREATE SCHEMA x

CREATE TABLE x.questions (id int PRIMARY KEY, user_id int NOT NULL);
INSERT INTO x.questions SELECT generate_series(1,11), (random()*20000)::int;

CREATE TABLE x.users (id int PRIMARY KEY);
INSERT INTO x.users SELECT generate_series(1, 200);

CREATE FUNCTION x.question_weighting()
  RETURNS TABLE (question_id int, weighting int) AS 
$BODY$
SELECT q.id, (random()*10)::int
FROM   x.questions q;
$BODY$
  LANGUAGE sql;

CREATE FUNCTION x.similarity(int)
  RETURNS TABLE (user_id int, score int) AS
$BODY$
  SELECT u.id, (random() * $1)::int
  FROM   x.users u;
$BODY$
  LANGUAGE sql;

WITH qqw AS (
    SELECT q.id, q.user_id, qw.weighting
    FROM   x.questions q
    JOIN   x.question_weighting() qw ON qw.question_id = q.id
    -- WHERE  ??
    )
SELECT id, weighting
FROM   qqw
JOIN   (
    SELECT *
    FROM   x.similarity((
        SELECT weighting
        FROM   qqw      
        -- WHERE ??
        LIMIT  1
        ))
    ) sim USING (user_id);

也许这可以在较低的层次上进行简化。