How to prevent MariaDB from simplfiying logical expressions - view

I'm having a very weird problem with MariaDB 10, it simplify automaticaly some logical expression !!
here's an example
CREATE VIEW test AS
SELECT 1
FROM test_table
WHERE NOT (1 < 2 OR 3 > 4);
SHOW CREATE VIEW test;
CREATE ALGORITHM = UNDEFINED
DEFINER =`root`#`localhost`
SQL SECURITY DEFINER VIEW `test` AS
SELECT 1 AS `1`
FROM `test_table`
WHERE ((1 >= 2) AND (3 <= 4))
as you see it has transformed the expression NOT (1 < 2 OR 3 > 4) to ((1 >= 2) AND (3 <= 4))
because it's not correct in intervals cases

MariaDB applied one of the http://en.wikipedia.org/wiki/De_Morgan%27s_laws . Why do you think it is not correct?

Related

In Oracle, repeatedly assign values from a set to column

I have a table in Oracle that has a column that I would like to assign a value to from a set of possible values. I like to assign the values in order of the set, repeatedly, for the entire table.
For example:
If the set of values is {1, 2, 3}. I'd like to assign the values in this pattern until the last row is reached:
rowNum someCol valueCol
1 this 1
2 is 2
3 some 3
4 other 1
5 column 2
6 in 3
7 the 1
8 table 2
I can't figure out how to do this with a traditional update statement. Anone that could help with this problem?
Use Modulo to achieve desire result
UPDATE TableName
SET valueCol= CASE WHEN rowNum % 3 == 1 then 1
WHEN rowNum % 3 == 2 then 2
WHEN rowNum % 3 == 0 then 3
END
update tablename
set valuecol = case mod(rownum, 3) when 0 then 3 else mod(rownum, 3) end
;

converting Infix to prefix conversion

Recently I have went through some sites,
for converting the Infix to Prefix notation and finally got tucked up.
I have given the steps which I have done..
Ex:- (1+(2*3)) + (5*6) + (7/8)
Method 1:- (Manual Conversion without any algorithm):-
Step1: (1+(*23)) + (*56) + (/78)
Step2: (+1*23) + (*56) + (/78)
Step3: +(+1*23)(*56) + (/78)
Step4: +[+(+1*23)(*56)](/78)
Step5: +++1*23*56/78 **--- Final Ans -----**
Method 2:-
As per the site http://scanftree.com/Data_Structure/infix-to-prefix
Step1: Reverse it:-
) 8 / 7 ( + ) 6 * 5 ( + ) ) 3 * 2 ( + 1 (
Step2: Replace the '(' by ')' and vice versa:
( 8 / 7 ) + ( 6 * 5 ) + ( ( 3 * 2 ) + 1 )
Step3: Convert the expression to postfix form:-
8 7 / 6 5 * + 3 2 * 1 + +
Step4: Reverse it
+ + 1 * 2 3 + * 5 6 / 7 8 --- Final Ans -----
So, here I got totally hanged.
Could any one please provide some light on following things:-
On where I went wrong in the above 2 methods
Which is the right answer
so I can able to understand the concept more better.
Your method is not correct, look at the comment, it says that ( a + b ) + c = a + ( b + c ) . What about (a + b) * c? (a + b) * c != a + (b * c).
According to your manual algorithm, you put the last + is placed to the front. It is not correct. If you use * instead of last + , where did you put it ? Just think about that, then you can easily find the problem with your algorithm.
Another algorithm for solving this problem is just parenthesis it before proceeding. Replace every left parenthesis with the operator inside it.
Example, ((1+(2*3)) + ((5*6) + (7/8))) then it become ++1*23+*56+/78. Your algorithm is correct if the precedence of the operator inside is same. If it is not it will fail.
NOTE : Your answer can also be obtained by the arrangement of parenthesis. (((1+(2*3)) + (5*6)) + (7/8)) then it becomes +++1*23*56/78. But if the last one is * instead of + then it doesn't work.
(b * b - 4 * a * c ) / (2 * c) EQUATION--1;
Now I will solve this mathematically by substituting different variables, and doing 2 terms at time.
=> x=bb* ; y=4a* ; z=2c*
these above are the substitution of first time, use in eq 1
( x - y * c )/(z)
again doing the substitutions with new variables.
=> i=yc* ;
(x - i)/z
=> j=xi-;
j/z
Now this here is the base case solve it then substitute back all the variables accordingly.
jz/
Now back substitution
xi- 2c* /
bb* yc * - 2c* /
bb* 4a* c * -2a*/
The Manual conversion is correct because when we reverse the infix expression to calculate the prefix, the associativity of the expression changes from left to right to right to left which is not considered in the algorithm and often it is ignored.
Example:
expression:5-3-2 :infix to prefix(manual conversion)
(-5 3)-2
-(- 5 3) 2
- - 5 3 2
now by the algorithm(if associativity not changed)
reverse expression: 2 - 3 - 5
postfix: 2 3 - 5 -
again reverse to get prefix: - 5 - 3 2
now see if we ignored the associativity, it made a huge difference
now if we change the associativity from left to right to right to left:
then :
reverse expression: 2 - 3 - 5
postfix: 2 3 5 - - (like a^b^b to postfix: abc^^ because it is also right associative)
reverse - - 5 3 2

Arithmetic expression expressed as tree

Imagine an arithmetic expression such as (+ 1 (* 2 (- 3 5))) being thought of as a tree-like structure with numbers at the leaves and operator symbols at the interior nodes like below:
+
/ \
1 *
/ \
2 -
/ \
3 5
each node can be expressed by a three element list: (left-operand operator right-operand)
I am trying to write a function
(make-expr left-op operator right-op)
that produces
(left-op operator right-op)
for example:
(make-expr '(6 * 3) '+ '(5 -2)) => ((6 * 3) + (5 - 2))
> (list '(6 * 3) '+ '(5 - 2))
((6 * 3) + (5 - 2)
Yes, it's as simple as using the list function.

How to sum incrementaly in Oracle 8?

I need to do an incremental sum in Oracle.
My situation is the following:
RecordID Value
1 1
2 2
3 5
4 10
And I need to get something like this:
RecordID Sum_incremental
1 (1)
2 (1 + 2)
3 (1 + 2 + 5)
4 (1 + 2 + 5 + 10)
The clues: self join and group by.
The solution:
select a.recordid, sum(b.value) sum_incremental from mytable a, mytable b
where b.recordid <= a.recordid group by a.recordid
select recordid,
sum(value) over (order by recordid)
from some_data

Efficient program to print/return all increasing subsequences of size 3 in an array

Given an array like
1, 6, 5, 2, 3, 4
we need to print
1 2 3
1 3 4
1 2 4
2 3 4
What is the best way to do this?
Is this dynamic programming?
Is there a better way to do than the bruteforce O(n3)? I am sure there is.
The reason I say dynamic programming is because I can see this as something like
for '1' (print all results of sub problem of the rest of the array with subsequences of size 2).
for '2' (print all results of sub problems of the rest of the array with subseqences of size 2)
and go on like this.
However, there is a lot of overlap in the above two results, so we need to find an efficient way of reusing that, I guess.
Well, these are just random thoughts. You can correct me with the right appraoch.
OK, let me correct, if not print, I need the different increasing sequences returned. My point is, I need to find an approach to get to these sequences in the most efficient way.
You can walk through the array and remember what partial sequences are possible until the current point. Print and forget any sequences that reach length 3.
Example:
(1 6 5 2 3 4)
^
remember ((1))
(1 6 5 2 3 4)
^
remember ((1) (1 6) (6))
(1 6 5 2 3 4)
^
remember ((1) (1 6) (6) (1 5) (5))
(1 6 5 2 3 4)
^
remember ((1) (1 6) (6) (1 5) (5) (1 2) (2))
(1 6 5 2 3 4)
^
remember ((1) (1 6) (6) (1 5) (5) (1 2) (2) (1 3) (1 2 3) (2 3) (3))
print and forget (1 2 3)
remember ((1) (1 6) (6) (1 5) (5) (1 2) (2) (1 3) (2 3) (3))
(1 6 5 2 3 4)
^
remember ((1) (1 6) (6) (1 5) (5) (1 2) (2) (1 3) (2 3) (3) (1 4) (1 2 4) (2 4)
(1 3 4) (2 3 4) (3 4) (4))
print and forget (1 2 4)
print and forget (1 3 4)
print and forget (2 3 4)
done.
The challenge seems to lie in the choice of an appropriate data structure for the remembered subsequences.
In the generalized case you have to calculate the complexity based on two things:
1- Count of input numbers (I will call it b)
2- Length of output (I will call it d)
A generalized method that I can think of, is to construct an analogous graph to the problem in O(n^2):
If a larger number comes after a smaller number, There is a directed edge from smaller one to it.
Now in order to find all sequences of length d, You need to start from each number and output all paths of length (d - 1).
If you use a traversal method like BFS the complexity will be less than O(d x (b ^ (d - 1))).
However you can use adjacent matrix multiplication to find the paths of length d, which will bring the complexity down to something less than O((d - 2) x (b ^ 3)). (Nth power of an adjacency matrix will tell you how many paths exist from each node to another with length of N).
There are algorithms to reduce square matrix multiplication complexity a bit.
Create a list of ordered pairs (a,b) such that a<b and Index(a) < Index(b). O(n^2)
Sort this list (on either a or b -- doesn't matter) in O(n^2log(n)). Can be made O(nlog(n)) depending on data structure.
For each element in the list, find all matching ordered pairs using binary search -- worst case O(n^3log(n)), average case O(n^2log(n))

Resources