def schema_for_column(c): '''Returns the Schema object for the given Column.''' data_type = c.data_type.lower() column_type = c.column_type.lower() inclusion = 'available' # We want to automatically include all primary key columns if c.column_key.lower() == 'pri': inclusion = 'automatic' result = Schema(inclusion=inclusion) if data_type == 'bit' or column_type.startswith('tinyint(1)'): result.type = ['null', 'boolean'] elif data_type in BYTES_FOR_INTEGER_TYPE: result.type = ['null', 'integer'] bits = BYTES_FOR_INTEGER_TYPE[data_type] * 8 if 'unsigned' in c.column_type: result.minimum = 0 result.maximum = 2**bits - 1 else: result.minimum = 0 - 2**(bits - 1) result.maximum = 2**(bits - 1) - 1 elif data_type in FLOAT_TYPES: result.type = ['null', 'number'] elif data_type == 'decimal': result.type = ['null', 'number'] result.multipleOf = 10**(0 - c.numeric_scale) return result elif data_type in STRING_TYPES: result.type = ['null', 'string'] result.maxLength = c.character_maximum_length elif data_type in DATETIME_TYPES: result.type = ['null', 'string'] result.format = 'date-time' elif data_type in BINARY_TYPE: result.type = ['null', 'string'] result.maxLength = c.character_maximum_length * 2 else: result = Schema( None, inclusion='unsupported', description='Unsupported column type {}'.format(column_type)) return result
def schema_for_column(c, pks_for_table): # Return Schema(None) to avoid calling lower() on a column with no datatype if c.data_type is None: LOGGER.info('Skipping column %s since it had no datatype', c.column_name) return Schema(None) data_type = c.data_type.lower() result = Schema() # Scale of None indicates default of 6 digits numeric_scale = c.numeric_scale if data_type == 'number' and numeric_scale is not None and numeric_scale <= 0: result.type = nullable_column(c.column_name, 'integer', pks_for_table) return result elif data_type == 'number': # NB: Due to scale and precision variations in Oracle version, and # among numeric types, we're using a custom `singer.decimal` string # formatter for this, with no opinion on scale/precision. result.type = nullable_column(c.column_name, 'string', pks_for_table) result.format = 'singer.decimal' return result elif data_type == 'date' or data_type.startswith("timestamp"): result.type = nullable_column(c.column_name, 'string', pks_for_table) result.format = 'date-time' return result elif data_type in FLOAT_TYPES: result.type = nullable_column(c.column_name, 'number', pks_for_table) return result elif data_type in STRING_TYPES: character_used = c.character_used result.type = nullable_column(c.column_name, 'string', pks_for_table) if character_used == 'C': result.maxLength = c.char_length return result #these column types are insane. they are NOT actually ieee754 floats #instead they are represented as decimals, but despite this #it appears we can say nothing about their max or min #"float", "double_precision", "real" elif data_type in ['float', 'double_precision']: result.type = nullable_column(c.column_name, 'string', pks_for_table) result.format = 'singer.decimal' return result return Schema(None)
def schema_for_column(c): """Returns the Schema object for the given Column.""" data_type = c.data_type.lower() inclusion = "available" if c.is_primary_key == 1: inclusion = "automatic" result = Schema(inclusion=inclusion) if data_type == "bit": result.type = ["null", "boolean"] #In SQL Server tinyint is unsigned. elif data_type in BYTES_FOR_UNSIGNED_INTEGER_TYPE: result.type = ["null", "integer"] bits = BYTES_FOR_UNSIGNED_INTEGER_TYPE[data_type] * 8 result.minimum = 0 result.maximum = 2**bits - 1 elif data_type in BYTES_FOR_INTEGER_TYPE: result.type = ["null", "integer"] bits = BYTES_FOR_INTEGER_TYPE[data_type] * 8 result.minimum = 0 - 2**(bits - 1) result.maximum = 2**(bits - 1) - 1 elif data_type in FLOAT_TYPES: result.type = ["null", "number"] elif data_type in PRECISE_NUMERIC_TYPES: result.type = ["null", "number"] result.multipleOf = 10**(0 - c.numeric_scale) return result elif data_type in STRING_TYPES: result.type = ["null", "string"] if c.character_maximum_length is not None and c.character_maximum_length > 0: result.maxLength = c.character_maximum_length elif data_type in DATETIME_TYPES: result.type = ["null", "string"] result.format = "date-time" elif data_type in VARIANT_TYPES: result.type = ["null", "object"] else: result = Schema( None, inclusion="unsupported", description="Unsupported column type", ) return result
def schema_for_column(c): """Returns the Schema object for the given Column.""" data_type = c.data_type.lower() column_type = c.column_type.lower() inclusion = "available" # We want to automatically include all primary key columns if c.column_key.lower() == "pri": inclusion = "automatic" result = Schema(inclusion=inclusion) if data_type == "bit" or column_type.startswith("tinyint(1)"): result.type = ["null", "boolean"] elif data_type in BYTES_FOR_INTEGER_TYPE: result.type = ["null", "integer"] bits = BYTES_FOR_INTEGER_TYPE[data_type] * 8 if "unsigned" in c.column_type: result.minimum = 0 result.maximum = 2 ** bits - 1 else: result.minimum = 0 - 2 ** (bits - 1) result.maximum = 2 ** (bits - 1) - 1 elif data_type in FLOAT_TYPES: result.type = ["null", "number"] result.multipleOf = 10 ** (0 - 6) elif data_type == "decimal": result.type = ["null", "number"] result.multipleOf = 10 ** (0 - c.numeric_scale) return result elif data_type in STRING_TYPES: result.type = ["null", "string"] result.maxLength = c.character_maximum_length elif data_type in DATETIME_TYPES: result.type = ["null", "string"] result.format = "date-time" elif data_type in VARIANT_TYPES: result.type = ["null", "object"] else: result = Schema( None, inclusion="unsupported", description="Unsupported column type {}".format(column_type), ) return result
def schema_for_column(c): '''Returns the Schema object for the given Column.''' data_type = c.data_type.lower() inclusion = 'available' result = Schema(inclusion=inclusion) if data_type == 'boolean': result.type = ['null', 'boolean'] elif data_type in INTEGER_TYPES: result.type = ['null', 'number'] elif data_type in FLOAT_TYPES: result.type = ['null', 'number'] elif data_type in NUMBER_TYPES: result.type = ['null', 'number'] elif data_type in STRING_TYPES: result.type = ['null', 'string'] result.maxLength = c.character_maximum_length elif data_type in DATETIME_TYPES: result.type = ['null', 'string'] result.format = 'date-time' elif data_type == 'time': result.type = ['null', 'string'] result.format = 'time' elif data_type in BINARY_TYPES: result.type = ['null', 'string'] result.format = 'binary' elif data_type in OBJECT_TYPES: result.type = ['null', 'object', 'array'] else: result = Schema( None, inclusion='unsupported', description='Unsupported data type {}'.format(data_type)) return result
def schema_for_column(c): data_type = c.sql_data_type.lower() result = Schema() if data_type in INTEGER_TYPES: result.type = nullable_column('integer', c.is_primary_key) result.minimum = -1 * (2**(c.numeric_precision - 1)) result.maximum = 2**(c.numeric_precision - 1) - 1 return result elif data_type == 'bit' and c.character_maximum_length == 1: result.type = nullable_column('boolean', c.is_primary_key) return result elif data_type == 'boolean': result.type = nullable_column('boolean', c.is_primary_key) return result elif data_type == 'uuid': result.type = nullable_column('string', c.is_primary_key) return result elif data_type == 'hstore': result.type = nullable_column('string', c.is_primary_key) return result elif data_type == 'citext': result.type = nullable_column('string', c.is_primary_key) return result elif data_type in JSON_TYPES: result.type = nullable_column('string', c.is_primary_key) return result elif data_type == 'numeric': result.type = nullable_column('number', c.is_primary_key) if c.numeric_scale is None or c.numeric_scale > MAX_SCALE: LOGGER.warning( 'capping decimal scale to 38. THIS MAY CAUSE TRUNCATION') scale = MAX_SCALE else: scale = c.numeric_scale if c.numeric_precision is None or c.numeric_precision > MAX_PRECISION: LOGGER.warning( 'capping decimal precision to 100. THIS MAY CAUSE TRUNCATION') precision = MAX_PRECISION else: precision = c.numeric_precision result.exclusiveMaximum = True result.maximum = 10**(precision - scale) result.multipleOf = 10**(0 - scale) result.exclusiveMinimum = True result.minimum = -10**(precision - scale) return result elif data_type in {'time without time zone', 'time with time zone'}: #times are treated as ordinary strings as they can not possible match RFC3339 result.type = nullable_column('string', c.is_primary_key) return result elif data_type in ('date', 'timestamp without time zone', 'timestamp with time zone'): result.type = nullable_column('string', c.is_primary_key) result.format = 'date-time' return result elif data_type in FLOAT_TYPES: result.type = nullable_column('number', c.is_primary_key) return result elif data_type == 'text': result.type = nullable_column('string', c.is_primary_key) return result elif data_type == 'character varying': result.type = nullable_column('string', c.is_primary_key) result.maxLength = c.character_maximum_length return result elif data_type == 'character': result.type = nullable_column('string', c.is_primary_key) result.maxLength = c.character_maximum_length return result return Schema(None)
def schema_for_column(c, pks_for_table): data_type = c.data_type.lower() result = Schema() numeric_scale = c.numeric_scale or DEFAULT_NUMERIC_SCALE numeric_precision = c.numeric_precision or DEFAULT_NUMERIC_PRECISION if data_type == 'number' and numeric_scale <= 0: result.type = nullable_column(c.column_name, 'integer', pks_for_table) result.minimum = -1 * (10**numeric_precision - 1) result.maximum = (10**numeric_precision - 1) if numeric_scale < 0: result.multipleOf = -10 * numeric_scale return result elif data_type == 'number': result.type = nullable_column(c.column_name, 'number', pks_for_table) result.exclusiveMaximum = True result.maximum = 10**(numeric_precision - numeric_scale) result.multipleOf = 10**(0 - numeric_scale) result.exclusiveMinimum = True result.minimum = -10**(numeric_precision - numeric_scale) return result elif data_type == 'date' or data_type.startswith("timestamp"): result.type = nullable_column(c.column_name, 'string', pks_for_table) result.format = 'date-time' return result elif data_type in FLOAT_TYPES: result.type = nullable_column(c.column_name, 'number', pks_for_table) return result elif data_type in STRING_TYPES: character_used = c.character_used result.type = nullable_column(c.column_name, 'string', pks_for_table) if character_used == 'C': result.maxLength = c.char_length return result #these column types are insane. they are NOT actually ieee754 floats #instead they are represented as decimals, but despite this #it appears we can say nothing about their max or min #"real" elif data_type == 'float' and c.numeric_precision == 63: result.type = nullable_column(c.column_name, 'number', pks_for_table) result.multipleOf = 10**-18 return result #"float", "double_precision", elif data_type in ['float', 'double_precision']: result.type = nullable_column(c.column_name, 'number', pks_for_table) result.multipleOf = 10**-38 return result return Schema(None)