def _literal(t, expr): dtype = expr.type() op = expr.op() value = op.value if isinstance(dtype, dt.Interval): return sa.text("INTERVAL '{} {}'".format(value, dtype.resolution)) elif isinstance(dtype, dt.Set): return list(map(sa.literal, value)) # geo spatial data type elif isinstance(expr, ir.GeoSpatialScalar): # inline_metadata ex: 'SRID=4326;POINT( ... )' return sa.text(geo.translate_literal(expr, inline_metadata=True)) elif isinstance(value, np.ndarray): return sa.literal(value.tolist()) else: return sa.literal(value)
def _literal(_, expr): dtype = expr.type() op = expr.op() value = op.value if isinstance(dtype, dt.Interval): return sa.text(f"INTERVAL '{value} {dtype.resolution}'") elif isinstance(dtype, dt.Set): return list(map(sa.literal, value)) # geo spatial data type elif isinstance(expr, ir.GeoSpatialScalar): # inline_metadata ex: 'SRID=4326;POINT( ... )' return sa.literal_column( geo.translate_literal(expr, inline_metadata=True)) elif isinstance(value, tuple): return sa.literal(value, type_=to_sqla_type(dtype)) else: return sa.literal(value)
def literal(translator, expr: ibis.expr.operations.Literal) -> str: """Create a translator for literal operations. Parameters ---------- translator : ibis.omniscidb.compiler.OmniSciDBExprTranslator expr : ibis.expr.operations.Literal Returns ------- translated : str Raises ------ Exception if a TimestampValue expr is given and its value is a datetime and the format is invalid. NotImplementedError if the given literal expression is not recognized. """ op = expr.op() value = op.value # geo spatial data type if isinstance(expr, ir.GeoSpatialScalar): return geo.translate_literal(expr) # primitive data type elif isinstance(expr, ir.BooleanValue): return '1' if value else '0' elif isinstance(expr, ir.StringValue): return "'{0!s}'".format(value.replace("'", "\\'")) elif isinstance(expr, ir.NumericValue): return repr(value) elif isinstance(expr, ir.SetScalar): return _set_literal_format(translator, expr) elif isinstance(expr, ir.IntervalValue): return _interval_format(translator, expr) elif isinstance(expr, ir.TimestampValue): if isinstance(value, datetime): if value.microsecond != 0: msg = 'Unsupported subsecond accuracy {}' warnings.warn(msg.format(value)) value = value.strftime('%Y-%m-%d %H:%M:%S') elif isinstance(value, str): # check if the datetime format is a valid format ( # '%Y-%m-%d %H:%M:%S' or '%Y-%m-%d'). if format is '%Y-%m-%d' it # is converted to '%Y-%m-%d 00:00:00' msg = ("Literal datetime string should use '%Y-%m-%d %H:%M:%S' " "format. When '%Y-%m-%d' format is used, datetime will be " "converted automatically to '%Y-%m-%d 00:00:00'") try: dt_value = datetime.strptime(value, '%Y-%m-%d %H:%M:%S') except ValueError: try: dt_value = datetime.strptime(value, '%Y-%m-%d') warnings.warn(msg) except ValueError: raise Exception(msg) value = dt_value.strftime('%Y-%m-%d %H:%M:%S') return "'{0!s}'".format(value) elif isinstance(expr, ir.DateValue): if isinstance(value, date): value = value.strftime('%Y-%m-%d') return "CAST('{0!s}' as date)".format(value) # array data type elif isinstance(expr, ir.ArrayValue): return str(list(value)) else: raise NotImplementedError(type(expr))