Skip to content

Commit

Permalink
sqlparser: Support sqlite 3.48
Browse files Browse the repository at this point in the history
  • Loading branch information
simolus3 committed Jan 21, 2025
1 parent bb7bcb0 commit 74ce705
Show file tree
Hide file tree
Showing 11 changed files with 43 additions and 40 deletions.
6 changes: 3 additions & 3 deletions drift_dev/lib/src/analysis/drift_native_functions.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,14 +44,14 @@ class _MoorFfiFunctions with ArgumentCountLinter implements FunctionHandler {

@override
ResolveResult inferArgumentType(
AnalysisContext context, SqlInvocation call, Expression argument) {
TypeInferenceSession session, SqlInvocation call, Expression argument) {
return const ResolveResult(
ResolvedType(type: BasicType.real, nullable: false));
}

@override
ResolveResult inferReturnType(AnalysisContext context, SqlInvocation call,
List<Typeable> expandedArgs) {
ResolveResult inferReturnType(TypeInferenceSession session,
SqlInvocation call, List<Typeable> expandedArgs) {
if (call.name == 'current_time_millis') {
return const ResolveResult(
ResolvedType(type: BasicType.int, nullable: false));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ class _CustomFunctions extends FunctionHandler {

@override
ResolveResult inferArgumentType(
AnalysisContext context, SqlInvocation call, Expression argument) {
TypeInferenceSession session, SqlInvocation call, Expression argument) {
final types = _functions[call.name.toLowerCase()]?.argumentTypes;
if (types == null) {
return const ResolveResult.unknown();
Expand All @@ -51,8 +51,8 @@ class _CustomFunctions extends FunctionHandler {
}

@override
ResolveResult inferReturnType(AnalysisContext context, SqlInvocation call,
List<Typeable> expandedArgs) {
ResolveResult inferReturnType(TypeInferenceSession session,
SqlInvocation call, List<Typeable> expandedArgs) {
final type = _functions[call.name.toLowerCase()]?.returnType;

return type != null ? ResolveResult(type) : const ResolveResult.unknown();
Expand Down
2 changes: 2 additions & 0 deletions sqlparser/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

- Replace `ColonNamedVariable` with `NamedVariable` for all named variables.
- Analysis support for SQLite 3.48.
- Fix nullability analysis around fts5 tables (enabled when raising the version
to 3.48 to preserve backwards-compatibility).

## 0.40.0

Expand Down
1 change: 1 addition & 0 deletions sqlparser/lib/sqlparser.dart
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
library;

export 'src/analysis/analysis.dart';
export 'src/analysis/types/types.dart';
export 'src/analysis/types/join_analysis.dart';
export 'src/ast/ast.dart';
export 'src/engine/module/dbstat.dart' show DbStatExtension;
Expand Down
6 changes: 3 additions & 3 deletions sqlparser/lib/src/analysis/types/resolving_visitor.dart
Original file line number Diff line number Diff line change
Expand Up @@ -578,7 +578,7 @@ class TypeResolver extends RecursiveVisitor<TypeExpectation, void> {

final extensionHandler = _functionHandlerFor(e);
if (extensionHandler != null) {
return extensionHandler.inferReturnType(session.context, e, params);
return extensionHandler.inferReturnType(session, e, params);
}

session.context.reportError(AnalysisError(
Expand Down Expand Up @@ -828,8 +828,8 @@ class TypeResolver extends RecursiveVisitor<TypeExpectation, void> {

final expressionArgument = arg;

final result = extensionHandler.inferArgumentType(
session.context, e, expressionArgument);
final result =
extensionHandler.inferArgumentType(session, e, expressionArgument);
final type = result.type;
if (type != null) {
session._markTypeResolved(expressionArgument, type);
Expand Down
11 changes: 8 additions & 3 deletions sqlparser/lib/src/engine/module/fts5.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import 'package:sqlparser/sqlparser.dart';
import 'package:sqlparser/src/analysis/types/types.dart';

class Fts5Extension implements Extension {
const Fts5Extension();
Expand Down Expand Up @@ -153,7 +154,7 @@ class _Fts5Functions with ArgumentCountLinter implements FunctionHandler {

@override
ResolveResult inferArgumentType(
AnalysisContext context, SqlInvocation call, Expression argument) {
TypeInferenceSession session, SqlInvocation call, Expression argument) {
int? argumentIndex;
if (call.parameters is ExprFunctionParameters) {
argumentIndex = (call.parameters as ExprFunctionParameters)
Expand Down Expand Up @@ -196,13 +197,17 @@ class _Fts5Functions with ArgumentCountLinter implements FunctionHandler {
}

@override
ResolveResult inferReturnType(AnalysisContext context, SqlInvocation call,
List<Typeable> expandedArgs) {
ResolveResult inferReturnType(TypeInferenceSession session,
SqlInvocation call, List<Typeable> expandedArgs) {
switch (call.name.toLowerCase()) {
case 'bm25':
return const ResolveResult(ResolvedType(type: BasicType.real));
case 'highlight':
case 'snippet':
if (call is ExpressionInvocation) {
session.graph.addRelation(NullableIfSomeOtherIs(call, expandedArgs));
}

return const ResolveResult(
ResolvedType(type: BasicType.text, nullable: true));
}
Expand Down
10 changes: 3 additions & 7 deletions sqlparser/lib/src/engine/module/geopoly.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
import 'package:sqlparser/src/analysis/analysis.dart';
import 'package:sqlparser/src/ast/ast.dart';
import 'package:sqlparser/src/engine/module/module.dart';
import 'package:sqlparser/src/engine/sql_engine.dart';
import 'package:sqlparser/src/reader/tokenizer/token.dart';
import 'package:sqlparser/sqlparser.dart';

final class GeopolyExtension implements Extension {
const GeopolyExtension();
Expand Down Expand Up @@ -103,7 +99,7 @@ final class _GeopolyFunctionHandler extends FunctionHandler {

@override
ResolveResult inferArgumentType(
AnalysisContext context,
TypeInferenceSession session,
SqlInvocation call,
Expression argument,
) {
Expand Down Expand Up @@ -133,7 +129,7 @@ final class _GeopolyFunctionHandler extends FunctionHandler {

@override
ResolveResult inferReturnType(
AnalysisContext context,
TypeInferenceSession session,
SqlInvocation call,
List<Typeable> expandedArgs,
) {
Expand Down
6 changes: 3 additions & 3 deletions sqlparser/lib/src/engine/module/json1.dart
Original file line number Diff line number Diff line change
Expand Up @@ -60,13 +60,13 @@ class _Json1Functions implements FunctionHandler {

@override
ResolveResult inferArgumentType(
AnalysisContext context, SqlInvocation call, Expression argument) {
TypeInferenceSession session, SqlInvocation call, Expression argument) {
return const ResolveResult.unknown();
}

@override
ResolveResult inferReturnType(AnalysisContext context, SqlInvocation call,
List<Typeable> expandedArgs) {
ResolveResult inferReturnType(TypeInferenceSession session,
SqlInvocation call, List<Typeable> expandedArgs) {
final name = call.name.toLowerCase();

if (_returnStrings.contains(name)) {
Expand Down
13 changes: 4 additions & 9 deletions sqlparser/lib/src/engine/module/math.dart
Original file line number Diff line number Diff line change
@@ -1,9 +1,4 @@
import 'package:sqlparser/src/analysis/analysis.dart';
import 'package:sqlparser/src/ast/ast.dart';
import 'package:sqlparser/src/engine/options.dart';
import 'package:sqlparser/src/engine/sql_engine.dart';

import 'module.dart';
import 'package:sqlparser/sqlparser.dart';

/// A math extension providing functions built into sqlite.
///
Expand Down Expand Up @@ -73,13 +68,13 @@ class _MathFunctions extends FunctionHandler {

@override
ResolveResult inferArgumentType(
AnalysisContext context, SqlInvocation call, Expression argument) {
TypeInferenceSession session, SqlInvocation call, Expression argument) {
return const ResolveResult(ResolvedType(type: BasicType.real));
}

@override
ResolveResult inferReturnType(AnalysisContext context, SqlInvocation call,
List<Typeable> expandedArgs) {
ResolveResult inferReturnType(TypeInferenceSession session,
SqlInvocation call, List<Typeable> expandedArgs) {
if (_unaryToInt.contains(call.name.toLowerCase())) {
return const ResolveResult(ResolvedType(type: BasicType.int));
}
Expand Down
10 changes: 5 additions & 5 deletions sqlparser/lib/src/engine/module/module.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,26 +22,26 @@ abstract class FunctionHandler {
/// Resolve the return type of a function invocation.
///
/// The [call] refers to a function declared in [functionNames]. To provide
/// further analysis, the [context] may be used. To support function calls
/// further analysis, the [session] may be used. To support function calls
/// with a [StarFunctionParameter], [expandedArgs] contains the expanded
/// arguments from a `function(*)` call.
///
/// If resolving to a type isn't possible, implementations should return
/// [ResolveResult.unknown].
ResolveResult inferReturnType(
AnalysisContext context, SqlInvocation call, List<Typeable> expandedArgs);
ResolveResult inferReturnType(TypeInferenceSession session,
SqlInvocation call, List<Typeable> expandedArgs);

/// Resolve the type of an argument used in a function invocation.
///
/// The [call] refers to a function declared in [functionNames]. To provide
/// further analysis, the [context] may be used. This method should return
/// further analysis, the [session] may be used. This method should return
/// the inferred type of [argument], which is an argument passed to the
/// [call].
///
/// If resolving to a type isn't possible, implementations should return
/// [ResolveResult.unknown].
ResolveResult inferArgumentType(
AnalysisContext context, SqlInvocation call, Expression argument);
TypeInferenceSession session, SqlInvocation call, Expression argument);

/// Can optionally be used by implementations to provide [AnalysisError]s
/// from the [call].
Expand Down
12 changes: 8 additions & 4 deletions sqlparser/test/engine/module/fts5_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -185,8 +185,10 @@ void main() {

final select = result.root as SelectStatement;
final column = select.resolvedColumns!.singleWhere((c) => c.name == 'b');
expect(result.typeOf(column),
const ResolveResult(ResolvedType(type: BasicType.text)));
expect(
result.typeOf(column),
const ResolveResult(
ResolvedType(type: BasicType.text, nullable: true)));
});

test('return type of snippet()', () {
Expand All @@ -197,8 +199,10 @@ void main() {

final select = result.root as SelectStatement;
final column = select.resolvedColumns!.singleWhere((c) => c.name == 'b');
expect(result.typeOf(column),
const ResolveResult(ResolvedType(type: BasicType.text)));
expect(
result.typeOf(column),
const ResolveResult(
ResolvedType(type: BasicType.text, nullable: true)));
});
});

Expand Down

0 comments on commit 74ce705

Please sign in to comment.