Group terminals into set - grammar

What does this warning mean ?
How do I solve it ?
Here is the code I am referring to
expression : expression operator=DIV expression
| expression operator=MUL expression
| expression operator=ADD expression
| expression operator=SUB expression
| INT
| FLOAT
| BOOLEAN
| NULL
| ID
;

The ANTLR 4 parser generator can combine groups of transitions to form a single "set transition" in certain cases, reducing static and dynamic memory overhead as well as improving runtime performance. This can only occur if all alternatives of a block contain a single element or set. For example, the following code allows INT and FLOAT to be combined into a single transition:
// example 1
number
: INT
| FLOAT
;
// example 2, elements grouped into a set
primary
: '(' expression ')'
| (INT | FLOAT)
;
However, in the following situation the elements cannot be combined by the compiler so they'll be treated separately:
primary
: '(' expression ')'
| INT
| FLOAT
;
The hint suggests places where the simple addition of ( ... ) is enough to allow the compiler to collapse a set that it would otherwise not be able to. Altering your code to the following would address the warning.
expression
: expression operator=DIV expression
| expression operator=MUL expression
| expression operator=ADD expression
| expression operator=SUB expression
| ( INT
| FLOAT
| BOOLEAN
| NULL
| ID
)
;

Related

Express a ternary SQL data type

Context
I just met a single table in a PostgreSQL database which is actually only defining a triplet of coded values that are used across the whole database as a ternary data type. I am a bit astonished at first glance, I feel it's weird; there should be some ternary data type?
I've searched the web, especially the PostgreSQL documentation without any apparent success (I'm probably wrong with my search keywords?!), but maybe there is no other solution.
Question
I would like to know if it exists a ternary (as comparison with binary or boolean) data type in PostgreSQL or more generally in SQL which permits to express a "ternary state" (or "ternary boolean" which is clearly is an abuse of language), which I would represent as a general idea as:
+-------+----------+--------------------+
| id | type | also expressed as |
+-------+----------+--------------------+
| 0 | false | 0 |
| 1 | true | 1 |
| 2 | unknown | 2 |
+-------+----------+--------------------+
where unknown can be whatever third state you are actually dealing with.
I would like to know if it exists a ternary (as comparison with binary or boolean) data type
Actually, the boolean data type is ternary because it can have the values true, false and null.
Consider this table:
create table data (some_number int, some_flag boolean);
And the following data:
insert into data (some_number, some_flag)
values (1, true), (2, false), (3, null);
Then the following:
select *
from data
where some_flag = false;
will only return one row (with some_number = 2)
there is not a specific ternary operator but you could use case
select case when operator =0 then 'false'
when operatore =1 then 'true'
when operator = 2 then 'unknow'
else 'not managed'
end
from your_table
I second a_horse_with_no_name's solution for your specific example, but the more general approach is to use an enum data type:
CREATE TYPE ternary AS ENUM (
'never',
'sometimes',
'always'
);
Constants of such a data type are written as string constantls, e.g. 'never', but the internal storage uses 4 bytes per value, regardless of the length of the label.

Postgres: why do I need to quote column name in max()? [duplicate]

This question already has answers here:
When do Postgres column or table names need quotes and when don't they?
(2 answers)
Omitting the double quote to do query on PostgreSQL
(5 answers)
PostgreSQL "Column does not exist" but it actually does
(6 answers)
Postgresql Column Not Found, But Shows in Describe
(1 answer)
sql statement error: "column .. does not exist"
(1 answer)
Closed 3 years ago.
So I encountered the following behaviour which surprised me. I first thought DateTime might be a postgres data type but what about BidOpen? Then there is the funny thing with the case of the column name in the error message. I almost feel this has to do with unquoted names being case insensitive. Why do I need to surround the column name in quotes for the query to work?
mydatabase=# select max("DateTime") from fx.candle;
max
---------------------
2019-04-26 20:59:00
(1 row)
mydatabase=# select max(DateTime) from fx.candle;
ERROR: column "datetime" does not exist
LINE 1: select max(DateTime) from fx.candle;
^
HINT: Perhaps you meant to reference the column "candle.DateTime".
mydatabase=# select max(BidOpen) from fx.candle;
ERROR: column "bidopen" does not exist
LINE 1: select max(BidOpen) from fx.candle;
^
HINT: Perhaps you meant to reference the column "candle.BidOpen".
mydatabase=# select max("BidOpen") from fx.candle;
max
---------
125.816
(1 row)
The schema looks like this:
mydatabase=# \d fx.candle;
Table "fx.candle"
Column | Type | Modifiers
-----------+-----------------------------+-----------------------------------------------------------------
id | integer | not null default nextval('fx.candle_id_seq'::regclass)
DateTime | timestamp without time zone |
BidOpen | double precision | not null
BidHigh | double precision | not null
BidLow | double precision | not null
BidClose | double precision | not null
AskOpen | double precision | not null
AskHigh | double precision | not null
AskLow | double precision | not null
AskClose | double precision | not null
symbol_id | integer |
Indexes:
"candle_pkey" PRIMARY KEY, btree (id)
Foreign-key constraints:
"candle_symbol_id_fkey" FOREIGN KEY (symbol_id) REFERENCES fx.symbol(id)
My understanding is that Postgres is not case sensitive regarding column and table names unless you actually create them using double quotes in the beginning. If that be the case, then you would need to forever refer to them using double quotes, to ensure that the proper case literal is being used.
So, to avoid your current situation, you should also avoid creating column/table names in a case sensitive manner.
Your create table should look something like this:
create table fx.candle (
id integer not null default nextval('fx.candle_id_seq'::regclass),
...
datetime timestamp without time zone -- NO quotes here; important!
...
)

Impala find_in_set to match LIKE with percentage sign

Is there a way to match zz-10% in find_in_set?
For example:
select find_in_set('zz-1000','zz-10%,zz-2000,zz-3000');
This should return 1 but Impala doesn't support it.
I am wondering if there is some trick with Regex to workaround? The find_in_set seems to do only exact match.
Ideally this should return 1 only as I want to avoid hardcoding a bunch of zz-10% variations.
This is the definition of this function from https://www.cloudera.com/documentation/enterprise/5-14-x/topics/impala_string_functions.html
find_in_set(string str, string strList)
Purpose: Returns the position
(starting from 1) of the first occurrence of a specified string within
a comma-separated string. Returns NULL if either argument is NULL, 0
if the search string is not found, or 0 if the search string contains
a comma. Return type: int
I cannot change zz-1000 (the first param) because it's basically a Column. I could do a bunch of IF / CASE WHEN though if there is a way.
Thanks.
UPDATE 1
I tried this:
select find_in_set('zz-1000','zz-10\d+,zz-2000,zz-3000');
And got this:
+----------------------------------------------------+
| find_in_set('zz-1000', 'zz-10\d+,zz-2000,zz-3000') |
+----------------------------------------------------+
| 0 |
+----------------------------------------------------+
So that doesn't work either.
What about to use REGEXP_LIKE function:
+----------------------------------------------+
| regexp_like('zz-1000', 'zz-10\\d+$|zz-2000') |
+----------------------------------------------+
| true |
+----------------------------------------------+
When you have a static number of strings to compare, we can try this:
SELECT CASE
WHEN regexp_like('zz-1000', 'zz-10\\d+$') THEN 1
WHEN regexp_like('zz-1000', 'zz-2000') THEN 2
ELSE 0
END;

MS Access -SQL query

Could someone explain what does the below code mean .
In ms access select join query with below condition (inside where )
Table1.col1 <> [table2]!col2
What does it mean?
It means not equal. Same as !=. It fetches all rows that are not equal to your table1.col1
Table of operators
https://support.office.com/en-us/article/table-of-operators-e1bc04d5-8b76-429f-a252-e9223117d6bd
Comparison operators
| Operator | Purpose | Example |
| <> | Returns True if the first value is not equal to the second value. | Value1 <> Value2 |
—-
Introduction to Access SQL
https://support.office.com/en-gb/article/introduction-to-access-sql-d5f21d10-cd73-4507-925e-bb26e377fe7e
Any relational comparison operator: "=," "<," ">," "<=," ">=," or "<>."
—-
Definition: The Bang (!) Operator
What the bang operator does is simple and specific:
The bang operator provides late-bound access to the default member of an object, by passing the literal name following the bang operator as a string argument to that default member.

sql raiserror specify parameter

When i read the example of MSDN raiserror:
RAISERROR (N'This is message %s %d.', -- Message text.
10, -- Severity,
1, -- State,
N'number', -- First argument.
5); -- Second argument.
-- The message text returned is: This is message number 5.
GO
Why the doc using %s to specify the N'number',and %d to specify the 5 -- Second argument
The MSDN write like this:
For example, in the following RAISERROR statement, the first argument of N'number' replaces the first conversion specification of %s; and the second argument of 5 replaces the second conversion specification of %d.
My question is: How can explain it? Why don't using other like %a or %b.Any other %+apha can replace it.
I just want to get a meaningful understand.
This represents the parameter datatype.
+--------------------+----------------------+
| Type specification | Represents |
+--------------------+----------------------+
| d or i | Signed integer |
| o | Unsigned octal |
| s | String |
| u | Unsigned integer |
| x or X | Unsigned hexadecimal |
+--------------------+----------------------+
N'number' is an nvarchar string literal. So gets the %sspecifier. And the literal 5 is a signed indicator so is represented by %d.
As for the reason for these specifiers. This is documented in the RAISERROR topic
These type specifications are based on the ones originally defined for
the printf function in the C standard library