Merge lp:~mmcm/akiban-server/pg-chartio-queries into lp:~akiban-technologies/akiban-server/trunk

Proposed by Mike McMahon
Status: Merged
Approved by: Mike McMahon
Approved revision: 2634
Merged at revision: 2632
Proposed branch: lp:~mmcm/akiban-server/pg-chartio-queries
Merge into: lp:~akiban-technologies/akiban-server/trunk
Diff against target: 224 lines (+176/-1)
2 files modified
src/main/java/com/akiban/sql/pg/PostgresEmulatedMetaDataStatement.java (+167/-1)
src/main/java/com/akiban/sql/pg/PostgresServerConnection.java (+9/-0)
To merge this branch: bzr merge lp:~mmcm/akiban-server/pg-chartio-queries
Reviewer Review Type Date Requested Status
Akiban Build User Needs Fixing
Nathan Williams Approve
Review via email: mp+159902@code.launchpad.net

Description of the change

Add emulation for the simple queries that chartio sends when setting up a data source.

I am not too confident that this will be enough, but putting it into the cloud should allow testing to see whether it is enough for a demo at least.

Because they also use prepared statements, need to run the unparsed matchers then, too. I don't think this will any significant effects.

To post a comment you must log in.
Revision history for this message
Nathan Williams (nwilliams) wrote :

Delightful.

review: Approve
Revision history for this message
Akiban Build User (build-akiban) wrote :

There were 2 failures during build/test:

* job server-build failed at build number 3978: http://172.16.20.104:8080/job/server-build/3978/

* view must-pass failed: server-build is yellow

review: Needs Fixing

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'src/main/java/com/akiban/sql/pg/PostgresEmulatedMetaDataStatement.java'
2--- src/main/java/com/akiban/sql/pg/PostgresEmulatedMetaDataStatement.java 2013-04-04 21:44:13 +0000
3+++ src/main/java/com/akiban/sql/pg/PostgresEmulatedMetaDataStatement.java 2013-04-19 22:19:23 +0000
4@@ -128,7 +128,10 @@
5 "FROM pg_catalog.pg_trigger t\\s+" +
6 "WHERE t.tgrelid = '(-?\\d+)' AND (?:t.tgconstraint = 0|" + // 2
7 "\\(not tgisconstraint OR NOT EXISTS \\(SELECT 1 FROM pg_catalog.pg_depend d JOIN pg_catalog.pg_constraint c ON \\(d.refclassid = c.tableoid AND d.refobjid = c.oid\\) WHERE d.classid = t.tableoid AND d.objid = t.oid AND d.deptype = 'i' AND c.contype = 'f'\\)\\))(?:\\s+ORDER BY 1)?;?", true),
8- PSQL_DESCRIBE_VIEW("SELECT pg_catalog.pg_get_viewdef\\('(-?\\d+)'::pg_catalog.oid, true\\);?", true);
9+ PSQL_DESCRIBE_VIEW("SELECT pg_catalog.pg_get_viewdef\\('(-?\\d+)'::pg_catalog.oid, true\\);?", true),
10+ CHARTIO_TABLES("SELECT NULL AS TABLE_CAT, n.nspname AS TABLE_SCHEM, c.relname AS TABLE_NAME, CASE n.nspname ~ '^pg_' OR n.nspname = 'information_schema' WHEN true THEN CASE WHEN n.nspname = 'pg_catalog' OR n.nspname = 'information_schema' THEN CASE c.relkind WHEN 'r' THEN 'SYSTEM TABLE' WHEN 'v' THEN 'SYSTEM VIEW' WHEN 'i' THEN 'SYSTEM INDEX' ELSE NULL END WHEN n.nspname = 'pg_toast' THEN CASE c.relkind WHEN 'r' THEN 'SYSTEM TOAST TABLE' WHEN 'i' THEN 'SYSTEM TOAST INDEX' ELSE NULL END ELSE CASE c.relkind WHEN 'r' THEN 'TEMPORARY TABLE' WHEN 'i' THEN 'TEMPORARY INDEX' WHEN 'S' THEN 'TEMPORARY SEQUENCE' WHEN 'v' THEN 'TEMPORARY VIEW' ELSE NULL END END WHEN false THEN CASE c.relkind WHEN 'r' THEN 'TABLE' WHEN 'i' THEN 'INDEX' WHEN 'S' THEN 'SEQUENCE' WHEN 'v' THEN 'VIEW' WHEN 'c' THEN 'TYPE' WHEN 'f' THEN 'FOREIGN TABLE' ELSE NULL END ELSE NULL END AS TABLE_TYPE, d.description AS REMARKS FROM pg_catalog.pg_namespace n, pg_catalog.pg_class c LEFT JOIN pg_catalog.pg_description d ON (c.oid = d.objoid AND d.objsubid = 0) LEFT JOIN pg_catalog.pg_class dc ON (d.classoid=dc.oid AND dc.relname='pg_class') LEFT JOIN pg_catalog.pg_namespace dn ON (dn.oid=dc.relnamespace AND dn.nspname='pg_catalog') WHERE c.relnamespace = n.oid AND (false OR ( c.relkind = 'r' AND n.nspname !~ '^pg_' AND n.nspname <> 'information_schema' ) OR ( c.relkind = 'v' AND n.nspname <> 'pg_catalog' AND n.nspname <> 'information_schema' ) ) ORDER BY TABLE_TYPE,TABLE_SCHEM,TABLE_NAME "),
11+ CHARTIO_KEYS("SELECT NULL AS TABLE_CAT, n.nspname AS TABLE_SCHEM, ct.relname AS TABLE_NAME, a.attname AS COLUMN_NAME, \\(i.keys\\).n AS KEY_SEQ, ci.relname AS PK_NAME FROM pg_catalog.pg_class ct JOIN pg_catalog.pg_attribute a ON \\(ct.oid = a.attrelid\\) JOIN pg_catalog.pg_namespace n ON \\(ct.relnamespace = n.oid\\) JOIN \\(SELECT i.indexrelid, i.indrelid, i.indisprimary, information_schema._pg_expandarray\\(i.indkey\\) AS keys FROM pg_catalog.pg_index i\\) i ON \\(a.attnum = \\(i.keys\\).x AND a.attrelid = i.indrelid\\) JOIN pg_catalog.pg_class ci ON \\(ci.oid = i.indexrelid\\) WHERE true AND ct.relname = E'(.+)' AND i.indisprimary ORDER BY table_name, pk_name, key_seq", true),
12+ CHARTIO_COLUMNS("SELECT \\* FROM \\(SELECT n.nspname,c.relname,a.attname,a.atttypid,a.attnotnull OR \\(t.typtype = 'd' AND t.typnotnull\\) AS attnotnull,a.atttypmod,a.attlen,row_number\\(\\) OVER \\(PARTITION BY a.attrelid ORDER BY a.attnum\\) AS attnum, pg_catalog.pg_get_expr\\(def.adbin, def.adrelid\\) AS adsrc,dsc.description,t.typbasetype,t.typtype FROM pg_catalog.pg_namespace n JOIN pg_catalog.pg_class c ON \\(c.relnamespace = n.oid\\) JOIN pg_catalog.pg_attribute a ON \\(a.attrelid=c.oid\\) JOIN pg_catalog.pg_type t ON \\(a.atttypid = t.oid\\) LEFT JOIN pg_catalog.pg_attrdef def ON \\(a.attrelid=def.adrelid AND a.attnum = def.adnum\\) LEFT JOIN pg_catalog.pg_description dsc ON \\(c.oid=dsc.objoid AND a.attnum = dsc.objsubid\\) LEFT JOIN pg_catalog.pg_class dc ON \\(dc.oid=dsc.classoid AND dc.relname='pg_class'\\) LEFT JOIN pg_catalog.pg_namespace dn ON \\(dc.relnamespace=dn.oid AND dn.nspname='pg_catalog'\\) WHERE a.attnum > 0 AND NOT a.attisdropped AND c.relname LIKE E'(.+)'\\) c WHERE true ORDER BY nspname,c.relname,attnum ", true);
13
14 private String sql;
15 private Pattern pattern;
16@@ -185,6 +188,8 @@
17 new PostgresType(PostgresType.TypeOid.BOOL_TYPE_OID, (short)1, -1, AkType.BOOL, AkBool.INSTANCE.instance(FIELDS_NULLABLE));
18 static final PostgresType INT2_PG_TYPE =
19 new PostgresType(PostgresType.TypeOid.INT2_TYPE_OID, (short)2, -1, AkType.LONG, MNumeric.SMALLINT.instance(FIELDS_NULLABLE));
20+ static final PostgresType INT4_PG_TYPE =
21+ new PostgresType(PostgresType.TypeOid.INT4_TYPE_OID, (short)4, -1, AkType.LONG, MNumeric.INT.instance(FIELDS_NULLABLE));
22 static final PostgresType OID_PG_TYPE =
23 new PostgresType(PostgresType.TypeOid.OID_TYPE_OID, (short)4, -1, AkType.LONG, MNumeric.INT.instance(FIELDS_NULLABLE));
24 static final PostgresType TYPNAME_PG_TYPE =
25@@ -325,6 +330,21 @@
26 names = new String[] { "pg_get_viewdef" };
27 types = new PostgresType[] { VIEWDEF_PG_TYPE };
28 break;
29+ case CHARTIO_TABLES:
30+ ncols = 5;
31+ names = new String[] { "table_cat", "table_schem", "table_name", "table_type", "remarks" };
32+ types = new PostgresType[] { IDENT_PG_TYPE, IDENT_PG_TYPE, IDENT_PG_TYPE, LIST_TYPE_PG_TYPE, CHAR0_PG_TYPE };
33+ break;
34+ case CHARTIO_KEYS:
35+ ncols = 6;
36+ names = new String[] { "table_cat", "table_schem", "table_name", "column_name", "key_seq", "pk_name" };
37+ types = new PostgresType[] { IDENT_PG_TYPE, IDENT_PG_TYPE, IDENT_PG_TYPE, IDENT_PG_TYPE, INT2_PG_TYPE, IDENT_PG_TYPE };
38+ break;
39+ case CHARTIO_COLUMNS:
40+ ncols = 12;
41+ names = new String[] { "nspname", "relname", "attname", "atttypid", "attnotnull", "atttypmod", "attlen", "attnum", "adsrc", "description", "typbasetype", "typtype" };
42+ types = new PostgresType[] { IDENT_PG_TYPE, IDENT_PG_TYPE, IDENT_PG_TYPE, OID_PG_TYPE, CHAR1_PG_TYPE, INT4_PG_TYPE, INT2_PG_TYPE, INT2_PG_TYPE, CHAR0_PG_TYPE, CHAR0_PG_TYPE, OID_PG_TYPE, CHAR1_PG_TYPE };
43+ break;
44 default:
45 return;
46 }
47@@ -415,6 +435,15 @@
48 case PSQL_DESCRIBE_VIEW:
49 nrows = psqlDescribeViewQuery(server, messenger, encoder, maxrows, usePVals);
50 break;
51+ case CHARTIO_TABLES:
52+ nrows = chartioTablesQuery(server, messenger, encoder, maxrows, usePVals);
53+ break;
54+ case CHARTIO_KEYS:
55+ nrows = chartioKeysQuery(server, messenger, encoder, maxrows, usePVals);
56+ break;
57+ case CHARTIO_COLUMNS:
58+ nrows = chartioColumnsQuery(server, messenger, encoder, maxrows, usePVals);
59+ break;
60 }
61 {
62 messenger.beginMessage(PostgresMessages.COMMAND_COMPLETE_TYPE.code());
63@@ -1106,4 +1135,141 @@
64 return 1;
65 }
66
67+ private int chartioTablesQuery(PostgresServerSession server, PostgresMessenger messenger, ServerValueEncoder encoder, int maxrows, boolean usePVals) throws IOException {
68+ int nrows = 0;
69+ List<Columnar> tables = new ArrayList<>();
70+ AkibanInformationSchema ais = server.getAIS();
71+ tables.addAll(ais.getUserTables().values());
72+ tables.addAll(ais.getViews().values());
73+ Iterator<Columnar> iter = tables.iterator();
74+ while (iter.hasNext()) {
75+ TableName name = iter.next().getName();
76+ if (name.getSchemaName().equals(TableName.INFORMATION_SCHEMA) ||
77+ name.getSchemaName().equals(TableName.SECURITY_SCHEMA) ||
78+ !server.isSchemaAccessible(name.getSchemaName()))
79+ iter.remove();
80+ }
81+ Collections.sort(tables, tablesByName);
82+ for (Columnar table : tables) {
83+ TableName name = table.getName();
84+ messenger.beginMessage(PostgresMessages.DATA_ROW_TYPE.code());
85+ messenger.writeShort(5); // 5 columns for this query
86+ writeColumn(messenger, encoder, usePVals,
87+ null, IDENT_PG_TYPE);
88+ writeColumn(messenger, encoder, usePVals,
89+ name.getSchemaName(), IDENT_PG_TYPE);
90+ writeColumn(messenger, encoder, usePVals,
91+ name.getTableName(), IDENT_PG_TYPE);
92+ String type = table.isView() ? "VIEW" : "TABLE";
93+ writeColumn(messenger, encoder, usePVals,
94+ type, LIST_TYPE_PG_TYPE);
95+ writeColumn(messenger, encoder, usePVals,
96+ null, CHAR0_PG_TYPE);
97+ messenger.sendMessage();
98+ nrows++;
99+ if ((maxrows > 0) && (nrows >= maxrows)) {
100+ break;
101+ }
102+ }
103+ return nrows;
104+ }
105+
106+ private int chartioKeysQuery(PostgresServerSession server, PostgresMessenger messenger, ServerValueEncoder encoder, int maxrows, boolean usePVals) throws IOException {
107+ int nrows = 0;
108+ String name = groups.get(1);
109+ AkibanInformationSchema ais = server.getAIS();
110+ List<UserTable> tables = new ArrayList<>();
111+ for (UserTable table : ais.getUserTables().values()) {
112+ TableName tableName = table.getName();
113+ if (server.isSchemaAccessible(tableName.getSchemaName()) &&
114+ tableName.getTableName().equalsIgnoreCase(name)) {
115+ tables.add(table);
116+ }
117+ }
118+ Collections.sort(tables, tablesByName);
119+ rows:
120+ for (UserTable table : tables) {
121+ TableName tableName = table.getName();
122+ TableIndex index = table.getIndex(Index.PRIMARY_KEY_CONSTRAINT);
123+ if (index != null) {
124+ for (IndexColumn column : index.getKeyColumns()) {
125+ messenger.beginMessage(PostgresMessages.DATA_ROW_TYPE.code());
126+ messenger.writeShort(6); // 6 columns for this query
127+ writeColumn(messenger, encoder, usePVals,
128+ null, IDENT_PG_TYPE);
129+ writeColumn(messenger, encoder, usePVals,
130+ tableName.getSchemaName(), IDENT_PG_TYPE);
131+ writeColumn(messenger, encoder, usePVals,
132+ tableName.getTableName(), IDENT_PG_TYPE);
133+ writeColumn(messenger, encoder, usePVals,
134+ column.getColumn().getName(), IDENT_PG_TYPE);
135+ writeColumn(messenger, encoder, usePVals,
136+ (short)(column.getPosition() + 1), INT2_PG_TYPE);
137+ writeColumn(messenger, encoder, usePVals,
138+ index.getIndexName().getName(), IDENT_PG_TYPE);
139+ messenger.sendMessage();
140+ nrows++;
141+ if ((maxrows > 0) && (nrows >= maxrows)) {
142+ break rows;
143+ }
144+ }
145+ }
146+ }
147+ return nrows;
148+ }
149+
150+ private int chartioColumnsQuery(PostgresServerSession server, PostgresMessenger messenger, ServerValueEncoder encoder, int maxrows, boolean usePVals) throws IOException {
151+ int nrows = 0;
152+ String name = groups.get(1);
153+ AkibanInformationSchema ais = server.getAIS();
154+ List<UserTable> tables = new ArrayList<>();
155+ for (UserTable table : ais.getUserTables().values()) {
156+ TableName tableName = table.getName();
157+ if (server.isSchemaAccessible(tableName.getSchemaName()) &&
158+ tableName.getTableName().equalsIgnoreCase(name)) {
159+ tables.add(table);
160+ }
161+ }
162+ Collections.sort(tables, tablesByName);
163+ rows:
164+ for (UserTable table : tables) {
165+ TableName tableName = table.getName();
166+ for (Column column : table.getColumns()) {
167+ PostgresType type = PostgresType.fromAIS(column);
168+ messenger.beginMessage(PostgresMessages.DATA_ROW_TYPE.code());
169+ messenger.writeShort(12); // 12 columns for this query
170+ writeColumn(messenger, encoder, usePVals,
171+ tableName.getSchemaName(), IDENT_PG_TYPE);
172+ writeColumn(messenger, encoder, usePVals,
173+ tableName.getTableName(), IDENT_PG_TYPE);
174+ writeColumn(messenger, encoder, usePVals,
175+ column.getName(), IDENT_PG_TYPE);
176+ writeColumn(messenger, encoder, usePVals,
177+ type.getOid(), OID_PG_TYPE);
178+ writeColumn(messenger, encoder, usePVals,
179+ column.getNullable() ? "t" : "f", CHAR1_PG_TYPE);
180+ writeColumn(messenger, encoder, usePVals,
181+ type.getModifier(), INT4_PG_TYPE);
182+ writeColumn(messenger, encoder, usePVals,
183+ type.getLength(), INT2_PG_TYPE);
184+ writeColumn(messenger, encoder, usePVals,
185+ (short)(column.getPosition() + 1), INT2_PG_TYPE);
186+ writeColumn(messenger, encoder, usePVals,
187+ null, CHAR0_PG_TYPE);
188+ writeColumn(messenger, encoder, usePVals,
189+ null, CHAR0_PG_TYPE);
190+ writeColumn(messenger, encoder, usePVals,
191+ 0, OID_PG_TYPE);
192+ writeColumn(messenger, encoder, usePVals,
193+ "b", CHAR1_PG_TYPE);
194+ messenger.sendMessage();
195+ nrows++;
196+ if ((maxrows > 0) && (nrows >= maxrows)) {
197+ break rows;
198+ }
199+ }
200+ }
201+ return nrows;
202+ }
203+
204 }
205
206=== modified file 'src/main/java/com/akiban/sql/pg/PostgresServerConnection.java'
207--- src/main/java/com/akiban/sql/pg/PostgresServerConnection.java 2013-04-04 21:44:13 +0000
208+++ src/main/java/com/akiban/sql/pg/PostgresServerConnection.java 2013-04-19 22:19:23 +0000
209@@ -751,6 +751,15 @@
210 if (statementCache != null)
211 pstmt = statementCache.get(sql);
212 if (pstmt == null) {
213+ for (PostgresStatementParser parser : unparsedGenerators) {
214+ pstmt = parser.parse(this, sql, null);
215+ if (pstmt != null) {
216+ pstmt.setAISGeneration(ais.getGeneration());
217+ break;
218+ }
219+ }
220+ }
221+ if (pstmt == null) {
222 StatementNode stmt;
223 List<ParameterNode> params;
224 try {

Subscribers

People subscribed via source and target branches