Snowflake - how do I retrieve the name of the currently executing procedure? - snowflake-cloud-data-platform

I'd like to access the name of the currently executing procedure in Snowflake in the javascript procedure itself and store it in a variable.
When I interrogate the "this" object, I can see the name in the Variant return, but in terms of JSON I believe it's the name not the value and I'm not sure how to retrieve the first name in the JSON object.
What code do I need to use to get the procedure name?
procName = ???what code goes here with the this object???;
Do we know that the first name/value pair in "this" will always be the procedure name?
CREATE OR REPLACE PROCEDURE EDW_ADMIN.DAG_TEST()
RETURNS VARIANT
LANGUAGE JAVASCRIPT
AS
$$
var procName = "";
// procName = this.??? WHAT DO I PUT HERE ???
return this;
$$
;
The return / contents of "this" are:
{
"DAG_TEST": {},
"ResultSet": {},
"SfTimestamp": {},
"Snowflake": {},
"Statement": {},
"Status": {
"EOF": "eof",
"ERROR": "error",
"INITIALIZED": "initialized",
"SUCCESS": "success"
},
"_c_snowflake": {
"createExecError": {},
"createStatement": {}
},
"createError": {},
"extractValue": {},
"getColSqlTypeFromIdx": {},
"nativeTypes": {
"values": {
"BOOLEAN": "boolean",
"BUFFER": "buffer",
"DATE": "date",
"INVALID": "invalid",
"JSON": "json",
"NUMBER": "number",
"STRING": "string"
}
},
"noSuchColumnIdxErrorMessage": "Given column name/index does not exist: ",
"snowflake": {
"createStatement": {},
"execute": {}
},
"sqlTypeFromLibSfDbTypeVal": {},
"sqlTypes": {
"isArray": {},
"isBinary": {},
"isBoolean": {},
"isDate": {},
"isNumber": {},
"isObject": {},
"isText": {},
"isTime": {},
"isTimestamp": {},
"isTimestampLtz": {},
"isTimestampNtz": {},
"isTimestampTz": {},
"isVariant": {},
"values": {
"ARRAY": {
"libSfDbType": 9,
"name": "ARRAY"
},
"BINARY": {
"libSfDbType": 10,
"name": "BINARY"
},
"BOOLEAN": {
"libSfDbType": 12,
"name": "BOOLEAN"
},
"DATE": {
"libSfDbType": 3,
"name": "DATE"
},
"FIXED": {
"libSfDbType": 0,
"name": "FIXED"
},
"INVALID_SQL_TYPE": {
"libSfDbType": -1,
"name": "INVALID_SQL_TYPE"
},
"OBJECT": {
"libSfDbType": 8,
"name": "OBJECT"
},
"REAL": {
"libSfDbType": 1,
"name": "REAL"
},
"TEXT": {
"libSfDbType": 2,
"name": "TEXT"
},
"TIME": {
"libSfDbType": 11,
"name": "TIME"
},
"TIMESTAMP_LTZ": {
"libSfDbType": 4,
"name": "TIMESTAMP_LTZ"
},
"TIMESTAMP_NTZ": {
"libSfDbType": 5,
"name": "TIMESTAMP_NTZ"
},
"TIMESTAMP_TZ": {
"libSfDbType": 6,
"name": "TIMESTAMP_TZ"
},
"VARIANT": {
"libSfDbType": 7,
"name": "VARIANT"
}
}
},
"testFunc": {},
"typeFromLibSfDbTypeVal": {},
"validateBinds": {},
"validateColumnExists": {}
}

Try this in your proc:
const procName = Object.keys(this)[0];
Assuming that the procName is always the first key in the dictionary! Also using const wherever possible is a good practice.

Try CURRENT_STATEMENT(); haven't tested, so let us know

Related

How to parse complex data using react native?

Following is the DATA that needs to parse which can nested arrays and objects i need
help understanding the easiest way to parse this .
const country= [
{
"place": "sikkim",
"location": 2,
"Extension": "",
"Keys": {
"string": [
"Enabled",
"Disabled"
]
},
"ItemValues": {
"ItemData": [
{
"Name": "Enabled",
"Data": {
"Rows": {
"Row": {
"Values": {
"Type": false
}
}
}
}
},
{
"Name": "Value",
"Data": {
"Rows": {
"DataRow": {
"Values": {
"anyType": "100"
}
}
}
}
}
]
}
},
{
"place": "assam",
"location": 1,
"Extension": "",
"Keys": {
"string": "MinValue"
},
"ItemValues": {
"ItemData": {
"Name": "nValue",
"Data": {
"Rows": {
"DataRow": {
"Values": {
"anyType": "1"
}
}
}
}
}
}
}
]
In this array 2 objects are there i need to parse this complex array of object and
show like this -
OUTCOME should be -
sikkim:{
"place": "sikkim",
"location": 2,
"Extension": "",
"string":"Enabled","Disabled",
"Name": "Enabled",
"Type": false,
"Name": "Value",
"anyType": 100
},
assam:{.. so on}
code that i tried is displaying only keys -
const getValues = country =>
country === Object(country)
? Object.values(country).flatMap(getValues)
: [country];
console.log(getValues(country));
OUTPUT from above code =
 ["sikkim", 2, "", "Enabled", "Disabled", "Enabled", false, "Value", "100",
"assam", 1, "", "MinValue", "nValue", "1"]
Need to write logic which is smart way to write to cover this logic because my data
is huge any leads?I can achieve the output but writing many for and if loops but that
creates lot of confusion.

MongoDB $push in Array of Array Error: No value exists in scope for the shorthand property 'elements'

I wish to add data into Elements but get error. How to solve this mongodb/typescript problem?
Attempt 1 Error: No value exists in scope for the shorthand property 'elements'. Either declare one or provide an initializer.ts(18004)
Attempt 1
async add<T extends { id: string; parentID: string, elementNum: number, name: string, link: string}>(collectionName: string, args: T) {
const db = await this.getDB();
const collection = db.collection(collectionName);
return new Promise((resolve, reject) => {
collection.updateOne({ id: args.id }, {$push: {elements[args.elementNum]: {id: uuid(), name: args.name, link: args.link, elements: [] }}}, (err, res) => {
if (err) {
reject(err);
}
resolve(res);
});
});
}
Attempt 2
changed the following
collection.updateOne({ id: args.id }, {$push: {elements: {id: uuid(), name: args.name, link: args.link, elements: [] }}},
Attempt 2 results in Null added in the end
{
"id": "1",
"name": "wk1",
"iconFile": "icon.png",
"elements": [
[
{
"id": "2",
"name": "element2",
"link": "https",
"elements": [
{
"id": "1",
"name": "element1",
"link": "https:"
}
]
}
],
[
{
"id": "3",
"name": "element3",
"link": "https://",
"elements": [
{
"id": "4",
"name": "w",
"link": "http:/"
}
]
}
],
[
{
"id": "3",
"name": "element3",
"link": "https://",
"elements": [
{
"id": "4",
"name": "w",
"link": "http://"
}
]
},
{
"id": "3",
"name": "element3",
"link": "https://",
"elements": [
{
"id": "4",
"name": "w",
"link": "http://www."
}
]
}
],
null,
]
}
What I want to achieve is the following
{
"id": "1",
"name": "wk1",
"iconFile": "icon.png",
"elements": [
[
{
"id": "2",
"name": "element2",
"link": "https",
"elements": [
{
"id": "1",
"name": "element1",
"link": "https:"
}
]
},
{
"id": "newid",
"name": "newname",
"link": "newlink"
"elements":[]
}
],
[
{
"id": "3",
"name": "element3",
"link": "https://",
"elements": [
.......
]
}
Demo - https://mongoplayground.net/p/Dnmg3lL2891
Use - $[]
The all positional operator $[] indicates that the update operator
should modify all elements in the specified array field.
The $[] operator has the following form:
{ <update operator>: { "<array>.$[]" : value } }
db.collection.update({ _id: 1, "add.id": "1"},
{ $push: { "add.$[].elements": { id: "3", name: "a", link: "" } } })
Demo to push array instead of object - https://mongoplayground.net/p/dh3NSutIv4-
db.collection.update({ _id: 1, "add.id": "1"},
{ $push: { "add.$[].elements": [{ id: "3", name: "a", link: "" }] } })
const args = {};
args.elementNum = 0;
const update = {
[`add.$[].elements.${args.elementNum}`]: {
a: 1
}
};
console.log(update);
//collection.updateOne({ id: args.id }, update); // use like this

Elastic 7 mapping definition has unsupported parameters

I believe this issue has to do with the different syntax from elasticsearch 6.x -> 7.x
This is my first time using elasticsearch and havent been able to understand where the issue is.
How would I fix my mapping?
{
"settings": {
"analysis": {
"analyzer": {
"ssdeep_analyzer": {
"tokenizer": "ssdeep_tokenizer"
}
},
"tokenizer": {
"ssdeep_tokenizer": {
"type": "ngram",
"min_gram": 7,
"max_gram": 7,
"token_chars": [
"letter",
"digit",
"symbol"
]
}
}
}
},
"mappings": {
"_default_": {
"_all": {
"enabled": false
},
"dynamic": "strict",
"properties": {
"chunksize": {
"type": "integer"
},
"chunk": {
"analyzer": "ssdeep_analyzer",
"type": "text"
},
"double_chunk": {
"analyzer": "ssdeep_analyzer",
"type": "text"
},
"ssdeep": {
"type": "keyword"
},
"sha256": {
"type": "keyword"
}
}
},
"record": {}
}
}
The error that is being raised is
elasticsearch.exceptions.RequestError: RequestError(400, 'mapper_parsing_exception', 'Root mapping definition has unsupported parameters: [_default_ : {dynamic=strict, _all={enabled=false}, properties={sha256={type=keyword}, chunksize={type=integer}, chunk={analyzer=ssdeep_analyzer, type=text}, double_chunk={analyzer=ssdeep_analyzer, type=text}, ssdeep={type=keyword}}}] [record : {}]')
A bunch of issues here:
The _all field is deprecated (and has been since v6).
"record": {} is not a valid parameter.
_default_ is deprecated too.
Here's a working mapping:
{
"settings": {
"analysis": {
"analyzer": {
"ssdeep_analyzer": {
"tokenizer": "ssdeep_tokenizer"
}
},
"tokenizer": {
"ssdeep_tokenizer": {
"type": "ngram",
"min_gram": 7,
"max_gram": 7,
"token_chars": [
"letter",
"digit",
"symbol"
]
}
}
}
},
"mappings": {
"dynamic": "strict",
"properties": {
"chunksize": {
"type": "integer"
},
"chunk": {
"analyzer": "ssdeep_analyzer",
"type": "text"
},
"double_chunk": {
"analyzer": "ssdeep_analyzer",
"type": "text"
},
"ssdeep": {
"type": "keyword"
},
"sha256": {
"type": "keyword"
}
}
}
}

$unwind multiple arrays present in the embedded documents and show each array as single document as output in mongoDB

Refer below code. In this, field scenario is a embedded document which has has arrays and I want to showcase each array as a single document in the output. Note that each array contains embedded document in it so it would be helpful to get the code which extracts fields from those too. I'm not using java to query. Would be using external BI application which would be integrated in. Think I should also mention that i'm using NoSQLBooster for MongoDB application to create these queries.
{
"_id": {
"$oid": ""
},
"organisationId": "",
"bcpId": "",
"bcpName": "",
"bcpDescription": "",
"biaEntity": {},
"version": "0.01",
"status": "PENDING",
"primaryBridgeNumber": "1",
"alternateBridgeNumber": "2",
"scenario": [{
"_id": {
"$oid": "5e3ab709367d2c5f5826c6fd"
},
"scenario": "",
"strategies": [{
"mdmStrategy": {},
"strategy": {
"pSIStrategyDetails": {
"scenarioName": "",
"strategyName": "",
"rto": "",
"sustainablePeriod": {
},
"description": "1",
"primaryContact": {
},
"secondaryContact": {
},
"recoverySite": {
}
},
"pSICriticalStaff": {},
"specialRequirement": [{
}, {
}, {
}, {
}, {
}]
},
"createdOn": {},
"updatedOn": {}
}, {
"mdmStrategy": {},
"strategy": {
"pSIStrategyDetails": {},
"pSICriticalStaff": {},
"specialRequirement": [{
},
{
},
{
},
{
},
{
}]
},
"createdOn": {},
"updatedOn": }
}],
"description": "",
"status": "Active",
"createdOn": {},
"updatedOn": {}
}],
"updatedOn": {},
"createdOn": {},
"business_owner_id": {},
"bc_coordinator_id": {},
"backup_business_owner_id": {},
"backup_business_coordinator_id": {},
"sme_id": {},
"_class": "com.bcm.bcp.api.model.BcmBcpEntity"
}
expected output:
{{
"bcpId": "",
"bcpName": "",
"bcpDescription": "",
"version": "0.01",
"status": "PENDING",
"scenario.scenario":"---",
"scenario.strategies.strategy.strategyName":"---",
"scenario.strategies.strategy.rto":"---",
etc...
}{
"bcpId": "",
"bcpName": "",
"bcpDescription": "",
"version": "0.01",
"status": "PENDING",
"scenario.scenario":"---",
"scenario.strategies.strategy.strategyName":"---",
"scenario.strategies.strategy.rto":"---",
etc...
}{
"bcpId": "",
"bcpName": "",
"bcpDescription": "",
"version": "0.01",
"status": "PENDING",
"scenario.scenario":"---",
"scenario.strategies.strategy.strategyName":"---",
"scenario.strategies.strategy.rto":"---",
etc...
}}
"scenario.scenario":"---","scenario.strategies.strategy.strategyName":"---",
"scenario.strategies.strategy.rto":"---",
will be coming from the arrays so the output will be number of elements present in the array
U hope this is what you want:
db.collection.aggregate([
{
$unwind: "$scenario"
},
{
$unwind: "$scenario.strategies"
},
{
$project: {
bcpId: 1,
bcpName: 1,
bcpDescription: 1,
version: 1,
status: 1,
scenario: {
scenario: 1,
strategies: {
strategy: {
pSIStrategyDetails: {
rto: 1,
strategyName: 1
}
}
}
}
}
}
])
Output:
[
{
"_id": ObjectId("5a934e000102030405000000"),
"bcpDescription": "",
"bcpId": "",
"bcpName": "",
"scenario": {
"scenario": "",
"strategies": {
"strategy": {
"pSIStrategyDetails": {
"rto": "",
"strategyName": ""
}
}
}
},
"status": "PENDING",
"version": "0.01"
},
{
"_id": ObjectId("5a934e000102030405000000"),
"bcpDescription": "",
"bcpId": "",
"bcpName": "",
"scenario": {
"scenario": "",
"strategies": {
"strategy": {
"pSIStrategyDetails": {}
}
}
},
"status": "PENDING",
"version": "0.01"
}
]
Explanation: You need to use 2 $unwind operators, as it is like arrays of arrays and a $project operator to display only those fields, which you need.
MongoPlayGroundLink
P.S. - The question is still unclear.

How to I access many layers into a json array? Angularjs

After fighting with this for a couple of hours, I think I need to rephrase the question.
I have an object that contains and array of objects;
Playlist: [
{
"added_at": "2015-11-13T20:55:06Z",
"added_by": {
"external_urls": {
"spotify": "http://open.spotify.com/user/spotify_canada"
},
"href": "https://api.spotify.com/v1/users/spotify_canada",
"id": "spotify_canada",
"type": "user",
"uri": "spotify:user:spotify_canada"
},
"is_local": false,
"track": {
"album": {
"album_type": "album",
"available_markets": [
"CA",
"MX",
"US"
],
"external_urls": {
"spotify": "https://open.spotify.com/album/6Fr2rQkZ383FcMqFyT7yPr"
},
"href": "https://api.spotify.com/v1/albums/6Fr2rQkZ383FcMqFyT7yPr",
"id": "6Fr2rQkZ383FcMqFyT7yPr",
"images": [
{
"height": 640,
"url": "https://i.scdn.co/image/8b47495ce0c4a341f7196f70bcf4361e6257c1a0",
"width": 640
},
{
"height": 300,
"url": "https://i.scdn.co/image/da1e8958b6260e832660d0c5f3c80e9569c388c8",
"width": 300
},
{
"height": 64,
"url": "https://i.scdn.co/image/478dbfd0e579dee7392707b9a6848faff0cdfefd",
"width": 64
}
],
"name": "Purpose (Deluxe)",
"type": "album",
"uri": "spotify:album:6Fr2rQkZ383FcMqFyT7yPr"
},
"artists": [
{
"external_urls": {
"spotify": "https://open.spotify.com/artist/1uNFoZAHBGtllmzznpCI3s"
},
"href": "https://api.spotify.com/v1/artists/1uNFoZAHBGtllmzznpCI3s",
"id": "1uNFoZAHBGtllmzznpCI3s",
"name": "Justin Bieber",
"type": "artist",
"uri": "spotify:artist:1uNFoZAHBGtllmzznpCI3s"
}
],
"available_markets": [
"CA",
"MX",
"US"
],
"disc_number": 1,
"duration_ms": 205680,
"explicit": false,
"external_ids": {
"isrc": "USUM71511919"
},
"external_urls": {
"spotify": "https://open.spotify.com/track/4B0JvthVoAAuygILe3n4Bs"
},
"href": "https://api.spotify.com/v1/tracks/4B0JvthVoAAuygILe3n4Bs",
"id": "4B0JvthVoAAuygILe3n4Bs",
"name": "What Do You Mean?",
"popularity": 93,
"preview_url": "https://p.scdn.co/mp3-preview/13da79dc4803f65092d583f6e3bdf1fc4d8344e5",
"track_number": 3,
"type": "track",
"uri": "spotify:track:4B0JvthVoAAuygILe3n4Bs"
}
},
In side of each of these objects is another object I am trying to assign to scope.
So this - playlist {items [ {track {name: songname} } ] }
How do I go about assigning the track.name to scope?
Thanks!
I'm not sure if you just have a typo but why are you assigning your blank array to data.items instead of the other way around?
$scope.playListInfo = [];
$scope.getPlayList = function() {
Spotify.getPlaylistTracks('spotify_canada', '7ndCD5pklOTcrTcv4DErmI')
.then(function(data) {
var i = 0;
for(i; i < data.items.length; i++) {
var dataToKeep = {};
dataToKeep.name = data.items[i].track.name;
dataToKeep.artist = data.items[i].track.artist;
$scope.playListInfo[i] = dataToKeep;
}
});
};
Apparently, I don't have sufficient points to comment. So, I am creating this post.
The JSON data has items and not item. Also, I think you'd have to create a $scope.playlistTrack array outside the function.
$scope.playlistTrack = [];
$scope.getPlayList = function () {
Spotify.getPlaylistTracks('spotify_canada', '7ndCD5pklOTcrTcv4DErmI')
.then(function (datas) {
angular.forEach(datas, function(data){
data.items = $scope.playlistTrack;
});
});
};
Hope this answer helps.

Resources