MaxCompute CAST işlevi, bir ifadenin sonucunu hedef veri türüne dönüştürür. Bu konuda CAST işlevinin komut biçimi, parametreleri ve kullanım örnekleri açıklanmaktadır.
Komut biçimi
cast(<expr> as <type>)
Parametre açıklaması
expr: Gereklidir. Dönüştürülecek kaynak veriyi belirtir.
type: Gereklidir. Hedef veri türünü belirtir. Kullanım şekli aşağıdaki gibidir:
cast(double as bigint): DOUBLE değerini BIGINT değerine dönüştürür.cast(string as bigint): Bir dizeyi BIGINT değerine dönüştürür. Dize bir tamsayıyı temsil ediyorsa doğrudan dönüştürülür. Dize bir ondalık sayıyı veya bilimsel gösterimdeki bir sayıyı temsil ediyorsa önce DOUBLE türüne, ardından BIGINT türüne dönüştürülür.cast(string as datetime)veyacast(datetime as string): Varsayılan tarih biçimiyyyy-mm-dd hh:mi:sskullanılır.-
Temel veri türleri ile JSON türleri arasında dönüşüm yapma.
CAST işlevi, JSON türü ile STRING, BIGINT, INT, TINYINT, SMALLINT, DOUBLE, FLOAT, BOOLEAN ve
SQL-TYPEgibi temel veri türleri arasındaki dönüşümleri de destekler. Örnek komutlar aşağıda verilmiştir:cast(json as string): Bir JSON ifadesini STRING türüne dönüştürür. JSON ifadesi ARRAY veya OBJECT olamaz.cast(string as json): Bir STRING değerini STRING türünde bir JSON ifadesine dönüştürür. Bu işlemjson_parsevejson_formatişlevlerinden farklıdır.json_parse, geçerli bir JSON dizesini bir JSON değerine (bu bir JSON OBJECT olabilir) dönüştürür. Buna karşılık,cast(string as json)herhangi bir dizeyi bir JSON dizesine dönüştürür.cast(null as json): NULL bir değeri JSON türüne dönüştürür.cast(json 'null' as ...):json 'null'venullifadeleri SQLnulldeğerine dönüştürülür.
Dönüş değeri açıklaması
Hedef veri türünde bir değer döndürür.
setproject odps.function.strictmode=falsekomutunu çalıştırırsanız işlev, harflerden önce gelen sayıları döndürür.setproject odps.function.strictmode=truekomutunu çalıştırırsanız hata döner.-
Bir değeri Decimal türüne dönüştürürken:
odps.sql.decimal.tostring.trimzero=trueayarını yaparsanız ondalık noktasından sonraki sondaki sıfırlar kaldırılır.-
odps.sql.decimal.tostring.trimzero=falseayarını yaparsanız ondalık noktasından sonraki sondaki sıfırlar korunur.Önemliodps.sql.decimal.tostring.trimzeroparametresi hem tablolardan alınan verileri hem de statik değerleri etkiler.
Kullanım örnekleri
-
Örnek 1: Genel kullanım.
--Returns 1. select cast('1' as bigint); -
Örnek 2: STRING bir değeri BOOLEAN değerine dönüştürme. STRING boşsa
falsedöner; aksi takdirdetruedöner.-
STRING boştur.
select cast("" as boolean); --Returns +------+ | _c0 | +------+ | false | +------+ -
STRING boş değildir.
select cast("false" as boolean); --Returns true +------+ | _c0 | +------+ | true | +------+
-
-
Örnek 3: Bir dizeyi tarihe dönüştürme.
--Convert a string to a date. select cast("2022-12-20" as date); --Returns +------------+ | _c0 | +------------+ | 2022-12-20 | +------------+ --Convert a date string with a time part to a date. select cast("2022-12-20 00:01:01" as date); --Returns +------------+ | _c0 | +------------+ | NULL | +------------+ --To ensure the value is displayed correctly, set the following parameter: set odps.sql.executionengine.enable.string.to.date.full.format= true; select cast("2022-12-20 00:01:01" as date); --Returns +------------+ | _c0 | +------------+ | 2022-12-20 | +------------+NotVarsayılan olarak
odps.sql.executionengine.enable.string.to.date.full.formatparametresifalsedeğerindedir. Saat bölümü içeren bir tarih dizesini dönüştürmek için bu parametreyitrueolarak ayarlayın. -
Örnek 4 (Yanlış komut örneği): Geçersiz kullanım. Dönüşüm başarısız olursa veya desteklenmiyorsa bir özel durum oluşur. Aşağıdaki komut yanlış kullanım örneğidir.
select cast('abc' as bigint); -
Örnek 5:
setproject odps.function.strictmode=falseayarının yapıldığı senaryo örneği.setprojectodps.function.strictmode=false; select cast('123abc'as bigint); --Returns +------------+ |_c0| +------------+ |123| +------------+ -
Örnek 6:
setproject odps.function.strictmode=trueayarının yapıldığı senaryo örneği.setprojectodps.function.strictmode=true; select cast('123abc' as bigint); --Returns FAILED:ODPS-0130071:[0,0]Semanticanalysisexception-physicalplangenerationfailed:java.lang.NumberFormatException:ODPS-0123091:Illegaltypecast-Infunctioncast,value'123abc'cannotbecastedfromStringtoBigint. -
Örnek 7:
odps.sql.decimal.tostring.trimzeroayarının yapıldığı senaryo örneği.--Create a table. create table mf_dot (dcm1 decimal(38,18), dcm2 decimal(38,18)); --Insert data. insert into table mf_dot values (12.45500BD,12.3400BD); --When the flag is true or not set. set odps.sql.decimal.tostring.trimzero=true; --Remove trailing zeros after the decimal point. select cast(round(dcm1,3) as string),cast(round(dcm2,3) as string) from mf_dot; --Return value +------------+------------+ | _c0 | _c1 | +------------+------------+ | 12.455 | 12.34 | +------------+------------+ --When the flag is false. set odps.sql.decimal.tostring.trimzero=false; --Keep trailing zeros after the decimal point. select cast(round(dcm1,3) as string),cast(round(dcm2,3) as string) from mf_dot; --Return value +------------+------------+ | _c0 | _c1 | +------------+------------+ | 12.455 | 12.340 | +------------+------------+ --This parameter also applies to static values. set odps.sql.decimal.tostring.trimzero=false; select cast(round(12345.120BD,3) as string); --Returns: +------------+ | _c0 | +------------+ | 12345.120 | +------------+
-
Örnek 8: STRING ve JSON türleri arasında dönüşüm yapma.
--Convert JSON to string. select cast(json '123' as string); --Returns: +-----+ | _c0 | +-----+ | 123 | +-----+ --Convert JSON to string. select cast(json '"abc"' as string); --Returns: +-----+ | _c0 | +-----+ | abc | +-----+ --Convert JSON to string. select cast(json 'true' as string); --Returns: +-----+ | _c0 | +-----+ | TRUE | +-----+ --Convert JSON to string. select cast(json 'null' as string); --Returns: +-----+ | _c0 | +-----+ | NULL | +-----+ --Convert string to JSON. select cast('{"a":2}' as json); --Returns: +-----+ | _c0 | +-----+ | "{\"a\":2}" | +-----+ --Incorrect example of converting JSON to string. Converting JSON expressions of the ARRAY or OBJECT type to string is not supported. select cast(json '{"a":2}' as string); --An error is returned: FAILED: ODPS-0123091:Illegal type cast - Unsupported cast from json array/object to string -
Örnek 9: NUMBER ve JSON türleri arasında dönüşüm yapma.
--Convert JSON to bigint. select cast(json '123' as bigint); --Returns: +------------+ | _c0 | +------------+ | 123 | +------------+ --Convert JSON to float. select cast(json '"1.23"' as float); --Returns: +------+ | _c0 | +------+ | 1.23 | +------+ --Convert JSON to double. select cast(json '1.23' as double); --Returns: +------------+ | _c0 | +------------+ | 1.23 | +------------+ --Convert int to JSON. select cast(123 as json); --Returns: +-----+ | _c0 | +-----+ | 123 | +-----+ --Convert float to JSON. select cast(1.23 as json); --Returns: +-----+ | _c0 | +-----+ | 1.23 | +-----+ -
Örnek 10: BOOLEAN ve JSON türleri arasında dönüşüm yapma.
--Convert boolean to JSON. select cast(true as json); --Returns: +-----+ | _c0 | +-----+ | true | +-----+ --Convert JSON to boolean. select cast(json 'false' as boolean); --Returns: +------+ | _c0 | +------+ | false | +------+ --Convert JSON to boolean. select cast(json '"abc"' as boolean); --Returns: +------+ | _c0 | +------+ | true | +------+ --ARRAY or OBJECT cannot be converted to boolean. select cast(json '[1,2]' as boolean); --An error is returned: Unsupported cast from json array/object to boolean -
Örnek 11: NULL ve JSON türleri arasında dönüşüm yapma.
--Convert null to JSON. select json_type(cast(null as json)); --Returns: +-----+ | _c0 | +-----+ | NULL | +-----+
İlgili işlevler
CAST işlevi karmaşık tür işlevidir. ARRAY, MAP, STRUCT ve JSON gibi karmaşık veri türlerini işleyen işlevler hakkında daha fazla bilgi için bkz. Karmaşık tür işlevleri.
CAST işlevi başka bir işlev türü olarak da sınıflandırılır. Diğer iş senaryolarına yönelik işlevler hakkında daha fazla bilgi için bkz. Diğer işlevler.