Skip to content

Commit 8a2e871

Browse files
committed
Always use string comparison in expressions for string fields
Fixes #13204
1 parent 85fbeb2 commit 8a2e871

File tree

2 files changed

+24
-3
lines changed

2 files changed

+24
-3
lines changed

src/core/qgsexpression.cpp

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4161,9 +4161,11 @@ QVariant QgsExpression::NodeBinaryOperator::eval( QgsExpression *parent, const Q
41614161
{
41624162
return TVL_Unknown;
41634163
}
4164-
else if ( isDoubleSafe( vL ) && isDoubleSafe( vR ) )
4164+
else if ( isDoubleSafe( vL ) && isDoubleSafe( vR ) &&
4165+
( vL.type() != QVariant::String || vR.type() != QVariant::String ) )
41654166
{
4166-
// do numeric comparison if both operators can be converted to numbers
4167+
// do numeric comparison if both operators can be converted to numbers,
4168+
// and they aren't both string
41674169
double fL = getDoubleValue( vL, parent );
41684170
ENSURE_NO_EVAL_ERROR;
41694171
double fR = getDoubleValue( vR, parent );
@@ -4190,7 +4192,8 @@ QVariant QgsExpression::NodeBinaryOperator::eval( QgsExpression *parent, const Q
41904192
else // both operators non-null
41914193
{
41924194
bool equal = false;
4193-
if ( isDoubleSafe( vL ) && isDoubleSafe( vR ) )
4195+
if ( isDoubleSafe( vL ) && isDoubleSafe( vR ) &&
4196+
( vL.type() != QVariant::String || vR.type() != QVariant::String ) )
41944197
{
41954198
double fL = getDoubleValue( vL, parent );
41964199
ENSURE_NO_EVAL_ERROR;

tests/src/core/testqgsexpression.cpp

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,20 @@ class TestQgsExpression: public QObject
427427
QTest::newRow( "'nan'='x'" ) << "'nan'='x'" << false << QVariant( 0 );
428428
QTest::newRow( "'inf'='inf'" ) << "'inf'='inf'" << false << QVariant( 1 );
429429
QTest::newRow( "'inf'='x'" ) << "'inf'='x'" << false << QVariant( 0 );
430+
QTest::newRow( "'1.1'='1.1'" ) << "'1.1'='1.1'" << false << QVariant( 1 );
431+
QTest::newRow( "'1.1'!='1.1'" ) << "'1.1'!='1.1'" << false << QVariant( 0 );
432+
QTest::newRow( "'1.1'='1.10'" ) << "'1.1'='1.10'" << false << QVariant( 0 );
433+
QTest::newRow( "'1.1'!='1.10'" ) << "'1.1'!='1.10'" << false << QVariant( 1 );
434+
QTest::newRow( "1.1=1.10" ) << "1.1=1.10" << false << QVariant( 1 );
435+
QTest::newRow( "1.1 != 1.10" ) << "1.1 != 1.10" << false << QVariant( 0 );
436+
QTest::newRow( "'1.1'=1.1" ) << "'1.1'=1.1" << false << QVariant( 1 );
437+
QTest::newRow( "'1.10'=1.1" ) << "'1.10'=1.1" << false << QVariant( 1 );
438+
QTest::newRow( "1.1='1.10'" ) << "1.1='1.10'" << false << QVariant( 1 );
439+
QTest::newRow( "'1.1'='1.10000'" ) << "'1.1'='1.10000'" << false << QVariant( 0 );
440+
QTest::newRow( "'1E-23'='1E-23'" ) << "'1E-23'='1E-23'" << false << QVariant( 1 );
441+
QTest::newRow( "'1E-23'!='1E-23'" ) << "'1E-23'!='1E-23'" << false << QVariant( 0 );
442+
QTest::newRow( "'1E-23'='2E-23'" ) << "'1E-23'='2E-23'" << false << QVariant( 0 );
443+
QTest::newRow( "'1E-23'!='2E-23'" ) << "'1E-23'!='2E-23'" << false << QVariant( 1 );
430444

431445
// is, is not
432446
QTest::newRow( "is null,null" ) << "null is null" << false << QVariant( 1 );
@@ -437,6 +451,10 @@ class TestQgsExpression: public QObject
437451
QTest::newRow( "is not int" ) << "1 is not 1" << false << QVariant( 0 );
438452
QTest::newRow( "is text" ) << "'x' is 'y'" << false << QVariant( 0 );
439453
QTest::newRow( "is not text" ) << "'x' is not 'y'" << false << QVariant( 1 );
454+
QTest::newRow( "'1.1' is '1.10'" ) << "'1.1' is '1.10'" << false << QVariant( 0 );
455+
QTest::newRow( "'1.1' is '1.10000'" ) << "'1.1' is '1.10000'" << false << QVariant( 0 );
456+
QTest::newRow( "1.1 is '1.10'" ) << "1.1 is '1.10'" << false << QVariant( 1 );
457+
QTest::newRow( "'1.10' is 1.1" ) << "'1.10' is 1.1" << false << QVariant( 1 );
440458

441459
// logical
442460
QTest::newRow( "T or F" ) << "1=1 or 2=3" << false << QVariant( 1 );

0 commit comments

Comments
 (0)