小编Oli*_*ice的帖子

SQLAlchemy 嵌套 CTE 查询

sqlalchemy 核心查询构建器似乎取消嵌套并将 CTE 查询重新定位到已编译 sql 的“顶部”。

我正在转换现有的 Postgres 查询,该查询选择深度连接的数据作为单个 JSON 对象。语法非常人为,但它显着减少了大型查询的网络开销。目标是使用 sqlalchemy 核心查询构建器动态构建查询。

这是嵌套 CTE 的最小工作示例

with res_cte as (
    select
        account_0.name acct_name,
        (
            with offer_cte as (
                select
                    offer_0.id
                from
                    offer offer_0
                where
                    offer_0.account_id = account_0.id
            )
            select
                array_agg(offer_cte.id)
            from
                offer_cte
        ) as offer_arr
    from
        account account_0
)
select 
    acct_name::text, offer_arr::text
from res_cte
Run Code Online (Sandbox Code Playgroud)

结果

acct_name,  offer_arr
---------------------
oliver,     null
rachel,     {3}
buddy,      {4,5}
Run Code Online (Sandbox Code Playgroud)

(我的错误使用)核心查询构建器尝试取消嵌套offer_cte并导致每个offer.id都与结果中的每个相关联account_name

无需在答案中重新实现这个确切的查询,任何导致类似嵌套 CTE 的示例都是完美的。

python sql postgresql sqlalchemy

6
推荐指数
1
解决办法
345
查看次数

Tensorflow 采样 Softmax 损失正确用法

在具有许多类的分类问题中,tensorflow 文档建议使用sampled_softmax_loss而不是简单的softmax来减少训练运行时间。

根据文档代码(第1180行),sampled_softmax_loss的调用模式是:

tf.nn.sampled_softmax_loss(weights, # Shape (num_classes, dim)     - floatXX
                     biases,        # Shape (num_classes)          - floatXX 
                     labels,        # Shape (batch_size, num_true) - int64
                     inputs,        # Shape (batch_size, dim)      - floatXX  
                     num_sampled,   # - int
                     num_classes,   # - int
                     num_true=1,  
                     sampled_values=None,
                     remove_accidental_hits=True,
                     partition_strategy="mod",
                     name="sampled_softmax_loss")
Run Code Online (Sandbox Code Playgroud)

目前尚不清楚(至少对我来说)如何将现实世界的问题转换为该损失函数所需的形状。我认为“输入”字段是问题所在。

这是一个复制粘贴就绪的最小工作示例,在调用损失函数时会抛出矩阵乘法形状错误。

import tensorflow as tf

# Network Parameters
n_hidden_1 = 256  # 1st layer number of features
n_input = 784     # MNIST data input (img shape: 28*28)
n_classes …
Run Code Online (Sandbox Code Playgroud)

python tensorflow

5
推荐指数
1
解决办法
3822
查看次数

如何检查是否为 postgres 中的表启用了行级安全性

在 postgres 中的表上启用行级安全性非常简单:

alter table some_table enable row level security;
Run Code Online (Sandbox Code Playgroud)

您将如何检查给定模式中的哪些表启用了行级安全性(用于测试)?

database postgresql row-level-security

3
推荐指数
1
解决办法
1158
查看次数

Postgres 使用带有“record/”类型参数的函数检查约束

Postgres 检查约束可能引用当前行中的列,但不清楚如何引用整个record以使用以 arecord作为参数的函数

例如,用于计算记录中非空条目数量的通用检查约束:

-- Function that counts the number of non-null entries in a row
create function count_non_null(rec record) returns int as $$
begin
    return count(v) from json_each(row_to_json(rec)) x(k_, v) where json_typeof(x.v) <> 'null';
end;
$$ language plpgsql immutable strict;


-- Check constraint asserting that only 3 values may be set on each row
alter table some_table add constraint ck_three_key check (
    count_non_null(CURRENT_ROW) = 3 -- invalid
);
Run Code Online (Sandbox Code Playgroud)

在此上下文中不允许使用 CURRENT_ROW。有什么想法如何将当前行传递给检查约束内的函数吗?

sql postgresql

3
推荐指数
1
解决办法
1779
查看次数