@@ -202,6 +202,21 @@ class JDBCSuite extends SparkFunSuite
202
202
|partitionColumn '"Dept"', lowerBound '1', upperBound '4', numPartitions '4')
203
203
""" .stripMargin.replaceAll(" \n " , " " ))
204
204
205
+ conn.prepareStatement(
206
+ """ create table test."mixedCaseCols" ("Name" TEXT(32), "Id" INTEGER NOT NULL)""" )
207
+ .executeUpdate()
208
+ conn.prepareStatement(""" insert into test."mixedCaseCols" values ('fred', 1)""" ).executeUpdate()
209
+ conn.prepareStatement(""" insert into test."mixedCaseCols" values ('mary', 2)""" ).executeUpdate()
210
+ conn.prepareStatement(""" insert into test."mixedCaseCols" values (null, 3)""" ).executeUpdate()
211
+ conn.commit()
212
+
213
+ sql(
214
+ s """
215
+ |CREATE TEMPORARY TABLE mixedCaseCols
216
+ |USING org.apache.spark.sql.jdbc
217
+ |OPTIONS (url ' $url', dbtable 'TEST."mixedCaseCols"', user 'testUser', password 'testPass')
218
+ """ .stripMargin.replaceAll(" \n " , " " ))
219
+
205
220
// Untested: IDENTITY, OTHER, UUID, ARRAY, and GEOMETRY types.
206
221
}
207
222
@@ -604,30 +619,32 @@ class JDBCSuite extends SparkFunSuite
604
619
605
620
test(" compile filters" ) {
606
621
val compileFilter = PrivateMethod [Option [String ]](' compileFilter )
607
- def doCompileFilter (f : Filter ): String = JDBCRDD invokePrivate compileFilter(f) getOrElse(" " )
608
- assert(doCompileFilter(EqualTo (" col0" , 3 )) === " col0 = 3" )
609
- assert(doCompileFilter(Not (EqualTo (" col1" , " abc" ))) === " (NOT (col1 = 'abc'))" )
622
+ def doCompileFilter (f : Filter ): String =
623
+ JDBCRDD invokePrivate compileFilter(f, JdbcDialects .get(" jdbc:" )) getOrElse(" " )
624
+ assert(doCompileFilter(EqualTo (" col0" , 3 )) === """ "col0" = 3""" )
625
+ assert(doCompileFilter(Not (EqualTo (" col1" , " abc" ))) === """ (NOT ("col1" = 'abc'))""" )
610
626
assert(doCompileFilter(And (EqualTo (" col0" , 0 ), EqualTo (" col1" , " def" )))
611
- === " ( col0 = 0) AND (col1 = 'def')" )
627
+ === """ (" col0" = 0) AND (" col1" = 'def')"" " )
612
628
assert(doCompileFilter(Or (EqualTo (" col0" , 2 ), EqualTo (" col1" , " ghi" )))
613
- === " ( col0 = 2) OR (col1 = 'ghi')" )
614
- assert(doCompileFilter(LessThan (" col0" , 5 )) === " col0 < 5" )
629
+ === """ (" col0" = 2) OR (" col1" = 'ghi')"" " )
630
+ assert(doCompileFilter(LessThan (" col0" , 5 )) === """ " col0" < 5"" " )
615
631
assert(doCompileFilter(LessThan (" col3" ,
616
- Timestamp .valueOf(" 1995-11-21 00:00:00.0" ))) === " col3 < '1995-11-21 00:00:00.0'" )
617
- assert(doCompileFilter(LessThan (" col4" , Date .valueOf(" 1983-08-04" ))) === " col4 < '1983-08-04'" )
618
- assert(doCompileFilter(LessThanOrEqual (" col0" , 5 )) === " col0 <= 5" )
619
- assert(doCompileFilter(GreaterThan (" col0" , 3 )) === " col0 > 3" )
620
- assert(doCompileFilter(GreaterThanOrEqual (" col0" , 3 )) === " col0 >= 3" )
621
- assert(doCompileFilter(In (" col1" , Array (" jkl" ))) === " col1 IN ('jkl')" )
632
+ Timestamp .valueOf(" 1995-11-21 00:00:00.0" ))) === """ "col3" < '1995-11-21 00:00:00.0'""" )
633
+ assert(doCompileFilter(LessThan (" col4" , Date .valueOf(" 1983-08-04" )))
634
+ === """ "col4" < '1983-08-04'""" )
635
+ assert(doCompileFilter(LessThanOrEqual (" col0" , 5 )) === """ "col0" <= 5""" )
636
+ assert(doCompileFilter(GreaterThan (" col0" , 3 )) === """ "col0" > 3""" )
637
+ assert(doCompileFilter(GreaterThanOrEqual (" col0" , 3 )) === """ "col0" >= 3""" )
638
+ assert(doCompileFilter(In (" col1" , Array (" jkl" ))) === """ "col1" IN ('jkl')""" )
622
639
assert(doCompileFilter(In (" col1" , Array .empty)) ===
623
- " CASE WHEN col1 IS NULL THEN NULL ELSE FALSE END" )
640
+ """ CASE WHEN " col1" IS NULL THEN NULL ELSE FALSE END"" " )
624
641
assert(doCompileFilter(Not (In (" col1" , Array (" mno" , " pqr" ))))
625
- === " (NOT (col1 IN ('mno', 'pqr')))" )
626
- assert(doCompileFilter(IsNull (" col1" )) === " col1 IS NULL" )
627
- assert(doCompileFilter(IsNotNull (" col1" )) === " col1 IS NOT NULL" )
642
+ === """ (NOT (" col1" IN ('mno', 'pqr')))"" " )
643
+ assert(doCompileFilter(IsNull (" col1" )) === """ " col1" IS NULL"" " )
644
+ assert(doCompileFilter(IsNotNull (" col1" )) === """ " col1" IS NOT NULL"" " )
628
645
assert(doCompileFilter(And (EqualNullSafe (" col0" , " abc" ), EqualTo (" col1" , " def" )))
629
- === " ((NOT (col0 != 'abc' OR col0 IS NULL OR 'abc' IS NULL) "
630
- + " OR (col0 IS NULL AND 'abc' IS NULL))) AND (col1 = 'def')" )
646
+ === """ ((NOT (" col0" != 'abc' OR " col0" IS NULL OR 'abc' IS NULL) "" "
647
+ + """ OR (" col0" IS NULL AND 'abc' IS NULL))) AND (" col1" = 'def')"" " )
631
648
}
632
649
633
650
test(" Dialect unregister" ) {
@@ -824,4 +841,24 @@ class JDBCSuite extends SparkFunSuite
824
841
val schema = JdbcUtils .schemaString(df.schema, " jdbc:mysql://localhost:3306/temp" )
825
842
assert(schema.contains(" `order` TEXT" ))
826
843
}
844
+
845
+ test(" SPARK-18141: Predicates on quoted column names in the jdbc data source" ) {
846
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id < 1" ).collect().size == 0 )
847
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id <= 1" ).collect().size == 1 )
848
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id > 1" ).collect().size == 2 )
849
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id >= 1" ).collect().size == 3 )
850
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id = 1" ).collect().size == 1 )
851
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id != 2" ).collect().size == 2 )
852
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id <=> 2" ).collect().size == 1 )
853
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name LIKE 'fr%'" ).collect().size == 1 )
854
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name LIKE '%ed'" ).collect().size == 1 )
855
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name LIKE '%re%'" ).collect().size == 1 )
856
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name IS NULL" ).collect().size == 1 )
857
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name IS NOT NULL" ).collect().size == 2 )
858
+ assert(sql(" SELECT * FROM mixedCaseCols" ).filter($" Name" .isin()).collect().size == 0 )
859
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name IN ('mary', 'fred')" ).collect().size == 2 )
860
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name NOT IN ('fred')" ).collect().size == 1 )
861
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id = 1 OR Name = 'mary'" ).collect().size == 2 )
862
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name = 'mary' AND Id = 2" ).collect().size == 1 )
863
+ }
827
864
}
0 commit comments