如何防止人们在雪花桌上跑步 *?

发布于 2025-02-13 14:05:16 字数 463 浏览 0 评论 0原文

出于合规性,我们要阻止SQL分析师在表上运行选择 *。相反,我们要强迫他们明确要求他们选择的列。我该如何用雪花执行此操作?

我使用计算出的列看到了SQL Server的提示,雪花有等效吗?

For compliance reasons we want to block SQL analysts from running SELECT * on a table. Instead, we want to force them to explicitly ask for the columns they want to select. How can I enforce this with Snowflake?

I saw a tip for SQL server using a calculated column, does Snowflake have an equivalent?

enter image description here

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

痞味浪人 2025-02-20 14:05:18

这是一个解决方法,从定义上讲,解决方法不是理想的解决方案。我想知道的是,是否有某种方法可以向用户获取一条消息,即select *是导致错误的选择。我尝试了引发错误的JS UDF,但不能用作列的默认值。

可以使用截断错误显示自定义消息:

create or replace table mytable (
    i number, s string
    , no_select_star string as ('Code smell: SELECT * '::CHAR(1))
);
    
INSERT INTO mytable(i, s)  VALUES (1, 'a');

QUERY:

SELECT * FROM  mytable;

QUERY: utume:

“在此处输入图像说明”

it's a workaround and by definition workarounds are not ideal solutions. What I'm wondering is if there's some way to get a message to the user that it's the select * that's causing the error. I tried a JS UDF that throws an error, but that can't be used as a default for a column.

It is possible to use truncation error to display custom message:

create or replace table mytable (
    i number, s string
    , no_select_star string as ('Code smell: SELECT * '::CHAR(1))
);
    
INSERT INTO mytable(i, s)  VALUES (1, 'a');

Query:

SELECT * FROM  mytable;

Output:

enter image description here

眼眸里的那抹悲凉 2025-02-20 14:05:18

为什么不使用行访问策略?它可能需要进行一些调整,但是您可以创建类似的行访问策略:

create or replace row access policy test_policy as (val varchar) returns boolean ->
  case
    when lower(current_statement()) like '%select%*%' 
  then false else true end;

如果查询中存在选择 *,将此策略应用于表格将不会返回任何记录。您可以将此策略应用于每个表,并且不会以任何方式影响您的架构。

Why not use a row access policy, instead? It might take some tweaking, but you could create a row access policy similar to:

create or replace row access policy test_policy as (val varchar) returns boolean ->
  case
    when lower(current_statement()) like '%select%*%' 
  then false else true end;

Applying this policy to a table would not return any records if a select * was present in the query. You could apply this policy to every table and it wouldn't affect your schema in any way.

旧梦荧光笔 2025-02-20 14:05:18

这是另一种方法。

  1. 没有狡猾的列

  2. 无模式污染

  3. 向用户自定义的教育消息

  4. 本地雪花功能

  5. 手柄' *'在任何位置(选择col, * ...)

  6. 的手柄' *'确实需要更多的思考来设置 - >必须应用于每列

  7. 可以使用一个新的错误消息更新所有表格。

  8. 可扩展/可自动加以包含其他可爱的用户梦dream以求的任何其他狡猾的sql

  9. 您可以学习

  10. 要在生产中使用您需要处理所有数据类型 - >如果整个桌子都有动态面膜,那会很好。

  11. 仍然在后台运行SQL,但希望在用户厌倦了看到“坏坏”之后,他们会改变自己的方式。

  12. 允许您豁免某些用户(例如,将自己豁免为“选择前10 *”)

     创建或替换屏蔽策略select_star_bad_bad_bad_bad as(val string) 
    返回字符串 - >
    案件
       当regexp_count(current_statement(),$ \ *+$)
      否则瓦尔
    结尾;
    
    Alter表如果存在mytable1修改列S设置掩码策略select_star_bad_bad_bad_bad;
     

Here's an alternative approach.

  1. No dodgy columns

  2. No schema pollution

  3. Education message to user customisable

  4. Native Snowflake functionality

  5. Handles '*' in any position (select col , * ...)

  6. Does take more thought to set up -> must be applied to each column

  7. Can update ALL your tables with new error messages in one fell swoop

  8. Extendable/adaptable to include any other dodgy SQL your lovely users dream up

  9. You get to learn about Dynamic Masking which is super cool!

  10. To use in production you'd need to handle ALL datatypes -> would be nice if there was a Dynamic Mask for the entire table.

  11. Still runs the SQL in background but hopefully after the users get sick of seeing 'bad bad bad' they'll change their ways.

  12. Allows you to exempt SOME users (e.g. exempt yourself for select top 10 *)

    create or replace masking policy select_star_bad_bad_bad as (val string) 
    returns string ->
    case
       when REGEXP_COUNT(current_statement(),$\*+$) >0 then'select * = bad bad bad'
      else val
    end;
    
    alter table if exists mytable1 modify column s set masking policy select_star_bad_bad_bad;
    
白首有我共你 2025-02-20 14:05:17

当然,您可以在Snowflake中使用派生/计算的列创建表:

create or replace table mytable (
    i number, s string
    , no_select_star number as (1/0));

一旦该表具有数据,您将无法在其上运行选择 *,因为分别为0是一个无效的数字:

“在此处输入图像说明”

您还可以将计算的列附加到现有表中,以获得相同的效果

alter table mytable2
add column no_select_star number as (1/0);

< a href =“ https://i.sstatic.net/zfe9g.png” rel =“ noreferrer”> “在此处输入图像说明”

Sure, you can create tables with derived/computed columns in Snowflake:

create or replace table mytable (
    i number, s string
    , no_select_star number as (1/0));

Once that table has data, you won't be able to run select * on it, as the division by 0 is an invalid number:

enter image description here

You can also append a computed column to an existing table for the same effects:

alter table mytable2
add column no_select_star number as (1/0);

In action:

enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文