如何强制雪花用户使用列值(例如日期/时间戳)从表中过滤结果?

发布于 2025-02-13 19:04:49 字数 418 浏览 0 评论 0原文

在讨论如何停止雪花用户运行select 在表上,我们得到了一个<一个href =“ https://twitter.com/rahulj51/status/15449427654774333345” rel =“ noreferrer”>相关问题日期/时间戳列“

那么,我如何在查询表格时强迫雪花用户使用一系列日期?

When discussing how to stop Snowflake users from running select * on a table, we got a related question: "If only there was a way to do something similar to enforce a where clause on the date/timestamp column"

enter image description here

So how I can force Snowflake users to use a range of dates when querying a table?

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

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

发布评论

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

评论(2

GRAY°灰色天空 2025-02-20 19:04:50

注意:检查使用表UDFS

的清洁剂解决方案


解决此问题的一种方法是创建一个视图在桌子上。此视图的第一行应该充满无效的计算,并且日期/时间戳在正常日期之外:

create or replace table mytable3(i number, s string, d date);

insert into mytable3 values (1, 2, '2020-01-01');

create or replace secure view mytable3_view 
as
select 'you need to add a date filter'::int i, 'you need to add a date filter'::int s, '1-1-1'::date d
union all
select * 
from mytable3
;

现在,每当有人查询视图时,每当他们不过滤不合适的日期时,它都会丢弃描述性错误:

select i, s
from mytable3_view
--where d > '2000-01-01'
;

-- Numeric value 'you need to add a date filter' is not recognized

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

请注意,使用Secure>安全视图,我们可以管理权限,以便分析师可以以便可以使分析师可以可以仅通过视图获取数据,而无需让它们访问基础表。

额外的学分:

Note: Check a cleaner solution using table UDFS


One way to solve this is to create a view over the table. The first row of this view should be full of invalid computations, and a date/timestamp outside the normal range of dates:

create or replace table mytable3(i number, s string, d date);

insert into mytable3 values (1, 2, '2020-01-01');

create or replace secure view mytable3_view 
as
select 'you need to add a date filter'::int i, 'you need to add a date filter'::int s, '1-1-1'::date d
union all
select * 
from mytable3
;

Now whenever someone queries that view, it will throw a descriptive error whenever they don't filter out the inappropriate date:

select i, s
from mytable3_view
--where d > '2000-01-01'
;

-- Numeric value 'you need to add a date filter' is not recognized

enter image description here

Note that with a secure view we can manage permissions so analysts can only get data through the view, without giving them access to the underlying table.

Extra credits:

对风讲故事 2025-02-20 19:04:50

一个更清洁的替代方案(如

create or replace secure function table_within(since date, until date )
returns table(i number, s string, d date)
as $
select i, s, d
from mytable3
where d between since and until
$;

然后,您可以使用select *从table(function_name(function_name)(function_name(直到))

select * 
from table(table_within('2019-01-01'::date, '2021-01-01'::date))

“在此处输入图像描述”

A cleaner alternative (as suggested on reddit): Create a SQL table function requiring the filtering parameters, and then returns the filtered table:

create or replace secure function table_within(since date, until date )
returns table(i number, s string, d date)
as $
select i, s, d
from mytable3
where d between since and until
$;

Then you can use it with select * from table(function_name(since, until)):

select * 
from table(table_within('2019-01-01'::date, '2021-01-01'::date))

enter image description here

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