| 1 | = RFC 21: OGR SQL type cast and field name alias = |
| 2 | |
| 3 | Author: Tamas Szekeres[[BR]] |
| 4 | Contact: szekerest@gmail.com[[BR]] |
| 5 | Status: Proposed |
| 6 | |
| 7 | == Summary == |
| 8 | |
| 9 | This proposal provides support for changing the column name and the column type |
| 10 | in the OGR SQL select list. |
| 11 | |
| 12 | The main motivation of this change is to provide better control when transferring |
| 13 | the OGR_STYLE special field from each data source to the other. |
| 14 | For example we can use ogr2ogr for this purpose using the fillowing command line: |
| 15 | |
| 16 | {{{ |
| 17 | ogr2ogr -f "ESRI Shapefile" -sql "select *, OGR_STYLE from rivers" rivers.shp rivers.tab |
| 18 | }}} |
| 19 | |
| 20 | The shape driver will truncate the OGR_STYLE field to 80 characters by default in length |
| 21 | that may not be enough to hold the actual value. |
| 22 | So as to fix this issue we might want to specify the desired length in the select list, like: |
| 23 | |
| 24 | {{{ |
| 25 | ogr2ogr -f "ESRI Shapefile" -sql "select *, CAST(OGR_STYLE AS character(255)) from rivers" rivers.shp rivers.tab |
| 26 | }}} |
| 27 | |
| 28 | In some cases it would also be useful to change the name of the field in the target data source: |
| 29 | |
| 30 | {{{ |
| 31 | ogr2ogr -f "ESRI Shapefile" -sql "select *, CAST(OGR_STYLE AS character(255)) AS 'STYLE' from rivers" rivers.shp rivers.tab |
| 32 | }}} |
| 33 | |
| 34 | == Main concepts == |
| 35 | |
| 36 | To support these new features we will extend the current OGR SQL syntax. The proposed addition will keep the syntax |
| 37 | conform to the SQL92 specification: |
| 38 | |
| 39 | {{{ |
| 40 | SELECT <field-list> FROM <table_def> |
| 41 | [LEFT JOIN <table_def> |
| 42 | ON [<table_ref>.]<key_field> = [<table_ref>.].<key_field>]* |
| 43 | [WHERE <where-expr>] |
| 44 | [ORDER BY <sort specification list>] |
| 45 | |
| 46 | <field-list> ::= <column-spec> [ { , <column-spec> }... ] |
| 47 | |
| 48 | <column-spec> ::= <field-spec> [ <as clause> ] |
| 49 | | CAST ( <field-spec> AS <data type> ) [ <as clause> ] |
| 50 | |
| 51 | <field-spec> ::= [DISTINCT] <field_ref> |
| 52 | | <field_func> ( [DISTINCT] <field-ref> ) |
| 53 | | Count(*) |
| 54 | |
| 55 | <as clause> ::= [ AS ] <column_name> |
| 56 | |
| 57 | <data type> ::= character [ ( field_length ) ] |
| 58 | | float [ ( field_length ) ] |
| 59 | | integer [ ( field_length ) ] |
| 60 | |
| 61 | <field-func> ::= AVG | MAX | MIN | SUM | COUNT |
| 62 | |
| 63 | <field_ref> ::= [<table_ref>.]field_name |
| 64 | |
| 65 | <sort specification list> ::= |
| 66 | <sort specification> [ { <comma> <sort specification> }... ] |
| 67 | |
| 68 | <sort specification> ::= <sort key> [ <ordering specification> ] |
| 69 | |
| 70 | <sort key> ::= <field_ref> |
| 71 | |
| 72 | <ordering specification> ::= ASC | DESC |
| 73 | |
| 74 | <table_def> ::= ['<datasource name>'.]table_name [table_alias] |
| 75 | |
| 76 | <table_ref> ::= table_name | table_alias |
| 77 | }}} |
| 78 | |
| 79 | == Implementation == |
| 80 | |
| 81 | To implement the addition the following changes should be made in the OGR codebase: |
| 82 | |
| 83 | 1. In swq.h I'll have to add to 3 fields to swq_col_def to hold the field_alias the target_type |
| 84 | and the field_length. |
| 85 | |
| 86 | 2. In swq.c I'll have to change swq_select_preparse to take care of the field alias and the |
| 87 | CAST specification. |
| 88 | |
| 89 | 3. A new function (swq_parse_typename) will be added to parse the supported typenames. |
| 90 | |
| 91 | 4. In ogr_gensql.cpp the .ctor of OGRGenSQLResultsLayer will be changed to set up the field name and the |
| 92 | field length to the target data source |
| 93 | |
| 94 | 5. In ogr_gensql.cpp TranslateFeature will be modified to take care of the type change if specified. |
| 95 | |
| 96 | == Backward Compatibility == |
| 97 | |
| 98 | The backward compatibility for the current SQL syntax will be retained. This addition |
| 99 | doesn't requre changes in the OGR C and SWIG API. |
| 100 | |
| 101 | == Documentation == |
| 102 | |
| 103 | The OGR SQL document will be updated to reflect the new features. |
| 104 | I'll take care of updating the OGR Style Documentation with the support of transferring the |
| 105 | styles between the data sources. |
| 106 | |
| 107 | == Implementation Staffing == |
| 108 | |
| 109 | Tamas Szekeres will implement the RFC in the development version. |
| 110 | |
| 111 | == References == |
| 112 | |
| 113 | * Tracking bug for this feature (containing all of the proposed code changes): #2171 |
| 114 | |
| 115 | == Voting History == |
| 116 | |
| 117 | None |