Extended Syntax

Python Parser currently supports core Python syntax while incorporating some DolphinDB-specific extensions. It can seamlessly call DolphinDB's built-in functions, SQL statements, and metaprogramming.

SQL Programming

Similar to DolphinDB, Python Parser's SQL programming features distributed computing. The following table introduces SQL statements supported and unsupported by Python Parser. For details about each statement, please refer to SQL Reference.

SQL

Python Parser

select
create ×
alter ×
where
any/all ×
case ×
exec
order by
group by
interval
cgroup by
coalesce ×
context by
having
partition
pivot by
update ×
insert into ×
delete ×
drop ×
limit
sample
top
map
nullIf ×
union/union all ×
with ×
distinct ×
ej
sej
lj(left join)
lsj(left semijoin)
fj(full join)
cj
aj
wj
pwj
pj
inner join
join ×
hint keywords
is null ×
between ×
exists ×
in ×
like ×

Notes:

  • To call DolphinDB's built-in functions, it is necessary to use toddb() to convert Python objects into DolphinDB objects.

  • In Python Parser scripts other than SQL, pairs or sequences cannot be accessed through "a:b" or "a..b" and can only be accessed with functions pair or seq.

  • Both SQL and DolphinDB use "a:b" to specify a range.

Examples

import dolphindb as ddb
// Create a database
dbName="dfs://test"
if existsDatabase(dbName):
	dropDatabase(dbName)

// To call DolphinDB's built-in functions, it is necessary to use toddb() to convert Python objects into DolphinDB objects
db1=database("", ddb.HASH, [ddb.SYMBOL, 10].toddb())
db2=database("", ddb.LIST, ["A"+string(seq(1,10)), "B"+string(seq(1,10)), "C"+string(seq(1,10))].toddb())
db=database(dbName, ddb.COMPO, [db1, db2].toddb())

// Create a table
// In Python Parser scripts other than SQL, pairs or sequences cannot be accessed through "a:b" or "a..b" and can only be accessed with functions pair or seq
n=20
id=symbol(string(rand(uuid(), n)))
sym=rand(flatten(("A"+string(seq(1,10)), "B"+string(seq(1,10)), "C"+string(seq(1,10))).toddb()), n)
qty=rand(100, n)
t=table(id, sym, qty)

pt=db.createPartitionedTable(t, `pt, [`id, `sym].toddb()).append!(t)

// Make a query
// Both SQL and DolphinDB use "a:b" to specify a range
select top 0:5 * from pt where sym like "A%"
Output:
id sym qty
7cd8359b-e1f4-b8b0-a0e5-6f0c6e971132 A3 84
b3aafacc-525e-3459-252d-4a3b63bfc994 A9 82
96252fcb-80ec-4272-5113-205eb02dee87 A9 96
3816fa58-94a7-97c0-170a-9f9e79594567 A9 85
a12e999c-5c5b-a392-3865-3384b823f583 A3 45
114cd225-2ff5-25f5-98ce-84e4253215e5 A2 77

Metaprogramming

DolphinDB supports metacode for dynamic expression generation. A metacode block contains objects or expressions enclosed by angle brackets <>.

Python Parser shares the same coding conventions with DolphinDB, but their syntax differs. For example, the current version of Python Parser does not support some of DolphinDB-specific syntax, such as "1 2 3", "a:b", "a..b", and "func [1,2,3]".

eval(<1 + 3>)

Output: 4

sqlColAlias(<avg(PRC)>, `avgPRC)

Output: < avg(PRC) as avgPRC >

n=20
id=symbol(string(rand(uuid(), n)))
sym=rand(flatten(("A"+string(seq(1,10)), "B"+string(seq(1,10)), "C"+string(seq(1,10))).toddb()), n)
qty=rand(100, n)
t=table(id, sym, qty)
sql(select=sqlCol("*"), from=t, groupBy=sqlCol(`sym), groupFlag=0, limit=1)

Output: < select top 1 * from tf0746a0500000000 context by sym >

Reference: Metaprogramming.

timer Statement

The timer(n): statement calculates the execution time of the specified Python Parser statements. The parameter n is the number of executions.

def fib(n):
    xs = []
    a, b = 0, 1
    while a < n:
        xs.append(a)
        a, b = b, a + b
	return xs
timer(1000): fib(2000)

Output: Time elapsed: 108.709 ms

Reference: timer.

String Creation

In Python Parser, strings can be created not only with single quotes (') or double quotes (") as in Python but also with back quotes (`) as in DolphinDB.