T-SQL Grammar Parser Rule Mutually Left-Recursive With Itself - grammar

I'm actually just extracting a portion of a T-SQL grammar (https://github.com/antlr/grammars-v4). Specifically, the portion that deals with the WHERE-clause, minus any sub-query logic. Unfortunately, it appears that
expression
: primitive_expression
| function_call
| expression COLLATE id
| case_expression
| full_column_name
| bracket_expression
| unary_operator_expression
| expression op=(ASTERSIK | SLASH_F | P_SIGN) expression
| expression op=(PLUS | HYPHEN | AMPERSAND | CARET | PIPE | (PIPE PIPE)) expression
| expression comparison_operator expression
| expression assignment_operator expression
| over_clause
;
is left-recursive with itself. I'm not sure why I would be getting the error and the whole project doesn't.

Related

ERROR: only one relationship type is allowed for MERGE open cypher query agensgraph

Is the below realtionship creation allowed in open cypher query I am trying this in agensgraph,
MATCH (mc: mat_comp)
MATCH (p:plant)
MATCH (mb: material)
WHERE mc.component = mb.material and mc.plant=p.b_plant
MERGE (mc) <- [ comp_2_p] - ( p)
;
ERROR: only one relationship type is allowed for MERGE
What mistake I am doing... because mat_comp and plant nodes has plant as common mat_comp and material nodes has material as common
mat_comp
has material column
plant column
p: plant
mb:
material colum
MATCH (mc: mat_comp)
MATCH (p:plant)
MATCH (mb: material)
WHERE mc.comp = mb.material and mc.plant=p.b_plant
RETURN mc.comp, mb.material, mc.plant, p.b_plant;
comp | material | plant | b_plant
------------+------------+-------+--------------
"10" | "10" | "33" | "33"
=# \d material
material | character varying(50) | | | |
b_plant | character varying(50) |
=# \d mat_comp
material | character varying(50) | | | |
comp | character varying(50) | | | |
plant | character varying(50) | | | |
-# \d plant
--------------+-----------------------+-----------+----------+---------+-------------
b_plant | character varying(50) |
I think your query has a simple mistake in MERGE clause.
To cut to the point. You can use this query
MERGE (mc) <- [:comp_2_p] - (p)
The mistake is colon.
The word after the colon is type of component (such as node and edge).
The word before the colon is like a pronoun in Cypher clause (I don't know its exact name.).
If you still have another ERROR using above query, you should check whether the elabel "comp_2_p" is declared or not.

Oracle SQL regex extraction

I have data as follows in a column
+----------------------+
| my_column |
+----------------------+
| test_PC_xyz_blah |
| test_PC_pqrs_bloh |
| test_Mobile_pqrs_bleh|
+----------------------+
How can I extract the following as columns?
+----------+-------+
| Platform | Value |
+----------+-------+
| PC | xyz |
| PC | pqrs |
| Mobile | pqrs |
+----------+-------+
I tried using REGEXP_SUBSTR
Default first pattern occurrence for platform:
select regexp_substr(my_column, 'test_(.*)_(.*)_(.*)') as platform from table
Getting second pattern occurrence for value:
select regexp_substr(my_column, 'test_(.*)_(.*)_(.*)', 1, 2) as value from table
This isn't working, however. Where am I going wrong?
For Non-empty tokens
select regexp_substr(my_column,'[^_]+',1,2) as platform
,regexp_substr(my_column,'[^_]+',1,3) as value
from my_table
;
For possibly empty tokens
select regexp_substr(my_column,'^.*?_(.*)?_.*?_.*$',1,1,'',1) as platform
,regexp_substr(my_column,'^.*?_.*?_(.*)?_.*$',1,1,'',1) as value
from my_table
;
+----------+-------+
| PLATFORM | VALUE |
+----------+-------+
| PC | xyz |
+----------+-------+
| PC | pqrs |
+----------+-------+
| Mobile | pqrs |
+----------+-------+
(.*) is greedy by nature, it will match all character including _ character as well, so test_(.*) will match whole of your string. Hence further groups in pattern _(.*)_(.*) have nothing to match, whole regex fails. The trick is to match all characters excluding _. This can be done by defining a group ([^_]+). This group defines a negative character set and it will match to any character except for _ . If you have better pattern, you can use them like [A-Za-z] or [:alphanum]. Once you slice your string to multiple sub strings separated by _, then just select 2nd and 3rd group.
ex:
SELECT REGEXP_SUBSTR( my_column,'(([^_]+))',1,2) as platform, REGEXP_SUBSTR( my_column,'(([^_]+))',1,3) as value from table;
Note: AFAIK there is no straight forward method to Oracle to exact matching groups. You can use regexp_replace for this purpose, but it unlike capabilities of other programming language where you can exact just group 2 and group 3. See this link for example.

Remove invalid data based on particular pattern SQL Server

I have a sample data like shown below
------------------------------------------------
| ID | Column 1 | Column 2 |
------------------------------------------------
| 1 | 0229-10010 |Valid |
------------------------------------------------
| 2 | 20483 |InValid |
------------------------------------------------
| 3 | 319574R06-STAT |Valid |
------------------------------------------------
| 4 | ,,,,,,,,,,,,,,1,,,,,,, |InValid |
------------------------------------------------
| 5 | "PBOM-SSE, CHAMBER" |Valid |
------------------------------------------------
| 6 | ""PBOM-SSE, CHAMBER |InValid |
------------------------------------------------
| 7 | "PBOM-SSE CHAMBER", |InValid |
------------------------------------------------
| 8 | #DRM-1102.Z |InValid |
------------------------------------------------
| 9 | DRM#1102.Z |Valid |
------------------------------------------------
| 10 |OEM-2-202 4079 KALREZ |Valid |
------------------------------------------------
| 11 |-OEM2202 4079 KALREZ# |InValid |
------------------------------------------------
What i want to do is i need to create a pattern in such a way that i need to fetch only invalid data. Just for representation i have mentioned Valid and Invalid. In my table i don't have any flag as such.
Here the trick is same, wildcard characters appearing at different places makes different sense. Consider record ID-5 and Id-6. In both the cases wildcard characters are same, but the position decides whether its valid or not. Again position is also not so clear. I guess you can make out why particular record in column 1 is valid and invalid. In record 8, '#' before that item doesn't makes sense, where as # after Alphabet makes sense (in record 9).
In record 2, there are lot of blank spaces before number, that's why its invalid, but that doesn't mean that space itself is wild card.
I have written query like below.
SELECT [PartNumber]
FROM [IBSSSystems].[dbo].[Part]
WHERE (PartNumber LIKE '%[?;.,$^#&*{}:"<>/|\ %'']%'
OR PartNumber LIKE '%[%'
OR PartNumber LIKE '%]%')
The above query understands that whenever it see any wildcard character in a record , it fetches that. But I need the query in such a way that it understands and fetches only invalid data. I guess there will be lot of And and Or in the resulting query, but i'm confused. I hope you can help me out. Thanks in advance.
SELECT [PartNumber]
FROM [IBSSSystems].[dbo].[Part]
WHERE (PartNumber LIKE '[^A-Za-z0-9"]%' ESCAPE '\' -- When the First character is special charater its InValid ( " is an exception)
OR PartNumber LIKE '%[^A-Za-z0-9" ]' ESCAPE '\' -- When the Last character is special charater its InValid ( " is an exception, also trailing spaces are exception)
OR PartNumber LIKE '%[^A-Za-z0-9 ][^A-Za-z0-9 ]%' -- When there are two or more consecutive special charaters its InValid
OR PartNumber LIKE '%[\^\[\]\\_?;$#&*{}:<>/|''~`]%' ESCAPE '\' -- Add characters here which do not allowed to have any occurrence in the string
)

ANTLR4 - Mutually left-recursive grammar

I am getting the error: The following sets of rules are mutually left-recursive [symbolExpression]. In my grammar, symbolExpression is directly left-recursive so shouldn't ANTLR4 be handling this?
Here are the relevant parts of my parser:
operation:
OPERATOR '(' (operation | values | value | symbolExpression) ')' #OperatorExpression
| bracketedSymbolExpression #BracketedOperatorExpression
;
symbolExpression:
(operation | values | value | symbolExpression) SYMBOL (operation | values | value | symbolExpression);
bracketedSymbolExpression:
'(' (operation | values | value | symbolExpression) SYMBOL (operation | values | value | symbolExpression) ')';
list: '[' (operation | value) (',' (operation | value))* ']';
values: (operation | value) (',' (operation | value))+;
value:
NUMBER
| IDENTIFIER
| list
| object;
The elements 'symbolExpression' and 'operation' in the rule 'symbolExpression' are interdependently left recursive.
Without knowing the language specification, it is impossible to say whether the language itself is irrecoverably ambiguous.
Nonetheless, one avenue to try is to refactor the grammar to move repeated clauses, like
( operation | value )
and
(operation | values | value | symbolExpression)
to their own rules with the goal of unifying the 'operation' and 'symbolExpression' (and perhaps 'bracketedSymbolExpression') rules into a single rule -- a rule that is at most self left-recursive. Something like
a : value
| LPAREN a* RPAREN
| LBRACK a* LBRACK
| a SYMBOL a
| a ( COMMA a )+
;

ANTLR4 : Syntax Error, '<' came as a complete surprise

I try to use the Java.g directly written by Terrence (https://github.com/antlr/grammars-v4/blob/master/java/Java.g4). And I will use this grammar in ANTLRWorks (http://tunnelvisionlabs.com/products/demo/antlrworks). In this code, I got the error
"Syntax Error, '<' came as a complete surprise"
| <assoc=right> expression
( '='
| '+='
| '-='
| '*='
| '/='
| '&='
| '|='
| '^='
| '>>='
| '>>>='
| '<<='
| '%='
)
expression
That means that ANTLRWorks2 is slightly out of date and uses an earlier version of ANTLR. I think Sam will be updating soon.