DynamoDB how to use index in PartiQL queries? - amazon-dynamodb

I have seen the dynamoDB doc for the PartiQL syntax:
SELECT expression [, ...]
FROM table[.index]
[ WHERE condition ] [ [ORDER BY key [DESC|ASC] , ...]
but in practice:
select * from dev .pk-all-index
where "pk" = 'config' AND ("brand" = 'tesla' OR contains("aliases", 'tesla.com'))
gives me the error:
An error occurred during the execution of the command.
ValidationException: Statement wasn't well formed, can't be processed:
Unexpected keyword

from "tablename"."indexname"

You might want to put the table name and index under quotes, individually.
SELECT * FROM "dev"."pk-all-index" WHERE "pk" = 'config' AND ("brand" = 'tesla' OR contains("aliases", 'tesla.com'))

Related

New to PartiQL - what is wrong with this SELECT statement?

Using DynamoDB with PartiQL, I am trying to select records using a global index:
SELECT *
FROM "My-Table.My-Index"
WHERE Foo = 'bar'
AND Timestamp >= 638014354649346173
AND Timestamp <= 638014355249346173
ORDER BY Timestamp
I am getting an exception of:
One or more errors occurred. (Statement wasn't well formed, can't be processed: Unexpected keyword)
Not sure what I am doing wrong here as I am new to PartiQL syntax.
It should be like this:
SELECT *
FROM "My-Table"."My-Index"
WHERE "Foo" = 'bar'
AND "Timestamp" >= 638014354649346173
AND "Timestamp" <= 638014355249346173
ORDER BY "Timestamp"

INSERT INTO with subquery and ON CONFLICT

I want to insert all elements from a JSON array into a table:
INSERT INTO local_config(parameter, value)
SELECT json_extract(j.value, '$.parameter'), json_extract(j.value, '$.value')
FROM json_each(json('[{"parameter": 1, "value": "value1"}, {"parameter": 2, "value": "value2"}]')) AS j
WHERE value LIKE '%'
ON CONFLICT (parameter) DO UPDATE SET value = excluded.value;
This works so far, but do I really need the WHERE value LIKE '%' clause?
When I remove it:
INSERT INTO local_config(parameter, value)
SELECT json_extract(j.value, '$.parameter'), json_extract(j.value, '$.value')
FROM json_each(json('[{"parameter": 1, "value": "value1"}, {"parameter": 2, "value": "value2"}]')) AS j
ON CONFLICT (parameter) DO UPDATE SET value = excluded.value;
I get this error:
[SQLITE_ERROR] SQL error or missing database (near "DO": syntax error)
From SQL As Understood By SQLite/Parsing Ambiguity:
When the INSERT statement to which the UPSERT is attached takes its
values from a SELECT statement, there is a potential parsing
ambiguity. The parser might not be able to tell if the "ON" keyword is
introducing the UPSERT or if it is the ON clause of a join. To work
around this, the SELECT statement should always include a WHERE
clause, even if that WHERE clause is just "WHERE true".
So you need the WHERE clause, but it can be a simple WHERE true or just WHERE 1.

Data ingestion issue with KQL update policy ; Query schema does not match table schema

I'm writing a function which takes in raw data table (contains multijson telemetry data) and reformat it to a multiple cols. I use .set MyTable <| myfunction|limit 0 to create my target table based off of the function and use update policy to alert my target table.
Here is the code :
.set-or-append MyTargetTable <|
myfunction
| limit 0
.alter table MyTargetTable policy update
#'[{ "IsEnabled": true, "Source": "raw", "Query": "myfunction()", "IsTransactional": false, "PropagateIngestionProperties": false}]'
But I'm getting ingestion failures: Here is the ingestion failure message :
Failed to invoke update policy. Target Table = 'MyTargetTable', Query = '
let raw = __table("raw", 'All', 'AllButRowStore')
| where extent_id() in (guid(659e3b3c-6859-426d-9c37-003623834455));
myfunction()': Query schema does not match table schema
I double check the query schema and target table; they are the same . I'm not sure what this error means.
Also, I ran count on both the raw and mytarget tables; there are relatively large discrepancies (400 rows for My target and 2000 rows in raw table).
Any advise will be appreciated.
Generally speaking - to find the root of the mismatch between schemas, you can run something along the following lines, and filter for differences:
myfunction
| getschema
| join kind=leftouter (
table('MyTargetTable')
| getschema
) on ColumnOrdinal, ColumnType
In addition - you should make sure the output schema of the function you use in your update policy is 'stable', i.e. isn't affected by the input data
The output schema of some query plugins such as pivot() and bag_unpack() depends on the input data, and therefore it isn't recommended to use those in update policies.

DynamoDB how to use sort key with PartiQL query?

Hello I m new to DynamoDB, I have created a TABLE, with Partition Key "pk" and Sort Key "id"
in then item explorer I can query with the pk and sort key value and it seems to work.
In the PartiQL Editor I do
SELECT * FROM "dev" WHERE "pk" = 'config' AND "id" = "7b733512cc98445891dcb07dc4299ace"
and I get the error Filter Expression can only contain non-primary key attributes: Primary key attribute: id
I don't know how I can specify the sort key in the key conditions instead of the filter condition with the WHERE clause.
I found the error, if you use " instead of ' it doesn't work. so the correct query is :
SELECT * FROM "dev" WHERE "pk" = 'config' AND "id" = '7b733512cc98445891dcb07dc4299ace'

SQLite Syntax Error

I made a sql query, below. I am getting an exception which I have shown as well. Any help is appreciated.
QUERY:
INSERT OR REPLACE INTO CLAIMS (DATE ,TIME , ADDRESS , CITY,
STATE , POSTAL , PHFNAME ,PHLNAME ,PHEMAIL ,PHPHONE ,AGENCY ,POLICY ,VEHICLENAME,
YEAR ,MAKE ,MODEL ,PLATELICENSE ,LSTATE ,VIN ,DRIVERNAME,DRFNAME ,DRLNAME ,
DRPHONE ,DREMAIL ,DRLICENSE) VALUES("Wednesday, May 4, 2011",
"10:39:10 PM EDT", "400 Chatham","Pune", "Penn", "45223", "John",
"Richard","jsmith#newyahoo.com","+1-11111111111",
"Three Rivers Insurance","(null)", "(null)", "(null)",
"(null)", "(null)","(null)", "(null)", "(null)","(null)",
"(null)", "(null)","(null)", "(null)","(null)") WHERE DATE LIKE
'Wednesday, May 4,%' AND TIME = '10:39:10 PM EDT'
Error:
SQLiteManager: Likely SQL syntax error:
[ near "WHERE": syntax error ]
Exception Name: NS_ERROR_FAILURE
Exception Message: Component returned failure code: 0x80004005 (NS_ERROR_FAILURE) [mozIStorageConnection.createStatement]
I faced the same issue for sync table for insert and update at a time and finally i use.
To use the insert or replace statement you need to use the coalesce function that required primary key column to put the where clause.
COALESCE((SELECT PRIMARY_KEY_COLUMN FROM TABLE_NAME WHERE UNIQUE_COLUMN = 'VALUE'),
(SELECT MAX(PRIMARY_KEY_COLUMN)+1 FROM TABLE_NAME))
it worked for me e.g
INSERT OR REPLACE INTO TBL_ADDRESSES
(ADDRESS_ID,ADDRESS1,ADDRESS2,ADDRESS3,ADDRESS_NAME,CITY,DB_ID)
VALUES (
COALESCE((SELECT ADDRESS_ID FROM TBL_ADDRESSES WHERE DB_ID = '111'),
(SELECT MAX(ADDRESS_ID)+1 FROM TBL_ADDRESSES)),
'IT 27','Pratap DSFSDSDDSDSF','test add ','IT 27','Jaipur','111') ;
INSERT OR REPLACE doesn't support WHERE clause, see theSQLite Insert doc. You can use a WHERE clause in UPDATE statements, see the SQLite Insert doc
Can help you if you can let us know what you wanted here ?

Resources