@@ -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
@@ -632,30 +647,32 @@ class JDBCSuite extends SparkFunSuite
632
647
633
648
test(" compile filters" ) {
634
649
val compileFilter = PrivateMethod [Option [String ]](' compileFilter )
635
- def doCompileFilter (f : Filter ): String = JDBCRDD invokePrivate compileFilter(f) getOrElse(" " )
636
- assert(doCompileFilter(EqualTo (" col0" , 3 )) === " col0 = 3" )
637
- assert(doCompileFilter(Not (EqualTo (" col1" , " abc" ))) === " (NOT (col1 = 'abc'))" )
650
+ def doCompileFilter (f : Filter ): String =
651
+ JDBCRDD invokePrivate compileFilter(f, JdbcDialects .get(" jdbc:" )) getOrElse(" " )
652
+ assert(doCompileFilter(EqualTo (" col0" , 3 )) === """ "col0" = 3""" )
653
+ assert(doCompileFilter(Not (EqualTo (" col1" , " abc" ))) === """ (NOT ("col1" = 'abc'))""" )
638
654
assert(doCompileFilter(And (EqualTo (" col0" , 0 ), EqualTo (" col1" , " def" )))
639
- === " ( col0 = 0) AND (col1 = 'def')" )
655
+ === """ (" col0" = 0) AND (" col1" = 'def')"" " )
640
656
assert(doCompileFilter(Or (EqualTo (" col0" , 2 ), EqualTo (" col1" , " ghi" )))
641
- === " ( col0 = 2) OR (col1 = 'ghi')" )
642
- assert(doCompileFilter(LessThan (" col0" , 5 )) === " col0 < 5" )
657
+ === """ (" col0" = 2) OR (" col1" = 'ghi')"" " )
658
+ assert(doCompileFilter(LessThan (" col0" , 5 )) === """ " col0" < 5"" " )
643
659
assert(doCompileFilter(LessThan (" col3" ,
644
- Timestamp .valueOf(" 1995-11-21 00:00:00.0" ))) === " col3 < '1995-11-21 00:00:00.0'" )
645
- assert(doCompileFilter(LessThan (" col4" , Date .valueOf(" 1983-08-04" ))) === " col4 < '1983-08-04'" )
646
- assert(doCompileFilter(LessThanOrEqual (" col0" , 5 )) === " col0 <= 5" )
647
- assert(doCompileFilter(GreaterThan (" col0" , 3 )) === " col0 > 3" )
648
- assert(doCompileFilter(GreaterThanOrEqual (" col0" , 3 )) === " col0 >= 3" )
649
- assert(doCompileFilter(In (" col1" , Array (" jkl" ))) === " col1 IN ('jkl')" )
660
+ Timestamp .valueOf(" 1995-11-21 00:00:00.0" ))) === """ "col3" < '1995-11-21 00:00:00.0'""" )
661
+ assert(doCompileFilter(LessThan (" col4" , Date .valueOf(" 1983-08-04" )))
662
+ === """ "col4" < '1983-08-04'""" )
663
+ assert(doCompileFilter(LessThanOrEqual (" col0" , 5 )) === """ "col0" <= 5""" )
664
+ assert(doCompileFilter(GreaterThan (" col0" , 3 )) === """ "col0" > 3""" )
665
+ assert(doCompileFilter(GreaterThanOrEqual (" col0" , 3 )) === """ "col0" >= 3""" )
666
+ assert(doCompileFilter(In (" col1" , Array (" jkl" ))) === """ "col1" IN ('jkl')""" )
650
667
assert(doCompileFilter(In (" col1" , Array .empty)) ===
651
- " CASE WHEN col1 IS NULL THEN NULL ELSE FALSE END" )
668
+ """ CASE WHEN " col1" IS NULL THEN NULL ELSE FALSE END"" " )
652
669
assert(doCompileFilter(Not (In (" col1" , Array (" mno" , " pqr" ))))
653
- === " (NOT (col1 IN ('mno', 'pqr')))" )
654
- assert(doCompileFilter(IsNull (" col1" )) === " col1 IS NULL" )
655
- assert(doCompileFilter(IsNotNull (" col1" )) === " col1 IS NOT NULL" )
670
+ === """ (NOT (" col1" IN ('mno', 'pqr')))"" " )
671
+ assert(doCompileFilter(IsNull (" col1" )) === """ " col1" IS NULL"" " )
672
+ assert(doCompileFilter(IsNotNull (" col1" )) === """ " col1" IS NOT NULL"" " )
656
673
assert(doCompileFilter(And (EqualNullSafe (" col0" , " abc" ), EqualTo (" col1" , " def" )))
657
- === " ((NOT (col0 != 'abc' OR col0 IS NULL OR 'abc' IS NULL) "
658
- + " OR (col0 IS NULL AND 'abc' IS NULL))) AND (col1 = 'def')" )
674
+ === """ ((NOT (" col0" != 'abc' OR " col0" IS NULL OR 'abc' IS NULL) "" "
675
+ + """ OR (" col0" IS NULL AND 'abc' IS NULL))) AND (" col1" = 'def')"" " )
659
676
}
660
677
661
678
test(" Dialect unregister" ) {
@@ -853,4 +870,24 @@ class JDBCSuite extends SparkFunSuite
853
870
val schema = JdbcUtils .schemaString(df.schema, " jdbc:mysql://localhost:3306/temp" )
854
871
assert(schema.contains(" `order` TEXT" ))
855
872
}
873
+
874
+ test(" SPARK-18141: Predicates on quoted column names in the jdbc data source" ) {
875
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id < 1" ).collect().size == 0 )
876
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id <= 1" ).collect().size == 1 )
877
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id > 1" ).collect().size == 2 )
878
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id >= 1" ).collect().size == 3 )
879
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id = 1" ).collect().size == 1 )
880
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id != 2" ).collect().size == 2 )
881
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id <=> 2" ).collect().size == 1 )
882
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name LIKE 'fr%'" ).collect().size == 1 )
883
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name LIKE '%ed'" ).collect().size == 1 )
884
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name LIKE '%re%'" ).collect().size == 1 )
885
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name IS NULL" ).collect().size == 1 )
886
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name IS NOT NULL" ).collect().size == 2 )
887
+ assert(sql(" SELECT * FROM mixedCaseCols" ).filter($" Name" .isin()).collect().size == 0 )
888
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name IN ('mary', 'fred')" ).collect().size == 2 )
889
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name NOT IN ('fred')" ).collect().size == 1 )
890
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Id = 1 OR Name = 'mary'" ).collect().size == 2 )
891
+ assert(sql(" SELECT * FROM mixedCaseCols WHERE Name = 'mary' AND Id = 2" ).collect().size == 1 )
892
+ }
856
893
}
0 commit comments