|
28 | 28 | import tech.ydb.jdbc.impl.helper.ExceptionAssert; |
29 | 29 | import tech.ydb.jdbc.impl.helper.JdbcConnectionExtention; |
30 | 30 | import tech.ydb.jdbc.impl.helper.SqlQueries; |
| 31 | +import tech.ydb.jdbc.impl.helper.StatsAssert; |
31 | 32 | import tech.ydb.jdbc.impl.helper.TableAssert; |
32 | 33 | import tech.ydb.test.junit5.YdbHelperExtension; |
33 | 34 |
|
@@ -927,83 +928,215 @@ public void testUnsupportedComplexTypes(String type) throws SQLException { |
927 | 928 | } |
928 | 929 |
|
929 | 930 | @Test |
930 | | - public void testFullScanAnalyzer() throws SQLException { |
931 | | - try (Connection connection = jdbc.createCustomConnection("jdbcFullScanDetector", "true")) { |
932 | | - String selectAll = QUERIES.selectAllSQL(); |
933 | | - String selectByKey = QUERIES.selectAllByKey("1"); |
934 | | - String preparedSelect = QUERIES.selectAllByKey("?"); |
| 931 | + public void fullScanAnalyzerSchemeQueriesTest() throws SQLException { |
| 932 | + StatsAssert sa = new StatsAssert(); |
| 933 | + |
| 934 | + String createTable = QUERIES.withTableName("CREATE TABLE #tableName_1(id Int32, value Int32, PRIMARY KEY(id))"); |
| 935 | + String dropTable = QUERIES.withTableName("DROP TABLE #tableName_1;"); |
935 | 936 |
|
| 937 | + try (Connection connection = jdbc.createCustomConnection("jdbcFullScanDetector", "true")) { |
936 | 938 | try (Statement st = connection.createStatement()) { |
937 | 939 | try (ResultSet rs = st.executeQuery(" print_JDBC_stats(); ")) { |
938 | | - Assertions.assertFalse(rs.next()); // not stats |
| 940 | + sa.check(rs) |
| 941 | + .assertMetaColumns() |
| 942 | + .assertNoRows(); |
939 | 943 | } |
940 | 944 |
|
941 | | - try (ResultSet rs = st.executeQuery(selectAll)) { |
942 | | - Assertions.assertFalse(rs.next()); |
| 945 | + // scheme queries don't collect stats |
| 946 | + Assertions.assertFalse(st.execute(createTable)); |
| 947 | + Assertions.assertFalse(st.execute(dropTable)); |
| 948 | + |
| 949 | + try (ResultSet rs = st.executeQuery("Print_JDBC_stats();\n")) { |
| 950 | + sa.check(rs) |
| 951 | + .assertMetaColumns() |
| 952 | + .assertNoRows(); |
| 953 | + } |
| 954 | + } |
| 955 | + } |
| 956 | + } |
| 957 | + |
| 958 | + @Test |
| 959 | + public void fullScanAnalyzerSchemeWrongQueryTest() throws SQLException { |
| 960 | + StatsAssert sa = new StatsAssert(); |
| 961 | + |
| 962 | + String wrongQuery = QUERIES.withTableName("select * from wrong_table;"); |
| 963 | + |
| 964 | + try (Connection connection = jdbc.createCustomConnection("jdbcFullScanDetector", "true")) { |
| 965 | + try (Statement st = connection.createStatement()) { |
| 966 | + try (ResultSet rs = st.executeQuery("print_JDBC_stats();")) { |
| 967 | + sa.check(rs) |
| 968 | + .assertMetaColumns() |
| 969 | + .assertNoRows(); |
943 | 970 | } |
944 | 971 |
|
| 972 | + // scheme queries don't collect stats |
| 973 | + ExceptionAssert.ydbException("Cannot find table", () -> st.execute(wrongQuery)); |
| 974 | + |
945 | 975 | try (ResultSet rs = st.executeQuery("Print_JDBC_stats();\n")) { |
946 | | - Assertions.assertTrue(rs.next()); |
947 | | - Assertions.assertEquals(selectAll, rs.getString("sql")); |
948 | | - Assertions.assertEquals(true, rs.getBoolean("is_fullscan")); |
949 | | - Assertions.assertEquals(1l, rs.getLong("executed")); |
| 976 | + TableAssert.ResultSetAssert check = sa.check(rs).assertMetaColumns(); |
950 | 977 |
|
951 | | - Assertions.assertFalse(rs.next()); |
| 978 | + check.nextRow( |
| 979 | + sa.sql("select * from wrong_table;"), |
| 980 | + sa.yql("select * from wrong_table;"), |
| 981 | + sa.isNotFullScan(), sa.isError(), sa.executed(1), sa.hasNoAst(), sa.hasPlan() |
| 982 | + ).assertAll(); |
| 983 | + |
| 984 | + check.assertNoRows(); |
952 | 985 | } |
| 986 | + } |
| 987 | + } |
| 988 | + } |
| 989 | + |
| 990 | + @Test |
| 991 | + public void fullScanAnalyzerStatementTest() throws SQLException { |
| 992 | + StatsAssert sa = new StatsAssert(); |
| 993 | + |
| 994 | + String selectAll = QUERIES.selectAllSQL(); |
| 995 | + String selectByKey = QUERIES.selectAllByKey("1"); |
953 | 996 |
|
| 997 | + try (Connection connection = jdbc.createCustomConnection("jdbcFullScanDetector", "true")) { |
| 998 | + try (Statement st = connection.createStatement()) { |
| 999 | + try (ResultSet rs = st.executeQuery(" print_JDBC_stats(); ")) { |
| 1000 | + sa.check(rs) |
| 1001 | + .assertMetaColumns() |
| 1002 | + .assertNoRows(); |
| 1003 | + } |
| 1004 | + |
| 1005 | + // full scan query |
954 | 1006 | try (ResultSet rs = st.executeQuery(selectAll)) { |
955 | 1007 | Assertions.assertFalse(rs.next()); |
956 | 1008 | } |
| 1009 | + |
| 1010 | + try (ResultSet rs = st.executeQuery("\tPrint_JDBC_staTs();")) { |
| 1011 | + TableAssert.ResultSetAssert check = sa.check(rs).assertMetaColumns(); |
| 1012 | + |
| 1013 | + check.nextRow( |
| 1014 | + sa.sql("select * from ydb_connection_test"), |
| 1015 | + sa.yql("select * from ydb_connection_test"), |
| 1016 | + sa.isFullScan(), sa.isNotError(), sa.executed(1), sa.hasAst(), sa.hasPlan() |
| 1017 | + ).assertAll(); |
| 1018 | + |
| 1019 | + check.assertNoRows(); |
| 1020 | + } |
| 1021 | + |
| 1022 | + // key read query |
957 | 1023 | try (ResultSet rs = st.executeQuery(selectByKey)) { |
958 | 1024 | Assertions.assertFalse(rs.next()); |
959 | 1025 | } |
960 | 1026 |
|
961 | | - try (ResultSet rs = st.executeQuery("Print_JDBC_stats();\n")) { |
962 | | - Assertions.assertTrue(rs.next()); |
963 | | - Assertions.assertEquals(selectAll, rs.getString("sql")); |
964 | | - Assertions.assertEquals(true, rs.getBoolean("is_fullscan")); |
965 | | - Assertions.assertEquals(2l, rs.getLong("executed")); |
| 1027 | + try (ResultSet rs = st.executeQuery("print_JDBC_staTs();")) { |
| 1028 | + TableAssert.ResultSetAssert check = sa.check(rs).assertMetaColumns(); |
966 | 1029 |
|
967 | | - Assertions.assertTrue(rs.next()); |
968 | | - Assertions.assertEquals(selectByKey, rs.getString("sql")); |
969 | | - Assertions.assertEquals(false, rs.getBoolean("is_fullscan")); |
970 | | - Assertions.assertEquals(1l, rs.getLong("executed")); |
| 1030 | + check.nextRow( |
| 1031 | + sa.sql("select * from ydb_connection_test"), |
| 1032 | + sa.yql("select * from ydb_connection_test"), |
| 1033 | + sa.isFullScan(), sa.isNotError(), sa.executed(1), sa.hasAst(), sa.hasPlan() |
| 1034 | + ).assertAll(); |
971 | 1035 |
|
| 1036 | + check.nextRow( |
| 1037 | + sa.sql("select * from ydb_connection_test where key = 1"), |
| 1038 | + sa.yql("select * from ydb_connection_test where key = 1"), |
| 1039 | + sa.isNotFullScan(), sa.isNotError(), sa.executed(1), sa.hasAst(), sa.hasPlan() |
| 1040 | + ).assertAll(); |
| 1041 | + |
| 1042 | + check.assertNoRows(); |
| 1043 | + } |
| 1044 | + |
| 1045 | + // key read query |
| 1046 | + try (ResultSet rs = st.executeQuery(selectByKey)) { |
972 | 1047 | Assertions.assertFalse(rs.next()); |
973 | 1048 | } |
974 | 1049 |
|
975 | | - try (PreparedStatement ps = connection.prepareStatement(preparedSelect)) { |
976 | | - ps.setLong(1, 1); |
977 | | - try (ResultSet rs = ps.executeQuery()) { |
978 | | - Assertions.assertFalse(rs.next()); |
979 | | - } |
980 | | - ps.setLong(1, 2); |
981 | | - try (ResultSet rs = ps.executeQuery()) { |
982 | | - Assertions.assertFalse(rs.next()); |
983 | | - } |
984 | | - ps.setLong(1, 3); |
985 | | - try (ResultSet rs = ps.executeQuery()) { |
986 | | - Assertions.assertFalse(rs.next()); |
987 | | - } |
| 1050 | + try (ResultSet rs = st.executeQuery("print_JDBC_staTs();")) { |
| 1051 | + TableAssert.ResultSetAssert check = sa.check(rs).assertMetaColumns(); |
| 1052 | + |
| 1053 | + check.nextRow( |
| 1054 | + sa.sql("select * from ydb_connection_test where key = 1"), |
| 1055 | + sa.yql("select * from ydb_connection_test where key = 1"), |
| 1056 | + sa.isNotFullScan(), sa.isNotError(), sa.executed(2), sa.hasAst(), sa.hasPlan() |
| 1057 | + ).assertAll(); |
| 1058 | + |
| 1059 | + check.nextRow( |
| 1060 | + sa.sql("select * from ydb_connection_test"), |
| 1061 | + sa.yql("select * from ydb_connection_test"), |
| 1062 | + sa.isFullScan(), sa.isNotError(), sa.executed(1), sa.hasAst(), sa.hasPlan() |
| 1063 | + ).assertAll(); |
| 1064 | + |
| 1065 | + check.assertNoRows(); |
988 | 1066 | } |
| 1067 | + } |
| 1068 | + } |
| 1069 | + } |
989 | 1070 |
|
990 | | - try (ResultSet rs = st.executeQuery("Print_JDBC_stats();\n")) { |
991 | | - Assertions.assertTrue(rs.next()); |
992 | | - Assertions.assertEquals(preparedSelect, rs.getString("sql")); |
993 | | - Assertions.assertEquals(false, rs.getBoolean("is_fullscan")); |
994 | | - Assertions.assertEquals(3l, rs.getLong("executed")); |
| 1071 | + @Test |
| 1072 | + public void fullScanAnalyzerPreparedStatementTest() throws SQLException { |
| 1073 | + StatsAssert sa = new StatsAssert(); |
995 | 1074 |
|
996 | | - Assertions.assertTrue(rs.next()); |
997 | | - Assertions.assertEquals(selectAll, rs.getString("sql")); |
998 | | - Assertions.assertEquals(true, rs.getBoolean("is_fullscan")); |
999 | | - Assertions.assertEquals(2l, rs.getLong("executed")); |
| 1075 | + String preparedSelectByKey = QUERIES.selectAllByKey("?"); |
| 1076 | + String preparedSelectByColumn = QUERIES.selectAllByColumnValue("c_Text", "?"); |
1000 | 1077 |
|
1001 | | - Assertions.assertTrue(rs.next()); |
1002 | | - Assertions.assertEquals(selectByKey, rs.getString("sql")); |
1003 | | - Assertions.assertEquals(false, rs.getBoolean("is_fullscan")); |
1004 | | - Assertions.assertEquals(1l, rs.getLong("executed")); |
| 1078 | + try (Connection connection = jdbc.createCustomConnection("jdbcFullScanDetector", "true")) { |
| 1079 | + try (Statement st = connection.createStatement()) { |
| 1080 | + try (ResultSet rs = st.executeQuery("print_JDBC_stats();")) { |
| 1081 | + sa.check(rs) |
| 1082 | + .assertMetaColumns() |
| 1083 | + .assertNoRows(); |
| 1084 | + } |
| 1085 | + } |
1005 | 1086 |
|
1006 | | - Assertions.assertFalse(rs.next()); |
| 1087 | + try (PreparedStatement ps = connection.prepareStatement(preparedSelectByKey)) { |
| 1088 | + ps.setInt(1, 1); |
| 1089 | + ps.execute(); |
| 1090 | + |
| 1091 | + ps.setInt(1, 2); |
| 1092 | + ps.execute(); |
| 1093 | + } |
| 1094 | + |
| 1095 | + try (Statement st = connection.createStatement()) { |
| 1096 | + try (ResultSet rs = st.executeQuery("print_JDBC_stats();")) { |
| 1097 | + TableAssert.ResultSetAssert check = sa.check(rs).assertMetaColumns(); |
| 1098 | + |
| 1099 | + check.nextRow( |
| 1100 | + sa.sql("select * from ydb_connection_test where key = ?"), |
| 1101 | + sa.yql("DECLARE $jp1 AS Int32;\nselect * from ydb_connection_test where key = $jp1"), |
| 1102 | + sa.isNotFullScan(), sa.isNotError(), sa.executed(2), sa.hasAst(), sa.hasPlan() |
| 1103 | + ).assertAll(); |
| 1104 | + |
| 1105 | + check.assertNoRows(); |
| 1106 | + } |
| 1107 | + } |
| 1108 | + |
| 1109 | + try (PreparedStatement ps = connection.prepareStatement(preparedSelectByColumn)) { |
| 1110 | + ps.setString(1, "v1"); |
| 1111 | + ps.execute(); |
| 1112 | + |
| 1113 | + ps.setString(1, null); |
| 1114 | + ps.execute(); |
| 1115 | + } |
| 1116 | + |
| 1117 | + try (Statement st = connection.createStatement()) { |
| 1118 | + try (ResultSet rs = st.executeQuery("print_JDBC_stats();")) { |
| 1119 | + TableAssert.ResultSetAssert check = sa.check(rs).assertMetaColumns(); |
| 1120 | + |
| 1121 | + check.nextRow( |
| 1122 | + sa.sql("select * from ydb_connection_test where key = ?"), |
| 1123 | + sa.yql("DECLARE $jp1 AS Int32;\nselect * from ydb_connection_test where key = $jp1"), |
| 1124 | + sa.isNotFullScan(), sa.isNotError(), sa.executed(2), sa.hasAst(), sa.hasPlan() |
| 1125 | + ).assertAll(); |
| 1126 | + |
| 1127 | + check.nextRow( |
| 1128 | + sa.sql("select * from ydb_connection_test where c_Text = ?"), |
| 1129 | + sa.yql("DECLARE $jp1 AS Text;\nselect * from ydb_connection_test where c_Text = $jp1"), |
| 1130 | + sa.isFullScan(), sa.isNotError(), sa.executed(1), sa.hasAst(), sa.hasPlan() |
| 1131 | + ).assertAll(); |
| 1132 | + |
| 1133 | + check.nextRow( |
| 1134 | + sa.sql("select * from ydb_connection_test where c_Text = ?"), |
| 1135 | + sa.yql("DECLARE $jp1 AS Text?;\nselect * from ydb_connection_test where c_Text = $jp1"), |
| 1136 | + sa.isFullScan(), sa.isNotError(), sa.executed(1), sa.hasAst(), sa.hasPlan() |
| 1137 | + ).assertAll(); |
| 1138 | + |
| 1139 | + check.assertNoRows(); |
1007 | 1140 | } |
1008 | 1141 | } |
1009 | 1142 | } |
|
0 commit comments