如何根据 Snowflake 中的表达式定义簇键?

How to define a cluster key based on a expression in Snowflake?

根据 Snowflake 文档 (https://docs.snowflake.net/manuals/user-guide/tables-clustering-keys.html),一个簇键可以定义为一个或多个 table columns/expressions。 他们带来的例子是:

-- cluster by expressions
create or replace table t2 (c1 timestamp, c2 string, c3 number) cluster by (to_date(c1), substring(c2, 0, 10));

我想从日期列中提取年、月和日,并根据这些表达式创建一个簇键,但没有找到解决方法。 这是我已经尝试过的:

 CREATE TABLE TBL_DATECREATED  (DATECREATED_UTC)  
 CLUSTER BY (
              TO_DATE(DATECREATED_UTC)
              )  
 AS
 SELECT DATECREATED_UTC FROM BASETABLE_CONTACTS

结果:

 SQL compilation error: invalid type [TO_DATE(TBL_DATECREATED.DATECREATED_UTC)] for parameter 'TO_DATE'

**Mention: SELECT TO_DATE(DATECREATED_UTC) FROM BASETABLE_CONTACTS works fine!

CREATE MATERIALIZED VIEW MV_DATECREATED (DATECREATED_UTC, EMAILADDRESS)
CLUSTER BY ( year(DATECREATED_UTC)
             -- extract(year from DATECREATED_UTC)
             ,EMAILADDRESS
            )
AS
SELECT DATECREATED_UTC, EMAILADDRESS FROM BASETABLE_CONTACTS 

结果:

SQL compilation error: Function EXTRACT does not support UNKNOWN argument type
(for commented expression i received the same error message)

CREATE MATERIALIZED VIEW MV_DATECREATED (DATECREATED_UTC, EMAILADDRESS)
CLUSTER BY (  DATECREATED_UTC
             ,substring(EMAILADDRESS, 1, 3)
            )
AS
SELECT DATECREATED_UTC, EMAILADDRESS FROM BASETABLE_CONTACTS 

结果:

SQL compilation error: error line 3 at position 14 Invalid argument types for function 'SUBSTRING': (UNKNOWN, NUMBER(1,0), NUMBER(1,0))

预先感谢每个 suggestion/solution!

尝试以下操作,当您在创建 table 的同时定义聚簇键时,也许 Snowflake 无法正确确定列的数据类型?

CREATE MATERIALIZED VIEW MV_DATECREATED (DATECREATED_UTC timestamp, EMAILADDRESS varchar)
CLUSTER BY ( year(DATECREATED_UTC)
             -- extract(year from DATECREATED_UTC)
             ,EMAILADDRESS
            )
AS
SELECT DATECREATED_UTC, EMAILADDRESS FROM BASETABLE_CONTACTS 

对于第一个错误,请尝试将数据类型添加到创建 table。例如:

CREATE TABLE TBL_DATECREATED  (DATECREATED_UTC timestamptz)

对于第二个和第三个问题,请检查数据类型是否符合您的预期。

我们必须使用以下语句来定义基于物化视图表达式的簇键。

CREATE or replace MATERIALIZED VIEW MV_DATECREATED (DATECREATED_UTC,EMAILADDRESS) cluster by(DATECREATED_UTC,EMAILADDRESS ) AS SELECT to_date(DATECREATED_UTC), EMAILADDRESS FROM BASETABLE_CONTACTS;

CREATE or replace MATERIALIZED VIEW MV_DATECREATED (DATECREATED_UTC,EMAILADDRESS) cluster by(DATECREATED_UTC,EMAILADDRESS ) AS SELECT DATECREATED_UTC, EMAILADDRESS FROM BASETABLE_CONTACTS;

alter materialized view MV_DATECREATED cluster by(TO_DATE(DATECREATED_UTC),EMAILADDRESS );