Snowlake UDF Example | User-Defined Functions In SNowflake In Hindi

Image could not load

Image on unsplash

What are UDFs In Snowflake

आपने SQL में कई सारे built in functions जैसे avg(), sum() , count(), max(), min() use किये होंगे , लेकिन snowflake में आपने custom functions को भी UDFs (User Defined Functions) कहते हैं।

Snowflake में UDFs (User Defined Functions) , आपके द्वारा लिखे गए custom functions हैं जो SQL queries में callable होते हैं। मतलब इनकी returned value को SQL queries में use कर सकते हैं।

Snowflake UDF Syntax

UDF को define करने का तरीका almost Snowflake Procedure की तरह ही same होता है। हालंकि Procedure और UDFs में काफी difference है जो आगे देखेंगे।

Snowflake में आप SQL , SCALA , JAVA , Python , JavaScript programming languages में भी UDFs define कर सकते हैं।

normally SQL syntax कुछ इस तरह से होता है।

CREATE [ OR REPLACE ] [ { TEMP | TEMPORARY } ] [ SECURE ] FUNCTION name (
  [ arg_name arg_data_type ...)
  [ COPY GRANTS ]
  RETURNS { result_data_type | TABLE ( col_name col_data_type ) }
  [ [ NOT ] NULL ]
  [ { VOLATILE | IMMUTABLE } ]
  [ MEMOIZABLE ]
  [ COMMENT = 'string_literal' ]
  AS 'function_definition'

अब अगर आपको UDF के अंदर variables define करने हैं तो आप DECLARE statements का use कर सकते हैं।

For Example

CREATE OR REPLACE FUNCTION function_name(param1 VARCHAR, param2 INT)
  RETURNS VARCHAR
  LANGUAGE SQL
AS
$$
-- Declare variables
DECLARE
    var1 INT;
    var2 VARCHAR;
BEGIN
    -- Set values to variables
    var1 := 10;
    var2 := 'Example';

    -- Use variables in SQL statements
END;
$$;

ये normal SQL syntax था , हालाँकि Languages के according कुछ options काम या ज्यादा हो सकते हैं। और इन्ही options का use करके आप useful UDF define कर सकते है।

UDFs में return statement required होता है , मतलब function कुछ return करना ही चाहिए। SQL language के साथ आपको return keyword का use नहीं करना पड़ता है। बाकी programming language के साथ return keyword लिखना पड़ेगा।

Snowflake UDF Example

तो बसे पहले हम एक simple UDF example देखते हैं जो एक parameter को accept करेगा और जिसमे हम किसी user का नाम print करेंगे।

-- list user defined functions. show user functions; -- now create an UDF. CREATE or replace FUNCTION greeting(name string) RETURNS string AS $$ 'Welcome to snowflake ' || name $$; -- list again. show user functions;

हमने string parameter के साथ greeting() नाम का function define किया है। अब इसे आप कितन बार भी call करके use में ले सकते हैं।

Call UDF

select greeting('Babu Rao');
+-------------------------------+
| GREETING('BABU RAO')          |
|-------------------------------|
| Welcome to snowflake Babu Rao |
+-------------------------------+

Call again

select greeting('Raju');

Output

+---------------------------+
| GREETING('RAJU')          |
|---------------------------|
| Welcome to snowflake Raju |
+---------------------------+

Snowflake UDF with JavaScript

अब same example को JavaScript Programming language का use करके देख लेते हैं।

CREATE or replace FUNCTION greeting_js(name string) RETURNS string language JavaScript AS $$ return 'Welcome to snowflake ' + NAME $$;

Call JS UDF

function को call करने का तरीका same ही है।

select greeting('Raju');

Output

+---------------------------+
| GREETING('RAJU')          |
|---------------------------|
| Welcome to snowflake Raju |
+---------------------------+

इसी तरह से आप अपनी need के according UDFs को define करके SQL queries में use कर सकते हैं।

Difference between UDF & Stored Procedure

UDF & Stored Procedure दोनों का structure लगभग एक जैसा है हालाँकि इनमे कुछ differences हैं -

FeaturesUDFsStored Procedures

Part of SQL query

Ability of overload

One or more input parameters

Can use JavaScript API

Return value optional

Called Itself recursively

returned value is usable in SQL

Why use UDFs In Snowflake

  • Reusable code : SQL queries में bar-bar logic को लिखने से बचने के लिए।

  • Complex logic : SQL में natively available नहीं होने वाली functionality को define करके use में लेने के लिए।

  • Performance optimization : Computationally expensive operations को database server पर nearly offload करने के लिए।

Stored procedures in snowflake

Recent Blogs

Loading ...

Rahul Kumar

Rahul Kumar

Hi ! I'm Rahul Kumar Rajput founder of learnhindituts.com. I'm a software developer having more than 4 years of experience. I love to talk about programming as well as writing technical tutorials and blogs that can help to others. I'm here to help you navigate the coding cosmos and turn your ideas into reality, keep coding, keep learning :)

Get connected with me. :) LinkedIn Twitter Instagram Facebook