Address PR comments: also check for literal EQ columnref

This commit is contained in:
Alessandro Pasotti 2024-11-14 10:00:29 +01:00
parent b4ca6bca6e
commit dccbed2cc0
2 changed files with 13 additions and 1 deletions

View File

@ -716,7 +716,7 @@ bool QgsExpressionNodeBinaryOperator::prepareNode( QgsExpression *parent, const
if ( op->op() == boEQ )
{
// If left is a column ref and right is a literal, collect
if ( dynamic_cast<QgsExpressionNodeColumnRef *>( op->opLeft() ) && dynamic_cast<QgsExpressionNodeLiteral *>( op->opRight() ) )
if ( ( dynamic_cast<QgsExpressionNodeColumnRef *>( op->opLeft() ) && dynamic_cast<QgsExpressionNodeLiteral *>( op->opRight() ) ) )
{
const QString fieldName = op->opLeft()->dump();
if ( !orValuesMap.contains( fieldName ) )
@ -727,6 +727,17 @@ bool QgsExpressionNodeBinaryOperator::prepareNode( QgsExpression *parent, const
orValuesMap[fieldName].append( op->opRight()->clone() );
return true;
}
else if ( ( dynamic_cast<QgsExpressionNodeColumnRef *>( op->opRight() ) && dynamic_cast<QgsExpressionNodeLiteral *>( op->opLeft() ) ) )
{
const QString fieldName = op->opRight()->dump();
if ( !orValuesMap.contains( fieldName ) )
{
orFieldNames.append( fieldName );
orValuesMap.insert( fieldName, QgsExpressionNode::NodeList() );
}
orValuesMap[fieldName].append( op->opLeft()->clone() );
return true;
}
return false;
}

View File

@ -5600,6 +5600,7 @@ class TestQgsExpression: public QObject
QTest::newRow( "simple3 mixed" ) << QStringLiteral( "field = 'value' OR field = 'value2' OR field2 = 'value3'" ) << QStringLiteral( "field IN ('value', 'value2') OR field2 = 'value3'" );
QTest::newRow( "simple3 mixed 2" ) << QStringLiteral( "field2 = 'value3' OR field = 'value1' OR field = 'value2'" ) << QStringLiteral( "field2 = 'value3' OR field IN ('value1', 'value2')" );
QTest::newRow( "simple3 mixed 3" ) << QStringLiteral( "field = 'value1' OR field2 = 'value3' OR field = 'value2'" ) << QStringLiteral( "field IN ('value1', 'value2') OR field2 = 'value3'" );
QTest::newRow( "simple mixed order" ) << QStringLiteral( "field = 'value' OR 'value2' = field OR field = 'value3'" ) << QStringLiteral( "field IN ('value', 'value2', 'value3')" );
// test with IN
QTest::newRow( "simple IN" ) << QStringLiteral( "field IN ('value', 'value2') OR field = 'value3'" ) << QStringLiteral( "field IN ('value', 'value2', 'value3')" );