Type System Reference
Type system and schema definitions.
DataType
DataType
Bases: Enum
SQL data types supported by SQLStream.
Source code in sqlstream/core/types.py
is_numeric
is_temporal
is_comparable
Check if this type can be compared with another type.
Source code in sqlstream/core/types.py
coerce_to
Determine the result type when coercing this type to another.
Used for type promotion in expressions like INTEGER + FLOAT.
Source code in sqlstream/core/types.py
Schema
Schema
Schema definition for a table or query result.
Holds column names and their corresponding data types.
Source code in sqlstream/core/types.py
461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 | |
__init__
Initialize schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
columns
|
dict[str, DataType]
|
Dictionary mapping column names to data types |
required |
__getitem__
__contains__
__len__
get_column_names
get_column_type
validate_column
Validate that a column exists in the schema.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
column
|
str
|
Column name to validate |
required |
Raises:
| Type | Description |
|---|---|
ValueError
|
If column doesn't exist |
Source code in sqlstream/core/types.py
from_row
staticmethod
Infer schema from a single row.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
row
|
dict[str, Any]
|
Dictionary representing a row |
required |
Returns:
| Type | Description |
|---|---|
Schema
|
Inferred Schema |
Source code in sqlstream/core/types.py
from_rows
staticmethod
Infer schema from multiple rows.
This provides more accurate type inference by looking at multiple values.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
rows
|
list[dict[str, Any]]
|
List of dictionaries representing rows |
required |
Returns:
| Type | Description |
|---|---|
Schema
|
Inferred Schema |
Source code in sqlstream/core/types.py
merge
Merge two schemas, coercing types where needed.
This is useful for operations like UNION or JOIN where schemas need to be compatible.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
other
|
Schema
|
Another schema to merge with |
required |
Returns:
| Type | Description |
|---|---|
Schema
|
Merged schema with coerced types |
Source code in sqlstream/core/types.py
infer_common_type
infer_common_type
Infer a common type from a list of values.
This is useful for schema inference when reading data files.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
values
|
list[Any]
|
List of values to infer type from |
required |
Returns:
| Type | Description |
|---|---|
DataType
|
Common DataType that can represent all values |
Examples:
>>> infer_common_type([1, 2, 3])
DataType.INTEGER
>>> infer_common_type([1, 2.5, 3])
DataType.FLOAT
>>> infer_common_type([1, "hello", 3])
DataType.STRING
Source code in sqlstream/core/types.py
infer_type
infer_type
Infer the data type from a Python value.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
Any
|
Python value to infer type from |
required |
Returns:
| Type | Description |
|---|---|
DataType
|
Inferred DataType |
Examples:
>>> infer_type(42)
DataType.INTEGER
>>> infer_type(3.14)
DataType.FLOAT
>>> infer_type("hello")
DataType.STRING
>>> infer_type(None)
DataType.NULL
>>> infer_type(Decimal("19.99"))
DataType.DECIMAL
Source code in sqlstream/core/types.py
240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 | |
infer_type_from_string
infer_type_from_string
Parse a string value and return the typed Python value.
This is used by readers to convert string data into proper Python types.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
String value to parse |
required |
Returns:
| Type | Description |
|---|---|
Any
|
Typed Python value (int, float, Decimal, datetime, bool, str, etc.) |
Examples:
>>> infer_type_from_string("42")
42
>>> infer_type_from_string("3.14159265358979")
Decimal("3.14159265358979")
>>> infer_type_from_string("2024-01-15 10:30:00")
datetime(2024, 1, 15, 10, 30, 0)
Source code in sqlstream/core/types.py
is_json_string
is_json_string
Check if a string contains valid JSON (object or array).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
String to check |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if valid JSON object/array, False otherwise |
Source code in sqlstream/core/types.py
parse_date
parse_date
Try to parse date from string using multiple formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
String to parse |
required |
Returns:
| Type | Description |
|---|---|
date | None
|
date object if successful, None otherwise |
Source code in sqlstream/core/types.py
parse_datetime
parse_datetime
Try to parse datetime from string using multiple formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
String to parse |
required |
Returns:
| Type | Description |
|---|---|
datetime | None
|
datetime object if successful, None otherwise |
Source code in sqlstream/core/types.py
parse_time
parse_time
Try to parse time from string using multiple formats.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
value
|
str
|
String to parse |
required |
Returns:
| Type | Description |
|---|---|
time | None
|
time object if successful, None otherwise |