@quereus/quereus 0.7.2 → 0.7.4
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +3 -3
- package/dist/src/common/constants.js.map +0 -1
- package/dist/src/common/datatype.js.map +0 -1
- package/dist/src/common/errors.js.map +0 -1
- package/dist/src/common/json-types.js.map +0 -1
- package/dist/src/common/logger.js.map +0 -1
- package/dist/src/common/type-inference.js.map +0 -1
- package/dist/src/common/types.js.map +0 -1
- package/dist/src/core/database-options.js.map +0 -1
- package/dist/src/core/database.js.map +0 -1
- package/dist/src/core/param.js.map +0 -1
- package/dist/src/core/statement.js.map +0 -1
- package/dist/src/func/builtins/aggregate.js.map +0 -1
- package/dist/src/func/builtins/builtin-window-functions.js.map +0 -1
- package/dist/src/func/builtins/conversion.js.map +0 -1
- package/dist/src/func/builtins/datetime.js.map +0 -1
- package/dist/src/func/builtins/explain.js.map +0 -1
- package/dist/src/func/builtins/generation.js.map +0 -1
- package/dist/src/func/builtins/index.js.map +0 -1
- package/dist/src/func/builtins/json-helpers.js.map +0 -1
- package/dist/src/func/builtins/json-tvf.js.map +0 -1
- package/dist/src/func/builtins/json.js.map +0 -1
- package/dist/src/func/builtins/scalar.js.map +0 -1
- package/dist/src/func/builtins/schema.js.map +0 -1
- package/dist/src/func/builtins/string.js.map +0 -1
- package/dist/src/func/builtins/timespan.js.map +0 -1
- package/dist/src/func/context.js.map +0 -1
- package/dist/src/func/registration.js.map +0 -1
- package/dist/src/index.js.map +0 -1
- package/dist/src/parser/ast.js.map +0 -1
- package/dist/src/parser/index.js.map +0 -1
- package/dist/src/parser/lexer.js.map +0 -1
- package/dist/src/parser/parser.js.map +0 -1
- package/dist/src/parser/utils.js.map +0 -1
- package/dist/src/parser/visitor.js.map +0 -1
- package/dist/src/planner/analysis/binding-collector.js.map +0 -1
- package/dist/src/planner/analysis/const-evaluator.js.map +0 -1
- package/dist/src/planner/analysis/const-pass.js.map +0 -1
- package/dist/src/planner/analysis/constraint-extractor.js.map +0 -1
- package/dist/src/planner/analysis/predicate-normalizer.js.map +0 -1
- package/dist/src/planner/building/alter-table.js.map +0 -1
- package/dist/src/planner/building/block.js.map +0 -1
- package/dist/src/planner/building/constraint-builder.js.map +0 -1
- package/dist/src/planner/building/create-assertion.js.map +0 -1
- package/dist/src/planner/building/create-view.js.map +0 -1
- package/dist/src/planner/building/ddl.js.map +0 -1
- package/dist/src/planner/building/declare-schema.js.map +0 -1
- package/dist/src/planner/building/delete.js.map +0 -1
- package/dist/src/planner/building/drop-assertion.js.map +0 -1
- package/dist/src/planner/building/drop-table.js.map +0 -1
- package/dist/src/planner/building/drop-view.js.map +0 -1
- package/dist/src/planner/building/expression.js.map +0 -1
- package/dist/src/planner/building/function-call.js.map +0 -1
- package/dist/src/planner/building/insert.js.map +0 -1
- package/dist/src/planner/building/pragma.js.map +0 -1
- package/dist/src/planner/building/schema-resolution.js.map +0 -1
- package/dist/src/planner/building/select-aggregates.js.map +0 -1
- package/dist/src/planner/building/select-compound.js.map +0 -1
- package/dist/src/planner/building/select-context.js.map +0 -1
- package/dist/src/planner/building/select-modifiers.js.map +0 -1
- package/dist/src/planner/building/select-projections.js.map +0 -1
- package/dist/src/planner/building/select-window.js.map +0 -1
- package/dist/src/planner/building/select.js.map +0 -1
- package/dist/src/planner/building/table-function.js.map +0 -1
- package/dist/src/planner/building/table.js.map +0 -1
- package/dist/src/planner/building/transaction.js.map +0 -1
- package/dist/src/planner/building/update.js.map +0 -1
- package/dist/src/planner/building/with.js.map +0 -1
- package/dist/src/planner/cache/correlation-detector.js.map +0 -1
- package/dist/src/planner/cache/materialization-advisory.js.map +0 -1
- package/dist/src/planner/cache/reference-graph.js.map +0 -1
- package/dist/src/planner/cost/index.js.map +0 -1
- package/dist/src/planner/debug/logger-utils.js.map +0 -1
- package/dist/src/planner/debug.js.map +0 -1
- package/dist/src/planner/framework/characteristics.js.map +0 -1
- package/dist/src/planner/framework/context.js.map +0 -1
- package/dist/src/planner/framework/pass.js.map +0 -1
- package/dist/src/planner/framework/physical-utils.js.map +0 -1
- package/dist/src/planner/framework/registry.js.map +0 -1
- package/dist/src/planner/framework/trace.js.map +0 -1
- package/dist/src/planner/nodes/add-constraint-node.js.map +0 -1
- package/dist/src/planner/nodes/aggregate-function.js.map +0 -1
- package/dist/src/planner/nodes/aggregate-node.js.map +0 -1
- package/dist/src/planner/nodes/array-index-node.js.map +0 -1
- package/dist/src/planner/nodes/block.js.map +0 -1
- package/dist/src/planner/nodes/cache-node.js.map +0 -1
- package/dist/src/planner/nodes/constraint-check-node.js.map +0 -1
- package/dist/src/planner/nodes/create-assertion-node.js.map +0 -1
- package/dist/src/planner/nodes/create-index-node.js.map +0 -1
- package/dist/src/planner/nodes/create-table-node.js.map +0 -1
- package/dist/src/planner/nodes/create-view-node.js.map +0 -1
- package/dist/src/planner/nodes/cte-node.js.map +0 -1
- package/dist/src/planner/nodes/cte-reference-node.js.map +0 -1
- package/dist/src/planner/nodes/declarative-schema.js.map +0 -1
- package/dist/src/planner/nodes/delete-node.js.map +0 -1
- package/dist/src/planner/nodes/distinct-node.js.map +0 -1
- package/dist/src/planner/nodes/dml-executor-node.js.map +0 -1
- package/dist/src/planner/nodes/drop-assertion-node.js.map +0 -1
- package/dist/src/planner/nodes/drop-table-node.js.map +0 -1
- package/dist/src/planner/nodes/drop-view-node.js.map +0 -1
- package/dist/src/planner/nodes/filter.js.map +0 -1
- package/dist/src/planner/nodes/function.js.map +0 -1
- package/dist/src/planner/nodes/insert-node.js.map +0 -1
- package/dist/src/planner/nodes/internal-recursive-cte-ref-node.js.map +0 -1
- package/dist/src/planner/nodes/join-node.js.map +0 -1
- package/dist/src/planner/nodes/limit-offset.js.map +0 -1
- package/dist/src/planner/nodes/plan-node-type.js.map +0 -1
- package/dist/src/planner/nodes/plan-node.js.map +0 -1
- package/dist/src/planner/nodes/pragma.js.map +0 -1
- package/dist/src/planner/nodes/project-node.js.map +0 -1
- package/dist/src/planner/nodes/recursive-cte-node.js.map +0 -1
- package/dist/src/planner/nodes/reference.js.map +0 -1
- package/dist/src/planner/nodes/remote-query-node.js.map +0 -1
- package/dist/src/planner/nodes/retrieve-node.js.map +0 -1
- package/dist/src/planner/nodes/returning-node.js.map +0 -1
- package/dist/src/planner/nodes/scalar.js.map +0 -1
- package/dist/src/planner/nodes/sequencing-node.js.map +0 -1
- package/dist/src/planner/nodes/set-operation-node.js.map +0 -1
- package/dist/src/planner/nodes/single-row.js.map +0 -1
- package/dist/src/planner/nodes/sink-node.js.map +0 -1
- package/dist/src/planner/nodes/sort.js.map +0 -1
- package/dist/src/planner/nodes/stream-aggregate.js.map +0 -1
- package/dist/src/planner/nodes/subquery.js.map +0 -1
- package/dist/src/planner/nodes/table-access-nodes.js.map +0 -1
- package/dist/src/planner/nodes/table-function-call.js.map +0 -1
- package/dist/src/planner/nodes/transaction-node.js.map +0 -1
- package/dist/src/planner/nodes/update-node.js.map +0 -1
- package/dist/src/planner/nodes/values-node.js.map +0 -1
- package/dist/src/planner/nodes/view-reference-node.js.map +0 -1
- package/dist/src/planner/nodes/window-function.js.map +0 -1
- package/dist/src/planner/nodes/window-node.js.map +0 -1
- package/dist/src/planner/optimizer-tuning.js.map +0 -1
- package/dist/src/planner/optimizer.js.map +0 -1
- package/dist/src/planner/planning-context.js.map +0 -1
- package/dist/src/planner/resolve.js.map +0 -1
- package/dist/src/planner/rules/access/rule-select-access-path.js.map +0 -1
- package/dist/src/planner/rules/aggregate/rule-aggregate-streaming.js.map +0 -1
- package/dist/src/planner/rules/cache/rule-cte-optimization.js.map +0 -1
- package/dist/src/planner/rules/cache/rule-materialization-advisory.js.map +0 -1
- package/dist/src/planner/rules/cache/rule-mutating-subquery-cache.js.map +0 -1
- package/dist/src/planner/rules/join/rule-join-greedy-commute.js.map +0 -1
- package/dist/src/planner/rules/join/rule-join-key-inference.js.map +0 -1
- package/dist/src/planner/rules/join/rule-quickpick-enumeration.js.map +0 -1
- package/dist/src/planner/rules/predicate/rule-predicate-pushdown.js.map +0 -1
- package/dist/src/planner/rules/retrieve/rule-grow-retrieve.js.map +0 -1
- package/dist/src/planner/scopes/aliased.js.map +0 -1
- package/dist/src/planner/scopes/base.js.map +0 -1
- package/dist/src/planner/scopes/empty.js.map +0 -1
- package/dist/src/planner/scopes/global.js.map +0 -1
- package/dist/src/planner/scopes/multi.js.map +0 -1
- package/dist/src/planner/scopes/param.js.map +0 -1
- package/dist/src/planner/scopes/registered.js.map +0 -1
- package/dist/src/planner/scopes/scope.js.map +0 -1
- package/dist/src/planner/stats/basic-estimates.js.map +0 -1
- package/dist/src/planner/stats/index.js.map +0 -1
- package/dist/src/planner/type-utils.js.map +0 -1
- package/dist/src/planner/util/key-utils.js.map +0 -1
- package/dist/src/planner/validation/determinism-validator.js.map +0 -1
- package/dist/src/planner/validation/plan-validator.js.map +0 -1
- package/dist/src/runtime/async-util.js.map +0 -1
- package/dist/src/runtime/cache/shared-cache.js.map +0 -1
- package/dist/src/runtime/context-helpers.js.map +0 -1
- package/dist/src/runtime/deferred-constraint-queue.js.map +0 -1
- package/dist/src/runtime/emission-context.js.map +0 -1
- package/dist/src/runtime/emit/add-constraint.js.map +0 -1
- package/dist/src/runtime/emit/aggregate.js.map +0 -1
- package/dist/src/runtime/emit/array-index.js.map +0 -1
- package/dist/src/runtime/emit/between.js.map +0 -1
- package/dist/src/runtime/emit/binary.js.map +0 -1
- package/dist/src/runtime/emit/block.js.map +0 -1
- package/dist/src/runtime/emit/cache.js.map +0 -1
- package/dist/src/runtime/emit/case.js.map +0 -1
- package/dist/src/runtime/emit/cast.js.map +0 -1
- package/dist/src/runtime/emit/collate.js.map +0 -1
- package/dist/src/runtime/emit/column-reference.js.map +0 -1
- package/dist/src/runtime/emit/constraint-check.js.map +0 -1
- package/dist/src/runtime/emit/create-assertion.js.map +0 -1
- package/dist/src/runtime/emit/create-index.js.map +0 -1
- package/dist/src/runtime/emit/create-table.js.map +0 -1
- package/dist/src/runtime/emit/create-view.js.map +0 -1
- package/dist/src/runtime/emit/cte-reference.js.map +0 -1
- package/dist/src/runtime/emit/cte.js.map +0 -1
- package/dist/src/runtime/emit/delete.js.map +0 -1
- package/dist/src/runtime/emit/distinct.js.map +0 -1
- package/dist/src/runtime/emit/dml-executor.js.map +0 -1
- package/dist/src/runtime/emit/drop-assertion.js.map +0 -1
- package/dist/src/runtime/emit/drop-table.js.map +0 -1
- package/dist/src/runtime/emit/drop-view.js.map +0 -1
- package/dist/src/runtime/emit/filter.js.map +0 -1
- package/dist/src/runtime/emit/insert.js.map +0 -1
- package/dist/src/runtime/emit/internal-recursive-cte-ref.js.map +0 -1
- package/dist/src/runtime/emit/join.js.map +0 -1
- package/dist/src/runtime/emit/limit-offset.js.map +0 -1
- package/dist/src/runtime/emit/literal.js.map +0 -1
- package/dist/src/runtime/emit/parameter.js.map +0 -1
- package/dist/src/runtime/emit/pragma.js.map +0 -1
- package/dist/src/runtime/emit/project.js.map +0 -1
- package/dist/src/runtime/emit/recursive-cte.js.map +0 -1
- package/dist/src/runtime/emit/remote-query.js.map +0 -1
- package/dist/src/runtime/emit/retrieve.js.map +0 -1
- package/dist/src/runtime/emit/returning.js.map +0 -1
- package/dist/src/runtime/emit/scalar-function.js.map +0 -1
- package/dist/src/runtime/emit/scan.js.map +0 -1
- package/dist/src/runtime/emit/schema-declarative.js.map +0 -1
- package/dist/src/runtime/emit/sequencing.js.map +0 -1
- package/dist/src/runtime/emit/set-operation.js.map +0 -1
- package/dist/src/runtime/emit/sink.js.map +0 -1
- package/dist/src/runtime/emit/sort.js.map +0 -1
- package/dist/src/runtime/emit/subquery.js.map +0 -1
- package/dist/src/runtime/emit/table-valued-function.js.map +0 -1
- package/dist/src/runtime/emit/temporal-arithmetic.js.map +0 -1
- package/dist/src/runtime/emit/transaction.js.map +0 -1
- package/dist/src/runtime/emit/unary.js.map +0 -1
- package/dist/src/runtime/emit/update.js.map +0 -1
- package/dist/src/runtime/emit/values.js.map +0 -1
- package/dist/src/runtime/emit/window-function.js.map +0 -1
- package/dist/src/runtime/emit/window.js.map +0 -1
- package/dist/src/runtime/emitters.js.map +0 -1
- package/dist/src/runtime/register.js.map +0 -1
- package/dist/src/runtime/scheduler.js.map +0 -1
- package/dist/src/runtime/types.js.map +0 -1
- package/dist/src/runtime/utils.js.map +0 -1
- package/dist/src/schema/assertion.js.map +0 -1
- package/dist/src/schema/catalog.js.map +0 -1
- package/dist/src/schema/change-events.js.map +0 -1
- package/dist/src/schema/column.js.map +0 -1
- package/dist/src/schema/declared-schema-manager.js.map +0 -1
- package/dist/src/schema/function.js.map +0 -1
- package/dist/src/schema/manager.js.map +0 -1
- package/dist/src/schema/schema-differ.js.map +0 -1
- package/dist/src/schema/schema-hasher.js.map +0 -1
- package/dist/src/schema/schema.js.map +0 -1
- package/dist/src/schema/table.js.map +0 -1
- package/dist/src/schema/view.js.map +0 -1
- package/dist/src/schema/window-function.js.map +0 -1
- package/dist/src/types/builtin-types.js.map +0 -1
- package/dist/src/types/index.js.map +0 -1
- package/dist/src/types/json-type.js.map +0 -1
- package/dist/src/types/logical-type.js.map +0 -1
- package/dist/src/types/plugin-interface.js.map +0 -1
- package/dist/src/types/registry.js.map +0 -1
- package/dist/src/types/temporal-types.js.map +0 -1
- package/dist/src/types/validation.js.map +0 -1
- package/dist/src/util/affinity.js.map +0 -1
- package/dist/src/util/ast-stringify.js.map +0 -1
- package/dist/src/util/cached.js.map +0 -1
- package/dist/src/util/coercion.js.map +0 -1
- package/dist/src/util/comparison.js.map +0 -1
- package/dist/src/util/environment.js.map +0 -1
- package/dist/src/util/hash.js.map +0 -1
- package/dist/src/util/latches.js.map +0 -1
- package/dist/src/util/mutation-statement.js.map +0 -1
- package/dist/src/util/patterns.js.map +0 -1
- package/dist/src/util/plan-formatter.js.map +0 -1
- package/dist/src/util/plugin-helper.js.map +0 -1
- package/dist/src/util/row-descriptor.js.map +0 -1
- package/dist/src/util/serialization.js.map +0 -1
- package/dist/src/util/sql-literal.js.map +0 -1
- package/dist/src/util/working-table-iterable.js.map +0 -1
- package/dist/src/vtab/best-access-plan.js.map +0 -1
- package/dist/src/vtab/connection.js.map +0 -1
- package/dist/src/vtab/filter-info.js.map +0 -1
- package/dist/src/vtab/index-info.js.map +0 -1
- package/dist/src/vtab/manifest.js.map +0 -1
- package/dist/src/vtab/memory/connection.js.map +0 -1
- package/dist/src/vtab/memory/index.js.map +0 -1
- package/dist/src/vtab/memory/layer/base-cursor.js.map +0 -1
- package/dist/src/vtab/memory/layer/base.js.map +0 -1
- package/dist/src/vtab/memory/layer/connection.js.map +0 -1
- package/dist/src/vtab/memory/layer/interface.js.map +0 -1
- package/dist/src/vtab/memory/layer/manager.js.map +0 -1
- package/dist/src/vtab/memory/layer/safe-iterate.js.map +0 -1
- package/dist/src/vtab/memory/layer/scan-plan.js.map +0 -1
- package/dist/src/vtab/memory/layer/transaction-cursor.js.map +0 -1
- package/dist/src/vtab/memory/layer/transaction.js.map +0 -1
- package/dist/src/vtab/memory/module.js.map +0 -1
- package/dist/src/vtab/memory/table.js.map +0 -1
- package/dist/src/vtab/memory/types.js.map +0 -1
- package/dist/src/vtab/memory/utils/logging.js.map +0 -1
- package/dist/src/vtab/memory/utils/primary-key.js.map +0 -1
- package/dist/src/vtab/module.js.map +0 -1
- package/dist/src/vtab/table.js.map +0 -1
- package/src/common/constants.ts +0 -60
- package/src/common/datatype.ts +0 -85
- package/src/common/errors.ts +0 -189
- package/src/common/json-types.ts +0 -16
- package/src/common/logger.ts +0 -97
- package/src/common/type-inference.ts +0 -39
- package/src/common/types.ts +0 -151
- package/src/core/database-options.ts +0 -258
- package/src/core/database.ts +0 -1461
- package/src/core/param.ts +0 -56
- package/src/core/statement.ts +0 -528
- package/src/func/builtins/aggregate.ts +0 -269
- package/src/func/builtins/builtin-window-functions.ts +0 -166
- package/src/func/builtins/conversion.ts +0 -226
- package/src/func/builtins/datetime.ts +0 -500
- package/src/func/builtins/explain.ts +0 -779
- package/src/func/builtins/generation.ts +0 -43
- package/src/func/builtins/index.ts +0 -167
- package/src/func/builtins/json-helpers.ts +0 -237
- package/src/func/builtins/json-tvf.ts +0 -224
- package/src/func/builtins/json.ts +0 -588
- package/src/func/builtins/scalar.ts +0 -423
- package/src/func/builtins/schema.ts +0 -213
- package/src/func/builtins/string.ts +0 -306
- package/src/func/builtins/timespan.ts +0 -179
- package/src/func/context.ts +0 -258
- package/src/func/registration.ts +0 -201
- package/src/index.ts +0 -172
- package/src/parser/ast.ts +0 -581
- package/src/parser/index.ts +0 -65
- package/src/parser/lexer.ts +0 -806
- package/src/parser/parser.ts +0 -3352
- package/src/parser/utils.ts +0 -10
- package/src/parser/visitor.ts +0 -188
- package/src/planner/analysis/README.md +0 -93
- package/src/planner/analysis/binding-collector.ts +0 -83
- package/src/planner/analysis/const-evaluator.ts +0 -63
- package/src/planner/analysis/const-pass.ts +0 -282
- package/src/planner/analysis/constraint-extractor.ts +0 -764
- package/src/planner/analysis/predicate-normalizer.ts +0 -237
- package/src/planner/building/alter-table.ts +0 -49
- package/src/planner/building/block.ts +0 -93
- package/src/planner/building/constraint-builder.ts +0 -178
- package/src/planner/building/create-assertion.ts +0 -7
- package/src/planner/building/create-view.ts +0 -29
- package/src/planner/building/ddl.ts +0 -24
- package/src/planner/building/declare-schema.ts +0 -22
- package/src/planner/building/delete.ts +0 -218
- package/src/planner/building/drop-assertion.ts +0 -11
- package/src/planner/building/drop-table.ts +0 -13
- package/src/planner/building/drop-view.ts +0 -19
- package/src/planner/building/expression.ts +0 -205
- package/src/planner/building/function-call.ts +0 -129
- package/src/planner/building/insert.ts +0 -435
- package/src/planner/building/pragma.ts +0 -34
- package/src/planner/building/schema-resolution.ts +0 -176
- package/src/planner/building/select-aggregates.ts +0 -318
- package/src/planner/building/select-compound.ts +0 -119
- package/src/planner/building/select-context.ts +0 -85
- package/src/planner/building/select-modifiers.ts +0 -236
- package/src/planner/building/select-projections.ts +0 -177
- package/src/planner/building/select-window.ts +0 -259
- package/src/planner/building/select.ts +0 -567
- package/src/planner/building/table-function.ts +0 -49
- package/src/planner/building/table.ts +0 -40
- package/src/planner/building/transaction.ts +0 -23
- package/src/planner/building/update.ts +0 -331
- package/src/planner/building/with.ts +0 -180
- package/src/planner/cache/correlation-detector.ts +0 -83
- package/src/planner/cache/materialization-advisory.ts +0 -265
- package/src/planner/cache/reference-graph.ts +0 -196
- package/src/planner/cost/index.ts +0 -169
- package/src/planner/debug/logger-utils.ts +0 -68
- package/src/planner/debug.ts +0 -480
- package/src/planner/framework/README.md +0 -132
- package/src/planner/framework/characteristics.ts +0 -503
- package/src/planner/framework/context.ts +0 -239
- package/src/planner/framework/pass.ts +0 -354
- package/src/planner/framework/physical-utils.ts +0 -210
- package/src/planner/framework/registry.ts +0 -261
- package/src/planner/framework/trace.ts +0 -259
- package/src/planner/nodes/add-constraint-node.ts +0 -62
- package/src/planner/nodes/aggregate-function.ts +0 -155
- package/src/planner/nodes/aggregate-node.ts +0 -267
- package/src/planner/nodes/array-index-node.ts +0 -50
- package/src/planner/nodes/block.ts +0 -80
- package/src/planner/nodes/cache-node.ts +0 -103
- package/src/planner/nodes/constraint-check-node.ts +0 -138
- package/src/planner/nodes/create-assertion-node.ts +0 -51
- package/src/planner/nodes/create-index-node.ts +0 -41
- package/src/planner/nodes/create-table-node.ts +0 -35
- package/src/planner/nodes/create-view-node.ts +0 -44
- package/src/planner/nodes/cte-node.ts +0 -168
- package/src/planner/nodes/cte-reference-node.ts +0 -125
- package/src/planner/nodes/declarative-schema.ts +0 -221
- package/src/planner/nodes/delete-node.ts +0 -102
- package/src/planner/nodes/distinct-node.ts +0 -107
- package/src/planner/nodes/dml-executor-node.ts +0 -104
- package/src/planner/nodes/drop-assertion-node.ts +0 -50
- package/src/planner/nodes/drop-table-node.ts +0 -36
- package/src/planner/nodes/drop-view-node.ts +0 -37
- package/src/planner/nodes/filter.ts +0 -144
- package/src/planner/nodes/function.ts +0 -98
- package/src/planner/nodes/insert-node.ts +0 -126
- package/src/planner/nodes/internal-recursive-cte-ref-node.ts +0 -61
- package/src/planner/nodes/join-node.ts +0 -336
- package/src/planner/nodes/limit-offset.ts +0 -144
- package/src/planner/nodes/plan-node-type.ts +0 -95
- package/src/planner/nodes/plan-node.ts +0 -503
- package/src/planner/nodes/pragma.ts +0 -98
- package/src/planner/nodes/project-node.ts +0 -337
- package/src/planner/nodes/recursive-cte-node.ts +0 -158
- package/src/planner/nodes/reference.ts +0 -334
- package/src/planner/nodes/remote-query-node.ts +0 -73
- package/src/planner/nodes/retrieve-node.ts +0 -86
- package/src/planner/nodes/returning-node.ts +0 -269
- package/src/planner/nodes/scalar.ts +0 -772
- package/src/planner/nodes/sequencing-node.ts +0 -113
- package/src/planner/nodes/set-operation-node.ts +0 -87
- package/src/planner/nodes/single-row.ts +0 -85
- package/src/planner/nodes/sink-node.ts +0 -61
- package/src/planner/nodes/sort.ts +0 -166
- package/src/planner/nodes/stream-aggregate.ts +0 -293
- package/src/planner/nodes/subquery.ts +0 -268
- package/src/planner/nodes/table-access-nodes.ts +0 -323
- package/src/planner/nodes/table-function-call.ts +0 -134
- package/src/planner/nodes/transaction-node.ts +0 -55
- package/src/planner/nodes/update-node.ts +0 -138
- package/src/planner/nodes/values-node.ts +0 -244
- package/src/planner/nodes/view-reference-node.ts +0 -97
- package/src/planner/nodes/window-function.ts +0 -73
- package/src/planner/nodes/window-node.ts +0 -199
- package/src/planner/optimizer-tuning.ts +0 -105
- package/src/planner/optimizer.ts +0 -332
- package/src/planner/planning-context.ts +0 -190
- package/src/planner/resolve.ts +0 -101
- package/src/planner/rules/README.md +0 -96
- package/src/planner/rules/access/rule-select-access-path.ts +0 -399
- package/src/planner/rules/aggregate/rule-aggregate-streaming.ts +0 -162
- package/src/planner/rules/cache/rule-cte-optimization.ts +0 -79
- package/src/planner/rules/cache/rule-materialization-advisory.ts +0 -77
- package/src/planner/rules/cache/rule-mutating-subquery-cache.ts +0 -104
- package/src/planner/rules/join/rule-join-greedy-commute.ts +0 -48
- package/src/planner/rules/join/rule-join-key-inference.ts +0 -35
- package/src/planner/rules/join/rule-quickpick-enumeration.ts +0 -267
- package/src/planner/rules/predicate/rule-predicate-pushdown.ts +0 -144
- package/src/planner/rules/retrieve/rule-grow-retrieve.ts +0 -337
- package/src/planner/scopes/aliased.ts +0 -50
- package/src/planner/scopes/base.ts +0 -10
- package/src/planner/scopes/empty.ts +0 -12
- package/src/planner/scopes/global.ts +0 -73
- package/src/planner/scopes/multi.ts +0 -40
- package/src/planner/scopes/param.ts +0 -95
- package/src/planner/scopes/registered.ts +0 -67
- package/src/planner/scopes/scope.ts +0 -16
- package/src/planner/stats/basic-estimates.ts +0 -107
- package/src/planner/stats/index.ts +0 -158
- package/src/planner/type-utils.ts +0 -87
- package/src/planner/util/key-utils.ts +0 -46
- package/src/planner/validation/determinism-validator.ts +0 -104
- package/src/planner/validation/plan-validator.ts +0 -335
- package/src/runtime/async-util.ts +0 -283
- package/src/runtime/cache/shared-cache.ts +0 -169
- package/src/runtime/context-helpers.ts +0 -191
- package/src/runtime/deferred-constraint-queue.ts +0 -196
- package/src/runtime/emission-context.ts +0 -319
- package/src/runtime/emit/add-constraint.ts +0 -78
- package/src/runtime/emit/aggregate.ts +0 -581
- package/src/runtime/emit/array-index.ts +0 -25
- package/src/runtime/emit/between.ts +0 -51
- package/src/runtime/emit/binary.ts +0 -357
- package/src/runtime/emit/block.ts +0 -23
- package/src/runtime/emit/cache.ts +0 -64
- package/src/runtime/emit/case.ts +0 -87
- package/src/runtime/emit/cast.ts +0 -151
- package/src/runtime/emit/collate.ts +0 -9
- package/src/runtime/emit/column-reference.ts +0 -17
- package/src/runtime/emit/constraint-check.ts +0 -290
- package/src/runtime/emit/create-assertion.ts +0 -82
- package/src/runtime/emit/create-index.ts +0 -15
- package/src/runtime/emit/create-table.ts +0 -15
- package/src/runtime/emit/create-view.ts +0 -52
- package/src/runtime/emit/cte-reference.ts +0 -38
- package/src/runtime/emit/cte.ts +0 -39
- package/src/runtime/emit/delete.ts +0 -24
- package/src/runtime/emit/distinct.ts +0 -40
- package/src/runtime/emit/dml-executor.ts +0 -198
- package/src/runtime/emit/drop-assertion.ts +0 -45
- package/src/runtime/emit/drop-table.ts +0 -27
- package/src/runtime/emit/drop-view.ts +0 -49
- package/src/runtime/emit/filter.ts +0 -30
- package/src/runtime/emit/insert.ts +0 -42
- package/src/runtime/emit/internal-recursive-cte-ref.ts +0 -37
- package/src/runtime/emit/join.ts +0 -148
- package/src/runtime/emit/limit-offset.ts +0 -73
- package/src/runtime/emit/literal.ts +0 -17
- package/src/runtime/emit/parameter.ts +0 -59
- package/src/runtime/emit/pragma.ts +0 -56
- package/src/runtime/emit/project.ts +0 -46
- package/src/runtime/emit/recursive-cte.ts +0 -111
- package/src/runtime/emit/remote-query.ts +0 -47
- package/src/runtime/emit/retrieve.ts +0 -15
- package/src/runtime/emit/returning.ts +0 -41
- package/src/runtime/emit/scalar-function.ts +0 -69
- package/src/runtime/emit/scan.ts +0 -106
- package/src/runtime/emit/schema-declarative.ts +0 -215
- package/src/runtime/emit/sequencing.ts +0 -24
- package/src/runtime/emit/set-operation.ts +0 -141
- package/src/runtime/emit/sink.ts +0 -27
- package/src/runtime/emit/sort.ts +0 -75
- package/src/runtime/emit/subquery.ts +0 -203
- package/src/runtime/emit/table-valued-function.ts +0 -106
- package/src/runtime/emit/temporal-arithmetic.ts +0 -302
- package/src/runtime/emit/transaction.ts +0 -205
- package/src/runtime/emit/unary.ts +0 -101
- package/src/runtime/emit/update.ts +0 -66
- package/src/runtime/emit/values.ts +0 -66
- package/src/runtime/emit/window-function.ts +0 -42
- package/src/runtime/emit/window.ts +0 -458
- package/src/runtime/emitters.ts +0 -183
- package/src/runtime/register.ts +0 -150
- package/src/runtime/scheduler.ts +0 -488
- package/src/runtime/types.ts +0 -242
- package/src/runtime/utils.ts +0 -177
- package/src/schema/assertion.ts +0 -21
- package/src/schema/catalog.ts +0 -269
- package/src/schema/change-events.ts +0 -80
- package/src/schema/column.ts +0 -51
- package/src/schema/declared-schema-manager.ts +0 -82
- package/src/schema/function.ts +0 -188
- package/src/schema/manager.ts +0 -1034
- package/src/schema/schema-differ.ts +0 -214
- package/src/schema/schema-hasher.ts +0 -26
- package/src/schema/schema.ts +0 -222
- package/src/schema/table.ts +0 -409
- package/src/schema/view.ts +0 -19
- package/src/schema/window-function.ts +0 -56
- package/src/types/builtin-types.ts +0 -350
- package/src/types/index.ts +0 -17
- package/src/types/json-type.ts +0 -152
- package/src/types/logical-type.ts +0 -91
- package/src/types/plugin-interface.ts +0 -10
- package/src/types/registry.ts +0 -204
- package/src/types/temporal-types.ts +0 -290
- package/src/types/validation.ts +0 -120
- package/src/util/affinity.ts +0 -151
- package/src/util/ast-stringify.ts +0 -887
- package/src/util/cached.ts +0 -25
- package/src/util/coercion.ts +0 -113
- package/src/util/comparison.ts +0 -510
- package/src/util/environment.ts +0 -52
- package/src/util/hash.ts +0 -90
- package/src/util/latches.ts +0 -47
- package/src/util/mutation-statement.ts +0 -135
- package/src/util/patterns.ts +0 -56
- package/src/util/plan-formatter.ts +0 -48
- package/src/util/plugin-helper.ts +0 -110
- package/src/util/row-descriptor.ts +0 -105
- package/src/util/serialization.ts +0 -47
- package/src/util/sql-literal.ts +0 -22
- package/src/util/working-table-iterable.ts +0 -38
- package/src/vtab/best-access-plan.ts +0 -244
- package/src/vtab/connection.ts +0 -36
- package/src/vtab/filter-info.ts +0 -23
- package/src/vtab/index-info.ts +0 -84
- package/src/vtab/manifest.ts +0 -86
- package/src/vtab/memory/connection.ts +0 -73
- package/src/vtab/memory/index.ts +0 -191
- package/src/vtab/memory/layer/base-cursor.ts +0 -124
- package/src/vtab/memory/layer/base.ts +0 -275
- package/src/vtab/memory/layer/connection.ts +0 -203
- package/src/vtab/memory/layer/interface.ts +0 -47
- package/src/vtab/memory/layer/manager.ts +0 -909
- package/src/vtab/memory/layer/safe-iterate.ts +0 -49
- package/src/vtab/memory/layer/scan-plan.ts +0 -84
- package/src/vtab/memory/layer/transaction-cursor.ts +0 -162
- package/src/vtab/memory/layer/transaction.ts +0 -229
- package/src/vtab/memory/module.ts +0 -667
- package/src/vtab/memory/table.ts +0 -251
- package/src/vtab/memory/types.ts +0 -23
- package/src/vtab/memory/utils/logging.ts +0 -36
- package/src/vtab/memory/utils/primary-key.ts +0 -163
- package/src/vtab/module.ts +0 -162
- package/src/vtab/table.ts +0 -177
|
@@ -1,909 +0,0 @@
|
|
|
1
|
-
import type { Database } from '../../../core/database.js';
|
|
2
|
-
import { type TableSchema, type IndexSchema, buildColumnIndexMap, columnDefToSchema } from '../../../schema/table.js';
|
|
3
|
-
import { type BTreeKey, type BTreeKeyForPrimary } from '../types.js';
|
|
4
|
-
import { StatusCode, type SqlValue, type Row } from '../../../common/types.js';
|
|
5
|
-
import { BaseLayer } from './base.js';
|
|
6
|
-
import { TransactionLayer } from './transaction.js';
|
|
7
|
-
import type { Layer } from './interface.js';
|
|
8
|
-
import { MemoryTableConnection } from './connection.js';
|
|
9
|
-
import { Latches } from '../../../util/latches.js';
|
|
10
|
-
import { QuereusError, ConstraintError } from '../../../common/errors.js';
|
|
11
|
-
import { ConflictResolution, IndexConstraintOp } from '../../../common/constants.js';
|
|
12
|
-
import type { ColumnDef as ASTColumnDef, LiteralExpr } from '../../../parser/ast.js';
|
|
13
|
-
import { compareSqlValues } from '../../../util/comparison.js';
|
|
14
|
-
import type { ScanPlan } from './scan-plan.js';
|
|
15
|
-
import type { ColumnSchema } from '../../../schema/column.js';
|
|
16
|
-
import { scanBaseLayer } from './base-cursor.js';
|
|
17
|
-
import { scanTransactionLayer } from './transaction-cursor.js';
|
|
18
|
-
import { createPrimaryKeyFunctions, buildPrimaryKeyFromValues, type PrimaryKeyFunctions } from '../utils/primary-key.js';
|
|
19
|
-
import { createMemoryTableLoggers } from '../utils/logging.js';
|
|
20
|
-
import { getSyncLiteral } from '../../../parser/utils.js';
|
|
21
|
-
import { createLogger } from '../../../common/logger.js';
|
|
22
|
-
import { validateAndParse } from '../../../types/validation.js';
|
|
23
|
-
|
|
24
|
-
let tableManagerCounter = 0;
|
|
25
|
-
const logger = createMemoryTableLoggers('layer:manager');
|
|
26
|
-
|
|
27
|
-
export class MemoryTableManager {
|
|
28
|
-
public readonly managerId: number;
|
|
29
|
-
public readonly db: Database;
|
|
30
|
-
public readonly schemaName: string;
|
|
31
|
-
private _tableName: string;
|
|
32
|
-
public get tableName() { return this._tableName; }
|
|
33
|
-
|
|
34
|
-
private baseLayer: BaseLayer;
|
|
35
|
-
private _currentCommittedLayer: Layer;
|
|
36
|
-
private connections: Map<number, MemoryTableConnection> = new Map();
|
|
37
|
-
public readonly isReadOnly: boolean;
|
|
38
|
-
public tableSchema: TableSchema;
|
|
39
|
-
|
|
40
|
-
private primaryKeyFunctions!: PrimaryKeyFunctions;
|
|
41
|
-
|
|
42
|
-
constructor(
|
|
43
|
-
db: Database,
|
|
44
|
-
_moduleName: string,
|
|
45
|
-
schemaName: string,
|
|
46
|
-
tableName: string,
|
|
47
|
-
initialSchema: TableSchema,
|
|
48
|
-
readOnly: boolean = false
|
|
49
|
-
) {
|
|
50
|
-
this.managerId = tableManagerCounter++;
|
|
51
|
-
this.db = db;
|
|
52
|
-
this.schemaName = schemaName;
|
|
53
|
-
this._tableName = tableName;
|
|
54
|
-
this.tableSchema = initialSchema;
|
|
55
|
-
this.isReadOnly = readOnly;
|
|
56
|
-
|
|
57
|
-
this.initializePrimaryKeyFunctions();
|
|
58
|
-
|
|
59
|
-
this.baseLayer = new BaseLayer(this.tableSchema);
|
|
60
|
-
this._currentCommittedLayer = this.baseLayer;
|
|
61
|
-
}
|
|
62
|
-
|
|
63
|
-
private initializePrimaryKeyFunctions(): void {
|
|
64
|
-
this.primaryKeyFunctions = createPrimaryKeyFunctions(this.tableSchema);
|
|
65
|
-
}
|
|
66
|
-
|
|
67
|
-
private get primaryKeyFromRow() {
|
|
68
|
-
return this.primaryKeyFunctions.extractFromRow;
|
|
69
|
-
}
|
|
70
|
-
|
|
71
|
-
private get comparePrimaryKeys() {
|
|
72
|
-
return this.primaryKeyFunctions.compare;
|
|
73
|
-
}
|
|
74
|
-
|
|
75
|
-
public get currentCommittedLayer(): Layer {
|
|
76
|
-
return this._currentCommittedLayer;
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
public connect(): MemoryTableConnection {
|
|
80
|
-
const connection = new MemoryTableConnection(this, this._currentCommittedLayer);
|
|
81
|
-
this.connections.set(connection.connectionId, connection);
|
|
82
|
-
return connection;
|
|
83
|
-
}
|
|
84
|
-
|
|
85
|
-
public async disconnect(connectionId: number): Promise<void> {
|
|
86
|
-
const connection = this.connections.get(connectionId);
|
|
87
|
-
if (!connection) return;
|
|
88
|
-
|
|
89
|
-
// If the connection still has an un-committed pending layer, defer
|
|
90
|
-
// disconnect until the layer is either committed or rolled back by the
|
|
91
|
-
// transaction coordinator. This avoids accidental rollback during
|
|
92
|
-
// implicit transactions.
|
|
93
|
-
if (connection.pendingTransactionLayer && !connection.pendingTransactionLayer.isCommitted()) {
|
|
94
|
-
logger.debugLog(`[Disconnect] Deferring disconnect of connection ${connectionId} while transaction pending for ${this._tableName}`);
|
|
95
|
-
return;
|
|
96
|
-
}
|
|
97
|
-
|
|
98
|
-
// No pending changes – safe to remove immediately.
|
|
99
|
-
this.connections.delete(connectionId);
|
|
100
|
-
|
|
101
|
-
// Attempt fast layer-collapse in the background (best-effort)
|
|
102
|
-
void this.tryCollapseLayers().catch(err => {
|
|
103
|
-
logger.error('Disconnect', this._tableName, 'Layer collapse failed', err);
|
|
104
|
-
});
|
|
105
|
-
}
|
|
106
|
-
|
|
107
|
-
public async commitTransaction(connection: MemoryTableConnection): Promise<void> {
|
|
108
|
-
if (this.isReadOnly) {
|
|
109
|
-
if (connection.pendingTransactionLayer && connection.pendingTransactionLayer.hasChanges()) {
|
|
110
|
-
throw new QuereusError(`Table ${this._tableName} is read-only, cannot commit changes.`, StatusCode.READONLY);
|
|
111
|
-
}
|
|
112
|
-
connection.pendingTransactionLayer = null;
|
|
113
|
-
connection.clearSavepoints();
|
|
114
|
-
return;
|
|
115
|
-
}
|
|
116
|
-
const pendingLayer = connection.pendingTransactionLayer;
|
|
117
|
-
if (!pendingLayer) return;
|
|
118
|
-
|
|
119
|
-
const lockKey = `MemoryTable.Commit:${this.schemaName}.${this._tableName}`;
|
|
120
|
-
const release = await Latches.acquire(lockKey);
|
|
121
|
-
logger.debugLog(`[Commit ${connection.connectionId}] Acquired lock for ${this._tableName}`);
|
|
122
|
-
try {
|
|
123
|
-
// Walk up the parent chain to find if the current committed layer is an ancestor
|
|
124
|
-
// This handles savepoint chains properly
|
|
125
|
-
let currentParent: Layer | null = pendingLayer.getParent();
|
|
126
|
-
let foundCommittedLayer = false;
|
|
127
|
-
while (currentParent) {
|
|
128
|
-
if (currentParent === this._currentCommittedLayer) {
|
|
129
|
-
foundCommittedLayer = true;
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
currentParent = currentParent.getParent();
|
|
133
|
-
}
|
|
134
|
-
|
|
135
|
-
// Also check if the current committed layer and pending layer are siblings
|
|
136
|
-
// (both children of the same parent) - this handles coordinated multi-connection commits
|
|
137
|
-
if (!foundCommittedLayer) {
|
|
138
|
-
const pendingParent = pendingLayer.getParent();
|
|
139
|
-
let committedAncestor: Layer | null = this._currentCommittedLayer;
|
|
140
|
-
while (committedAncestor) {
|
|
141
|
-
if (committedAncestor === pendingParent) {
|
|
142
|
-
foundCommittedLayer = true;
|
|
143
|
-
break;
|
|
144
|
-
}
|
|
145
|
-
committedAncestor = committedAncestor.getParent();
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
|
|
149
|
-
if (!foundCommittedLayer) {
|
|
150
|
-
// During coordinated multi-connection commits (explicit COMMIT or implicit transaction commit),
|
|
151
|
-
// sibling layers are allowed. Only enforce strict validation outside coordinated commits.
|
|
152
|
-
if (!this.db._inCoordinatedCommit()) {
|
|
153
|
-
connection.pendingTransactionLayer = null;
|
|
154
|
-
connection.clearSavepoints();
|
|
155
|
-
logger.warn('Commit Transaction', this._tableName, 'Stale commit detected, rolling back', { connectionId: connection.connectionId });
|
|
156
|
-
throw new QuereusError(`Commit failed: concurrent update on table ${this._tableName}. Retry.`, StatusCode.BUSY);
|
|
157
|
-
}
|
|
158
|
-
}
|
|
159
|
-
|
|
160
|
-
pendingLayer.markCommitted();
|
|
161
|
-
this._currentCommittedLayer = pendingLayer;
|
|
162
|
-
logger.debugLog(`[Commit ${connection.connectionId}] CurrentCommittedLayer set to ${pendingLayer.getLayerId()} for ${this._tableName}`);
|
|
163
|
-
connection.readLayer = pendingLayer;
|
|
164
|
-
connection.pendingTransactionLayer = null;
|
|
165
|
-
connection.clearSavepoints();
|
|
166
|
-
} finally {
|
|
167
|
-
release();
|
|
168
|
-
logger.debugLog(`[Commit ${connection.connectionId}] Released lock for ${this._tableName}`);
|
|
169
|
-
}
|
|
170
|
-
}
|
|
171
|
-
|
|
172
|
-
async tryCollapseLayers(): Promise<void> {
|
|
173
|
-
const lockKey = `MemoryTable.Collapse:${this.schemaName}.${this._tableName}`;
|
|
174
|
-
let release: (() => void) | null = null;
|
|
175
|
-
try {
|
|
176
|
-
const acquirePromise = Latches.acquire(lockKey);
|
|
177
|
-
const timeoutPromise = new Promise<null>((resolve) => setTimeout(() => resolve(null), 10)); // Short timeout
|
|
178
|
-
const result = await Promise.race([
|
|
179
|
-
acquirePromise.then(releaseFn => ({ release: releaseFn })),
|
|
180
|
-
timeoutPromise.then(() => ({ release: null }))
|
|
181
|
-
]);
|
|
182
|
-
release = result.release;
|
|
183
|
-
if (!release) {
|
|
184
|
-
logger.debugLog(`[Collapse] Lock busy for ${this._tableName}, skipping.`);
|
|
185
|
-
return;
|
|
186
|
-
}
|
|
187
|
-
logger.debugLog(`[Collapse] Acquired lock for ${this._tableName}`);
|
|
188
|
-
let collapsedCount = 0;
|
|
189
|
-
const maxCollapseIterations = 10; // Prevent infinite loops
|
|
190
|
-
let iterations = 0;
|
|
191
|
-
|
|
192
|
-
// Continue collapsing layers as long as it's safe to do so
|
|
193
|
-
while (iterations < maxCollapseIterations &&
|
|
194
|
-
this._currentCommittedLayer instanceof TransactionLayer &&
|
|
195
|
-
this._currentCommittedLayer.isCommitted()) {
|
|
196
|
-
|
|
197
|
-
const layerToPromote = this._currentCommittedLayer as TransactionLayer;
|
|
198
|
-
const parentLayer = layerToPromote.getParent();
|
|
199
|
-
if (!parentLayer) {
|
|
200
|
-
logger.error('Collapse Layers', this._tableName, 'Committed TransactionLayer has no parent', { layerId: layerToPromote.getLayerId() });
|
|
201
|
-
break;
|
|
202
|
-
}
|
|
203
|
-
|
|
204
|
-
// Check if anyone is still using the parent layer or any of its ancestors
|
|
205
|
-
if (this.isLayerInUse(parentLayer)) {
|
|
206
|
-
logger.debugLog(`[Collapse] Parent layer ${parentLayer.getLayerId()} or its ancestors in use. Cannot collapse layer ${layerToPromote.getLayerId()}.`);
|
|
207
|
-
break;
|
|
208
|
-
}
|
|
209
|
-
|
|
210
|
-
logger.debugLog(`[Collapse] Promoting layer ${layerToPromote.getLayerId()} to become independent from parent ${parentLayer.getLayerId()} for ${this._tableName}`);
|
|
211
|
-
|
|
212
|
-
// With inherited BTrees, "collapsing" means making the transaction layer independent
|
|
213
|
-
// by calling clearBase() on its BTrees, effectively making it the new base data
|
|
214
|
-
layerToPromote.clearBase();
|
|
215
|
-
|
|
216
|
-
// Update connections that were reading from the collapsed parent layer
|
|
217
|
-
for (const conn of this.connections.values()) {
|
|
218
|
-
if (conn.readLayer === parentLayer) {
|
|
219
|
-
// Update connections to read from the now-independent transaction layer
|
|
220
|
-
conn.readLayer = layerToPromote;
|
|
221
|
-
logger.debugLog(`[Collapse] Connection ${conn.connectionId} updated to read from independent layer ${layerToPromote.getLayerId()}`);
|
|
222
|
-
}
|
|
223
|
-
}
|
|
224
|
-
|
|
225
|
-
collapsedCount++;
|
|
226
|
-
iterations++;
|
|
227
|
-
|
|
228
|
-
// The layer is now independent, but check if we can collapse further
|
|
229
|
-
// by examining if this layer can be promoted above its (now detached) parent
|
|
230
|
-
logger.debugLog(`[Collapse] Layer ${layerToPromote.getLayerId()} is now independent for ${this._tableName}`);
|
|
231
|
-
}
|
|
232
|
-
|
|
233
|
-
// Trigger garbage collection of unreferenced layers
|
|
234
|
-
if (collapsedCount > 0) {
|
|
235
|
-
void this.cleanupUnreferencedLayers();
|
|
236
|
-
logger.operation('Collapse Layers', this._tableName, { collapsedCount, iterations });
|
|
237
|
-
} else {
|
|
238
|
-
logger.debugLog(`[Collapse] No layers collapsed for ${this._tableName}. Current: ${this._currentCommittedLayer.getLayerId()}`);
|
|
239
|
-
}
|
|
240
|
-
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
241
|
-
} catch (e: any) {
|
|
242
|
-
logger.error('Collapse Layers', this._tableName, e);
|
|
243
|
-
} finally {
|
|
244
|
-
if (release) {
|
|
245
|
-
release();
|
|
246
|
-
logger.debugLog(`[Collapse] Released lock for ${this._tableName}`);
|
|
247
|
-
}
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* Checks if a layer is currently in use by any connections.
|
|
253
|
-
* This includes checking if any connection is reading from the layer,
|
|
254
|
-
* has it as a pending transaction layer, or has it as a savepoint.
|
|
255
|
-
*/
|
|
256
|
-
private isLayerInUse(layer: Layer): boolean {
|
|
257
|
-
for (const conn of this.connections.values()) {
|
|
258
|
-
// Check if connection is reading from this layer
|
|
259
|
-
if (conn.readLayer === layer) {
|
|
260
|
-
return true;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
// Check if connection has this layer as pending transaction
|
|
264
|
-
if (conn.pendingTransactionLayer === layer) {
|
|
265
|
-
return true;
|
|
266
|
-
}
|
|
267
|
-
|
|
268
|
-
// Check if connection has this layer in its parent chain
|
|
269
|
-
let currentLayer = conn.pendingTransactionLayer?.getParent();
|
|
270
|
-
while (currentLayer) {
|
|
271
|
-
if (currentLayer === layer) {
|
|
272
|
-
return true;
|
|
273
|
-
}
|
|
274
|
-
if (currentLayer instanceof TransactionLayer) {
|
|
275
|
-
currentLayer = currentLayer.getParent();
|
|
276
|
-
} else {
|
|
277
|
-
break;
|
|
278
|
-
}
|
|
279
|
-
}
|
|
280
|
-
}
|
|
281
|
-
return false;
|
|
282
|
-
}
|
|
283
|
-
|
|
284
|
-
/**
|
|
285
|
-
* Performs garbage collection of layers that are no longer referenced
|
|
286
|
-
* by any connections or the current committed layer chain.
|
|
287
|
-
*/
|
|
288
|
-
private cleanupUnreferencedLayers(): void {
|
|
289
|
-
// For now, this is a no-op since JavaScript's garbage collector
|
|
290
|
-
// will handle cleanup of unreferenced objects automatically.
|
|
291
|
-
// In the future, we could implement more aggressive cleanup
|
|
292
|
-
// or tracking of layer references for memory monitoring.
|
|
293
|
-
logger.debugLog(`[Cleanup] Triggering garbage collection hint for ${this._tableName}`);
|
|
294
|
-
|
|
295
|
-
// Optional: Force garbage collection if available (Node.js with --expose-gc)
|
|
296
|
-
if (typeof global !== 'undefined' && global.gc) {
|
|
297
|
-
try {
|
|
298
|
-
global.gc();
|
|
299
|
-
} catch {
|
|
300
|
-
// Ignore errors - gc() might not be available
|
|
301
|
-
}
|
|
302
|
-
}
|
|
303
|
-
}
|
|
304
|
-
|
|
305
|
-
// With inherited BTrees, lookupEffectiveRow is much simpler
|
|
306
|
-
public lookupEffectiveRow(primaryKey: BTreeKeyForPrimary, startLayer: Layer): Row | null {
|
|
307
|
-
// With inherited BTrees, a simple get() will traverse the inheritance chain automatically
|
|
308
|
-
const primaryTree = startLayer.getModificationTree('primary');
|
|
309
|
-
if (!primaryTree) return null;
|
|
310
|
-
|
|
311
|
-
const result = primaryTree.get(primaryKey);
|
|
312
|
-
return result === undefined ? null : result as Row;
|
|
313
|
-
}
|
|
314
|
-
|
|
315
|
-
// Simplified for compatibility, though less relevant with inherited BTrees
|
|
316
|
-
lookupEffectiveValue(key: BTreeKeyForPrimary, indexName: string | 'primary', startLayer: Layer): Row | null {
|
|
317
|
-
if (indexName !== 'primary') {
|
|
318
|
-
logger.error('lookupEffectiveValue', this._tableName, 'Currently only supports primary index for MemoryTableManager');
|
|
319
|
-
return null;
|
|
320
|
-
}
|
|
321
|
-
const primaryTree = startLayer.getModificationTree('primary');
|
|
322
|
-
if (!primaryTree) return null;
|
|
323
|
-
|
|
324
|
-
const result = primaryTree.get(key);
|
|
325
|
-
return result === undefined ? null : result;
|
|
326
|
-
}
|
|
327
|
-
|
|
328
|
-
public async performMutation(
|
|
329
|
-
connection: MemoryTableConnection,
|
|
330
|
-
operation: 'insert' | 'update' | 'delete',
|
|
331
|
-
values: Row | undefined,
|
|
332
|
-
oldKeyValues?: Row,
|
|
333
|
-
onConflict: ConflictResolution = ConflictResolution.ABORT
|
|
334
|
-
): Promise<Row | undefined> {
|
|
335
|
-
this.validateMutationPermissions(operation);
|
|
336
|
-
|
|
337
|
-
const wasExplicitTransaction = connection.explicitTransaction;
|
|
338
|
-
this.ensureTransactionLayer(connection);
|
|
339
|
-
|
|
340
|
-
const targetLayer = connection.pendingTransactionLayer!;
|
|
341
|
-
this.cleanConflictResolutionFromValues(values);
|
|
342
|
-
|
|
343
|
-
try {
|
|
344
|
-
let result: Row | undefined;
|
|
345
|
-
|
|
346
|
-
switch (operation) {
|
|
347
|
-
case 'insert':
|
|
348
|
-
result = await this.performInsert(targetLayer, values, onConflict);
|
|
349
|
-
break;
|
|
350
|
-
case 'update':
|
|
351
|
-
result = await this.performUpdate(targetLayer, values, oldKeyValues, onConflict);
|
|
352
|
-
break;
|
|
353
|
-
case 'delete':
|
|
354
|
-
result = await this.performDelete(targetLayer, oldKeyValues);
|
|
355
|
-
break;
|
|
356
|
-
default: {
|
|
357
|
-
const exhaustiveCheck: never = operation;
|
|
358
|
-
throw new QuereusError(`Unsupported operation: ${exhaustiveCheck}`, StatusCode.INTERNAL);
|
|
359
|
-
}
|
|
360
|
-
}
|
|
361
|
-
|
|
362
|
-
// Auto-commit if we weren't already in an explicit transaction
|
|
363
|
-
if (!wasExplicitTransaction && this.db.getAutocommit()) {
|
|
364
|
-
await this.commitTransaction(connection);
|
|
365
|
-
}
|
|
366
|
-
|
|
367
|
-
return result;
|
|
368
|
-
} catch (e) {
|
|
369
|
-
if (e instanceof ConstraintError && onConflict === ConflictResolution.IGNORE) {
|
|
370
|
-
// Auto-commit the transaction even when ignoring constraint errors
|
|
371
|
-
if (!wasExplicitTransaction && this.db.getAutocommit()) {
|
|
372
|
-
await this.commitTransaction(connection);
|
|
373
|
-
}
|
|
374
|
-
return undefined;
|
|
375
|
-
}
|
|
376
|
-
throw e;
|
|
377
|
-
}
|
|
378
|
-
}
|
|
379
|
-
|
|
380
|
-
private validateMutationPermissions(_operation: 'insert' | 'update' | 'delete'): void {
|
|
381
|
-
if (this.isReadOnly) {
|
|
382
|
-
throw new QuereusError(`Table '${this._tableName}' is read-only`, StatusCode.READONLY);
|
|
383
|
-
}
|
|
384
|
-
}
|
|
385
|
-
|
|
386
|
-
private ensureTransactionLayer(connection: MemoryTableConnection): void {
|
|
387
|
-
if (!connection.pendingTransactionLayer) {
|
|
388
|
-
// Lazily create a new TransactionLayer based on the current committed layer
|
|
389
|
-
connection.pendingTransactionLayer = new TransactionLayer(this._currentCommittedLayer);
|
|
390
|
-
// If this method is called from a DML statement outside an explicit BEGIN, the
|
|
391
|
-
// transaction is auto-created (autocommit mode). Leave explicitTransaction flag as-is.
|
|
392
|
-
}
|
|
393
|
-
}
|
|
394
|
-
|
|
395
|
-
// Deprecated: conflict resolution is now passed explicitly to performMutation
|
|
396
|
-
private extractConflictResolution(values: Row | undefined): ConflictResolution {
|
|
397
|
-
return (values && (values as any)._onConflict)
|
|
398
|
-
? (values as any)._onConflict as ConflictResolution
|
|
399
|
-
: ConflictResolution.ABORT;
|
|
400
|
-
}
|
|
401
|
-
|
|
402
|
-
private shouldSkipPkCheck(values: Row | undefined): boolean {
|
|
403
|
-
return !!(values && (values as any)._skipPkCheck);
|
|
404
|
-
}
|
|
405
|
-
|
|
406
|
-
private cleanConflictResolutionFromValues(values: Row | undefined): void {
|
|
407
|
-
if (values && (values as any)._onConflict) {
|
|
408
|
-
delete (values as any)._onConflict;
|
|
409
|
-
}
|
|
410
|
-
if (values && (values as any)._skipPkCheck) {
|
|
411
|
-
delete (values as any)._skipPkCheck;
|
|
412
|
-
}
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
private async performInsert(
|
|
416
|
-
targetLayer: TransactionLayer,
|
|
417
|
-
values: Row | undefined,
|
|
418
|
-
onConflict: ConflictResolution
|
|
419
|
-
): Promise<Row | undefined> {
|
|
420
|
-
if (!values) {
|
|
421
|
-
throw new QuereusError("INSERT requires values.", StatusCode.MISUSE);
|
|
422
|
-
}
|
|
423
|
-
|
|
424
|
-
// Validate and parse values according to column types
|
|
425
|
-
const schema = targetLayer.getSchema();
|
|
426
|
-
const validatedRow: Row = values.map((value, index) => {
|
|
427
|
-
if (index >= schema.columns.length) {
|
|
428
|
-
throw new QuereusError(
|
|
429
|
-
`Too many values for INSERT into ${this._tableName}: expected ${schema.columns.length}, got ${values.length}`,
|
|
430
|
-
StatusCode.ERROR
|
|
431
|
-
);
|
|
432
|
-
}
|
|
433
|
-
const column = schema.columns[index];
|
|
434
|
-
return validateAndParse(value, column.logicalType, column.name);
|
|
435
|
-
});
|
|
436
|
-
|
|
437
|
-
const newRowData: Row = validatedRow;
|
|
438
|
-
const primaryKey = this.primaryKeyFromRow(newRowData);
|
|
439
|
-
const skipPkCheck = this.shouldSkipPkCheck(values);
|
|
440
|
-
|
|
441
|
-
// Only check for existing rows if not skipping PK checks (engine-level constraint checking handles this)
|
|
442
|
-
if (!skipPkCheck) {
|
|
443
|
-
const existingRow = this.lookupEffectiveRow(primaryKey, targetLayer);
|
|
444
|
-
|
|
445
|
-
if (existingRow !== null) {
|
|
446
|
-
if (onConflict === ConflictResolution.IGNORE) return undefined;
|
|
447
|
-
throw new ConstraintError(`UNIQUE constraint failed: ${this._tableName} PK.`);
|
|
448
|
-
}
|
|
449
|
-
}
|
|
450
|
-
|
|
451
|
-
targetLayer.recordUpsert(primaryKey, newRowData, null);
|
|
452
|
-
return newRowData;
|
|
453
|
-
}
|
|
454
|
-
|
|
455
|
-
private async performUpdate(
|
|
456
|
-
targetLayer: TransactionLayer,
|
|
457
|
-
values: Row | undefined,
|
|
458
|
-
oldKeyValues: Row | undefined,
|
|
459
|
-
onConflict: ConflictResolution
|
|
460
|
-
): Promise<Row | undefined> {
|
|
461
|
-
if (!values || !oldKeyValues) {
|
|
462
|
-
throw new QuereusError("UPDATE requires new values and old key values.", StatusCode.MISUSE);
|
|
463
|
-
}
|
|
464
|
-
|
|
465
|
-
// Validate and parse values according to column types
|
|
466
|
-
const schema = targetLayer.getSchema();
|
|
467
|
-
const validatedRow: Row = values.map((value, index) => {
|
|
468
|
-
if (index >= schema.columns.length) {
|
|
469
|
-
throw new QuereusError(
|
|
470
|
-
`Too many values for UPDATE on ${this._tableName}: expected ${schema.columns.length}, got ${values.length}`,
|
|
471
|
-
StatusCode.ERROR
|
|
472
|
-
);
|
|
473
|
-
}
|
|
474
|
-
const column = schema.columns[index];
|
|
475
|
-
return validateAndParse(value, column.logicalType, column.name);
|
|
476
|
-
});
|
|
477
|
-
|
|
478
|
-
const newRowData: Row = validatedRow;
|
|
479
|
-
const targetPrimaryKey = buildPrimaryKeyFromValues(oldKeyValues, schema.primaryKeyDefinition);
|
|
480
|
-
const oldRowData = this.lookupEffectiveRow(targetPrimaryKey, targetLayer);
|
|
481
|
-
|
|
482
|
-
if (!oldRowData) {
|
|
483
|
-
if (onConflict === ConflictResolution.IGNORE) return undefined;
|
|
484
|
-
logger.warn('UPDATE', this._tableName, 'Target row not found', {
|
|
485
|
-
primaryKey: oldKeyValues.join(',')
|
|
486
|
-
});
|
|
487
|
-
return undefined;
|
|
488
|
-
}
|
|
489
|
-
|
|
490
|
-
const newPrimaryKey = this.primaryKeyFromRow(newRowData);
|
|
491
|
-
const isPrimaryKeyChanged = this.comparePrimaryKeys(targetPrimaryKey, newPrimaryKey) !== 0;
|
|
492
|
-
|
|
493
|
-
if (isPrimaryKeyChanged) {
|
|
494
|
-
return this.performUpdateWithPrimaryKeyChange(targetLayer, targetPrimaryKey, newPrimaryKey, oldRowData, newRowData, onConflict);
|
|
495
|
-
} else {
|
|
496
|
-
targetLayer.recordUpsert(targetPrimaryKey, newRowData, oldRowData);
|
|
497
|
-
return newRowData;
|
|
498
|
-
}
|
|
499
|
-
}
|
|
500
|
-
|
|
501
|
-
private performUpdateWithPrimaryKeyChange(
|
|
502
|
-
targetLayer: TransactionLayer,
|
|
503
|
-
oldPrimaryKey: BTreeKeyForPrimary,
|
|
504
|
-
newPrimaryKey: BTreeKeyForPrimary,
|
|
505
|
-
oldRowData: Row,
|
|
506
|
-
newRowData: Row,
|
|
507
|
-
onConflict: ConflictResolution
|
|
508
|
-
): Row | undefined {
|
|
509
|
-
const existingRowAtNewKey = this.lookupEffectiveRow(newPrimaryKey, targetLayer);
|
|
510
|
-
|
|
511
|
-
if (existingRowAtNewKey !== null) {
|
|
512
|
-
if (onConflict === ConflictResolution.IGNORE) return undefined;
|
|
513
|
-
throw new ConstraintError(`UNIQUE constraint failed on new PK for ${this._tableName}.`);
|
|
514
|
-
}
|
|
515
|
-
|
|
516
|
-
targetLayer.recordDelete(oldPrimaryKey, oldRowData);
|
|
517
|
-
targetLayer.recordUpsert(newPrimaryKey, newRowData, null);
|
|
518
|
-
return newRowData;
|
|
519
|
-
}
|
|
520
|
-
|
|
521
|
-
private async performDelete(
|
|
522
|
-
targetLayer: TransactionLayer,
|
|
523
|
-
oldKeyValues: Row | undefined
|
|
524
|
-
): Promise<Row | undefined> {
|
|
525
|
-
if (!oldKeyValues) {
|
|
526
|
-
throw new QuereusError("DELETE requires key values.", StatusCode.MISUSE);
|
|
527
|
-
}
|
|
528
|
-
|
|
529
|
-
const schema = targetLayer.getSchema();
|
|
530
|
-
const targetPrimaryKey = buildPrimaryKeyFromValues(oldKeyValues, schema.primaryKeyDefinition);
|
|
531
|
-
const oldRowData = this.lookupEffectiveRow(targetPrimaryKey, targetLayer);
|
|
532
|
-
|
|
533
|
-
if (!oldRowData) return undefined;
|
|
534
|
-
|
|
535
|
-
targetLayer.recordDelete(targetPrimaryKey, oldRowData);
|
|
536
|
-
return oldRowData;
|
|
537
|
-
}
|
|
538
|
-
|
|
539
|
-
public renameTable(newName: string): void {
|
|
540
|
-
logger.operation('Rename Table', this._tableName, { newName });
|
|
541
|
-
this._tableName = newName;
|
|
542
|
-
}
|
|
543
|
-
|
|
544
|
-
// --- Schema Operations (simplified with inherited BTrees) ---
|
|
545
|
-
async addColumn(columnDefAst: ASTColumnDef): Promise<void> {
|
|
546
|
-
if (this.isReadOnly) throw new QuereusError(`Table '${this._tableName}' is read-only`, StatusCode.READONLY);
|
|
547
|
-
const lockKey = `MemoryTable.SchemaChange:${this.schemaName}.${this._tableName}`;
|
|
548
|
-
const release = await Latches.acquire(lockKey);
|
|
549
|
-
const originalManagerSchema = this.tableSchema;
|
|
550
|
-
try {
|
|
551
|
-
await this.ensureSchemaChangeSafety();
|
|
552
|
-
|
|
553
|
-
// Get default nullability setting from database options
|
|
554
|
-
const defaultNullability = this.db.options.getStringOption('default_column_nullability');
|
|
555
|
-
const defaultNotNull = defaultNullability === 'not_null';
|
|
556
|
-
|
|
557
|
-
const newColumnSchema = columnDefToSchema(columnDefAst, defaultNotNull);
|
|
558
|
-
if (this.tableSchema.columns.some(c => c.name.toLowerCase() === newColumnSchema.name.toLowerCase())) {
|
|
559
|
-
throw new QuereusError(`Duplicate column name: ${newColumnSchema.name}`, StatusCode.ERROR);
|
|
560
|
-
}
|
|
561
|
-
let defaultValue: SqlValue = null;
|
|
562
|
-
const defaultConstraint = columnDefAst.constraints.find(c => c.type === 'default');
|
|
563
|
-
if (defaultConstraint && defaultConstraint.expr) {
|
|
564
|
-
if (defaultConstraint.expr.type === 'literal') {
|
|
565
|
-
defaultValue = getSyncLiteral(defaultConstraint.expr as LiteralExpr);
|
|
566
|
-
} else {
|
|
567
|
-
logger.warn('Add Column', this._tableName, 'Default for new col is expr; existing rows get NULL.', { columnName: newColumnSchema.name });
|
|
568
|
-
}
|
|
569
|
-
}
|
|
570
|
-
// Check for NOT NULL constraint (could be explicit or from default behavior)
|
|
571
|
-
if (newColumnSchema.notNull && defaultValue === null && !(defaultConstraint?.expr?.type ==='literal')) {
|
|
572
|
-
throw new QuereusError(`Cannot add NOT NULL col '${newColumnSchema.name}' without DEFAULT.`, StatusCode.CONSTRAINT);
|
|
573
|
-
}
|
|
574
|
-
const updatedColumnsSchema: ReadonlyArray<ColumnSchema> = Object.freeze([...this.tableSchema.columns, newColumnSchema]);
|
|
575
|
-
const finalNewTableSchema: TableSchema = Object.freeze({
|
|
576
|
-
...this.tableSchema,
|
|
577
|
-
columns: updatedColumnsSchema,
|
|
578
|
-
columnIndexMap: buildColumnIndexMap(updatedColumnsSchema),
|
|
579
|
-
});
|
|
580
|
-
this.baseLayer.updateSchema(finalNewTableSchema);
|
|
581
|
-
await this.baseLayer.addColumnToBase(newColumnSchema, defaultValue);
|
|
582
|
-
this.tableSchema = finalNewTableSchema;
|
|
583
|
-
this.initializePrimaryKeyFunctions();
|
|
584
|
-
logger.operation('Add Column', this._tableName, { columnName: newColumnSchema.name });
|
|
585
|
-
} catch (e: any) {
|
|
586
|
-
this.baseLayer.updateSchema(originalManagerSchema);
|
|
587
|
-
this.tableSchema = originalManagerSchema;
|
|
588
|
-
this.initializePrimaryKeyFunctions();
|
|
589
|
-
logger.error('Add Column', this._tableName, e);
|
|
590
|
-
throw e;
|
|
591
|
-
} finally {
|
|
592
|
-
release();
|
|
593
|
-
}
|
|
594
|
-
}
|
|
595
|
-
|
|
596
|
-
async dropColumn(columnName: string): Promise<void> {
|
|
597
|
-
if (this.isReadOnly) throw new QuereusError(`Table '${this._tableName}' is read-only`, StatusCode.READONLY);
|
|
598
|
-
const lockKey = `MemoryTable.SchemaChange:${this.schemaName}.${this._tableName}`;
|
|
599
|
-
const release = await Latches.acquire(lockKey);
|
|
600
|
-
const originalManagerSchema = this.tableSchema;
|
|
601
|
-
try {
|
|
602
|
-
await this.ensureSchemaChangeSafety();
|
|
603
|
-
const oldNameLower = columnName.toLowerCase();
|
|
604
|
-
const colIndex = this.tableSchema.columns.findIndex(c => c.name.toLowerCase() === oldNameLower);
|
|
605
|
-
if (colIndex === -1) throw new QuereusError(`Column '${columnName}' not found.`, StatusCode.ERROR);
|
|
606
|
-
if (this.tableSchema.primaryKeyDefinition.some(def => def.index === colIndex)) {
|
|
607
|
-
throw new QuereusError(`Cannot drop PK column "${columnName}".`, StatusCode.CONSTRAINT);
|
|
608
|
-
}
|
|
609
|
-
|
|
610
|
-
const updatedColumnsSchema = this.tableSchema.columns.filter((_, idx) => idx !== colIndex);
|
|
611
|
-
const updatedPkDefinition = this.tableSchema.primaryKeyDefinition.map(def => ({
|
|
612
|
-
...def, index: def.index > colIndex ? def.index - 1 : def.index
|
|
613
|
-
})).filter(def => def.index !== colIndex);
|
|
614
|
-
const updatedPrimaryKeyNames = updatedPkDefinition.map(def => updatedColumnsSchema[def.index]?.name).filter(Boolean) as string[];
|
|
615
|
-
|
|
616
|
-
const updatedIndexes = (this.tableSchema.indexes || []).map(idx => ({
|
|
617
|
-
...idx,
|
|
618
|
-
columns: idx.columns
|
|
619
|
-
.map(ic => ({ ...ic, index: ic.index > colIndex ? ic.index - 1 : ic.index }))
|
|
620
|
-
.filter(ic => ic.index !== colIndex)
|
|
621
|
-
})).filter(idx => idx.columns.length > 0);
|
|
622
|
-
|
|
623
|
-
const finalNewTableSchema: TableSchema = Object.freeze({
|
|
624
|
-
...this.tableSchema,
|
|
625
|
-
columns: Object.freeze(updatedColumnsSchema),
|
|
626
|
-
columnIndexMap: buildColumnIndexMap(updatedColumnsSchema),
|
|
627
|
-
primaryKeyDefinition: Object.freeze(updatedPkDefinition),
|
|
628
|
-
primaryKey: Object.freeze(updatedPrimaryKeyNames),
|
|
629
|
-
indexes: Object.freeze(updatedIndexes)
|
|
630
|
-
});
|
|
631
|
-
|
|
632
|
-
this.baseLayer.updateSchema(finalNewTableSchema);
|
|
633
|
-
await this.baseLayer.dropColumnFromBase(colIndex);
|
|
634
|
-
this.tableSchema = finalNewTableSchema;
|
|
635
|
-
this.initializePrimaryKeyFunctions();
|
|
636
|
-
logger.operation('Drop Column', this._tableName, { columnName });
|
|
637
|
-
} catch (e: any) {
|
|
638
|
-
this.baseLayer.updateSchema(originalManagerSchema);
|
|
639
|
-
this.tableSchema = originalManagerSchema;
|
|
640
|
-
this.initializePrimaryKeyFunctions();
|
|
641
|
-
logger.error('Drop Column', this._tableName, e);
|
|
642
|
-
throw e;
|
|
643
|
-
} finally {
|
|
644
|
-
release();
|
|
645
|
-
}
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
async renameColumn(oldName: string, newColumnDefAst: ASTColumnDef): Promise<void> {
|
|
649
|
-
if (this.isReadOnly) throw new QuereusError(`Table '${this._tableName}' is read-only`, StatusCode.READONLY);
|
|
650
|
-
const lockKey = `MemoryTable.SchemaChange:${this.schemaName}.${this._tableName}`;
|
|
651
|
-
const release = await Latches.acquire(lockKey);
|
|
652
|
-
const originalManagerSchema = this.tableSchema;
|
|
653
|
-
try {
|
|
654
|
-
await this.ensureSchemaChangeSafety();
|
|
655
|
-
const oldNameLower = oldName.toLowerCase();
|
|
656
|
-
const newColumnName = newColumnDefAst.name;
|
|
657
|
-
const newNameLower = newColumnName.toLowerCase();
|
|
658
|
-
const colIndex = this.tableSchema.columns.findIndex(c => c.name.toLowerCase() === oldNameLower);
|
|
659
|
-
if (colIndex === -1) throw new QuereusError(`Column '${oldName}' not found.`, StatusCode.ERROR);
|
|
660
|
-
if (oldNameLower !== newNameLower && this.tableSchema.columns.some((c, i) => i !== colIndex && c.name.toLowerCase() === newNameLower)) {
|
|
661
|
-
throw new QuereusError(`Target name '${newColumnName}' already exists.`, StatusCode.ERROR);
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
// Get default nullability setting from database options
|
|
665
|
-
const defaultNullability = this.db.options.getStringOption('default_column_nullability');
|
|
666
|
-
const defaultNotNull = defaultNullability === 'not_null';
|
|
667
|
-
|
|
668
|
-
const newColumnSchemaAtIndex = columnDefToSchema(newColumnDefAst, defaultNotNull);
|
|
669
|
-
const updatedCols = this.tableSchema.columns.map((c, i) => i === colIndex ? newColumnSchemaAtIndex : c);
|
|
670
|
-
const updatedIndexes = (this.tableSchema.indexes || []).map(idx => ({
|
|
671
|
-
...idx,
|
|
672
|
-
columns: idx.columns.map(ic =>
|
|
673
|
-
ic.index === colIndex ? { ...ic, name: newColumnName } : ic
|
|
674
|
-
)
|
|
675
|
-
}));
|
|
676
|
-
|
|
677
|
-
const finalNewTableSchema: TableSchema = Object.freeze({
|
|
678
|
-
...this.tableSchema,
|
|
679
|
-
columns: Object.freeze(updatedCols),
|
|
680
|
-
columnIndexMap: buildColumnIndexMap(updatedCols),
|
|
681
|
-
primaryKeyDefinition: Object.freeze(this.tableSchema.primaryKeyDefinition),
|
|
682
|
-
indexes: Object.freeze(updatedIndexes),
|
|
683
|
-
});
|
|
684
|
-
|
|
685
|
-
this.baseLayer.updateSchema(finalNewTableSchema);
|
|
686
|
-
await this.baseLayer.handleColumnRename();
|
|
687
|
-
this.tableSchema = finalNewTableSchema;
|
|
688
|
-
this.initializePrimaryKeyFunctions();
|
|
689
|
-
logger.operation('Rename Column', this._tableName, { oldName, newName: newColumnName });
|
|
690
|
-
} catch (e: any) {
|
|
691
|
-
this.baseLayer.updateSchema(originalManagerSchema);
|
|
692
|
-
this.tableSchema = originalManagerSchema;
|
|
693
|
-
this.initializePrimaryKeyFunctions();
|
|
694
|
-
logger.error('Rename Column', this._tableName, e);
|
|
695
|
-
throw e;
|
|
696
|
-
} finally {
|
|
697
|
-
release();
|
|
698
|
-
}
|
|
699
|
-
}
|
|
700
|
-
|
|
701
|
-
async createIndex(newIndexSchemaEntry: IndexSchema, ifNotExistsFromAst?: boolean): Promise<void> {
|
|
702
|
-
if (this.isReadOnly) throw new QuereusError(`Table '${this._tableName}' is read-only`, StatusCode.READONLY);
|
|
703
|
-
const lockKey = `MemoryTable.SchemaChange:${this.schemaName}.${this._tableName}`;
|
|
704
|
-
const release = await Latches.acquire(lockKey);
|
|
705
|
-
const originalManagerSchema = this.tableSchema;
|
|
706
|
-
try {
|
|
707
|
-
await this.ensureSchemaChangeSafety();
|
|
708
|
-
|
|
709
|
-
const indexName = newIndexSchemaEntry.name;
|
|
710
|
-
if (this.tableSchema.indexes?.some(idx => idx.name.toLowerCase() === indexName.toLowerCase())) {
|
|
711
|
-
if (!ifNotExistsFromAst) {
|
|
712
|
-
throw new QuereusError(`Index '${indexName}' already exists on table '${this._tableName}'.`, StatusCode.ERROR);
|
|
713
|
-
}
|
|
714
|
-
logger.operation('Create Index', this._tableName, 'Index already exists, IF NOT EXISTS specified. Skipping creation.');
|
|
715
|
-
return;
|
|
716
|
-
}
|
|
717
|
-
|
|
718
|
-
for (const iCol of newIndexSchemaEntry.columns) {
|
|
719
|
-
if (iCol.index < 0 || iCol.index >= this.tableSchema.columns.length) {
|
|
720
|
-
throw new QuereusError(`Column index ${iCol.index} for index '${indexName}' is out of bounds for table '${this._tableName}'.`, StatusCode.ERROR);
|
|
721
|
-
}
|
|
722
|
-
}
|
|
723
|
-
|
|
724
|
-
const finalNewTableSchema: TableSchema = Object.freeze({
|
|
725
|
-
...this.tableSchema,
|
|
726
|
-
indexes: Object.freeze([...(this.tableSchema.indexes || []), newIndexSchemaEntry])
|
|
727
|
-
});
|
|
728
|
-
|
|
729
|
-
this.baseLayer.updateSchema(finalNewTableSchema);
|
|
730
|
-
await this.baseLayer.addIndexToBase(newIndexSchemaEntry);
|
|
731
|
-
|
|
732
|
-
this.tableSchema = finalNewTableSchema;
|
|
733
|
-
logger.operation('Create Index', this._tableName, { indexName });
|
|
734
|
-
} catch (e: any) {
|
|
735
|
-
this.baseLayer.updateSchema(originalManagerSchema);
|
|
736
|
-
this.tableSchema = originalManagerSchema;
|
|
737
|
-
logger.error('Create Index', this._tableName, e);
|
|
738
|
-
throw e;
|
|
739
|
-
} finally {
|
|
740
|
-
release();
|
|
741
|
-
}
|
|
742
|
-
}
|
|
743
|
-
|
|
744
|
-
async dropIndex(indexName: string, ifExists?: boolean): Promise<void> {
|
|
745
|
-
if (this.isReadOnly) throw new QuereusError(`Table '${this._tableName}' is read-only`, StatusCode.READONLY);
|
|
746
|
-
const lockKey = `MemoryTable.SchemaChange:${this.schemaName}.${this._tableName}`;
|
|
747
|
-
const release = await Latches.acquire(lockKey);
|
|
748
|
-
const originalManagerSchema = this.tableSchema;
|
|
749
|
-
try {
|
|
750
|
-
await this.ensureSchemaChangeSafety();
|
|
751
|
-
const indexNameLower = indexName.toLowerCase();
|
|
752
|
-
const indexExists = this.tableSchema.indexes?.some(idx => idx.name.toLowerCase() === indexNameLower);
|
|
753
|
-
if (!indexExists) {
|
|
754
|
-
if (ifExists) {
|
|
755
|
-
logger.operation('Drop Index', this._tableName, 'Index not on table, IF EXISTS. Skipping.');
|
|
756
|
-
return;
|
|
757
|
-
}
|
|
758
|
-
throw new QuereusError(`Index '${indexName}' not on table '${this._tableName}'.`, StatusCode.ERROR);
|
|
759
|
-
}
|
|
760
|
-
const finalNewTableSchema: TableSchema = Object.freeze({
|
|
761
|
-
...this.tableSchema,
|
|
762
|
-
indexes: Object.freeze((this.tableSchema.indexes || []).filter(idx => idx.name.toLowerCase() !== indexNameLower))
|
|
763
|
-
});
|
|
764
|
-
this.baseLayer.updateSchema(finalNewTableSchema);
|
|
765
|
-
await this.baseLayer.dropIndexFromBase(indexName);
|
|
766
|
-
this.tableSchema = finalNewTableSchema;
|
|
767
|
-
logger.operation('Drop Index', this._tableName, { indexName });
|
|
768
|
-
} catch (e: any) {
|
|
769
|
-
this.baseLayer.updateSchema(originalManagerSchema);
|
|
770
|
-
this.tableSchema = originalManagerSchema;
|
|
771
|
-
logger.error('Drop Index', this._tableName, e);
|
|
772
|
-
throw e;
|
|
773
|
-
} finally {
|
|
774
|
-
release();
|
|
775
|
-
}
|
|
776
|
-
}
|
|
777
|
-
|
|
778
|
-
public planAppliesToKey(
|
|
779
|
-
plan: ScanPlan,
|
|
780
|
-
key: BTreeKey,
|
|
781
|
-
keyComparator: (a: BTreeKey, b: BTreeKey) => number
|
|
782
|
-
): boolean {
|
|
783
|
-
if (plan.equalityKey !== undefined) {
|
|
784
|
-
return keyComparator(key, plan.equalityKey) === 0;
|
|
785
|
-
}
|
|
786
|
-
const keyForBoundComparison = Array.isArray(key) ? key[0] : key;
|
|
787
|
-
if (plan.lowerBound && (keyForBoundComparison !== undefined && keyForBoundComparison !== null)) {
|
|
788
|
-
const cmp = compareSqlValues(keyForBoundComparison, plan.lowerBound.value);
|
|
789
|
-
if (cmp < 0 || (cmp === 0 && plan.lowerBound.op === IndexConstraintOp.GT)) return false;
|
|
790
|
-
}
|
|
791
|
-
if (plan.upperBound && (keyForBoundComparison !== undefined && keyForBoundComparison !== null)) {
|
|
792
|
-
const cmp = compareSqlValues(keyForBoundComparison, plan.upperBound.value);
|
|
793
|
-
if (cmp > 0 || (cmp === 0 && plan.upperBound.op === IndexConstraintOp.LT)) return false;
|
|
794
|
-
}
|
|
795
|
-
return true;
|
|
796
|
-
}
|
|
797
|
-
|
|
798
|
-
public async destroy(): Promise<void> {
|
|
799
|
-
const lockKey = `MemoryTable.Destroy:${this.schemaName}.${this._tableName}`;
|
|
800
|
-
const release = await Latches.acquire(lockKey);
|
|
801
|
-
try {
|
|
802
|
-
for (const connection of this.connections.values()) {
|
|
803
|
-
if (connection.pendingTransactionLayer) connection.rollback();
|
|
804
|
-
}
|
|
805
|
-
this.connections.clear();
|
|
806
|
-
this._currentCommittedLayer = this.baseLayer;
|
|
807
|
-
this.baseLayer = new BaseLayer(this.tableSchema);
|
|
808
|
-
logger.operation('Destroy', this._tableName, 'Manager destroyed and data cleared');
|
|
809
|
-
} finally {
|
|
810
|
-
release();
|
|
811
|
-
}
|
|
812
|
-
}
|
|
813
|
-
|
|
814
|
-
private async ensureSchemaChangeSafety(): Promise<void> {
|
|
815
|
-
if (this._currentCommittedLayer !== this.baseLayer) {
|
|
816
|
-
logger.warn('Schema Change', this._tableName, 'Transaction layers exist. Attempting to consolidate to base...');
|
|
817
|
-
|
|
818
|
-
// For schema changes, we need to consolidate all data into the base layer
|
|
819
|
-
// instead of just promoting layers
|
|
820
|
-
await this.consolidateToBaseLayer();
|
|
821
|
-
|
|
822
|
-
if (this._currentCommittedLayer !== this.baseLayer) {
|
|
823
|
-
throw new QuereusError(
|
|
824
|
-
`Cannot perform schema change on table ${this._tableName} while older transaction versions are in use by active connections. Commit/rollback active transactions and retry.`,
|
|
825
|
-
StatusCode.BUSY
|
|
826
|
-
);
|
|
827
|
-
}
|
|
828
|
-
}
|
|
829
|
-
|
|
830
|
-
// After ensuring we're at the base layer, update all connections to read from the base layer
|
|
831
|
-
// This is necessary because connections might still be reading from promoted/collapsed layers
|
|
832
|
-
for (const connection of this.connections.values()) {
|
|
833
|
-
if (connection.readLayer !== this.baseLayer) {
|
|
834
|
-
logger.debugLog(`[Schema Safety] Updating connection ${connection.connectionId} to read from base layer`);
|
|
835
|
-
connection.readLayer = this.baseLayer;
|
|
836
|
-
}
|
|
837
|
-
}
|
|
838
|
-
|
|
839
|
-
logger.debugLog(`Schema change safety check passed for ${this._tableName}. Current committed layer is base.`);
|
|
840
|
-
}
|
|
841
|
-
|
|
842
|
-
/** Consolidates all transaction data into the base layer for schema changes */
|
|
843
|
-
private async consolidateToBaseLayer(): Promise<void> {
|
|
844
|
-
const lockKey = `MemoryTable.Consolidate:${this.schemaName}.${this._tableName}`;
|
|
845
|
-
const release = await Latches.acquire(lockKey);
|
|
846
|
-
|
|
847
|
-
try {
|
|
848
|
-
logger.debugLog(`[Consolidate] Acquired lock for ${this._tableName}`);
|
|
849
|
-
|
|
850
|
-
// If current committed layer is a transaction layer, we need to merge its data into the base
|
|
851
|
-
if (this._currentCommittedLayer instanceof TransactionLayer && this._currentCommittedLayer.isCommitted()) {
|
|
852
|
-
const transactionLayer = this._currentCommittedLayer as TransactionLayer;
|
|
853
|
-
|
|
854
|
-
logger.debugLog(`[Consolidate] Copying data from transaction layer ${transactionLayer.getLayerId()} to base layer for ${this._tableName}`);
|
|
855
|
-
|
|
856
|
-
// Copy all data from the transaction layer to the base layer
|
|
857
|
-
await this.copyTransactionDataToBase(transactionLayer);
|
|
858
|
-
|
|
859
|
-
// Force all connections to read from the base layer
|
|
860
|
-
for (const conn of this.connections.values()) {
|
|
861
|
-
if (conn.readLayer === transactionLayer) {
|
|
862
|
-
logger.debugLog(`[Consolidate] Updating connection ${conn.connectionId} from transaction layer to base layer`);
|
|
863
|
-
conn.readLayer = this.baseLayer;
|
|
864
|
-
}
|
|
865
|
-
}
|
|
866
|
-
|
|
867
|
-
// Now we can set the base layer as the current committed layer
|
|
868
|
-
this._currentCommittedLayer = this.baseLayer;
|
|
869
|
-
logger.debugLog(`[Consolidate] CurrentCommittedLayer set to base for ${this._tableName}`);
|
|
870
|
-
}
|
|
871
|
-
} finally {
|
|
872
|
-
release();
|
|
873
|
-
logger.debugLog(`[Consolidate] Released lock for ${this._tableName}`);
|
|
874
|
-
}
|
|
875
|
-
}
|
|
876
|
-
|
|
877
|
-
/** Copies all data from a transaction layer to the base layer */
|
|
878
|
-
private async copyTransactionDataToBase(transactionLayer: TransactionLayer): Promise<void> {
|
|
879
|
-
const primaryTree = transactionLayer.getModificationTree('primary');
|
|
880
|
-
if (!primaryTree) return;
|
|
881
|
-
|
|
882
|
-
// Iterate through all entries in the transaction layer's primary tree
|
|
883
|
-
for (const path of primaryTree.ascending(primaryTree.first())) {
|
|
884
|
-
const row = primaryTree.at(path)!;
|
|
885
|
-
// Insert the row into the base layer
|
|
886
|
-
this.baseLayer.primaryTree.insert(row);
|
|
887
|
-
}
|
|
888
|
-
|
|
889
|
-
// Also need to rebuild secondary indexes in the base layer
|
|
890
|
-
await this.baseLayer.rebuildAllSecondaryIndexes();
|
|
891
|
-
}
|
|
892
|
-
|
|
893
|
-
// New method to abstract layer scanning
|
|
894
|
-
public async* scanLayer(layer: Layer, plan: ScanPlan): AsyncIterable<Row> {
|
|
895
|
-
if (layer instanceof TransactionLayer) {
|
|
896
|
-
const parentLayer = layer.getParent();
|
|
897
|
-
if (!parentLayer) {
|
|
898
|
-
throw new QuereusError("TransactionLayer encountered without a parent layer during scan.", StatusCode.INTERNAL);
|
|
899
|
-
}
|
|
900
|
-
// With inherited BTrees, scanning is much simpler - we just scan the layer's BTrees directly
|
|
901
|
-
// The inheritance is handled automatically by the BTree
|
|
902
|
-
yield* scanTransactionLayer(layer, plan, this.scanLayer(parentLayer, plan));
|
|
903
|
-
} else if (layer instanceof BaseLayer) {
|
|
904
|
-
yield* scanBaseLayer(layer, plan);
|
|
905
|
-
} else {
|
|
906
|
-
throw new QuereusError("Encountered an unknown layer type during scanLayer operation.", StatusCode.INTERNAL);
|
|
907
|
-
}
|
|
908
|
-
}
|
|
909
|
-
}
|